全面防禦asp網站防黑客攻擊[我的原創] - 中國WEB開發者網絡 (http://www.webasp.net) -- 技術教程 (http://www.webasp.net/article/) --- 全面防禦asp網站防黑客攻擊[我的原創] (http://www.webasp.net/article/15/14603.htm) |
| -- 作者:雨浪 -- 發佈日期: 2004-11-06 |
| ##############################################
本文所有代碼版權歸屬[雨浪],轉載請聲明 ############################################## 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=javascript>alert('地址欄中的參數錯誤,本頁面將自動關閉');window.close();</script>") case 2 Response.Write "<Script Language=JavaScript>location.href='"&redi&"'</Script>" case 3 Response.Write "<Script Language=JavaScript>alert('地址欄中的參數錯誤,本頁將自動導向其他頁面');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=JavaScript>alert('出現錯誤!參數 "&my_Cs(my_x)&" 的值中包含非法字符串!\n\n 請不要在參數中出現:;,and,select,update,insert,delete,chr 等非法字符!');window.close();</Script>" Case "2" Response.Write "<Script Language=JavaScript>location.href='"&redi&"'</Script>" Case "3" Response.Write "<Script Language=JavaScript>alert('出現錯誤!參數 "&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=JavaScript>alert('出錯了!表單包含非法字符串!\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)%> |
| webasp.net |