← All posts

Linux rootkit detection in memory: cross-views and hook checks

8 min read

A Linux kernel rootkit has two jobs: stay loaded without being listed, and change what user space sees. Both leave structural traces in memory. The rootkit has to unlink itself from lists the kernel still references elsewhere, and its filtering code has to be reachable from a pointer the kernel follows. ramparser checks both, from the same structured snapshot described in Linux memory forensics.

Every pointer check applies one rule. Code the kernel calls must live in the kernel's text (_stext to _etext, plus .init.text) or in the core region of a module on the module list. A pointer into a listed module is attributed to that module. A pointer into neither is reported as "no known code", the strongest signal in this post.

These checks require the kernel's ISF (symbol file). Each one runs only if the symbols it needs are present, and the integrity view lists the checks that actually ran.

Hidden processes

Attacker: unlink a task_struct from the task list so ps, top and /proc never iterate over it (DKOM).

Structure: every task still owns a struct pid registered in the PID allocator, init_pid_ns.idr (an xarray on modern kernels, a radix tree on older ones). This is the modern form of the book's linux_pidhashtable cross-view.

Check: ramparser walks the PID table, follows each pid.tasks[PIDTYPE_PID] back to its task_struct, and compares it with every task reached from init_task.tasks, threads included. Tasks already marked dead are skipped, because exiting tasks leave the list first.

Finding: a line such as pid 4121 kworkerd (task 0xffff8881…), rated critical. A process that is in the PID table but not in the task list was hidden on purpose.

False positives: a task created or torn down during acquisition can show up in one view only. Check its state and whether the same PID shows up in open sockets or files.

Shared credentials

Each process gets its own struct cred when it forks; only the threads of one process share theirs. A rootkit that wants to make a shell root without calling setuid simply points the shell's task_struct.cred at the credentials of init or another root process. This is what linux_check_creds looks for, and ramparser does the same: two processes whose cred pointer is identical are flagged, critical when the shared identity is root. The finding names both processes, for example Shares its credentials with systemd (1), with the address of the cred structure. On a clean system no two processes share one.

Hidden modules

Attacker: list_del the module from modules (what lsmod reads), and often kobject_del it from sysfs as well. Diamorphine and many other LKM kits do both.

Check: three views are compared, following linux_check_modules and linux_hidden_modules:

  1. The modules list.
  2. The sysfs module_kset list, each kobject pointing to its module.
  3. A carve of the module area (0xffffffffa0000000 and up on x86-64). Each struct module contains its own sysfs kobject, whose mkobj.mod field points back at the module. A value v found at v + offsetof(module, mkobj.mod) marks a module at v, and that back-pointer survives unlinking from both lists. Candidates are kept only if their name is printable and their state is valid.
Module listsysfsCarvedVerdict
yesyesyesnormal
noyes—critical: removed from lsmod
nonoyescritical: only in module memory
yesno—suspicious: removed from sysfs only

Finding: diamorphine at 0xffffffffc0833080 — only in module memory (carved).

False positives: a module in the middle of loading or unloading. The snapshot records each listed module's state (coming, going).

The kernel view downloads the loaded image of any module on the list (linux_moddump): code and data as they sit in kernel memory, not the .ko file, with pages not in RAM zero-filled. Modules found only by the carve are not offered for download.

Syscall table integrity

Attacker: overwrite sys_call_table entries (kill, getdents64, openat…) so every program gets filtered answers.

Check: linux_check_syscall. sys_call_table, ia32_sys_call_table and x32_sys_call_table are read in full. Their length comes from the distance to the next symbol, because the ISF does not record it. Every entry's owner is resolved.

Finding: sys_call_table[62] → 0xffffffffc0a1b2c0 (module diamorphine). Index 62 is kill on x86-64, which Diamorphine uses as its control channel. Any entry outside the kernel is rated critical, and the owning module gets the "owns syscall table entries" flag in the module list.

Interrupt descriptor table

