In PHP, I know that mysql_real_escape
is much safer than using addslashes
.
However, I could not find an example of a situation where addslashes
would let an SQL Injection happen.
Can anyone give some examples?
In PHP, I know that mysql_real_escape
is much safer than using addslashes
.
However, I could not find an example of a situation where addslashes
would let an SQL Injection happen.
Can anyone give some examples?
As an addition for the readers of the answers here: This MySQL bug has already been fixed:)
Also, it is always good practice to use prepared statements. It is the most exploit-free way you can fire queries (and, in several use cases the most performant). And it would have saved you from this flaw.
mysql_real_escape_string() versus Prepared Statements clearly explains mysql_real_escape_string() isn't 100% secure.
using mysql_set_charset('GBK') to replace mysql_query("SET CHARACTER SET 'GBK'"), the mysql_real_escape_string() can be 100% secure.
Well, here's the article you want.
Basically, the way the attack works is by getting
addslashes()
to put a backslash in the middle of a multibyte character such that the backslash loses its meaning by being part of a valid multibyte sequence.The general caveat from the article:
Chris Shiflett clearly explains with the bellow example, That will of-course work if you try it when using GBK encoding in your database. Even I tried it, this proves, there are chances for sql injection, even though they are very less, but someone with good knowledge and capability can easily inject. Here is an Example...
Although the use of addslashes() or magic_quotes_gpc would normally be considered as somewhat secure, the use of GBK would render them near useless. The following PHP cURL script would be able to make use of the injection, I hope this will help you a bit more to understand: