现在的位置: 首页 > 综合 > 正文

彻底清除SQL挂马的方法

2013年08月14日 ⁄ 综合 ⁄ 共 3025字 ⁄ 字号 评论关闭

        接到一个很有挑战性的任务:一个网站被Google报告有恶意网站,俺的任务就是将这几行字从Google上抹去!

        首先的反应是网页被嵌入了iframe的恶意脚本,但是仔细检查后发现并不存在这样的问题,反而是从Google的诊断上看,数据库里的内容比较可疑。

        下载下数据库一看,果然在所有设定长度较大的varchar字段和几乎所有的ntext字段上,都在最后被挂了诸如:<script scrr='xxx.com></script'>之类的代码。

        问题找到了,但是解决的方法却是不能接受的,一个个清除恶意代码的代价太大,不得已,写了一个遍历清除垃圾码的脚本 :
declare c_cursor cursor local for
 select a.name, b.name
 from sysobjects as a
 join syscolumns as b
  on a.id = b.id
 join systypes as c
  on c.xusertype = b.Xtype
 where
  a.xtype = 'U'and c.name in('ntext','nvarchar','nchar','varchar','char')

declare @tabname sysname,@colname sysname, @trash varchar(100)
select @trash = 'src=http://www.64do.com/script.js'
open c_cursor
fetch next from c_cursor into @tabname,@colname
while @@fetch_status = 0
 begin
 --print('update cn98003.'+@tabname+' set '+@colname+'=cn98003.f_Clean('+@colname+') '+'where '+@colname+' like '+'''%'+@trash+'%''')
 
 exec('update cn98003.'+@tabname+' set '+@colname+'=replace(replace(convert(varchar(8000),'+@colname+'),''<script src=http://www.64do.com/script.js></script>'',''''),''<script src=http://www.pkseio.ru/script.js></script>'','''') '+'where '+@colname+' like '+'''%'+@trash+'%''')

 fetch next from c_cursor into @tabname,@colname
 end
close c_cursor
deallocate c_cursor

执行该脚本,可以解决所有varchar类和长度小于8000的text类型的垃圾码,但是对于长度在 8000以上的ntext字段,由于类型转换的原因,需要改变方法:

declare @ptrval varbinary(16)

declare ct cursor local for
select articleid from cn98003.cv_product where location like '%64do%'

declare @id int
declare @offset int
open ct
fetch next from ct into @id
while @@fetch_status = 0
begin
 select @ptrval=textptr(location) from cn98003.cv_product where articleid = @id
 select @offset=patindex('%www.64do%',location) from cn98003.cv_product where articleid = @id

 updatetext cn98003.cv_product.location @ptrval @offset NULL ''
end
close ct
deallocate ct

经过处理后还原数据库,过了两天后google的恶意网站提醒解除 经过思考,我认为这种入侵其实是通过自动程序采用穷举的方法,其原理同样是sql注入,但是更加凶狠。为了防止sql注入,在asp网站所有文件include这个网页:

  1. '安全检查设置
  2. Dim ar_str,ar_qstr,str_index,qstr_index,strlist,strlist1
  3. strlist = "'|#|exec|insert|select|delete|update|%|chr|char|mid|master|truncate|declare|(|)|*|or|@|and|=|-" 
  4. strlist1 = "exec|insert|select|delete|update|truncate|declare|'" 
  5. If Request.Form <>"" Then 
  6. ar_str = split(strlist1,"|",-1,1) 
  7.     For Each qstr_index In Request.Form 
  8.         For str_index=0 To Ubound(ar_str) 
  9.             If Instr(LCase(Request.Form(qstr_index)),ar_str(str_index)) <>0 Then                
  10.             Response.Write " <Script Language=JavaScript>alert('请不要在参数中包含非法字符!');" 
  11.             Response.Write " alert('如有问题请与网络管理员联系!');" 
  12.             Response.write"javascript:history.go(-1) </SCRIPT>" 
  13.             Response.End 
  14.             End If 
  15.         Next 
  16.     Next 
  17. End If 
  18. If Request.QueryString <>"" Then
  19. ar_str = split(strlist,"|",-1,1) 
  20. For Each qstr_index In Request.QueryString
  21.     For str_index=0 To UBound(ar_str)
  22.         If Instr(1,LCase(Request.QueryString(qstr_index)),ar_str(str_index),1) <>0 Then  
  23.         Response.Write " <Script Language=JavaScript>alert('请不要在参数中包含非法字符!');" 
  24.         Response.Write " alert('如有问题请与网络管理员联系!');" 
  25.         Response.write"javascript:history.go(-1) </SCRIPT>" 
  26.         Response.End 
  27.         End if
  28.     Next
  29. Next 
  30. End If 

 

抱歉!评论已关闭.