python-obfuscation-framework 1.9.4__py3-none-any.whl → 1.10.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pof/cli_v2.py CHANGED
@@ -92,9 +92,6 @@ def add_obfuscation(tokens, args):
92
92
  if args.obf_constants:
93
93
  logger.debug("obfuscating constants")
94
94
  tokens = ConstantsObfuscator().obfuscate_tokens(tokens)
95
- if args.obf_definitions:
96
- logger.debug("obfuscating definitions")
97
- tokens = DefinitionsObfuscator().obfuscate_tokens(tokens)
98
95
  if args.obf_a85:
99
96
  logger.debug("obfuscating a85")
100
97
  tokens = ASCII85Obfuscator().obfuscate_tokens(tokens)
@@ -146,9 +143,6 @@ def add_obfuscation(tokens, args):
146
143
  if args.obf_names:
147
144
  logger.debug("obfuscating names")
148
145
  tokens = NamesObfuscator().obfuscate_tokens(tokens)
149
- if args.obf_names_rope:
150
- logger.debug("obfuscating names_rope")
151
- tokens = NamesRopeObfuscator().obfuscate_tokens(tokens)
152
146
  if args.obf_numbers:
153
147
  logger.debug("obfuscating numbers")
154
148
  tokens = NumberObfuscator().obfuscate_tokens(tokens)
pof/main.py CHANGED
@@ -45,7 +45,6 @@ from pof.obfuscator import (
45
45
  BuiltinsObfuscator,
46
46
  CommentsObfuscator,
47
47
  ConstantsObfuscator,
48
- DefinitionsObfuscator,
49
48
  DocstringObfuscator,
50
49
  ExceptionObfuscator,
51
50
  GlobalsObfuscator,
@@ -56,7 +55,6 @@ from pof.obfuscator import (
56
55
  NumberObfuscator,
57
56
  PrintObfuscator,
58
57
  StringsObfuscator,
59
- VariablesObfuscator,
60
58
  XORObfuscator,
61
59
  )
62
60
  from pof.stager import ImageStager, RC4Stager
@@ -123,8 +121,6 @@ class Obfuscator(BaseObfuscator):
123
121
  generator=ex_generator,
124
122
  ).obfuscate_tokens(tokens)
125
123
 
126
- tokens = DefinitionsObfuscator().obfuscate_tokens(tokens)
127
-
128
124
  # configure generator
129
125
  # generator = alphabet_generator()
130
126
  gen_dict = {
@@ -145,42 +141,7 @@ class Obfuscator(BaseObfuscator):
145
141
  obf_builtins_rate=0.3,
146
142
  ).obfuscate_tokens(tokens)
147
143
 
148
- # FIXME (deoktr): breaks !
149
- # for detailed explanation just consider the following:
150
- # ```
151
- # import module
152
- # class Bar:
153
- # def __init__(self):
154
- # self.foo = getattr(config, "FOO", True)
155
- # module.imported.function(self.foo)
156
- # ```
157
- # in this case the first instance of `foo` will successfully be
158
- # obfuscated (as it should) but then with the getattr it's marked has
159
- # "imported" because it's the result of a builtin function, but notice
160
- # that it's not a "simple" variable but rather it's a class attribute
161
- # and has a 'self' behind it, so if the variable is marked has imported
162
- # and a `.` is placed behind it, it won't be changed, this is the case
163
- # in the function call, which is itself imported, and thus set the
164
- # imported attribute for itself and all the parameters given to it
165
- # this is because we don't keep track of the level of the imported
166
- #
167
- # FIXME (deoktr): breaks !
168
- # Another problem is related to result of function, this is, ofc very
169
- # hard to deal with, but if a function returns an object, such has for
170
- # example an object of an imported class, which attribute are not
171
- # obfuscatable, then it breaks.
172
- # ```
173
- # import foo
174
- # def a():
175
- # return foo.bar()
176
- # x = a()
177
- # x.baz()
178
- # ```
179
- # In this context, `baz` would be obfuscated, but it shouldn't because
180
- # the function is part of the `foo` imported module
181
- # tokens = NamesObfuscator(generator=generator).obfuscate_tokens(tokens)
182
- # TODO (deoktr): use alternative variable obfuscator using the AST
183
- # tokens = VariablesObfuscator().obfuscate_tokens(tokens)
144
+ tokens = NamesObfuscator(generator=generator).obfuscate_tokens(tokens)
184
145
 
185
146
  tokens = GlobalsObfuscator().obfuscate_tokens(tokens)
186
147
  tokens = BuiltinsObfuscator().obfuscate_tokens(tokens)
@@ -241,9 +202,7 @@ class Obfuscator(BaseObfuscator):
241
202
  tokens = self._get_tokens(source)
242
203
  tokens = CommentsObfuscator().obfuscate_tokens(tokens)
243
204
  generator = BasicGenerator.alphabet_generator()
