#!/usr/bin/env python3.9 # -*- coding: utf-8 -*- import string import random import socketserver import signal from os import urandom from hashlib import sha256 from flag import FLAG
classCard: def__init__(self): random.seed(urandom(32)) self.cards = [] for t in ('Hearts', 'Spades', 'Diamonds', 'Clubs'): for p in ('J', 'Q', 'K', 'A'): self.cards.append(f'{p}{t}')
defdeal(self): n = random.getrandbits(4) return self.cards[n]
classTask(socketserver.BaseRequestHandler): def_recv_all(self): BUFF_SIZE = 1024 data = b'' whileTrue: part = self.request.recv(BUFF_SIZE) data += part iflen(part) < BUFF_SIZE: break return data.strip()
self.send('Greetings! I will give you my secret, if you can guess my card 200 times in a row. ' 'One coin, one chance.')
signal.alarm(3600)
while coin > 0: coin -= 1 c = card.deal() r = self.recv(prompt='Your guess > ').decode('l1') if r == c: count += 1 self.send(f'Correct! Your progress: {count}/200.') if count >= 200: self.send('You are the Card Shark! Flag is yours:') self.send(FLAG) break else: count = 0 self.send(f'Sorry! My card is {c}.')
if coin == 0: self.send('You have no money! See you another day.')
from pwn import * from Crypto.Util.number import * from hashlib import sha256 import string from randcrack import RandCrack from tqdm import tqdm from MT19937 import MT19937, MT19937_symbolic
alphabet = string.ascii_letters + string.digits
defproof(hash, part): for a in alphabet: for b in alphabet: for c in alphabet: for d in alphabet: s = (a+b+c+d).encode() + part if sha256(s).hexdigest() == hash.decode(): print(f'Found XXXX: {a+b+c+d}') return a+b+c+d
ID = 'dcb894124cb27c186d8a996bdbb43602' s = remote(ID + '.2022.capturetheflag.fun', 1337, ssl=True) s.recvuntil(b'XXX+') part = s.recvuntil(b')')[:-1] s.recvuntil(b'== ') hash = s.recvline()[:-1]
s.send(proof(hash, part).encode())
cards = [] for t in ('Hearts', 'Spades', 'Diamonds', 'Clubs'): for p in ('J', 'Q', 'K', 'A'): cards.append(f'{p}{t}')
known = [] for i in tqdm(range(4992)): s.send('J'.encode()) s.recvuntil(b'is ') key = s.recvline()[:-2].decode() pos = cards.index(key) known.append(pos)
''' #预测 rc = RandCrack() for i in known: rc.submit(i)
cnt = 0 for i in tqdm(range(205)): pos = rc.predict_getrandbits(4) s.send(cards[pos].encode()) res = s.recvline().decode() print(res) if 'Correct' in res: cnt += 1 if cnt >= 200: s.interactive() ''' #预测 rc = MT19937(state_from_data=(known, 4))
#好像一开始是初始态,得先过4992个 for i in known: assert i == rc() >> (32-4) print("[*] Cloning successful!")
cnt = 0 for i inrange(205): pos = rc() >> (32-4) # s.sendlineafter(b'Your guess > ', cards[pos].encode()) s.send(cards[pos].encode()) res = s.recvline().decode() print(res) if'Correct'in res: cnt += 1 if cnt >= 200: s.interactive()