-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCryptoPingServer.py
136 lines (91 loc) · 2.93 KB
/
CryptoPingServer.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
import os, re, socket, threading, struct, sys, string
from ctypes import *
from urllib import urlopen
from subprocess import call
import os.path
import time
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto import Random
hopCount = 0
hostname = "hostname"
def read_file(filename):
f = open(filename,'r')
data = f.read()
return data
def create_file(filename, data):
f = open(filename,'w')
f.write(data)
f.close()
return
def recv_data(sock):
a = sock.recv(4)
data_len = struct.unpack('!I',a)[0]
data = sock.recv(data_len)
return data
def send_data(sock,data):
data_len = struct.pack('!I',len(data))
sock.send(data_len)
sock.send(data)
return
def send_file_contents(file_name,usersock,userinfo): #DOWNLOAD
# This was so insecure. The client could have requested the private key or whatever file!
# data = read_file(file_name)
data = read_file("%s-pub.key" % (hostname))
send_data(usersock,data)
return 0
def receive_file_contents(file_name,usersock):#UPLOAD
data = recv_data(usersock)
create_file(file_name,data)
return 0
def handle_connection(usersock,userinfo):
global hopCount
the_order = recv_data(usersock).upper()
if(the_order == "GETPUB"):
send_file_contents(recv_data(usersock),usersock,userinfo)
nonce = recv_data(usersock)
print "Got nonce", nonce
pri = open("%s-pri.key" % (hostname),"r")
key = RSA.importKey(pri)
hash = SHA256.new(nonce).digest()
signature = key.sign(hash, '')
signature = str(signature)
send_data(usersock,signature)
return
def getIP(hostname):
AS, idx = hostname.replace('h', '').split('-')
AS = int(AS)
if AS == 4:
AS = 3
ip = '%s.0.%s.1' % (10+AS, idx)
return ip
# Arg 1 : my IP -> ip to bind the local socket
# Arg 2 : my port -> port to bind the local socket
def main():
global hopCount
global hostname
print "Getting ready for Cryptographic ping ..."
hostname = sys.argv[1]
my_ip = getIP(hostname)
my_port = int(sys.argv[2])
if(not(os.path.isfile("%s-pub.key" % (hostname)))):
random_generator = Random.new().read
key = RSA.generate(1024,random_generator)
privateHandle = open("%s-pri.key" % (hostname),'wb')
privateHandle.write(key.exportKey())
public_key = key.publickey()
pubHandle = open("%s-pub.key" % (hostname), 'wb')
pubHandle.write(public_key.exportKey())
pubHandle.close()
privateHandle.close()
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print "Binding at " + my_ip + ":" + str(my_port)
sock.bind((my_ip,my_port))
print "I'm listening now ..."
while(1):
sock.listen(0)
conn, addr = sock.accept()
print "New Client: " + str(addr)
threading.Thread(None,handle_connection,None,(conn,addr)).start()
return
main()