-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc37.py
81 lines (66 loc) · 1.81 KB
/
c37.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
from matasano import *
class SRP_Server:
def __init__(self):
self.N = 2**256-2**224+2**192+2**96-1 #Arbitrary, from NIST curve
self.g = 2
self.k = 3
self.salts = {}
self.hashes = {}
self.OnRecv = self.Init_SRP
def Register(self, username, password):
salt = randbytes(16)
x = bytes2int(hash(salt+password,sha256))
v = pow(self.g,x,self.N)
self.salts[username] = salt
self.hashes[username] = v
def Init_SRP(self,msg):
username = msg[0]
A = msg[1]
b = randint(0,self.N-1)
B = (self.k*self.hashes[username]+pow(self.g,b,self.N))%self.N
u = bytes2int(hash(int2bytes(A)+int2bytes(B),sha256))
S = int2bytes(pow(A*pow(self.hashes[username],u,self.N),b,self.N))
self.K = hash(S,sha256)
self.Send((self.salts[username],B))
self.OnRecv = self.Check_SRP
def Check_SRP(self,msg):
self.Send(hmac(self.K,self.salts[msg[0]],sha256) == msg[1])
self.OnRecv = self.Init_SRP
def Send(self,msg):
self.conn.Send(msg,self)
class SRP_Client_Bad:
def __init__(self):
self.N = 2**256-2**224+2**192+2**96-1 #Arbitrary, from NIST curve
self.g = 2
self.k = 3
self.salts = {}
self.hashes = {}
def Login(self, username, password):
self.username = username
self.password = password
self.Send((self.username,0))
self.OnRecv = self.GenerateHmac
def GenerateHmac(self, msg):
salt = msg[0]
S = int2bytes(0)
K = hash(S,sha256)
h = hmac(K,salt,sha256)
self.Send((self.username,h))
self.OnRecv = self.PrintMsg
def PrintMsg(self,msg):
print("Logged in: %r" % msg)
def Send(self,msg):
self.conn.Send(msg,self)
C = SRP_Client_Bad()
S = SRP_Server()
conn = Connection(C,S)
S.Register(b'user',b'pass')
print("Good login")
C.Login(b'user', b'pass')
while not conn.Done():
conn.Update()
print()
print("Bad login")
C.Login(b'user', b'passw')
while not conn.Done():
conn.Update()