Skip to content

Commit 41445d9

Browse files
committed
fix: StringsObfuscator
1 parent 8ee9561 commit 41445d9

18 files changed

Lines changed: 416 additions & 73 deletions

File tree

pof/obfuscator/boolean.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,30 @@ def obf_true():
4444
(RPAR, ")"),
4545
]
4646
case 3:
47-
# not False
47+
# (not False)
4848
return [
49+
(LPAR, "("),
4950
(NAME, "not"),
5051
(NAME, "False"),
52+
(RPAR, ")"),
5153
]
5254
case 4:
53-
# not not True
55+
# (not not True)
5456
return [
57+
(LPAR, "("),
5558
(NAME, "not"),
5659
(NAME, "not"),
5760
(NAME, "True"),
61+
(RPAR, ")"),
5862
]
5963
case 5:
60-
# "" in ""
64+
# ("" in "")
6165
return [
66+
(LPAR, "("),
6267
(STRING, "''"),
6368
(NAME, "in"),
6469
(STRING, "''"),
70+
(RPAR, ")"),
6571
]
6672
case 6:
6773
# bool(1)
@@ -97,25 +103,31 @@ def obf_false():
97103
(RPAR, ")"),
98104
]
99105
case 3:
100-
# not True
106+
# (not True)
101107
return [
108+
(LPAR, "("),
102109
(NAME, "not"),
103110
(NAME, "True"),
111+
(RPAR, ")"),
104112
]
105113
case 4:
106-
# not not False
114+
# (not not False)
107115
return [
116+
(LPAR, "("),
108117
(NAME, "not"),
109118
(NAME, "not"),
110119
(NAME, "False"),
120+
(RPAR, ")"),
111121
]
112122
case 5:
113-
# "" not in ""
123+
# ("" not in "")
114124
return [
125+
(LPAR, "("),
115126
(STRING, "''"),
116127
(NAME, "not"),
117128
(NAME, "in"),
118129
(STRING, "''"),
130+
(RPAR, ")"),
119131
]
120132
case 6:
121133
# bool(0)

pof/obfuscator/names.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,30 @@ def visit_Call(self, node: ast.Call) -> ast.Call:
192192
node.args[1] = ast.Constant(value=self.mapping[node.args[1].value])
193193
return node
194194

195+
def _is_globals_call(self, node: ast.expr) -> bool:
196+
"""Check if node is a call to globals()."""
197+
return (
198+
isinstance(node, ast.Call)
199+
and isinstance(node.func, ast.Name)
200+
and node.func.id == "globals"
201+
and not node.args
202+
)
203+
195204
def visit_Subscript(self, node: ast.Subscript) -> ast.Subscript:
196205
self.generic_visit(node)
197206
if (
198-
isinstance(node.value, ast.Attribute)
199-
and node.value.attr == "__dict__"
200-
and isinstance(node.slice, ast.Constant)
207+
isinstance(node.slice, ast.Constant)
201208
and isinstance(node.slice.value, str)
202209
and node.slice.value in self.mapping
210+
and (
211+
# __dict__['name'] pattern
212+
(
213+
isinstance(node.value, ast.Attribute)
214+
and node.value.attr == "__dict__"
215+
)
216+
# globals()['name'] pattern
217+
or self._is_globals_call(node.value)
218+
)
203219
):
204220
node.slice = ast.Constant(value=self.mapping[node.slice.value])
205221
return node
@@ -212,6 +228,7 @@ class NamesObfuscator:
212228
"__file__",
213229
"__name__",
214230
"__doc__",
231+
"__builtins__",
215232
"__package__",
216233
"__loader__",
217234
"__spec__",

pof/obfuscator/remove/print.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def obfuscate_tokens(tokens):
5353

5454
if inside_print:
5555
if print_par_depth == parenthesis_depth and (
56-
tokval not in ("(", "print") and prev_tokval != "print"
56+
tokval not in ("(", "print") and prev_tokval not in {"print", "."}
5757
): # check if still inside print
5858
inside_print = False
5959
else:

0 commit comments

Comments
 (0)