← all posts

· 4 min read · by Eav Puthcambo

One bad signature to factor them all

A custom binary protocol with homemade crypto falls to a leaked key and a classic Bellcore RSA-CRT fault attack. One faulty signature is all it takes to factor n.

"The gates open for no army — but they swing wide every evening for gifts. Bring something shiny, get the keeper's seal on it, waltz right in." TrojanHorse, Cyber Arena CTF 2026 (crypto / pwn)

This was the hardest challenge in its set: a stripped, static-PIE server speaking a bespoke binary protocol, with a homemade cipher, a homemade MAC, and per-connection RSA on top. The flag is never sent in the clear. It's wrapped in two layers of encryption, one of them keyed by the RSA private exponent.

To get it out I needed three bugs in a row. Here's the chain.

The protocol

Every message looks like this:

[type:1][len:2 LE][body]

Client→server bodies are encrypted in CTR mode. Even the type byte goes through a secret S-box, so before I could send a single valid command, I had to reverse the crypto:

Primitive What it is
ARX permutation 20 rounds of add-rotate-xor, round constants from a table
CTR keystream the permutation over [counter ^ K0, K1, K2, K3]
Sponge MAC seeded with SHA-512's IV words, rate 32: the keeper's "seal"
Command S-box Fisher–Yates shuffle driven by the keystream

There's no flag.txt string in the binary either. The filename is XOR-obfuscated with 0xa1, which is why grepping for it finds nothing.

Bug 1: the key is in the greeting 🎁

At startup the server reads 16 bytes from /dev/urandom for half of the cipher key. The other half is hardcoded digits of π.

And then it sends those 16 random bytes to the client in the very first packet.

So the whole cipher key, and therefore the command S-box, can be rebuilt client-side. That gave me a working protocol client. One subtle thing I had to get right: a global counter is incremented on every packet and feeds the keystream, so the client has to track it exactly or everything desyncs after the first command.

How the flag is locked

Reading the startup code showed the flag's journey:

  1. Read flag.txt.
  2. Encrypt it with a MAC derived from d, the RSA private exponent.
  3. When you send the P command, add a second layer keyed by a MAC of your own request body.
P_response = Enc(MAC(your_body), Enc(MAC(d), flag))

I control the outer key, so peeling that layer is free. The inner layer needs d, which means factoring n.

Bug 2: a signing oracle with CRT

The S command is a raw RSA signing oracle: sig = m^d mod n, computed with the Chinese Remainder Theorem for speed. That means the server computes the signature separately mod p and mod q, then combines them.

Here's the catch: an attacker-controlled field in the S request sits right next to the CRT parameters in memory. Send it too long, and it corrupts one half of the CRT computation.

Bug 3: Bellcore's 1997 attack still works

This is the classic Bellcore fault attack. If the mod-q half of a CRT signature is wrong but the mod-p half is right, then the faulty signature s' still satisfies s'^e ≡ m (mod p), but not mod q. So:

gcd(s'^e − m, n) = p

One faulty signature, one gcd, and n is factored:

_, bk = p.cmd(s, 0x4b); N = int.from_bytes(bk[:128], "big")     # K -> modulus
m, K = 2, 56
_, out = p.cmd(s, 0x53, struct.pack("<H", 16) + m.to_bytes(16, "big")
                        + struct.pack("<H", K) + b"\xff" * K)    # overflow -> fault
sig = int.from_bytes(out, "big")
pf  = gcd((pow(sig, e, N) - m) % N, N)                            # 💥 p
qf  = N // pf

From there it's bookkeeping: compute d = e⁻¹ mod λ(n), derive the inner MAC, and peel both layers:

factored: True
FLAG: MPTC{n3v3r_tru5t_4_p4t13nt_gr33k_b34r1ng_g1ft5}

Never trust a patient Greek bearing gifts. 🐴

📂 Full writeup and the pure-Python protocol client: ctf-writeups/crypto/…/trojanhorse

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

Read more

all posts →