Skip to content

Commit

Permalink
cannot detect of which type an alias is by analysing the ast...
Browse files Browse the repository at this point in the history
so merged function and classes scramble names.
both or none has to be obfuscated if an alias is used.
  • Loading branch information
pk-fr committed Feb 27, 2020
1 parent 6343c37 commit e6608d0
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 48 deletions.
8 changes: 4 additions & 4 deletions include/check_version.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Author: Pascal KISSIAN
// Resume: http://pascal.kissian.net
//
// Copyright (c) 2015-2018 Pascal KISSIAN
// Copyright (c) 2015-2020 Pascal KISSIAN
//
// Published under the MIT License
// Consider it as a proof of concept!
Expand All @@ -18,7 +18,7 @@
if(!file_exists("$yakpro_po_base_directory/PHP-Parser/composer.json"))
{
fprintf(STDERR,"Error:\tPHP-Parser is not correctly installed!%sYou can try to use the following command:%s\t# %s%s",PHP_EOL,PHP_EOL,$php_parser_git_commandline,PHP_EOL);
exit(-1);
exit(21);
}

$t_composer = json_decode(file_get_contents("$yakpro_po_base_directory/PHP-Parser/composer.json")); //print_r($t_composer);
Expand All @@ -30,13 +30,13 @@
if (substr($php_parser_branch,0,2)!='4.')
{
fprintf(STDERR,"Error:\tWrong version of PHP-Parser detected!%sCurrently, only 4.x branch of PHP-Parser is supported!%s\tYou can try to use the following command:%s\t# %s%s",PHP_EOL,PHP_EOL,PHP_EOL,$php_parser_git_commandline,PHP_EOL);
exit(-1);
exit(22);
}

if (!version_compare(PHP_VERSION,$required_php_version,$operator))
{
fprintf(STDERR,"Error:\tPHP Version must be %s %s%s",$operator,$required_php_version,PHP_EOL);
exit(-1);
exit(23);
}


