asp最脆弱的不是技术,而是防范黑客攻击
而无论怎样攻击,就两点
通过querystring和form
只要这两处防范好了,问题就解决了
下面是我自家独创的一些代码,供大家参考
一.防通过querystring的sql攻击
一般sql能够攻击的页面通常是在参数为数字的页面
1.首先我们做一个警告子过程
子过程功能:错误信息提示
参数说明:errmsg 错误信息说明 var 处理方式 1返回不刷新 2返回上页 3关闭页面
public sub alarm(errmsg,var)
response.write("
")
response.write("
")
response.write("
")
response.write("
")
response.write("
")
response.write("
对于操作失败我们表示抱歉! 如果仍有问题,请给我们发送错误报告
")
response.write("
操作失败的可能原因:
")
response.write("
")
response.write("
")
response.write("
")
response.write("
"&errmsg&"
")
response.write("
")
if var=1 then
response.write("")
elseif var=2 then
response.write("")
elseif var=3 then
response.write("")
end if
response.write("
")
response.write("
")
end sub
2.写一个验证数字的函数
函数功能:检测是否为数字并且数字是否有效
返 回 值:boolean
public function isinteger(para)
if isnumeric(para)=false then isinteger=false
dim str
dim l,i
if isnull(para) then
isinteger=false
exit function
end if
str=cstr(para)
if trim(str)="" then
isinteger=false
exit function
end if
l=len(str)
for i=1 to l
if mid(str,i,1)>"9" or mid(str,i,1)<"0" then
isinteger=false
exit function
end if
next
isinteger=true
end function
3.写一个对querysting参数是否为数字的验证过程
子过程功能,验证参数是否为数字
参数说明:manage 处理方式:1=提示信息并关闭页面,2=转向页面,3=先提示再转向 redi 出错时转向的页面,str:接受检测的变量
public sub integerok(manage,redi,str)
if isinteger(str)=false then
select case manage
case 1
response.write("")
case 2
response.write ""
case 3
response.write ""
end select
end if
end sub
4.写一个对qureystring整体验证的子过程
参数说明:manage 处理方式:1=提示信息并关闭页面,2=转向页面,3=先提示再转向 redi 出错时转向的页面
public sub saferush(manage,redi)
dim my_url,my_a,my_x,my_cs(),my_ts my_url:转接过来的url地址 my_a:获取url地址中用&隔开的字符串数组
my_url=request.servervariables("query_string") my_x:interger my_cs()动态数组
my_a=split(my_url,"&")
redim my_cs(ubound(my_a))
on error resume next
for my_x=0 to ubound(my_a)
my_cs(my_x) = left(my_a(my_x),instr(my_a(my_x),"=")-1)
next
for my_x=0 to ubound(my_cs)
if my_cs(my_x)<>"" then
if instr(lcase(request(my_cs(my_x))),"")<>0 or instr(lcase(request(my_cs(my_x))),"and")<>0 or instr(lcase(request(my_cs(my_x))),"select")<>0 or instr(lcase(request(my_cs(my_x))),"update")<>0 or instr(lcase(request(my_cs(my_x))),"chr")<>0 or instr(lcase(request(my_cs(my_x))),"delete%20from")<>0 or instr(lcase(request(my_cs(my_x))),";")<>0 or instr(lcase(request(my_cs(my_x))),"insert")<>0 or instr(lcase(request(my_cs(my_x))),"mid")<>0 or instr(lcase(request(my_cs(my_x))),"master.")<>0 then
select case manage
case "1"
response.write ""
case "2"
response.write ""
case "3"
response.write ""
end select
response.end
end if
end if
next
end sub
好了下面举实例说明:
假设存在一个http://www.webasp.net/tech/admin_news_tg.asp?class=1的页面
可以如此防范
call saferush(2,"../")
classid = request.querystring("classid")
call integerok(2,"../",classid)
二.对表单提交进行防范,很简单啦:)
sub safeform(var)
form_badword="∥%∥&∥*∥#∥@∥(∥)∥=" 在这部份定义post非法参数,使用"∥"号间隔
on error resume next
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
if var=1 then
response.write ""
elseif var=2 then
qczc.err_list"
出错了!在您提交的表单中包含非法字符串
请不要在表单中出现非法的字符串
在此过程中,我们已将您的ip记录在案",1
end if
response.end
end if
next
next
end if
end sub
使用方法,只要在提交表单的页面头部加一条语句就可以了
<%call safeform(2)%>
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!