How to deal with the Trojan horse hanging on the ASP website database
I believe many people have encountered the database being hacked. Here, I'll talk about how I dealt with it.
Step one: Make a backup of your existing database.
Step two:
Execute the following ASP file to remove the JS Trojan in the database:
Note: I wrote conn.asp myself.
'Put the JS Trojan content here: Please remember to change it to the JS Trojan content in your database.
<!--#include file=conn.asp-->
<%
Server.ScriptTimeOut=180
Set rstSchema = conn.OpenSchema(20)
k=1
Do Until rstSchema.EOF 'Traverse the database table
If rstSchema(TABLE_TYPE)=TABLE Then
response.write K&.<font color=red><b>&rstSchema(TABLE_NAME) & </b></font>: 'Display table name
Set rs=Server.CreateObject(ADODB.Recordset)
sql=select * from [ & rstSchema(TABLE_NAME)&]
rs.open sql,conn,1,3
For i=0 to rs.fields.count-1 'Traverse the fields in the table
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' only processes fields whose field type is character type
conn.execute(update [&rstSchema(TABLE_NAME)&] set &rs(i).name& =replace(cast(&rs(i).name& as varchar(8000)),'Put JS Trojan content here',''))
response.write rs(i).name & &rs(i).Type & 'Display the executed field name.
End If
Next
response.write <br>
End If
rstSchema.MoveNext
k=k+1
Loop
response.Write executed successfully
%>
If there are many database tables, the above traversal of the database structure will be stopped by IIS before it is completed. At this time you can
If rstSchema(TABLE_TYPE)=TABLE Then
Appropriately add the range of k values, such as:
If rstSchema(TABLE_TYPE)=TABLE k>10 and k<20 Then
In this case, only 9 tables will be operated at a time.
Step three:
According to the characteristics of database JS injection (which will include characters such as <script, </script> and http://),
Put the following code in conn.asp:
Function Cheack_Sqljs()'Prevents JS injection from external links in the database: true indicates JS injection from external links is discovered.
Dim F_Post,F_Get
Cheack_Sqljs=False
If Request.Form<> Then' Detection when the form is submitted
For Each F_Post In Request.Form
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
Check_Sqljs=True
Exit For
End If
Next
End If
If Request.QueryString<> Then'QueryString detection on submission
For Each F_Get In Request.QueryString
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
Check_Sqljs=True
Exit For
End If
Next
End If
End Function
Function CheckDataFrom()'Check the source of submitted data: True means the data is submitted from outside the site
CheckDataFrom=True
server_v1=Cstr(Request.ServerVariables(HTTP_REFERER))
server_v2=Cstr(Request.ServerVariables(SERVER_NAME))
if mid(server_v1,8,len(server_v2))<>server_v2 then
CheckDataFrom=False
end if
End Function
If Cheack_Sqljs or CheckDataFrom Then
Response.Write <Script Language=JavaScript>alert('Execution prohibited, illegal operation.');</Script>
Response.End()
End If