Expand Down
53 changes: 44 additions & 9 deletions include/classes/parser_extensions/my_node_visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Author: Pascal KISSIAN
// Resume: http://pascal.kissian.net
//
// Copyright (c) 2015-2019 Pascal KISSIAN
// Copyright (c) 2015-2020 Pascal KISSIAN
//
// Published under the MIT License
// Consider it as a proof of concept!
Expand All @@ -14,6 +14,7 @@
class MyNodeVisitor extends PhpParser\NodeVisitorAbstract // all parsing and replacement of scrambled names is done here!
{ // see PHP-Parser for documentation!
private $t_loop_stack = array();
private $t_node_stack = array();
private $current_class_name = null;
private $is_in_class_const_definition = false;

Expand Down Expand Up @@ -60,6 +61,12 @@ public function enterNode(PhpParser\Node $node)
global $conf;
global $t_scrambler;

if (count($this->t_node_stack))
{
$node->setAttribute('parent', $this->t_node_stack[count($this->t_node_stack)-1]);
}
$this->t_node_stack[] = $node;

if ($conf->obfuscate_loop_statement) // loop statements are replaced by goto ...
{
$scrambler = $t_scrambler['label'];
Expand Down Expand Up @@ -138,7 +145,7 @@ public function leaveNode(PhpParser\Node $node)

if ($conf->obfuscate_function_name)
{
$scrambler = $t_scrambler['function'];
$scrambler = $t_scrambler['function_or_class'];
if ($node instanceof PhpParser\Node\Stmt\Function_)
{
$name = $node->name->name;
Expand Down Expand Up @@ -205,7 +212,7 @@ public function leaveNode(PhpParser\Node $node)

if ($conf->obfuscate_class_name)
{
$scrambler = $t_scrambler['class'];
$scrambler = $t_scrambler['function_or_class'];
if ($node instanceof PhpParser\Node\Stmt\Class_)
{
$name = $this->get_identifier_name($node->name);
Expand Down Expand Up @@ -297,7 +304,7 @@ public function leaveNode(PhpParser\Node $node)

if ($conf->obfuscate_interface_name)
{
$scrambler = $t_scrambler['class'];
$scrambler = $t_scrambler['function_or_class'];
if ($node instanceof PhpParser\Node\Stmt\Interface_)
{
$name = $this->get_identifier_name($node->name);
Expand Down Expand Up @@ -352,7 +359,7 @@ public function leaveNode(PhpParser\Node $node)

if ($conf->obfuscate_trait_name)
{
$scrambler = $t_scrambler['class'];
$scrambler = $t_scrambler['function_or_class'];
if ($node instanceof PhpParser\Node\Stmt\Trait_)
{
$name = $this->get_identifier_name($node->name);
Expand Down Expand Up @@ -517,10 +524,34 @@ public function leaveNode(PhpParser\Node $node)
}
}
}

if ($node instanceof PhpParser\Node\Stmt\UseUse)
{
if ($conf->obfuscate_function_name || $conf->obfuscate_class_name)
{
if (!$conf->obfuscate_function_name || !$conf->obfuscate_class_name)
{
fprintf(STDERR, "Warning:[use alias] cannot determine at compile time if it is a function or a class alias".PHP_EOL."\tyou must obfuscate both functions and classes or none...".PHP_EOL."\tObfuscated code may not work!".PHP_EOL);
}
$scrambler = $t_scrambler['function_or_class'];
$name = $this->get_identifier_name($node->alias);
if ( is_string($name) && (strlen($name) !== 0) )
{
$r = $scrambler->scramble($name);
if ($r!==$name)
{
//$node->alias = $r;
$this->set_identifier_name($node->alias,$r);
$node_modified = true;
}
}
}
}


if ($conf->obfuscate_namespace_name)
{
$scrambler = $t_scrambler['class'];
$scrambler = $t_scrambler['function_or_class'];
if ( ($node instanceof PhpParser\Node\Stmt\Namespace_) || ($node instanceof PhpParser\Node\Stmt\UseUse) )
{
if (isset($node->name->parts))
Expand All @@ -541,19 +572,23 @@ public function leaveNode(PhpParser\Node $node)
}
}
}
/*
if ($node instanceof PhpParser\Node\Stmt\UseUse)
{
$name = $node->alias;
//$name = $node->alias;
$name = $this->get_identifier_name($node->alias);
if ( is_string($name) && (strlen($name) !== 0) )
{
$r = $scrambler->scramble($name);
if ($r!==$name)
{
$node->alias = $r;
//$node->alias = $r;
$this->set_identifier_name($node->alias,$r);
$node_modified = true;
}
}
}
*/
if ( ($node instanceof PhpParser\Node\Expr\FuncCall) || ($node instanceof PhpParser\Node\Expr\ConstFetch) )
{
if (isset($node->name->parts)) // not set when indirect call (i.e.function name is a variable value!)
Expand Down Expand Up @@ -1052,7 +1087,7 @@ public function leaveNode(PhpParser\Node $node)
}
}
}

array_pop($this->t_node_stack);
if ($node_modified) return $node;
}
}
Expand Down
32 changes: 24 additions & 8 deletions include/classes/scrambler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Author: Pascal KISSIAN
// Resume: http://pascal.kissian.net
//
// Copyright (c) 2015-2019 Pascal KISSIAN
// Copyright (c) 2015-2020 Pascal KISSIAN
//
// Published under the MIT License
// Consider it as a proof of concept!
Expand Down Expand Up @@ -158,6 +158,7 @@ function __construct($type,$conf,$target_directory)
$this->t_ignore_prefix = $t;
}
break;
/*
case 'function':
$this->case_sensitive = false;
$this->t_ignore = array_flip($this->t_reserved_function_names);
Expand All @@ -174,6 +175,7 @@ function __construct($type,$conf,$target_directory)
$this->t_ignore_prefix = $t;
}
break;
*/
case 'property':
$this->case_sensitive = true;
$this->t_ignore = array_flip($this->t_reserved_variable_names);
Expand All @@ -197,11 +199,25 @@ function __construct($type,$conf,$target_directory)
$this->t_ignore_prefix = $t;
}
break;
case 'class': // same instance is used for scrambling classes, interfaces, and traits. and namespaces... for aliasing
case 'function_or_class': // same instance is used for scrambling classes, interfaces, and traits. and namespaces... and functions ...for aliasing
$this->case_sensitive = false;
$this->t_ignore = array_flip($this->t_reserved_class_names);
$this->t_ignore = array_flip($this->t_reserved_function_names);
$t = get_defined_functions(); $t = array_map('strtolower',$t['internal']); $t = array_flip($t);
$this->t_ignore = array_merge($this->t_ignore,$t);
if (isset($conf->t_ignore_functions))
{
$t = $conf->t_ignore_functions; $t = array_map('strtolower',$t); $t = array_flip($t);
$this->t_ignore = array_merge($this->t_ignore,$t);
}
if (isset($conf->t_ignore_functions_prefix))
{
$t = $conf->t_ignore_functions_prefix; $t = array_map('strtolower',$t); $t = array_flip($t);
$this->t_ignore_prefix = $t;
}