linux_check_idt: the 256 gates of idt_table are decoded (low, middle and high offset fields, as laid out in the ISF's gate struct) and each handler is attributed. Early exception stubs that live in .init.text count as kernel. An off-kernel vector is critical. This technique is rare on modern kernels, but the check costs nothing.

File, /proc and network operation tables

Modern rootkits rarely touch the syscall table. They swap a function pointer in an operations structure instead: iterate_shared of a directory to hide files, show of a seq_operations to hide connections. This is the ground of linux_check_fop and linux_check_afinfo.

ramparser resolves the type of each symbol from the ISF, enumerates its function-pointer members, and checks every non-NULL pointer. The tables covered:

  • /proc: proc_root_operations, proc_root_inode_operations, proc_dir_operations, proc_fd_operations, proc_tid_base_operations, proc_pid_link_inode_operations
  • Connection listings: tcp4_seq_ops, tcp6_seq_ops, udp_seq_ops, udp6_seq_ops, raw_seq_ops
  • Terminals: tty_fops, ptmx_fops
  • File systems: ext4_file_operations, ext4_dir_operations, ext4_file_inode_operations, ext4_dir_inode_operations, xfs_file_operations, xfs_dir_file_operations, btrfs_file_operations

A finding reads tcp4_seq_ops.show → 0x… (no known code).

Inline hooks

A rootkit can also leave every table untouched and overwrite the first bytes of the function a table points to with a jump. This is linux_check_inline_kernel. ramparser decodes the first instruction of every handler in sys_call_table and ia32_sys_call_table and of every function in the ops tables above, after an optional endbr64, looking for the trampolines rootkits write: jmp rel32, jmp [rip], push imm32/ret, and movabs followed by jmp or push/ret on the same register. A jump that stays in the function's own image, such as a tail call, passes. One that leaves it is reported: into a module it is suspicious, into no known code critical. The call to __fentry__ that ftrace installs is left to the ftrace check below. The older on-demand check_inline_hooks byte-pattern sweep remains; it is not anchored to prologues, so its hits are leads to disassemble.

Hook frameworks

Some kernel interfaces let code register a callback without patching anything. Rootkits use them for the same reason legitimate tools do: they are stable. Each registered callback is listed with its owner.

  • ftrace: ftrace_ops_list. Hooking through ftrace is the standard technique of current LKM rootkits. The check reports each callback and its owner, not the list of functions it filters.
  • kprobes: the 64 buckets of kprobe_table, with the probed symbol (or address) and the pre/post handler.
  • Keyboard notifiers (linux_keyboard_notifiers): keyboard_notifier_list, which receives every keystroke. Any callback outside the kernel is critical, since this is the kernel keylogger hook.
  • Terminal input (linux_check_tty): every terminal hands typed input to its line discipline's receive_buf / receive_buf2. The check walks tty_drivers and, because devpts pseudo-terminals (SSH sessions) sit in no driver table, also the ttys processes hold open, reached through file->private_data. A handler in no known code is critical: that is a terminal keylogger, passwords included. One in a module is flagged for review, since PPP, SLIP and Bluetooth UART line disciplines live in modules.
  • Netfilter (linux_netfilter): IPv4 and IPv6 hook entries of init_net at all five hook points, the usual place for magic-packet backdoors. Module-owned hooks are normal (iptables, conntrack), so only "no known code" is critical.
  • LSM: every list under security_hook_heads. The active security modules are named (apparmor, yama…), and any hook outside the kernel is critical, because built-in LSMs live in kernel text.
  • eBPF: programs in prog_idr with id, type and name. Tracing types (kprobe, tracepoint, tracing, perf_event, raw_tracepoint, lsm) are rated suspicious, which is where eBPF rootkits such as TripleCross and ebpfkit attach. cgroup and socket programs are routine.

For ftrace and kprobes, the rating depends on the owner: kernel is informational, a module is suspicious, and no known code is critical. Falco, Tetragon, sysdig, livepatch and EDR agents all produce ftrace, kprobe or eBPF entries. Identify the owner before calling a finding.

Kernel taint

tainted_mask is decoded into named flags: proprietary module, out-of-tree module, unsigned module, force-loaded, live patch and so on. Per module, the taint bits and sig_ok produce "out-of-tree" and "unsigned" flags in the module list. A freshly built rootkit on a signed distribution kernel usually taints the kernel as both. Acquisition modules (lime, avml, pmem, fmem) are labelled as such, so the tool that captured the image is not mistaken for a suspect.

What is not checked structurally

Listing the files the kernel itself has open has no structural equivalent here yet. Userland hooks, GOT and inline (linux_apihooks), are covered in Linux userland malware in memory.

Without an ISF, the on-demand plugins check_syscall, check_fop, check_afinfo, check_tty, check_lsm, netfilter, keyboard_notifiers, check_pidhash, bpf_progs and kernel_taint only sweep for symbol strings. They show which subsystems appear in memory, not whether a pointer was changed.

A practical order

  1. Read the taint flags and the module list first. An unsigned out-of-tree module on a distribution kernel is the lead.
  2. Check hidden tasks and hidden modules. Either one is enough to escalate.
  3. Go through syscall, IDT, ops-table and inline-hook results, and any handler owned by "no known code".
  4. Go through the hook frameworks, ruling out known agents by owner.
  5. For every finding, go back to the processes, sockets and files it was hiding.

Further reading