Skip to content

Hook cheat sheet: Zig ​

How to add a vulnboxd runtime hook to a Zig 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.

Zig ​

SetupUse this whenChangeHub automation
Zig: request tracing for std.http.ServerStarted with zig build, zig build-exe or zig runsource, rebuildpartly

With source access ​

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

Zig: request tracing for std.http.Server ​

  • Use this when: Zig service started with zig build, zig build-exe or zig run.
  • Hub automation: partly. The hub pushes the compose lines; the source change is yours.
  • Check and undo: hook mounted into the container

Vendors vbxtrace.zig and C1's vbxhook into the service, compiles vbxhook.c into the executable and serves each connection through vbxtrace.serve (request boundaries per keep-alive request); spawn/file/connect helpers link child processes, files and outgoing connections to the request. Needs the read-only hook mount at /opt/vbxtrace and a dynamically linked executable.

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

yaml
services:
  app:
    volumes:
      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro

2. Source change, then rebuild:

  1. copy /usr/local/lib/vulnboxd/trace/native/zig/vbxtrace.zig into the service's source directory (next to main.zig: Zig imports only files inside the module's directory)
  2. copy native/c/vbxhook.h, native/c/vbxhook.c and include/vbxtrace.h into ./vbxtrace/
  3. build.zig: exe.linkLibC(); exe.addIncludePath(b.path("vbxtrace")); exe.addCSourceFile(.{ .file = b.path("vbxtrace/vbxhook.c") }); keep the executable dynamically linked (on musl: .linkage = .dynamic). Command line builds: add -lc -dynamic -I vbxtrace vbxtrace/vbxhook.c
  4. replace the per-connection std.http.Server loop with vbx.serve(conn, &buf, ctx, handler), or keep the loop and use vbx.HttpConnection.init / receiveHead / req.respond / req.end
  5. optional: use vbx.spawnAndWait / vbx.createFile / vbx.tcpConnectToAddress / vbx.spawnThread where the handler starts processes, opens files, connects out or hands work to another thread
  6. add the read-only mount of /usr/local/lib/vulnboxd/trace at /opt/vbxtrace to the service in docker-compose.yml

Example change on a sample service (hooks/native/c2/proposals/zig-stdhttp.diff):

diff
Adds vulnboxd request tracing to a std.http.Server service (Zig 0.14.1). Vendor first (not shown):
  src/vbxtrace.zig          <- /usr/local/lib/vulnboxd/trace/native/zig/vbxtrace.zig
  vbxtrace/vbxhook.h, .c    <- /usr/local/lib/vulnboxd/trace/native/c/
  vbxtrace/vbxtrace.h       <- /usr/local/lib/vulnboxd/trace/include/vbxtrace.h
and mount /usr/local/lib/vulnboxd/trace at /opt/vbxtrace (read-only) in docker-compose.yml.

--- a/build.zig
+++ b/build.zig
@@ -9,5 +9,11 @@
         .target = target,
         .optimize = optimize,
     });
+    // vulnboxd request tracing (vbxtrace): vbxhook.c dlopen()s /opt/vbxtrace at run time, so link libc
+    // dynamically.
+    exe.linkLibC();
+    exe.linkage = .dynamic;
+    exe.addIncludePath(b.path("vbxtrace"));
+    exe.addCSourceFile(.{ .file = b.path("vbxtrace/vbxhook.c") });
     b.installArtifact(exe);
 }
--- a/src/main.zig
+++ b/src/main.zig
@@ -1,11 +1,12 @@
 const std = @import("std");
+const vbx = @import("vbxtrace.zig");
 
-fn handle(req: *std.http.Server.Request) !void {
-    if (std.mem.startsWith(u8, req.head.target, "/note/")) {
-        const name = req.head.target[6..];
+fn handle(_: void, req: *vbx.Request) !void {
+    if (std.mem.startsWith(u8, req.inner.head.target, "/note/")) {
+        const name = req.inner.head.target[6..];
         var dir = try std.fs.cwd().makeOpenPath("data", .{});
         defer dir.close();
-        const file = try dir.createFile(name, .{});
+        const file = try vbx.createFile(req.ctx, dir, name, .{});
         defer file.close();
         try file.writeAll("saved\n");
         return req.respond("ok\n", .{});
@@ -14,13 +15,8 @@
 }
 
 fn connection(conn: std.net.Server.Connection) void {
-    defer conn.stream.close();
     var buf: [8192]u8 = undefined;
-    var server = std.http.Server.init(conn, &buf);
-    while (server.state == .ready) {
-        var req = server.receiveHead() catch return;
-        handle(&req) catch return;
-    }
+    vbx.serve(conn, &buf, {}, handle);
 }
 
 pub fn main() !void {
Exact detection rules

Package C2 (Zig, Nim, Crystal, D, Odin, V), proposal C2:zig, defined in hooks/native/c2/proposals/zig.toml.

  • Image matches (regex): zig
  • Command matches (regex): \bzig\s+(build|build-exe|run)\b
  • Files: build.zig, build.zig.zon, *.zig, src/*.zig

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.