-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path38.py
More file actions
185 lines (140 loc) · 5.54 KB
/
38.py
File metadata and controls
185 lines (140 loc) · 5.54 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import convert
import diffiehellman as dh
import hash
import random
import socket
import srp
import threading
class ModifiedSRPClient(srp.SRPClient):
'''
Returns true if logging in to the given server with the given username and password succeeds
'''
def login(self, ip, port):
sendSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sendSocket.connect((ip, port))
privateKey = srp.generatePrivateKey()
sendSocket.send(str(self.username)+","+str(srp.generateClientPublicValue(privateKey)))
salt, serverPublicValue, u = self.recieveServerValues(sendSocket)
if salt is None or serverPublicValue is None or u is None:
sendSocket.close()
return False
x = srp.getHashInt(self.password+salt)
sharedSecret = pow(serverPublicValue, (privateKey+u*x), srp.STANDARD_N)
sharedSecret = convert.intToByteString(hash.sha256(convert.intToByteString(sharedSecret)))
validator = srp.generateClientValidator(sharedSecret, salt)
sendSocket.send(str(validator))
if self.recieveOK(sendSocket):
sendSocket.close()
return True
sendSocket.close()
return False
'''
Recieves the server's public value and salt from the given socket.
Returns a tuple of salt, public value
Returns None, None if something goes wrong
'''
def recieveServerValues(self, recieveSocket):
try:
data = recieveSocket.recv(1024)
splitData = data.split(",")
return (convert.intToByteString(int(splitData[0])), int(splitData[1]), int(splitData[2]))
except Exception as e:
return (None, None, None)
class ModifiedSRPServer(srp.SRPServer):
def runServer(self):
#Socket setup
recieveSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
recieveSocket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
recieveSocket.bind(("", self.port))
recieveSocket.listen(5)
#Establish connection
clientSock, _ = recieveSocket.accept()
userName, clientPublicValue = self.recieveClientValues(clientSock)
#Error case
if userName is None or clientPublicValue is None:
return
passwordVerifier, salt = self.users[userName]
sessionPrivateKey = srp.generatePrivateKey()
u = random.randint(0, 2**128)
#Send public values
clientSock.sendall(str(convert.byteStringToInt(salt))+","+str(dh.generatePublicValue(sessionPrivateKey, srp.STANDARD_G, srp.STANDARD_N))+","+str(u))
#Derive shared secret
sharedSecret = pow(clientPublicValue*pow(passwordVerifier, u, srp.STANDARD_N), sessionPrivateKey, srp.STANDARD_N)
sharedSecret = convert.intToByteString(hash.sha256(convert.intToByteString(sharedSecret)))
clientValidator = self.recieveClientValidator(clientSock)
#Error check
if clientValidator is not None:
if clientValidator == srp.generateClientValidator(sharedSecret, salt):
clientSock.sendall("OK")
return
try:
clientSock.sendall("NOTOK")
except Exception:
pass
class AttackSRPServer(ModifiedSRPServer):
def runServer(self):
#Socket setup
recieveSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
recieveSocket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
recieveSocket.bind(("", self.port))
recieveSocket.listen(5)
#Establish connection
clientSock, _ = recieveSocket.accept()
userName, clientPublicValue = self.recieveClientValues(clientSock)
#Error case
if userName is None or clientPublicValue is None:
return
salt = srp.generateRandomSalt()
sessionPrivateKey = srp.generatePrivateKey()
u = random.randint(0, 2**128)
#Send public values
clientSock.sendall(str(convert.byteStringToInt(salt))+","+str(dh.generatePublicValue(sessionPrivateKey, srp.STANDARD_G, srp.STANDARD_N))+","+str(u))
#Get validator
clientValidator = self.recieveClientValidator(clientSock)
#We're not actually checking the password
clientSock.sendall("OK")
#Function to check that the password will produce a matching validator
checkPassword = lambda password: self.generateClientValidator(password, salt, clientPublicValue, sessionPrivateKey, u) == clientValidator
'''
#Assume the password is made up only of printing ascii characters
initialChar = 32
finalChar = 126
#Check all possible passwords - this is really, really, slow.
password = [initialChar]
passwordLen = 1
while not checkPassword("".join(map(chr, password))):
if password == [finalChar]*passwordLen:
passwordLen += 1
password = [initialChar]*passwordLen
else:
index = passwordLen-1
while(password[index] == finalChar):
password[index] = initialChar
index -= 1
password[index] += 1
print "".join(map(chr, password))
'''
#Altarnatively, a dictionary attack
passwordFile = open("10k most common.txt")
for line in passwordFile:
line = line.rstrip()
if checkPassword(line):
print line
return
def generateClientValidator(self, password, salt, clientPublicValue, serverPrivateKey, u, debug = False):
passwordVerifier = srp.generatePasswordVerifier(password, salt)
sharedSecret = pow(clientPublicValue * pow(passwordVerifier, u, srp.STANDARD_N), serverPrivateKey, srp.STANDARD_N)
sharedSecret = convert.intToByteString(hash.sha256(convert.intToByteString(sharedSecret)))
return srp.generateClientValidator(sharedSecret, salt)
if __name__ == "__main__":
server = ModifiedSRPServer(50000)
server.addUser("steve", "password")
serverThread = threading.Thread(target=server.runServer)
serverThread.start()
client = ModifiedSRPClient("steve", "password")
client.login("localhost", 50000)
server = AttackSRPServer(50000)
serverThread = threading.Thread(target=server.runServer)
serverThread.start()
client = ModifiedSRPClient("steve", "letsgo")
client.login("localhost", 50000)