OHFRICK
Challenge
wtf happened to my source code!!?
File given: code.py, about 63 KB, a single line, built entirely out of chains like eval(str([]==[])[...]...).
Deobfuscating without reading it
Trying to trace it by hand isn't going to happen. The easiest way to deal with it is to simply not let it run as intended. Whatever this eventually reconstructs, it has to call exec() on the real source to actually run it, so hooking exec before that happens turns the whole obfuscated payload into its own decoder:
import builtins
with open("code.py", "r", encoding="utf-8") as f:
content = f.read().strip()
original_exec = builtins.exec
def hooked_exec(obj, *a, **kw):
if isinstance(obj, str): print(obj)
builtins.exec = hooked_exec
builtins.eval(content)
Running this immediately prints the real, tiny source file underneath 63 KB of obfuscation. Now we get a nice, neat piece of code we can brute force:
f,i,o=input("f:"),int,ord
assert len(f)==14
T=[f[8]=="_",5**2*2==i(f[1:3]),f[0]==str(True)[-1],"reset"==f[5]+f[0]+f[9]+f[0]+f[3],f[12]+f[i(f[-1])**2]==str(credits).strip()[4:6]]
F=[i(f[4])+i(f[6])!=i(f[11]),o(f[7])+2!=o(f[0]),i(f[11])%2,str(None)[::-i(f[6])][:2]!=f[0]+f[10],i(f[6])**i(f[i(f[6])])>i(f[-1])]
assert all(T)
assert not any(F)
print("o"+f[-2])
Brute forcing the password
And brute force we shall:
import itertools
import string
digits = string.digits
evens = "02468"
chars = string.ascii_letters + string.digits + "_!@{}"
credits_str = str(credits).strip()
for f4, f6, f10, f11, f12, f13 in itertools.product(digits, digits, chars, evens, chars, digits):
f = f"e50t{f4}r{f6}c_s{f10}{f11}{f12}{f13}"
try:
idx_sq = int(f[13]) ** 2
if idx_sq >= len(f):
continue
if len(credits_str) >= 6 and f[12] + f[idx_sq] != credits_str[4:6]:
continue
if int(f[4]) + int(f[6]) != int(f[11]):
continue
step = int(f[6])
if step == 0:
continue
if str(None)[::-step][:2] != f[0] + f[10]:
continue
val_f6_idx = int(f[int(f[6])])
if int(f[6]) ** val_f6_idx > int(f[13]):
continue
print(f"Found match: {f}")
except (IndexError, ValueError, TypeError):
continue