$this->t_ignore = array_merge($this->t_ignore, array_flip($this->t_reserved_class_names));
$this->t_ignore = array_merge($this->t_ignore, array_flip($this->t_reserved_variable_names));
$this->t_ignore = array_merge($this->t_ignore, array_flip($this->t_reserved_function_names));
// $this->t_ignore = array_merge($this->t_ignore, array_flip($this->t_reserved_function_names));
$t = get_defined_functions(); $t = array_flip($t['internal']);
$this->t_ignore = array_merge($this->t_ignore,$t);
if ($conf->t_ignore_pre_defined_classes!='none')
Expand Down Expand Up @@ -236,7 +252,7 @@ function __construct($type,$conf,$target_directory)
if (isset($conf->t_ignore_classes_prefix))
{
$t = $conf->t_ignore_classes_prefix; $t = array_map('strtolower',$t); $t = array_flip($t);
$this->t_ignore_prefix = $t;
$this->t_ignore_prefix = array_merge($this->t_ignore_prefix,$t);
}
if (isset($conf->t_ignore_interfaces_prefix))
{
Expand Down Expand Up @@ -309,7 +325,7 @@ function __construct($type,$conf,$target_directory)
{
fprintf(STDERR,"Error:\tContext format has changed! run with --clean option!".PHP_EOL);
$this->context_directory = null; // do not overwrite incoherent values when exiting
exit(-1);
exit(1);
}
$this->t_scramble = $t[1];
$this->t_rscramble = $t[2];
Expand All @@ -322,7 +338,7 @@ function __construct($type,$conf,$target_directory)
function __destruct()
{
//print_r($this->t_scramble);
if (!$this->silent) fprintf(STDERR,"Info:\t[%-14s] scrambled \t: %8d%s",$this->scramble_type,count($this->t_scramble),PHP_EOL);
if (!$this->silent) fprintf(STDERR,"Info:\t[%-17s] scrambled \t: %8d%s",$this->scramble_type,count($this->t_scramble),PHP_EOL);
if (isset($this->context_directory)) // the destructor will save the current context
{
$t = array();
Expand Down Expand Up @@ -392,7 +408,7 @@ public function scramble($s)
if (!isset($this->t_scramble[$r]))
{
fprintf(STDERR,"Scramble Error: Identifier not found after 50 iterations!%sAborting...%s",PHP_EOL,PHP_EOL); // should statistically never occur!
exit;
exit(2);
}
}
return $this->case_sensitive ? $this->t_scramble[$r] : $this->case_shuffle($this->t_scramble[$r]);
Expand Down
16 changes: 8 additions & 8 deletions include/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Author: Pascal KISSIAN
// Resume: http://pascal.kissian.net
//
// Copyright (c) 2015-2019 Pascal KISSIAN
// Copyright (c) 2015-2020 Pascal KISSIAN
//
// Published under the MIT License
// Consider it as a proof of concept!
Expand Down Expand Up @@ -159,7 +159,7 @@ function create_context_directories($target_directory) // self-explanatory
if (!file_exists($dir))
{
fprintf(STDERR,"Error:\tCannot create directory [%s]%s",$dir,PHP_EOL);
exit(-1);
exit(51);
}
}
$target_directory = realpath($target_directory);
Expand Down Expand Up @@ -209,13 +209,13 @@ function obfuscate_directory($source_dir,$target_dir,$keep_mode=false) // self
if ($conf->follow_symlinks)
{
fprintf(STDERR,"Error:\t [%s] nested directories have been created!\nloop detected when follow_symlinks option is set to true!%s",$conf->max_nested_directory,PHP_EOL);
exit(-1);
exit(52);
}
}
if (!$dp = opendir($source_dir))
{
fprintf(STDERR,"Error:\t [%s] directory does not exists!%s",$source_dir,PHP_EOL);
exit(-1);
exit(53);
}
$t_dir = array();
$t_file = array();
Expand All @@ -230,7 +230,7 @@ function obfuscate_directory($source_dir,$target_dir,$keep_mode=false) // self
if ($source_stat===false)
{
fprintf(STDERR,"Error:\t cannot stat [%s] !%s",$source_path,PHP_EOL);
exit(-1);
exit(54);
}

