Python client libraries with C-level sockets (P7)
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.
Optional precision for Python services that talk to databases or HTTP servers through C libraries: psycopg2 and psycopg 3 (libpq), mysqlclient (libmysqlclient / libmariadb) and pycurl (libcurl) open their sockets in C, where P1's socket wrappers cannot see them. P7 reports those connections — with the local port the sensor joins on — for the request that made them. It gets in with the same compose-only change as P1.
Covers
| Library | Versions | Reported |
|---|---|---|
| psycopg2 / psycopg2-binary | 2.8+ | psycopg2.connect |
| psycopg 3 (C, binary and pure-Python implementations: libpq connects in every case) | 3.1+ | Connection.connect, AsyncConnection.connect |
mysqlclient (MySQLdb) | 2.x | MySQLdb.connect / Connection(...) |
| pycurl | 7.43+ | each Curl.perform that opened a new connection |
Already covered by P1 (pure-Python sockets), verified: redis-py (with and without hiredis — hiredis only parses, the socket is Python's), PyMySQL, pymongo, asyncpg, aiomysql, requests / urllib3, http.client, httpx, aiohttp's client.
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, 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. The hub proposes it (hooks/python/p7/proposals/compose.toml) when it detects psycopg2, psycopg2-binary, psycopg, psycopg-binary, psycopg-c, mysqlclient or pycurl among the dependencies.
What it records
| Event | Emitted from | Join key |
|---|---|---|
context_enter / context_exit | around the blocking connect (psycopg2.connect, psycopg's sync Connection.connect, MySQLdb Connection.__init__, Curl.perform): the thread does nothing but this request's connect while blocked, so the kernel's DNS lookups and connect on that thread link exactly | thread |
connected | after the connect returns: the connection's fd (conn.fileno()) is read with getsockname/getpeername through a temporary socket object that is detached again (the library's fd is never closed or duplicated); pycurl: CURLINFO_LOCAL_PORT / PRIMARY_IP / PRIMARY_PORT when CURLINFO_NUM_CONNECTS > 0 (a reused connection was reported when it was opened) | local port (+ peer, tcp) |
psycopg's AsyncConnection.connect gets connected only (no enter/exit: the event loop keeps serving other requests while libpq connects). Unix-socket connections (host=/var/run/postgresql, MySQL's localhost) have no port: only the enter/exit pair.
Context following
Nothing of its own: the connect runs in the request's thread, task or greenlet, whose id P1 and the framework packages maintain.
Known gaps
- Connection pools (
psycopg_pool, SQLAlchemy's pool, Django's persistent connections) connect once and reuse: the connection is reported for the request that opened it; later requests using it are linked by the sensor through the local port only if it can tell which request used the pooled socket, which a hook cannot know either. - pycurl's
CurlMulti(many transfers driven together) is not covered;Curl.performis. pycurl.Curlis replaced by a transparent subclass soperformcan be wrapped (the C type's methods cannot be);type(c) is pycurl.Curlandisinstancestill hold for objects created afterwards.- Other C clients (
oracledbthick mode,pyodbc,grpcio,confluent-kafka) are not covered: kernel only.
Safety
- Wrapped:
psycopg2.connect,psycopg.Connection.connect,psycopg.AsyncConnection.connect,MySQLdb.connections.Connection.__init__,pycurl.Curl.perform(subclass). Each calls the original first with the same arguments and returns / raises what it does (also whenperformraises, the enter/exit pair is closed); the binding's work is guarded, counted, and switched off after 5 internal errors.VBXTRACE=0installs nothing. - The hooks are only installed when the program imports the library.
- Chaos runs (every event call failing) give byte-identical responses on the 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. Removing the PYTHONPATH entry and the mount removes all Python packages at once.
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_clients. - Which links did a flow get? The hub shows each link's method; this package's are
hook:P7. - No
connectedfor the database: check the connection uses TCP (host=127.0.0.1/ a service name), not a unix socket.
Sample results
hooks/run-samples.sh python --package p7 — PostgreSQL, MariaDB and Redis run inside the container; an http.server app (requests marked by P6) opens a fresh connection per route.
| Sample | What | Python | events | chaos / baseline |
|---|---|---|---|---|
| clients | psycopg2, psycopg 3, mysqlclient, redis-py + hiredis, pycurl (fetching from the app itself) | 3.12 | 27 match | byte-identical |