ASP网站中数据库被挂木马的解决方案
本文我们详细介绍了ASP数据库被挂木马的详细解决方案之编程处理的方法,解决方案分为以下三步:
第一步:
为现有数据库做好备份。
第二步:
执行如下ASP文件,这样就可以去掉数据库当中的JS木马:
注:conn.asp自己去写了。
'这里放入JS木马内容:请记得改为自己数据库中的JS木马内容。
<!--#includefile="conn.asp"-->
<%
Server.ScriptTimeOut=180SetrstSchema=conn.OpenSchema(20)
k=1
DoUntilrstSchema.EOF'遍历数据库表
IfrstSchema("TABLE_TYPE")="TABLE"Then
response.writeK&".<fontcolor=red><b>"&rstSchema("TABLE_NAME")&"</b></font>:"'显示表名Setrs=Server.CreateObject("ADODB.Recordset")
sql="select*from["&rstSchema("TABLE_NAME")&"]"
rs.opensql,conn,1,3Fori=0tors.fields.count-1'遍历表中字段Ifint(rs(i).Type)=129
orint(rs(i).Type)=130orint(rs(i).Type)=200orint(rs(i).Type)=201orint(rs(i).Type)=202orint(rs(i).Type)=203Then'只处理字段类型为字符型的字段
conn.execute("update["&rstSchema("TABLE_NAME")&"]set"&rs(i).name&"=replace(cast("&rs(i).name&"
asvarchar(8000)),'这里放入JS木马内容','')")response.writers(i).name&""&rs(i).Type&""'显示执行过的字段名。
EndIf
Next
response.write"<br>"
EndIf
rstSchema.MoveNext
kk=k+1
Loop
response.Write"执行成功"
%>
如果数据库表很多的话,上面的遍历数据库结构未执行完就被IIS给停止了。在这时候可以在:
IfrstSchema("TABLE_TYPE")="TABLE"Then
当中适当加入k值的范围,如:
IfrstSchema("TABLE_TYPE")="TABLE"k>10andk<20Then
这样的话就一次只操作9个表。
第三步:
根据数据库JS注入的特性(会包括<script、</script>和http://这样的字符),在conn.asp里面放入如下代码:
FunctionCheack_Sqljs()'防止数据库外链JS注入:true为发现外链JS注入。
DimF_Post,F_Get
Cheack_Sqljs=False
IfRequest.Form<>""Then'表单提交时的检测ForEachF_PostInRequest.FormIf(Instr(LCase(Request.Form(F_Post)),
"<script")<>0orInstr(LCase(Request.Form(F_Post)),"</script>")<>0)
andInstr(LCase(Request.Form(F_Post)),"http://")<>0Then
Cheack_Sqljs=True
ExitFor
EndIf
Next
EndIf
IfRequest.QueryString<>""Then'QueryString提交时的检测ForEachF_GetInRequest.QueryString
If(Instr(LCase(Request.Form(F_Get)),"<script")<>0orInstr(LCase(Request.Form(F_Get)),"</script>")<>0)
andInstr(LCase(Request.Form(F_Get)),"http://")<>0Then
Cheack_Sqljs=True
ExitFor
EndIf
Next
EndIf
EndFunction
FunctionCheckDataFrom()'检查提交数据来源:True为数据从站外提交过来的
CheckDataFrom=True
server_v1=Cstr(Request.ServerVariables("HTTP_REFERER"))server_v2=Cstr(Request.ServerVariables("SERVER_NAME"))
ifmid(server_v1,8,len(server_v2))<>server_v2then
CheckDataFrom=False
endif
EndFunction
IfCheack_SqljsorCheckDataFromThen
Response.Write"<ScriptLanguage=JavaScript>alert('禁止执行,非法操作。');</Script>"Response.End()
EndIf
关于ASP数据库被挂木马时的编程处理方法就介绍到这里了,希望本次的介绍能够带给您一些收获,谢谢!