Two instance methods of SQL injection database repair _mssql

Source: Internet
Author: User
Tags rowcount sql injection

1. The first situation is the need to replace all the specified injection strings (replace only the injected string as empty)

Copy Code code as follows:

declare @delStr nvarchar (500)
Set @delStr = ' <script src=http://www.jb51.net/js/common.js></script> '--the field string injected here
/****************************************/
/********** The following are operational entities ************/
SET NOCOUNT ON
declare @tableName nvarchar (MB), @columnName nvarchar (m), @tbID int, @iRow int, @iResult int
declare @sql nvarchar (2000)
Set @iResult =0
DECLARE cur cursor FOR
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 FOR
Select name from syscolumns where Xtype in (231,167,239,175,) 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 ('% ' + @d Elstr + '% ', [' + @columnName + '])-1 + ' + ' SUBSTRING ([' + @columnName + '], PATINDEX ('% ' + @delStr + '% ', [' + @col Umnname + '] + ' + ' len (' + @delStr + '), datalength ([' + @columnName + ']) where [' + @columnName + '] like '% ' + @delS tr+ '% '
EXEC sp_executesql @sql
Set @iRow =@ @rowcount
Set @iResult = @iResult + @iRow
If @iRow >0
Begin
print ' table: ' + @tableName + ', column: ' + @columnName + ' updated ' +convert (varchar), @iRow) + ' record; '
End
FETCH NEXT from Cur1 into @columnName

End
Close Cur1
Deallocate Cur1
FETCH NEXT from cur into @tableName, @tbID
End
print ' Database tutorial total ' +convert (varchar, @iResult) + ' record updated!!! '
Close cur
Deallocate cur
SET NOCOUNT OFF

2. The second is to delete the injection to the beginning of the table and the end of it. (This method directly finds the starting position of the injection and deletes all the following)

Copy Code code as follows:

--recovery is injected into the database
--2013-09-26
declare @delStr nvarchar (500)
Set @delStr = ' </title><style>. '--the start sample of the injected field string, and the data after this position is injected data

/********** The following are operational entities ************/
SET NOCOUNT ON
declare @tableName nvarchar (MB), @columnName nvarchar (m), @tbID int, @iRow int, @iResult int
declare @sql nvarchar (2000)
Set @iResult =0
DECLARE cur cursor FOR
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 FOR
Select name from syscolumns where Xtype in (231,167,239,175,) 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 + '] Li Ke '% ' + @delStr + '% '
EXEC sp_executesql @sql
Set @iRow =@ @rowcount
Set @iResult = @iResult + @iRow
If @iRow >0
Begin
print ' table: ' + @tableName + ', column: ' + @columnName + ' updated ' +convert (varchar), @iRow) + ' record; '
End
FETCH NEXT from Cur1 into @columnName

End
Close Cur1
Deallocate Cur1
FETCH NEXT from cur into @tableName, @tbID
End
print ' Database tutorial total ' +convert (varchar, @iResult) + ' record updated!!! '
Close cur
Deallocate cur
SET NOCOUNT OFF

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.