萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP 程式設計

ASP 資料庫被掛馬清理與防護:批次移除惡意 Script 標籤 + 參數過濾雙重防線

現象與成因

資料庫文字欄位 (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 (每請求自動攔截)

<% ' 在 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 Or 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 Or 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
%>

第三階段:根本修復 (開發規範)

  1. 全站參數化查詢:杜絕字串拼接 SQL,使用 ADODB.Command + Parameters
  2. 輸出編碼:前端輸出用戶內容時必須 Server.HTMLEncode 或對應 JS/URL 編碼
  3. 最小權限資料庫帳號:網站連線帳號無 DROP/ALTER/xp_cmdshell 權限
  4. WAF/防火牆:雲端 WAF (Cloudflare, Alibaba Cloud WAF) 攔截已知攻擊特徵
  5. 定期掃描:排程執行掃描腳本,檢測資料庫中是否出現新惡意模式

掃描腳本:排程每日執行

<% ' scan_malware.asp - 由 Windows 排程器呼叫
Dim conn, rs, tbl, col, sql, cnt, patterns, p
patterns = Array("", "https://", "eval(", "document.write", "fromCharCode")
' ... 遍歷所有表、文字欄位,對每個 pattern 執行
' SELECT COUNT(*) FROM [tbl] WHERE [col] LIKE '%pattern%'
' 記錄發現數量,超過閾值發送 Email 警報
%>
copyright © 萬盛學電腦網 all rights reserved