天天看點

Sql Server 批量替換被插入的木馬記錄

最近公司做的一個事業性質網站被黑客攻擊了,通過sql注入方式,把木馬注入了資料庫,整個MSSQL SERVER

的資料都被附加上惡意腳本了,最近找了找 批量替換被插入的木馬記錄,找到了一條好的語句,用處很大,僅僅使用十幾行遊智語句,把整個資料庫的所有表的惡

意木馬清除掉了,而且在Google搜尋到此記錄幾率很小,在此專門轉載一下!為了以後自己能找得到,也希望後人能得到幫助。

原文如下:

declare @t varchar(555),@c varchar(555) ,@inScript varchar(8000) 

set @inScript='惡意代碼' 

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 

exec('update ['+@t+'] set ['+@c+']=replace(cast(['+@c+'] as varchar(8000)),'''+@inScript+''','''')' ) 

end 

close table_cursor 

deallocate table_cursor;

徹底杜絕SQL注入

1.不要使用sa使用者連接配接資料庫 

2、建立一個public權限資料庫使用者,并用這個使用者通路資料庫 

3、[角色]去掉角色public對sysobjects與syscolumns對象的select通路權限 

4、[使用者]使用者名稱-> 右鍵-屬性-權限-在sysobjects與syscolumns上面打“×” 

5、通過以下代碼檢測(失敗表示權限正确,如能顯示出來則表明權限太高): 

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 @c 

END 

CLOSE Table_Cursor 

DEALLOCATE Table_Cursor