MT19937: reverse tempering
Mersenne Twister (MT19937) is the generator behind the random module in Python, PHP, Ruby and many other languages. Its internal state consists of 624 32-bit words. Before each output, the state word is passed through a mixing function called tempering — this improves the statistical quality of the numbers.
Important point: tempering is not encryption. It has no secret key, only shift and XOR operations. And each operation is reversible. Thus, it is possible to exactly recover the internal state word from the output number. This is the first and most important step in cracking MT19937.
def temper(x): # x — MT19937 ning ichki holat soʻzi
y = x
y ^= y >> 11
y ^= (y << 7) & 0x9D2C5680
y &= 0xFFFFFFFF
y ^= (y << 15) & 0xEFC60000
y &= 0xFFFFFFFF
y ^= y >> 18
return y & 0xFFFFFFFF # aynan shu son chiqishga beriladi
To derive the reverse operations, it is enough to understand two patterns:
y = x ^ (x >> s)— here the highestsbits ofyare the same as those ofx. Knowing them, we find the nextsbits and thus go down.y = x ^ ((x << s) & mask)— exactly the same, but we go up starting from the lowestsbits.- Practical trick: if you repeat
x = y; x = y ^ (x >> s)several times, the result stabilizes and becomes exactly the originalx.
The generator output three consecutive numbers:
y1 = 112016232
y2 = 3673192664
y3 = 3008816174
Find their corresponding internal state words (that is, find such x that temper(x) = y) and assemble the flag: BAYROQ{PRNG_untemper_<x1>_<x2>_<x3>} — the numbers are in decimal format.
Enumerating all 2^32 variants (brute-force) will not work here — write the inverse function.