Securinets CTFReverse Engineering
revenant
challenge is a loader. It derives a key from the hostname, data.bin and its own ELF sections, then decrypts an 800 KB payload appended to itself using XTEA-CTR running in a Vulkan compute shader.
- Competition
Securinets CTF
- Category
- Reverse EngineeringHard · estimated
- Source status
- Source-documented · not platform-verified
- Document
- PDF edition5 min read
TL;DR
challenge is a loader. It derives a key from the hostname, data.bin and its own ELF sections, then decrypts an 800 KB payload appended to itself using XTEA-CTR running in a Vulkan compute shader. If the decrypted payload is an ELF, it gets executed from a memfd. The payload (stage 2) is hard-wired to print still dead.. Its real flag check is still in the binary, though: S-box, then reverse, then per-byte rotate, then Salsa20 XOR, then compare against a constant. Inverting that check gives the flag.
hostname "blackkad" + hash(data.bin) + build-id → SHA-256 → XTEA key/IVXTEA-CTR (GPU) over appended blob → stage2 ELFstage2 check: sbox → reverse → rol(i%7+1) → ⊕ Salsa20 == target → invert → flag01
Recon
challenge: ELF64, dynamically linked againstlibcandlibvulkan.so.1, built with clang 21 and LLD, and stripped.The section headers end at
0x5ec8, but the file is0xca898bytes long. That leaves ~800 KB of high-entropy data appended after the ELF.The last 8 bytes,
0x0c4990, give the length of that blob.The only libc imports are
getauxval,getrlimit/setrlimit,sigaltstack,memsetandmemcpy. All file I/O goes through rawsyscallinstructions.stringsfindsSecurinets{c3b7a291...59110}in a custom.note.securinetssection. That's a decoy: stage 2 rejects it.The binary also contains an embedded SPIR-V module with the names
ParamsBuf,key,iv0,iv1,n_blocks,PtBufandCtBuf.
When run, it prints nothing and exits.
02
Stage 1 – the loader
Every string is obfuscated as enc[i] ^ (0x1a + 0xb*i) ^ 0x37 (strs.py). Decoded, they are /proc/self/status, TracerPid:, /proc/self/exe, .stab, .note.gnu.build-id and data.bin.
Here's what main does:
Anti-debug: reads
TracerPidand remembers whether the process is being traced.Maps
/proc/self/exeand looks up two sections:.stab(8 bytes):86;911;>.note.gnu.build-id
Calls
uname()and comparesnodename[0:8]with.stab ^ 0x5a=blackkad. On a mismatch it doesn't exit. It quietly flips bit 7 of every hostname byte, so the key comes out wrong. That's the "dead on arrival" part.Reads
data.binand hashes the first 256 bytes with a small stub that it decodes at runtime (byte ^ (i + 0x71)) and runs from an RWX page:Text h = 0; for b in data[:256]: h = ror64(h,1) ^ b; h += 0xbf58476d1ce4e5b9There's also a
rdtsctiming check: if this step is slow,h ^= 0xbb…. Thenh ^= build_id_desc[4:12].Computes
digest = SHA-256(hostname[0:8] ‖ h ‖ data.bin[0xa0:0xb0]). The IV and K constants are the standard ones.Builds the GPU parameters:
key = digest[0:16],iv0 = digest[16:20],iv1 = digest[20:24],n_blocks = len/8.Dispatches the compute shader over the appended blob, checks the result for
\x7fELF, writes it to amemfd_createfile andexecves/proc/self/fd/N.
03
The compute shader
spvdis.py is a small SPIR-V disassembler. The shader turns out to be XTEA in CTR mode, with 32 rounds and delta 0x9e3779b9:
v0 = iv0 + idx; v1 = iv1XTEA-encrypt(v0, v1, key)pt[2*idx] = ct[2*idx] ^ v0pt[2*idx+1] = ct[2*idx+1] ^ v1Every input to the key is in the handout, and the hostname comes straight from .stab, so the whole decryption can run offline on the CPU (solve.py). No GPU or hostname spoofing is needed:
hostname b'blackkad' L 0xc4990 magic b'\x7fELF'04
Stage 2 – "still dead."
stage2 is a static, stripped glibc binary. main (0x404050) works like this:
if (dead) // byte at 0x4c2330, initialised to 1, never cleared { puts("still dead."); return 1; }fgets(buf, 0x100, stdin); strip '\n';if (check(buf, len)) puts("[+] correct!");else { printf("[specter] session token: Securinets{%s}\n", random_hex); puts("[-] wrong!"); }Nothing ever writes to dead, so the binary always dies. Wrong guesses also print a random fake flag. A constructor in .init_array (0x403bc0) runs another TracerPid check plus a second anti-debug check, fills the global tables, and sets check = 0x403040.
check (0x403040) requires len == 76, then:
b[i] = SBOX[in[i]], with the S-box copied from0x48dec0.Reverses the 76 bytes. This is done with AVX2
vperm2i128andvpshufb.b[i] = rol8(b[i], (i % 7) + 1).b ^= Salsa20(key = 0x48dfc0, nonce = 0)keystream. The key schedule rotates each byte left by 3 and then by 5, which cancels out.memcmp(b, target, 76), wheretarget = 0x48dfe0[0:64] ‖ 0xfeaecc3589a1a323 ‖ 0x02ab8200. Anrdtsctiming check is folded into the return value.
Every stage is a bijection, so the check can be inverted. To avoid having to reproduce the AVX2 Salsa20 exactly, I pulled the keystream straight from the real binary:
patch_dump.pyclearsdeadand redirects thememcmpcall into a 23-byte code cave thatwrite(2, buf, 76)s the final buffer.Running it on two different 76-byte inputs and XORing out
rol(reverse(sbox(input)))gave the same keystream both times. That confirms the S-box, reverse and rotate stages are correct.solve3.pythen works backwards:target ⊕ ks→ror→ reverse → inverse S-box.
Securinets{5db6ce3527dfeeafdf5da028ab9282f2bf2fe34400fa06da794cc50afaa43c0e}05
Verification
verify.sh patches only the dead byte and pipes candidates into the original check:
--- real flag:flag: [+] correct!--- decoy from stage 1 note:[specter] session token: Securinets{05611bc920441862ea070bef7e32c609ff5a02e80f377553a98a3547ab100eaa}flag: [-] wrong!Files
| File | Purpose |
|---|---|
strs.py | Decodes stage-1 strings and the runtime hash stub |
spvdis.py | Minimal SPIR-V disassembler, dumps shader.spv |
solve.py | Derives the SHA-256 key and decrypts the blob with XTEA-CTR, writing stage2 |
xref.py | Finds string cross-references in stage 2 |
solve2.py | Extracts the stage-2 tables and models the check |
patch_dump.py, run_dump.sh | Build and run stage2_dump to capture the real keystream |
solve3.py | Inverts the check, writing flag_candidate.txt |
verify.sh | Runs the original check with only the dead byte cleared |
Flag
Flag
Securinets{5db6ce3527dfeeafdf5da028ab9282f2bf2fe34400fa06da794cc50afaa43c0e}
Source-documented · not platform-verified
Takeaways
Flags sitting in plain sight (
.note.securinets, the[specter]token) are decoys."Dead" binaries often fail on purpose, through a hard-coded kill flag or an environment-bound key that corrupts things silently. Look for what the code would do if it lived.
If every key input ships with the challenge, replicate the crypto offline instead of satisfying the environment (here: the hostname, a Vulkan GPU, no debugger, and timing checks).
When a cipher is buried in hand-vectorised AVX2, patching the binary to dump its internal buffer and solving for the keystream is quicker than emulating it exactly.
