|
33 | 33 | 'padding_oracle',
|
34 | 34 | ]
|
35 | 35 |
|
| 36 | + |
36 | 37 | def padding_oracle(payload: Union[bytes, str],
|
37 | 38 | block_size: int,
|
38 | 39 | oracle: OracleFunc,
|
@@ -93,14 +94,14 @@ def padding_oracle(payload: Union[bytes, str],
|
93 | 94 | payload = to_bytes(payload)
|
94 | 95 | null_byte = to_bytes(null_byte)
|
95 | 96 |
|
96 |
| - |
97 | 97 | # Does the user want the encryption routine
|
98 | 98 | if (mode == 'encrypt'):
|
99 | 99 | return encrypt(payload, block_size, oracle, num_threads, null_byte, pad_payload, logger)
|
100 | 100 |
|
101 | 101 | # If not continue with decryption as normal
|
102 | 102 | return decrypt(payload, block_size, oracle, num_threads, null_byte, return_raw, logger)
|
103 | 103 |
|
| 104 | + |
104 | 105 | def decrypt(payload, block_size, oracle, num_threads, null_byte, return_raw, logger):
|
105 | 106 | # Wrapper to handle exceptions from the oracle function
|
106 | 107 | def wrapped_oracle(ciphertext: bytes):
|
@@ -171,21 +172,22 @@ def bytes_xor(byte_string_1: bytes, byte_string_2: bytes):
|
171 | 172 |
|
172 | 173 | plaintext_blocks = blocks(payload)
|
173 | 174 | ciphertext_blocks = [null_byte * block_size for _ in range(len(plaintext_blocks)+1)]
|
174 |
| - |
| 175 | + |
175 | 176 | solve_index = '1'
|
176 | 177 | block_total = str(len(plaintext_blocks))
|
177 | 178 |
|
178 | 179 | for index in range(len(plaintext_blocks)-1, -1, -1):
|
179 | 180 | plaintext = solve(b'\x00' * block_size + ciphertext_blocks[index+1], block_size, wrapped_oracle,
|
180 |
| - num_threads, result_callback, plaintext_callback) |
| 181 | + num_threads, result_callback, plaintext_callback) |
181 | 182 | ciphertext_blocks[index] = bytes_xor(plaintext_blocks[index], plaintext)
|
182 | 183 | solve_index = str(int(solve_index)+1)
|
183 |
| - |
| 184 | + |
184 | 185 | ciphertext = b''.join(ciphertext_blocks)
|
185 | 186 | logger.info(f"forged ciphertext: {ciphertext}")
|
186 | 187 |
|
187 | 188 | return ciphertext
|
188 | 189 |
|
| 190 | + |
189 | 191 | def get_logger():
|
190 | 192 | logger = logging.getLogger('padding_oracle')
|
191 | 193 | formatter = logging.Formatter('[%(asctime)s][%(levelname)s] %(message)s')
|
|
0 commit comments