Werkzeug Debug Console PIN
When a Flask application runs with debug mode on, Werkzeug publishes an interactive Python console at /console. It is not open: it asks for a nine-digit PIN that the server prints on its standard output at startup. The idea is that only whoever can see the terminal gets in.
The problem is that the PIN is not random. Werkzeug derives it from a handful of system values, always the same ones, so anyone who can read files on the machine can compute it without seeing the terminal. An arbitrary file read, which on its own looks minor, turns here into Python execution as the server's user.
The mechanism. The PIN comes from two groups of ingredients:
- The probably public bits: the user running the process, the module name (
flask.app), the application's class name (Flask) and the absolute path offlask/app.pyin that installation. - The private bits: the network interface's MAC, converted to decimal, and the machine's machine-id.
Each one comes from a specific file, and all of them are readable through the same inclusion or traversal:
curl 'http://<ip>/<lfi>/..//proc/self/environ' --path-as-is
curl 'http://<ip>/<lfi>/..//sys/class/net/eth0/address' --path-as-is
curl 'http://<ip>/<lfi>/..//etc/machine-id' --path-as-isThree details decide whether the calculation works:
- The path of
flask/app.pydoes not need to be read: the debugger's own error page hands it over. Trigger any exception and the traceback prints something like/usr/local/lib/python3.10/site-packages/flask/app.py. - A container has no
/etc/machine-id. There the value is built from theboot_id(/proc/sys/kernel/random/boot_id) plus the container identifier, which comes from the/proc/self/cgroupline. - Old Werkzeug versions derive the PIN with MD5 and new ones with SHA-1. If the computed PIN is rejected, the first suspect is the algorithm, not the ingredients.
With the PIN, the /console prompt runs arbitrary Python in the application's context, which is a reverse shell away.
Requirements. Debug mode on (if /console answers with the PIN form, it is) and an arbitrary file read on the same machine: a path traversal, an unsanitised file-serving route or anything that returns the contents of a path the attacker chooses.
How to detect it. In the access log the sequence is distinctive: reads of /proc/self/environ, /sys/class/net/*/address, /etc/machine-id or /proc/sys/kernel/random/boot_id from the same IP, and shortly after, requests to /console and to ?__debugger__=yes&cmd=pinauth. Several failed PIN attempts also leave their trace in Werkzeug's own output. But the detection that really matters comes earlier: a Flask application serving with the debugger on in production is the finding, and a single request to /console reveals it from outside.
Defence. Never run Flask with debug=True outside the development machine: the PIN is a barrier against an oversight, not a security control, and it was meant to protect a console that should not be published at all. In production, serve through a real WSGI server (gunicorn, uWSGI) and leave FLASK_DEBUG=0. And fix the file read that makes it possible, which is the flaw everything else hangs from.
Machines where it's used
- OpenSourceHackTheBox