1. In the first case, replace all the specified injection strings (only the injected strings are empty)
Copy codeThe Code is as follows:
Declare @ delStr nvarchar (500)
Set @ delStr = '<script src = http://www.jb51.net/js/common.js> </script>' -- the injected field string
/*************************************** */
************/
Set nocount on
Declare @ tableName nvarchar (100), @ columnName nvarchar (100), @ tbID int, @ iRow int, @ iResult int
Declare @ SQL nvarchar (2000)
Set @ iResult = 0
Declare cur cursor
Select name, id from sysobjects where xtype = 'U'
Open cur
Fetch next from cur into @ tableName, @ tbID
While @ fetch_status = 0
Begin
Declare cur1 cursor
Select name from syscolumns where xtype in (231,167,239,175, 35, 99) and id = @ tbID
Open cur1
Fetch next from cur1 into @ columnName
While @ fetch_status = 0
Begin
Set @ SQL = 'Update ['+ @ tableName +'] set ['+ @ columnName +'] = SUBSTRING (['+ @ columnName +'], '+' 1, PATINDEX (''% '+ @ delStr +' %'', ['+ @ columnName +'])-1) + '+ 'substring ([' + @ columnName + '], PATINDEX (''%' + @ delStr + '%'', [' + @ columnName + ']) + '+ 'len (''' + @ delStr + '''), datalength ([' + @ columnName + ']) where ['+ @ columnName +'] like ''% '+ @ delStr +' % '''
Exec sp_executesql @ SQL
Set @ iRow = @ rowcount
Set @ iResult = @ iResult + @ iRow
If @ iRow> 0
Begin
Print 'table: '+ @ tableName +', column: '+ @ columnName +' updated '+ convert (varchar (10), @ iRow) + 'records ;'
End
Fetch next from cur1 into @ columnName
End
Close cur1
Deallocate cur1
Fetch next from cur into @ tableName, @ tbID
End
Print 'database tutorials total '+ convert (varchar (10), @ iResult) +' records updated !!! '
Close cur
Deallocate cur
Set nocount off
2. The second type is to delete the start position and end of the injection to the table. (This method directly finds the starting position of the injection and deletes all the following values)
Copy codeThe Code is as follows:
-- Restore the injected Database
-- 2013-09-26
Declare @ delStr nvarchar (500)
Set @ delStr = '</title> <style>.' -- sample the start of the injected field string. All the data after this position is injected with data.
************/
Set nocount on
Declare @ tableName nvarchar (100), @ columnName nvarchar (100), @ tbID int, @ iRow int, @ iResult int
Declare @ SQL nvarchar (2000)
Set @ iResult = 0
Declare cur cursor
Select name, id from sysobjects where xtype = 'U'
Open cur
Fetch next from cur into @ tableName, @ tbID
While @ fetch_status = 0
Begin
Declare cur1 cursor
Select name from syscolumns where xtype in (231,167,239,175, 35, 99) and id = @ tbID
Open cur1
Fetch next from cur1 into @ columnName
While @ fetch_status = 0
Begin
Set @ SQL = 'Update ['+ @ tableName +'] set ['+ @ columnName +'] =
SUBSTRING (['+ @ columnName +'], 1, PATINDEX (''% '+ @ delStr +' %'', ['+ @ columnName +'])-1) where ['+ @ columnName +'] like ''% '+ @ delStr +' % '''
Exec sp_executesql @ SQL
Set @ iRow = @ rowcount
Set @ iResult = @ iResult + @ iRow
If @ iRow> 0
Begin
Print 'table: '+ @ tableName +', column: '+ @ columnName +' updated '+ convert (varchar (10), @ iRow) + 'records ;'
End
Fetch next from cur1 into @ columnName
End
Close cur1
Deallocate cur1
Fetch next from cur into @ tableName, @ tbID
End
Print 'database tutorials total '+ convert (varchar (10), @ iResult) +' records updated !!! '
Close cur
Deallocate cur
Set nocount off