-
Notifications
You must be signed in to change notification settings - Fork 0
/
S04C32.py
84 lines (68 loc) · 2.09 KB
/
S04C32.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Imports
import os
import web
import json
import time
import hashlib
from lib import *
# Web Server
urls = (
'/hello', 'Hello',
'/test', 'Hash'
)
app = web.application(urls, globals())
HMAC_obj = HMAC(b"YELLOW_SUBMARINE", hashlib.sha1)
class Hello:
def GET(self):
params = web.input()
name = params.name
if not name:
name = 'World'
string = "Hello, " + name + "!"
return {"name" : string}
class Hash:
def _insecure_compare(self, hash1, hash2, delay):
for b1, b2 in zip(hash1, hash2):
if b1 != b2:
return False
time.sleep(delay)
return True
def GET(self):
global HMAC_obj
params = web.input()
file = params.file
signature = params.signature
delay = params.delay
hmac = HMAC_obj.compute(file.encode())
if self._insecure_compare(hmac.encode(), signature.encode(), float(delay)):
return web.HTTPError(200)
else:
return web.HTTPError(500)
def main():
# Given
delay = 0.005
signature = ""
filename = "foo"
for _ in range(hashlib.sha1().digest_size * 2):
# We go for twice the size because hexadecimal byte is 2 digits long.
times = []
# This loop goes over all 16 hexadecimal bytes.
for i in range(16):
runtime = 0
# Introduced more rounds so the time difference is prominent
for _ in range(20):
start = time.time()
response = app.request("/test?delay=" + str(delay) + "&file=" + filename + "&signature=" + signature + hex(i)[-1])
finish = time.time()
runtime += finish - start
times.append(runtime)
signature += hex(times.index(max(times)))[-1]
print("> Discovered signature:", signature)
response = app.request("/test?delay=" + str(delay) + "&file=" + filename + "&signature=" + signature + hex(i)[-1])
if response.status == 200:
print("> Brute force successful.\n> Signature:", signature)
else:
print("Brute force failed.")
return
if __name__=="__main__":
main()