SheruBhai ECDSA nonces leak through a flawed PRNG

crypto
flag L3m0n{T4dpan_7adpan_Tad4p_k3_Mar4_kh4de_h0_k3_Mar4!!!!!!!}

"Aise thodi khulega Sheru Bhai ka locker dost... Locker kholne ke chakkar mein tadap tadap ke maroge!"

ECDSA over P-384, nonces drawn from Python's random (). The flag is split across two private keys, d1 and d2. This is the "longest 20 minutes" challenge of the event - it took a lot more than 20 minutes, but every stage is a distinct, satisfying trick.

What you get

yolo.jpeg, a picture of a lion in a white suit holding a lemon. unzip -l yolo.jpeg finds an archive appended to the end of the JPEG, which unpacks to output.txt:

yolo.jpeg: a lion in a white suit holding a lemon, with a zip archive appended after the image data

output
line 1: 14 signatures (r, s) made with d1
line 2: 550 floats from random.random()
line 3: 5 signatures made with d2

Insight

The generator leaks the message hash z1 inside the nonce itself:

python
1def sign_prequel(n=14):
2    for _ in range(n):
3        hi = random.getrandbits(192)
4        lo = random.getrandbits(160)
5        k  = (hi << 192) | (z1 << 160) | lo       # z1 embedded in the nonce!
6        sigs.append(sign(z1, d1, k))
7    floats = [random.random() for _ in range(550)]
8
9def sign_original():
10    for _ in range(urandom_18bit):                 # hidden skip N < 2^18
11        random.getrandbits(32)
12    return [sign(z2, d2, random.getrandbits(384)) for _ in range(5)]

That's a real bug, but a minor one compared to the actual flaw: nonces coming from a PRNG whose internal state is mostly recoverable from other public outputs of the same stream.

Pinning down the Mersenne Twister state

Every bit of a random.random() float is a GF(2)-linear function of the 624-word state. Building that system from 550 floats and running Gaussian elimination over GF(2):

  • 7513 variables, rank 6752, leaving a 761-dimensional kernel - a real ambiguity, not a solver limitation. Any kernel vector added to the state still reproduces all 550 floats exactly.
  • Words 154..623 come out fully determined.
  • Word 0 of the stream has 31 free bits; words 1-7 are fully known; words 8-153 each carry the same 5-dimensional XOR ambiguity (31 + 146x5 = 761, which accounts for the whole kernel).

So the floats alone can't produce the later nonces either - 730 of those 761 free dimensions change the forward output stream. The early signatures are the only lever left.

1/2Stage 1: the one weak nonce

Nonce 0 is built from MT words 0-10. Word 0 has 31 unknown bits, words 1-5 are known, words 6-7 are known, and words 8-10 each have 5 unknown bits - 46 unknown MT bits total feeding k0. The signature equation is

output
s0 * k0 ≡ z1 + r0 * d1   (mod Q)

d1 is 48 bytes of flag starting with the known L3m0n{, and z1 is 4 printable bytes. Altogether roughly 350 bits of small, structured unknowns against one 384-bit modular equation - about 34 bits of slack.

The 5-bit XOR ambiguities in words 8, 9, 10 aren't integer-linear, so brute-force them directly: 32^3 = 2^15 cases. With those three words fixed, everything else is integer-linear - word 0 (32 bits), 4 printable bytes of z1, 42 printable bytes of d1 - and goes into a dimension-50 lattice via a centred Kannan embedding (shift every variable to the midpoint of its range, scale to a common weight, put the modular equation in a heavily weighted column). Plain is enough per case, run as 20 parallel workers at roughly 30 cases/sec/core.

A candidate only counts if it satisfies all 14 early signatures. Hit at (m8, m9, m10) = (16, 3, 6):

output
z1 = b'SHER'
d1 = b'L3m0n{T4dpan_7adpan_Tad4p_k3_Mar4_kh4de_h0_k3_Ma'

2/2Stage 2: rebuild the state, recover d2

With d1 and z1 known, every k_i = (z1 + r_i*d1)/s_i is computable, giving exact MT output words 0..153. Untempering those plus the float-solved words 154..623 gives the complete 624-word state - verified by re-deriving all 550 floats and all 14 early signatures.

Running the generator forward, try every skip N < 2^18. For each N, build 5 candidate nonces and solve for d2 from signatures 0 and 1:

output
d2 = (s0*k0 - s1*k1) / (r0 - r1)
z2 = s0*k0 - r0*d2

Accept N only if signatures 2-4 also satisfy the equation. Hit at N = 256791:

output
d2   = b'r4!!!!!!!}' + b'&'*38     # PKCS#7-style padding to 96 bytes
msg2 = b'Sheru Bhai se bachke rehna warna tadap ke mara!!'

unpad(d1 || d2, 96) is the flag.

  • Additive-delta HNP: modelled the kernel shifts as integer deltas added to each word. They're XOR shifts, not addition, so carries break the model silently.
  • One big 32-bit-per-word lattice over all 14 signatures: eliminating d1 and treating every uncertain word as a free 32-bit integer throws away the 5-bit structure. The Gaussian-heuristic margin disappears and LLL/BKZ find nothing.
  • Exact 13-binary-variable model across 2-4 signatures: more principled, but the resulting lattice's Gaussian-heuristic ratio sits around 0.5-0.6 - too dense for LLL or BKZ-30 to reduce.
  • over the full 384-bit multiplications: ran out of memory or never finished.
  • Assuming d1 is L3m0n{ + hex digits: worked on simulated test data, failed on the real data for every candidate length. The actual model is "printable bytes," not "hex digits" - a plausible but wrong guess cost real debugging time until both stage-1 variants were re-checked against simulated instances with known flags first.

Attacking all 14 signatures at once "because more data sounds better" is exactly backwards here. The least-unknown nonce, combined with a known plaintext format for d1 (starts with L3m0n{, printable bytes), makes a far smaller, far denser-friendly lattice than one assembled from every signature at once.

Note

Key takeaways: brute-force the bits that aren't integer-linear (the XOR ambiguity) and let the lattice handle everything that is. And validate any attack strategy on simulated data with a known answer before trusting it on the real signatures - that step is what caught the hex-vs-printable mistake.

Reproduce
bash
1cd work
2# fpylll lives in user site-packages, so run without -I
3for p in $(seq 0 19); do python3 lat_d1b.py $p 20 > bf/p$p.txt & done; wait   # writes sol_d1.pkl
4python3 phase2b.py                                                           # prints the flag

mt.pkl caches the GF(2) system for lat_d1b.py. phase2b.py rebuilds it itself if missing, which takes about two minutes.