首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> SQL Server 資料庫 >> 關於SqlServer

防範 SQL 注入攻擊新手入門:ASP 請求過濾函數 SafeRequest 實作

     編輯:關於SqlServer

針對 ASP 程式 SQL 注入漏洞,綜合改良網上方案,提供 SafeRequest 函數替換原生 Request,攔截高危關鍵字。

核心代碼

Function SafeRequest(ParaName)
    Dim ParaValue
    ParaValue = Request(ParaName)
    If IsNumeric(ParaValue) = True Then
        SafeRequest = ParaValue
        Exit Function
    ElseIf InStr(LCase(ParaValue), "select ") > 0 Or _
           InStr(LCase(ParaValue), "insert ") > 0 Or _
           InStr(LCase(ParaValue), "delete from") > 0 Or _
           InStr(LCase(ParaValue), "count(") > 0 Or _
           InStr(LCase(ParaValue), "drop table") > 0 Or _
           InStr(LCase(ParaValue), "update ") > 0 Or _
           InStr(LCase(ParaValue), "truncate ") > 0 Or _
           InStr(LCase(ParaValue), "asc(") > 0 Or _
           InStr(LCase(ParaValue), "mid(") > 0 Or _
           InStr(LCase(ParaValue), "char(") > 0 Or _
           InStr(LCase(ParaValue), "xp_cmdshell") > 0 Or _
           InStr(LCase(ParaValue), "exec master") > 0 Or _
           InStr(LCase(ParaValue), "net localgroup administrators") > 0 Or _
           InStr(LCase(ParaValue), " and ") > 0 Or _
           InStr(LCase(ParaValue), "net user") > 0 Or _
           InStr(LCase(ParaValue), " or ") > 0 Then
        Response.Write ""
        Response.End
    Else
        SafeRequest = ParaValue
    End If
End Function

使用方式

全站將 Request("param") 替換為 SafeRequest("param")。

局限與進階建議

  • 黑名單模式易被繞過(編碼、大小寫、註解、字串拼接)。
  • 根本方案:參數化查詢 (Command.Parameters) + 最小權限資料庫帳號 + WAF/輸入驗證白名單。
  • 數值型參數優先 IsNumeric 驗證,字串型嚴格長度/格式校驗。

此函數可作為舊系統快速加固臨時方案,新專案務必採用參數化查詢從源頭消除注入風險。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved