import random print('----Welcome to my division calc----') print(''' menu: [1] Division calc [2] Get flag ''') whileTrue: choose = input(': >>> ') if choose == '1': try: denominator = int(input('input the denominator: >>> ')) except: print('INPUT NUMBERS') continue nominator = random.getrandbits(32) if denominator == '0': print('NO YOU DONT') continue else: print(f'{nominator}//{denominator} = {nominator//denominator}') elif choose == '2': try: ans = input('input the answer: >>> ') rand1 = random.getrandbits(11000) rand2 = random.getrandbits(10000) correct_ans = rand1 // rand2 if correct_ans == int(ans): print('WOW') withopen('flag', 'r') as f: print(f'Here is your flag: {f.read()}') else: print(f'NOPE, the correct answer is {correct_ans}') except: print('INPUT NUMBERS') else: print('Invalid choice')
PRNG问题,使用了getrandbits,涉及到MT19937
破解MT19937,需要624个32位数,也就是19968bits的信息
选择1的时候,输入分母,就会给出分数,那么,我们让分母等于1,就会得到分子,
使用Pwntools工具如此重复624次,就可以得到624个32位的数字
使用Randcrack库进行破解,恢复MT19937的状态
然后再获得rand1,rand2就行了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import random from pwn import * from randcrack import RandCrack from tqdm import * conn = remote('47.93.96.189', 24598, timeout=10) rc = RandCrack() for i in trange(624): conn.sendlineafter(b': >>> ',b'1') conn.sendlineafter(b'input the denominator: >>> ',b'1') s = str(conn.recvline().strip())[2:] number = s.split("//")[0] rc.submit(int(number)) rand1 =rc.predict_getrandbits(11000) rand2 =rc.predict_getrandbits(10000) ans = rand1//rand2 conn.sendlineafter(b': >>> ',b'2') conn.sendlineafter(b'input the answer: >>> ',str(ans).encode()) print(conn.recvline().strip()) print(conn.recvline().strip())
defcomplex_pow(c, exp, n): result = Complex(1, 0) while exp > 0: if exp & 1: result = result * c result.re = result.re % n result.im = result.im % n c = c * c c.re = c.re % n c.im = c.im % n exp >>= 1 return result
bits = 128 p = getPrime(1024) q = getPrime(1024) n = p * q m = Complex(getRandomRange(1, n), getRandomRange(1, n)) e = 3 c = complex_pow(m, e, n) print(f"n = {n}") print(f"mh = {(m >> bits << bits).tolist()}") print(f"C = {c.tolist()}") print(f"enc = {ChaCha20.new(key=hashlib.sha256(str(m.re + m.im).encode()).digest(), nonce=b'Pr3d1ctmyxjj').encrypt(flag)}")