Dagestan JPEG-appended Python zipapp, self-decrypting VM

rev
flag L3m0n{41b89874dfd0e7d00b3ec0b0d3aafdbc}

One handout, dagestan.jpeg. file calls it a JPEG, and its comment marker proudly holds L3m0n{00000000000000000000000000000000} - 32 zeros. A placeholder, not an answer: the category is Rev, which means somewhere in this file is a program, not just a picture.

The handout dagestan.jpeg: an ordinary-looking meme that is also a Python zipapp

Hint

JPEG viewers stop at the end-of-image marker. Most other tools keep reading.

The polyglot

Past the last image segment sits a second file entirely: a Python zipapp. A zip local header (PK\x03\x04) appears at a fixed offset, and the end-of-central-directory record sits at the very end of the file - so python3 dagestan.jpeg just runs. Python doesn't care that the file also happens to be a valid JPEG; it finds the zip structure appended at the end and executes __main__.py from it.

Pitfall

binwalk-style carving from "everything after EOI" fails here with Invalid argument, because the zip's central directory stores offsets absolute to the whole file, JPEG header included - not relative to wherever you started slicing. Carve starting from the real PK\x03\x04 offset, not from the end of the image data.

Self-decrypting on its own filename

The zipapp's loader reads the first 65536 bytes of the file it was launched from (the JPEG header itself), hashes them with SHA-256, and uses that digest as a keystream to XOR-decrypt a 38,993-byte PAYLOAD embedded in the source, then exec()s the result. This is exactly why the entry point only works when the file keeps its original name and content: sys.argv[0] resolves back to the same JPEG bytes that seed the decryption key.

The VM is a self-referential crackme

The decrypted payload is a small stack VM over 128-bit values. Its program, 5103 words from a 33-word vocabulary, maps each word to an opcode index that's further XORed against another keystream derived from the same JPEG prefix, with a rot/advance state machine permuting which opcode each token decodes to as execution proceeds.

Insight

The key opcode is DECRYPT: it pops a (unit_len, tag) pair, checks tag == sha256(sha256(flag[:16*(i+1)]))[:4], and only on success decrypts the next block of the token stream and re-seeds the opcode environment from that key. The program's own later code is encrypted with a key derived from the correct flag prefix - so the flag is literally the key that unlocks the code that checks the flag, one 16-byte block gating one code block at a time.

Solving each block with z3

Stage 0 is straight-line code: build a 128-bit constant, XOR in flag[0:16], run a long chain of x ^= x << k / x ^= x >> k / multiply / rotate mixing, then one final EQ against a hardcoded constant. No data-dependent branches before that compare - the whole stage is one invertible 128-bit function of the first 16 flag bytes. Modeling it directly in as a 128-bit bit-vector, with the flag bytes constrained to printable ASCII, finds a unique solution:

output
L3m0n{41b89874df

Feeding that prefix back in makes the stage-0 EQ pass, satisfies the DECRYPT tag check, and unlocks the next code block - which does the exact same thing for flag[16:32], giving d0e7d00b3ec0b0d3, then a third block gives the tail.

Pitfall

The last block has to be solved without the printable-ASCII constraint. It decodes to aafdbc} followed by nine zero-padding bytes, and forcing all 16 bytes to printable ASCII makes that particular z3 query come back UNSAT. Drop the constraint for the final block once you know it's the tail.

output
L3m0n{41b89874dfd0e7d00b3ec0b0d3aafdbc}

Note

Each EQ only pins down 128 bits of state per 16-byte block - this isn't a hash preimage search at any stage, since every mixing step is invertible. z3 just solves the equation directly rather than searching; the only real work is correctly modeling the mix operations as bit-vector constraints.