commit c9e57ef52d3801acdfb743f3d352e3eb64145353
parent 7cf90290d1fcb4b3baf5dfda58ef62ff5630ab98
Author: mpizzzle <michael.770211@gmail.com>
Date: Mon, 30 Oct 2017 21:42:08 +0000
more pointless refactoring (issue still not fixed)
Diffstat:
1 file changed, 14 insertions(+), 13 deletions(-)
diff --git a/set3/cbc_padding_oracle.py b/set3/cbc_padding_oracle.py
@@ -5,26 +5,27 @@ from Crypto.Random import random
key = Random.new().read(AES.block_size)
iv = Random.new().read(AES.block_size)
-with open('files/17.txt') as f:
- split_file = f.read().splitlines()
-
def encryption_oracle():
- plaintext = split_file[random.randint(0, 9)]
+ with open('files/17.txt') as f:
+ plaintext = f.read().splitlines()[random.randint(0, 9)]
+
pad_len = AES.block_size - (len(plaintext) % AES.block_size)
return AES.new(key, AES.MODE_CBC, iv).encrypt(plaintext + ''.join([chr(pad_len) for i in range(pad_len)]))
-def pkcs7_padding_oracle(msg, strip_mode):
- if ord(msg[len(msg) - 1]) > AES.block_size or ord(msg[len(msg) - 1]) == 0:
+def pkcs7_padding_validation(msg, strip_mode):
+ len_pad = ord(msg[len(msg) - 1])
+
+ if len_pad == 0 or len_pad > AES.block_size:
return False if not strip_mode else ""
- for c in msg[:len(msg) - ord(msg[len(msg) - 1]) - 1 : -1]:
- if c != msg[len(msg) - 1]:
+ for c in msg[len(msg) - len_pad:]:
+ if c != chr(len_pad):
return False if not strip_mode else ""
- return True if not strip_mode else msg[:len(msg) - ord(msg[len(msg) - 1])]
+ return True if not strip_mode else msg[:len(msg) - len_pad]
-def decrypt_and_validate_padding(ciphertext):
- return pkcs7_padding_oracle(AES.new(key, AES.MODE_CBC, iv).decrypt(ciphertext), False)
+def padding_oracle(ciphertext):
+ return pkcs7_padding_validation(AES.new(key, AES.MODE_CBC, iv).decrypt(ciphertext), False)
ciphertext = iv + encryption_oracle()
plaintext = ""
@@ -43,7 +44,7 @@ for b_idx in reversed(range((len(ciphertext) / AES.block_size) - 1)):
block[AES.block_size - i - 1] = chr(c)
blocks[b_idx] = ''.join(block)
- if decrypt_and_validate_padding(''.join(blocks[:b_idx + 2])):
+ if padding_oracle(''.join(blocks[:b_idx + 2])):
plaintext += chr(c ^ ord(guessed_byte) ^ (i + 1))
padding.append(c)
@@ -60,4 +61,4 @@ for b_idx in reversed(range((len(ciphertext) / AES.block_size) - 1)):
for p in range(i + 1):
block[AES.block_size - p - 1] = chr(padding[p] ^ (p + 1) ^ (i + 2))
-print pkcs7_padding_oracle(plaintext[::-1], True).decode("base64")
+print pkcs7_padding_validation(plaintext[::-1], True).decode("base64")