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
| from pwn import * from Crypto.Util.number import long_to_bytes import time
N = 16
p = remote("0.0.0.0", 10002)
p.recvuntil(b"key: ") cipher = p.recvline() cipher = bytes.fromhex(cipher.decode()) p.recvline() p.recvline()
IV = cipher[:16] FB = cipher[16:32] Z = b"\x00"*16 I = [255]*16 known = b'{"key": "hitcon{'[:16] IV0 = xor(IV, known)
oracle_count = 0
def oracle(m): global oracle_count oracle_count += 1 p.sendline(m.hex().encode()) return not b"weirdo" in p.recvline()
def oracle_multi(ms): global oracle_count l = len(ms) oracle_count += l res = [0] * l for i in range(l): p.sendline(ms[i].hex().encode()) res[i] = not b"weirdo" in p.recvline() return res
offset_db = [-1 for i in range(16)]
def get_offset_db(): cur = 0; for i in range(256): if not -1 in offset_db: break ciphers = [] for offset in range(16): OFF = (offset ^ FB[-1]) % 16 check = I[:] check[offset] = 0 cipher = xor(IV0, check, i) + FB + xor(IV0, i) + FB ciphers.append(cipher) res = oracle_multi(ciphers) if res.count(True) == 1: offset = res.index(True) OFF = (offset ^ FB[-1]) % 16 offset_db[OFF] = i return offset_db
def oracle_block_top(BIV, BC): res = [0] * 16
for offset in range(16): real_offset = (offset ^ BC[-1]) % 16 IVL = xor(IV0, offset_db[offset]) top_7 = -1 ciphers = [] for diff in range(0, 256, 2): check = list(BIV[:]) check[real_offset] ^= diff cipher = xor(BIV, check) + BC + IVL + FB ciphers.append(cipher)
res2 = oracle_multi(ciphers) result = list(zip(res2, range(0, 256, 2))) for ora, diff in result: if ora: print(offset, diff) top_7 = (diff ^ BIV[real_offset] ^ offset_db[offset]) & 0xfe res[real_offset] = top_7 break else: raise ValueError("Padding not found")
return res
def oracle_block_lower(BIV, BC, Mtop): lowers = [0] * 16
baseIV = xor(BIV, Mtop) diff = [0] * 16 diff[-2] = 0xf0 IV2 = xor(baseIV, diff)
ciphers = [] for brute in range(0, 256, 2): IV1 = [brute] + [0] * 15 ciphers.append(bytes(IV1) + IV2 + BC)
if oracle_multi(ciphers).count(True) == 1: lowers[-2] = 0 else: lowers[-2] = 1
diff = [0] * 16 diff[-3] = 0xf8 IV2 = xor(baseIV, diff, lowers) ciphers = [] for brute in range(0, 256, 2): IV1 = [brute] + [0] * 15 ciphers.append(bytes(IV1) + IV2 + BC)
if oracle_multi(ciphers).count(True) == 1: lowers[-1] = 1 else: lowers[-1] = 0
for loc in range(13, -1, -1): diff = [0] * 16 diff[loc] = 0xf0 IV2 = xor(baseIV, diff, lowers) ciphers = [] for brute in range(0, 256, 2): IV1 = [brute] + [0] * 15 ciphers.append(bytes(IV1) + IV2 + BC)
if oracle_multi(ciphers).count(True) == 1: lowers[loc] = 0 else: lowers[loc] = 1
return [i+j for i, j in zip(Mtop, lowers)]
def oracle_block(BIV, BC): tops = oracle_block_top(BIV, BC) full = oracle_block_lower(BIV, BC, tops) return full
def attack(): offset_db = get_offset_db() print(offset_db, oracle_count) m = known for loc in range(32, len(cipher), 16): m+=bytes(oracle_block(cipher[loc-16:loc], cipher[loc:loc+16])) print(m)
print(m) print(oracle_count)
if __name__ == "__main__": attack()
|