Python raw socket servers (P6)
Ready
The hub proposes this hook for the containers it fits, and nothing changes until a player approves the proposal (Hook proposals). The cheat sheet has the steps by hand.
For Python services built on the standard library's servers — socketserver (threading and forking TCP/UDP servers: the classic attack/defense line-protocol service), http.server (also the base of Werkzeug's development server, wsgiref and xmlrpc.server) — and for hand-rolled select / selectors loops. Threading and forking servers are already linked exactly by the kernel; P6 adds exact session and request boundaries. Hand-rolled loops serve many connections on one thread and need a source change (a vendored shim, a few lines); everything else gets in with the same compose-only change as P1.
Covers
| Python | Kernel alone | |
|---|---|---|
socketserver.ForkingTCPServer / ForkingMixIn | 3.8–3.14 | exact (process per connection) |
socketserver.ThreadingTCPServer / ThreadingMixIn | 3.8–3.14 | exact per thread, unless the handler hands work off |
socketserver.TCPServer (one at a time), UDP servers | 3.8–3.14 | exact while one connection is open |
http.server (HTTPServer, ThreadingHTTPServer), Werkzeug dev server, wsgiref, xmlrpc.server | 3.8–3.14 | as above; hook adds keep-alive request boundaries |
hand-rolled select / selectors / epoll loops | 3.8–3.14 | not linkable: one thread serves every connection |
How it gets in
From a registry (local development, or a build with network; the same release as the hook directory): pip install vbxtrace==1.0.0, then python -m vbxtrace app.py or import vbxtrace.auto first (no sitecustomize needed); see Local development.
Compose-only for socketserver and http.server, identical for every Python package:
services:
<service>:
+ volumes:
+ - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
+ environment:
+ PYTHONPATH: /opt/vbxtrace/pythonAppend with : to an existing PYTHONPATH. Proposed for any Python service (hooks/python/p6/proposals/compose.toml, plus P1's detection).
Source change for hand-rolled event loops (hooks/python/p6/proposals/source-session.toml): copy vbxtrace_shim.py (in the hook directory at /usr/local/lib/vulnboxd/trace/python-shim/, source hooks/python/p6/shim/) next to the service, plus the compose change above. The shim uses the hook when it is mounted and does nothing otherwise, so the service runs unchanged without it:
import selectors
import socket
+import vbxtrace_shim as vbx
...
if key.fileobj is srv:
conn, _ = srv.accept()
conn.setblocking(False)
- buffers[conn] = b""
+ buffers[conn] = [b"", vbx.Session(conn)] # request_begin
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]: # this connection's work
+ data = conn.recv(4096)
+ if not data:
+ sel.unregister(conn)
+ conn.close()
+ buffers[conn][1].close() # request_end
+ del buffers[conn]
+ continue
+ handle(conn, data)For thread- or process-per-connection code that is not socketserver: with vbx.session(conn): handle(conn).
What it records
| Event | Emitted from | Join key |
|---|---|---|
| lazy raw session | socketserver.BaseServer.finish_request — in the handler's thread (ThreadingMixIn) or the forked child (ForkingMixIn); TCP: the accepted socket's endpoints; UDP: one session per datagram, peer = the sender. Nothing is emitted unless the handler spawns, connects or opens a file (then request_begin first, request_end when the handler returns) | peer address and port (index 0) |
request_begin / request_end | http.server.BaseHTTPRequestHandler: a successful parse_request begins (index = the request's number on the connection, counted on the handler instance), handle_one_request returning ends; status from send_response (also send_error) or log_request; wsgiref: WSGIRequestHandler.handle returning, status from ServerHandler.close | peer address and port, request index |
request_begin / request_end | vbxtrace_shim.Session (source change): created = begin, close() = end, raw | peer address and port |
spawned, connected, opened | P1 | child pid, local port, pid + fd |
An HTTP request inside a socketserver connection drops that connection's lazy session (no double counting). In a forked child the context ids continue from the parent's counter; ids are per process, and the sensor keys them by pid.
Context following
Threading servers run each connection on its own thread, forking servers in its own process: the id is set there for the handler's duration. Threads the handler starts carry it (P1). With the shim, with session: sets the id for that block on the loop thread.
Known gaps
- Hand-rolled loops without the shim: kernel only (which cannot link them).
socketserverhandlers that pass the socket to a worker thread of their own: usevbxtrace.handoff()/adopt()there.http.serverrequests answered with an error beforeparse_requestsucceeds (414, malformed request line) are not counted; the server closes such connections.
Safety
- Wrapped:
socketserver.BaseServer.finish_request;http.server.BaseHTTPRequestHandler.parse_request,send_response,log_request,handle_one_request;wsgiref.simple_server.WSGIRequestHandler.handleandServerHandler.close. Each calls the original first with the same arguments and returns / raises what it does; the binding's work is guarded, counted, and switched off after 5 internal errors.VBXTRACE=0installs nothing (and makes the shim a no-op). - Chaos runs (every event call failing) give byte-identical responses on every sample.
- Overhead has not been measured yet; it will be measured on the sample services in the dedicated performance phase.
Turning it off
Set VBXTRACE=0 in the service's environment and restart it, or revert the change that added the hook; vulnboxd's rollback covers a hook that breaks a service. For the source change, reverting also removes vbxtrace_shim.py; leaving the shim in place without the mount is harmless.
Troubleshooting
- Are the probes there?
readelf -n /usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.solists them;sudo bpftrace -l 'usdt:/usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.so:*'too. - Is the hook loaded?
VBXTRACE_DEBUG=1printsinstalled fw_socketserver;python -c 'import vbxtrace_shim as v; print(v.session.__module__)'printsvbxtracewhen the shim found the hook. - Which links did a flow get? The hub shows each link's method; this package's are
hook:P6. - A session never appears: lazy sessions only appear when the connection spawns, connects or opens a file.
Sample results
hooks/run-samples.sh python --package p6.
| Sample | What | Python | events | chaos / baseline |
|---|---|---|---|---|
| socketserver | ThreadingHTTPServer (keep-alive scenario), threading and forking line servers | 3.9 | 24 match | byte-identical |
| selectors | selectors loop with the shim | 3.13 Alpine (musl) | 8 match | byte-identical |