Skip to content

Hook cheat sheet: Python ​

How to add a vulnboxd runtime hook to a Python service by hand. Find your setup in the table, then follow its block.

  • Without source access: only the compose file changes; redeploy. With source access: you change the code or Dockerfile and rebuild.
  • Hub automation says how much the hub's proposals do for you. Fully: approve the proposal in the hub; the block is the fallback when it failed. With conditions: the same, when the conditions listed hold. Partly: the hub pushes the compose part, the rest is yours. Not at all: everything is by hand.
  • /usr/local/lib/vulnboxd/trace is the hook directory vulnboxd installs on the vulnbox. Compose snippets mount it read-only at /opt/vbxtrace in the container.
  • How to check that the hook works and undo it is at the end of the page.
  • Runtime hooks explains what a hook does.

Python ​

SetupUse this whenChangeHub automation
Python runtime hook (PYTHONPATH)Started with python, pypy, gunicorn, uvicorn, hypercorn or 5 morecomposefully
Python WSGI hook (gunicorn, waitress, Werkzeug, CherryPy, ...)Depends on gunicorn, waitress, werkzeug, flask, django or 6 more (in requirements.txt, pyproject.toml or a lockfile)composefully
Python ASGI hook (uvicorn, hypercorn, daphne)Depends on uvicorn, hypercorn, daphne, granian, fastapi or 5 more (in requirements.txt, pyproject.toml or a lockfile)composefully
Python asyncio hook (aiohttp, Sanic, Tornado, Twisted, raw asyncio/trio)Depends on aiohttp, sanic, tornado, twisted, trio or 1 more (in requirements.txt, pyproject.toml or a lockfile)composefully
Python gevent / eventlet hookDepends on gevent or eventlet (in requirements.txt, pyproject.toml or a lockfile)composefully
Python socketserver / http.server hookStarted with python -m http.servercomposefully
Python C-level database/HTTP clients (psycopg, mysqlclient, pycurl)Depends on psycopg2, psycopg2-binary, psycopg, psycopg-binary, psycopg-c or 2 more (in requirements.txt, pyproject.toml or a lockfile)composefully
Mark connections in a hand-rolled event loop (vbxtrace_shim.py)With *.py filessource, rebuildnot at all

Without source access ​

Only the compose file changes: the image and the code stay as they are.

Python runtime hook (PYTHONPATH) ​

  • Use this when: Python service started with python, pypy, gunicorn, uvicorn, hypercorn or 5 more.
  • Hub automation: fully. The hub proposes exactly this change and pushes it (one commit per container) once you approve it. Do it by hand only when the proposal failed, e.g. on an unusual compose file.
  • Check and undo: hook mounted into the container

Mount vulnboxd's hook directory read-only and add /opt/vbxtrace/python to PYTHONPATH: child processes, outgoing connections and file opens are linked to the request that caused them. Image and source stay untouched.

Compose: merge into the service (app is its name) and redeploy:

yaml
services:
  app:
    volumes:
      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
    environment:
      PYTHONPATH: "/opt/vbxtrace/python"   # already set? append, separated by ":"
Exact detection rules

Package P1 (Python core binding), proposal P1:compose, defined in hooks/python/p1/proposals/compose.toml.

  • Image matches (regex): ^(docker\.io/)?(library/)?python:, ^(docker\.io/)?(library/)?pypy:, ^ghcr\.io/astral-sh/uv, ^tiangolo/
  • Command matches (regex): \bpython[0-9.]*\b, \bpypy[0-9.]*\b, \bgunicorn\b, \buvicorn\b, \bhypercorn\b, \bdaphne\b, \bgranian\b, \bwaitress-serve\b, \buwsgi\b, \bflask\b
  • Files: requirements*.txt, pyproject.toml, Pipfile, Pipfile.lock, poetry.lock, uv.lock, setup.py, setup.cfg, *.py

Python WSGI hook (gunicorn, waitress, Werkzeug, CherryPy, ...) ​

  • Use this when: Python service that depends on gunicorn, waitress, werkzeug, flask, django or 6 more (in requirements.txt, pyproject.toml or a lockfile).
  • Hub automation: fully. The hub proposes exactly this change and pushes it (one commit per container) once you approve it. Do it by hand only when the proposal failed, e.g. on an unusual compose file.
  • Check and undo: hook mounted into the container

Request boundaries for WSGI servers (gunicorn, waitress, cheroot/CherryPy, Werkzeug, wsgiref) and every framework on them, with the same compose change as the Python runtime hook.

Compose: merge into the service (app is its name) and redeploy:

yaml
services:
  app:
    volumes:
      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
    environment:
      PYTHONPATH: "/opt/vbxtrace/python"   # already set? append, separated by ":"
Exact detection rules

