def create_admin(oracle): prefix_len = len_of_prefix(oracle) inp = 'A' * 16 cipher = oracle(inp) keystream = XOR(inp, cipher[prefix_len:prefix_len + 16]) attack = cipher[:prefix_len] + XOR('XXXX;admin=true;', keystream) + cipher[prefix_len + 16:] return attack if verify_admin(attack) else False
def create_admin(oracle): inp='A'*16+'XadminXtrueXAAAA' cp=oracle(inp) req_decrypted_block=';admin=true;AAAA' #Now modify the cp block corresponding to 'A'*16 so that next block on decrypion gives req_decrypted_block attack=XOR(XOR('XadminXtrueXAAAA',req_decrypted_block),cp[32:48])+cp[48:] if (verify_admin(attack)): return attack
def find_nxt_chr(cipher,pr_block,cur_block,known,oracle): k =15-len(known) #kth chr to be decrypted in the block C0 =pr_block P ='X'*(16-len(known)-1)+'?'+known _P ='X'*(16-len(known)-1)+chr(len(known)+1)*(len(known)+1) _C0 =XOR(XOR(_P,P),C0) for c in range(256): _C0=_C0[0:k]+chr(c)+_C0[k+1:] attack=_C0+cur_block if (oracle(attack)): return XOR(XOR(_P[k],chr(c)),C0[k])
def AES_128_CTR(data,key,nonce=0): from math import ceil keystream='' for i in range(int(ceil(len(data)/16.0))): keystream+=AES_128_ECB_encrypt(int_TO_little_endian(nonce),key,False) nonce+=1 keystream=keystream[:len(data)] return XOR(data,keystream)
def crack(): cipher = encryption_oracle('A'*80) attack = cipher[0:16]+'\x00'*16+cipher[0:16]+cipher[-32:] recovered_pt = '' try: recovered_pt = decryption_oracle(attack) except ValueError as e: recovered_pt = str(e) key = XOR(recovered_pt[0:16],recovered_pt[32:48]) if key == IV : print '[+] Cracked key : %s' % repr(key)
def crack(ciphers): key = '' max_len = max(len(c) for c in ciphers) for i in range(max_len): #Xor key for the i-th byte ith_cipher = '' for c in ciphers: try: ith_cipher += c[i] except: pass key += single_byte_xor_key(ith_cipher) print '[+] Cracked KEY : ' + key print '[+] Decrypting Cipher' for c in ciphers: print XOR(c, key[:len(c)])
def crack(cipher, oracle): attack = 'A'*len(cipher) keystream = XOR (attack, oracle(cipher, 0, attack)) pt = XOR(cipher, keystream) return pt