php 实现php代码的加密解密

php 代码加密类,大家可以根据自己的需求进行修改,原类如下,是对之前的加密解密类的有一次修改,希望能分享给大家。本次在ubuntu下测试没有问题,与之前的版本的区别在于,这次的版本更加的通用性。

    <?php  
    /* 
     * @auther:wangyaofeng 
     * @time:2014.11.6 
     * @action:对php项目进行加密处理,注意如果项目中存在框架目录或没有必要加密的目录,请提前移出
     * */  
    class Encryption{  
        private $c='';//存储密文  
        private $s='',$q1,$q2,$q3,$q4,$q5,$q6;//存储生成的加密后的文件内容  
        //如果不设置一个值,isset会表示不存在;  
        private $file='';//读取文件的路径  
        private $source='',$target='';
        //构造函数,实例化时调用初始化全局变量;  
        public function __construct(){  
            //初始化全局变量  
            $this->initialVar();  
            //echo "hello \n";  
        }  
        /* 
        *@input  $property_name,$value 
        *@output  
        *   魔法方法,对变量进行设置值;可根据需求进行处理。若直接去除if判断表示可用设置任何属性的值,包括不存在的属性;
        */  
        public function __set($property_name,$value){  
            //定义过的变量;  
            if(isset($this->$property_name)){  
                $this->$property_name = $value;  
            }else{  
                //异常处理,处理未声明的变量赋值;可根据需求进行处理。  
                throw new Exception("property does not exist");  
            }  
        }  
        //魔法方法 取出变量的值;  
        public function __get($property_name){  
            if(isset($this->$property_name)){  
                return $this->$property_name;  
            }else{  
                //throw new Exception("property does not exist");  
                return NULL;  
            }  
        }  
        //取随机排序  
        private function RandAbc($length=""){//随机排序取回  
          $str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";  
          return str_shuffle($str);  
        }  
        //对明文内容进行加密处理
        private function ciphertext($filename){  
            //$filename='index.php';  
            $T_k1=$this->RandAbc();  
            $T_k2=$this->RandAbc();  
            $vstr=file_get_contents($filename);  
            $v1=base64_encode($vstr);  
            $c=strtr($v1,$T_k1,$T_k2);
            $this->c=$T_k1.$T_k2.$c;  
            return $this;  
        }
        //初始化变量  
        private function initialVar(){  
            $this->q1="O00O0O";//base64_decode  
            $this->q2="O0O000";//$c(原文经过strtr置换后的密文,由 目标字符+替换字符+base64_encode(‘原文内容’)构成)  
            $this->q3="O0OO00";//strtr  
            $this->q4="OO0O00";//substr  
            $this->q5="OO0000";//52  
            $this->q6="O00OO0";//urldecode解析过的字符串(n1zb/ma5\vt0i28-pxuqy*6%6Crkdg9_ehcswo4+f37j)

        }  
        //生成加密后的模板(复杂版本);  
        private function model(){  
            //$c = $this->c;
            //$this->initialVar();  
            $this->s='<?php $'.$this->q6.'=urldecode("%6E1%7A%62%2F%6D%615%5C%76%740%6928%2D%70%78%75%71%79%2A6%6C%72%6B%64%679%5F%65%68%63%73%77%6F4%2B%6637%6A");$'.  
            $this->q1.'=$'.$this->q6.'{3}.$'.$this->q6.'{6}.$'.$this->q6.'{33}.$'.$this->q6.'{30};$'.$this->q3.'=$'.$this->q6.'{33}.$'.$this->q6.'{10}.$'  
            .$this->q6.'{24}.$'.$this->q6.'{10}.$'.$this->q6.'{24};$'.$this->q4.'=$'.$this->q3.'{0}.$'.$this->q6.'{18}.$'.$this->q6.'{3}.$'.$this->q3.'{0}  
            .$'.$this->q3.'{1}.$'.$this->q6.'{24};$'.$this->q5.'=$'.$this->q6.'{7}.$'.$this->q6.'{13};$'.$this->q1.'.=$'.$this->q6.'{22}.$'.$this->q6.'{36}  
            .$'.$this->q6.'{29}.$'.$this->q6.'{26}.$'.$this->q6.'{30}.$'.$this->q6.'{32}.$'.$this->q6.'{35}.$'.$this->q6.'{26}.$'.$this->q6.'{30};  
            eval($'.$this->q1.'("'.base64_encode('$'.$this->q2.'="'.$this->c.'";
            eval(\'?>\'.$'.$this->q1.'($'.$this->q3.'($'.$this->q4.'($'.$this->q2.',$'.$this->q5.'*2),$'.$this->q4.'($'.$this->q2.',$'.$this->q5.',$'.$this->q5.'),  
            $'.$this->q4.'($'.$this->q2.',0,$'.$this->q5.'))));').'"));?>';  
            return $this;  
        }
        //创建加密文件  
        private function build($target){
            //$this->encodes("./index.php");  
            //$this->model();  
            $fpp1 = fopen($target,'w');  
            fwrite($fpp1,$this->s) or die('写入是失败!');
            fclose($fpp1);
            return $this;  
        }  
        //加密处理 连贯操作  
        public function encode($file,$target){  
            //$file = "index.php";  
            //连贯操作其实就是利用函数处理完后返回自身  
            $this->ciphertext($file)->model()->build($target);
            echo 'encode------'.$target.'-----ok<br/>';  
        }  
        //解密  
        public function decode($file,$target=''){
            //读取要解密的文件
            $fpp1 = file_get_contents($file);
            $this->decodeMode($fpp1)->build($target);
            echo 'decode------'.$target.'-----ok<br/>';
        }
        //解密模板,得到解密后的文本
        private function decodeMode($fpp1){
            //以eval为标志 截取为数组,前半部分为密文中的替换掉的函数名,后半部分为密文
            $m = explode('eval',$fpp1);
            //对系统函数的替换部分进行执行,得到系统变量
            $varStr = substr($m[0],strpos($m[0],'$'));
            //执行后,后续就可以使用替换后的系统函数名
            eval($varStr);
            //判断是否有密文
            if(!isset($m[1])){
                return $this;
            }

            //对密文进行截取 {$this->q4}  substr
            $star =  strripos($m[1],'(');
            $end = strpos($m[1],')');
            $str = ${$this->q4}($m[1],$star,$end);
            //对密文解密 {$this->q1}  base64_decode
            $str = ${$this->q1}($str);
            //截取出解密后的  核心密文
            $evallen = strpos($str,'eval');
            $str = substr($str,0,$evallen);
            //执行核心密文 使系统变量被赋予值 $O0O000
            eval($str);
            //并不能将如下段封装,因为 ${$this->qn} 并不能在全文中起作用
            $this->s = ${$this->q1}(
                ${$this->q3}(
                    ${$this->q4}(
                        ${$this->q2},${$this->q5}*2
                    ),
                    ${$this->q4}(
                        ${$this->q2},${$this->q5},${$this->q5}
                    ),
                    ${$this->q4}(
                        ${$this->q2},0,${$this->q5}
                    )
                )
            );
            return $this;
        }
        //递归读取并创建目标目录结构
        private function targetDir($target){
            if(!empty($target) )  {
                if(!file_exists($target)){
                    mkdir($target,0777,true);
                }else{
                    chmod($target,0777);
                }

            }
        }

        //递归解密 对指定文件夹下的php文件解密  
        public function decodeDir($source,$target=""){
            if(is_dir($source)){
                $this->targetDir($target);
                $dir = opendir($source);  
                while(false!=$file=readdir($dir))  
                {  
                    //列出所有文件并去掉'.'和'..' 此处用的实例为thinkphp框架,所以默认排除里Thinkphp目录,用户可以按照自己的需求设置
                    if($file!='.' && $file!='..' && $file !='ThinkPHP')
                    {  
                        $path = $target.DIRECTORY_SEPARATOR.$file;  
                        $sourcePath =  $source.DIRECTORY_SEPARATOR.$file;  
                        $this->decodeDir($sourcePath,$path);  
                    }  
                }  
          
            }else if(is_file($source)){  
                $extension=substr($source,strrpos($source,'.')+1);  
                if(strtolower($extension)=='php'){
                    $this->decode($source,$target);  
                }else{  
                    //不是php的文件不处理  
                    copy($source, $target);  
                }  
                //return;  
            }
        }  
        //递归加密 对指定文件夹下的php文件加密  
        public function encodeDir($source,$target){
            if(is_dir($source)){
                $this->targetDir($target);
                $dir = opendir($source);  
                while(false!=$file=readdir($dir))  
                {  
                    //列出所有文件并去掉'.'和'..'
                    if($file!='.' && $file!='..' && $file !='ThinkPHP')
                    {  
                        $path = $target.DIRECTORY_SEPARATOR.$file;  
                        $sourcePath =  $source.DIRECTORY_SEPARATOR.$file;  
                        $this->encodeDir($sourcePath,$path);  
                    }  
                }  

            }else if(is_file($source)){
                $extension=substr($source,strrpos($source,'.')+1);
                if(strtolower($extension)=='php'){
                    $this->encode($source,$target);
                }else{  
                    copy($source, $target);  
                }
            }  
        }
    }  
    $ob = new Encryption();  
    $ob->source = "/var/www/bookReservation";
    $ob->target = "/var/www/jiami/bookReservation";
    //解密指定文件  
    //$ob->decode('D:\\php\\WWW\\workspace\\weixin2\\Application\\Home\\Controller\\IndexController.class.php');  
      
    //$ob->decode('jiami.php');  
    //$ob->decode('dam6.php');  
    //对一个指定的文件目录进行加密  
    $ob->encodeDir($ob->source,$ob->target);
    //对一个指定的文件目录进行解密
    $ob->decodeDir($ob->target,"/var/www/jiami/bookReservationD");


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CTF/php后门分析中经常遇到加密压缩过的PHP文件, <? $O00OO0=urldecode("n1zb/ma5\vt0i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nT29QSU9vNFpjMjFtaFNpWmVvQXFIUDBDZW9ySTVxQlA1cjJ6NTV6ejVRMlJDVDh3TmFETWZhTXpmZEl0dVlNSEowRFdSWWlMdVlpdWROQWxnTkF6ZTJEVmhTWVpPb2s3c0VyWkNUc2FUbnRNbGZKdGE2N01aZnV5ZVAwQ2ZTUmFuQjVNQ05oaUJpRGlLa0RMdVlpdWROQWxnWWlFdWk5RUVSRGdnTjRJTzBSNGhTUnpmTjh0Q0tsd05UOHlnSGFpeFhYOXZ6bkJWK2Q3YnpYUXZ6QjlNZHJac0Vxa2ZCZnFRYXVyTzB3R0trZkx1WWl1ZE5BbGdpV09LTTlZRVJEV0oxV1dSWUlJZVRwdGMyOXpmYU10ZW9BcUhQMENlb3JJNXFCUDVyMno1NXp6NVEyUkNUOHdOYURNZmFNemZkSXR1TVJIUllNd0RSOUVFUkRnT29QSU9vNFpKM08xUXREcVFCdVpPb2s3c0VyWkNUV2dSWTFVNmYyZjVyTlc1cW5GNWV6MjU1eno1UTJSQ1Q4d05UOFpmU1JhbkI1TUNOaGdSWTFVSjFXV1JZSXRlTldFZHU1TERZaXVFUjlFRVJEZ2dONElPMlYwUUJQWk9vazdzRXJaQ1RXWUR1T1JEK0I4SUhCaWxvclpzRXFrZkJmcVFhdXJPMGlFdWk5WUR1T1JEb0FsZ1NmVlFGd01DS2x3TnRPTUFKUnFBYXVyZ1Q0WmMyOW9mZDl1blNNem4xV2d1Tjl1blNNem4xV2d1TjVQbkZwVENLbHdOSTBDc0VyL0dJPT0iO2V2YWwoJz8+Jy4kTzAwTzBPKCRPME9PMDAoJE9PME8wMCgkTzBPMDAwLCRPTzAwMDAqMiksJE9PME8wMCgkTzBPMDAwLCRPTzAwMDAsJE9PMDAwMCksJE9PME8wMCgkTzBPMDAwLDAsJE9PMDAwMCkpKSk7")); ?> 编译环境 ===================================================================== PHP : /usr/bin/php7.2 PHP_SAPI : cli PHP_VERSION : 7.2.9-1 ZEND_VERSION: 3.2.0 PHP_OS : Linux - Linux kali 4.14.0-kali3-amd64 #1 SMP Debian 4.14.17-1kali1 (2018-02-16) x86_64 INI actual : /root/temp/evalhook/tmp-php.ini More .INIs : CWD : /root/temp/evalhook Extra dirs : VALGRIND : Not used ===================================================================== TIME START 2019-01-04 07:46:12 ===================================================================== php -d extension=evalhook.so encoded_script.php 可直接dump大部分加密php文件中eval中的内容

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值