標籤:
<%
Dim Fy_Url,Fy_a,Fy_x,Fy_Cs(),Fy_Cl,Fy_Ts,Fy_Zx
‘---定義部份 頭------
Fy_Cl = 1 ‘處理方式:1=提示資訊,2=轉向頁面,3=先提示再轉向
Fy_Zx = "index.Asp" ‘出錯時轉向的頁面
‘---定義部份 尾------
On Error Resume Next
Fy_Url=Request.ServerVariables("QUERY_STRING")
Fy_a=split(Fy_Url,"&")
redim Fy_Cs(ubound(Fy_a))
On Error Resume Next
for Fy_x=0 to ubound(Fy_a)
Fy_Cs(Fy_x) = left(Fy_a(Fy_x),instr(Fy_a(Fy_x),"=")-1)
Next
For Fy_x=0 to ubound(Fy_Cs)
If Fy_Cs(Fy_x)<>"" Then
If Instr(LCase(Request(Fy_Cs(Fy_x))),"‘")<>0 or Instr(LCase(Request(Fy_Cs(Fy_x))),"select")<>0 or Instr(LCase(Request(Fy_Cs(Fy_x))),"update")<>0 or Instr(LCase(Request(Fy_Cs(Fy_x))),"chr")<>0 or Instr(LCase(Request(Fy_Cs(Fy_x))),"delete%20from")<>0 or Instr(LCase(Request(Fy_Cs(Fy_x))),";")<>0 or Instr(LCase(Request(Fy_Cs(Fy_x))),"insert")<>0 or Instr(LCase(Request(Fy_Cs(Fy_x))),"mid")<>0 Or Instr(LCase(Request(Fy_Cs(Fy_x))),"master.")<>0 Then
Select Case Fy_Cl
Case "1"
Response.Write "<Script Language=JavaScript>alert(‘ 出現錯誤!參數 "&Fy_Cs(Fy_x)&" 的值中包含非法字串!\n\n 請不要在參數中出現:and,select,update,insert,delete,chr 等非法字元!\n\n我已經設定了不能SQL注入,請不要對我進行非法手段!‘);window.close();</Script>"
Case "2"
Response.Write "<Script Language=JavaScript>location.href=‘"&Fy_Zx&"‘</Script>"
Case "3"
Response.Write "<Script Language=JavaScript>alert(‘ 出現錯誤!參數 "&Fy_Cs(Fy_x)&"的值中包含非法字串!\n\n 請不要在參數中出現:,and,select,update,insert,delete,chr 等非法字元!\n\n設計了門,非法侵入請離開,謝謝!‘);location.href=‘"&Fy_Zx&"‘;</Script>"
End Select
Response.End
End If
End If
Next
%>
以上代碼為較多網友所用,從使用的感言來看,效果顯著。
Dim Query_Badword,Form_Badword,Err_Message,Err_Web,form_name
‘------定義部份 頭----------------------------------------------------------------------
Err_Message = 1 ‘處理方式:1=提示資訊,2=轉向頁面,3=先提示再轉向
Err_Web = "Err.Asp" ‘出錯時轉向的頁面
Query_Badword="‘‖and‖select‖update‖chr‖delete‖%20from‖;‖insert‖mid‖master.‖set‖chr(37)‖="
‘在這部份定義get非法參數,使用"‖"號間隔
Form_Badword="‘‖%‖&‖*‖#‖@‖=‖select‖and‖set‖delete" ‘在這部份定義post非法參數,使用"‖"號間隔
‘------定義部份 尾-----------------------------------------------------------------------
‘
On Error Resume Next
‘----- 對 get query 值 的過濾.
if request.QueryString<>"" then
Chk_badword=split(Query_Badword,"‖")
FOR EACH Query_form_name IN Request.QueryString
for i=0 to ubound(Chk_badword)
If Instr(LCase(request.QueryString(Query_form_name)),Chk_badword(i))<>0 Then
Select Case Err_Message
Case "1"
Response.Write "<Script Language=JavaScript>alert(‘傳參錯誤!參數 "&form_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(‘傳參錯誤!參數 "&form_name&"的值中包含非法字串!\n\n請不要在參數中出現:and update delete ; insert mid master 等非法字元!‘);location.href=‘"&Err_Web&"‘;</Script>"
End Select
Response.End
End If
NEXT
NEXT
End if
‘-----對 post 表 單值的過濾.
if request.form<>"" then
Chk_badword=split(Form_Badword,"‖")
FOR EACH form_name IN Request.Form
for i=0 to ubound(Chk_badword)
If Instr(LCase(request.form(form_name)),Chk_badword(i))<>0 Then
Select Case Err_Message
Case "1"
Response.Write "<Script Language=JavaScript>alert(‘出錯了!表單 "&form_name&" 的值中包含非法字串!\n\n請不要在表單中出現: % & * # ( ) 等非法字元!‘);window.close();</Script>"
Case "2"
Response.Write "<Script Language=JavaScript>location.href=‘"&Err_Web&"‘</Script>"
Case "3"
Response.Write "<Script Language=JavaScript>alert(‘出錯了!參數 "&form_name&"的值中包含非法字串!\n\n請不要在表單中出現: % & * # ( ) 等非法字元!‘);location.href=‘"&Err_Web&"‘;</Script>"
End Select
Response.End
End If
NEXT
NEXT
end if
以上是另一種版本。
<%
Dim GetFlag Rem(提交方式)
Dim ErrorSql Rem(非法字元)
Dim RequestKey Rem(提交資料)
Dim ForI Rem(迴圈標記)
ErrorSql = "‘~;~and~(~)~exec~update~count~*~%~chr~mid~master~truncate~char~declare" Rem(每個敏感字元或者詞語請使用半形 "~" 格開)
ErrorSql = split(ErrorSql,"~")
If Request.ServerVariables("REQUEST_METHOD")="GET" Then
GetFlag=True
Else
GetFlag=False
End If
If GetFlag Then
For Each RequestKey In Request.QueryString
For ForI=0 To Ubound(ErrorSql)
If Instr(LCase(Request.QueryString(RequestKey)),ErrorSql(ForI))<>0 Then
response.write "<script>alert(""警告:\n請不要做任何嘗試"");location.href=""index.asp"";</script>"
Response.End
End If
Next
Next
Else
For Each RequestKey In Request.Form
For ForI=0 To Ubound(ErrorSql)
If Instr(LCase(Request.Form(RequestKey)),ErrorSql(ForI))<>0 Then
response.write "<script>alert(""警告:\n請不要做任何嘗試"");location.href=""index.asp"";</script>"
Response.End
End If
Next
Next
End If
%>
1、將上述代碼放入conn檔案中即可!
2、將上述代碼儲存為safe.asp,在conn檔案中引入也可!
防SQL注入代碼(ASP版)