Skip to content

Hook cheat sheet: Lua and OpenResty ​

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

Runtimes on this page: OpenResty / nginx with Lua · Standalone Lua.

OpenResty / nginx with Lua ​

SetupUse this whenChangeHub automation
OpenResty runtime hook (nginx include)Depends on lapis, lua-resty-http, lua-resty-mysql, lua-resty-redis or lua-resty-core (in a rockspec)compose + mounted configfully
OpenResty runtime hook (include line in nginx.conf)Depends on lapis (in a rockspec)source, rebuildpartly

Without source access ​

Only the compose file changes: the image and the code stay as they are.

OpenResty runtime hook (nginx include) ​

  • Use this when: OpenResty / nginx with Lua service that depends on lapis, lua-resty-http, lua-resty-mysql, lua-resty-redis or lua-resty-core (in a rockspec).
  • Hub automation: fully. The hub proposes exactly this change and pushes it (one commit per container) once you approve it. Do it by hand only when the proposal failed, e.g. on an unusual compose file.
  • Check and undo: hook mounted into the container

Mounts vulnboxd's hook directory read-only at /opt/vbxtrace and its nginx include as /etc/nginx/conf.d/zz-vbxtrace.conf, so every request reports its boundaries and the processes, files and cosocket connections it causes. Image and Lua code stay untouched; remove the include to switch it off (VBXTRACE=0 only works if nginx.conf declares env VBXTRACE;).

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

yaml
services:
  app:
    volumes:
      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
      - /usr/local/lib/vulnboxd/trace/openresty/vbxtrace.conf:/etc/nginx/conf.d/zz-vbxtrace.conf:ro

The mounted file, as the hook directory has it (hooks/lua/l2/openresty/vbxtrace.conf):

nginx
# vulnboxd's OpenResty hook (package L2). Include inside the http {} block (the official OpenResty images
# include /etc/nginx/conf.d/*.conf there, so mounting this file as /etc/nginx/conf.d/zz-vbxtrace.conf is
# enough). Needs vulnboxd's hook directory mounted read-only at /opt/vbxtrace.
#
# Only directives that apps rarely set at http level, each of which only calls into the hook (loaded
# lazily per worker, so no init_by_lua / init_worker_by_lua / lua_package_path is taken from the app):
#   rewrite_by_lua_block, access_by_lua_block   start the request (whichever runs first)
#   log_by_lua_block                            end it
# A location that sets its own *_by_lua for one of these phases replaces the inherited one there; the
# other phases still run (see docs/hooks/l2.md).

rewrite_by_lua_block {
    local v = package.loaded["vbxtrace.openresty"]
    if v == nil then
        local ok, m = pcall(dofile, "/opt/vbxtrace/openresty/vbxtrace.lua")
        v = ok and type(m) == "table" and m or false
        package.loaded["vbxtrace.openresty"] = v
    end
    if v then v.begin() end
}

access_by_lua_block {
    local v = package.loaded["vbxtrace.openresty"]
    if v == nil then
        local ok, m = pcall(dofile, "/opt/vbxtrace/openresty/vbxtrace.lua")
        v = ok and type(m) == "table" and m or false
        package.loaded["vbxtrace.openresty"] = v
    end
    if v then v.begin() end
}

log_by_lua_block {
    local v = package.loaded["vbxtrace.openresty"]
    if v then v.finish() end
}
Exact detection rules

