Buscar

0d7391da-f8da-4f76-8c84-9882ceab5dee

Prévia do material em texto

CRIPTOGRAFIA HASH E HACKING 
 
W
BA
04
70
_v
1.
0 
 
 
 
 
Proposta de Resolução 
Autoria: Marcelo Ferreira Zochio 
Leitura crítica: Priscilla Viana Cunha 
 
O aluno deve desenvolver um programa que aplique o ataque Padding Oracle 
ou pesquisar algum que o faça. Se ele realizar a pesquisa, com certeza 
encontrará o PadBuster, que realiza ataques Padding Oracle contra sites que 
usam o modo CBC. Se ele desenvolver um software para quebrar textos 
cifrados em modo CBC, poderá escolher qualquer linguagem que ele se sinta 
confortável em escrever o programa. A seguir, uma solução em Python: 
import random 
import sys 
from Crypto.Cipher import AES 
BLOCK_SIZE = 16 # bytes 
#INIT_VEC = 'This is an IV456' # hardcoding this is a terrible idea 
INIT_VEC = random.randint(1,10000) 
iv= hex(INIT_VEC)[2:8].zfill(16) 
EXAMPLE_TEXT = raw_input("Digite o texto a ser cifrado: ") 
EXAMPLE_TEXT1 = EXAMPLE_TEXT.encode('hex') 
hex_symbols = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'] 
final_cleartext = '' 
class InvalidPadding(Exception): 
 pass 
def blockify(text, block_size=BLOCK_SIZE): 
 return [text[i:i+block_size] for i in range(0, len(text), block_size)] 
def key_gen(): 
 
 
 
 return "".join([chr(random.getrandbits(8)) for _ in xrange(BLOCK_SIZE)]) 
def validate_padding(padded_text): 
 return all([n == padded_text[-1] for n in padded_text[-ord(padded_text[-1]):]]) 
def pkcs7_pad(text): 
 length = BLOCK_SIZE - (len(text) % BLOCK_SIZE) 
 text += chr(length) * length 
 return text 
def pkcs7_depad(text): 
 if not validate_padding(text): 
 raise InvalidPadding() 
 return text[:-ord(text[-1])] 
def encrypt(plaintext, key, init_vec): 
 cipher = AES.new(key, AES.MODE_CBC, iv) 
 padded_text = pkcs7_pad(plaintext) 
 ciphertext = cipher.encrypt(padded_text) 
 return ciphertext 
def decrypt(ciphertext, key, iv): 
 cipher = AES.new(key, AES.MODE_CBC, iv) 
 padded_text = cipher.decrypt(ciphertext) 
 plaintext = pkcs7_depad(padded_text) 
 return plaintext 
def numberify(characters): 
 return map(lambda x: ord(x), characters) 
def stringify(numbers): 
 
 
 
 return "".join(map(lambda x: chr(x), numbers)) 
if __name__ == "__main__": 
 my_key = key_gen() 
 print "Chave gerada: ", my_key 
 IV = numberify(iv) 
 ciphertext = numberify(encrypt(EXAMPLE_TEXT1, my_key, iv)) 
 blocks = blockify(ciphertext) 
 cleartext = [] 
 for block_num, (c1, c2) in enumerate(zip([IV]+blocks, blocks)): 
 print "cracking block {} out of {}".format(block_num+1, len(blocks)) 
 i2 = [0] * 16 
 p2 = [0] * 16 
 for i in xrange(15,-1,-1): 
 for b in xrange(0,256): 
 prefix = c1[:i] 
 pad_byte = (BLOCK_SIZE-i) 
 suffix = [pad_byte ^ val for val in i2[i+1:]] 
 evil_c1 = prefix + [b] + suffix 
 try: 
 decrypt(stringify(c2), my_key, stringify(evil_c1)) 
 except InvalidPadding: 
 pass 
 else: 
 i2[i] = evil_c1[i] ^ pad_byte 
 
 
 
 p2[i] = c1[i] ^ i2[i] 
 break 
 cleartext+=p2 
 separated_cleartext = stringify(cleartext) 
 # print "i2:", i2 
 # print "c2:", c2 
 # print "p2:", p2 
 # print "block:[{}]".format(stringify(p2)) 
 # print "expected:[{}]".format(EXAMPLE_TEXT1[(16 * block_num):(16 * 
block_num)+16]) 
 for i in separated_cleartext: 
 for j in hex_symbols: 
 if i==j: 
 final_cleartext+=i 
 else: 
 pass 
 print "Texto decifrado: ", final_cleartext.decode('hex') 
 
 
 
 
 
 
	CRIPTOGRAFIA HASH E HACKING
	Proposta de Resolução
	Autoria: Marcelo Ferreira Zochio
	Leitura crítica: Priscilla Viana Cunha

Continue navegando