MT19937: 624 outputs and the future
The internal state of MT19937 is 624 32-bit words. The generator takes these words one by one, passes them through the temper() function, and outputs the result. When the 624 words run out, the "twist" operation updates the entire array and the cycle starts again.
There is a fatal vulnerability here: temper() is invertible. So an attacker who has seen 624 consecutive outputs reverses each using untemper() and collects a complete copy of the internal array. After that, they calculate the entire future (and past) of the generator. No brute-force is needed — this is an exact calculation.
This is exactly why generating a key, token or password with the random module is strictly forbidden.
Server code:
import random
random.seed(<maxfiy urugʻ>)
# 1) jurnalga (log) 624 ta chiqish yozildi
chiqishlar = [random.getrandbits(32) for _ in range(624)]
# 2) shundan keyingi chiqishlar kalit sifatida ishlatildi
soz = [random.getrandbits(32) for _ in range(10)]
kalit = b''.join(w.to_bytes(4, 'big') for w in soz)
ct = bytes(p ^ k for p, k in zip(bayroq, kalit)) # bayroq 39 bayt
The attached chiqishlar.txt file contains those 624 outputs (one decimal number per line, order preserved).
Encrypted flag (hex):
cd0d3b3045adffc2a55cdfc70cda1dbf7741b914942b478db23a09ab1204ab72
eabfa4988927f8
Solution path
- Take the
untemper()function from the previous challenge. - Untemper each of the 624 outputs — the result is the
mt[0..623]array. - Resume the generator from this state. The easiest way is Python's own module:
random.setstate((3, tuple(mt) + (624,), None)). Here624is the index: on the next request an automatic "twist" will be performed, just like in the real generator. - Now call
getrandbits(32)to get the key words, convert them to 4 big-endian bytes, and XOR them with the ciphertext.
If you wish, you can also write MT19937 manually: the twist operation is mt[i] = mt[(i+397)%624] ^ (y >> 1) ^ (0x9908B0DF if y & 1 else 0), where y = (mt[i] & 0x80000000) | (mt[(i+1)%624] & 0x7FFFFFFF).