#!/usr/bin/env python3 """nitro -- self-modifying-code rev. Fully static solve: never runs the binary. main() mprotects its own .enccode section RWX, XOR-decrypts secret_check with an 8-byte repeating key, then calls it. We replicate that decryption offline, then replay the flag keystream -- which is derived from secret_check's own decrypted bytes, so the code doubles as its own key material. """ from pwn import * import re, sys e = ELF('./nitro', checksec=False) # --- 1. replicate main()'s SMC decryption ------------------------------- key = e.read(e.sym['smc_key.0'], 8) # 1337c0debaadf00d start = e.sym['secret_check'] N = 0x15e # loop bound in main() dec = bytes(c ^ key[i & 7] for i, c in enumerate(e.read(start, N))) print(f'[*] smc key : {key.hex()}') print(f'[*] secret_check : {start:#x} .. {start+N:#x}') # --- 2. recover the password --------------------------------------------- # secret_check writes it one byte at a time: mov BYTE PTR [rbp+disp8], imm8 # -> opcode c6 45 . Take the longest run of consecutive # stack slots holding printable bytes; that run is the password. slots = sorted({d[0]: v[0] for d, v in re.findall(rb'\xc6\x45(.)(.)', dec, re.S)}.items()) best = run = [] for d, v in slots: if run and d == run[-1][0] + 1 and 0x20 <= v < 0x7f: run.append((d, v)) else: run = [(d, v)] if 0x20 <= v < 0x7f else [] if len(run) > len(best): best = run[:] pw = bytes(v for _, v in best).decode() print(f'[*] password : {pw}') # --- 3. replay the flag keystream ---------------------------------------- # k = secret_check[(i*7 + 3) % 0x15e] + i*5 + 0x6b (all mod 256) # flag[i] = enc_flag[i] ^ k enc = e.read(e.sym['enc_flag'], 0x2f) flag = bytes((enc[i] ^ ((dec[(i*7 + 3) % N] + i*5 + 0x6b) & 0xff)) & 0xff for i in range(0x2f)) print(f'[+] flag : {flag.decode()}') # --- 4. optional: emit the decrypted binary for clean decompilation ------- if '--patch' in sys.argv: e.write(start, dec); e.save('nitro.dec'); os.chmod('nitro.dec', 0o755) print('[*] wrote nitro.dec')