兩個(gè)小函數(shù)讓你的ASP程序?qū)QL注入免疫!
Rem ## 長(zhǎng)整數(shù)轉(zhuǎn)換
Function toNum(s, default)
If IsNumeric(s) and s <> "" then
toNum = CLng(s)
Else
toNum = default
End If
End Function
Rem ## SQL 語(yǔ)句轉(zhuǎn)換
Function toSql(str)
If IsNull(str) Then str = ""
toSql = replace(str, "''", "''''")
End Function
示例:
Dim sql
Dim strWhere, strName, intAge
strName = toSql(request("user"))
intAge = toNum(request("age"), 20)
sql = "SELECT * FROM [USER]" & _
"WHERE [AGE] > " & strName & _
" AND [USERNAME] = ''" & intAge & "''"
一般情況下, 通過上面兩個(gè)函數(shù)的過慮, 可以杜絕網(wǎng)上的SQL注入攻擊!如果你覺得有需要, 可以加上對(duì)chr(0)的替換, 將toSql函數(shù)改為如下:
Function toSql(str)
If IsNull(str) Then str = ""
str = replace(str, chr(0), "")
toSql = replace(str, "''", "''''")
End Function
另注:
***********************************************************************
檢測(cè)外部提交的函數(shù)
Function CheckUrlRefer()
Dim strLocalUrl, intUrlLen, strUrlRefer
strLocalUrl = "intUrlLen = Len(strLocalUrl)
strUrlRefer = LCase(request.ServerVariables("HTTP_REFERER") & "")
''檢測(cè)前一個(gè)頁(yè)面是否來(lái)自 strLocalUrl
If Left(strUrlRefer, intUrlLen) = strLocalUrl Then
CheckUrlRefer = True
Else
CheckUrlRefer = False
End If
End Function
***********************************************************************
該函數(shù)可以幫助你抵擋外部的SQL注入測(cè)試, 只需要在頁(yè)面的頭部調(diào)用即可.
通過簡(jiǎn)單的兩個(gè)小函數(shù), 讓你的ASP程序更安全!
歡迎高手指正(請(qǐng)將繞過這兩個(gè)函數(shù)的方法寫出來(lái))!
相關(guān)討論頁(yè)面:
http://community.csdn.net/Expert/TopicView.asp?id=3585010
http://community.csdn.net/Expert/TopicView.asp?id=3582230
http://community.csdn.net/Expert/topic/3589/3589480.xml?temp=.4866449
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
dim qs,errc,iii
qs=request.servervariables("query_string")
dim nothis(18)
nothis(0)="net user"
nothis(1)="xp_cmdshell"
nothis(2)="/add"
nothis(3)="exec%20master.dbo.xp_cmdshell"
nothis(4)="net localgroup administrators"
nothis(5)="select"
nothis(6)="count"
nothis(7)="asc"
nothis(8)="char"
nothis(9)="mid"
nothis(10)="''"
nothis(11)=":"
nothis(12)=""""
nothis(13)="insert"
nothis(14)="delete"
nothis(15)="drop"
nothis(16)="truncate"
nothis(17)="from"
nothis(18)="%"
errc=false
for iii= 0 to ubound(nothis)
if instr(qs,nothis(iii))<>0 then
errc=true
end if
next
if errc then
Response.Write("對(duì)不起,非法URL地址請(qǐng)求!")
response.end
end if
***************************************************************
當(dāng)然這方法做得太“絕”了,但是我也是沒有辦法啊。這個(gè)方法是在網(wǎng)上看到的,運(yùn)行于一個(gè)網(wǎng)站上,現(xiàn)在一切良好。為了安全我只能這樣。我想只要有關(guān)SQL的敏感單詞都進(jìn)行過濾掉應(yīng)該沒有什么吧,當(dāng)然像樓主的做到那一步是基本上可以了,可以修補(bǔ)一下用用。記得我最初用的是《SQL注入天書》上面提供的防范方法,后來(lái)才改用這個(gè)。
將我以前用的代碼也帖出來(lái)供參考,大家有興趣可以去百度或GOOGLE中搜索一下《SQL注入天書》了解
使用這個(gè)函數(shù),對(duì)客戶端提交來(lái)的數(shù)據(jù)進(jìn)行驗(yàn)證。。。
<%
Function SafeRequest(ParaName,ParaType)
''--- 傳入?yún)?shù) ---
''ParaName:參數(shù)名稱-字符型
''ParaType:參數(shù)類型-數(shù)字型(1表示以上參數(shù)是數(shù)字,0表示以上參數(shù)為字符)
Dim ParaValue
ParaValue=Request(ParaName)
If ParaType=1 then
If not isNumeric(ParaValue) then
Response.write "參數(shù)" & ParaName & "必須為數(shù)字型!"
Response.end
End if
Else
ParaValue=replace(ParaValue,"''","''''")
End if
SafeRequest=ParaValue
End function%>
該文章在 2011/2/16 1:00:56 編輯過