Jump to content

Prevent SQL Injection by escaping?


resting

Recommended Posts

I seem to remember I'd read somewhere that we should escape all user input to prevent SQL injections. I did a test, but found no difference to it.

 

The query

$query ="SELECT * FROM login WHERE username = '$username' AND password = '$password'"

;

 

Top portion shows the unescaped results. Bottom the escaped results. Both queries returned results.

sql_injection.png

 

Any example to show what good can mysql_real_escape_string do?

Link to comment
https://forums.phpfreaks.com/topic/178289-prevent-sql-injection-by-escaping/
Share on other sites

On the first example without mysql_real_escape string you could easily enter ' or 1=1, which would make the query selected where the username and password are correct OR where 1=1 (which is everywhere, because it's always true).

 

The second example where you're using mysql_real_escape_string, instead of or 1=1 actually being processed it's included in the value for password. So in this case it's password='1 or 1=1'.

I the injection would have not been "1' OR '1=1", but rather:

1' OR '1'='1

Resulting in an unescaped result of:

SELECT * FROM login WHERE username = '1' AND password = '1' OR '1'='1'

 

Escaped:

SELECT * FROM login WHERE username = '1' AND password = '1\' OR \'1\'=\'1'

 

Try your example again with:

username: a username that works (I guess you used "1" before)

password: 1' OR '1'='1

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.