Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
formatting issues
forgot to check formatting was ok, have fixed some issues with it. flake8 is not happy with the formatting
  • Loading branch information
q3st1on committed Nov 7, 2023
commit 0a89f865c2a524b6685ab6c3f48cf71f6bed2fac
10 changes: 6 additions & 4 deletions src/padding_oracle/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
'padding_oracle',
]


def padding_oracle(payload: Union[bytes, str],
block_size: int,
oracle: OracleFunc,
Expand Down Expand Up @@ -93,14 +94,14 @@ def padding_oracle(payload: Union[bytes, str],
payload = to_bytes(payload)
null_byte = to_bytes(null_byte)


# Does the user want the encryption routine
if (mode == 'encrypt'):
return encrypt(payload, block_size, oracle, num_threads, null_byte, pad_payload, logger)

# If not continue with decryption as normal
return decrypt(payload, block_size, oracle, num_threads, null_byte, return_raw, logger)


def decrypt(payload, block_size, oracle, num_threads, null_byte, return_raw, logger):
# Wrapper to handle exceptions from the oracle function
def wrapped_oracle(ciphertext: bytes):
Expand Down Expand Up @@ -171,21 +172,22 @@ def bytes_xor(byte_string_1: bytes, byte_string_2: bytes):

plaintext_blocks = blocks(payload)
ciphertext_blocks = [null_byte * block_size for _ in range(len(plaintext_blocks)+1)]

solve_index = '1'
block_total = str(len(plaintext_blocks))

for index in range(len(plaintext_blocks)-1, -1, -1):
plaintext = solve(b'\x00' * block_size + ciphertext_blocks[index+1], block_size, wrapped_oracle,
num_threads, result_callback, plaintext_callback)
num_threads, result_callback, plaintext_callback)
ciphertext_blocks[index] = bytes_xor(plaintext_blocks[index], plaintext)
solve_index = str(int(solve_index)+1)

ciphertext = b''.join(ciphertext_blocks)
logger.info(f"forged ciphertext: {ciphertext}")

return ciphertext


def get_logger():
logger = logging.getLogger('padding_oracle')
formatter = logging.Formatter('[%(asctime)s][%(levelname)s] %(message)s')
Expand Down
4 changes: 3 additions & 1 deletion src/padding_oracle/solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'add_padding'
]


class Pass(NamedTuple):
block_index: int
solved: List[int]
Expand Down Expand Up @@ -266,10 +267,11 @@ def remove_padding(data: Union[str, bytes, List[int]]) -> bytes:
data = to_bytes(data)
return data[:-data[-1]]


def add_padding(data: Union[str, bytes, List[int]], block_size: int) -> bytes:
'''
Add PKCS#7 padding bytes.
'''
data = to_bytes(data)
pad_len = block_size - len(data) % block_size
return data + (bytes([pad_len]) * pad_len)
return data + (bytes([pad_len]) * pad_len)