Twist and Shout
Recovering the internal state of Python's Mersenne Twister PRNG.
Problem
Solution
from secret import flag
from Crypto.Util.number import *
import os
import random
state_len = 624*4
right_pad = random.randint(0,state_len-len(flag))
left_pad = state_len-len(flag)-right_pad
state_bytes = os.urandom(left_pad)+flag+os.urandom(right_pad)
state = tuple( int.from_bytes(state_bytes[i:i+4],'big') for i in range(0,state_len,4) )
random.setstate((3,state+(624,),None))
random.randint(0,0)
outputs = [random.getrandbits(32) for i in range(624)]
print(*outputs,sep='\n')Pseudo-RNGs
Mersenne Twister
Internal State
Recovering the Internal State
Recovering the Previous State
Solving the Challenge

Last updated