Skip to content

Commit aa29231

Browse files
committed
fix: ImportsObfuscator to include complex cases
1 parent 08b0c2e commit aa29231

8 files changed

Lines changed: 295 additions & 59 deletions

File tree

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,20 +750,50 @@ exec(binascii.a2b_hex("".join(['7072696e-7428-2748-656c-6c6f2c20776f','726c6427-
750750

751751
#### ImportsObfuscator
752752

753-
Source: `import pathlib`
753+
Remove keyword `import`.
754754

755755
```python
756-
pathlib=__import__('pathlib')
756+
# import X
757+
X = __import__("X")
758+
759+
# import X as Y
760+
Y = __import__("X")
761+
762+
# import X, Y
763+
X = __import__("X")
764+
Y = __import__("Y")
765+
766+
# import X.Y
767+
X = __import__("X.Y")
768+
769+
# from X import Y
770+
Y = __import__("X", fromlist=["Y"]).Y
771+
772+
# from X import Y as Z
773+
Z = __import__("X", fromlist=["Y"]).Y
774+
775+
# from X import Y, Z
776+
Y = __import__("X", fromlist=["Y"]).Y
777+
Z = __import__("X", fromlist=["Z"]).Z
757778
```
758779

780+
> [!NOTE]
781+
> Does not support wildcard imports `*`, or local imports `from . import`.
782+
759783
#### CharFromDocObfuscator
760784

785+
Get a single character from a documentation.
786+
761787
Source: `print('h')`
762788

763789
```python
764790
print(oct.__doc__[8])
765791
```
766792

793+
> [!WARNING]
794+
> This is highly prone to error, if the documentation change between Python
795+
> versions, then this may break.
796+
767797
#### AddCommentsObfuscator
768798

769799
```python

pof/obfuscator/esoteric/doc.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,31 +183,31 @@ class CharFromDocObfuscator:
183183
)
184184

185185
@staticmethod
186-
def get_char_indexes(string, char):
186+
def _get_char_indexes(string, char):
187187
return [pos for pos, c in enumerate(string) if c == char]
188188

189189
@classmethod
190-
def try_find_doc_index(cls, char):
190+
def _try_find_doc_index(cls, char):
191191
# take a random builtin doc and search for index in it
192192
builtin = random.choice(cls.BUILTINS)
193193
doc = __builtins__[builtin].__doc__
194194
if not doc:
195195
msg = f"doc for {builtin} is not a string"
196196
raise PofError(msg)
197-
indexes = cls.get_char_indexes(doc, char)
197+
indexes = cls._get_char_indexes(doc, char)
198198
if len(indexes) == 0:
199199
msg = "char not present"
200200
raise PofError(msg)
201201
index = random.choice(indexes)
202202
return builtin, index
203203

204-
def obfuscate_char(self, char):
204+
def _obfuscate_char(self, char):
205205
builtin = None
206206
index = None
207207
retry = 3
208208
for _ in range(retry):
209209
try:
210-
builtin, index = self.try_find_doc_index(char)
210+
builtin, index = self._try_find_doc_index(char)
211211
except PofError:
212212
pass
213213
else:
@@ -239,7 +239,7 @@ def obfuscate_tokens(self, tokens):
239239
string = ast.literal_eval(tokval)
240240
if len(string) == 1:
241241
try:
242-
new_tokens = self.obfuscate_char(string)
242+
new_tokens = self._obfuscate_char(string)
243243
except PofError as e:
244244
logger.debug(str(e))
245245
except Exception: # noqa: BLE001

pof/obfuscator/esoteric/globals.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def obfuscate_tokens(cls, tokens):
4747
local_functions.append(tokval)
4848
prev_tokval = tokval
4949

50-
result = [] # obfuscated tokens
50+
result = []
5151
prev_tokval = None
5252
for index, (toknum, tokval, *_) in enumerate(tokens):
5353
new_tokens = [(toknum, tokval)]
@@ -60,7 +60,7 @@ def obfuscate_tokens(cls, tokens):
6060
# ensure it's not a definition
6161
and prev_tokval not in ["def", "class", "."]
6262
# ensure it's not an argument of a call
63-
and next_tokval not in ["="]
63+
and next_tokval != "="
6464
and tokval not in cls.RESERVED
6565
):
6666
new_tokens = [

0 commit comments

Comments
 (0)