Linux userland malware in memory: injection, GOT hooks and bash
7 min read
Most Linux intrusions never touch the kernel. A cryptominer dropped
in /tmp, a library preloaded into sshd to steal passwords, a reverse
shell living in a bash process: all of it happens in user space, and
all of it leaves structures in RAM that disk forensics cannot see once
the attacker deletes their files.
This post walks through the process-level checks ramparser runs on a
Linux image, using the technique names from The Art of Memory
Forensics (chapters 21, 25 and 26) and the matching Volatility
linux_* plugins. Everything below comes from the structured task walk,
which needs the kernel's ISF: task_struct, mm_struct, VMAs, file
descriptors and sockets are read with symbol-accurate offsets.
Libraries loaded from the wrong place
What the attacker does. Shared-library injection (ptrace plus
dlopen, or LD_PRELOAD) puts attacker code into a legitimate process.
The library has to come from somewhere writable, and it is often deleted
right after loading.
What exposes it. Every file-backed mapping in the process's VMA list
carries its path. ramparser collects each mapped .so and flags a
library whose path is in world-writable scratch space (/tmp,
/var/tmp, /dev/shm, /run/user/<uid>), in any dot-directory, that
ends in (deleted), or that is a memfd: object living only in memory.
A per-process LD_PRELOAD= in the environment is flagged separately.
Two system-wide checks target preload rootkits (Azazel, Jynx,
libprocesshider):
/etc/ld.so.preloadpresent and non-empty in the page cache. Stock systems do not have this file; the finding carries its size and modification time.- The same library from outside the system trees mapped into at least
three processes and 20% of all user processes.
/usr/localcounts as outside: packages never install there, and it is a favourite drop location.
False positives. Developer machines load libraries from build
directories; self-contained applications under /opt or /snap are
treated as system trees and do not trigger.
Process hollowing: the program is not mapped
Every process maps the executable it was started from. If the
mm_struct.exe_file names /usr/sbin/apache2 but no VMA is backed
by that file, the original image was unmapped and replaced. ramparser
flags this when the process has file mappings but none of them is its
own executable. A (deleted) binary that is still mapped is not
reported as hollowing; it is reported as running from a deleted file,
which is its own finding.
Anonymous executable memory (linux_malfind)
Code that was injected or unpacked at run time sits in memory with no
file behind it. ramparser marks every VMA that is executable, has no
backing file and is not the vDSO. These regions show in the process
memory map with an exec-private anomaly and add weight to the
process's suspicion score.
On its own this is a weak signal: JVMs, Node.js and browsers create such regions constantly. Its value comes from being combined with the two checks that follow.
Threads running in anonymous memory (linux_info_regs)
When a thread enters the kernel, its user registers are saved in a
pt_regs frame at the top of its kernel stack. ramparser locates that
frame for every user thread (trying both the 16 KiB and 8 KiB stack
sizes and keeping the candidate whose saved segment selectors are user
mode), then records the instruction pointer, stack pointer, the system
call the thread was in (from orig_ax, named through
sys_call_table) and whether it was 32-bit compat code.
The detection rule is simple: if a thread's saved instruction pointer
lies inside an anonymous executable region, that code was running at
capture time. A finding reads like
tid 801 at 0x7f00000008db (running). Miners that JIT their hashing
loops trip it too, which is still worth knowing.
GOT/PLT overwrites (linux_plthook)
A dynamically linked program calls library functions through its
Global Offset Table. Overwriting a slot redirects every call to
readdir, write or accept without touching any code page.
For every x86-64 ELF object mapped in a process, ramparser reads the
ELF header, follows PT_DYNAMIC to the DT_JMPREL relocations, reads
each R_X86_64_JUMP_SLOT slot and resolves the symbol name from
.dynsym. The target of each slot is then classified:
| Reason | Meaning |
|---|---|
anonymous | points into executable memory with no file behind it: injected code |
non-executable | points into heap, stack or any mapping without execute permission |
unmapped | points into no mapping at all |
foreign library | a system object's import resolves into a library outside the system folders: preload interposition |
Unresolved lazy-binding slots point back into the object's own PLT and
pass. Slots bound to the vDSO (gettimeofday, clock_gettime) pass.
Pages that are not resident are skipped rather than read as zeros, so a
paged-out GOT never becomes a fake hook to address 0. A hook into
anonymous memory scores as critical, e.g.
auth_password in sshd → [anon] (anonymous).
Inline hooks in library code (linux_apihooks)
A hook can also be written into the function itself: the first bytes
of readdir in libc are overwritten with a jump, and every caller
lands in the attacker's code, whatever its GOT says. For every mapped
ELF object, ramparser decodes the first instruction of each exported
function (STT_FUNC in .dynsym), with the same trampoline decoder as
the kernel check: jmp rel32, jmp [rip], push/ret and
movabs + jmp/push-ret, after an optional endbr64. The
destination is classified with the four reasons above. Only resident
pages are read; a patched page is a private copy, so it is resident
unless swapped out.
Libraries the loader does not know (linux_ldrmodules)
The dynamic loader keeps every library it loads, dlopen included, in
its link_map list, reached from the executable's DT_DEBUG entry.
ramparser walks that list and matches it to the mapped ELF objects by
load address, since link_map names are often symlinks. A library
whose code is mapped but absent from the list was mapped by hand or
unlinked to hide it. Static binaries have no list, and the check is
skipped when it is not resident.
Raw and packet sockets (linux_list_raw)
Sniffers, port-knocking backdoors and covert channels need to see
traffic below TCP and UDP. ramparser flags any process holding an
AF_PACKET socket or an inet/inet6 socket of type raw. Daemons
that legitimately do this (DHCP clients, systemd-networkd,
NetworkManager, ping, lldpd, chronyd and similar) are allow-listed,
and netlink sockets are ignored.
Data queued on sockets (linux_pkt_queues)
For every socket, ramparser walks the sk_receive_queue and
sk_write_queue rings of sk_buffs and records how many packets and
bytes were waiting, plus the first bytes of the first buffer. In the
network view a TCP or UDP row then shows, for example,
412 B unread, with a preview on hover. Unread bytes on a C2 socket
can be the next command; unsent bytes can be exfiltrated data that
never reached the wire.
bash: hash table and history (linux_bash_hash, linux_bash)
For every process whose executable is bash, ramparser searches the
[heap] mappings for two structures.
The command hash table. bash remembers which file each typed
command resolved to (hash -l). ramparser looks for a HASH_TABLE
with 64 buckets whose entry count matches its chains, then reads each
command, its resolved path and hit count. A command resolved into
/tmp, /dev/shm, a dot-directory or a deleted file flags the shell,
even when the history was wiped: nc → /tmp/.x/nc (3×).
The history list. bash stamps each entry with a #<epoch> string.
ramparser finds those timestamp strings on the heap, then the
HIST_ENTRY structures pointing at them, and reads the command line
next to each. Lines that bash loaded from ~/.bash_history at start-up
all share the load time; when five or more entries of one shell carry
the same stamp, they are labelled as loaded from the file and given no
execution time, so the timeline is not polluted. Recovered commands
feed the same command list as audit records, where downloads into
/tmp and similar patterns are tagged.
Reading the result
These rules produce one ranked suspect list, not a pile of tables.
Each reason carries a weight and its evidence. Other process rules
contribute to the same score: a shell whose stdin/stdout is a socket, a
shell spawned by a web server, a user process named like a kernel
thread, a working directory in a hidden folder. A process that loads a
library from /dev/shm, has a thread executing anonymous code and a
hooked GOT tells a consistent story. One JIT region in a Java process
does not.