InStr([start, ]string1, string2[, compare])
start:搜尋起始位置(預設 1),若指定 compare 則必填。string1:被搜尋字串。string2:要尋找的字串。compare:vbBinaryCompare (0) 二進位比較(預設),vbTextCompare (1) 文字比較。回傳:找到 → 位置(1-based);未找到 → 0;string1 為空/Null → 0/Null。
If InStr(Request("id"), " ") > 0 Or InStr(Request("id"), "'") > 0 Then
Response.Redirect "index.asp"
End If
邏輯:偵測輸入參數 id 是否包含空格或單引號,發現則導向首頁阻擋攻擊。
AND 1=1?典型注入 payload:id=90 AND 1=1 必含空格。
InStr(Request("id"), " ") 偵測到空格即攔截,使 AND 1=1、OR 'a'='a 等依賴空格分隔的注入失效。
Dim badChars : badChars = Array(" ", "'", ";", ",", "--", "/*", "*/", "xp_", "sp_")
Dim i, inputVal : inputVal = Request("id")
For i = 0 To UBound(badChars)
If InStr(inputVal, badChars(i)) > 0 Then
Response.Redirect "index.asp"
End If
Next
陣列化管理危險字串,易維護擴充。
InStr 過濾可作為第一道防線或舊系統快速修補,但絕不能當作唯一防護。新專案務必採用參數化查詢從根源杜絕 SQL 注入。