Python Privilege Escalation
Python binary is vulnerable to privilege escalation in some situations.
If the python script is under the current user's home directory, we can remove the script and create the new one with the same name.
We can insert arbitrary code in the new script. For example,
Assume the python script can be executed as root with SETENV,NOPASSWD. For example,
With SETENV, we can change PYTHONPATH when executing the script, and insert malicious script to the module which is imported in the script. First off, check what module is imported in the python script (e.g. /opt/example.py here).
We can forge the imported module.
The content of the module is below. This is a script that executes reverse shell.
Replace <local-ip>
with your local ip address.
After that, in another local terminal, start listener for getting a shell.
Then run the python script with updating PYTHONPATH in the remote machine.
By setting "PYTHONPATH=/tmp/", the python script will import modules from /tmp/ directories so the "random" module is imported from /tmp/random.py. Finally, we should get a shell in local terminal.
If the Python script contains a module that can be modified by current user, we can inject arbitrary code into the module. First, check what modules the Python script uses.
Assume the “random” module is used in the script. Find the path of the module and check if it’s writable.
If we know we can modify it, inject arbitrary code in this module.
Assume the “random” module path is /usr/lib/python3.6/random.py
.
Then execute the Python script and we can spawn the root shell.
If you find the executable which is created in Python. For instance,
You can enter OS commands in some input.
For example,
Interective Python (IPython) is a command shell for interective computing in multiple programming languages.
References
Last updated