1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ScramblePrivateMethod.php |
4
|
|
|
* |
5
|
|
|
* @category Naneau |
6
|
|
|
* @package Obfuscator |
7
|
|
|
* @subpackage NodeVisitor |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Naneau\Obfuscator\Node\Visitor; |
11
|
|
|
|
12
|
|
|
use Naneau\Obfuscator\Node\Visitor\TrackingRenamerTrait; |
13
|
|
|
use Naneau\Obfuscator\Node\Visitor\SkipTrait; |
14
|
|
|
|
15
|
|
|
use Naneau\Obfuscator\Node\Visitor\Scrambler as ScramblerVisitor; |
16
|
|
|
use Naneau\Obfuscator\StringScrambler; |
17
|
|
|
|
18
|
|
|
use PhpParser\Node; |
19
|
|
|
|
20
|
|
|
use PhpParser\Node\Stmt\Class_ as ClassNode; |
21
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
22
|
|
|
use PhpParser\Node\Expr\MethodCall; |
23
|
|
|
use PhpParser\Node\Expr\StaticCall; |
24
|
|
|
use PhpParser\Node\Expr\Variable; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* ScramblePrivateMethod |
28
|
|
|
* |
29
|
|
|
* Renames private methods |
30
|
|
|
* |
31
|
|
|
* WARNING |
32
|
|
|
* |
33
|
|
|
* This method is not foolproof. This visitor scans for all private method |
34
|
|
|
* declarations and renames them. It then finds *all* method calls in the |
35
|
|
|
* class, and renames them if they match the name of a renamed method. If your |
36
|
|
|
* class calls a method of *another* class that happens to match one of the |
37
|
|
|
* renamed private methods, this visitor will rename it. |
38
|
|
|
* |
39
|
|
|
* @category Naneau |
40
|
|
|
* @package Obfuscator |
41
|
|
|
* @subpackage NodeVisitor |
42
|
|
|
*/ |
43
|
|
|
class ScramblePrivateMethod extends ScramblerVisitor |
44
|
|
|
{ |
45
|
|
|
use TrackingRenamerTrait; |
46
|
|
|
use SkipTrait; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Before node traversal |
50
|
|
|
* |
51
|
|
|
* @param Node[] $nodes |
52
|
|
|
* @return array |
53
|
|
|
**/ |
54
|
|
|
public function beforeTraverse(array $nodes) |
55
|
|
|
{ |
56
|
|
|
$this |
57
|
|
|
->resetRenamed() |
58
|
|
|
->skip($this->variableMethodCallsUsed($nodes)); |
59
|
|
|
|
60
|
|
|
$this->scanMethodDefinitions($nodes); |
61
|
|
|
|
62
|
|
|
return $nodes; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Check all variable nodes |
67
|
|
|
* |
68
|
|
|
* @param Node $node |
69
|
|
|
* @return void |
70
|
|
|
**/ |
71
|
|
|
public function enterNode(Node $node) |
72
|
|
|
{ |
73
|
|
|
if ($this->shouldSkip()) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Scramble calls |
78
|
|
|
if ($node instanceof MethodCall || $node instanceof StaticCall) { |
79
|
|
|
|
80
|
|
|
// Node wasn't renamed |
81
|
|
|
if (!$this->isRenamed($node->name)) { |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Scramble usage |
86
|
|
|
return $this->scramble($node); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Recursively scan for method calls and see if variables are used |
92
|
|
|
* |
93
|
|
|
* @param Node[] $nodes |
94
|
|
|
* @return void |
95
|
|
|
**/ |
96
|
|
|
private function variableMethodCallsUsed(array $nodes) |
97
|
|
|
{ |
98
|
|
|
foreach ($nodes as $node) { |
99
|
|
|
if ($node instanceof MethodCall && $node->name instanceof Variable) { |
100
|
|
|
// A method call uses a Variable as its name |
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Recurse over child nodes |
105
|
|
|
if (isset($node->stmts) && is_array($node->stmts)) { |
|
|
|
|
106
|
|
|
$used = $this->variableMethodCallsUsed($node->stmts); |
|
|
|
|
107
|
|
|
|
108
|
|
|
if ($used) { |
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Recursively scan for private method definitions and rename them |
119
|
|
|
* |
120
|
|
|
* @param Node[] $nodes |
121
|
|
|
* @return void |
122
|
|
|
**/ |
123
|
|
View Code Duplication |
private function scanMethodDefinitions(array $nodes) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
foreach ($nodes as $node) { |
126
|
|
|
// Scramble the private method definitions |
127
|
|
|
if ($node instanceof ClassMethod && ($node->type & ClassNode::MODIFIER_PRIVATE)) { |
128
|
|
|
|
129
|
|
|
// Record original name and scramble it |
130
|
|
|
$originalName = $node->name; |
131
|
|
|
$this->scramble($node); |
132
|
|
|
|
133
|
|
|
// Record renaming |
134
|
|
|
$this->renamed($originalName, $node->name); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Recurse over child nodes |
138
|
|
|
if (isset($node->stmts) && is_array($node->stmts)) { |
|
|
|
|
139
|
|
|
$this->scanMethodDefinitions($node->stmts); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: