Let's say I have code like this:
$dbh = new PDO("blahblah"); $stmt = $dbh->prepare('SELECT * FROM users where username = :username'); $stmt->execute( array(':username' => $_REQUEST['username']) );
The PDO documentation says:
The parameters to prepared statements don't need to be quoted; the driver handles it for you.
Is that truly all I need to do to avoid SQL injections? Is it really that easy?
You can assume MySQL if it makes a difference. Also, I'm really only curious about the use of prepared statements against SQL injection. In this context, I don't care about XSS or other possible vulnerabilities.
The short answer is NO, PDO prepares will not defend you from all possible SQL-Injection attacks. For certain obscure edge-cases.
I'm adapting this answer to talk about PDO...
The long answer isn't so easy. It's based off an attack demonstrated here.
So, let's start off by showing the attack...
In certain circumstances, that will return more than 1 row. Let's dissect what's going on here:
Selecting a Character Set
For this attack to work, we need the encoding that the server's expecting on the connection both to encode
'as in ASCII i.e.
0x27and to have some character whose final byte is an ASCII
0x5c. As it turns out, there are 5 such encodings supported in MySQL 5.6 by default:
sjis. We'll select
Now, it's very important to note the use of
SET NAMEShere. This sets the character set ON THE SERVER. There is another way of doing it, but we'll get there soon enough.
The payload we're going to use for this injection starts with the byte sequence
gbk, that's an invalid multibyte character; in
latin1, it's the string
¿'. Note that in
0x27on its own is a literal
We have chosen this payload because, if we called
addslashes()on it, we'd insert an ASCII
0x5c, before the
'character. So we'd wind up with
0xbf5c27, which in
gbkis a two character sequence:
0x27. Or in other words, a valid character followed by an unescaped
'. But we're not using
addslashes(). So on to the next step...
The important thing to realize here is that PDO by default does NOT do true prepared statements. It emulates them (for MySQL). Therefore, PDO internally builds the query string, calling
mysql_real_escape_string()(the MySQL C API function) on each bound string value.
The C API call to
addslashes()in that it knows the connection character set. So it can perform the escaping properly for the character set that the server is expecting. However, up to this point, the client thinks that we're still using
latin1for the connection, because we never told it otherwise. We did tell the server we're using
gbk, but the client still thinks it's
Therefore the call to
mysql_real_escape_string()inserts the backslash, and we have a free hanging
'character in our "escaped" content! In fact, if we were to look at
gbkcharacter set, we'd see:
Which is exactly what the attack requires.
This part is just a formality, but here's the rendered query:
Congratulations, you just successfully attacked a program using PDO Prepared Statements...
The Simple Fix
Now, it's worth noting that you can prevent this by disabling emulated prepared statements:
This will usually result in a true prepared statement (i.e. the data being sent over in a separate packet from the query). However, be aware that PDO will silently fallback to emulating statements that MySQL can't prepare natively: those that it can are listed in the manual, but beware to select the appropriate server version).
The Correct Fix
The problem here is that we didn't call the C API's
SET NAMES. If we did, we'd be fine provided we are using a MySQL release since 2006.
If you're using an earlier MySQL release, then a bug in
mysql_real_escape_string()meant that invalid multibyte characters such as those in our payload were treated as single bytes for escaping purposes even if the client had been correctly informed of the connection encoding and so this attack would still succeed. The bug was fixed in MySQL 4.1.20, 5.0.22 and 5.1.11.
But the worst part is that
PDOdidn't expose the C API for
mysql_set_charset()until 5.3.6, so in prior versions it cannot prevent this attack for every possible command! It's now exposed as a DSN parameter, which should be used instead of
The Saving Grace
As we said at the outset, for this attack to work the database connection must be encoded using a vulnerable character set.
utf8mb4is not vulnerable and yet can support every Unicode character: so you could elect to use that instead—but it has only been available since MySQL 5.5.3. An alternative is
utf8, which is also not vulnerable and can support the whole of the Unicode Basic Multilingual Plane.
Alternatively, you can enable the
NO_BACKSLASH_ESCAPESSQL mode, which (amongst other things) alters the operation of
mysql_real_escape_string(). With this mode enabled,
0x27will be replaced with
0x5c27and thus the escaping process cannot create valid characters in any of the vulnerable encodings where they did not exist previously (i.e.
0xbf27etc.)—so the server will still reject the string as invalid. However, see @eggyal's answer for a different vulnerability that can arise from using this SQL mode (albeit not with PDO).
The following examples are safe:
Because the server's expecting
Because we've properly set the character set so the client and the server match.
Because we've turned off emulated prepared statements.
Because we've set the character set properly.
Because MySQLi does true prepared statements all the time.
You're 100% safe.
Otherwise, you're vulnerable even though you're using PDO Prepared Statements...
I've been slowly working on a patch to change the default to not emulate prepares for a future version of PHP. The problem that I'm running into is that a LOT of tests break when I do that. One problem is that emulated prepares will only throw syntax errors on execute, but true prepares will throw errors on prepare. So that can cause issues (and is part of the reason tests are borking).