SQL Injection
SQL Injection
SQL injection refers to the act of someone inserting a MySQL statement to be run on your
database without your knowledge. Injection usually occurs when you ask a user for input, like their
name, and instead of a name they give you a MySQL statement that you will unknowingly run on your
database.
Display:
Normal: SELECT * FROM customers WHERE username = 'timmy'
Injection: SELECT * FROM customers WHERE username = '' OR 1''
The normal query is no problem, as our MySQL statement will just select everything from
customers that has a username equal to timmy.
However, the injection attack has actually made our query behave differently than we intended.
By using a single quote (') they have ended the string part of our MySQL query
username = ' '
and then added on to our WHERE statement with an OR clause of 1 (always true).
username = ' ' OR 1
This OR clause of 1 will always be true and so every single entry in the "customers" table would
be selected by this statement!
Display:
SELECT * FROM customers WHERE username = ' '; DELETE FROM customers WHERE 1 or username =
''
If you were run this query, then the injected DELETE statement would completely empty your
"customers" table. Now that you know this is a problem, how can you prevent it?
$name_bad = mysql_real_escape_string($name_bad);
$query_bad = "SELECT * FROM customers WHERE username = '$name_bad'";
echo "Escaped Bad Injection: <br />" . $query_bad . "<br />";
$name_evil = "'; DELETE FROM customers WHERE 1 or username = '";
$name_evil = mysql_real_escape_string($name_evil);
$query_evil = "SELECT * FROM customers WHERE username = '$name_evil'";
echo "Escaped Evil Injection: <br />" . $query_evil;
Display:
Escaped Bad Injection:
SELECT * FROM customers WHERE username = '\' OR 1\''
Escaped Evil Injection:
SELECT * FROM customers WHERE username = '\'; DELETE FROM customers WHERE 1 or username
= \''
Notice that those evil quotes have been escaped with a backslash \, preventing the injection
attack. Now all these queries will do is try to find a username that is just completely ridiculous:
Bad: \' OR 1\'
Evil: \'; DELETE FROM customers WHERE 1 or username = \'
And I don't think we have to worry about those silly usernames getting access to our MySQL
database. So please do use the handy mysql_real_escape_string()function to help prevent SQL
Injection attacks on your websites. You have no excuse not to use it after reading this lesson!
After checking their balance they browse away from the site (without logging off) and start reading
web pages about olives from Madagascar. One of these olive sites is owned by an attacker. The
attacker's website has the following <img> tag:
<img src="http://bank.com/transfer.asp?to_acct=445544&amount=1000">
When the victim's browser loads the malicious page that contains this img tag, the victims browser
makes the transfer request/transfer.asp?to_acct=445544&amount=1000 to bank.com using the
authenticated cookie from the earlier session. Upon making this request, the bank then transfers
$1,000 from the victim's account to account 445544. The attacker has now successfully executed a
cross-site request forgery attack against a user of bank.com
http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php