Package L2 (OpenResty hook), proposal L2:openresty, defined in hooks/lua/l2/proposals/openresty.toml.

  • Image matches (regex): ^(docker\.io/)?openresty/openresty, ^[^ ]*/openresty:, ^(docker\.io/)?bitnami/openresty, ^(docker\.io/)?leafo/lapis
  • Command matches (regex): \bopenresty\b, \blapis\s+server\b, \bnginx\b.*-p\b
  • Dependency on: lapis, lua-resty-http, lua-resty-mysql, lua-resty-redis, lua-resty-core
  • Files: nginx.conf, conf/nginx.conf, conf.d/*.conf, *.rockspec, config.lua, config.moon, nginx.conf.compiled

With source access ​

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

OpenResty runtime hook (include line in nginx.conf) ​

  • Use this when: OpenResty / nginx with Lua service that depends on lapis (in a rockspec).
  • Hub automation: partly. The hub pushes the compose lines; the source change is yours.
  • Check and undo: hook mounted into the container

Mounts vulnboxd's hook directory read-only at /opt/vbxtrace and adds include /opt/vbxtrace/openresty/vbxtrace.conf; inside the http block of the service's nginx.conf. Optional: env VBXTRACE; at the top level makes VBXTRACE=0 work as an off switch.

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. add the read-only mount of /usr/local/lib/vulnboxd/trace at /opt/vbxtrace to the service
  2. add include /opt/vbxtrace/openresty/vbxtrace.conf; inside the http { } block of nginx.conf (for Lapis: in nginx.conf, the template lapis compiles)
  3. optionally add env VBXTRACE; at the top level of nginx.conf so VBXTRACE=0 switches the hook off

Example change on a sample service (hooks/lua/l2/proposals/include.diff):

diff
--- a/nginx.conf
+++ b/nginx.conf
@@ -1,9 +1,11 @@
 worker_processes ${{NUM_WORKERS}};
 error_log stderr notice;
 daemon off;
 pid logs/nginx.pid;
+env VBXTRACE;
 
 events {
   worker_connections 1024;
 }
 
 http {
+  include /opt/vbxtrace/openresty/vbxtrace.conf;
   include mime.types;
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -3,3 +3,5 @@ services:
   <service>:
     build: .
+    volumes:
+      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
Exact detection rules

Package L2 (OpenResty hook), proposal L2:include, defined in hooks/lua/l2/proposals/include.toml.

  • Command matches (regex): \bopenresty\b, \blapis\s+server\b
  • Dependency on: lapis
  • Files: nginx.conf, conf/nginx.conf, config.lua, config.moon

Standalone Lua ​

SetupUse this whenChangeHub automation
Lua runtime hook (LUA_INIT)Depends on copas, http, cqueues, luasocket, xavante or 1 more (in a rockspec)composefully

Without source access ​

Only the compose file changes: the image and the code stay as they are.

Lua runtime hook (LUA_INIT) ​

  • Use this when: Standalone Lua service that depends on copas, http, cqueues, luasocket, xavante or 1 more (in a rockspec).
  • Hub automation: fully. The hub proposes exactly this change and pushes it (one commit per container) once you approve it. Do it by hand only when the proposal failed, e.g. on an unusual compose file.
  • Check and undo: hook mounted into the container

Mounts vulnboxd's hook directory read-only at /opt/vbxtrace and sets LUA_INIT=@/opt/vbxtrace/lua/vbxtrace.lua (a previous LUA_INIT keeps running, after the hook), so Copas and lua-http services report request boundaries, and the processes, files and connections each request causes. Image and source stay untouched; set VBXTRACE=0 to switch it off.

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

yaml
services:
  app:
    volumes:
      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
    environment:
      LUA_INIT: "@/opt/vbxtrace/lua/vbxtrace.lua"
      # LUA_INIT already set? move its value to VBXTRACE_LUA_INIT_PREV and set LUA_INIT as above
Exact detection rules

Package L3 (Lua hook (standalone)), proposal L3:lua-init, defined in hooks/lua/l3/proposals/lua-init.toml.

  • Image matches (regex): ^(docker\.io/)?nickblah/lua, ^[^ ]*/lua:, ^[^ ]*/luajit
  • Command matches (regex): \blua(jit|5\.[1-4])?\b, \blua(jit|5\.[1-4])?\s+\S+\.lua\b
  • Dependency on: copas, http, cqueues, luasocket, xavante, pegasus
  • Files: *.rockspec, *.lua, rockspec, .luarocks/*, lua_modules/*

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.