Simply Permutation Network
Can you remind me what the S in SPN stands for?
solve this crypto ctf
Classic setup: a "Substitution–Permutation Network" where someone forgot the S. Let me confirm the structure and exploit it.
The 5 leading zero bytes are just big-endian padding — the message is 59 bytes, so the flag is 31 bytes and flag[4:27] = "y0u_4ls0_n33d_sub5t17ut". Since m2 = m2_comp ^ flag4, the 4 unknown leading bytes of the flag also unlock the 4 unknown trailing bytes exactly:
Neither guess fits. The tail is byte-wise V ^ flag4 (V = low 32 bits of m2_comp), and both sides are printable ASCII — that's a strong joint constraint. Let me enumerate all consistent pairs:
The algebra is exact, so the free bits are provably only the flag's first and last 4 bytes (64 unknown bits total — information-theoretically unrecoverable from ciphertext alone). Let me verify the attack end-to-end against the reference cipher, then enumerate the printable-consistent completions:
Fixing the two bugs in my validation harness (flag-length construction and the rindex guard):
The end-to-end validation now passes: F=31, msglen=59 (matching the 5 pad bytes we observed) recovers the middle exactly, confirming the unknown bits are precisely flag[0:4] and flag[27:31]. Now let me pin those down — note the tail must complete the word: ...sub5t17ut + 10n} = "substitution", which forces the prefix via X = T ^ V:
tail "10n}" => prefix "BIU{" — Bar-Ilan University's CTF format. Let me finalize the solve and run an independent sanity check on whether the effective-key map could refute a wrong guess:
The flag computation is instant (it printed before my buggy print crashed); only the statistics loop timed out. Let me run a faster version plus a web check on the flag format in parallel:
The web search found no public writeup (university/private CTF), but the math is closed. Here's the complete solution:
What the S stands for
Substitution — and this cipher doesn't have any. The round function is only xor round key → bit permutation, both GF(2)-affine operations. Expanding the 16 rounds:
C = k₁₆ ⊕ L(k₁₅ ⊕ L(⋯ L(k₀ ⊕ m)⋯)) = K* ⊕ L¹⁶(m)
K* = k₁₆ ⊕ L(k₁₅) ⊕ L²(k₁₄) ⊕ ⋯ ⊕ L¹⁶(k₀)
The whole 16-round "SPN" with a 256-bit key collapses to a one-time pad with an effective key K* (I verified this identity against the challenge's own encrypt_block in solve.py's self-test). The LCG round keys and 2²⁵⁶ keyspace are irrelevant.
Attack (final_solve.py)
- Known plaintext: block 1 starts with
"This is the encrypted flag: "→K* = C₁ ⊕ L¹⁶(m₁)known except the 32 bits contributed by the flag's first 4 bytes. - Decrypt block 2 (the flag block):
m₂ = L⁻¹⁶(C₂ ⊕ K*). SinceL⁻¹⁶∘L¹⁶ = id, the unknown bits land exactly onm₂'s low 32 bits — everything else is exact:flag[4:27] = y0u_4ls0_n33d_sub5t17ut(the 5 leading\x00are big-endian padding → message is 59 bytes, flag is 31 bytes). - Tie the two unknown groups together:
m₂ = m₂_comp ⊕ flag[0:4], sotail = head ⊕ VwithV = 73793b06. I verified statistically (20k random keys, all 32 free bits balanced ~0.5, all pairs distinct) that the ciphertext imposes no further constraint — the 8 unknown chars are information-theoretically lost and must be completed semantically. - Completion:
...sub5t17ut+10n}= "substitution", which forceshead = tail ⊕ V =BIU{(Bar-Ilan University CTF format). Sanity check: 4 + 23 + 4 = 31 chars → 59-byte message → exactly the 5 padding bytes observed. ✓
Flag
BIU{y0u_4ls0_n33d_sub5t17ut10n}
Artifacts: solve.py (affine decomposition + validation), enumerate_tail.py (consistency enumeration), final_solve.py (final solve).
write a write up.md of only how to solve the challenge pleasae no special charactres well explained like explaining the tought to someone that doesnt know the knowledge
Done. The writeup is at writeup.md — pure ASCII, no special characters (verified: 0 non-ASCII bytes).
It walks a reader with zero background through the whole solve:
- What an SPN is and why the missing S-box matters — explained via "flip one input bit, watch what happens to the output" intuition for XOR vs. an S-box.
- The collapse: since every round is only XOR + bit moving, the 16 rounds unroll into
C = Kstar XOR L^16(m)— a one-time pad in disguise, withKstarbeing the round keys piled up through the permutation. - Known plaintext: the 28-byte
"This is the encrypted flag: "prefix givesKstarexcept 32 bits, whose positions are tracked precisely. - Decrypting block 2: the inverse permutation sends those 32 unknown bits back to exactly the last 4 bytes, so the middle
y0u_4ls0_n33d_sub5t17utdecrypts perfectly, and the 5 zero bytes reveal the flag length (31 bytes). - Closing the gaps: why the ciphertext alone can't give the first/last 4 bytes (any guess has a consistent key — backed by the 20k-key statistical check), then the two-step completion: finish
sub5t17utwith10n}, and XOR the tail with the maskVto force the prefixBIU{.
Ends with the final flag BIU{y0u_4ls0_n33d_sub5t17ut10n} and a one-line lesson.
