Hook cheat sheet: Crystal
How to add a vulnboxd runtime hook to a Crystal 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/traceis the hook directory vulnboxd installs on the vulnbox. Compose snippets mount it read-only at/opt/vbxtracein 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.
Crystal
| Setup | Use this when | Change | Hub automation |
|---|---|---|---|
| Crystal: request tracing for HTTP::Server, Kemal, Lucky | Depends on kemal, lucky, amber, athena or grip (in its manifest) | source, rebuild | partly |
With source access
These change the service's code or Dockerfile, then it is rebuilt.
Crystal: request tracing for HTTP::Server, Kemal, Lucky
- Use this when: Crystal service that depends on
kemal,lucky,amber,athenaorgrip(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.cr and C1's vbxhook into src/vbxtrace/, builds vbxhook.o in the Dockerfile, and puts Vbxtrace::Handler first in the handler chain: every request is reported with its exact per-connection index (counted on the connection's fiber) and status; Vbxtrace.process_run / file_open / tcp_connect link child processes, files and outgoing connections. Needs the read-only hook mount at /opt/vbxtrace and a dynamically linked binary.
1. Compose: merge into the service (app is its name) and redeploy:
services:
app:
volumes:
- /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro2. Source change, then rebuild:
- copy
/usr/local/lib/vulnboxd/trace/native/crystal/vbxtrace.crtosrc/vbxtrace/vbxtrace.cr, andnative/c/vbxhook.h,native/c/vbxhook.candinclude/vbxtrace.hintosrc/vbxtrace/ Dockerfile, beforecrystal build/shards build:RUN cc -O2 -c -Isrc/vbxtrace -o src/vbxtrace/vbxhook.o src/vbxtrace/vbxhook.c(the image needs a C compiler; the officialcrystallang/crystalimages have one)require "./vbxtrace/vbxtrace"in the main file (afterrequire "http/server"/"kemal")HTTP::Server.new([Vbxtrace::Handler.new, ...])— Kemal:use Vbxtrace::Handler.new, 0beforeKemal.run(older Kemal:add_handler) — Lucky:Vbxtrace::Handler.newfirst in the middleware array ofsrc/app_server.cr- optional:
Vbxtrace.process_run/Vbxtrace.process,Vbxtrace.file_open,Vbxtrace.tcp_connect,Vbxtrace.spawnwhere handlers start processes, open files, connect out or spawn fibers - drop
--staticfromcrystal build(or compilevbxhook.cwith-DVBXHOOK_STATICand linklibvbxtrace-<arch>.a) - add the read-only mount of
/usr/local/lib/vulnboxd/traceat/opt/vbxtraceto the service indocker-compose.yml
Example change on a sample service (hooks/native/c2/proposals/crystal-http.diff):
Adds vulnboxd request tracing to a Crystal HTTP::Server service (also Kemal: use Vbxtrace::Handler.new, 0).
Vendor first (not shown):
src/vbxtrace/vbxtrace.cr <- /usr/local/lib/vulnboxd/trace/native/crystal/vbxtrace.cr
src/vbxtrace/vbxhook.h, vbxhook.c <- /usr/local/lib/vulnboxd/trace/native/c/
src/vbxtrace/vbxtrace.h <- /usr/local/lib/vulnboxd/trace/include/vbxtrace.h
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/Dockerfile b/Dockerfile
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,5 +1,7 @@
FROM crystallang/crystal:1.17.1-alpine
WORKDIR /app
COPY . .
-RUN crystal build --release -o /app/notes src/server.cr
+# vulnboxd request tracing: vbxhook.c (vendored in src/vbxtrace/) becomes the object vbxtrace.cr links
+RUN cc -O2 -c -Isrc/vbxtrace -o src/vbxtrace/vbxhook.o src/vbxtrace/vbxhook.c \
+ && crystal build --release -o /app/notes src/server.cr
CMD ["/app/notes"]
diff --git a/src/server.cr b/src/server.cr
--- a/src/server.cr
+++ b/src/server.cr
@@ -1,13 +1,14 @@
require "http/server"
+require "./vbxtrace/vbxtrace" # vulnboxd request tracing
-server = HTTP::Server.new([HTTP::LogHandler.new]) do |ctx|
+server = HTTP::Server.new([Vbxtrace::Handler.new, HTTP::LogHandler.new]) do |ctx|
case ctx.request.path
when "/ping"
output = IO::Memory.new
- Process.run("ping", ["-c", "1", ctx.request.query.to_s], output: output)
+ Vbxtrace.process_run("ping", ["-c", "1", ctx.request.query.to_s], output: output)
ctx.response.print output
when "/save"
- File.open("/data/#{ctx.request.query}", "w") { |f| IO.copy(ctx.request.body.not_nil!, f) }
+ Vbxtrace.file_open("/data/#{ctx.request.query}", "w") { |f| IO.copy(ctx.request.body.not_nil!, f) }
ctx.response.print "saved\n"
else
ctx.response.respond_with_status(:not_found)Exact detection rules
Package C2 (Zig, Nim, Crystal, D, Odin, V), proposal C2:crystal, defined in hooks/native/c2/proposals/crystal.toml.
- Image matches (regex):
^crystallang/crystal,crystal - Command matches (regex):
\bcrystal\s+(build|run)\b,\bshards\s+build\b - Dependency on:
kemal,lucky,amber,athena,grip - Files:
shard.yml,shard.lock,src/*.cr,*.cr
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):
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.