360提供的Asp版和Php版的防注入代码

 Php版本的:

PHP代码
  1. <?php  
  2. //Code By Safe3   
  3. function customError($errno$errstr$errfile$errline)  
  4. {   
  5.  echo "<b>Error number:</b> [$errno],error on line $errline in $errfile<br />";  
  6.  die();  
  7. }  
  8. set_error_handler("customError",E_ERROR);  
  9. $getfilter="'|(and|or)\\b.+?(>|<|=|in|like)|\\/\\*.+?\\*\\/|<\\s*script\\b|\\bEXEC\\b|UNION.+?Select|Update.+?SET|Insert\\s+INTO.+?VALUES|(Select|Delete).+?FROM|(Create|Alter|Drop|TRUNCATE)\\s+(TABLE|DATABASE)";  
  10. $postfilter="\\b(and|or)\\b.{1,6}?(=|>|<|\\bin\\b|\\blike\\b)|\\/\\*.+?\\*\\/|<\\s*script\\b|\\bEXEC\\b|UNION.+?Select|Update.+?SET|Insert\\s+INTO.+?VALUES|(Select|Delete).+?FROM|(Create|Alter|Drop|TRUNCATE)\\s+(TABLE|DATABASE)";  
  11. $cookiefilter="\\b(and|or)\\b.{1,6}?(=|>|<|\\bin\\b|\\blike\\b)|\\/\\*.+?\\*\\/|<\\s*script\\b|\\bEXEC\\b|UNION.+?Select|Update.+?SET|Insert\\s+INTO.+?VALUES|(Select|Delete).+?FROM|(Create|Alter|Drop|TRUNCATE)\\s+(TABLE|DATABASE)";  
  12. function StopAttack($StrFiltKey,$StrFiltValue,$ArrFiltReq){    
  13.   
  14. if(is_array($StrFiltValue))  
  15. {  
  16.     $StrFiltValue=implode($StrFiltValue);  
  17. }    
  18. if (preg_match("/".$ArrFiltReq."/is",$StrFiltValue)==1){     
  19.         //slog("<br><br>操作IP: ".$_SERVER["REMOTE_ADDR"]."<br>操作时间: ".strftime("%Y-%m-%d %H:%M:%S")."<br>操作页面:".$_SERVER["PHP_SELF"]."<br>提交方式: ".$_SERVER["REQUEST_METHOD"]."<br>提交参数: ".$StrFiltKey."<br>提交数据: ".$StrFiltValue);  
  20.         print "360websec notice:Illegal operation!";  
  21.         exit();  
  22. }        
  23. }    
  24. //$ArrPGC=array_merge($_GET,$_POST,$_COOKIE);  
  25. foreach($_GET as $key=>$value){   
  26.     StopAttack($key,$value,$getfilter);  
  27. }  
  28. foreach($_POST as $key=>$value){   
  29.     StopAttack($key,$value,$postfilter);  
  30. }  
  31. foreach($_COOKIE as $key=>$value){   
  32.     StopAttack($key,$value,$cookiefilter);  
  33. }  
  34. if (file_exists('update360.php')) {  
  35.     echo "请重命名文件update360.php,防止黑客利用<br/>";  
  36.     die();  
  37. }  
  38. function slog($logs)  
  39. {  
  40.   $toppath=$_SERVER["DOCUMENT_ROOT"]."/log.htm";  
  41.   $Ts=fopen($toppath,"a+");  
  42.   fputs($Ts,$logs."\r\n");  
  43.   fclose($Ts);  
  44. }  
  45. ?>  

Asp版本的:

ASP/Visual Basic代码
  1. <%   
  2. 'Code by safe3  
  3. On Error Resume Next  
  4. if request.querystring<>"" then call stophacker(request.querystring,"'|(and|or)\b.+?(>|<|=|in|like)|/\*.+?\*/|<\s*script\b|\bEXEC\b|UNION.+?Select|Update.+?SET|Insert\s+INTO.+?VALUES|(Select|Delete).+?FROM|(Create|Alter|Drop|TRUNCATE)\s+(TABLE|DATABASE)")  
  5. if request.Form<>"" then call stophacker(request.Form,"\b(and|or)\b.{1,6}?(=|>|<|\bin\b|\blike\b)|/\*.+?\*/|<\s*script\b|\bEXEC\b|UNION.+?Select|Update.+?SET|Insert\s+INTO.+?VALUES|(Select|Delete).+?FROM|(Create|Alter|Drop|TRUNCATE)\s+(TABLE|DATABASE)")  
  6. if request.Cookies<>"" then call stophacker(request.Cookies,"\b(and|or)\b.{1,6}?(=|>|<|\bin\b|\blike\b)|/\*.+?\*/|<\s*script\b|\bEXEC\b|UNION.+?Select|Update.+?SET|Insert\s+INTO.+?VALUES|(Select|Delete).+?FROM|(Create|Alter|Drop|TRUNCATE)\s+(TABLE|DATABASE)")   
  7. ms()  
  8. function stophacker(values,re)  
  9.  dim l_get, l_get2,n_get,regex,IP  
  10.  for each n_get in values  
  11.   for each l_get in values  
  12.    l_get2 = values(l_get)  
  13.    set regex = new regexp  
  14.    regex.ignorecase = true  
  15.    regex.global = true  
  16.    regex.pattern = re  
  17.    if regex.test(l_get2) then  
  18.                                 IP=Request.ServerVariables("HTTP_X_FORWARDED_FOR")  
  19.                                 If IP = "" Then   
  20.                                   IP=Request.ServerVariables("REMOTE_ADDR")  
  21.                                 end if  
  22.                                 'slog("<br><br>操作IP: "&ip&"<br>操作时间: " & now() & "<br>操作页面:"&Request.ServerVariables("URL")&"<br>提交方式: "&Request.ServerVariables("Request_Method")&"<br>提交参数: "&l_get&"<br>提交数据: "&l_get2)  
  23.     Response.Write "360websec notice:Illegal operation!"  
  24.     Response.end  
  25.    end if  
  26.    set regex = nothing  
  27.   next  
  28.  next  
  29. end function   
  30.   
  31. sub slog(logs)  
  32.         dim toppath,fs,Ts  
  33.         toppath = Server.Mappath("/log.htm")  
  34.                                 Set fs = CreateObject("scripting.filesystemobject")  
  35.                                 If Not Fs.FILEEXISTS(toppath) Then   
  36.                                     Set Ts = fs.createtextfile(toppath, True)  
  37.                                     Ts.close  
  38.                                 end if  
  39.                                     Set Ts= Fs.OpenTextFile(toppath,8)  
  40.                                     Ts.writeline (logs)  
  41.                                     Ts.Close  
  42.                                     Set Ts=nothing  
  43.                                     Set fs=nothing  
  44. end sub  
  45. sub ms()  
  46.         dim path,fs  
  47.         path = Server.Mappath("update360.asp")  
  48.         Set fs = CreateObject("scripting.filesystemobject")  
  49.         If Fs.FILEEXISTS(path) Then   
  50.         Response.Write "请重命名升级文件update360.asp防止黑客利用"  
  51.         Response.End  
  52.         end if  
  53.         Set fs=nothing  
  54. end sub  
  55. %>  


上一篇: php统计mysql记录条数
下一篇: 转载:六招教您提高B2B行业网站客户转化率
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: asp php
相关日志:
评论: 0 | 引用: 0 | 查看次数: 3727
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭

 广告位

↑返回顶部↑