I understood the issue perfectly.
My problem is that rather then the system confirming only 1 record is returned from the query (by checking the number of rows returned) you force it to return only 1 row with the limit statement. Hence, if i was to undertake an attack that bypassed the input sanitizing routine you wouldn't have a clue if 100 records or 1 record was returned. Thus you can't detect a injection problem with the system login ( = fail).
Then you go on to say that this can happen due to a truncation attack or no password being present... which results in multiple rows being returned, so it is better to limit it to a single row to avoid problems.
Let me ask Ronald, with your 10 years experience do you really think it is better to just return the first row found that matches our query (since we are relying on the DESC ordering clause) rather then verifying only a single row was returned from the query (i.e. data integrity)?
.....
Let me explain it better for the benefit of the thread
Example 1 (My version) with injection (no encryption).
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM USERS WHERE USERID='test' AND PASS='' or 1=1", $link);
$num_rows = mysql_num_rows($result);
if ($num_rows > 1){
//mulitple rows returned - log query for analysis - more then likely a hack attack or bad data
}elseif ($num_rows == 1){
//do login
}else {
//username - password incorrect
}
?>
Example 2 (Your version) with injection (no encryption).
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM USERS WHERE USERID='test' AND PASS='' or 1=1 LIMIT 1", $link);
if (!$result) {
//username -password incorrect
} else {
//do login
}
?>
As you can see the first example detects the injection, the second example using your method doesn't.
Of course, you could argue that the attacker only needs to add the limit clause to avoid detection... and whilst this is true, if someone with 10 years programming experience and your knowledge isn't aware of this basic injection detection technique then its a fair bet (most) hackers aren't either
----------
'Just because you got the bacon, lettuce, and tomato don't mean I'm gonna give you my toast.'