分享
 
 
 

全面防御asp网站防黑客攻击

王朝asp·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

asp最脆弱的不是技术,而是防范黑客攻击。而无论怎样攻击,通过QueryString和form,只要这两处防范好了,问题就解决了

下面是我自家独创的一些代码,供大家参考

一.防通过querystring的sql攻击

一般sql能够攻击的页面通常是在参数为数字的页面

1.首先我们做一个警告子过程

'''''子过程功能:错误信息提示''''''''''''''''''

'''''参数说明:errmsg 错误信息说明 var 处理方式 1返回不刷新 2返回上页 3关闭页面''''''''''''''''''

Public sub alarm(errmsg,var)

response.Write("<table width=514 height=293 border=0 align=center cellpadding=0 cellspacing=0")

response.Write("<tr<td height=43<img src=images/error_bg.gif width=514 height=43</td</tr<tr")

response.Write("<td height=239 valign=top ")

response.Write("<table width='100%' height='100%' cellpadding=0 cellspacing=1 bgcolor='#cccccc' style='border-left:1px solid #DDDDDD;border-right:1px solid #DDDDDD'")

response.Write("<tr bgcolor='#FFFFFF'<td width='57%' align='center'<img src='images/error_show.gif' width='272' height='92'</td")

response.Write("<td width='43%' align='center'<div align='center' style='line-height:150%'<font color='#0099FF' style='font-size:9pt'对于操作失败我们表示抱歉!<br如果仍有问题,请给我们发送错误报告 </font</div</td</tr")

response.Write("<tr bgcolor='#FFFFFF'<td height=25 colspan=2

<strong<font color=#0099FF style='font-size:9pt'操作失败的可能原因:</font</strong</td</tr")

response.Write("<tr bgcolor='#FFFFFF'<td height=86 colspan=2")

response.Write("<table width='100%' border=0 cellspacing=0 cellpadding=0")

response.Write("<tr<td width='2%' </td")

response.Write("<td width='98%'<font style='font-size:9pt' color='#FF0000'"&errmsg&"</font</td")

response.Write("</tr</table</td</tr<tr align=center bgcolor='#FFFFFF'<td height=49 colspan=2")

if var=1 then

response.Write("<a href='javascript:history.go(-1)'<img src='images/error_enterenter.gif' width='61' height='21' style='cursor:hand' border='0'</a")

elseif var=2 then

response.Write("<a href='javascript:location.href='"&request.ServerVariables("HTTP_REFERER")&"'<img src='images/error_enterenter.gif' width='61' height='21' style='cursor:hand' border='0'</a")

elseif var=3 then

response.Write("<a href='javascript:window.close()'<img src='images/error_enterenter.gif' width='61' height='21' style='cursor:hand' border='0'</a")

end if

response.Write("</td</tr</table</td</tr<tr")

response.Write("<td height=9<img src='images/error_down.gif' width='514' height='9'</td</tr</table")

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("<script language=javascriptalert('地址栏中的参数错误,本页面将自动关闭');window.close();</script")

case 2

Response.Write "<Script Language=javascriptlocation.href='"&redi&"'</Script"

case 3

Response.Write "<Script Language=javascriptalert('地址栏中的参数错误,本页将自动导向其他页面');location.href='"&redi&"';</Script"

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 "<Script Language=javascriptalert('出现错误!参数 "&my_Cs(my_x)&" 的值中包含非法字符串!\n\n 请不要在参数中出现:;,and,select,update,insert,delete,chr 等非法字符!');window.close();</Script"

Case "2"

Response.Write "<Script Language=javascriptlocation.href='"&redi&"'</Script"

Case "3"

Response.Write "<Script Language=javascriptalert('出现错误!参数 "&my_Cs(my_x)&"的值中包含非法字符串!\n\n 请不要在参数中出现:;,and,select,update,insert,delete,chr 等非法字符!');location.href='"&redi&"';</Script"

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 "<Script Language=javascriptalert('出错了!表单包含非法字符串!\n\n请不要在表单中出现非法字符!');history.go(-1);</Script"

elseif var=2 then

qczc.Err_List"<li出错了!在您提交的表单中包含非法字符串<li请不要在表单中出现非法的字符串<li在此过程中,我们已将您的IP记录在案",1

end if

Response.End

End If

NEXT

NEXT

end if

end sub

使用方法,只要在提交表单的页面头部加一条语句就可以了

<%call safeform(2)%

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有