asp防SQL注入函数

来源:互联网 发布:防止arp攻击的软件 编辑:程序博客网 时间:2024/05/01 18:44
  1. 'SQL防注入函数,调用方法,在需要防注入的地方替换以前的request("XXXX")为SafeRequest("XXXX")   
  2. 'www.yongfa365.com   
  3.   
  4. Function SafeRequest(ParaValue)   
  5.     ParaValue = Trim(Request(ParaValue))   
  6.     If ParaValue = "" Then  
  7.         SafeRequest = ""  
  8.         Exit Function  
  9.     End If  
  10.     '要过滤的字符以","隔开   
  11.     LockValue = "',Select,Update,Delete,insert,Count(,drop table,truncate,Asc(,Mid(,char(,xp_cmdshell,exec master,net localgroup administrators,And,net user,Or"  
  12.     LockValue = Split(LockValue, ",")   
  13.     '判断是否有注入   
  14.     For i = 0 To UBound(LockValue)   
  15.         If InStr(LCase(ParaValue), LCase(LockValue(i)))>0 Then  
  16.             errmsg = 1   
  17.             Exit For  
  18.         End If  
  19.     Next  
  20.     '注入处理   
  21.     If errmsg = 1 Then  
  22.         Response.Write "<script language='javascript'>alert('可疑的SQL注入请求!');window.history.go(-1);</script>"  
  23.         response.End  
  24.     Else  
  25.         SafeRequest = ParaValue   
  26.     End If  
  27. End Function  

下边是用正则表达式过滤的例子

view plaincopy to clipboardprint?
  1.   
  2. 'SQL防注入函数,调用方法,在需要防注入的地方替换以前的request("XXXX")为SafeRequest("XXXX")      
  3. 'www.yongfa365.com      
  4.      
  5. Function SafeRequest(ParaValue)   
  6.     ParaValue = Trim(Request(ParaValue))   
  7.     '正则表达式过滤   
  8.     Set re = New RegExp  
  9.     '禁止使用的注入字符   
  10.     re.Pattern = "/'|Select|Update|Delete|insert|Count|drop table|truncate|Asc|Mid|char|xp_cmdshell|exec master|net localgroup administrators|And|net user|Or"  
  11.     re.IgnoreCase = True  
  12.     re.Global = True  
  13.     Set Matches = re.Execute(ParaValue)   
  14.     RegExpTest = Matches.count  
  15.     '注入处理   
  16.     If RegExpTest >0 Then  
  17.         Response.Write "<script language='javascript'>alert('可疑的SQL注入请求!');window.history.go(-1);</script>"  
  18.         response.End  
  19.     Else  
  20.         SafeRequest = ParaValue   
  21.     End If  
  22. End Function  
原创粉丝点击