Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Injection Threat with PHP Forms?

I was trying to demonstrate the need for prepared statements in PHP, but I came into a bit of a problem because PHP does some conveniently strange things with strings passed in through forms.

The statement I'm trying to "break" is a simple:

SELECT username FROM users WHERE username = '$username' AND password = '$password'

But doing something like passing in X' or TRUE;-- for the password doesn't actually work because PHP automatically escapes the single-quote and turns the statement into:

SELECT username FROM users WHERE username = '$username' AND password = 'X\' or TRUE;--'

And the password equality remains intact because of the escaped single-quote. Is this statement considered safe? How can it be broken, if not?

like image 481
Jengerer Avatar asked Aug 21 '26 10:08

Jengerer


1 Answers

It sounds like you have magic quotes turned on. I would definitely not consider that statement safe, as if you were to move it to a server running PHP 5.4, it would be completely vulnerable. Magic quotes has been a deprecated feature for a long time, and it can mess up other parts of the application, so don't rely on it.

To demonstrate, just set magic_quotes_gpc = Off in php.ini.

like image 151
Ry- Avatar answered Aug 23 '26 02:08

Ry-



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!