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
| from pwn import* from hashlib import* import string from Crypto.Util.number import* from gmpy2 import* import os import math strr= string.ascii_letters+string.digits
def times2(input_data,blocksize = 16): assert len(input_data) == blocksize output = bytearray(blocksize) carry = input_data[0] >> 7 for i in range(len(input_data) - 1): output[i] = ((input_data[i] << 1) | (input_data[i + 1] >> 7)) % 256 output[-1] = ((input_data[-1] << 1) ^ (carry * 0x87)) % 256 assert len(output) == blocksize return output
def times3(input_data): assert len(input_data) == 16 output = times2(input_data) output = xor_block(output, input_data) assert len(output) == 16 return output
def back_times2(output_data,blocksize = 16): assert len(output_data) == blocksize input_data = bytearray(blocksize) carry = output_data[-1] & 1 for i in range(len(output_data) - 1,0,-1): input_data[i] = (output_data[i] >> 1) | ((output_data[i-1] % 2) << 7) input_data[0] = (carry << 7) | (output_data[0] >> 1) if(carry): input_data[-1] = ((output_data[-1] ^ (carry * 0x87)) >> 1) | ((output_data[-2] % 2) << 7) assert len(input_data) == blocksize return input_data
def xor_block(input1, input2): assert len(input1) == len(input2) output = bytearray() for i in range(len(input1)): output.append(input1[i] ^ input2[i]) return output
def hex_to_bytes(input): return bytearray(long_to_bytes(int(input,16)))
def pow(): p.recvuntil("XXXX+") str1 = str(p.recvuntil(")")[:-1],encoding='utf-8') p . recvuntil("= ") sha = p.recvuntil("\n")[:-1] for i in strr: for j in strr: for k in strr: for m in strr: s = sha256((i+j+k+m+str1).encode()).hexdigest() if s == str(sha,encoding='utf-8'): p.recvuntil("XXXX:") p.sendline(i+j+k+m) return 1
def giveusername(): p.recvuntil("> ") p.sendline('014')
def get_encrypt(msg): nonce = bytearray(os.urandom(16)) p.recvuntil("Enter option > ") p.sendline ("1") p.recvuntil("Enter nonce > ") p.sendline (nonce.hex()) p.recvuntil("Enter message > ") fake_m = bytearray(b'\x00'*15+b'\x80'+b'\x00'*16) p.sendline(fake_m.hex()) p.recvuntil("ciphertext: ") ciphertext = p.recvuntil("\n")[:-1] p.recvuntil("tag: ") tag = p.recvuntil("\n")[:-1] p.recvuntil("Enter option > ") p.sendline ("2") p.recvuntil("Enter nonce > ") p.sendline (nonce.hex()) p.recvuntil("Enter ciphertext > ") m0 = bytearray(b'\x00'*15+b'\x80') m1 = bytearray(b'\x00'*16) c0 = hex_to_bytes(ciphertext[:32]) p.sendline (xor_block(c0,m0).hex()) p.recvuntil("tag > ") c1 = ciphertext[32:] p.sendline (c1) p.recvuntil("data > ") p.sendline ("") p.recvuntil("message: ") enc = xor_block(bytearray(hex_to_bytes(p.recvuntil("\n")[:-1])),m0) A = back_times2(enc) B = enc C = xor_block(B,c0) msg = bytearray(msg) p.recvuntil("option > ") p.sendline ("1") p.recvuntil("nonce > ") p.sendline (xor_block(B,m0).hex()) p.recvuntil("message > ") p.sendline (xor_block(msg,times2(C)).hex()+m1.hex()) p.recvuntil("ciphertext: ") enc = bytearray(hex_to_bytes(p.recvuntil("\n")[:-1])[:16]) p.recvline() return xor_block(enc,times2(C))
def my_pmac(header, blocksize = 16): assert len(header) m = int(max(1, math.ceil(len(header) / float(blocksize)))) offset = get_encrypt(bytearray([0] * blocksize)) offset = times3(offset) offset = times3(offset) checksum = bytearray(blocksize) for i in range(m - 1): offset = times2(offset) H_i = header[(i * blocksize):(i * blocksize) + blocksize] assert len(H_i) == blocksize xoffset = xor_block(H_i, offset) encrypted = get_encrypt(xoffset) checksum = xor_block(checksum, encrypted) offset = times2(offset) H_m = header[((m - 1) * blocksize):] assert len(H_m) <= blocksize if len(H_m) == blocksize: offset = times3(offset) checksum = xor_block(checksum, H_m) else: H_m.append(int('10000000', 2)) while len(H_m) < blocksize: H_m.append(0) assert len(H_m) == blocksize checksum = xor_block(checksum, H_m) offset = times3(offset) offset = times3(offset) final_xor = xor_block(offset, checksum) auth = get_encrypt(final_xor) return auth
def my_ocb_encrypt(plaintext, header, nonce, blocksize = 16): assert nonce m = int(max(1, math.ceil(len(plaintext) / float(blocksize)))) offset = get_encrypt(nonce) checksum = bytearray(blocksize) ciphertext = bytearray() for i in range(m - 1): offset = times2(offset) M_i = plaintext[(i * blocksize):(i * blocksize) + blocksize] assert len(M_i) == blocksize checksum = xor_block(checksum, M_i) xoffset = get_encrypt(xor_block(M_i, offset)) ciphertext += xor_block(offset, xoffset) assert len(ciphertext) % blocksize == 0 M_m = plaintext[((m - 1) * blocksize):] offset = times2(offset) bitlength = len(M_m) * 8 assert bitlength <= blocksize * 8 tmp = bytearray(blocksize) tmp[-1] = bitlength pad = get_encrypt(xor_block(tmp, offset)) tmp = bytearray() C_m = xor_block(M_m, pad[:len(M_m)]) ciphertext += C_m tmp = M_m + pad[len(M_m):] assert len(tmp) == blocksize checksum = xor_block(tmp, checksum) offset = times3(offset) tag = get_encrypt(xor_block(checksum, offset)) if len(header) > 0: tag = xor_block(tag, my_pmac(header)) return (tag, ciphertext)
def last_io(): p.recvuntil("option > ") p.sendline("3") p.recvuntil("nonce > ") p.sendline(lastnonce.hex()) p.recvuntil("ciphertext > ") p.sendline(lastcipher) p.recvuntil("tag > ") p.sendline(lasttag) flag = p.recvuntil("}") print(flag) p.close()
if __name__ == "__main__": p = remote("127.0.0.1",10007) pow() giveusername() lastnonce = bytearray(hex_to_bytes('55'*16)) lasttag,lastcipher = (my_ocb_encrypt(bytearray(b'please_give_me_the_flag'),bytearray(b'from Alice'),lastnonce)) lasttag = lasttag.hex() lastcipher = lastcipher.hex() last_io()
|