1   #!/usr/local/bin/php
  2  
  3   <?php
  4  
  5           if (isset($argv[2]) && $argv[2] != '') {
  6                   define('SEED', $argv[2]);
  7           }
  8           else {
  9                   define('SEED', rand(0, 100000));
  10           }
  11  
  12           function gen_name($old)
  13           {
  14                   return md5($old.SEED);
  15                   //return '_n_'.$old;
  16           }
  17  
  18           $functions = array();
  19    
  20     $php = file($argv[1]);
  21     $n = 1;
  22     for($x=0;$x<count($php);$x++)
  23     {
  24           $line = $php[$x];
  25           
  26           if ( preg_match("/^[ ]*\/\/.*?$/", trim($line)) ) {
  27                   //echo  "SKIPPING ".$php[$x];
  28                   $php[$x] = "";
  29                   continue; //cut comments
  30           }
  31           
  32           if ( preg_match("/^\/\*.*\*\/$/", trim($line)) ) {
  33                   //echo  "SKIPPING ".$php[$x];
  34                   $php[$x] = "";
  35                   continue; //cut comments
  36           }
  37           
  38           if(substr($line,0,10)=="function _")
  39           {
  40                   $dec_parts = explode(" ",$line,2);
  41                   $pp = explode("(",$dec_parts[1]);
  42                   $name = $pp[0];
  43                   $attribs="(".$pp[1];
  44                   
  45                   //echo "found func $name attribs: $attribs\n";
  46                           
  47                   $start = $x;
  48                   for($f=$x;$f<count($php);$f++)
  49                   {
  50                           if(substr($php[$f],0,1)=="}")
  51                           {
  52                             $end = $f;    
  53                             break;
  54                           }
  55                   }
  56                   if($start && $end && strlen($name))
  57                   {
  58                           $newname = "_".gen_name($n);
  59                           $n++;
  60                           $functions[$name] = array("start"=>$start,"end"=>$end,"attribs"=>$attribs,"newname"=>$newname);
  61                   }
  62           }
  63     }
  64   //  print_r($functions);
  65     //echo "<PRE>"; print_r($functions); echo "</PRE>";
  66  
  67     function GetVarName($s)
  68     {
  69             $alphabet = "abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  70            
  71             $name = "";
  72             $var_end = 0;
  73             $char = substr($s,$var_end,1);                
  74             if(substr($s,0,1)=="$")
  75             {
  76                   $var_end++;
  77                   $char = substr($s,$var_end,1);                  
  78             }                      
  79             while(is_numeric(strpos($alphabet,$char)) && strlen($char))
  80             {
  81                   $name .= $char;
  82                   $var_end++;
  83                   $char = substr($s,$var_end,1);                  
  84             }
  85           return $name;
  86     }
  87    
  88     function obscure_func($NewName,$Attribs,$code)
  89     {     
  90           global $functions;
  91                   
  92           $globals = array();
  93           
  94           $globals[] = '$this';
  95           $globals[] = '$_GET';
  96           $globals[] = '$_FILES';
  97           $globals[] = '$_POST';
  98           $globals[] = '$_COOKIE';
  99           $globals[] = '$_SERVER';
  100           
  101           $variables = array();
  102  
  103                   $new_code = array();
  104           for($x=0;$x<=count($code);$x++)
  105           {
  106                   $line = $code[$x];
  107                   $line = ltrim($line);
  108                   $line = str_replace("\t","",$line);
  109                   $g = strpos($line,"global");
  110                   if(is_numeric($g))
  111                   {
  112                           $vars = trim(substr($line,$g+7));
  113                           $vars = substr($vars,0,-1);
  114                           $v = explode(",",$vars);
  115                           for($z=0;$z<count($v);$z++)
  116                             $globals[] = ltrim($v[$z]);
  117                   }               
  118                   $new_code[$x] = $line;
  119           }
  120           $code = $new_code;
  121           for($x=0;$x<count($code);$x++)
  122           {               
  123                   foreach($functions as $name=>$attr)
  124                   {
  125                           $line = $code[$x];
  126                           $code[$x] = str_replace($name,$attr["newname"],$line);
  127                   }
  128           }
  129  
  130           $VarCount =0;
  131           if(strlen($Attribs)>3)
  132           {
  133                   $Attribs = trim($Attribs);
  134                   $Attribs = str_replace("\t","",$Attribs);
  135                   //echo "getting attribs from $Attribs\n";
  136             $a = explode(",",substr($Attribs,1,-1));
  137   //        echo "got attribs for func [$Attribs]:\n";
  138   //        var_dump($a);
  139             if (is_array($a) && $a[0] != '') {
  140                     foreach($a as $attr)
  141                   {
  142                           list($attr,$default) = explode('=', $attr);
  143                           //echo "attr: $attr / def = $default\n";
  144                           if ($default != '') {
  145                                   $defaults[$attr] = $default;
  146                                   //echo "stored defaults for $attr\n";
  147                           }
  148                                   $variables[$attr] = '$_'.gen_name($VarCount++);
  149                     }
  150                   }
  151           }
  152           
  153           for($x=0;$x<count($code);$x++)
  154           {               
  155                   $line = $code[$x];
  156                   if(!strlen($line))
  157                     continue;
  158                   $p = strpos($line,"$");                 
  159                   while($p>0)
  160                   {
  161                           if(substr($line,$p,2)!="$$")
  162                           {
  163                             $name=GetVarName(substr($line,$p));
  164                             if(strlen($name))
  165                             {
  166                                   $name = "$".trim($name);
  167                                   if(!in_array($name,$globals) && !array_key_exists($name,$variables))
  168                                   $variables[$name] = '$_'.gen_name($VarCount++);
  169                             }
  170                           }
  171                           $p = strpos($line,"$",$p+1);            
  172                   }               
  173           } 
  174           
  175           for($x=0;$x<count($code);$x++)
  176           { 
  177   //              print_r($variables);
  178                   foreach($variables as $v=>$varname)
  179                   {                       
  180                           //echo "strpos ".$code[$x].', '.$v."\n";
  181                           $p = strpos($code[$x],$v);
  182                           while(is_numeric($p))
  183                           {
  184                             $t = GetVarName(substr($code[$x],$p));
  185                             if('$'.$t == $v)
  186                             {     
  187                               $code[$x] = substr_replace($code[$x],$varname,$p,strlen($t)+1);
  188                             }
  189                             $p = strpos($code[$x],$v,$p+1);
  190                           }
  191                   }       
  192           }
  193           
  194           $o = "function $NewName"."(";
  195           if (is_array($a)) {
  196                   foreach($a as $attr)
  197                   {
  198                           list($attr,$default) = explode('=', $attr);
  199                       $av[] = ($variables[$attr].(isset($defaults[$attr]) ? '='.$defaults[$attr] : ''));
  200                   }
  201             }
  202           if(count($av)>0)
  203             $o .= implode(",",$av);
  204           $o .= ")";
  205           //echo "reversed: $o\n";
  206           $o .= implode(" ",$code);
  207           //$o = str_replace("\n","",$o);
  208           return $o;
  209     }
  210    
  211     $out = "";
  212     $outline = 0;
  213    
  214     $shuffled = array_rand($functions, count($functions));
  215   //  print_r($shuffled);
  216  
  217     foreach ($shuffled as $name) {
  218           $pos = $functions[$name];
  219     
  220     //foreach($functions as $name =>$pos)
  221     //{
  222            $dest = $pos["start"];
  223            $newname = $pos["newname"];
  224            if(!$outline)
  225              $outline = $dest;
  226           
  227  
  228            unset($code);
  229            for($x=$dest+1;$x<=$pos["end"];$x++)
  230            {
  231                   $code[] = $php[$x];
  232            }
  233            $newcode = obscure_func($newname,$pos["attribs"],$code);
  234            $out .= $newcode;
  235     }
  236     foreach($functions as $name=>$pos)
  237     {
  238           for($x=$pos["start"];$x<=$pos["end"];$x++)
  239           {
  240                   $php[$x] = "";                  
  241           }
  242     }
  243    
  244     $code =array();
  245     for($x=0;$x<count($php);$x++) {
  246           $line = $php[$x];
  247             foreach($functions as $name=>$attr)
  248             {
  249                   $line = str_replace($name,$attr["newname"],$line);
  250             }
  251             $code[$x] = $line;
  252           }
  253           $php = $code;
  254    
  255     $line=1;
  256    
  257     $tmp_file = fopen($argv[1].'_', 'w');
  258    
  259     for($x=0;$x<count($php);$x++)
  260     {     
  261           if($x==$outline) {
  262             //echo "$line: ".$out;
  263             fwrite($tmp_file, $out);
  264           }
  265           if(strlen($php[$x])) {
  266                   //echo "$line: ".ltrim($php[$x]);
  267                   fwrite($tmp_file, ltrim($php[$x]));
  268           }
  269           $line++;
  270     }
  271     fclose($tmp_file);
  272     rename($argv[1].'_', $argv[1]);
  273           
  274   ?>