Skip to content

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 ​

PythonKernel alone
socketserver.ForkingTCPServer / ForkingMixIn3.8–3.14exact (process per connection)
socketserver.ThreadingTCPServer / ThreadingMixIn3.8–3.14exact per thread, unless the handler hands work off
socketserver.TCPServer (one at a time), UDP servers3.8–3.14exact while one connection is open
http.server (HTTPServer, ThreadingHTTPServer), Werkzeug dev server, wsgiref, xmlrpc.server3.8–3.14as above; hook adds keep-alive request boundaries
hand-rolled select / selectors / epoll loops3.8–3.14not 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:

diff
 services:
   <service>:
+    volumes:
+      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
+    environment:
+      PYTHONPATH: /opt/vbxtrace/python

Append 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:

diff
 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 ​

EventEmitted fromJoin key
lazy raw sessionsocketserver.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_endhttp.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.closepeer address and port, request index
request_begin / request_endvbxtrace_shim.Session (source change): created = begin, close() = end, rawpeer address and port
spawned, connected, openedP1child 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).
  • socketserver handlers that pass the socket to a worker thread of their own: use vbxtrace.handoff() / adopt() there.
  • http.server requests answered with an error before parse_request succeeds (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.handle and ServerHandler.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=0 installs 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.so lists them; sudo bpftrace -l 'usdt:/usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.so:*' too.
  • Is the hook loaded? VBXTRACE_DEBUG=1 prints installed fw_socketserver; python -c 'import vbxtrace_shim as v; print(v.session.__module__)' prints vbxtrace when 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.

SampleWhatPythoneventschaos / baseline
socketserverThreadingHTTPServer (keep-alive scenario), threading and forking line servers3.924 matchbyte-identical
selectorsselectors loop with the shim3.13 Alpine (musl)8 matchbyte-identical