網站頻繁被報毒「TrojanDownloader」,檢查發現資料庫欄位內藏惡意 <script src=https://evil.com/xxx.js>。本文提供:資料庫備份 → ASP 批次清除腳本 → 連線檔加入防注入過濾器,三步驟徹底清除並防禦二次植入。
mysqldump -u root -p dbname > backup_$(date +%F).sql。<%@ Language=VBScript CodePage=65001 %>
<!--#include file="conn.asp"-->
<%
' === 設定區 ===
Const EVIL_PATTERN = "<script src=https://" ' 依實際掛馬特徵修改,區分大小寫用 LCase 比對
Const BATCH_SIZE = 10 ' 每次處理資料表數量,避免 IIS 超時
Dim conn, rsSchema, tblName, rsData, fld, sql, cnt, k
k = 0
Set conn = GetConn() ' conn.asp 需提供 GetConn() 回傳 ADODB.Connection
' 取得所有使用者資料表
Set rsSchema = conn.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "TABLE"))
Do Until rsSchema.EOF
tblName = rsSchema("TABLE_NAME")
k = k + 1
If k <= BATCH_SIZE Then ' 分批處理,避免逾時
' 遍歷該資料表所有文字欄位
Set rsData = conn.Execute("SELECT * FROM [" & tblName & "] WHERE 1=0") ' 僅取結構
For Each fld In rsData.Fields
If fld.Type = adLongVarWChar Or fld.Type = adVarWChar Or fld.Type = adVarChar Or fld.Type = adLongVarChar Then
sql = "UPDATE [" & tblName & "] SET [" & fld.Name & "] = REPLACE([" & fld.Name & "], '" & Replace(EVIL_PATTERN, "'", "''") & "', '') WHERE [" & fld.Name & "] LIKE '%" & Replace(EVIL_PATTERN, "'", "''") & "%'"
conn.Execute sql, cnt
Response.Write "Table: " & tblName & " Field: " & fld.Name & " Cleaned: " & cnt & "<br>"
End If
Next
rsData.Close
End If
rsSchema.MoveNext
Loop
rsSchema.Close
Response.Write "<hr>Cleanup batch completed.<br>"
%>
使用方式:將上述儲存為 clean_js.asp 放入網站根目錄,瀏覽器訪問執行。若資料表超過 BATCH_SIZE,多執行幾次或調大數值 (注意 IIS 執行逾時預設 90 秒)。
<%
' conn.asp 現有連線代碼之後新增
Function Check_SqlJs()
' 偵測 POST/GET 參數中是否包含典型 JS 掛馬特徵:' OR '1'='1 + https://
Dim key, val, pattern
pattern = Array("' or '1'='1", "' or '1'='1'", "https://", "https://", "<script", "<iframe", "eval(", "document.cookie", "fromcharcode")
If Request.Form.Count > 0 Then
For Each key In Request.Form
val = LCase(Request.Form(key))
For Each p In pattern
If InStr(val, p) > 0 Then
Check_SqlJs = True
Exit Function
End If
Next
Next
End If
If Request.QueryString.Count > 0 Then
For Each key In Request.QueryString
val = LCase(Request.QueryString(key))
For Each p In pattern
If InStr(val, p) > 0 Then
Check_SqlJs = True
Exit Function
End If
Next
Next
End If
Check_SqlJs = False
End Function
Function Check_Data_Source()
' 簡易 Referer 驗證:防止跨站提交
Dim ref, host
ref = Request.ServerVariables("HTTP_REFERER")
host = Request.ServerVariables("SERVER_NAME")
If ref = "" Then Check_Data_Source = False : Exit Function
If InStr(LCase(ref), LCase(host)) = 0 Then
Check_Data_Source = False
Else
Check_Data_Source = True
End If
End Function
' === 全域攔截 ===
If Check_SqlJs() Or Not Check_Data_Source() Then
Response.Write "非法請求被攔截"
Response.End
End If
%>
<script src=https:// 取代為空字串。UPDATE [Table] SET [Column] = REPLACE([Column], '<script src=https://', '') WHERE [Column] LIKE '%<script src=https://%' 逐表執行。「清除只是權宜之計,參數化查詢與最小權限才是根治之道。」—— 記住這句話,下次就不會再被同樣手法攻破!