代碼發布!兩個小函數讓你的ASP程式對SQL注入免疫!

來源:互聯網
上載者:User

Rem  ##  長整數轉換 

Function  toNum(s,  default) 

           If  IsNumeric(s)  and  s  <>  ""  then 

                       toNum  =  CLng(s) 

           Else 

                       toNum  =  default 

           End  If 

End  Function 

 

Rem  ##  SQL  語句轉換 

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 &  "'"

一般情況下, 通過上面兩個函數的過慮, 可以杜絕網上的SQL注入攻擊!如果你覺得有需要, 可以加上對chr(0)的替換, 將toSql函數改為如下:

Function  toSql(str) 

           If  IsNull(str)  Then  str  =  "" 

           str =  replace(str, chr(0),  "") 

           toSql  =  replace(str,  "'",  "''") 

End  Function

另注:

***********************************************************************

檢測外部提交的函數

Function CheckUrlRefer()

   Dim strLocalUrl, intUrlLen, strUrlRefer

   strLocalUrl = "http://127.0.0.1"

   intUrlLen = Len(strLocalUrl)

   strUrlRefer = LCase(request.ServerVariables("HTTP_REFERER") & "")

   '檢測前一個頁面是否來自 strLocalUrl

   If Left(strUrlRefer, intUrlLen) = strLocalUrl Then

     CheckUrlRefer = True

   Else

     CheckUrlRefer = False

   End If

End Function

***********************************************************************

該函數可以協助你抵擋外部的SQL注入測試, 只需要在頁面的頭部調用即可.

通過簡單的兩個小函數, 讓你的ASP程式更安全!

歡迎高手指正(請將繞過這兩個函數的方法寫出來)!

相關討論頁面:

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("對不起,非法URL地址請求!")

response.end

end if

***************************************************************

當然這方法做得太“絕”了,但是我也是沒有辦法啊。這個方法是在網上看到的,運行於一個網站上,現在一切良好。為了安全我只能這樣。我想只要有關SQL的敏感單詞都進行過濾掉應該沒有什麼吧,當然像樓主的做到那一步是基本上可以了,可以修補一下用用。記得我最初用的是《SQL注入天書》上面提供的防範方法,後來才改用這個。

將我以前用的代碼也帖出來供參考,大家有興趣可以去百度或GOOGLE中搜尋一下《SQL注入天書》瞭解

使用這個函數,對用戶端提交來的資料進行驗證。。。

<%

 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

%>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.