不知道是不是有些人給SQL注入嚇怕了,把一大堆根本對系統安全沒威脅的字元(比如"號,%號等)都過濾了(甚至NB聯盟有些人居然也這樣做),使得一些系統的User Interface極不友好,經常出現"輸入特殊字元"的提示,造成使用者的流失.
以下是根據我一年注入經驗的總結,在絕對安全的前提下,過濾得最少,最友好的防注入方法:
1.數字型變數:用isNumeric()判斷是否為數字
2.字元型或其它類型變數:將單引號'替換成兩個
下面給出兩個函數,用來代替ASP的Request函數,只要每處地方使用這兩個函數取值,SQL注入根本沒有用武之地.
'----------------------------------------------------------------
' 擷取數字型參數
'----------------------------------------------------------------
Function ReqNum ( StrName )
ReqNum = Request ( StrName )
if not isNumeric ( ReqNum ) then
response.write "參數必須為數字型!"
response.end
end if
End Function
'----------------------------------------------------------------
' 擷取字元型參數
'----------------------------------------------------------------
Function ReqStr ( StrName )
ReqStr = Replace ( Request(StrName), "'", "''" )
End Function
或是加代碼過濾
Function sqlstr(data) '過濾字串
data = Trim(Replace(Request(data), "&", "&"))
data = replace(data, "<", "<")
data = replace(data, ">", ">")
data = replace(data, "'", """")
data = replace(data, "*", "")
data = replace(data, "?", "")
data = replace(data, "select", "")
data = replace(data, "insert", "")
data = replace(data, "delete", "")
data = replace(data, "update", "")
data = replace(data, "delete", "")
data = replace(data, "create", "")
data = replace(data, "drop", "")
data = replace(data, "declare", "")
data = replace(data, vbCrLf&vbCrlf, "</p><p>")
data = replace(data, vbCrLf, "<br>")
sqlstr = (data)
End Function
不夠自己加!