示例#1
0
function obfuscate($filename)
{
    global $conf;
    global $parser, $traverser, $prettyPrinter;
    global $debug_mode;
    try {
        $source = php_strip_whitespace($filename);
        fprintf(STDERR, "Obfuscating %s%s", $filename, PHP_EOL);
        //var_dump( token_get_all($source));    exit;
        if ($source === '') {
            throw new Exception("Error obfuscating [{$filename}]: php_strip_whitespace returned an empty string!");
        }
        try {
            $stmts = $parser->parse($source . PHP_EOL . PHP_EOL);
            // PHP-Parser returns the syntax tree
        } catch (PhpParser\Error $e) {
            $source = file_get_contents($filename);
            $stmts = $parser->parse($source . PHP_EOL . PHP_EOL);
        }
        if ($debug_mode === 2) {
            $source = file_get_contents($filename);
            $stmts = $parser->parse($source . PHP_EOL . PHP_EOL);
        }
        if ($debug_mode) {
            var_dump($stmts);
        }
        $stmts = $traverser->traverse($stmts);
        //  Use PHP-Parser function to traverse the syntax tree and obfuscate names
        if ($conf->shuffle_stmts) {
            $last_inst = array_pop($stmts);
            $stmts = shuffle_statements($stmts);
            $stmts[] = $last_inst;
        }
        $code = $prettyPrinter->prettyPrintFile($stmts);
        //  Use PHP-Parser function to output the obfuscated source, taking the modified obfuscated syntax tree as input
        $code = trim($code);
        //  var_dump($stmts);
        if (isset($conf->strip_indentation) && $conf->strip_indentation) {
            $code = remove_whitespaces($code);
        }
        $endcode = substr($code, 6);
        $code = '<?php' . PHP_EOL;
        $code .= $conf->get_comment();
        // comment obfuscated source
        if (isset($conf->extract_comment_from_line) && isset($conf->extract_comment_to_line)) {
            $t_source = file($filename);
            for ($i = $conf->extract_comment_from_line - 1; $i < $conf->extract_comment_to_line; ++$i) {
                $code .= $t_source[$i];
            }
        }
        if (isset($conf->user_comment)) {
            $code .= '/*' . PHP_EOL . $conf->user_comment . PHP_EOL . '*/' . PHP_EOL;
        }
        $code .= $endcode;
        return $code;
    } catch (Exception $e) {
        fprintf(STDERR, "Obfuscator Parse Error [%s]:%s\t%s%s", $filename, PHP_EOL, $e->getMessage(), PHP_EOL);
        return null;
    }
}
示例#2
0
 private function shuffle_stmts(PhpParser\Node &$node)
 {
     global $conf;
     if ($conf->shuffle_stmts) {
         if (isset($node->stmts)) {
             $stmts = $node->stmts;
             $chunk_size = shuffle_get_chunk_size($stmts);
             if ($chunk_size <= 0) {
                 return false;
             }
             // should never occur!
             if (count($stmts) > 2 * $chunk_size) {
                 //    $last_inst      = array_pop($stmts);
                 $stmts = shuffle_statements($stmts);
                 //    $stmts[]        = $last_inst;
                 $node->stmts = $stmts;
                 return true;
             }
         }
     }
     return false;
 }