The bad answer:
Specifically, can you think of any examples of SQL injection that
would succeed despite the input strings being escaped (using
mysql_escape_string)?
Not reliably. You are referring to mysql_escape_string()
, which does not take into account the connection encoding (while mysql_real_escape_string()
does).
So maybe a carefully crafted string, with a carefully crafted incomplete UTF8 codepoint in front, might result in, say, a quote sign being escaped by mysql_escape_string()
but the escape itself being ignored by MySQL since it will "see" it as an UTF8 character.
E.g.:
0xC2' OR 1=1 ;--
would be escaped by mysql_escape_string()
as
0xC2\' OR 1=1 ;--
which would be assembled to
WHERE password='0xC2\' OR 1=1 ;--';
and seen by MySQL (if the proper connection encoding was in effect) as, say,
WHERE password='€' OR 1=1 ;[--';] <-- the bracketed part is considered a comment and ignored
which would be a classic SQL injection.
But this plays on the fact that you specified, maybe through distraction, a doubly deprecated function. If you really were referring to mysql_real_escape_string()
, then it would not work.
Also, this assumes that neither the server, nor the application layer (e.g. PHP) employs any kind of charset validation when populating the input. If they did, the invalid UTF8 would be deleted on arrival, and never even be seen by mysql_escape_string
, which would then of course be enough.
The real answer:
Do not use mysql_escape_string
(or mysql_whatever
) at all. They've been deprecated and your code might stop working. Use PDO functions instead.