萬盛學電腦網

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

ASP 動態頁面防采集策略:動態渲染、IP 限流與行為分析

ASP 反采集綜合防禦體系:從基礎阻擋到智慧型行為分析

針對惡意采集器(爬蟲),單一防禦手段易被繞過。本文建構多層次防禦體系:動態頁面渲染干擾、IP 信譽與限流、行為特徵分析、蜜罐陷阱、驗證碼挑戰,並提供 ASP 完整實作程式碼。

防禦層級架構

層級手段成本有效性誤傷風險
L1 基礎User-Agent 過濾、Referer 檢查低低(易偽造)低
L2 動態JS 渲染、AJAX 載入、字體加密中中(需無頭瀏覽器)低
L3 限流IP/Session 頻率限制、驗證碼中高中
L4 行為請求模式分析、滑鼠軌跡、停留時間高極高低
L5 陷阱蜜罐連結、隱藏表單、假資料低高(針對性)極低

L1:基礎請求過濾 (Global.asa / 包含檔)

<%
' ===== anti_scraper.asp =====
Sub BlockScrapers()
    Dim ua, ref, ip, isBot
    ua = LCase(Request.ServerVariables("HTTP_USER_AGENT"))
    ref = Request.ServerVariables("HTTP_REFERER")
    ip = GetRealIP()  ' 使用前文真實 IP 函數
    
    ' 已知爬蟲 UA 特徵
    Dim botPatterns : botPatterns = Array("bot", "crawler", "spider", "scraper", "curl", "wget", "python", "java", "php", "go-http", "okhttp", "apache-httpclient")
    Dim pattern
    For Each pattern In botPatterns
        If InStr(ua, pattern) > 0 Then
            Call LogAndBlock(ip, "UA_MATCH:" & pattern, ua)
            Exit Sub
        End If
    Next
    
    ' 空 User-Agent 或異常短
    If ua = "" Or Len(ua) < 20 Then
        Call LogAndBlock(ip, "EMPTY_UA", ua)
        Exit Sub
    End If
    
    ' Referer 檢查(可選,嚴格模式)
    ' If ref = "" Or InStr(ref, Request.ServerVariables("SERVER_NAME")) = 0 Then ...
End Sub

Sub LogAndBlock(ip, reason, ua)
    ' 記錄封鎖日誌
    ExecuteNonQuery "INSERT INTO BlockLog (IP, Reason, UA, BlockTime) VALUES (?, ?, ?, Now())", Array(ip, reason, ua)
    ' 回傳 403 或偽裝內容
    Response.Status = "403 Forbidden"
    Response.Write "

403 Forbidden

Access denied.

" Response.End End Sub ' 頁面頂端呼叫 Call BlockScrapers() %>

L2:動態內容渲染干擾

<%
' ===== 動態關鍵內容載入 =====
' 關鍵資料(價格、聯絡方式、核心內容)不在初始 HTML,由 AJAX 載入

' 頁面模板
' 
載入中...
' AJAX 端點:get_price.asp ' 驗證 Session/Token/Referer If Session("verified_human") <> True Then Response.Write "驗證失敗" Response.End End If Dim productId : productId = CLng(Request("id")) Dim price : price = GetProductPrice(productId) ' 資料庫查詢 Response.Write price %> <script> // 前端載入(延遲、隨機延遲) setTimeout(function() { fetch('get_price.asp?id=' + productId, { headers: {'X-Requested-With': 'XMLHttpRequest'}, credentials: 'same-origin' }).then(r => r.text()).then(function(price) { document.getElementById('price-container').innerHTML = price; }); }, Math.random() * 1000 + 500); // 500-1500ms 隨機延遲 </script>

L3:IP + Session 雙重限流

<%
' ===== 速率限制模組 =====
Class RateLimiter
    Private windowSeconds, maxRequests, prefix
    
    Private Sub Class_Initialize()
        windowSeconds = 60   ' 1 分鐘窗口
        maxRequests = 30     ' 最大請求數
        prefix = "ratelimit_"
    End Sub
    
    Public Function CheckLimit(identifier)
        Dim cacheKey, now, windowStart, requests, i, timestamps
        cacheKey = prefix & identifier
        now = Timer()
        windowStart = now - windowSeconds
        
        ' 從 Application 讀取時間戳陣列(生產環境用 Redis)
        timestamps = Application(cacheKey)
        If Not IsArray(timestamps) Then
            timestamps = Array()
        End If
        
        ' 過濾過期時間戳
        Dim validTs() : ReDim validTs(-1)
        For i = 0 To UBound(timestamps)
            If timestamps(i) > windowStart Then
                ReDim Preserve validTs(UBound(validTs) + 1)
                validTs(UBound(validTs)) = timestamps(i)
            End If
        Next
        
        ' 檢查是否超限
        If UBound(validTs) + 1 >= maxRequests Then
            CheckLimit = False
            Exit Function
        End If
        
        ' 記錄本次請求
        ReDim Preserve validTs(UBound(validTs) + 1)
        validTs(UBound(validTs)) = now
        Application.Lock
        Application(cacheKey) = validTs
        Application.UnLock
        
        CheckLimit = True
    End Function
