#!/usr/bin/python

from pwn import *
from cbcExploit import PaddingOracle

var='5745d4bbed207b6f09bcbaac0782ffeebdf92d8a.3ced346abc4e2a298ef26337cb6e697b6b1d8900051e49a323c5ddf7ee361c6fa2d0e7ae3414805d134890e8b3bed32f2edec8e3ace12d529d7f1d9d12d0eb7b'.split('.')
mac=var[0]
var=var[1]

class Padding(PaddingOracle):
	def oracle(self, payload, iv, previous_resp, **kwargs):
		payload=kwargs['prefix']+'.'+iv+payload
		ret=subprocess.check_output(['./test_cbc.py', payload])
		if 'Nope' in ret:
			return True, None
		return False, None

key_size=16
po=Padding(key_size)

decrypted=po.decrypt(ciphertext=var, is_correct=True, prefix=mac)

want_it='username=ahg8v94\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x12&username=admin'
want_it=po.add_padding(want_it).encode('hex')
orig=po.add_padding(decrypted).encode('hex')

print po.fake_ciphertext(new_plaintext=want_it, orginal_ciphertext=var, orginal_plaintext=orig, is_correct=True, prefix=mac)