-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine-persistence.py
More file actions
278 lines (221 loc) · 8.92 KB
/
Copy pathengine-persistence.py
File metadata and controls
278 lines (221 loc) · 8.92 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
"""
SIHAI v5.0 — Persistence Manager
All persistence methods for Android
"""
import os
import re
import sys
import json
import stat
import pwd
import grp
import asyncio
import logging
import shutil
import subprocess
from typing import Dict, List, Optional
from pathlib import Path
from datetime import datetime
logger = logging.getLogger('SIHAI.Persistence')
class PersistenceManager:
"""Install persistence using all available methods"""
def __init__(self):
self.methods = {
'termux_boot': self._install_termux_boot,
'init_d': self._install_init_d,
'rc_local': self._install_rc_local,
'cron': self._install_cron,
'service': self._install_service,
'module': self._install_module,
'magisk': self._install_magisk_module,
'selinux_policy': self._install_selinux_policy,
'property': self._install_property,
'boot_completed': self._install_boot_receiver
}
self.installed_methods = []
async def initialize(self):
"""Initialize persistence manager"""
logger.info("Persistence manager initialized")
return True
async def install_all(self) -> Dict:
"""Install all persistence methods"""
results = {}
for method_name, method_func in self.methods.items():
try:
result = await method_func()
if result:
self.installed_methods.append(method_name)
results[method_name] = {'success': True}
logger.info(f"Persistence installed: {method_name}")
except Exception as e:
results[method_name] = {'success': False, 'error': str(e)}
logger.debug(f"Persistence {method_name} failed: {e}")
return results
async def _install_termux_boot(self) -> bool:
"""Install via Termux:Boot"""
boot_dir = Path.home() / '.termux' / 'boot'
boot_dir.mkdir(parents=True, exist_ok=True)
script = boot_dir / 'sihai.sh'
script.write_text(f"""#!/data/data/com.termux/files/usr/bin/bash
# SIHAI Auto-start
cd {os.getcwd()}
python engine/core.py --god-mode --daemon &
""")
script.chmod(0o755)
return script.exists()
async def _install_init_d(self) -> bool:
"""Install via /etc/init.d (requires root)"""
if os.geteuid() != 0:
return False
init_script = """#!/system/bin/sh
# SIHAI Persistence
start-stop-daemon -S -b -x /data/data/com.termux/files/usr/bin/python \\
-- /data/data/com.termux/files/home/SIHAI-Termux/engine/core.py --god-mode --daemon
"""
with open('/etc/init.d/sihai', 'w') as f:
f.write(init_script)
os.chmod('/etc/init.d/sihai', 0o755)
# Add to default runlevels
for level in ['2', '3', '4', '5']:
rc_path = f'/etc/rc{level}.d/S99sihai'
os.symlink('/etc/init.d/sihai', rc_path)
return True
async def _install_rc_local(self) -> bool:
"""Install via /etc/rc.local (requires root)"""
if os.geteuid() != 0:
return False
rc_local = '/etc/rc.local'
entry = f'\n# SIHAI Persistence\nstart-stop-daemon -S -b -x /data/data/com.termux/files/usr/bin/python -- /data/data/com.termux/files/home/SIHAI-Termux/engine/core.py --god-mode --daemon &\n'
if os.path.exists(rc_local):
with open(rc_local, 'a') as f:
f.write(entry)
else:
with open(rc_local, 'w') as f:
f.write('#!/system/bin/sh\n' + entry)
os.chmod(rc_local, 0o755)
return True
async def _install_cron(self) -> bool:
"""Install via cron job"""
cron_dir = Path.home() / '.termux' / 'cron'
cron_dir.mkdir(parents=True, exist_ok=True)
# Add crontab entry
cron_entry = f"@reboot cd {os.getcwd()} && python engine/core.py --god-mode --daemon &\n"
crontab = cron_dir / 'root'
with open(crontab, 'a') as f:
f.write(cron_entry)
# Start cron daemon
subprocess.run(['crond'], capture_output=True)
return True
async def _install_service(self) -> bool:
"""Install as system service (requires root)"""
if os.geteuid() != 0:
return False
# Create service file for Termux services
service_dir = Path('/data/data/com.termux/files/usr/var/service')
service_dir.mkdir(parents=True, exist_ok=True)
service_file = service_dir / 'sihai' / 'run'
service_file.parent.mkdir(parents=True, exist_ok=True)
service_file.write_text(f"""#!/data/data/com.termux/files/usr/bin/bash
exec python {os.getcwd()}/engine/core.py --god-mode --daemon
""")
service_file.chmod(0o755)
# Enable service
subprocess.run(['sv', 'enable', 'sihai'], capture_output=True)
subprocess.run(['sv', 'start', 'sihai'], capture_output=True)
return True
async def _install_module(self) -> bool:
"""Install as kernel module (requires root)"""
if os.geteuid() != 0:
return False
# Copy eBPF program
ebpf_src = 'ebpf/sihai_kern.o'
if os.path.exists(ebpf_src):
shutil.copy(ebpf_src, '/lib/modules/sihai.ko')
# Load module at boot
with open('/etc/modules', 'a') as f:
f.write('sihai\n')
return True
async def _install_magisk_module(self) -> bool:
"""Install as Magisk module (requires root + Magisk)"""
if os.geteuid() != 0:
return False
module_dir = Path('/data/adb/modules/sihai')
module_dir.mkdir(parents=True, exist_ok=True)
# Create module.prop
(module_dir / 'module.prop').write_text("""id=sihai
name=SIHAI Engine
version=v5.0
versionCode=500
author=SIHAI
description=SIHAI v5.0 God Mode Engine - Persistence Module
""")
# Create post-fs-data script
post_fs = module_dir / 'post-fs-data.sh'
post_fs.write_text(f"""#!/system/bin/sh
# SIHAI post-fs-data
/data/data/com.termux/files/usr/bin/python {os.getcwd()}/engine/core.py --god-mode --daemon &
""")
post_fs.chmod(0o755)
# Create service script
service_sh = module_dir / 'service.sh'
service_sh.write_text(f"""#!/system/bin/sh
# SIHAI service
/data/data/com.termux/files/usr/bin/python {os.getcwd()}/engine/core.py --god-mode --daemon &
""")
service_sh.chmod(0o755)
# Set SELinux context
subprocess.run(['chcon', '-R', 'u:object_r:system_file:s0', str(module_dir)],
capture_output=True)
return True
async def _install_selinux_policy(self) -> bool:
"""Install SELinux policy for persistence (requires root)"""
if os.geteuid() != 0:
return False
# Create SELinux policy module
policy = """
module sihai 1.0;
require {
type init_t;
type shell_exec;
type proc_net_t;
class file { read write execute };
class process { fork execmem };
}
allow init_t shell_exec:file { read execute };
allow init_t proc_net_t:file { read write };
allow init_t self:process { fork execmem };
"""
with open('/tmp/sihai.te', 'w') as f:
f.write(policy)
# Compile and load policy
subprocess.run(['checkmodule', '-M', '-m', '/tmp/sihai.te', '-o', '/tmp/sihai.mod'],
capture_output=True)
subprocess.run(['semodule_package', '-m', '/tmp/sihai.mod', '-o', '/tmp/sihai.pp'],
capture_output=True)
subprocess.run(['semodule', '-i', '/tmp/sihai.pp'], capture_output=True)
return True
async def _install_property(self) -> bool:
"""Install via system property (requires root)"""
if os.geteuid() != 0:
return False
# Set system property for auto-start
subprocess.run(['setprop', 'persist.sys.sihai.enabled', '1'], capture_output=True)
subprocess.run(['setprop', 'ctl.start', 'sihai'], capture_output=True)
return True
async def _install_boot_receiver(self) -> bool:
"""Install via boot completed broadcast receiver"""
# Create a simple script that runs on boot
boot_script = Path.home() / '.bashrc'
entry = f"""
# SIHAI Auto-start
if [ -z "$SIHAI_RUNNING" ]; then
export SIHAI_RUNNING=1
cd {os.getcwd()} && python engine/core.py --god-mode --daemon &
fi
"""
with open(boot_script, 'a') as f:
f.write(entry)
return True
async def shutdown(self):
"""Cleanup persistence"""
logger.info("Persistence manager shutdown")