End Class

' 使用
Dim limiter : Set limiter = New RateLimiter
Dim clientId : clientId = GetRealIP() & "_" & Session.SessionID
If Not limiter.CheckLimit(clientId) Then
    Response.Status = "429 Too Many Requests"
    Response.Write "請求過於頻繁,請稍後再試"
    Response.End
End If
%>

L4:行為分析 - 請求間隔與路徑異常檢測

<%
' ===== 行為分析模組 =====
Class BehaviorAnalyzer
    Private minInterval, maxRequestsPerMinute
    
    Private Sub Class_Initialize()
        minInterval = 500      ' 最小請求間隔 500ms
        maxRequestsPerMinute = 60
    End Sub
    
    Public Function AnalyzeRequest(ip, url, userAgent)
        Dim now, lastRequestTime, requestCount, isSuspicious
        now = Timer()
        
        ' 讀取歷史
        lastRequestTime = Application("last_req_" & ip)
        requestCount = Application("req_count_" & ip)
        
        If lastRequestTime = "" Then lastRequestTime = 0
        If requestCount = "" Then requestCount = 0
        
        ' 檢查間隔
        If (now - lastRequestTime) * 1000 < minInterval Then
            isSuspicious = True
            Call LogSuspicious(ip, "INTERVAL_TOO_SHORT", now - lastRequestTime)
        End If
        
        ' 檢查頻率(簡易:分鐘計數器重置)
        Dim minuteKey : minuteKey = "req_min_" & ip & "_" & Date() & "_" & Hour(Now()) & "_" & Minute(Now())
        Dim currentMinCount : currentMinCount = Application(minuteKey)
        If currentMinCount = "" Then currentMinCount = 0
        currentMinCount = currentMinCount + 1
        Application.Lock
        Application(minuteKey) = currentMinCount
        Application.UnLock
        
        If currentMinCount > maxRequestsPerMinute Then
            isSuspicious = True
            Call LogSuspicious(ip, "RATE_EXCEED", currentMinCount)
        End If
        
        ' 更新狀態
        Application.Lock
        Application("last_req_" & ip) = now
        Application("req_count_" & ip) = requestCount + 1
        Application.UnLock
        
        AnalyzeRequest = Not isSuspicious
    End Function
    
    Private Sub LogSuspicious(ip, reason, detail)
        ExecuteNonQuery "INSERT INTO SuspiciousActivity (IP, Reason, Detail, Time) VALUES (?, ?, ?, Now())", Array(ip, reason, detail)
    End Sub
End Class

' 使用
Dim analyzer : Set analyzer = New BehaviorAnalyzer
If Not analyzer.AnalyzeRequest(GetRealIP(), Request.ServerVariables("URL"), Request.ServerVariables("HTTP_USER_AGENT")) Then
    ' 可選:要求驗證碼、延遲回應、記錄封鎖
    Response.Write ""
End If
%>

L5:蜜罐陷阱

<%
' ===== 蜜罐連結(隱藏連結,正常用戶不會點擊)=====
' 頁面中加入:
' 

' /honey/trap.asp
Dim ip : ip = GetRealIP()
ExecuteNonQuery "INSERT INTO HoneyPotLog (IP, UA, Referer, Time) VALUES (?, ?, ?, Now())", Array(ip, Request.ServerVariables("HTTP_USER_AGENT"), Request.ServerVariables("HTTP_REFERER"))
' 直接封鎖或標記
Application.Lock
Application("blocked_ip_" & ip) = Now()
Application.UnLock
Response.Write "404 Not Found"
Response.Status = "404 Not Found"
Response.End
%>

<style>
#honey-link { display: none !important; visibility: hidden; position: absolute; left: -9999px; }
</style>

驗證碼挑戰整合

<%
' 觸發條件:IP 可疑、請求異常、高價值頁面
Sub RequireCaptcha()
    Session("require_captcha") = True
    Session("captcha_code") = GenerateCaptcha(4)  ' 4位數字/字母
    ' 儲存圖片或輸出 Base64
End Sub

Function ValidateCaptcha(inputCode)
    If Session("require_captcha") <> True Then ValidateCaptcha = True : Exit Function
    ValidateCaptcha = (StrComp(inputCode, Session("captcha_code"), 0) = 0)
    If ValidateCaptcha Then
        Session("require_captcha") = False
        Session("verified_human") = True
    End If
End Function
%>

綜合防禦部署清單

  1. ✅ 部署 Global.asa 全域請求過濾
  2. ✅ 關鍵資料 AJAX 動態載入 + Token 驗證
  3. ✅ IP + Session 雙重限流(Application/Redis)
  4. ✅ 行為分析:間隔、頻率、路徑熵值
  5. ✅ 部署蜜罐連結於網站地圖、robots.txt 禁止路徑
  6. ✅ 關鍵操作(註冊、發文、查詢)整合驗證碼
  7. ✅ 定期分析日誌,更新封鎖名單、調整閾值
  8. ✅ 監控儀表板:即時請求率、封鎖率、誤判申訴管道
copyright © 萬盛學電腦網 all rights reserved