資料庫文字欄位 (nvarchar/text) 被大量注入 `` 等惡意代碼,導致前端頁面載入外部惡意腳本。常見入口:未過濾的表單提交、編輯器上傳、SQL Injection。
<%@ Language=VBScript CodePage=65001 %>
<!--#include file="conn.asp"-->
<%
Server.ScriptTimeout = 300
Dim conn, rsSchema, rs, tblName, colName, colType, sql, cleaned, totalCleaned
totalCleaned = 0
' 遍歷所有使用者資料表
Set rsSchema = conn.OpenSchema(20) ' adSchemaTables
Do Until rsSchema.EOF
If rsSchema("TABLE_TYPE") = "TABLE" Then
tblName = rsSchema("TABLE_NAME")
' 跳過系統表
If Left(tblName, 4) <> "MSys" And Left(tblName, 3) <> "sys" Then
Response.Write "處理表: " & tblName & "
"
' 取得所有文字型欄位
Set rs = conn.OpenSchema(4, Array(Empty, Empty, tblName)) ' adSchemaColumns
Do Until rs.EOF
colName = rs("COLUMN_NAME")
colType = rs("DATA_TYPE")
' 文字型別: 129=Char, 130=VarChar, 200=VarWChar, 201=LongVarChar, 202=VarWChar, 203=LongVarWChar
If colType = 129 Or colType = 130 Or colType = 200 Or colType = 201 Or colType = 202 Or colType = 203 Then
' 只清理包含惡意模式的列,減少寫入負載
sql = "SELECT " & pKey & ", " & colName & " FROM [" & tblName & "] WHERE " & colName & " LIKE '%', '')"
conn.Execute sql
totalCleaned = totalCleaned + conn.RecordsAffected
Response.Write " 清理欄位 " & colName & ": " & conn.RecordsAffected & " 筆
"
End If
rsData.Close: Set rsData = Nothing
End If
rs.MoveNext
Loop
rs.Close
End If
End If
rsSchema.MoveNext
Loop
rsSchema.Close
Response.Write "
總共清理: " & totalCleaned & " 筆資料"
%>
<% ' 在 conn.asp 或 global.asa 最頂端引入
Function CheckSQLInjection()
Dim key, val, raw
CheckSQLInjection = False
' 檢查 Form
For Each key In Request.Form
val = Request.Form(key)
raw = LCase(val)
If (InStr(raw, "") > 0) And InStr(raw, "https://") > 0 Then
LogAttack "SQL_INJECTION_FORM", key, val
CheckSQLInjection = True
Exit Function
End If
Next
' 檢查 QueryString
For Each key In Request.QueryString
val = Request.QueryString(key)
raw = LCase(val)
If (InStr(raw, "") > 0) And InStr(raw, "https://") > 0 Then
LogAttack "SQL_INJECTION_QS", key, val
CheckSQLInjection = True
Exit Function
End If
Next
End Function
Function CheckReferer()
Dim ref, host
ref = Request.ServerVariables("HTTP_REFERER")
host = Request.ServerVariables("SERVER_NAME")
CheckReferer = True
If ref <> "" Then
If InStr(1, ref, "://" & host, vbTextCompare) = 0 And _
InStr(1, ref, "://www." & host, vbTextCompare) = 0 Then
CheckReferer = False
End If
End If
End Function
Sub LogAttack(type, field, value)
Dim fso, f, line
line = Now() & vbTab & type & vbTab & Request.ServerVariables("REMOTE_ADDR") & vbTab & field & vbTab & value & vbCrLf
Set fso = Server.CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set f = fso.OpenTextFile(Server.MapPath("attack_log.txt"), 8, True)
f.Write line
f.Close
On Error GoTo 0
End Sub
' === 全域攔截 ===
If CheckSQLInjection() Or Not CheckReferer() Then
Response.Write ""
Response.End
End If
%>
ADODB.Command + ParametersServer.HTMLEncode 或對應 JS/URL 編碼DROP/ALTER/xp_cmdshell 權限<% ' scan_malware.asp - 由 Windows 排程器呼叫
Dim conn, rs, tbl, col, sql, cnt, patterns, p
patterns = Array("