-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
325 lines (268 loc) · 8.49 KB
/
client.py
File metadata and controls
325 lines (268 loc) · 8.49 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import base64
import sys
import os
import time
import socket
import random
import hashlib
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
def generateRandomString(l):
mk = ""
for _ in range(l):
x = random.randint(0,61)
if(x<26):
mk += chr(ord('A')+x)
elif(x<52):
mk += chr(ord('a')+x-26)
else:
mk += chr(ord('0')+x-52)
return mk
def sender(args):
counter = 0
for i in range(1, len(args)-1):
if args[i]=='-n':
NAME = args[i+1].ljust(12)
counter += 1
elif args[i]=='-o':
OTHER = args[i+1].ljust(12)
counter += 1
elif args[i]=='-i':
INPUTFILE = args[i+1]
counter+=1
elif args[i]=='-a':
KDCIP = args[i+1]
counter += 1
elif args[i]=='-p':
KDCPORT = int(args[i+1])
counter += 1
if counter != 5:
print("Invalid arguments")
return
#connect to kdc
IV = b'\x00'*16
MYPORT = 10001
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
IP = '127.0.0.1'.ljust(16)
s.bind((IP, MYPORT))
print("Using port ", MYPORT)
s.connect((KDCIP, KDCPORT))
print("Registering to KDC - ", NAME)
# send initial regisitration msg
masterKey = generateRandomString(12)
# print(masterKey)
# msg size = 5+16+1+8+1+12+1+12+1 = 57 bytes
msg = "|301|"+ IP +"|"+str(MYPORT).ljust(8)+"|"+masterKey+"|"+NAME+"|"
s.sendall(bytes(msg, 'utf-8'))
data = s.recv(1024)
data = data.decode('utf-8').split('|')
if(data[1]!= '302'):
print("KDC registration failed")
exit(0)
s.close()
print("Registration successful !!!")
print("Sleeping ...")
time.sleep(15)
# generate key from master key using md5
sender_key = hashlib.md5(bytes(masterKey, 'utf-8')).digest()
#ask for details of other
nonce1 = generateRandomString(16)
text = bytes(NAME+ "|" +OTHER+ "|" +nonce1, 'utf-8')
# print(text)
if len(text)%16 > 0:
pad = 16-len(text)%16
text = text + (b'\x00'*pad)
# print(text)
cipher = Cipher(algorithms.AES(sender_key), modes.CBC(IV))
encryptor = cipher.encryptor()
enc_text = encryptor.update(text) + encryptor.finalize()
# print(enc_text)
enc_text64 = base64.b64encode(enc_text)
enc_text64 = enc_text64.decode('utf-8')
msg = bytes("|305|"+enc_text64+"|"+NAME+"|", 'utf-8')
print("Querying KDC for session with - ", OTHER)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((IP, MYPORT))
s.connect((KDCIP, KDCPORT))
s.sendall(msg)
# print(msg)
reply = s.recv(1024)
s.close()
parts = reply.decode('utf-8').split('|')
# print(parts)
opcode = parts[1]
if(opcode!='306'):
print("Failed !!!")
exit(0)
E_ka64 = parts[2]
E_ka = base64.b64decode(E_ka64)
# 160 bytes
# print(E_ka)
sender_key = hashlib.md5(bytes(masterKey, 'utf-8')).digest()
cipher = Cipher(algorithms.AES(sender_key), modes.CBC(IV))
dec = cipher.decryptor()
msg = dec.update(E_ka)
# print(len(msg)) #160 bytes
msg2 = msg[:80]
E_kb = msg[80:]
i = len(msg2)-1
while(msg2[i]==0):
i = i-1
msg2 = msg2[0:i+1]
msg2 = msg2.decode('utf-8').split('|')
unique_session_code = msg2[0]
print("Successfully received address and session key from KDC ...")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((IP, MYPORT))
s.connect((msg2[4], int(msg2[5])))
E_kb64 = base64.b64encode(E_kb).decode('utf-8')
print("Sending session key to - ", OTHER)
transfer_req = "|309|" + E_kb64 + "|" + NAME + "|"
s.sendall(bytes(transfer_req, 'utf-8'))
time.sleep(2)
# encrypt file data
inpF = open(INPUTFILE, 'r')
data = inpF.read()
inpF.close()
session_key = hashlib.md5(bytes(unique_session_code, 'utf-8')).digest()
# print(session_key)
cipher = Cipher(algorithms.AES(session_key), modes.CBC(IV))
encr = cipher.encryptor()
data = bytes(data, 'utf-8')
if len(data)%16>0:
pad = 16-len(data)%16
data = data + (b'\x00'*pad)
enc_data = encr.update(data) + encr.finalize()
print("Sending encrypted file to ", OTHER)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((IP, MYPORT))
s.connect((msg2[4], int(msg2[5])))
enc_data64 = base64.b64encode(enc_data).decode('utf-8')
sending = enc_data64
s.sendall(bytes(sending, 'utf-8'))
s.close()
print("FINISHED \n Exiting ...")
return
def receiver(args):
counter = 0
for i in range(1, len(args)-1):
if args[i]=='-n':
NAME = args[i+1].ljust(12)
counter += 1
elif args[i]=='-o':
OUTFILE = args[i+1]
counter += 1
elif args[i]=='-s':
OUTENC = args[i+1]
counter+=1
elif args[i]=='-a':
KDCIP = args[i+1]
counter += 1
elif args[i]=='-p':
KDCPORT = int(args[i+1])
counter += 1
if counter != 5:
print("Invalid arguments")
return
#connect to kdc
IV = b'\x00'*16
MYPORT = 10002
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
IP = '127.0.0.1'.ljust(16)
s.bind((IP, MYPORT))
s.connect((KDCIP, KDCPORT))
print("Using port ", MYPORT)
print("Registering with KDC - ", NAME)
# send initial regisitration msg
masterKey = generateRandomString(12)
# msg size = 5 + 16 + 1 + 8+ 1 + 12 + 1 + 12 + 1 = 57 bytes
msg = "|301|"+IP+"|"+str(MYPORT).ljust(8)+"|"+masterKey+"|"+NAME+"|"
s.sendall(bytes(msg, 'utf-8'))
data = s.recv(1024)
data = data.decode('utf-8').split('|')
if(data[1]!= '302'):
print("KDC registration failed")
exit(0)
s.close()
print("Registration successful !!!")
time.sleep(2)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((IP, MYPORT))
s.listen(2)
conn, addr = s.accept()
print("Connection request from ", addr)
data = conn.recv(1024)
msg = data.decode('utf-8')
# print(msg)
opcode = msg[1:4]
if opcode=='309':
parts = msg.split('|')
E_kb64 = parts[2]
E_kb = base64.b64decode(E_kb64)
recv_key = hashlib.md5(bytes(masterKey, 'utf-8')).digest()
cipher = Cipher(algorithms.AES(recv_key), modes.CBC(IV))
decr = cipher.decryptor()
msg3 = decr.update(E_kb) + decr.finalize()
i = len(msg3)-1
while(msg3[i]==0):
i -= 1
msg3 = msg3[0:i+1].decode('utf-8').split('|')
# print(msg3)
conn.close()
unique_session_code = msg3[0]
session_key = hashlib.md5(bytes(unique_session_code, 'utf-8')).digest()
# print(session_key)
print("Received Session key from - ", msg3[1])
# receive the file / message
conn, addr = s.accept()
data = conn.recv(4096)
msg = data.decode('utf-8')
print("Connected to ", addr)
enc_data64 = msg
encF = open(OUTENC, 'w')
encF.write(enc_data64)
encF.close()
print("Writing encrypted message in base64 format to -", OUTENC)
enc_data = base64.b64decode(enc_data64)
# print(enc_data)
cipher = Cipher(algorithms.AES(session_key), modes.CBC(IV))
decr = cipher.decryptor()
data = decr.update(enc_data) + decr.finalize()
i = len(data)-1
while(data[i]==0):
i -= 1
data = data[0:i+1]
# print(data.decode('utf-8'))
outF = open(OUTFILE, 'w')
outF.write(data.decode('utf-8'))
outF.close()
print("Writing decoded message to -", OUTFILE)
conn.close()
s.close()
print("Exiting ...")
return
else:
print("Did not receive code 309 for incoming message \n Exiting ...")
conn.close()
s.close()
exit(0)
return
if __name__=='__main__':
argc = len(sys.argv)
if(argc < 12):
print("Insufficient arguments")
exit(0)
NAME = ""
MODE = ""
for i in range(1, argc-1):
if (sys.argv[i]=='-n'):
NAME = sys.argv[i+1]
elif (sys.argv[i]=='-m'):
MODE = sys.argv[i+1]
if(MODE == 'S'):
sender(sys.argv)
elif(MODE == 'R'):
receiver(sys.argv)
else:
print("invalid MODE")
exit(0)