Two instance methods for SQL Injection database Restoration

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.