-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathp32.py
52 lines (37 loc) · 1.33 KB
/
p32.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
43
44
45
46
47
48
49
50
51
52
from time import time
from main import Solution
from p31 import hmac_sha1, master_key, start_server, validate_sig, use_network
from requests import post
def p32() -> str:
file = b'secrets.docx'
expected = hmac_sha1(master_key, file)
print(f'Calculated HMAC-SHA1 - {expected}')
rounds = 10
is_networked = use_network()
known = b''
while len(known) < len(expected):
unknown = (len(expected) - len(known) - 2) * b'?'
longest, best = 0.0, ''
for byte in range(0xff + 1):
total = 0.0
for _ in range(rounds):
sig = known + bytes([byte]) + unknown
url = f'http://0.0.0.0:8080/hmac?file={file}&sig={sig}&sleep={5}'
start = time()
if is_networked:
post(url)
else:
validate_sig(file, sig, 5)
end = time()
total += end - start
if total > longest:
longest = total
best = bytes([byte])
known += best
print(known)
return f'Calculated HMAC-SHA1 - {known}'
def main():
return Solution('32: Break HMAC-SHA1 with a slightly less artificial timing leak', p32)
# BELOW CODE RUNS THE WEBSERVER THAT HANDLES THE POST TO /hmac
if __name__ == '__main__':
start_server()