Skip to content

Hook cheat sheet: Nim ​

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

Nim ​

SetupUse this whenChangeHub automation
Nim: request tracing for std/asynchttpserverDepends on asynchttpserver (in its manifest)source, rebuildpartly

With source access ​

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

Nim: request tracing for std/asynchttpserver ​

  • Use this when: Nim service that depends on asynchttpserver (in its manifest).
  • Hub automation: partly. The hub pushes the compose lines; the source change is yours.
  • Check and undo: hook mounted into the container

Vendors vbxtrace.nim (+ vbxtrace/asynchttp.nim) and C1's vbxhook into the service; vbxtrace.nim compiles vbxhook.c itself. Importing vbxtrace/asynchttp instead of std/asynchttpserver reports every request with its exact per-connection index and status; ctx(req) with the vbxtrace helpers links child processes, files and outgoing connections. Needs the read-only hook mount at /opt/vbxtrace.

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/nim/ (vbxtrace.nim and vbxtrace/) into the service's source directory
  2. copy native/c/vbxhook.h, native/c/vbxhook.c and include/vbxtrace.h next to vbxtrace.nim (it compiles vbxhook.c with {.compile.}; other locations: -d:vbxhookC=<path> -d:vbxhookInc=<dir>)
  3. replace 'import std/asynchttpserver' (or asynchttpserver in an import list) with 'import vbxtrace, vbxtrace/asynchttp': serve, acceptRequest and respond become the tracing versions
  4. optional: startProcess(ctx(req), ...), open(ctx(req), ...), dialAsync(ctx(req), ...) where the handler starts processes, opens files or connects out
  5. keep the binary dynamically linked (no --passL:-static), or compile vbxhook.c with -DVBXHOOK_STATIC and link libvbxtrace-<arch>.a
  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/nim-asynchttpserver.diff):

diff
Adds vulnboxd request tracing to a std/asynchttpserver service (Nim 2.2). Vendor first (not shown):
  vbxtrace.nim, vbxtrace/     <- /usr/local/lib/vulnboxd/trace/native/nim/
  vbxhook.h, vbxhook.c        <- /usr/local/lib/vulnboxd/trace/native/c/
  vbxtrace.h                  <- /usr/local/lib/vulnboxd/trace/include/vbxtrace.h
all next to server.nim; vbxtrace.nim compiles vbxhook.c itself ({.compile.}).

diff --git a/docker-compose.yml b/docker-compose.yml
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -3,3 +3,5 @@
     build: .
     ports:
       - "8080:8080"
+    volumes:
+      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
diff --git a/server.nim b/server.nim
--- a/server.nim
+++ b/server.nim
@@ -1,13 +1,14 @@
-import std/[asyncdispatch, asynchttpserver, osproc]
+import std/[asyncdispatch, osproc]
+import vbxtrace, vbxtrace/asynchttp   # vulnboxd request tracing (replaces std/asynchttpserver)
 
 proc cb(req: Request) {.async, gcsafe.} =
   case req.url.path
   of "/ping":
-    let p = startProcess("/bin/ping", args = ["-c", "1", req.url.query], options = {poUsePath})
+    let p = startProcess(ctx(req), "/bin/ping", args = ["-c", "1", req.url.query], options = {poUsePath})
     discard p.waitForExit()
     await req.respond(Http200, p.outputStream.readAll())
   of "/save":
-    let f = open("/data/" & req.url.query, fmWrite)
+    let f = open(ctx(req), "/data/" & req.url.query, fmWrite)
     f.write(req.body)
     f.close()
     await req.respond(Http200, "saved\n")
Exact detection rules

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

  • Image matches (regex): ^nimlang/nim, nim
  • Command matches (regex): \bnim\s+(c|compile|cpp)\b, \bnimble\s+build\b
  • Dependency on: asynchttpserver
  • Files: *.nimble, nim.cfg, config.nims, *.nim, src/*.nim

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.