#!/usr/bin/python # -*- coding: utf-8 -*- from Crypto.Cipher import AES from Crypto.Util.strxor import strxor from Crypto.Random import get_random_bytes from FLAG import flag classMAC: def__init__(self): self.key = get_random_bytes(16) self.iv = get_random_bytes(16) defpad(self, msg): pad_length = 16 - len(msg) % 16 return msg + chr(pad_length) * pad_length defunpad(self, msg): return msg[:-ord(msg[-1])] defcode(self, msg): res = chr(0)*16 for i in range(len(msg)/16): res = strxor(msg[i*16:(i+1)*16], res) aes = AES.new(self.key, AES.MODE_CBC, self.iv) return aes.encrypt(res).encode('hex') defidentity(self, msg, code): if self.code(msg) == code: msg = self.unpad(msg) if msg == 'please send me your flag': print'remote: ok, here is your flag:%s' % flag else: print'remote: I got it' else: print'remote: hacker!' if __name__ == '__main__': mac = MAC() message = 'see you at three o\'clock tomorrow' print'you seem to have intercepted something:{\%s:\%s}' %(mac.pad(message).encode('hex'), mac.code(mac.pad(message))) print'so send your message:' msg = raw_input() print'and your code:' code = raw_input() mac.identity(msg.decode('hex'), code) exit()
先给出了一段明文的 hex 和其加密之后的 hex,然后要求给出一段明文和其对应的密文,然后就就判断其为 please send me your flag 且密文正确的话就给出 flag。 这里很有意思的是这里
1 2 3 4 5 6
defcode(self, msg): res = chr(0)*16 for i in range(len(msg)/16): res = strxor(msg[i*16:(i+1)*16], res) aes = AES.new(self.key, AES.MODE_CBC, self.iv) return aes.encrypt(res).encode('hex')
#!/usr/bin/python # -*- coding: utf-8 -*- from Crypto.Cipher import AES from Crypto.Util.strxor import strxor from Crypto.Random import get_random_bytes flag = "fuck" classMAC: def__init__(self): self.key = get_random_bytes(16) self.iv = get_random_bytes(16) defpad(self, msg): pad_length = 16 - len(msg) % 16 return msg + chr(pad_length) * pad_length defunpad(self, msg): return msg[:-ord(msg[-1])] defcode(self, msg): res = chr(0)*16 # 最终目的 res 相等 24054d4c1a0f19444e0f4016080f1805 for i in range(len(msg)/16): res = strxor(msg[i*16:(i+1)*16], res) aes = AES.new(self.key, AES.MODE_CBC, self.iv) print(res.encode('hex')) return aes.encrypt(res).encode('hex') defidentity(self, msg, code): if self.code(msg) == code: msg = self.unpad(msg) if msg == 'please send me your flag': print'remote: ok, here is your flag:%s' % flag else: print'remote: I got it' else: print'remote: hacker!' if __name__ == '__main__': mac = MAC() message = 'see you at three o\'clock tomorrow' print'you seem to have intercepted something:{\%s:\%s}' %(mac.pad(message).encode('hex'), mac.code(mac.pad(message))) print'so send your message:' msg = 'please send me your flag' print(msg) msg_o = msg + chr(63 - len(msg)) * (63 - len(msg)) res = chr(0)*16 for i in range(len(msg_o)/16 - 1): res = strxor(msg_o[i*16:(i+1)*16], res) msg_o = msg_o[:32] + strxor("24054d4c1a0f19444e0f4016080f1805".decode('hex'), res) + msg_o[48:] print(msg_o.encode('hex')) print'and your code:' code = raw_input() mac.identity(msg.decode('hex'), code) exit()
解释一下,可以看到 code 那里我加了个 print ,输出第一组明文的十六位摘要和第二组明文的十六位摘要。而后对第二组明文进行二次摘要,对其加上一段十六位文本,让其异或之后与第一段明文的十六位摘要相等。再加上 1~15 个 pad,最后处理时利用 pad 保留下我们需要的文本 ‘please send me your flag’。