-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_5.py
More file actions
134 lines (118 loc) · 4.21 KB
/
5_5.py
File metadata and controls
134 lines (118 loc) · 4.21 KB
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
import socket
import hashlib
import hmac
import json
import subprocess
import time
import os
from lib.Message import Message
from lib.utils import crypto_random
from lib.socket_wrapper import socket_send, socket_receive, get_client_socket
N = 0x009f4f57c0d386b90c5cf147d96466c5c7b2d154e7d32a58695191847f58f5e2ca9c28b497ae1b31d1c2507b1c489662a9d39c5b5100503888cfd7c762a7c1310d26b8ae38ad8de4ba3ff800022221c73be2da3113b4a7ba404a32a446adb9dedb2958bab3b26f2984396e1af1fc28594182b2a72de7fed99ea03e34c0d69e02db
g = 2
k = 3
I = '[email protected]'
P = 'somepassword1234'
def log_in_with_A_set(sock, A):
# C & S: Agree on N=[NIST Prime], g=2, k=3, I (email), P (password)
print "Sending N, g & k to the server."
data = json.dumps({'N': N, 'g': g, 'k': k})
socket_send(sock, data)
print ""
# C->S: Send I, A=0
print "C->S: Send I, A=0"
print "I:", I
print "A:", A
print ""
data = json.dumps({'I': I, 'A': A})
socket_send(sock, data)
values = socket_receive(sock)
for key, value in values.iteritems():
globals()[key] = value
assert salt != None
assert B != None
print 'Received variables salt and B'
print "salt:", salt
print "B:", B
print ""
# S, C: Compute string uH = SHA256(A|B), u = integer of uH
print "S, C: Compute string uH = SHA256(A|B), u = integer of uH"
u_sha256_generator = hashlib.sha256()
u_sha256_generator.update(Message().set_int(A).to_str() + Message().set_int(B).to_str())
u = int(u_sha256_generator.hexdigest(), 16)
print "u:", u
print ""
# C:
# Generate string xH=SHA256(salt|password)
# Convert xH to integer x somehow (put 0x on hexdigest)
print " Generate string xH=SHA256(salt|password)"
print " Convert xH to integer x somehow (put 0x on hexdigest)"
x_sha256_generator = hashlib.sha256()
x_sha256_generator.update(Message().set_int(salt).to_str() + P)
x = int(x_sha256_generator.hexdigest(), 16)
# Generate S = (B - k * g**x)**(a + u * x) % N
print " We know S = 0"
# buff = B - k * g**x
S = 0
print "S:", S
# Generate K = SHA256(S)
print " Generate K = SHA256(S)"
K_sha256_generator = hashlib.sha256()
K_sha256_generator.update(Message().set_int(S).to_str())
K = int(K_sha256_generator.hexdigest(), 16)
print "K:", K
print ""
# C->S: Send HMAC-SHA256(K, salt)
signature = hmac.HMAC(Message().set_int(K).to_str(), Message().set_int(salt).to_str(), hashlib.sha256).hexdigest()
data = json.dumps({'signature': signature})
socket_send(sock, data)
print "C->S: Send HMAC-SHA256(K, salt)"
print ""
values = socket_receive(sock)
for key, value in values.iteritems():
globals()[key] = value
assert response != None
if response == "OK":
print "OK: Logged in"
elif response == 'ERROR':
print "ERROR: Not logged in"
def log_loop_with_A_set(args, A):
# Start server.
print "Starting the server ..."
server_path = os.path.join('lib', 'srp_server.py')
server_logfile = os.path.join("/tmp", "srp_server.log")
with open(server_logfile, 'w+') as devnull:
server_process = subprocess.Popen(
'python {} {}'.format(server_path, str(args.port_number)),
shell=True,
stdout=devnull,
stderr=devnull)
time.sleep(2)
ret = server_process.poll()
if ret != None:
print "Server has not started correctly. Port already in use?"
exit(1)
# Get communication socket.
sock = get_client_socket(args.hostname, args.port_number)
print "Communications initialised"
print ""
try:
log_in_with_A_set(sock, A)
finally:
sock.shutdown(socket.SHUT_RDWR)
def main(args):
for A in [0, N, N**2]:
print "*" * 70
log_loop_with_A_set(args, A)
print "*" * 70
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description='Break SRP with a zero key - Challenge 35 (Set 5) of Matasano Crypto Challenge.'
)
parser.add_argument('hostname',
help='Address of the host to connect to', type=str)
parser.add_argument('port_number',
help='Port number to connect to', type=int)
args = parser.parse_args()
main(args)