Path: blob/master/Block-Cipher/Attack-CBC-Byte-at-a-Time/exploit.py
1402 views
from Crypto.Cipher import AES1from Crypto.Util.number import *2from os import urandom3from secret import flag45BLOCKSIZE = 166key = urandom(16)7print "key: ", key.encode("hex")8iv = urandom(16)9print "iv: ", iv.encode("hex")1011def pad(s):12s += (BLOCKSIZE - (len(s) % BLOCKSIZE))*(chr(BLOCKSIZE - (len(s) % BLOCKSIZE)))13return s1415def encryption(plaintext):16plaintext = plaintext + flag17plaintext = pad(plaintext)18assert len(plaintext) % BLOCKSIZE == 019obj1 = AES.new(key, AES.MODE_CBC, iv)20ciphertext = obj1.encrypt(plaintext)21return ciphertext.encode("hex")2223#<----------------------------------</Server-Side>----------------------------------->24#<----------------------------------<Attacker's-Side>-------------------------------->25s = ""26for k in range(4):27for i in range(1, BLOCKSIZE+1):28input_str = 'a'*(16-i)29ct = encryption(input_str)[32*k:32*k+32]30for j in range(256):31ct1 = encryption(input_str + s + chr(j))[32*k:32*k+32]32if ct == ct1:33s += chr(j)34break35print s36#<-----------------------------------</Attacker's-Side>------------------------------>3738