當我要擷取一個數字型變數str
value=saferequest("str",1,0)
這句的意思是:擷取參數str中的值,並進行數字判斷,不是數位或者為空白的時候,value就等於0,否則,value等於request("str")的值。
function saferequest(paraname,paratype,lenlimit)
dim paravalue
paravalue = trim(request(paraname))
if paratype = 1 then
if isnull(paravalue) or (not isnumeric(paravalue)) then
paravalue = lenlimit
end if
else
if isnull(paravalue) then
paravalue = ""
else
dim strbadchar, arrbadchar, tempchar, i
strbadchar = "+,',--,^," & chr(34) & "," & chr(0) & ""
arrbadchar = split(strbadchar, ",")
tempchar = paravalue
for i = 0 to ubound(arrbadchar)
tempchar = replace(tempchar, arrbadchar(i), "")
next
tempchar = replace(tempchar, "@@", "@")
if lenlimit <> -1 then
tempchar = left(tempchar,lenlimit)
end if
paravalue = tempchar
end if
end if
saferequest = paravalue
end function
使用方法:
當我要擷取一個字元型變數str
value=saferequest("str",0,50)
這句的意思是:擷取參數str中的值,只擷取前50個字元,超過的丟失,並對那些特殊符號進行了過濾。
方法二
function saferequest(paraname,paratype)
'--- 傳入參數 ---
'paraname:參數名稱-字元型
'paratype:參數類型-數字型(1表示以上參數是數字,0表示以上參數為字元)
dim paravalue
paravalue=request(paraname)
if paratype=1 then
if not isnumeric(paravalue) then
response.write "參數" & paraname & "必須為數字型!"
response.end
end if
else
paravalue=replace(paravalue,"'","''")
end if
saferequest=paravalue
end function
方法三
通用的sql防注入程式一般的http請求不外乎get 和 post,所以只要我們在檔案中過濾所有post或者get請求中的參數資訊中非法字元即可,所以我們實現http 請求資訊過濾就可以判斷是是否受到sql注入攻擊。
iis傳遞給asp教程.dll的get 請求是是以字串的形式,,當 傳遞給request.querystring資料後,asp解析器會分析request.querystring的資訊,,然後根據"&",分出各個數組內的資料所以get的攔截如下:
首先我們定義請求中不能包含如下字元:
引用:
--------------------------------------------------------------------------------
|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare
--------------------------------------------------------------------------------
各個字元用"|"隔開,,然後我們判斷的得到的request.querystring,具體代碼如下 :
引用:
--------------------------------------------------------------------------------
dim sql_injdata
sql_injdata = "'|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare"
sql_inj = split(sql_injdata,"|")
if request.querystring<>"" then
for each sql_get in request.querystring
for sql_data=0 to ubound(sql_inj)
if instr(request.querystring(sql_get),sql_inj(sql_data))>0 then
response.write "<script language=****>alert('www.haohaoxx8.comsql通用防注入系統提示↓nn請不要在參數中包含非法字元嘗試注入!');history.back(-1)</script>"
response.end
end if
next
next
end if
--------------------------------------------------------------------------------
這樣我們就實現了get請求的注入的攔截,但是我們還要過濾post請求,所以我們還得繼續考慮request.form,這個也是以數組形式存在的,我們只需要再進一次迴圈判斷即可。代碼如下:
引用:
--------------------------------------------------------------------------------
if request.form<>"" then
for each sql_post in request.form
for sql_data=0 to ubound(sql_inj)
if instr(request.form(sql_post),sql_inj(sql_data))>0 then
response.write "<script language=****>alert('www.haohaoxx8.comsql通用防注入系統提示↓nn請不要在參數中包含非法字元嘗試注入!</script>"
response.end
end if
next
next
end if