본문 바로가기

컴퓨터 엔지니어/DATABASE

Mass SQL Injection으로 인한 대량 악성코드 삭제용 스크립트

-- Mass SQL Injection으로 인한 대량 악성코드 삭제용 스크립트 2008.11
-- varchar, nvarchar 등 으로 형 변환되어 악성코드가 삽입된 경우에 한해 사용
-- 한국 마이크로소프트 제공

declare @tab varchar(255), @col varchar(255), @owner varchar(255), @type int
declare table_cursor cursor for
select so.name, sc.name, sc.xtype, su.name
from sysobjects so inner join syscolumns sc on so.id = sc.id
     inner join sysusers su on so.uid = su.uid
where so.xtype='u' and (sc.xtype=99 or sc.xtype=35 or sc.xtype=231 or sc.xtype=167)
open table_cursor
fetch next from table_cursor into @tab, @col, @type, @owner

while(@@fetch_status=0)
Begin
-- <<악성코드>> 부분을 실제 삭제하려는 악성코드로 수정해 주세요. (예: <script src=hxxp://attacker.tld></script>)
 if (@type = 35 or @type = 167)  -- varchar나 text 데이터 타입일 경우 (SQL2005 환경이라면 varchar(max) 사용)
    exec('update ' + @owner + '.[' + @tab + '] set [' + @col + '] = replace(convert(varchar(8000), [' + @col + ']), '‘<<악성코드>>'','''')')
 else                        -- nvarchar나 ntext 데이터 타입일 경우 (SQL2005 환경이라면 nvarchar(max) 사용)
     exec('update ' + @owner + '.[' + @tab + '] set [' + @col + '] = replace(convert(nvarchar(4000), [' + @col + ']), '‘<<악성코드>>'','''')')
 print '[' + @col + ']' + ' column of ' + @owner + '.' + @tab + ' has been updated.'
 fetch next from table_cursor into @tab, @col, @type, @owner
End
close table_cursor
deallocate table_cursor