Drupal 7 pre auth sql injection and remote code execution
Drupal's expandArguments function built SQL placeholder names from array keys, so a non-integer key could break out of the IN clause and inject SQL. Because PDO allowed multi-queries, an unauthenticated attacker could insert, dump or modify data, and by inserting a crafted session could run PHP code.
- Fixed in 6 months
- Paid in 6 months
- Disclosed after 6 months
Motivation
I found a SQL Injection bug in Drupal < 7.32. Which can lead to a code execution.
You need not have any user or knowledge of the targeted site.
Since Drupal is used as they state by "millions of websites and applications" I thought about applying for this bug bounty.
The Bug
Drupal uses Prepared Statements to secure the SQL Querys from Injections. To handle IN statements they created a expandArguments function, which uses the Array keys to create names for the placeholders.
foreach ($data as $i => $value) {
[...]
$new_keys[$key . '_' . $i] = $value;
}
The function assumes that it is called with an array which has no keys. Example:
db_query("SELECT * FROM {users} where name IN (:name)", array(':name'=>array('user1','user2')));
Which results in this SQL Statement
SELECT * from users where name IN (:name_0, :name_1)
with the parameters name_0 = user1 and name_1 = user2.
The Problem occurs, if the array has keys, which are no integers. Example:
db_query("SELECT * FROM {users} where name IN (:name)", array(':name'=>array('test) -- ' => 'user1','test' => 'user2')));
this results in an exploitable SQL query:
SELECT * FROM users WHERE name IN (:name_test) -- , :name_test )
with parameters :name_test = user2.
Since Drupal uses PDO, multi-queries are allowed. So this SQL Injection can be used to insert arbitrary data in the database, dump or modify existing data or drop the whole database.
With the possibility to INSERT arbitrary data into the database an attacker can execute any PHP code through a manipulated Session and Drupal features with callbacks.
Advisory
CVE Information
The Common Vulnerabilities and Exposures project (cve.mitre.org) has assigned the name CVE-2014-3704 to this vulnerability.
Poc
I included two PoCs. The first creates one request to create a session which has Admin privileges (UserID 1). The second executes code with only one request and destroys the session afterwards to not create a new Database entry. Some parts of the Second PoC were discovered with help of my coworker Stefan Esser.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The expandArguments helper assumed array keys were plain integers and used them directly to build placeholder names, so a key containing SQL syntax broke out of the query.
Impact
An unauthenticated attacker could insert, dump or modify data, drop the database, and through a manipulated session execute arbitrary PHP code.
What the attacker needed
No account needed; target runs Drupal below 7.32
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Drupal is a popular website-building system. It tried to keep database queries safe by using placeholders, but a helper that handled lists of values trusted the names given to those values, so a crafted name slipped extra SQL into the query. An attacker with no account could then read or change the database, and even get code to run on the server.
What is SQL injection?
Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.