Skip to content

Commit

Permalink
works with the app
Browse files Browse the repository at this point in the history
  • Loading branch information
Kkevsterrr committed Dec 6, 2016
1 parent f7972c6 commit 365a564
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
15 changes: 8 additions & 7 deletions client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

ERR_MSG = BAD + "Lost server connection. Please try again later."
SEP = "|:|"
END_SEP = "!:!"
END_SEP = "~"

####################################################################
#
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 19 additions & 10 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
unpad = lambda s : s[0:-ord(s[-1])]

SEP = "|:|"
END_SEP = "!:!"
END_SEP = "~"

MSG = 0
NEWC = 1
Expand Down Expand Up @@ -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))
Expand All @@ -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]
Expand Down Expand Up @@ -119,34 +120,42 @@ 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
self.clients[cid] = c
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
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 365a564

Please sign in to comment.