-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlaf.c
528 lines (419 loc) · 12.8 KB
/
laf.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
LAF - Linux Application Firewall (for linux Intel 32 and 64bits and ARM 32 bits)
This firewall allows only communications made from allowed processes
The detection and block is performed over the socket, AF_UNIX are allowed
allways.
If other kind of socket is created (AF_INET,AF_INET6,...) if the
processname is not in the whitelist the socket creation is canceled.
Copyright 2015-2016 by @sha0coder and @capi_x
Licensed under GNU General Public License 3.0 or later.
Some rights reserved. See COPYING, AUTHORS.
@license GPL-3.0 <http://www.gnu.org/licenses/gpl-3.0.txt>
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
#include <linux/limits.h> /* ULLONG_MAX */
#include <linux/net.h>
#include <linux/netlink.h>
#include <linux/skbuff.h>
#include <linux/sched.h> /* current struct */
#include <linux/in.h> /* sockaddr_in */
#include <linux/socket.h>
#include <linux/syscalls.h>
#include <linux/sysctl.h>
#include <linux/version.h>
#include <net/sock.h>
#include "laf.h"
#include "ia32_addr.h"
#ifdef __x86_64__
# define __NR_socketcall 102
# define IA32_AF_INET 0x100000002
#elif defined(__i386__)
# ifndef __NR_socket
# define __NR_socket (__X32_SYSCALL_BIT + 41)
# endif
# define IA32_AF_INET 0x2
#else
# define IA32_AF_INET 0x2
#endif
/*
* ENABLED 0 -> don't block sockets | ENABLED 1 -> block sockets
* DEBUG 1 -> log allowed sockets
* LOG 0 -> don't log blocks | LOG 1 -> log blocks */
static int ENABLED = 0;
static int DEBUG = 0;
static int LOG = 1;
static int sysctl_min_val = 0;
static int sysctl_max_val = 1;
static struct ctl_table_header *busy_sysctl_header;
struct sock *laf_netlink = NULL;
char *whitelist_exact [MAX_WHITELIST];
char *whitelist_similar[MAX_WHITELIST];
asmlinkage long (*old_socketcall) (int call, unsigned long __user *args);
asmlinkage int (*old_socket) (int domain, int type, int protocol);
unsigned long **st;
unsigned long **ia32_st;
static void laf_netlink_send(char * msg_out, unsigned long msg_out_size) {
int res = 0;
struct nlmsghdr *nlh;
struct sk_buff *skb_out;
skb_out = nlmsg_new(NLMSG_ALIGN(msg_out_size), GFP_KERNEL);
if (!skb_out) {
printk(KERN_ALERT "LAF: Failed to allocate new skb.\n");
kfree(msg_out);
return;
}
nlh = nlmsg_put(skb_out, 0, 1, NLMSG_DONE, msg_out_size, 0);
strncpy(nlmsg_data(nlh), msg_out, msg_out_size - 1);
res = nlmsg_multicast(laf_netlink, skb_out, 0, NETLINK_LAF_GRP, GFP_KERNEL);
/* a client must be listening, if not it will throw a -3 error (only in debug mode) */
if ( res < 0 && DEBUG)
printk(KERN_ALERT "LAF: Error %i sending netlink multicast.\n", res);
}
static void laf_get_whitelist (char **whitelist) {
unsigned long msg_out_len = 0;
char *msg_out;
int i=0;
printk(KERN_INFO "LAF: Request - get whitelist.\n");
for (i=0; i<MAX_WHITELIST; i++) {
if (whitelist[i] == NULL)
break;
msg_out_len = msg_out_len + strlen(whitelist[i]) + 1;
}
if (msg_out_len == 0)
return;
/* alloc the string size + end null */
msg_out = kmalloc(msg_out_len + 1,GFP_KERNEL);
memset (msg_out, 0, msg_out_len);
for (i=0; i<MAX_WHITELIST; i++) {
if (whitelist[i] == NULL)
break;
strcat(msg_out,whitelist[i]);
strcat(msg_out,"/");
}
if (DEBUG)
printk(KERN_INFO "LAF: %s\n", msg_out);
laf_netlink_send(msg_out, msg_out_len);
kfree(msg_out);
}
static void laf_set_whitelist(char *msg_raw, char **whitelist) {
int i = 0;
char *token;
char *msg_in = msg_raw + 1;
const char delim[] = "/";
printk(KERN_INFO "LAF: Request - update whitelist.\n");
/* clean and kfree all whitelist matrix */
for (i=0; i<MAX_WHITELIST; i++) {
if (whitelist[i] != NULL) {
kfree(whitelist[i]);
whitelist[i] = NULL;
}
}
/* iter msg_in */
token = strsep(&msg_in, delim);
i = 0;
while( token != NULL )
{
if (i >= (MAX_WHITELIST - 1)) {
printk(KERN_INFO "LAF: whitelist limit reached (%i).\n", MAX_WHITELIST);
break;
}
if (strlen(token) > 0) {
if (DEBUG)
printk(KERN_INFO "LAF: exact - %s\n", token );
whitelist[i] = kmalloc(strlen(token) + 1, GFP_KERNEL);
strcpy(whitelist[i],token);
i++;
}
token = strsep(&msg_in, delim);
}
whitelist[i] = NULL;
}
static void laf_netlink_recv(struct sk_buff *skb) {
struct nlmsghdr *nlh;
char *msg_raw;
/* the first msg_raw char is the command, the next is the payload and points to msg */
nlh = (struct nlmsghdr*)skb->data;
msg_raw = (char*)nlmsg_data(nlh);
if (DEBUG)
printk(KERN_INFO "LAF: recv from %d - %s\n", nlh->nlmsg_pid, msg_raw);
switch (msg_raw[0]) {
case '1':
laf_get_whitelist(whitelist_exact);
break;
case '2':
laf_get_whitelist(whitelist_similar);
break;
case '3':
laf_set_whitelist(msg_raw, whitelist_exact);
break;
case '4':
laf_set_whitelist(msg_raw, whitelist_similar);
break;
default:
printk(KERN_INFO "LAF: recv -> WTF?!\n");
break;
}
return;
}
static void laf_send_alert(int type, int domain, int protocol, char *comm, int pid, int tgid, char *parent_comm, int parent_pid ) {
unsigned long new_msg_size = 0;
unsigned long org_msg_size = 0;
char *new_msg;
/* kernel log */
if (LOG)
switch (type) {
case LAF_BLOCK_S:
printk(KERN_INFO "LAF: fam %02d proto %02d blocked: %s (%i:%i) parent: %s (%i)\n", domain, protocol, comm, pid, tgid, parent_comm, parent_pid);
break;
case LAF_ALLOW_S:
printk(KERN_INFO "LAF: fam %02d proto %02d allowed: %s (%i:%i) parent: %s (%i)\n", domain, protocol, comm, pid, tgid, parent_comm, parent_pid);
break;
case LAF_BLOCK_SC:
printk(KERN_INFO "LAF: call %02d fam 0x%x blocked: %s (%i:%i) parent: %s (%i)\n", domain, protocol, comm, pid, tgid, parent_comm, parent_pid);
break;
case LAF_ALLOW_SC:
printk(KERN_INFO "LAF: call %02d fam 0x%x allowed: %s (%i:%i) parent: %s (%i)\n", domain, protocol, comm, pid, tgid, parent_comm, parent_pid);
break;
}
/* netlink broadcast */
org_msg_size = strlen(comm) + strlen(parent_comm) + ( sizeof(int) * 6 ) + 1;
new_msg_size = org_msg_size + 8; /* 7 slash + 1 null */
new_msg = (char *) kmalloc(new_msg_size, GFP_KERNEL);
sprintf(new_msg, "/%i/%i/%s/%i/%i/%s/%i", domain, protocol, comm, pid, tgid, parent_comm, parent_pid);
laf_netlink_send(new_msg, new_msg_size);
kfree(new_msg);
}
static unsigned long **acquire_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;
}
#ifdef __x86_64__
static unsigned long **acquire_ia32_sys_call_table(void)
{
return (unsigned long **)SYSCALL_IA32_ADDR;
}
#endif
int isWhitelistedSimilar(void) {
int i;
for (i=0; i<MAX_WHITELIST; i++) {
if (whitelist_similar[i] == NULL)
return 0;
if (strstr((char *)(current->comm), whitelist_similar[i]) != NULL)
return 1;
}
return 0;
}
int isWhitelistedExact(void) {
int i;
for (i=0; i<MAX_WHITELIST; i++) {
if (whitelist_exact[i] == NULL)
return 0;
if (strcmp(whitelist_exact[i], (char *)(current->comm)) == 0)
return 1;
}
return 0;
}
static void disable_page_protection(void) {
#ifndef __arm__
unsigned long value;
asm volatile("mov %%cr0, %0" : "=r" (value));
if(!(value & 0x00010000))
return;
asm volatile("mov %0, %%cr0" : : "r" (value & ~0x00010000));
#endif
}
static void enable_page_protection(void) {
#ifndef __arm__
unsigned long value;
asm volatile("mov %%cr0, %0" : "=r" (value));
if((value & 0x00010000))
return;
asm volatile("mov %0, %%cr0" : : "r" (value | 0x00010000));
#endif
}
/* new_socket - function to block or allow a new socket
* --
* Those family/type are whitelisted because cannot use IPv4/6:
*
* +============+===========================+====+
* | FAMILY | PROTOCOLS |TYPE|
* +============+===========================+====+
* | AF_UNIX | ALL PROTOCOLS | -- |
* +------------+---------------------------+----+
* | AF_NETLINK | NETLINK_AUDIT | 09 |
* | | NETLINK_KOBJECT_UEVENT | 15 |
* +------------+---------------------------+----+
*
* This function also check if the socket is whitelisted in the
* whitelist.
*/
asmlinkage int new_socket(int domain, int type, int protocol) {
if ((domain != AF_UNIX) && ((domain != AF_NETLINK) && (protocol != 9) && (protocol != 15))) {
if (!isWhitelistedExact() && !isWhitelistedSimilar() && ENABLED) {
laf_send_alert(LAF_BLOCK_S, domain,protocol,current->comm,current->pid,current->tgid,current->real_parent->comm,current->real_parent->pid);
return BLOCKED;
} else if (DEBUG)
laf_send_alert(LAF_ALLOW_S, domain,protocol,current->comm,current->pid,current->tgid,current->real_parent->comm,current->real_parent->pid);
}
return old_socket(domain,type,protocol);
}
asmlinkage long new_socketcall(int call, unsigned long __user *args) {
switch (call) {
case SYS_BIND:
case SYS_SOCKET:
if (!access_ok(VERIFY_READ, args, sizeof(unsigned long *)))
return BLOCKED;
if (args[0] == IA32_AF_INET) {
if (!isWhitelistedExact() && !isWhitelistedSimilar() && ENABLED) {
laf_send_alert(LAF_BLOCK_SC, call,args[0],current->comm,current->pid,current->tgid,current->real_parent->comm,current->real_parent->pid);
return BLOCKED;
} else if (DEBUG)
laf_send_alert(LAF_ALLOW_SC, call,args[0],current->comm,current->pid,current->tgid,current->real_parent->comm,current->real_parent->pid);
}
break;
}
return old_socketcall(call,args);
}
static struct ctl_table laf_child_table[] = {
{
.procname = "enabled",
.maxlen = sizeof(int),
.mode = 0644,
.data = &ENABLED,
.proc_handler = &proc_dointvec_minmax,
.extra1 = &sysctl_min_val,
.extra2 = &sysctl_max_val,
},
{
.procname = "debug",
.maxlen = sizeof(int),
.mode = 0644,
.data = &DEBUG,
.proc_handler = &proc_dointvec_minmax,
.extra1 = &sysctl_min_val,
.extra2 = &sysctl_max_val,
},
{
.procname = "log",
.maxlen = sizeof(int),
.mode = 0644,
.data = &LOG,
.proc_handler = &proc_dointvec_minmax,
.extra1 = &sysctl_min_val,
.extra2 = &sysctl_max_val,
},
{}
};
static struct ctl_table laf_main_table[] = {
{
.procname = "laf",
.mode = 0555,
.child = laf_child_table,
},
{}
};
static struct ctl_table kernel_main_table[] = {
{
.procname = "kernel",
.mode = 0555,
.child = laf_main_table,
},
{}
};
void unHook(void) {
disable_page_protection();
/* Reset old syscall table */
#ifndef __i386__
st[__NR_socket] = (void *)old_socket;
#endif
#ifndef __arm__
ia32_st[__NR_socketcall] = (void *)old_socketcall;
#endif
enable_page_protection();
}
void hook(void) {
disable_page_protection();
/* Intel 64 bits and ARM sys_socket */
#ifndef __i386__
old_socket = (void *)st[__NR_socket];
st[__NR_socket] = (void *)new_socket;
#endif
/* Intel 32 bit (real/emul) sys_socketcall */
#ifndef __arm__
old_socketcall = (void *)ia32_st[__NR_socketcall];
ia32_st[__NR_socketcall] = (void *)new_socketcall;
#endif
enable_page_protection();
}
static int __init load(void) {
/* netlink init */
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0)
laf_netlink = netlink_kernel_create(&init_net, NETLINK_LAF_USR, 0, laf_netlink_recv, NULL, THIS_MODULE);
#else
struct netlink_kernel_cfg cfg = {
.groups = NETLINK_LAF_GRP,
.input = laf_netlink_recv,
};
laf_netlink = netlink_kernel_create(&init_net, NETLINK_LAF_USR, &cfg);
#endif
if (!laf_netlink) {
printk(KERN_ALERT "LAF: Error creating netlink socket. (user: %i group: %x)\n",NETLINK_LAF_USR,NETLINK_LAF_GRP);
return -EFAULT;
}
/* register sysctl table */
busy_sysctl_header = register_sysctl_table(kernel_main_table);
if (!busy_sysctl_header) {
netlink_kernel_release(laf_netlink);
printk(KERN_ALERT "LAF: Failed to register kernel_main_table\n");
return -EFAULT;
}
/* load the syscall table addr in st */
st = acquire_sys_call_table();
/* load the 32 bit emul sys_table on amd64 or the real 32 sys_table on ia32 */
#ifdef __x86_64__
ia32_st = acquire_ia32_sys_call_table();
#else
ia32_st = st;
#endif
/* exec syscall hooks */
hook();
printk(KERN_INFO "LAF: Loaded\n");
if (DEBUG)
printk(KERN_INFO "LAF: st -> 0x%p - ia32_st -> 0x%p)\n",st,ia32_st);
return 0;
}
static void __exit unload(void) {
int i = 0;
/* restore original syscalls */
unHook();
/* release netlink */
netlink_kernel_release(laf_netlink);
/* unregister sysctl table */
unregister_sysctl_table(busy_sysctl_header);
/* free all mem */
for (i=0; i<MAX_WHITELIST; i++) {
if (whitelist_exact[i] != NULL) {
kfree(whitelist_exact[i]);
whitelist_exact[i] = NULL;
}
if (whitelist_similar[i] != NULL) {
kfree(whitelist_similar[i]);
whitelist_similar[i] = NULL;
}
}
printk(KERN_INFO "LAF: Unloaded\n");
}
module_init(load);
module_exit(unload);
MODULE_LICENSE ("GPL");
MODULE_AUTHOR ("@sha0coder");