-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathclient.py
205 lines (174 loc) · 6.5 KB
/
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
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
import socket
import threading
import sys
import os
import base64
import random
import ssl
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 = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-ord(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.core = core.PinaColada()
self.keys = {}
self.conn = None
self.cli = None
print "[*] Attempting to connect to server"
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)
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.replace("~", ""))
client_secret = random.randint(0, 99)
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))
client.settimeout(None) # Remove the timeout
self.send(MSG, "0", "PinaColada")
print("[*] Successfully connected.")
self.receive(client)
except Exception as e:
print e
print traceback.print_exc()
raw_input("")
finally:
self.shutdown()
def shutdown(self):
print GOOD + "Exiting..."
####################################################################
#
# Message Handling
#
####################################################################
def get(self, request):
reqs = request.split()
if request == "help":
return "help message" # TODO
if request == "categories":
return self.core.get_categories()
if "capabilities" in request:
return self.core.get_capabilities(category=reqs[1]) # TODO POTENTIAL BUG
if request == "ip":
return self.core.get_local_ip(self.core.default_iface) # TODO ADD INTERFACES
if request == "list":
info = {}
for cat in self.core.get_categories():
caps = self.core.get_capabilities(cat)
if caps != []:
info[cat] = []
for cap in caps:
info[cat].append(cap)
return info
def cli_init(self):
if self.cli:
self.cli.terminate(force=True)
self.cli = pexpect.spawn("sudo python cli.py")
self.cli.expect(re.escape(prompt))
return self.cli.before + prompt
def cli_communicate(self, data):
self.cli.sendline(data)
self.cli.expect(re.escape(prompt))
return "\n".join(self.cli.before.split("\n")[1:]) + prompt
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) + "\n")
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, d = self.unpack_data(data)
print "DATA FOR CLI: %s" % d
message_type = int(message_type)
print message_type, name, d
if message_type == CLI_INIT:
self.send(CLI_RESP, name, self.cli_init())
if message_type == CLI:
self.send(CLI_RESP, name, self.cli_communicate(d))
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__":
if os.getuid() != 0:
print BAD + "Please run me as root!"
sys.exit()
PinaColadaSocket("Client", SERVER_PORT, SERVER_IP).connect()