方法一
<%'--------定義部份------------------
Dim XH_Post,XH_Get,XH_In,XH_Inf,XH_Xh,XH_db,XH_dbstr
'自定義需要過濾的字串,用 "|" 分隔
XH_In = "'|;|and|exec|insert|select|delete%20from|update|count|*|%|chr|mid|master|truncate|char|declare|drop%20table|from|net%20user|xp_cmdshell|/add|net%20localgroup%20administrators|Asc|char"
'對數據進行拆分
XH_Inf = split(XH_In,"|")
'--------POST部份_Top------------------
If Request.Form<>"" Then
For Each XH_Post In Request.Form
For XH_Xh=0 To Ubound(XH_Inf)
If Instr(LCase(Request.Form(XH_Post)),XH_Inf(XH_Xh))<>0 Then
Response.Write "<Script Language=JavaScript>alert('請不要在參數中包含非法字符嘗試注入!');</Script>"
Response.Write "非法操作!系統做了如下記錄↓<br>"
Response.Write "操作IP:"&Request.ServerVariables("REMOTE_ADDR")&"<br>"
Response.Write "操作時間:"&Now&"<br>"
Response.Write "操作頁面:"&Request.ServerVariables("URL")&"<br>"
Response.Write "提交方式:POST<br>"
Response.Write "提交參數:"&XH_Post&"<br>"
Response.Write "提交數據:"&Request.Form(XH_Post)
Response.End
End If
Next
Next
End If
'--------GET部份_Top-------------------
If Request.QueryString<>"" Then
For Each XH_Get In Request.QueryString
For XH_Xh=0 To Ubound(XH_Inf)
If Instr(LCase(Request.QueryString(XH_Get)),XH_Inf(XH_Xh))<>0 Then
Response.Write "<Script Language=JavaScript>alert('請不要在參數中包含非法字符嘗試注入!');</Script>"
Response.Write "非法操作!系統做了如下記錄↓<br>"
Response.Write "操作IP:"&Request.ServerVariables("REMOTE_ADDR")&"<br>"
Response.Write "操作時間:"&Now&"<br>"
Response.Write "操作頁面:"&Request.ServerVariables("URL")&"<br>"
Response.Write "提交方式:GET<br>"
Response.Write "提交參數:"&XH_Get&"<br>"
Response.Write "提交數據:"&Request.QueryString(XH_Get)
Response.End
End If
Next
Next
End If
%>
方法二
<%
'************** ASP SQL 防注入**************
If Request.Form<>"" Then StopInjection(Request.Form)
If Request.QueryString<>"" Then StopInjection(Request.QueryString)
If Request.Cookies<>"" Then StopInjection(Request.Cookies)
Function StopInjection(values)
For Each N_Get In values
Dim L_Get, L_Get2
For Each L_Get In values
L_Get2 = values(L_Get)
Set regEx = New RegExp
regEx.IgnoreCase = True
regEx.Global = True
regEx.Pattern = "(\bselect\b|\sand\s|script|\sdeclare\s)"
If regEx.Test(L_Get2) Then
Alert()
response.End()
End If
Set regEx = Nothing
Next
Next
End Function
Sub Alert()
Dim str
str = "<"&"Script Language=JavaScript"&">"
str = str & "alert('== 本網站安全助手檢測到你所提交的內容存在有危險字符,已經禁止本次提交 ==\n');window.close();"
str = str & "<"&"/Script"&">"
response.write str
End Sub
'***************************************************
%>