悉远

 找回密码
 注册
搜索
查看: 1236|回复: 1

数据库被挂马的ASP处理方法

[复制链接]
发表于 2009-6-23 22:10:37 | 显示全部楼层 |阅读模式
数据库被挂马我相信很多人都碰到过。在这里,我讲下我处理的方法。
第一步:为现有数据库做好备份。
第二步:
执行如下ASP文件,这样就可以去掉数据库当中的JS木马:
注:conn.asp自己去写了。

程序代码
  1. '这里放入JS木马内容:请记得改为自己数据库中的JS木马内容。
  2. <!--#include file="conn.asp"-->
  3. <%
  4. Server.ScriptTimeOut=180
  5. Set rstSchema = conn.OpenSchema(20)
  6. k=1
  7. Do Until rstSchema.EOF '遍历数据库表
  8.     If rstSchema("TABLE_TYPE")="TABLE" Then
  9.         response.write K&".<font color=red><b>"&rstSchema("TABLE_NAME") & "</b></font>:" '显示表名
  10.         Set rs=Server.CreateObject("ADODB.Recordset")
  11.         sql="select * from [" & rstSchema("TABLE_NAME")&"]"
  12.         rs.open sql,conn,1,3
  13.         For i=0 to rs.fields.count-1 '遍历表中字段
  14.            If int(rs(i).Type)=129 or int(rs(i).Type)=130 or int(rs(i).Type)=200 or int(rs(i).Type)=201 or int(rs(i).Type)=202 or int(rs(i).Type)=203 Then'只处理字段类型为字符型的字段
  15.             conn.execute("update ["&rstSchema("TABLE_NAME")&"] set "&rs(i).name&" =replace(cast("&rs(i).name&" as varchar(8000)),'这里放入JS木马内容','')")
  16.             response.write rs(i).name &" "&rs(i).Type &" "'显示执行过的字段名。
  17.           End If
  18.         Next
  19.         response.write "<br>"
  20.     End If
  21.     rstSchema.MoveNext
  22.     k=k+1
  23. Loop
  24. response.Write "执行成功"
  25. %>
复制代码


如果数据库表很多的话,上面的遍历数据库结构未执行完就被IIS给停止了。在这时候可以在

程序代码
  1. If rstSchema("TABLE_TYPE")="TABLE" Then
复制代码


当中适当加入k值的范围,如:

程序代码
  1. If rstSchema("TABLE_TYPE")="TABLE" k>10 and k<20 Then
复制代码


这样的话就一次只操作9个表。
第三步:
根据数据库JS注入的特性(会包括<script、</script>和http://这样的字符),
在conn.asp里面放入如下代码:

程序代码
  1. Function Cheack_Sqljs()'防止数据库外链JS注入:true为发现外链JS注入。
  2.     Dim F_Post,F_Get
  3.     Cheack_Sqljs=False
  4.     If Request.Form<>"" Then'表单提交时的检测
  5.         For Each F_Post In Request.Form
  6.             If (Instr(LCase(Request.Form(F_Post)),"<script")<>0 or Instr(LCase(Request.Form(F_Post)),"</script>")<>0) and Instr(LCase(Request.Form(F_Post)),"http://")<>0 Then
  7.                 Cheack_Sqljs=True
  8.                 Exit For
  9.             End If
  10.         Next
  11.     End If
  12.     If Request.QueryString<>"" Then'QueryString提交时的检测
  13.         For Each F_Get In Request.QueryString
  14.             If (Instr(LCase(Request.Form(F_Get)),"<script")<>0 or Instr(LCase(Request.Form(F_Get)),"</script>")<>0) and Instr(LCase(Request.Form(F_Get)),"http://")<>0 Then
  15.                 Cheack_Sqljs=True
  16.                 Exit For
  17.             End If
  18.         Next
  19.     End If
  20. End Function
  21. Function CheckDataFrom()'检查提交数据来源:True为数据从站外提交过来的
  22.     CheckDataFrom=True
  23.     server_v1=Cstr(Request.ServerVariables("HTTP_REFERER"))
  24.     server_v2=Cstr(Request.ServerVariables("SERVER_NAME"))
  25.     if mid(server_v1,8,len(server_v2))<>server_v2 then
  26.         CheckDataFrom=False
  27.     end if
  28. End Function
  29. If Cheack_Sqljs or CheckDataFrom Then
  30.    Response.Write "<Script Language=JavaScript>alert('禁止执行,非法操作。');</Script>"
  31.    Response.End()
  32. End If
复制代码
发表于 2009-10-13 19:18:46 | 显示全部楼层
那个贴子 让它沉下去吧! 不要啦
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|QQ客服|联系我们|Archiver|手机版|小黑屋|悉远网络 ( 鄂ICP备09013446号 )

GMT+8, 2024-4-17 04:04 , Processed in 0.032687 second(s), 7 queries , Redis On.

Powered by Discuz! X3.5

Copyright © 2001-2023 Tencent Cloud.

快速回复 返回顶部 返回列表