2011年12月19日 星期一

[Asp].queryString 阻擋單引號比較好的解法

假設您有2道關卡, 來阻擋 hacker 的 sql injection,
第1道關卡: 寫在所有的 include 檔的第1行裡, 固定會執行.
第2道關卡: 寫在,每一個 request 指令之後, 用來判斷接收到的變數內容的正確及合法性.

* 附註: 聽說攻擊的方式, 不只有用單引號, 所以其實第1道關卡不是必須的, 但一定要在每一程式寫好第2道關卡進行檢查.


由於在 querystring 裡, %27 與 ' (單引號) 是不同的, 但直接擋掉 %27 感覺又怪怪的, 不知道會不會擋到不知名的中文字. 所以之前寫:
if instr(request.querystring(),"%27") > 0 then
...
end if


置換成:
if isQueryStringContainQuota() then
...
end if



'// purpose: check is querystring Contains quota.
'// ex: ret = isQueryStringContantQuota()
'// return:
' True: Found!
' False: not Found.
function isQueryStringContainQuota()
dim returnValue
returnValue = not True '// default: not found.

dim strSingleQuota
strSingleQuota = "'"

dim qItem
for each qItem in request.querystring
if instr(trim("" & request.querystring(qItem)),strSingleQuota) > 0 then
returnValue = True
exit for
end if
next

isQueryStringContainQuota = returnValue
end function

沒有留言:

張貼留言

Facebook 留言板