-
Notifications
You must be signed in to change notification settings - Fork 34
/
test_client.py
165 lines (140 loc) · 5.02 KB
/
test_client.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
import socket
import threading
import sys
import os
import base64
import random
import json
import traceback
import core
import pexpect
import re
from colorama import *
from Crypto.Cipher import AES
from Crypto import Random
from datetime import datetime
GOOD = Fore.GREEN + " + " + Fore.RESET
BAD = Fore.RED + " - " + Fore.RESET
WARN = Fore.YELLOW + " * " + Fore.RESET
INFO = Fore.BLUE + " + " + Fore.RESET
prompt = Fore.BLUE + ">> " + Fore.RESET
SERVER_PORT = 9999
SERVER_IP = "127.0.0.1"
ERR_MSG = BAD + "Lost server connection. Please try again later."
SEP = "|:|"
END_SEP = "!:!"
####################################################################
#
# Message Types
#
####################################################################
MSG = 0
GET = 1
CLOSE = 2
INFO = 3
REQ = 4
CLI_INIT = 10
CLI = 11
CLI_RESP = 12
BS = AES.block_size
print BS
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s: s[:-ord(s[len(s)-1:])]
replace_seps = lambda m: m.replace("|::|", SEP).replace("!::!", END_SEP)
save_seps = lambda m: m.replace(SEP, "|::|").replace(END_SEP, "!::!")
def get_date():
return "%02d:%02d:%02d" % (datetime.now().hour, datetime.now().minute, datetime.now().second)
class PinaColadaSocket(object):
def __init__(self, name, target_port, server_ip):
self.port = target_port
self.ip = server_ip
self.name = name
self.socket = None
self.keys = {}
print "[*] Attempting to connect to server"
def connect(self):
client = None
try:
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)
client_secret = random.randint(0, 99)
a = long(client.recv(1024))
b = (shared_base**client_secret) % shared_prime
client.send("%ld" % b)
self.keys[client] = pad("%ld" % ((a ** client_secret) % shared_prime))
client.settimeout(None) # Remove the timeout
self.send(MSG, "Client1")
print("[*] Successfully connected.")
receive_loop = threading.Thread(target=self.receive, args=(client,))
receive_loop.start()
self.send(CLI_INIT, "cli init")
while True:
line = raw_input(">> ")
self.send(CLI, line)
except Exception as e:
print e
print traceback.print_exc()
raw_input("")
finally:
self.shutdown()
def shutdown(self):
print GOOD + "Exiting..."
####################################################################
#
# Message Handling
#
####################################################################
def send(self, message_type, data):
#print "SENDING: <%d, %s>" %(message_type, data)
self.socket.send(self.encrypt(self.pack_data(message_type, self.name, data), self.socket))
def encrypt(self, string, sock):
iv = Random.new().read(AES.block_size)
cipher = AES.new(self.keys[sock], AES.MODE_CBC, iv)
return base64.b64encode(iv + cipher.encrypt(pad(string)))
def decrypt(self, msg, sock):
enc = base64.b64decode(msg)
cipher = AES.new(self.keys[sock], AES.MODE_CBC, enc[:16])
return unpad(cipher.decrypt(enc[16:]))
def unpack_data(self, msg):
msgs = [replace_seps(s) for s in msg.split(SEP)]
try:
msgs[0] = int(msgs[0]) # convert type to int
finally:
return msgs
def pack_data(self, message_type, name, data):
return str(message_type) + SEP + save_seps(name) + SEP + save_seps(data) + END_SEP
def print_msg(self, message):
print message
def handle(self, data):
message_type, name, data = self.unpack_data(data)
message_type = int(message_type)
if message_type == CLI_INIT:
self.send(CLI_RESP, self.cli_init())
if message_type == CLI:
self.send(CLI_RESP, self.cli_communicate(data))
self.print_msg(data)
####################################################################
#
# Receive Handling
#
####################################################################
def receive(self, sock):
try:
while True:
data = sock.recv(1024)
msgs = filter(None, self.decrypt(data, sock).split(END_SEP))
for m in msgs:
self.handle(m)
except Exception as e:
print e
traceback.print_exc()
sock.close()
finally:
sock.close()
self.shutdown()
if __name__ == "__main__":
PinaColadaSocket("Client", SERVER_PORT, SERVER_IP).connect()