-
Notifications
You must be signed in to change notification settings - Fork 3
/
syshook_execve.c
302 lines (247 loc) · 5.45 KB
/
syshook_execve.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
#include <linux/binfmts.h>
#include <linux/version.h>
#include <linux/skbuff.h>
#include <linux/module.h>
#include <linux/kprobes.h>
#include <linux/kallsyms.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION( 3, 10, 0 )
struct user_arg_ptr {
#ifdef CONFIG_COMPAT
bool is_compat;
#endif
union {
const char __user *const __user *native;
#ifdef CONFIG_COMPAT
const compat_uptr_t __user *compat;
#endif
} ptr;
};
#endif
struct kprobe kp;
static const char __user *get_user_arg_ptr( struct user_arg_ptr argv, int nr )
{
const char __user *native;
#ifdef CONFIG_COMPAT
if ( unlikely( argv.is_compat ) )
{
compat_uptr_t compat;
if ( get_user( compat, argv.ptr.compat + nr ) )
return(ERR_PTR( -EFAULT ) );
return(compat_ptr( compat ) );
}
#endif
if ( get_user( native, argv.ptr.native + nr ) )
return(ERR_PTR( -EFAULT ) );
return(native);
}
static int tmp_count( struct user_arg_ptr argv, int max )
{
int i = 0;
if ( argv.ptr.native != NULL )
{
for (;; )
{
const char __user *p = get_user_arg_ptr( argv, i );
if ( !p )
break;
if ( IS_ERR( p ) )
return(-EFAULT);
if ( i >= max )
return(-E2BIG);
++i;
if ( fatal_signal_pending( current ) )
return(-ERESTARTNOHAND);
cond_resched();
}
}
return(i);
}
int handler_pre( struct kprobe *p, struct pt_regs *regs )
{
int error = 0, i = 0, len = 0, offset = 0, max_len = 0;
const char __user * native = NULL;
char *total_argc_ptr = NULL;
char *total_envpc_ptr = NULL;
char *per_envp = NULL;
int tmp_argc = 0, total_argc_len = 0;
int tmp_envpc = 0, total_envpc_len = 0;
char *tmp = kmalloc( PATH_MAX, GFP_KERNEL );
struct user_arg_ptr argvx = { .ptr.native = regs->si };
struct user_arg_ptr envpx = { .ptr.native = regs->dx };
tmp_argc = tmp_count( argvx, MAX_ARG_STRINGS );
for ( i = 0; i < tmp_argc; i++ )
{
native = get_user_arg_ptr( argvx, i );
if ( IS_ERR( native ) )
{
error = -EFAULT;
goto err;
}
len = strnlen_user( native, MAX_ARG_STRLEN );
if ( !len )
{
error = -EFAULT;
goto err;
}
total_argc_len += len;
}
total_argc_ptr = kmalloc( total_argc_len + 16 * tmp_argc, GFP_ATOMIC );
if ( !total_argc_ptr )
{
error = -ENOMEM;
goto err;
}
memset( total_argc_ptr, 0, total_argc_len + 16 * tmp_argc );
for ( i = 0; i < tmp_argc; i++ )
{
if ( i == 0 )
{
continue;
}
native = get_user_arg_ptr( argvx, i );
if ( IS_ERR( native ) )
{
error = -EFAULT;
goto err;
}
len = strnlen_user( native, MAX_ARG_STRLEN );
if ( !len )
{
error = -EFAULT;
goto err;
}
if ( offset + len > total_argc_len + 16 * tmp_argc )
{
break;
}
if ( copy_from_user( total_argc_ptr + offset, native, len ) )
{
error = -EFAULT;
goto err;
}
offset += len - 1;
*(total_argc_ptr + offset) = ' ';
offset += 1;
}
/*--------envpx--------------*/
len = 0;
offset = 0;
tmp_envpc = tmp_count( envpx, MAX_ARG_STRINGS );
if ( tmp_envpc < 0 )
{
error = tmp_envpc;
goto err;
}
for ( i = 0; i < tmp_envpc; i++ )
{
native = get_user_arg_ptr( envpx, i );
if ( IS_ERR( native ) )
{
error = -EFAULT;
goto err;
}
len = strnlen_user( native, MAX_ARG_STRLEN );
if ( !len )
{
error = -EFAULT;
goto err;
}
if ( len > max_len )
{
max_len = len;
}
total_envpc_len += len;
}
per_envp = kmalloc( max_len + 16, GFP_KERNEL );
if ( !per_envp )
{
error = -ENOMEM;
goto err;
}
total_envpc_ptr = kmalloc( total_envpc_len + 16 * tmp_envpc, GFP_KERNEL );
if ( !total_envpc_ptr )
{
error = -ENOMEM;
goto err;
}
memset( total_envpc_ptr, 0, total_envpc_len + 16 * tmp_envpc );
for ( i = 0; i < tmp_envpc; i++ )
{
native = get_user_arg_ptr( envpx, i );
if ( IS_ERR( native ) )
{
error = -EFAULT;
goto err;
}
len = strnlen_user( native, MAX_ARG_STRLEN );
if ( !len )
{
error = -EFAULT;
goto err;
}
if ( offset + len > total_envpc_len + 16 * tmp_envpc )
{
break;
}
memset( per_envp, 0, max_len );
if ( copy_from_user( per_envp, native, len ) )
{
error = -EFAULT;
goto err;
}
if ( !strstr( per_envp, "PWD" ) && !strstr( per_envp, "LOGNAME" ) && !strstr( per_envp, "USER" ) )
{
continue;
}
if ( copy_from_user( total_envpc_ptr + offset, native, len ) )
{
error = -EFAULT;
goto err;
}
offset += len - 1;
*(total_envpc_ptr + offset) = ' ';
offset += 1;
}
printk( KERN_INFO "%s|%s|%u|%s|%d|%s\n", (char *) regs->di, total_argc_ptr, current->tgid, current->parent->comm, current->parent->tgid, total_envpc_ptr );
/*
* printk(KERN_INFO "|pid: %6d |tgid: %6d | parent_filename = %10s| parent_tgid: %6d |comm: %10s |filename = %10s|argv = %s \n",
* current->pid, current->tgid, current->parent->comm, current->parent->tgid ,current->comm, (char *)regs->di ,total_argc_ptr);
*/
err:
if ( tmp )
{
kfree( tmp );
tmp = NULL;
}
if ( per_envp )
{
kfree( per_envp );
per_envp = NULL;
}
if ( total_argc_ptr )
{
kfree( total_argc_ptr );
total_argc_ptr = NULL;
}
if ( total_envpc_ptr )
{
kfree( total_envpc_ptr );
total_envpc_ptr = NULL;
}
return(0);
}
static __init int init_kprobe_sample( void )
{
kp.symbol_name = "sys_execve";
kp.pre_handler = handler_pre;
register_kprobe( &kp );
printk( KERN_INFO "---------------kprobe for execve--------------\n" );
return(0);
}
static __exit void cleanup_kprobe_sample( void )
{
unregister_kprobe( &kp );
}
module_init( init_kprobe_sample );
module_exit( cleanup_kprobe_sample );
MODULE_LICENSE( "GPL" );