if (isset($conf->t_skip) && is_array($conf->t_skip) && in_array($source_path,$conf->t_skip)) continue;
Expand All @@ -246,7 +246,7 @@ function obfuscate_directory($source_dir,$target_dir,$keep_mode=false) // self
if (unlink($target_path)===false)
{
fprintf(STDERR,"Error:\t cannot unlink [%s] !%s",$target_path,PHP_EOL);
exit(-1);
exit(55);
}
}
}
Expand All @@ -263,7 +263,7 @@ function obfuscate_directory($source_dir,$target_dir,$keep_mode=false) // self
if (unlink($target_path)===false)
{
fprintf(STDERR,"Error:\t cannot unlink [%s] !%s",$target_path,PHP_EOL);
exit(-1);
exit(56);
}
}
}
Expand Down Expand Up @@ -295,7 +295,7 @@ function obfuscate_directory($source_dir,$target_dir,$keep_mode=false) // self
if (isset($conf->abort_on_error))
{
fprintf(STDERR, "Aborting...%s",PHP_EOL);
exit;
exit(57);
}
}
file_put_contents($target_path,$obfuscated_str.PHP_EOL);
Expand Down
16 changes: 8 additions & 8 deletions include/retrieve_config_and_arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Author: Pascal KISSIAN
// Resume: http://pascal.kissian.net
//
// Copyright (c) 2015-2018 Pascal KISSIAN
// Copyright (c) 2015-2020 Pascal KISSIAN
//
// Published under the MIT License
// Consider it as a proof of concept!
Expand Down Expand Up @@ -32,7 +32,7 @@
$pos = stripos($help,'####'); if ($pos!==false) $help = substr($help,0,$pos);
$help = trim(str_replace(array('## ','`'),array('',''),$help));
echo "$help".PHP_EOL;
exit;
exit(11);
}

$pos = array_search('--config-file',$t_args);
Expand Down Expand Up @@ -209,7 +209,7 @@
break;
}
fprintf(STDERR,"Error:\tsource_directory and target_directory not specified!%s\tneither within command line parameter,%s\tnor in config file!%s",PHP_EOL,PHP_EOL,PHP_EOL);
exit(-1);
exit(12);
case 1:
$source_file = realpath($t_args[0]);
if (($source_file!==false) && file_exists($source_file))
Expand All @@ -224,7 +224,7 @@
if (is_dir($x))
{
fprintf(STDERR,"Error:\tTarget file [%s] is a directory!%s", ($x!==false) ? $x : $target_file,PHP_EOL);
exit(-1);
exit(13);
}
if ( is_readable($x) && is_writable($x) && is_file($x) && (file_get_contents($x)!=='') )
{
Expand All @@ -235,7 +235,7 @@
{
$x = realpath($target_file);
fprintf(STDERR,"Error:\tTarget file [%s] exists and is not an obfuscated file!%s", ($x!==false) ? $x : $target_file,PHP_EOL);
exit(-1);
exit(14);
}
fclose($fp);
}
Expand All @@ -251,17 +251,17 @@
if ( $target_directory=='')
{
fprintf(STDERR,"Error:\tTarget directory is not specified!%s",PHP_EOL);
exit(-1);
exit(15);
}
create_context_directories($target_directory);
break;
}
}
fprintf(STDERR,"Error:\tSource file [%s] is not readable!%s",($source_file!==false) ? $source_file : $t_args[0],PHP_EOL);
exit(-1);
exit(16);
default:
fprintf(STDERR,"Error:\tToo much parameters are specified, I do not know how to deal with that!!!%s",PHP_EOL);
exit(-1);
exit(17);
}
//print_r($t_args);

Expand Down
Loading

0 comments on commit e6608d0

Please sign in to comment.