-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.py
36 lines (28 loc) · 1.32 KB
/
example.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
#!/usr/bin/env python
from padding_oracle import PaddingOracle
from optimized_alphabets import json_alphabet
import requests
# This function has to be implemented and will be passed to the PaddingOracle constructor.
# It gets a hex encoded cipher text and has to return True if it can be decrypted successfully,
# False otherwise.
#
# Here is an example implementation that I used for P.W.N. CTF 2018.
def oracle(cipher_hex):
headers = {'Cookie': 'vals={}'.format(cipher_hex)}
r = requests.get('http://converter.uni.hctf.fun/convert', headers=headers)
response = r.content
if b'Invalid padding bytes.' not in response:
return True
else:
return False
# Instantiate the helper with the oracle implementation
o = PaddingOracle(oracle, max_retries=-1)
# Decrypt the plain text.
# To make the guesswork faster, use an alphabet optimized for JSON data.
cipher = 'b5290bd594ba08fa58b1d5c7a19f876c338191a51eeeac94c2b434bdb8adbfb8596f996d6eddca93c059e3dc35f7bef36b57a5611250ec4528c11e1573799d2178c54c034b9ea8fda8ae9a4a41c67763'
plain, padding = o.decrypt(cipher, optimized_alphabet=json_alphabet())
print('Plaintext: {}'.format(plain))
# Craft a modified but valid cipher text
plain_new = plain[:24] + b'XXXX' + plain[28:]
cipher_new = o.craft(cipher, plain, plain_new)
print('Modified: {}'.format(cipher_new))