-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhook.c
284 lines (251 loc) · 7.9 KB
/
hook.c
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
#define O_WRONLY 0x0001
#define O_CREAT 0x0040
#define NULL 0x0
typedef unsigned int mode_t;
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
struct rusage {
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
long ru_maxrss; /* maximum resident set size */
long ru_ixrss; /* integral shared memory size */
long ru_idrss; /* integral unshared data size */
long ru_isrss; /* integral unshared stack size */
long ru_minflt; /* page reclaims */
long ru_majflt; /* page faults */
long ru_nswap; /* swaps */
long ru_inblock; /* block input operations */
long ru_oublock; /* block output operations */
long ru_msgsnd; /* messages sent */
long ru_msgrcv; /* messages received */
long ru_nsignals; /* signals received */
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary " */
};
static int write(int fd, const char *buf, unsigned int len) {
register int ret asm("r0");
register int _fd asm("r0") = fd;
register const char *_buf asm("r1") = buf;
register unsigned int _len asm("r2") = len;
register int sys_write asm("r7") = 4;
asm volatile("svc #0"
: "=r"(ret)
: "r"(_fd), "r"(_buf), "r"(_len), "r"(sys_write)
:);
return ret;
}
static int exit(int code) {
register int ret asm("r0");
register int _code asm("r0") = code;
register int sys_exit asm("r7") = 1;
asm volatile("svc #0" : "=r"(ret) : "r"(_code), "r"(sys_exit) :);
return ret;
}
static int execve(const char *filename, char *const argv[],
char *const envp[]) {
register int ret asm("r0");
register const char *_filename asm("r0") = filename;
register char *const *_argv asm("r1") = argv;
register char *const *_envp asm("r2") = envp;
register int sys_execve asm("r7") = 11;
asm volatile("svc #0"
: "=r"(ret)
: "r"(_filename), "r"(_argv), "r"(_envp), "r"(sys_execve)
:);
return ret;
}
static int fork(void) {
register int ret asm("r0");
register int sys_fork asm("r7") = 2;
asm volatile("svc #0" : "=r"(ret) : "r"(sys_fork) :);
return ret;
}
static int wait4(int pid, int *status, int options, struct rusage *rusage) {
register int ret asm("r0");
register int _pid asm("r0") = pid;
register int *_status asm("r1") = status;
register int _options asm("r2") = options;
register struct rusage *_rusage asm("r3") = rusage;
register int sys_wait4 asm("r7") = 114;
asm volatile("svc #0"
: "=r"(ret)
: "r"(_pid), "r"(_status), "r"(_options), "r"(_rusage),
"r"(sys_wait4)
:);
return ret;
}
static int open(const char *path, int flags, mode_t mode) {
register int ret asm("r0");
register const char *_path asm("r0") = path;
register int _flags asm("r1") = flags;
register mode_t _mode asm("r2") = mode;
register int sys_open asm("r7") = 5;
asm volatile("svc #0"
: "=r"(ret)
: "r"(_path), "r"(_flags), "r"(_mode), "r"(sys_open)
:);
return ret;
}
static int close(int fd) {
register int ret asm("r0");
register int _fd asm("r0") = fd;
register int sys_close asm("r7") = 6;
asm volatile("svc #0" : "=r"(ret) : "r"(_fd), "r"(sys_close) :);
return ret;
}
static int access(const char *pathname, int mode) {
register int ret asm("r0");
register const char *_pathname asm("r0") = pathname;
register int _mode asm("r1") = mode;
register int sys_access asm("r7") = 33;
asm volatile("svc #0"
: "=r"(ret)
: "r"(_pathname), "r"(_mode), "r"(sys_access)
:);
return ret;
}
static int unlink(const char *pathname) {
register int ret asm("r0");
register const char *_pathname asm("r0") = pathname;
register int sys_unlink asm("r7") = 10;
asm volatile("svc #0" : "=r"(ret) : "r"(_pathname), "r"(sys_unlink) :);
return ret;
}
static int setuid(int uid) {
register int ret asm("r0");
register int _uid asm("r0") = uid;
register int sys_setuid asm("r7") = 23;
asm volatile("svc #0" : "=r"(ret) : "r"(_uid), "r"(sys_setuid) :);
return ret;
}
static int setgid(int gid) {
register int ret asm("r0");
register int _gid asm("r0") = gid;
register int sys_setgid asm("r7") = 46;
asm volatile("svc #0" : "=r"(ret) : "r"(_gid), "r"(sys_setgid) :);
return ret;
}
typedef enum { false, true } bool;
void swap(char *a, char *b) {
if (!a || !b) return;
char temp = *(a);
*(a) = *(b);
*(b) = temp;
}
void reverse(char *str, int length) {
int start = 0;
int end = length - 1;
while (start < end) {
swap((str + start), (str + end));
start++;
end--;
}
}
static char *itoa(int num, char *str, int base) {
int i = 0;
bool isNegative = false;
if (num == 0) {
str[i++] = '0';
str[i] = '\0';
return str;
}
if (num < 0 && base == 10) {
isNegative = true;
num = -num;
}
while (num != 0) {
int rem = num % base;
str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
num = num / base;
}
if (isNegative == true) str[i++] = '-';
str[i] = '\0';
reverse(str, i);
return str;
}
void hooked_func() {
int log_fp, lock_fp;
// Paths
//@textify
char log_filename[] = "/storage/usb0/XXlog.txt";
//@textify
char script_path[] = "/storage/usb0/run.sh";
//@textify
char lock_file[] = "/storage/usb0/XXlock";
//@textify
char sh_path[] = "/system/bin/sh";
// Other
//@textify
char version[] = "v0.0.1-beta-2\n";
//@textify
char sanity_value[] = "Hello!\n";
//@textify
char space[] = " ";
//@textify
char sanity_failed[] = "Sanity check failed\n";
//@textify
char run_sh[] = "Going to launch usb0/run.sh\n";
//@textify
char pid_info[] = "PID: ";
//@textify
char status_info[] = "Status: ";
//@textify
char sh[] = "sh";
// Check if lock file exists
if (access(lock_file, 0) == 0) {
return;
}
// Create lock file
lock_fp = open(lock_file, O_WRONLY | O_CREAT, 0777);
if (lock_fp < 0) {
char error_str[15];
itoa(lock_fp, error_str, 10);
write(1, error_str, sizeof(error_str) - 1);
write(1, space, 2);
write(1, sanity_failed, 21);
return;
}
close(lock_fp);
log_fp = open(log_filename, O_WRONLY | O_CREAT, 0777);
if (log_fp < 0) {
char error_str[15];
itoa(log_fp, error_str, 10);
write(1, error_str, sizeof(error_str) - 1);
write(1, space, 2);
write(1, sanity_failed, 21);
return;
}
write(log_fp, sanity_value, sizeof(sanity_value) - 1);
write(log_fp, version, sizeof(version) - 1);
int pid = fork();
if (pid == 0) {
// Execute run.sh
if (access(script_path, 0) == 0) {
write(log_fp, run_sh, sizeof(run_sh) - 1);
char *argv[] = {sh, script_path, NULL};
execve(sh_path, argv, NULL);
exit(0);
} else {
write(log_fp, space, 2);
write(log_fp, sanity_failed, sizeof(sanity_failed) - 1);
exit(1);
}
}
int status;
write(log_fp, pid_info, sizeof(pid_info) - 1);
char pid_str[15];
itoa(pid, pid_str, 10);
write(log_fp, pid_str, sizeof(pid_str) - 1);
// Wait for run.sh to finish
wait4(pid, &status, 0, NULL);
char status_str[15];
itoa(status, status_str, 10);
write(log_fp, space, 2);
write(log_fp, status_info, sizeof(status_info) - 1);
write(log_fp, status_str, sizeof(status_str) - 1);
LOG_END:
close(log_fp);
return;
}