-
Notifications
You must be signed in to change notification settings - Fork 3
/
hack_open.c
92 lines (67 loc) · 2.16 KB
/
hack_open.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
/*
* hack_open.c
*
* PoC of overriding open(2) using a LKM
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/unistd.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Alan <[email protected]>");
MODULE_VERSION("0.0.1");
MODULE_DESCRIPTION("PoC for overriding open(2)");
/* defines the location in memory where our system call table exists*/
// TODO: use ELF sections?
unsigned long *sys_call_table = (unsigned long) 0xffffffffbbc00180;
/* This defines a pointer to the real open() syscall */
asmlinkage int (*old_open)(const char *filename, int flags, int mode);
/* enable use to memory page and write to it */
int
set_addr_rw(long unsigned int _addr)
{
unsigned int level;
pte_t *pte = lookup_address(_addr, &level);
/* enable write */
if (pte->pte &~ _PAGE_RW) pte->pte |= _PAGE_RW;
}
/* ensure that when cleanup occurs, make page write-protected */
int
set_addr_ro(long unsigned int _addr)
{
unsigned int level;
pte_t *pte = lookup_address(_addr, &level);
pte->pte = pte->pte &~_PAGE_RW;
}
asmlinkage int
new_open(const char *filename, int flags, int mode)
{
/* perform our malicious code here */
printk(KERN_INFO "Intercepting open(%s, %X, %X)\n", filename, flags, mode);
/* give execution BACK to the original syscall */
return (*old_open)(filename, flags, mode);
}
static int __init
init(void)
{
printk(KERN_INFO "Welcome to Kernel Town!\n");
/* allow us to write to memory page, so that we can hijack the system call */
set_addr_rw((unsigned long) sys_call_table);
/* grab system call number definition from sys_call_table */
old_open = (void *) sys_call_table[__NR_open];
/* set the open symbol to our new_open system call definition */
sys_call_table[__NR_open] = new_open;
return 0;
}
static void __exit
cleanup(void)
{
/* set the open symbol BACK to the old open system call definition */
sys_call_table[__NR_open] = old_open;
/* set memory page back to read-only */
set_addr_ro((unsigned long) sys_call_table);
printk(KERN_INFO "We are now leaving Kernel Town! Thanks for the stay!\n");
return;
}
module_init(init);
module_exit(cleanup);