From 365a56438445058aff33747f41ac7f0f504940a4 Mon Sep 17 00:00:00 2001 From: Kkevsterrr Date: Mon, 5 Dec 2016 23:43:26 -0500 Subject: [PATCH] works with the app --- client.py | 15 ++++++++------- server.py | 29 +++++++++++++++++++---------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/client.py b/client.py index f9d2042..5562c92 100644 --- a/client.py +++ b/client.py @@ -26,7 +26,7 @@ ERR_MSG = BAD + "Lost server connection. Please try again later." SEP = "|:|" -END_SEP = "!:!" +END_SEP = "~" #################################################################### # @@ -70,16 +70,17 @@ def __init__(self, name, target_port, server_ip): def connect(self): client = None try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - client = ssl.wrap_socket(s, ca_certs="cert/server.crt", cert_reqs=ssl.CERT_REQUIRED) + #s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + #s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + #client = ssl.wrap_socket(s, ca_certs="cert/server.crt", cert_reqs=ssl.CERT_REQUIRED) + client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((self.ip, self.port)) self.socket = client shared_prime, shared_base = client.recv(10).split("|") shared_prime = int(shared_prime) - shared_base = int(shared_base) + shared_base = int(shared_base.replace("~", "")) client_secret = random.randint(0, 99) - a = long(client.recv(1024)) + a = long(client.recv(1024).replace("~", "")) b = (shared_base**client_secret) % shared_prime client.send("%ld" % b) self.keys[client] = pad("%ld" % ((a ** client_secret) % shared_prime)) @@ -141,7 +142,7 @@ def cli_communicate(self, data): def send(self, message_type, requester, data): #print "SENDING: <%d, %s>" %(message_type, data) - self.socket.send(self.encrypt(self.pack_data(message_type, requester, data), self.socket)) + self.socket.send(self.encrypt(self.pack_data(message_type, requester, data), self.socket) + "\n") def encrypt(self, string, sock): iv = Random.new().read(AES.block_size) diff --git a/server.py b/server.py index 0df748f..640fa2f 100644 --- a/server.py +++ b/server.py @@ -25,7 +25,7 @@ unpad = lambda s : s[0:-ord(s[-1])] SEP = "|:|" -END_SEP = "!:!" +END_SEP = "~" MSG = 0 NEWC = 1 @@ -72,6 +72,7 @@ def __init__(self, name, port): def server(self): try: try: + self.bind_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.bind_socket.bind(("0.0.0.0", self.port)) except socket.error: print("[!] %s startup failed - can't bind to %s:%d" % (self.name, "0.0.0.0", self.port)) @@ -81,8 +82,8 @@ def server(self): sys.stdout.flush() while True: try: - client_sock, addr = self.bind_socket.accept() - client = ssl.wrap_socket(client_sock, server_side=True, certfile="cert/server.crt", keyfile="cert/server.key") + client, addr = self.bind_socket.accept() + #client = ssl.wrap_socket(client_sock, server_side=True, certfile="cert/server.crt", keyfile="cert/server.key") cid = random.randint(0, 99999) self.ips[cid] = addr[0] @@ -119,13 +120,18 @@ def handle_client(self, c, cid): shared_prime = number.getPrime(10) shared_base = number.getPrime(10) server_secret = random.randint(0, 99) - c.send(str(shared_prime) + "|" + str(shared_base)) + c.send(str(shared_prime) + "|" + str(shared_base) + "~") a = ((shared_base**server_secret) % shared_prime) - c.send("%ld" % a) # send A + print "sending %s to client" %( str(shared_prime) + "|" + str(shared_base)) + c.send("%ld~" % a) # send A b = long(c.recv(1024)) # receive B + print "got %ld from client" % b self.keys[c] = pad("%ld" % ((b ** server_secret) % shared_prime)) - - _, name, name = self.unpack_data(self.decrypt(c.recv(1024), c)) + print self.keys[c] + n = c.recv(1024) + print n + print self.decrypt(n, c) + _, name, name = self.unpack_data(self.decrypt(n, c)) name = name.replace(END_SEP, "").replace(SEP, "") print("(%s)" % name) self.ids[cid] = name @@ -133,20 +139,23 @@ def handle_client(self, c, cid): if name == "PinaColada": self.pi = c app.config["server"] = self - print id(app) - print self print "[*] Pina Colada has connected." else: + print '[*] Tunnel initialized for user %s' % name self.tunnels[cid] = c except Exception as e: self.print_exc(e, "\n[!] Failed to initialize client connection for %d." % id, always=True) self.close(cid) + traceback.print_exc() return False try: while True: d = c.recv(1024) + print d + print self.decrypt(d, c) msgs = filter(None, self.decrypt(d, c).split(END_SEP)) + print msgs for m in msgs: self.inbound(m, c) #print d @@ -257,7 +266,7 @@ def decrypt(self, msg, c): return unpad(cipher.decrypt(enc[16:])) def direct(self, msg_type, requester, c, msg): - c.send(self.encrypt(self.pack_data(msg_type, requester, msg), c)) + c.send(self.encrypt(self.pack_data(msg_type, requester, msg), c) + "\n") def unpack_data(self, msg): msgs = [self.replace_seps(s) for s in msg.split(SEP)]