-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.c
356 lines (278 loc) · 7.82 KB
/
main.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/unistd.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/fs.h>
#include <linux/fdtable.h>
#include <linux/cred.h>
#include <linux/dirent.h>
#include <linux/syscalls.h>
#include <linux/kprobes.h>
#include <linux/kallsyms.h>
#include <asm/uaccess.h>
#include <asm/processor.h>
#include "mmuhack.h"
#include "dumpcode.h"
//Custom defs
/*
#define __NR_open 5
#define __NR_access 33
#define __NR_stat64 195
#define __NR_lstat64 196
#define __NR_fstat64 197
*/
#ifndef __NR_syscalls
//TODO: find way to get rid of this.
#define __NR_syscalls 500
#endif
#ifndef NR_syscalls
#define NR_syscalls __NR_syscalls
#endif
unsigned long **sys_call_table;
unsigned long flags;
int hide_uid[100];
unsigned int hide_uid_count=0;
module_param_array(hide_uid, int, &hide_uid_count, 0644);
char *hide_file[100] = {"bin/su", "bin/busybox", "app/Superuser.apk", "bin/proc", "bin/librank", };
unsigned int hide_file_cnt=1;
module_param_array(hide_file, charp, &hide_file_cnt, 0644);
int check_hide_uid(void)
{
int i;
for (i = 0; i < hide_uid_count; i++)
{
if (hide_uid[i] == current_uid())
return 1;
}
return 0;
}
int check_hide_file(const char *filename)
{
int i;
for (i=0; i < hide_file_cnt; i++)
{
if (strstr(filename, hide_file[i]))
{
return 1;
}
}
return 0;
}
asmlinkage int my_do_execve(char __user * filename,
const char __user * const __user * argv,
const char __user * const __user * envp, struct pt_regs *regs);
static struct jprobe my_jprobe = {
.entry = (kprobe_opcode_t *) my_do_execve
};
static unsigned long **find_sys_call_table(void)
{
unsigned long int offset = PAGE_OFFSET;
unsigned long **sct;
while (offset < ULLONG_MAX)
{
sct = (unsigned long **)offset;
if (sct[__NR_close] == (unsigned long *)sys_close)
return sct;
offset += sizeof(void *);
}
return NULL;
}
long (*orig_sys_getdents64) (unsigned int fd,
struct linux_dirent64 __user * dirent, unsigned int count);
asmlinkage long my_sys_getdents64(unsigned int fd,
struct linux_dirent64 __user * dirent, unsigned int count)
{
int fake = 0;
int offset, copyret;
struct linux_dirent64 *td1, *td2, *cur, *prev;
unsigned long ret, tmp;
char *ptr;
char *tmp_path;
char *pathname;
struct file *file;
struct path path;
char fullpath[512] = {0, };
ret = orig_sys_getdents64(fd, dirent, count);
fake = check_hide_uid();
if (fake == 0)
return ret;
if (ret <= 0)
return ret;
if ((td2 = kmalloc(ret + 1, GFP_KERNEL)) == NULL)
{
printk("KMALLOC FAILED!");
return ret;
}
spin_lock(¤t->files->file_lock);
file = fcheck(fd);
if (!file) {
printk("It is weird... How can you reach here?\n");
spin_unlock(¤t->files->file_lock);
return ret;
}
path = file->f_path;
path_get(&file->f_path);
spin_unlock(¤t->files->file_lock);
tmp_path = (char *)__get_free_page(GFP_TEMPORARY);
if (!tmp_path) {
path_put(&path);
return -ENOMEM;
}
pathname = d_path(&path, tmp_path, PAGE_SIZE);
path_put(&path);
if (IS_ERR(pathname)) {
free_page((unsigned long)tmp_path);
printk("Errnous path. \n");
return ret;
}
copyret = copy_from_user(td2, dirent, ret);
td1 = td2;
ptr = (char *)td2;
tmp = ret;
prev = NULL;
while (ptr < (char *)td1 + tmp)
{
cur = (struct linux_dirent64 *)ptr;
offset = cur->d_reclen;
fullpath[0] = 0;
strcat(fullpath, pathname);
strcat(fullpath, "/");
strcat(fullpath, cur->d_name);
if (check_hide_file(fullpath))
{
if (!prev)
{
ret -= offset;
td2 = (struct linux_dirent64 *)((char *)td1 + offset);
}
else
{
prev->d_reclen += offset;
memset(cur, 0, offset);
}
}
else
prev = cur;
ptr += offset;
}
copyret = copy_to_user((void *)dirent, (void *)td2, ret);
copyret = copyret;
kfree(td1);
free_page((unsigned long)tmp_path);
return ret;
}
long (*orig_sys_access) (const char __user * filename, int mode);
asmlinkage long my_sys_access(const char __user * filename, int mode)
{
int fake = check_hide_uid();
if (!fake == 0 && check_hide_file(filename))
return -ENOENT;
return orig_sys_access(filename, mode);
}
asmlinkage int my_do_execve(char __user * filename,
const char __user * const __user * argv,
const char __user * const __user * envp, struct pt_regs *regs)
{
int fake = check_hide_uid();
if (fake == 0)
jprobe_return();
if (check_hide_file(filename))
filename[0] = 0;
jprobe_return();
return 0;
}
long (*orig_sys_stat64) (const char __user * filename, struct stat64 __user * statbuf);
asmlinkage long my_sys_stat64(const char __user * filename, struct stat64 __user * statbuf)
{
int fake = check_hide_uid();
if (!fake == 0 && check_hide_file(filename))
return -ENOENT;
return orig_sys_stat64(filename, statbuf);
}
asmlinkage long (*orig_sys_open) (const char __user * filename, int flags, umode_t mode);
asmlinkage long my_sys_open(const char __user * filename, int flags, umode_t mode)
{
int fake = check_hide_uid();
if (!fake == 0 && check_hide_file(filename))
return -ENOENT;
return orig_sys_open(filename, flags, mode);
}
static int init_hideroot(void)
{
int ret;
unsigned long int i;
pmd_t pmd_backup[100] = {0, };
unsigned long int pmd_cnt;
init_mmuhack();
printk("Hooking...\n");
sys_call_table = find_sys_call_table();
printk("%p\n", sys_call_table);
printk("Backing up original address...\n");
orig_sys_getdents64 = (void *)sys_call_table[__NR_getdents64];
orig_sys_access = (void *)sys_call_table[__NR_access];
orig_sys_stat64 = (void *)sys_call_table[__NR_stat64];
orig_sys_open = (void *)sys_call_table[__NR_open];
printk("Unlocking...\n");
pmd_cnt = (unsigned long)((&sys_call_table[NR_syscalls] - sys_call_table) / (~PAGE_MASK) + 2);
printk("Number of pages needed to unlocked: %ld\n", pmd_cnt);
for (i = 0; i < pmd_cnt; i++)
{
pmd_backup[i] = unlock_page((unsigned long) sys_call_table + (~PAGE_MASK) * i);
}
printk("OK. pages unlocked. Starting hook\n");
sys_call_table[__NR_getdents64] = (unsigned long *)my_sys_getdents64;
sys_call_table[__NR_access] = (unsigned long *)my_sys_access;
sys_call_table[__NR_stat64] = (unsigned long *)my_sys_stat64;
sys_call_table[__NR_open] = (unsigned long *)my_sys_open;
unlock_page(kallsyms_lookup_name("do_execve"));
// Hook do_execve using jprobe
my_jprobe.kp.addr = (kprobe_opcode_t *) kallsyms_lookup_name("do_execve");
if (!my_jprobe.kp.addr)
{
printk("Couldn't find %s to plant jprobe\n", "do_execve");
return -1;
}
if ((ret = register_jprobe(&my_jprobe)) < 0)
{
printk("register_jprobe failed, returned %d\n", ret);
return -1;
}
printk("Planted jprobe at %p, handler addr %p\n", my_jprobe.kp.addr, my_jprobe.entry);
printk("OK. now restoring PMDs.\n");
for (i = 0; i < pmd_cnt; i++)
{
restore_pmd((unsigned long) sys_call_table + (~PAGE_MASK) * i, pmd_backup[i]);
}
printk("Okay. Enjoy it!\n");
return 0;
}
static void cleanup_hideroot(void)
{
unsigned long int i;
pmd_t pmd_backup[100] = {0, };
unsigned long int pmd_cnt;
printk("Unlocking...\n");
pmd_cnt = (unsigned long)((&sys_call_table[NR_syscalls] - sys_call_table) / (~PAGE_MASK) + 2);
for (i = 0; i < pmd_cnt; i++)
{
pmd_backup[i] = unlock_page((unsigned long) sys_call_table + (~PAGE_MASK) * i);
}
printk("OK. Pages unlocked. now restoring..\n");
sys_call_table[__NR_getdents64] = (unsigned long *)orig_sys_getdents64;
sys_call_table[__NR_access] = (unsigned long *)orig_sys_access;
sys_call_table[__NR_stat64] = (unsigned long *)orig_sys_stat64;
sys_call_table[__NR_open] = (unsigned long *)orig_sys_open;
printk("Unregistering jprobe\n");
unregister_jprobe(&my_jprobe);
printk("OK. now restoring PMDs.\n");
for (i = 0; i < pmd_cnt; i++)
{
restore_pmd((unsigned long) sys_call_table + (~PAGE_MASK) * i, pmd_backup[i]);
}
printk("Okay. bye.\n");
}
module_init(init_hideroot);
module_exit(cleanup_hideroot);
MODULE_LICENSE("GPL");