1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| from exploit.mt19937.recoverMT19937 import * from Crypto.Random import get_random_bytes from hashlib import sha256 from pwn import * from hashlib import sha256 from tqdm import trange from Crypto.Cipher import AES from Crypto.Util.number import * from random import Random
context.log_level = "debug" xor_bytes = lambda b1, b2: bytes(a ^ b for a, b in zip(b1, b2)) pad_x923=lambda x,block_size=16:x+get_random_bytes((block_size-(len(x)%block_size)-1))+bytes([(block_size-len(x)%block_size)]) unpad_x923=lambda x,block_size=16:x[:-((x[-1]-1)%block_size)-1] from itertools import product from string import ascii_letters, digits table = ascii_letters + digits def proof_of_work(): sample1 = '[+] sha256(XXXX+IhJ7E8Ybs24CFirU) == 6a7993845557327af63c683c46449a39a06f3306136fe0a44833b09ba1558ecd' sample2 = '[+] Plz tell me XXXX: '
proof = sh.recvuntil(sample2.encode()).decode() t = re.findall(r'.*\(XXXX\+(\w+)\)\s==\s(.*)\n.*', proof) tail = t[0][0] _hash = t[0][1] for i in product(table, repeat=4): head = ''.join(i) t = hashlib.sha256((head + tail).encode()).hexdigest() if t == _hash: sh.sendline(head.encode()) break
def recover_chal(_key): keys = [] for _ in _key: i_key = bin(_)[2:].zfill(128) for j in range(len(i_key) // 32 - 1, -1, -1): keys.append(int(i_key[j * 32: (j + 1) * 32], 2))
parts = recover_state(keys[:624]) state = backtrace([0] * 4 + parts)[:624] prng = Random() prng.setstate((3, tuple(state + [0]), None)) k1, k2, k3, k4 = prng.getrandbits(32), prng.getrandbits(32), prng.getrandbits(32), prng.getrandbits(32) chal = bin(k4)[2:].zfill(32) + bin(k3)[2:].zfill(32) + bin(k2)[2:].zfill(32) + bin(k1)[2:].zfill(32) keys = [k4, k3, k2, k1] + keys parts = recover_state(keys[:624]) state = backtrace([0] * 4 + parts)[:624] prng = Random() prng.setstate((3, tuple(state + [0]), None)) k1, k2, k3, k4 = prng.getrandbits(32), prng.getrandbits(32), prng.getrandbits(32), prng.getrandbits(32) chal += bin(k4)[2:].zfill(32) + bin(k3)[2:].zfill(32) return chal def recover_iv(_key): keys = [] for _ in _key: i_key = bin(_)[2:].zfill(128) for j in range(len(i_key) // 32 - 1, -1, -1): keys.append(int(i_key[j * 32: (j + 1) * 32], 2))
parts = recover_state(keys[:624]) state = backtrace([0] * 4 + parts)[:624] prng = Random() prng.setstate((3, tuple(state + [0]), None)) for i in range(4): prng.getrandbits(32)
iv = [] for i in range(200): k1 = bin(prng.getrandbits(32))[2:].zfill(32) k2 = bin(prng.getrandbits(32))[2:].zfill(32) k3 = bin(prng.getrandbits(32))[2:].zfill(32) k4 = bin(prng.getrandbits(32))[2:].zfill(32) iv.append(k4 + k3 + k2 + k1) return iv
def iv2random( iv): return bytes_to_long(iv)
sh = remote("172.25.19.6",12345) proof_of_work() for xxx in range(50): pairs = [] r = [] ivlist = [] plainlist = [] c1list = [] plaintext = b'30'*15 for i in range(156): tb = os.urandom(15) sh.sendlineafter(b':> ', tb.hex()) plainlist.append(tb) iv_ret = bytes.fromhex(sh.recvline()[:-1].decode()) iv = iv_ret[:16] ci = iv_ret[16:] pairs.append([iv, ci]) c1list.append(ci) r.append(iv2random(iv)) chal = recover_chal(r) xx = recover_iv(r) chal = hex(int(chal, 2))[2:].zfill(16 * 3) chal1, chal2, chal3 = chal.encode()[:16], chal.encode()[16:32], chal.encode()[32:48] ivlis = [long_to_bytes(int(i,2)) for i in xx] ivlist = ivlis[:156] ivlist2 = ivlis[-44:]
chal4list = [b'\x00' * 15 + bytes([i]) for i in range(0x10, 0x100, 0x10)]
tanxinlist = [[0,0,0] for _ in range(44)] succlistchal1 = [] succlistchal2 = [] succlistchal3 = [] x = 0
for i in ivlist2: for j in c1list: if xor_bytes(xor_bytes(i,j),chal2)[-1] == 1: succlistchal1.append([i,j,x]) tanxinlist[x][0]+=1 x+=1
c2list = [] c3list = [] c4list = [] count = 0 for i in range(44): if tanxinlist[i][2]!=0: position = [index for index, sublist in enumerate(succlistchal3) if sublist[2] == i] ivnew = succlistchal3[position[0]] tb = xor_bytes(xor_bytes(ivnew[0],ivnew[1]),ivnew[3])[:15] sh.sendlineafter(b':> ', tb.hex()) iv_ret = bytes.fromhex(sh.recvline()[:-1].decode()) x = iv_ret[16:] c4list.append([x,ivnew[1],ivnew[4],ivnew[5]]) elif tanxinlist[i][1]!=0: position = [index for index, sublist in enumerate(succlistchal2) if sublist[2] == i] ivnew = succlistchal2[position[0]] tb = xor_bytes(xor_bytes(ivnew[0],ivnew[1]),chal3)[:15] sh.sendlineafter(b':> ', tb.hex()) iv_ret = bytes.fromhex(sh.recvline()[:-1].decode()) x = iv_ret[16:] c3list.append([x,ivnew[2],ivnew[1],ivnew[3]]) for c3l in c3list: x = c3l[1] for ivi in ivlist2[c3l[1]+1:]: x+=1 for chal4 in chal4list: if xor_bytes(xor_bytes(c3l[0],ivi),chal4)[-1] == 1 and tanxinlist[ivlist2.index(ivi)][2]==0: succlistchal3.append([ivi,c3l[0],x,chal4,c3l[2],c3l[3]]) tanxinlist[ivlist2.index(ivi)][2]+=1 print(tanxinlist) if(succlistchal3): count+=1 elif tanxinlist[i][0]!=0: position = [index for index, sublist in enumerate(succlistchal1) if sublist[2] == i] ivnew = succlistchal1[position[0]] tb = xor_bytes(xor_bytes(ivnew[0],ivnew[1]),chal2)[:15] sh.sendlineafter(b':> ', tb.hex()) iv_ret = bytes.fromhex(sh.recvline()[:-1].decode()) x = iv_ret[16:] c2list.append([x,ivnew[2],ivnew[1]]) for c2l in c2list: x = c2l[1] for ivi in ivlist2[c2l[1]+1:]: x+=1 if (xor_bytes(xor_bytes(c2l[0],ivi),chal3)[-1] == 1) and tanxinlist[ivlist2.index(ivi)][1]==0: succlistchal2.append([ivi,c2l[0],x,c2l[2]]) tanxinlist[ivlist2.index(ivi)][1]+=1 print(len(succlistchal2)) else: tb = os.urandom(15) sh.sendlineafter(b':> ', tb.hex()) iv_ret = bytes.fromhex(sh.recvline()[:-1].decode())
if(c4list): where = c1list.index(c4list[0][3]) ivfirst = xor_bytes(xor_bytes(pad_x923(plainlist[where]),ivlist[where]),chal1) print("@@@",chal.encode()) print(c4list[0]) end = c4list[0][3]+c4list[0][2]+c4list[0][1]+c4list[0][0] print(ivfirst.hex()) print(end.hex()) fate = ivfirst.hex()+end.hex() sh.sendlineafter(b'>> ', fate) else: fate = os.urandom(16+64).hex() sh.sendlineafter(b'>> ', fate) sh.recvline() sh.recvline() sh.recvline()
|