Package P2 (WSGI servers and frameworks), proposal P2:compose, defined in hooks/python/p2/proposals/compose.toml.

  • Command matches (regex): \bgunicorn\b, \bwaitress-serve\b, \bflask\s+run\b, manage\.py\s+runserver, \bcherryd\b
  • Dependency on: gunicorn, waitress, werkzeug, flask, django, bottle, pyramid, falcon, web.py, cherrypy, cheroot
  • Files: requirements*.txt, pyproject.toml, Pipfile.lock, poetry.lock, uv.lock, wsgi.py, manage.py

Python ASGI hook (uvicorn, hypercorn, daphne) ​

  • Use this when: Python service that depends on uvicorn, hypercorn, daphne, granian, fastapi or 5 more (in requirements.txt, pyproject.toml or a lockfile).
  • Hub automation: fully. The hub proposes exactly this change and pushes it (one commit per container) once you approve it. Do it by hand only when the proposal failed, e.g. on an unusual compose file.
  • Check and undo: hook mounted into the container

Request and websocket boundaries for ASGI servers (uvicorn, hypercorn, daphne, granian) and every framework on them (FastAPI, Starlette, Django ASGI, Quart, Litestar, BlackSheep, Sanic in ASGI mode).

Compose: merge into the service (app is its name) and redeploy:

yaml
services:
  app:
    volumes:
      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
    environment:
      PYTHONPATH: "/opt/vbxtrace/python"   # already set? append, separated by ":"
Exact detection rules

Package P3 (ASGI servers and frameworks), proposal P3:compose, defined in hooks/python/p3/proposals/compose.toml.

  • Image matches (regex): ^tiangolo/uvicorn-gunicorn
  • Command matches (regex): \buvicorn\b, \bhypercorn\b, \bdaphne\b, \bgranian\b, \bfastapi\s+(run|dev)\b, uvicorn\.workers\.UvicornWorker
  • Dependency on: uvicorn, hypercorn, daphne, granian, fastapi, starlette, quart, litestar, blacksheep, channels
  • Files: requirements*.txt, pyproject.toml, Pipfile.lock, poetry.lock, uv.lock, asgi.py

Python asyncio hook (aiohttp, Sanic, Tornado, Twisted, raw asyncio/trio) ​

  • Use this when: Python service that depends on aiohttp, sanic, tornado, twisted, trio or 1 more (in requirements.txt, pyproject.toml or a lockfile).
  • Hub automation: fully. The hub proposes exactly this change and pushes it (one commit per container) once you approve it. Do it by hand only when the proposal failed, e.g. on an unusual compose file.
  • Check and undo: hook mounted into the container

Request boundaries for aiohttp, Sanic, Tornado and twisted.web, and per-connection sessions for raw asyncio, trio, anyio and Twisted protocol servers.

Compose: merge into the service (app is its name) and redeploy:

yaml
services:
  app:
    volumes:
      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
    environment:
      PYTHONPATH: "/opt/vbxtrace/python"   # already set? append, separated by ":"
Exact detection rules

Package P4 (Other asyncio frameworks and raw asyncio), proposal P4:compose, defined in hooks/python/p4/proposals/compose.toml.

  • Command matches (regex): \btwistd\b, \bsanic\b
  • Dependency on: aiohttp, sanic, tornado, twisted, trio, anyio
  • Files: requirements*.txt, pyproject.toml, Pipfile.lock, poetry.lock, uv.lock, *.py

Python gevent / eventlet hook ​

  • Use this when: Python service that depends on gevent or eventlet (in requirements.txt, pyproject.toml or a lockfile).
  • Hub automation: fully. The hub proposes exactly this change and pushes it (one commit per container) once you approve it. Do it by hand only when the proposal failed, e.g. on an unusual compose file.
  • Check and undo: hook mounted into the container

Keeps the hook working after gevent or eventlet monkey patching, carries request context into spawned greenlets, and marks gevent.pywsgi / eventlet.wsgi requests.

Compose: merge into the service (app is its name) and redeploy:

yaml
services:
  app:
    volumes:
      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
    environment:
      PYTHONPATH: "/opt/vbxtrace/python"   # already set? append, separated by ":"
Exact detection rules

Package P5 (gevent / eventlet), proposal P5:compose, defined in hooks/python/p5/proposals/compose.toml.

  • Command matches (regex): -k\s*(gevent|eventlet)\b, --worker-class[= ](gevent|eventlet)\b
  • Dependency on: gevent, eventlet
  • Files: requirements*.txt, pyproject.toml, Pipfile.lock, poetry.lock, uv.lock

Python socketserver / http.server hook ​

  • Use this when: Python service started with python -m http.server.
  • Hub automation: fully. The hub proposes exactly this change and pushes it (one commit per container) once you approve it. Do it by hand only when the proposal failed, e.g. on an unusual compose file.
  • Check and undo: hook mounted into the container

