- no kernel API to enumerate some structure
- no real-time notifications about setting some hook
- no mapping on /proc or /sys (however this method is not reliable)
- no 3rd party tools to show this. As an example I chose volatility - just bcs I readed their folio "The Art of Memory Forensics"
воскресенье, 3 октября 2021 г.
what linux hiding
вторник, 28 сентября 2021 г.
PoC to hide kprobes list
as you may know list of kprobes has mapping on /sys in file /sys/kernel/debug/kprobes/list. And now when I have working filesystem notifications it would be extremely tempting try to make hiding content of this file. Let`s see what this inode contains:
sudo ./lkmem -s -c ~/krnl/curr ~/krnl/System.map-5.11.0-34- generic /sys/kernel/debug/kprobes/list - set notification for file /sys/kernel/debug/kprobes/list
- in fsnotify_handle_event callback check inode and mask
- if this is first opening of this file - patch kprobes_sops->show to our own function (be cautious with WP in cr0)
- if this is last closing of this file - return original handler to kprobes_sops->show
- also return original handler when driver is unloading
воскресенье, 26 сентября 2021 г.
filesystem notifications in linux kernel
disclaimer
Filesystems are the most complex part of any OS. I am not a specialist in linux filesystems and even don`t commit the code to linux kernel. So all information here cannot be considered reliable, code has tons of bugs and can damage your machine and ruin the rest of your life
Usermode notifications
linux has 3 mechanisms for passing filesystem notification to user-mode:
- just simple inode - for them dumped inode->i_ino & superblock s_dev - I don`t know how you can in usermode find mountpoint for this superblock
- mount point (btw struct mount even not described in linux/include). At least knowing mnt_id you can find name in /proc/pid/mountinfo file
- superblock - s_dev again dumping
суббота, 18 сентября 2021 г.
linux kernel uprobes
Lets consider another spying mechanism in linux kernel - uprobes. They also insert int3 but this time in user-mode and can be used for example to steal TLS traffic. I made simple code to set up uprobe for /usr/bin/ls on PLT thunk getenv:
objdump -d /usr/bin/lsls -i /usr/bin/ls
четверг, 9 сентября 2021 г.
linux kernel kprobes
without a doubt most crazy and insane spying mechanism in linux kernel is krobes
- It`s expensive - each time when int3 occurred typical call stack looks like:
xen_asm_exc_int3
asm_exc_int3
irq_entries_start
exc_int3
do_int3
kprobe_int3_handler
- It makes working with kdbg (which itself is too far away from windbg) like nightmare - function do_int3 first calls kgdb_ll_trap
- There is no mechanism to predict which functions cannot be kprobed. Let assume that your handler uses simple printk - so you can`t set kprobe on whole graph of functions called from printk (like vprintk_func, vprintk_default, vprintk_emit, __msecs_to_jiffies, arch_touch_nmi_watchdog, touch_softlockup_watchdog, __printk_safe_enter, _raw_spin_lock, vprintk_store, vscnprintf, cont_flush etc etc) and as far I know there is no way to even find them all
- Sure you have /sys/kernel/debug/kprobes/list file so you can see which functions was hooked. But there is no way to know by whom
sudo ./lkmem -k -c ~/krnl/curr ~/krnl/System.map-5.11.0-34-generic
kprobes[47]: 1 kprobe at 0xffffffffc0605080 flags 8 addr: 0xffffffffa4a9f040 - kernel!__do_sys_fork pre_handler: 0xffffffffc0603548 - lkcd post_handler: 0xffffffffc0603526 - lkcd
понедельник, 6 сентября 2021 г.
linux-kernel per-cpu vars
It`s hard to believe but linux has degraded version of KPCR on windows - so called "per-cpu variables". This is some isolated memory assigned to CPU (stored in gs segment register on x64 and in MSR register c13 on arm64) and can contains some interesting fields. Why this is important to know offsets some of this variables? Well, I suspect that linux kernel contains much more code for espionage than windows (for example trace events, tracepoints, kprobes, usb_mon_register etc etc). One of such code is function user_return_notifier_register with which you can register your own notifications. Unfortunately this list of notifications stored in per-cpu variable return_notifier_list
And as usually there is no some include file with definition of all of this per-cpu fields. Moreover this offsets depend from config for kernel building and differ in each build. Sounds like nightmare, reason to turn off the computer and go drink vodka looking at the autumn rain.Patch on github to extract this_cpu_off & return_notifier_list with some disasm magic
суббота, 28 августа 2021 г.
linux kernel tracing
It`s hard to believe but linux kernel has almost exact copy of windows ETW - event tracing. It is just as difficult to make it work, it is poorly documented, very complex and fragile. And yes, as you can guess - it also can`t show who and which parts of it in use. So I wrote some code to dump registered funcs in tracepoints and to check file ops for files in /sys/kernel/tracing/events
Lets start with tracepoints. As you see this structure has strange looked list of functions in field funcs, and calling happens in functions like event_triggers_call. How we can find this tracepoints? Well, they stored in trace_event_call->tp and array of pointers to trace_event_call located between symbols __start_ftrace_events & __stop_ftrace_events. Unfortunately all this treasures located in discardable section .init.data. But because they were all declared in the same manner we can find them by name - all symbols with prefix __tracepoint_ is what we need. So some examples (you can run lkmem -c -t vmlinux system.map to get this):
__tracepoint_sys_enter at 0xffffffff8b82e340: enabled 0 cnt 0
regfunc 0xffffffff8a192330 - kernel!syscall_regfunc
unregfunc 0xffffffff8a1923f0 - kernel!syscall_unregfunc
Well, no clients right now - cnt 0
Next about /sys/kernel/tracing/events files (this is perverted inhuman interface to manage trace events). I just dumping file->f_path.dentry->d_inode->i_fop for each such file. Sample of output (you can achieve this with lkmem -s vmlinux system.map path_to_some_sys_kernel_tracing_file):