From 390514af2aa3687fae90983df2dcae95a5cee4c3 Mon Sep 17 00:00:00 2001 From: Vyacheslav Gulyam Date: Fri, 10 Mar 2017 18:55:06 +0300 Subject: [PATCH 1/2] fix scramble private methods --- .../Node/Visitor/ScramblePrivateMethod.php | 14 ++++++++++++-- tests/before/MultipleClasses.php | 18 ++++++++++++++++++ tests/expected/MultipleClasses.php | 2 +- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/Naneau/Obfuscator/Node/Visitor/ScramblePrivateMethod.php b/src/Naneau/Obfuscator/Node/Visitor/ScramblePrivateMethod.php index 257c276..4b55682 100644 --- a/src/Naneau/Obfuscator/Node/Visitor/ScramblePrivateMethod.php +++ b/src/Naneau/Obfuscator/Node/Visitor/ScramblePrivateMethod.php @@ -45,6 +45,13 @@ class ScramblePrivateMethod extends ScramblerVisitor use TrackingRenamerTrait; use SkipTrait; + /** + * Active class + * + * @var ClassNode|bool + **/ + private $currentClassNode; + /** * Before node traversal * @@ -74,9 +81,12 @@ public function enterNode(Node $node) return; } - // Scramble calls - if ($node instanceof MethodCall || $node instanceof StaticCall) { + if ($node instanceof ClassNode) { + $this->currentClassNode = $node; + } + // Scramble calls + if (($node instanceof MethodCall && $node->var->name === 'this') || ($node instanceof StaticCall && $node->class instanceof Node\Name && $node->class->toString() === 'self')) { // Node wasn't renamed if (!$this->isRenamed($node->name)) { return; diff --git a/tests/before/MultipleClasses.php b/tests/before/MultipleClasses.php index f499458..44629e6 100644 --- a/tests/before/MultipleClasses.php +++ b/tests/before/MultipleClasses.php @@ -29,10 +29,28 @@ public function publicMethod() { echo "This is public method of second class"; $this->_privateProperty = parent::$publicProperty; } + + static public function anotherPublicMethod() { + } } class ThirdClass { + + static private function anotherPublicMethod() { + + } + public function __construct(SecondClass $secondObject) { $secondObject->publicMethod(); + $secondObject::anotherPublicMethod(); + } + + private function publicMethod() { + echo 'test'; + } + + protected function someFunc() { + $this->publicMethod(); + self::anotherPublicMethod(); } } \ No newline at end of file diff --git a/tests/expected/MultipleClasses.php b/tests/expected/MultipleClasses.php index a6e9e81..66b5b36 100644 --- a/tests/expected/MultipleClasses.php +++ b/tests/expected/MultipleClasses.php @@ -1,2 +1,2 @@ sp8839d9 = parent::$_protectedProperty; } public function publicMethod() { parent::publicMethod(); echo 'This is public method of second class'; $this->sp8839d9 = parent::$publicProperty; } } class ThirdClass { public function __construct(SecondClass $spb91639) { $spb91639->publicMethod(); } } \ No newline at end of file +class FirstClass { protected $_protectedProperty; public $publicProperty; protected function _protectedMethod() { echo 'This is protected method of first class'; } public function publicMethod() { echo 'This is public method of first class'; } } class SecondClass extends FirstClass { private $sp8839d9; protected function _protectedMethod() { parent::_protectedMethod(); echo 'This is protected method of second class'; $this->sp8839d9 = parent::$_protectedProperty; } public function publicMethod() { parent::publicMethod(); echo 'This is public method of second class'; $this->sp8839d9 = parent::$publicProperty; } public static function anotherPublicMethod() { } } class ThirdClass { private static function spe81a11() { } public function __construct(SecondClass $spb91639) { $spb91639->publicMethod(); $spb91639::anotherPublicMethod(); } private function sp70ab23() { echo 'test'; } protected function someFunc() { $this->sp70ab23(); self::spe81a11(); } } \ No newline at end of file From d0e2e69153c90c357fe2191ed0d5272c9de888c1 Mon Sep 17 00:00:00 2001 From: Vyacheslav Gulyam Date: Fri, 10 Mar 2017 19:25:10 +0300 Subject: [PATCH 2/2] added private property scrambler fixes --- .../Obfuscator/Node/Visitor/ScramblePrivateMethod.php | 11 ----------- .../Node/Visitor/ScramblePrivateProperty.php | 2 +- tests/before/MultipleClasses.php | 4 ++++ tests/expected/MultipleClasses.php | 2 +- 4 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/Naneau/Obfuscator/Node/Visitor/ScramblePrivateMethod.php b/src/Naneau/Obfuscator/Node/Visitor/ScramblePrivateMethod.php index 4b55682..3a00661 100644 --- a/src/Naneau/Obfuscator/Node/Visitor/ScramblePrivateMethod.php +++ b/src/Naneau/Obfuscator/Node/Visitor/ScramblePrivateMethod.php @@ -45,13 +45,6 @@ class ScramblePrivateMethod extends ScramblerVisitor use TrackingRenamerTrait; use SkipTrait; - /** - * Active class - * - * @var ClassNode|bool - **/ - private $currentClassNode; - /** * Before node traversal * @@ -81,10 +74,6 @@ public function enterNode(Node $node) return; } - if ($node instanceof ClassNode) { - $this->currentClassNode = $node; - } - // Scramble calls if (($node instanceof MethodCall && $node->var->name === 'this') || ($node instanceof StaticCall && $node->class instanceof Node\Name && $node->class->toString() === 'self')) { // Node wasn't renamed diff --git a/src/Naneau/Obfuscator/Node/Visitor/ScramblePrivateProperty.php b/src/Naneau/Obfuscator/Node/Visitor/ScramblePrivateProperty.php index 86c77cf..134705f 100644 --- a/src/Naneau/Obfuscator/Node/Visitor/ScramblePrivateProperty.php +++ b/src/Naneau/Obfuscator/Node/Visitor/ScramblePrivateProperty.php @@ -78,7 +78,7 @@ public function enterNode(Node $node) { if ($node instanceof PropertyFetch) { - if (!is_string($node->name)) { + if (!is_string($node->name) || $node->var->name !== "this") { return; } diff --git a/tests/before/MultipleClasses.php b/tests/before/MultipleClasses.php index 44629e6..d121ae2 100644 --- a/tests/before/MultipleClasses.php +++ b/tests/before/MultipleClasses.php @@ -36,6 +36,8 @@ static public function anotherPublicMethod() { class ThirdClass { + private $publicProperty; + static private function anotherPublicMethod() { } @@ -43,6 +45,7 @@ static private function anotherPublicMethod() { public function __construct(SecondClass $secondObject) { $secondObject->publicMethod(); $secondObject::anotherPublicMethod(); + $secondObject->publicProperty = 'test'; } private function publicMethod() { @@ -50,6 +53,7 @@ private function publicMethod() { } protected function someFunc() { + $this->publicProperty = 'test'; $this->publicMethod(); self::anotherPublicMethod(); } diff --git a/tests/expected/MultipleClasses.php b/tests/expected/MultipleClasses.php index 66b5b36..3afbca5 100644 --- a/tests/expected/MultipleClasses.php +++ b/tests/expected/MultipleClasses.php @@ -1,2 +1,2 @@ sp8839d9 = parent::$_protectedProperty; } public function publicMethod() { parent::publicMethod(); echo 'This is public method of second class'; $this->sp8839d9 = parent::$publicProperty; } public static function anotherPublicMethod() { } } class ThirdClass { private static function spe81a11() { } public function __construct(SecondClass $spb91639) { $spb91639->publicMethod(); $spb91639::anotherPublicMethod(); } private function sp70ab23() { echo 'test'; } protected function someFunc() { $this->sp70ab23(); self::spe81a11(); } } \ No newline at end of file +class FirstClass { protected $_protectedProperty; public $publicProperty; protected function _protectedMethod() { echo 'This is protected method of first class'; } public function publicMethod() { echo 'This is public method of first class'; } } class SecondClass extends FirstClass { private $sp8839d9; protected function _protectedMethod() { parent::_protectedMethod(); echo 'This is protected method of second class'; $this->sp8839d9 = parent::$_protectedProperty; } public function publicMethod() { parent::publicMethod(); echo 'This is public method of second class'; $this->sp8839d9 = parent::$publicProperty; } public static function anotherPublicMethod() { } } class ThirdClass { private $spa36ab6; private static function spe81a11() { } public function __construct(SecondClass $spb91639) { $spb91639->publicMethod(); $spb91639::anotherPublicMethod(); $spb91639->publicProperty = 'test'; } private function sp70ab23() { echo 'test'; } protected function someFunc() { $this->spa36ab6 = 'test'; $this->sp70ab23(); self::spe81a11(); } } \ No newline at end of file