ASP上兩個防止SQL注入式攻擊Function

穩萊

ASP上兩個防止SQL注入式攻擊Function
''==========================
''過濾提交表單中的SQL
''==========================
function ForSqlForm()
dim fqys,errc,i,items
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)="%"
'nothis(19)="@" 

errc=false

for i= 0 to ubound(nothis)
  for each items in request.Form
  if instr(request.Form(items),nothis(i))<>0 then
   response.write("<div>")
   response.write("你所填寫的訊息:" & server.HTMLEncode(request.Form(items)) & "<br>含非法字元:" & nothis(i))
   response.write("</div>")
   response.write("對不起,你所填寫的訊息含非法字元!<a href=""#"" onclick=""history.back()"">返回</a>")
   response.End()
  end if
  next
next
end function
''==========================
''過濾查詢中的SQL
''==========================
function ForSqlInjection()
dim fqys,errc,i
dim nothis(19)
fqys = request.ServerVariables("QUERY_STRING")
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)="%"
nothis(19)="@" 

errc=false

for i= 0 to ubound(nothis)
 if instr(FQYs,nothis(i))<>0 then
  errc=true
 end if
next

if errc then
 response.write "查詢訊息含非法字元!<a href=""#"" onclick=""history.back()"">返回</a>"
 response.end
end if
end function

 =============================================================

我比較喜歡用的程式 把它放在 conn.asp 內

Dim Query_Badword,Form_Badword,i,Err_Message,Err_Web,name

Err_Message = 3  '處理方式:1=提示信息,2=轉向頁面,3=先提示再轉向

Err_Web = "/" '出錯時轉向的頁面

'Query_Badword="'∥and∥select∥update∥chr∥delete∥%20from∥;∥insert∥mid∥master.∥set∥chr(37)∥="    
Query_Badword="'∥and∥select∥update∥chr∥delete∥%20from∥insert∥mid∥master.∥set∥chr(37)∥="    

On Error Resume Next

'----- 對 get query 值 的過濾.

if request.QueryString<>"" then
 Chk_badword=split(Query_Badword,"∥")
 FOR EACH Query_Name IN Request.QueryString
  for i=0 to ubound(Chk_badword)
   If Instr(LCase(request.QueryString(Query_Name)),Chk_badword(i))<>0 Then
    Select Case Err_Message
    Case "1"
     Response.Write "<Script Language=JavaScript>alert('傳參錯誤!參數 "&name&" 的值中包含非法字符串!\n\n請不要在參數中出現:and update delete ; insert mid master 等非法字符!');window.close();</Script>"
    Case "2"
     Response.Write "<Script Language=JavaScript>location.href='"&Err_Web&"'</Script>"
    Case "3"
     Response.Write "<Script Language=JavaScript>alert('傳參錯誤!參數 "&name&"的值中包含非法字符串!\n\n請不要在參數中出現:and update delete ; insert mid master 等非法字符!');location.href='"&Err_Web&"';</Script>"
    End Select
    Response.End
   End If
  NEXT
 NEXT
End if
 

 給當前日誌評分:
Loading Vote
正在讀取評分資料...


文章來自: Tank部落格
引用通告: 查看所有引用 | 我要引用此文章
Tags: 注入
相關日誌:

評論: 0 | 引用: 0 | 查看次數: -
發表評論
暱 稱:
密 碼: 遊客發言不需要密碼.
內 容:
驗證碼: 驗證碼
選 項:
雖然發表評論不用註冊,但是為了保護您的發言權,建議您註冊帳號.