你好! - 轻松快乐从此开始!

香山红叶网-计算机技术交流

当前位置: 主页 > 安全技术 >

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

时间:2012-09-29 21:11来源: 作者: 点击:等待统计信息……
摘要:PHP代码 ?php //CodeBySafe3 functioncustomError( $errno, $errstr, $errfile, $errline) { echo bErrornumber:/b[$errno],erroronline$errlinein$errfilebr/; die(); } set_error_handler( customError,E_ERROR); $getfilter= |(and|or)\\b.+?(||=|in|like……
1.将360_safe3.php传到要包含的文件的目录

2.在页面中加入防护,有两种做法,根据情况二选一即可:

香山红叶网- BUH+5*{6ON(UD9pP

a).在所需要防护的页面加入代码
require_once('360_safe3.php');
就可以做到页面防注入、跨站
如果想整站防注,就在网站的一个公用文件中,如数据库链接文件config.inc.php中!
添加require_once('360_safe3.php');来调用本代码

香山红叶网- LjhTwI%24%KzBI(R


b).在每个文件最前加上代码
在php.ini中找到:
Automatically add files before or after any PHP document.
auto_prepend_file = 360_safe3.php路径; 香山红叶网- Qs3Vl]OmU)A|]O:K


PHP代码
  • <?php  
  • //Code By Safe3   
  • function customError($errno, $errstr, $errfile, $errline)  
  • {   
  •  echo "<b>Error number:</b> [$errno],error on line $errline in $errfile<br />";  
  •  die();  
  • }  
  • set_error_handler("customError",E_ERROR);  
  • $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)";  
  • $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)";  
  • $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)";  
  • function StopAttack($StrFiltKey,$StrFiltValue,$ArrFiltReq){    
  •   
  • if(is_array($StrFiltValue))  
  • {  
  •     $StrFiltValue=implode($StrFiltValue);  
  • }    
  • if (preg_match("/".$ArrFiltReq."/is",$StrFiltValue)==1){     
  •         //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);  
  •         print "360websec notice:Illegal operation!";  
  •         exit();  
  • }        
  • }    
  • //$ArrPGC=array_merge($_GET,$_POST,$_COOKIE);  
  • foreach($_GET as $key=>$value){   
  •     StopAttack($key,$value,$getfilter);  
  • }  
  • foreach($_POST as $key=>$value){   
  •     StopAttack($key,$value,$postfilter);  
  • }  
  • foreach($_COOKIE as $key=>$value){   
  •     StopAttack($key,$value,$cookiefilter);  
  • }  
  • if (file_exists('update360.php')) {  
  •     echo "请重命名文件update360.php,防止黑客利用<br/>";  
  •     die();  
  • }  
  • function slog($logs)  
  • {  
  •   $toppath=$_SERVER["DOCUMENT_ROOT"]."/log.htm";  
  •   $Ts=fopen($toppath,"a+");  
  •   fputs($Ts,$logs."\r\n");  
  •   fclose($Ts);  
  • }  
  • ?>  
  •  

    asp使用方法:

    香山红叶网- nahX(]4JelXnlGxv

    1、将如下代码保存为360_safe3.asp 文件并上传到要包含的文件的目录 香山红叶网- E1WpoGK2lB:8c}3E

    2、在页面中引入上述 360_safe3.asp 文件:

    香山红叶网- +TVfUlRjfsXdTitg

    <!--#include virtual=“/360_safe3.asp”-->

    香山红叶网- cqSdK%JB{NKTNa~6

    如果想整站防注,就在网站的一个公用文件中,如数据库链接文件 conn.asp 中引入!

    香山红叶网- r1q3*zf|Baa4&v#4


    香山红叶网- 0xvb4|MblDkfHmV"

    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. %>  

    (责任编辑:admin)

    ------分隔线----------------------------
    栏目列表
    推荐内容
    二维码生成器
    AD-code726