-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathp36.py
163 lines (120 loc) · 4.51 KB
/
p36.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from hashlib import sha256
from hmac import compare_digest, new as new_hmac
from os import urandom
from typing import Dict
from main import Solution
from p33 import DiffieHellman
from Crypto.Random.random import randint
from requests import ConnectionError, post
from web import application, ctx, input as web_input
urls = ('/login', 'login', )
class Server:
class Response:
def __init__(self, code):
self.status_code = code
"""Simulates a server if you don't want to run one"""
passwd = {'[email protected]': 'abhorrentaardvark'}
N = DiffieHellman.default_p
g, k = 2, 3
salt, K = b'', b''
def get_client_ids(self, I: str, A: int) -> Dict:
self.salt = urandom(4)
P = Server.passwd[I]
xh = sha256(self.salt + P.encode()).hexdigest()
x = int(xh, 16)
v = pow(Server.g, x, Server.N)
b = randint(0, Server.N - 1)
exp_term = pow(Server.g, b, Server.N)
B = (Server.k * v + exp_term) % Server.N
uh = sha256(str(A).encode() + str(B).encode()).hexdigest()
u = int(uh, 16)
base = A * pow(v, u, Server.N) % Server.N
S = pow(base, b, Server.N)
self.K = sha256(str(S).encode()).digest()
return {'salt': self.salt, 'B': B}
def check_hmac(self, client_hmac: str) -> Response:
raw_hmac = new_hmac(self.K, self.salt, sha256)
server_hmac = raw_hmac.hexdigest()
mac_equal = compare_digest(client_hmac, server_hmac)
if mac_equal:
return self.Response(200)
else:
return self.Response(403)
def p36() -> str:
server = Server()
base_url = 'http://0.0.0.0:8080/login'
N = DiffieHellman.default_p
g, k = 2, 3
I, P = '[email protected]', 'abhorrentaardvark'
a = 0xdecafbad # randint(0, N - 1)
A = pow(g, a, N)
print('Sending I and A to server...')
try:
args = '?I={}&A={}'.format(I, A)
response = post(base_url + args)
response_content = eval(response.content)
except ConnectionError:
response_content = server.get_client_ids(I, A)
salt, B = response_content.get('salt'), response_content.get('B')
print(f'Server responded with salt = {salt} and B = {str(B)[:20]}...')
uh = sha256(str(A).encode() + str(B).encode()).hexdigest()
u = int(uh, 16)
xh = sha256(salt + P.encode()).hexdigest()
x = int(xh, 16)
base = B - k * pow(g, x, N)
exp = a + u * x
S = pow(base, exp, N)
K = sha256(str(S).encode()).digest()
print(f'Client computed K = {K[:20]}...')
client_hmac = new_hmac(K, salt, sha256).hexdigest()
print('Sending HMAC to server...')
try:
args = '?hmac={}'.format(client_hmac)
response = post(base_url + args)
except ConnectionError:
response = server.check_hmac(client_hmac)
if response.status_code != 200:
return 'Server responded with "403 Forbidden"'
return 'Server responded with "200 OK"'
def main() -> Solution:
return Solution('36: Implement Secure Remote Password (SRP)', p36)
# BELOW CODE IS THE WEBSERVER THAT HANDLES CLIENT SRP REQUESTS
class login():
N = DiffieHellman.default_p
g, k = 2, 3
passwd = {'[email protected]': 'abhorrentaardvark'}
salt, B, K = None, None, None
def POST(self) -> str:
data = web_input()
keys = data.keys()
if 'I' in keys and 'A' in keys:
login.salt = urandom(4)
P = login.passwd[data.I]
xh = sha256(login.salt + P.encode()).hexdigest()
x = int(xh, 16)
v = pow(login.g, x, login.N)
A = int(data.A)
b = randint(0, Server.N - 1)
exp_term = pow(login.g, b, login.N)
B = (login.k * v + exp_term) % login.N
uh = sha256(str(A).encode() + str(B).encode()).hexdigest()
u = int(uh, 16)
base = A * pow(v, u, self.N) % self.N
S = pow(base, b, login.N)
login.K = sha256(str(S).encode()).digest()
return str({'salt': login.salt, 'B': B})
elif 'hmac' in keys:
raw_hmac = new_hmac(login.K, login.salt, sha256)
server_hmac = raw_hmac.hexdigest()
mac_equal = compare_digest(data.hmac, server_hmac)
if mac_equal:
ctx.status = '200 OK'
return 'explicit 200'
else:
ctx.status = '403 Forbidden'
return 'explicit 403'
def start_server():
app = application(urls, globals())
app.run()
if __name__ == '__main__':
start_server()