I have been trying to figure out how exactly \x00, \n, \r, \, or \x1a can cause an SQL Injection (as it is mentioned at http://nl3.php.net/manual/en/function.mysql-real-escape-string.php)
I understand the idea of single quote and double quotes, but how and why I need to take care of the other items to make my query safe?
I was wondering about the same question and I found the answer in the C API documentation of MySQL, it states:
It is also explained in String Literals that:
The NUL character represents the end of a string in C language, so this can falsely terminate the input argument of the mysql client program. Same thing for
\x1a
, it marks the end-of-file under Windows (trytype test.txt
in a command prompt with a\x1a
character in the middle of the file).The main point is that an admin can miss important information in a log file if his log file reader doesn't show the data beyond one of these characters. But who still uses precarious
type
command or equivalent under Windows to read a log file anyway?In other terms, there is no danger with
\n
,\r
,\0
or\x1a
in PHP, other than potentially making a log file difficult to read.As for the backslash, \' OR 1==1 would be converted to \\' OR 1==1 if it was not escaped too, cancelling the effect of the escaping of the quote.
let's assume you have
\ -> \:
try
\' or 1=1; --'
, after escaping the quote, you would get\\' or 1=1; --'
and the SQL would beselect * from mytable where myfield='\\' or 1=1; --'
\x00
Not important for PHP, but for C
Sorry, too lazy for the rest.