the_open_door
coffee
ESEN
← Back to index

Werkzeug Debug Console PIN

ATT&CKT1190

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:

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-is

Three details decide whether the calculation works:

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.

Levelintermediate

Machines where it's used