-1

Suppose you have a webpage requires you to input your username and password for authentication.

The username name "abs" and the password is "1234abcd" - you gain entry to the your profile.

But my question is, why is that if I input "abs' --" in the username field and no password, it still returns my profile page?

What is happening behind the scenes with the server, SQL and user?

I just cant seem to understand this. Thanks guys for any help.

1 Answers1

0

You're apparently substituting the parameters into your SQL string without escaping them. The code presumably looks something like:

$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";

So after your inputs are substituted, the result is:

SELECT * FROM users WHERE username = 'abs' --' AND password = ''

The quote in the username you entered is terminating the string. Then the -- starts a comment, so the rest of the query is ignored. So you've transformed the query to just:

SELECT * FROM users WHERE username = 'abs'

and it no longer checks the password.

Barmar
  • 741,623
  • 53
  • 500
  • 612