244
- # tokens = NamesObfuscator(generator=generator).obfuscate_tokens(tokens)
245
- tokens = VariablesObfuscator(generator=generator).obfuscate_tokens(tokens)
246
- tokens = DefinitionsObfuscator(generator=generator).obfuscate_tokens(tokens)
205
+ tokens = NamesObfuscator(generator=generator).obfuscate_tokens(tokens)
247
206
  tokens = IndentsObfuscator().obfuscate_tokens(tokens)
248
207
  tokens = NewlineObfuscator().obfuscate_tokens(tokens)
249
208
  return self._untokenize(tokens)
@@ -309,18 +268,7 @@ class Obfuscator(BaseObfuscator):
309
268
  tokens = CommentsObfuscator().obfuscate_tokens(tokens)
310
269
  tokens = ExceptionObfuscator(generator=generator).obfuscate_tokens(tokens)
311
270
  tokens = LoggingObfuscator(generator=generator).obfuscate_tokens(tokens)
312
- # FIXME (deoktr): when placed BEFORE NamesObfuscator it breaks the code
313
- # tokens = ConstantsObfuscator(
314
- # generator=generator,
315
- # obf_number_rate=1,
316
- # # FIXME (deoktr): breaks if obf_string_rate=1 with NamesObfuscator
317
- # obf_string_rate=0,
318
- # # FIXME (deoktr): breaks if obf_builtins_rate=1 with NamesObfuscator
319
- # obf_builtins_rate=0,
320
- # ).obfuscate_tokens(tokens)
321
- # tokens = NamesObfuscator(generator=generator).obfuscate_tokens(tokens)
322
- tokens = VariablesObfuscator(generator=generator).obfuscate_tokens(tokens)
323
- tokens = DefinitionsObfuscator(generator=generator).obfuscate_tokens(tokens)
271
+ tokens = NamesObfuscator(generator=generator).obfuscate_tokens(tokens)
324
272
  tokens = ConstantsObfuscator(
325
273
  generator=generator,
326
274
  obf_number_rate=1,
@@ -384,10 +332,9 @@ class Obfuscator(BaseObfuscator):
384
332
  # tokens = ConstantsObfuscator(generator=generator).obfuscate_tokens(tokens)
385
333
 
386
334
  tokens = CommentsObfuscator().obfuscate_tokens(tokens)
387
- # tokens = DeepEncryptionEvasion().add_evasion(tokens) # TODO (deoktr): fix
388
- # tokens = NamesObfuscator(
389
- # generator=AdvancedGenerator.fixed_length_generator(),
390
- # ).obfuscate_tokens(tokens)
335
+ tokens = NamesObfuscator(
336
+ generator=AdvancedGenerator.fixed_length_generator(),
337
+ ).obfuscate_tokens(tokens)
391
338
  tokens = BooleanObfuscator().obfuscate_tokens(tokens)
392
339
  tokens = IndentsObfuscator().obfuscate_tokens(tokens)
393
340
  tokens = NewlineObfuscator().obfuscate_tokens(tokens)
@@ -26,7 +26,6 @@ from .compression.lzma import LzmaObfuscator
26
26
  from .compression.zlib import ZlibObfuscator
27
27
  from .constants import ConstantsObfuscator
28
28
  from .controlflow.control_flow_flatten import ControlFlowFlattenObfuscator
29
- from .definitions import DefinitionsObfuscator
30
29
  from .encoding.a85 import ASCII85Obfuscator
31
30
  from .encoding.b16 import Base16Obfuscator
32
31
  from .encoding.b32 import Base32Obfuscator
@@ -45,7 +44,6 @@ from .junk.add_comments import AddCommentsObfuscator
45
44
  from .junk.add_newlines import AddNewlinesObfuscator
46
45
  from .junk.dead_code import DeadCodeObfuscator
47
46
  from .names import NamesObfuscator
48
- from .names_rope import NamesRopeObfuscator
49
47
  from .numbers import NumberObfuscator
50
48
  from .other.tokens import TokensObfuscator
51
49
  from .remove.comments import CommentsObfuscator
@@ -59,7 +57,6 @@ from .stegano.ipv6encoding import IPv6Obfuscator
59
57
  from .stegano.macencoding import MACObfuscator
60
58
  from .stegano.uuidencoding import UUIDObfuscator
61
59
  from .strings import StringsObfuscator
62
- from .variables import VariablesObfuscator
63
60
 
64
61
  __all__ = [
65
62
  "ASCII85Obfuscator",
@@ -81,7 +78,6 @@ __all__ = [
81
78
  "ControlFlowFlattenObfuscator",
82
79
  "DeadCodeObfuscator",
83
80
  "DeepEncryptionObfuscator",
84
- "DefinitionsObfuscator",
85
81
  "DocstringObfuscator",
86
82
  "ExceptionObfuscator",
87
83
  "ExtractVariablesObfuscator",
@@ -95,7 +91,6 @@ __all__ = [
95
91
  "LzmaObfuscator",
96
92
  "MACObfuscator",
97
93
  "NamesObfuscator",
98
- "NamesRopeObfuscator",
99
94
  "NewlineObfuscator",
100
95
  "NumberObfuscator",
101
96
  "PrintObfuscator",
@@ -105,7 +100,6 @@ __all__ = [
105
100
  "StringsObfuscator",
106
101
  "TokensObfuscator",
107
102
  "UUIDObfuscator",
108
- "VariablesObfuscator",
109
103
  "WhitespaceObfuscator",
110
104
  "XORObfuscator",
111
105
  "ZlibObfuscator",
@@ -147,17 +147,37 @@ class DeadCodeObfuscator:
147
147
  tokens.append((DEDENT, ""))
148
148
  return tokens
149
149
 
150
- def _generate_dead_if_tokens(self, indent_level: int):
150
+ @staticmethod
151
+ def _get_false_cond():
151
152
  false_conds = [
152
153
  [(NAME, "False")],
153
154
  [(NUMBER, "0")],
154
155
  [(STRING, '""')],
155
156
  [(NAME, "None")],
157
+ [
158
+ (NUMBER, str(random.randint(1, 50))),
159
+ (OP, ">"),
160
+ (NUMBER, str(random.randint(51, 100))),
161
+ ],
162
+ [
163
+ (NUMBER, str(random.randint(1, 50))),
164
+ (OP, "=="),
165
+ (NUMBER, str(random.randint(51, 100))),
166
+ ],
167
+ [(NAME, "not"), (NAME, "True")],
168
+ [(NAME, "True"), (NAME, "and"), (NAME, "False")],
169
+ [(NUMBER, "0"), (OP, "*"), (NUMBER, str(random.randint(1, 999)))],
170
+ [(NAME, "len"), (OP, "("), (STRING, '""'), (OP, ")")],
171
+ [(NAME, "bool"), (OP, "("), (NUMBER, "0"), (OP, ")")],
172
+ [(OP, "("), (OP, ")"), (NAME, "and"), (NAME, "True")],
156
173
  ]
174
+ return random.choice(false_conds)
175
+
176
+ def _generate_dead_if_tokens(self, indent_level: int):
157
177
  inner_indent = " " * (indent_level + 1)
158
178
 
159
179
  tokens = [(NAME, "if")]
160
- tokens.extend(random.choice(false_conds))
180
+ tokens.extend(self._get_false_cond())
161
181
  tokens.extend(
162
182
  [
163
183
  (OP, ":"),
@@ -171,7 +191,7 @@ class DeadCodeObfuscator:
171
191
  num_elif = random.randint(0, max(0, self.max_branches - 1))
172
192
  for _ in range(num_elif):
173
193
  tokens.append((NAME, "elif"))
174
- tokens.extend(random.choice(false_conds))
194
+ tokens.extend(self._get_false_cond())
175
195
  tokens.extend(
176
196
  [
177
197
  (OP, ":"),
@@ -191,6 +211,10 @@ class DeadCodeObfuscator:
191
211
  empty_choices = [
192
212
  [(OP, "["), (OP, "]")],
193
213
  [(NAME, "range"), (OP, "("), (NUMBER, "0"), (OP, ")")],
214
+ [(OP, "("), (OP, ")")],
215
+ [(OP, "{"), (OP, "}")],
216
+ [(STRING, '""')],
217
+ [(NAME, "set"), (OP, "("), (OP, ")")],
194
218
  ]
195
219
 
196
220
  tokens = [
@@ -212,13 +236,15 @@ class DeadCodeObfuscator:
212
236
 
213
237
  def _generate_dead_while_tokens(self, indent_level: int):
214
238
  inner_indent = " " * (indent_level + 1)
215
- tokens = [
216
- (NAME, "while"),
217
- (NAME, "False"),
218
- (OP, ":"),
219
- (NEWLINE, "\n"),
220
- (INDENT, inner_indent),
221
- ]
239
+ tokens = [(NAME, "while")]
240
+ tokens.extend(self._get_false_cond())
241
+ tokens.extend(
242
+ [
243
+ (OP, ":"),
244
+ (NEWLINE, "\n"),
245
+ (INDENT, inner_indent),
246
+ ],
247
+ )
222
248
  tokens.extend(self._body_tokens())
223
249
  tokens.append((DEDENT, ""))
224
250
  return tokens
@@ -301,7 +327,7 @@ class DeadCodeObfuscator:
301
327
 
302
328
  if toknum == OP and tokval == "@":
303
329
  in_decorator = True
304
- if in_decorator and toknum == NEWLINE:
330
+ if in_decorator and toknum == NAME and tokval in ("def", "class"):
305
331
  in_decorator = False
306
332
 
307
333
  result.append((toknum, tokval))