Sessions for socketserver handlers and request boundaries for http.server (also Werkzeug's development server and wsgiref). Forking and threading servers are linked by the kernel already; this adds exact boundaries.

Compose: merge into the service (app is its name) and redeploy:

yaml
services:
  app:
    volumes:
      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
    environment:
      PYTHONPATH: "/opt/vbxtrace/python"   # already set? append, separated by ":"
Exact detection rules

Package P6 (Python raw socket servers), proposal P6:compose, defined in hooks/python/p6/proposals/compose.toml.

  • Command matches (regex): \bpython[0-9.]*\s+-m\s+http\.server\b
  • Files: *.py

Python C-level database/HTTP clients (psycopg, mysqlclient, pycurl) ​

  • Use this when: Python service that depends on psycopg2, psycopg2-binary, psycopg, psycopg-binary, psycopg-c or 2 more (in requirements.txt, pyproject.toml or a lockfile).
  • Hub automation: fully. The hub proposes exactly this change and pushes it (one commit per container) once you approve it. Do it by hand only when the proposal failed, e.g. on an unusual compose file.
  • Check and undo: hook mounted into the container

Links connections that psycopg2, psycopg 3, mysqlclient and pycurl open in C to the request that made them.

Compose: merge into the service (app is its name) and redeploy:

yaml
services:
  app:
    volumes:
      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
    environment:
      PYTHONPATH: "/opt/vbxtrace/python"   # already set? append, separated by ":"
Exact detection rules

Package P7 (Python client libraries (C-level sockets)), proposal P7:compose, defined in hooks/python/p7/proposals/compose.toml.

  • Dependency on: psycopg2, psycopg2-binary, psycopg, psycopg-binary, psycopg-c, mysqlclient, pycurl
  • Files: requirements*.txt, pyproject.toml, Pipfile.lock, poetry.lock, uv.lock

With source access ​

These change the service's code or Dockerfile, then it is rebuilt.

Mark connections in a hand-rolled event loop (vbxtrace_shim.py) ​

  • Use this when: Python service with *.py files.
  • Hub automation: not at all. The hub lists these steps with its proposal but changes nothing.
  • Check and undo: hook built into the service

Vendor vbxtrace_shim.py next to the service and wrap each connection's work in a vbxtrace session; the shim is a no-op when the hook is not mounted.

Source change, then rebuild:

  1. copy hooks/python/p6/shim/vbxtrace_shim.py (installed at /usr/local/lib/vulnboxd/trace/python-shim/vbxtrace_shim.py) next to the service's main module
  2. import vbxtrace_shim as vbx
  3. on accept: session = vbx.Session(conn); keep it with the connection's state
  4. around every piece of work for that connection: with session: ...
  5. when the connection closes: session.close()
  6. for thread- or process-per-connection handlers instead: with vbx.session(conn): handle(conn)
  7. also apply the compose change (mount + PYTHONPATH)

Example change on a sample service (hooks/python/p6/proposals/selectors.diff):

diff
--- a/server.py
+++ b/server.py
@@ -1,6 +1,7 @@
 import selectors
 import socket
 
+import vbxtrace_shim as vbx
 
 sel = selectors.DefaultSelector()
 buffers = {}
@@ -12,18 +13,21 @@ while True:
         if key.fileobj is srv:
             conn, _ = srv.accept()
             conn.setblocking(False)
-            buffers[conn] = b""
+            buffers[conn] = [b"", vbx.Session(conn)]
             sel.register(conn, selectors.EVENT_READ)
             continue
         conn = key.fileobj
-        data = conn.recv(4096)
-        if not data:
-            sel.unregister(conn)
-            conn.close()
-            del buffers[conn]
-            continue
-        handle(conn, data)
+        with buffers[conn][1]:
+            data = conn.recv(4096)
+            if not data:
+                sel.unregister(conn)
+                conn.close()
+                buffers[conn][1].close()
+                del buffers[conn]
+                continue
+            handle(conn, data)
Exact detection rules

Package P6 (Python raw socket servers), proposal P6:source-session, defined in hooks/python/p6/proposals/source-session.toml.

  • Files: *.py

Check that it works, and undo ​

Each block links the variant it needs.

Hook mounted into the container ​

Check: After the redeploy, this lists the processes that loaded the hook (empty: it is not loaded):

sh
docker compose exec app sh -c 'grep -l vbxtrace /proc/[0-9]*/maps'

The hub's Trace tab then shows links whose method starts with hook:.

Undo: Set VBXTRACE: "0" in the service's environment and restart it, or revert the commit (the hub's proposal screen has a revert per container). Remove the mount of /opt/vbxtrace only together with what loads from it: a runtime told to preload a missing file may refuse to start.

Hook built into the service ​

Check: After the rebuild, the binary carries the hook's probes (a count above 0):

sh
readelf -n <the built binary> | grep -c vbxtrace

The hub's Trace tab then shows links whose method starts with hook:.

Undo: Set VBXTRACE: "0" in the service's environment and restart it, or revert the change.