-
Notifications
You must be signed in to change notification settings - Fork 0
/
nomal_req_node.py
300 lines (246 loc) · 8.67 KB
/
nomal_req_node.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
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
# !/usr/bin/env python3
# coding:utf-8
import sys
import os
import git
from sys import argv
from socket import *
import socket as Socket
from uuid import uuid4
from fractions import gcd
import random
HOST = "0.0.0.0"
NOMAL_PORT = 33844
VALID_PORT = 33845
# For git
URL = 'git@github.com:ertlnagoya/Update_Test.git'
DIRECTORY = 'repo'
# For test
VER = "2"
HASH = "f52d885484f1215ea500a805a86ff443"
FILE_NAME = 'Update_Test'
METADATA = FILE_NAME + ";" + HASH + ";" + "len" + ";" + HOST
# "file_name+file_hash+piece_length+valid_node_URL"
DOWNLOAD = URL + ";" + HASH # "file_URL+file_hash+len"
# Generate a globally unique address for this
sender = str(uuid4()).replace('-', '')
def git_clone():
_repo_path = os.path.join('./', DIRECTORY)
# clone from remote
git_repo = git.Repo.clone_from(
URL, _repo_path, branch='master')
def git_pull():
repo = git.Repo(DIRECTORY)
o = repo.remotes.origin
o.pull()
print(o)
def recv_until(c, delim="\n"):
res = c.recv(1024)
if len(res) == 0:
return ""
while not res[-1] == delim:
data = c.recv(1024)
if len(data) == 0:
return res
res += data
return res
def lcm(p, q):
return (p * q) // gcd(p, q)
def generate_keys(p, q):
N = p * q
L = lcm(p - 1, q - 1)
for i in range(2, L):
if gcd(i, L) == 1:
E = i
break
for i in range(2, L):
if (E * i) % L == 1:
D = i
break
return (E, N), (D, N)
def encrypt(plain_text, public_key):
E, N = public_key
plain_integers = [ord(char) for char in plain_text]
encrypted_integers = [i ** E % N for i in plain_integers]
encrypted_text = ''.join(chr(i) for i in encrypted_integers)
return encrypted_text
def decrypt(encrypted_text, private_key):
D, N = private_key
encrypted_integers = [ord(char) for char in encrypted_text]
decrypted_intergers = [i ** D % N for i in encrypted_integers]
decrypted_text = ''.join(chr(i) for i in decrypted_intergers)
return decrypted_text
def tuple_key(payload):
'''
return public_key from payload
'''
data = []
key = []
data = payload.split("-")
data[0] = data[0].replace('(', '')
data[0] = data[0].replace(')', '')
key = data[0].split(",")
key[0] = int(key[0])
key[1] = int(key[1])
return tuple(key)
def randam(payload, r_before):
'''
return randam nuber from payload
'''
data = []
data = payload.split("-")
r = int(data[4])
if (r_before + 2 - r) != 0:
print("Error: Rundam nuber. It may be Reply Attack!!", r, r_before)
# print(r)
return r + 1
def make_payload(public_key, sender, NODE, INFO, r):
payload = (str(public_key) + '-' + sender + '-' + NODE +
'-' + INFO + '-' + str(r))
return payload
def client(HOST, public_key, private_key):
# Randam number
r = random.randrange(1000)
# conection
soc = socket(AF_INET)
soc.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
soc.connect((HOST, NOMAL_PORT))
print("[*] connecting to %s:%s" % (HOST, NOMAL_PORT))
# soc.connect((HOST, VALID_PORT))
# print("[*] connecting to %s:%s" % (HOST, VALID_PORT))
# verbose_ping(sys.argv[12)
# req_vercheck c1-1-1
payload = make_payload(public_key, sender, "nomalnode", VER, r)
soc.sendall(payload.encode("UTF-8"))
print("[*] c1-1-1:send", payload)
# Generates verifier H(fv) after checking res_verchk message c1-1-4
payload = soc.recv(1024)
payload = payload.decode("UTF-8")
payload = decrypt(payload, private_key)
print("[*] payload decode & decrypt: c1-1-4", payload)
public_server_key = tuple_key(payload)
# print("[*] public_server_key", public_server_key)
data = payload.split("-")
r = randam(payload, r - 1)
comp = int(data[3])
if str(data[2]) == "validnode":
print("[*] Server is valid node")
if int(VER) == comp:
# req_verification c1-1-5
print("[*] Version check: req = res!")
payload = make_payload(public_key, sender, "nomalnode", HASH, r)
payload = encrypt(payload, tuple(public_server_key))
# print(payload)
payload = payload.encode("UTF-8")
soc.sendall(payload)
print("[*] c1-1-5:send", data)
# Verifies and decrypts res_verification message,
# and compares H(fv) and H(fvnew c1-1-8
payload = soc.recv(1024)
payload = payload.decode("UTF-8")
payload = decrypt(payload, private_key)
print("[*] Reception: " + str(payload))
data = payload.split("-")
# print("c1-1-8: " + data[3])
if str(HASH) == str(data[3]):
print("[*] SAME!!")
else:
print("[*] Download start!")
git_pull()
else:
print("[*] It is not latest! Download start!")
# req_download c1-2-5
r = randam(payload, r)
payload = make_payload(public_key, sender, "nomalnode", 'Download', r)
payload = encrypt(payload, tuple(public_server_key))
# print(payload)
payload = payload.encode("UTF-8")
soc.sendall(payload)
# Downloads and installs the latest firmware file
# after checking res_download message c1-2-8
payload = soc.recv(1024)
payload = payload.decode("UTF-8")
payload = decrypt(payload, private_key)
print("[*] Reception: " + str(payload))
data = payload.split("-")
soc.close()
git_pull()
if str(data[2]) == "nomalnode":
print("[*] Server is nomal node")
if int(VER) == comp:
print("[*] Version check: req = res!")
# c2-2-4
payload = make_payload(public_key, sender, "nomalnode", HASH, r)
print("[*] c2-2-4:send", payload)
payload = encrypt(payload, tuple(public_server_key))
# print(payload)
payload = payload.encode("UTF-8")
soc.sendall(payload)
payload = soc.recv(1024)
payload = payload.decode("UTF-8")
payload = decrypt(payload, private_key)
print("[*] Reception: " + str(payload))
data = payload.split("-")
if str(HASH) == str(data[3]):
print("[*] SAME!!")
else:
print("[*] Download start!")
git_pull()
if int(VER) < comp:
print("[*] Version check: req < res!")
# req_metadata c2-3-5
soc.close()
soc = socket(AF_INET)
soc.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
soc.connect((HOST, VALID_PORT))
print("[*] connecting to %s:%s" % (HOST, VALID_PORT))
payload = make_payload(public_key, sender, 'req_metadata', 'metadata', r)
print("[*] c2-3-5:send", payload)
soc.sendall(payload.encode("UTF-8"))
# Decrypts res_metadata message and obtains H(fvnew) from Mvnew c2-3-8
payload = soc.recv(1024)
payload = payload.decode("UTF-8")
payload = decrypt(payload, private_key)
print("[*] Reception: c2-3-8 ", payload)
public_server_key = tuple_key(payload)
# res_download c2-3-9
r = randam(payload, r - 1)
payload = make_payload(public_key, sender, 'req_metadata', 'Download', r)
print("[*] c2-3-9:send", payload)
payload = encrypt(payload, tuple(public_server_key))
payload = payload.encode("UTF-8")
soc.sendall(payload)
git_pull()
if int(VER) > comp:
print("[*] Version check: req > res!")
# notice_download c2-1-5
payload = make_payload(public_key, sender, 'omalnode', 'notice', r)
print("[*] c2-1-5:send", payload)
payload = encrypt(payload, tuple(public_server_key))
payload = payload.encode("UTF-8")
soc.sendall(payload)
soc.close()
print("[*] Finish!!")
if __name__ == '__main__':
data = []
if len(sys.argv) == 2:
HOST = argv[1]
else:
if len(sys.argv) == 3:
NOMAL_PORT = int(argv[2])
print("[*] Port: ", NOMAL_PORT)
else:
print("[*] Default port:", NOMAL_PORT)
# sys.exit()
print("Error: ")
sys.exit()
if os.path.isdir("./repo"):
print("[*] already exist.")
else:
print("[*] make repo")
git_clone()
# RSA: generate
public_key, private_key = generate_keys(107, 3259)
print(public_key)
print(private_key)
client(HOST, public_key, private_key)