-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_attack.py
42 lines (31 loc) · 1.18 KB
/
run_attack.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
import socket
from padding_oracle_attack import PaddingOracle, PaddingOracleAttack
class SimOracle(PaddingOracle):
def __init__(self, host: str, port: int):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((host, port))
def check_padding(self, data: bytes, iv: bytes) -> bool:
self.socket.sendall(iv + data)
resp_data = self.socket.recv(4096)
if resp_data[:2] == b'er':
if resp_data[2:] in [b'Padding is incorrect.', b'PKCS#7 padding is incorrect.']:
return False
return True
if __name__ == '__main__':
import sys
import time
if len(sys.argv) != 4 :
print(f'Usage: {sys.argv[0]} HOST PORT HEXDATA')
exit(0)
host = sys.argv[1]
port = int(sys.argv[2])
data = bytes.fromhex(sys.argv[3])
oracle = SimOracle(host, port)
attack = PaddingOracleAttack(oracle, verbose=True)
start = time.time()
# Skip the HMAC, use second block of
# ciphertext as IV
res = attack.decrypt(data[48:], data[32:48])
end = time.time()
print(f'Done! Decrypted data in {end - start:.3f} seconds:')
print(res.decode('utf-8'))