21.Python-library-hijacking
Check SUID/SGID Script Permissions
Check Script Contents
Find Library Location
Check Library Permissions
Modify Library (Insert Malicious Code)
Run Script (With sudo if needed)
Key Concepts:
Python Libraries:
Modules that extend Python's functionality.
Examples: NumPy, Pandas, psutil.
Import Mechanism:
Python searches for modules in a specific order.
sys.path
shows the search path.
Hijacking Techniques:
Wrong write permissions on library files.
Manipulating the library search path.
Using the
PYTHONPATH
environment variable.
SUID/SGID:
Scripts with these bits set run with the owner's/group's privileges.
Attack Vectors and Exploitation:
Wrong Write Permissions:
If a library file is writable by the attacker, they can modify it.
Inject malicious code into the library.
If a SUID/SGID script imports the library, the malicious code runs with elevated privileges.
Library Path Manipulation:
Python searches for libraries in a specific order (defined by
sys.path
).If a higher-priority directory is writable, the attacker can place a malicious library there.
Python will import the malicious library instead of the legitimate one.
PYTHONPATH Environment Variable:
The
PYTHONPATH
variable allows specifying additional library search paths.If the attacker can control
PYTHONPATH
(e.g., viasudo
), they can redirect Python to a malicious directory.
Exploitation Steps (as described):
Wrong Write Permissions:
Identify a SUID/SGID Python script.
Identify the imported library.
Check the library's permissions.
Modify the library to inject malicious code.
Run the script.
Library Path Manipulation:
Identify a Python script that imports a library.
Check
sys.path
to determine the library search order.Identify a writable directory with higher priority.
Create a malicious library in that directory.
Run the script.
PYTHONPATH Environment Variable:
Identify a Python script that imports a library.
Check sudo -l to see if you can set environment variables for the python binary.
Create a malicious library.
Set
PYTHONPATH
to point to the malicious directory.Run the script.
Important Considerations and Enhancements:
Security Best Practices:
Use proper file permissions.
Avoid writable directories in the library search path.
Restrict
sudo
access.Do not allow the setting of environment variables via sudo, unless absolutely needed.
Attack Scenarios:
Developer environments.
Web applications.
Automation scripts.
Mitigation:
Regularly audit file permissions.
Use virtual environments.
Use package managers (e.g.,
pip
) to install libraries.Use tools like
bandit
to scan python code for vulnerabilities.
Detection:
Monitor file system changes.
Use intrusion detection systems.
Monitor for unexpected Python library imports.
Real world examples: Researching real world python library hijacking exploits will help solidify understanding of the attack vectors.
Virtual Environments: Emphasize the importance of virtual environments for python development.
Pip Security: Emphasize the importance of only installing python packages from trusted sources.
Last updated