asp+access型网站防sql注入代码包括get、post、cookie过滤代码

2024-12-25 23:10:57
推荐回答(3个)
回答1:

<%
部份代码.
Function HTMLEncode(fString)
fString=replace(fString,";",";")
fString=replace(fString,"<","<")
fString=replace(fString,">",">")
fString=replace(fString,"\","\")
fString=replace(fString,"--","--")
fString=replace(fString,CHR(9)," ")
fString=replace(fString,CHR(10),"
")
fString=replace(fString,CHR(13),"")
fString=replace(fString,CHR(22),"")
fString=replace(fString,CHR(32)," ")
fString=replace(fString,CHR(34),""")'双引号
fString=replace(fString,CHR(39),"'")'单引号
HTMLEncode=fString
End Function

Dim Fy_Url,Fy_a,Fy_x,Fy_Cs(),Fy_Cl,Fy_Ts,Fy_Zx
Fy_Cl = 1 '处理方式:1=提示信息,2=转向页面,3=先提示再转向
Fy_Zx = "Error.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))),"%20and%20")<>0 or Instr(LCase(Request(Fy_Cs(Fy_x))),"%20select%20")<>0 or Instr(LCase(Request(Fy_Cs(Fy_x))),"%20update%20")<>0 or Instr(LCase(Request(Fy_Cs(Fy_x))),"%20chr")<>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))),"%20insert%20")<>0 or Instr(LCase(Request(Fy_Cs(Fy_x))),"%20mid")<>0 Or Instr(LCase(Request(Fy_Cs(Fy_x))),"master.")<>0 Then
Select Case Fy_Cl
Case "1"
Response.Write ""
Case "2"
Response.Write ""
Case "3"
Response.Write ""
End Select
Response.End
End If
End If
Next

squery=lcase(Request.ServerVariables("QUERY_STRING"))
sURL=lcase(Request.ServerVariables("HTTP_HOST"))
allquery=squery+sURL
if InStr(allquery,"%20")<>0 or InStr(allquery," ")<>0 or InStr(allquery,"%27")<>0 or InStr(allquery,"'")<>0 or InStr(allquery,"%a1a1")<>0 or InStr(allquery," ")<>0 or InStr(allquery,"%24")<>0 or InStr(allquery,"$")<>0 or InStr(allquery,"%3b")<>0 or InStr(allquery,";")<>0 or InStr(allquery,"%%")<>0 or InStr(allquery,"%3c")<>0 or InStr(allquery,"<")<>0 or InStr(allquery,"%3e")<>0 or InStr(allquery,">")<>0 or InStr(allquery,"%28")<>0 or InStr(allquery,"(")<>0 or InStr(allquery,"%29")<>0 or InStr(allquery,")")<>0 then
response.write "非法访问"
Response.End
end if
%>

回答2:

<%

Dim Query_Badword,Form_Badword,i,Err_Message,Err_Web,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="'‖%‖&‖*‖#‖(‖)‖=" '在这部份定义post非法参数,使用"‖"号间隔

'------定义部份 尾-----------------------------------------------------------------------
'
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 ""
Case "2"
Response.Write ""
Case "3"
Response.Write ""
End Select
Response.End
End If
NEXT
NEXT
End if

'-----对 post 表 单值的过滤.

if request.form<>"" then
Chk_badword=split(Form_Badword,"‖")
FOR EACH name IN Request.Form
for i=0 to ubound(Chk_badword)
If Instr(LCase(request.form(name)),Chk_badword(i))<>0 Then
Select Case Err_Message
Case "1"
Response.Write ""
Case "2"
Response.Write ""
Case "3"
Response.Write ""
End Select
Response.End
End If
NEXT
NEXT
end if
%>

回答3:

你自己去参考一下网上已经很成熟的系统中的防注入代码就可以了吧