← all posts

· 3 min read · by Eav Puthcambo

A Python program hidden inside itself, many times over

An obfuscated Python challenge decrypts a layer, runs it, and repeats, with anti-debug that silently corrupts the key if it sees you. The weak spot is the one library it forgot to protect.

Inferior Student: Nothing Stay, LYKN CTF 2026 (reversing)

The handout is challl.py: 1.2 MB of unicode-obfuscated Python with heavy anti-debug. It's a matryoshka doll. Each layer decrypts the next with ChaCha20, decompresses it with LZMA into a Python code object, and runs it. Do anything it doesn't like and it just prints nothing, which is the challenge's name: "Nothing Stay."

The anti-debug trick

Every layer's decryption key is:

key = sha256(d1 + bytes([d2 ^ eps]))[:32]

where eps is an anti-debug accumulator. In a clean run it's 0. The moment any tracer, timing anomaly or tampering is detected, eps becomes non-zero, which produces a wrong key, which fails the layer's SHA-256 integrity check, which makes the program silently do nothing. It never tells you it caught you.

And it integrity-checks the obvious hooking targets, exec, marshal, time, so you can't wrap those to peek inside.

The gap

Here's the thing it doesn't protect: the crypto library. The anti-debug never inspects ChaCha20.new, and wrapping it is cheap enough not to trip the timing checks. That's the whole way in.

So:

  1. Wrap ChaCha20.new and steal the encrypted chunk table the worker reads from: a list of (d1, d2, nonce, ciphertext, expected_sha256) tuples.
  2. Brute eps offline. It's a single byte. For each chunk, find the eps where the decrypted-then-hashed result matches the expected SHA-256. It comes out 0 for every chunk, confirming the clean value.
  3. Decrypt and decompress each chunk into its Python code object. One of them is the big main payload, which is just another packer of the same shape. Peel until you reach the innermost layer.

The innermost checker

The deepest layer is the actual flag check. It reads your input, then does:

ChaCha20(key, nonce).encrypt(your_input) == target

with a fixed key and nonce. ChaCha20 is a stream cipher, so encryption and decryption are the same operation, and the key and nonce are constants. That means the flag is simply:

flag = ChaCha20(key, nonce).decrypt(target)

No brute force, no solving. I wrap input() so that at the moment the checker asks for the flag, I grab the key, nonce and target out of its frame and decrypt the target directly:

LYKNCTF{Im_At_The_PayPhone_Tryin_To_Home_Allof_My_change_1_Spent_0n_u_
Where_have_ThE_T1m3S_G0n3_B4bY_Its_Wr0nG_wh3rE_aRe_Th3_Pl4nS_W3_M4d3_F0r_2}

All those layers of ChaCha20, LZMA and anti-debug, and the flag was reversible from a single stored ciphertext the whole time, once I stopped fighting the parts it was watching and leaned on the one library it wasn't.

📂 Full writeup with the table grabber, the eps brute and the capture script: ctf-writeups/reverse/…/inferior student

Thanks for reading! Questions or corrections? Ping me on GitHub.

Read more

all posts →