SQL批量替換被插入的木馬記錄解析
SQL批量替換——木馬是罪魁禍?zhǔn)?/strong>
最近公司做的一個事業(yè)性質(zhì)網(wǎng)站被黑客攻擊了,通過SQL注入方式,把木馬注入了數(shù)據(jù)庫,整個MSSQL SERVER 的數(shù)據(jù)都被附加上惡意腳本了,最近找了找批量替換被插入的木馬記錄,找到了一條好的語句,用處很大,僅僅使用十幾行游標(biāo)語句,把整個數(shù)據(jù)庫的所有表的惡意木馬清除掉了,而且在Google搜索到此記錄幾率很?。?/p>
原文如下:
- 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+''','''')' )
- fetch next from table_cursor into @t,@c
- end
- close table_cursor
- deallocate table_cursor;
徹底杜絕SQL注入
1.不要使用sa用戶連接數(shù)據(jù)庫
2、新建一個public權(quán)限數(shù)據(jù)庫用戶,并用這個用戶訪問數(shù)據(jù)庫
3、[角色]去掉角色public對sysobjects與syscolumns對象的select訪問權(quán)限
4、[用戶]用戶名稱-> 右鍵-屬性-權(quán)限-在sysobjects與syscolumns上面打“×”
5、通過以下代碼檢測(失敗表示權(quán)限正確,如能顯示出來則表明權(quán)限太高):
- 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
- FETCH NEXT FROM Table_Cursor INTO @T,@C
- END
- CLOSE Table_Cursor
- DEALLOCATE Table_Cursor
木馬很可恨,它遍布于網(wǎng)絡(luò)的各個領(lǐng)域,數(shù)據(jù)庫的SQL注入是個典型的攻擊實力,使用批量替換被插入的木馬記錄中的語句刪除木馬的方法,希望你能學(xué)會。
【編輯推薦】