ASP之防止外部数据提交的脚本

来源:互联网 发布:制作淘宝店铺模板 编辑:程序博客网 时间:2024/04/28 23:40

提交时可能会有人修改Script从本地提交,这样存在安全提交的问题,所以应该要求从服务器断路径提交,其他地址提交无效:  


<% 
            server_v1=Cstr(Request.ServerVariables("HTTP_REFERER")) 
            server_v2=Cstr(Request.ServerVariables("SERVER_NAME")) 
            if mid(server_v1,8,len(server_v2))<>server_v2 then 
            response.write "<br><br><center><table border=1 cellpadding=20 bordercolor=black 
            bgcolor=#EEEEEE width=450>" 
            response.write "<tr><td style=“font:9pt Verdana“>" 
            response.write "你提交的路径有误,禁止从站点外部提交数据请不要乱该参数!" 
            response.write "</td></tr></table></center>" 
            response.end 
            end if 
%>

 

 

 

 

 

请看下面的一个函数

<%
Function isSelfRefer()
Dim sHttp_Referer, sServer_Name
sHttp_Referer = CStr(Request.ServerVariables("HTTP_REFERER"))
sServer_Name = CStr(Request.ServerVariables("SERVER_NAME"))
If Mid(sHttp_Referer, 8, Len(sServer_Name)) = sServer_Name Then
IsSelfRefer = True
Else
IsSelfRefer = False
End If
End Function


if isSelfRefer() then
response.write "ok"
else
response.write "no"
end if

%>

把以上代码放到aa.asp,如果是直接输入网址或者是从外部网部链接到本站,http://doamain/aa.asp 就会显示NO,

如果系从本站链接到aa.asp,或通过表单提交到aa.asp,将会显示ok

可以防止一些伪造表单向站内提交数据

 

 

 

防止从外部提交数据的方法
第一种做法,屏蔽特殊字符和关键字
fqys=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 i= 0 to ubound(nothis)
if instr(FQYs,nothis(i))<>0 then
errc=true
end if
next
if errc then
response.write "<script language=""javascript"">"
response.write "parent.alert('很抱歉!你正在试图攻击本服务器或者想取得本服务器最高管理权!将直接转向首页..');"
response.write "self.location.href='default.asp';"
response.write "</script>"
response.end
end if

第二种可以防止客户从本地提交到网站上
<%
server_v1=Cstr(Request.ServerVariables("HTTP_REFERER"))
server_v2=Cstr(Request.ServerVariables("SERVER_NAME"))
if mid(server_v1,8,len(server_v2))<>server_v2 then
response.write "<br><br><center><table border=1 cellpadding=20 bordercolor=black bgcolor=#EEEEEE width=450>"
response.write "<tr><td style=font:9pt Verdana>"
response.write "你提交的路径有误,禁止从站点外部提交数据请不要乱该参数!"
response.write "</td></tr></table></center>"
response.end
end if
%>

第三。这样可以防止在输入框上打上or 1=1 的字样
If Instr(request("username"),"=")>0 or
Instr(request("username"),"%")>0 or
Instr(request("username"),chr(32))>0 or
Instr(request("username"),"?")>0 or
Instr(request("username"),"&")>0 or
Instr(request("username"),";")>0 or
Instr(request("username"),",")>0 or
Instr(request("username"),"'")>0 or
Instr(request("username"),"?")>0 or
Instr(request("username"),chr(34))>0 or
Instr(request("username"),chr(9))>0 or
Instr(request("username")," ")>0 or
Instr(request("username"),"$")>0 or
Instr(request("username"),">")>0 or
Instr(request("username"),"<")>0 or
Instr(request("username"),"""")>0 then

 

 

 

 

 

 

第一种做法,屏蔽特殊字符和关键字

fqys=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 i= 0 to ubound(nothis)

if instr(FQYs,nothis(i))<>0 then

errc=true

end if

next

if errc then

response.write ""

response.end

end if

第二种可以防止客户从本地提交到网站上

第三。这样可以防止在输入框上打上or 1=1 的字样

If Instr(request("username"),"=")>0 or

Instr(request("username"),"%")>0 or

Instr(request("username"),chr(32))>0 or

Instr(request("username"),"?")>0 or

Instr(request("username"),"&")>0 or

Instr(request("username"),";")>0 or

Instr(request("username"),",")>0 or

Instr(request("username"),"'")>0 or

Instr(request("username"),"?")>0 or

Instr(request("username"),chr(34))>0 or

Instr(request("username"),chr(9))>0 or

Instr(request("username")," ")>0 or

Instr(request("username"),"$")>0 or

Instr(request("username"),">")>0 or

Instr(request("username"),"<")>0 or

Instr(request("username"),"""")>0 then

 

 

 

 

<%
Function isSelfRefer()
Dim sHttp_Referer, sServer_Name
sHttp_Referer = CStr(Request.ServerVariables("HTTP_REFERER"))
sServer_Name = CStr(Request.ServerVariables("SERVER_NAME"))
If Mid(sHttp_Referer, 8, Len(sServer_Name)) = sServer_Name Then
IsSelfRefer = True
Else
IsSelfRefer = False
End If
End Function


if isSelfRefer() then
response.write "ok"
else
response.write "no"
end if

%>

把以上代码放到aa.asp,如果是直接输入网址或者是从外部网部链接到本站,http://doamain/aa.asp 就会显示NO,

如果系从本站链接到aa.asp,或通过表单提交到aa.asp,将会显示ok

可以防止一些伪造表单向站内提交数据