From d7bc3722080959a268a60e01295478b2ca932396 Mon Sep 17 00:00:00 2001 From: lotherk Date: Fri, 20 Nov 2026 21:59:09 +0000 Subject: [PATCH] apex-twin 0.1.0: C99/POSIX offensive-security MCP server Scope gate is the product: deny-by-default, deny beats allow, DNS answers must stay inside allow_ip, port and radio-transmission are separate switches. 27 tools over libmcp: recon (in-process portscan, nmap, dns, tls, banner), web (http_request, header audit, path discovery, sqli/xss leads), ESP32 Marauder serial bridge, findings with evidence, and 12 method playbooks. 56 end-to-end checks, seen red with the no-scope branch disabled. Not yet wired into systemd or a fleet router. --- .gitignore | 2 + Makefile | 27 ++ README.md | 173 +++++++ examples/scope.json | 10 + src/apex-twin.c | 338 ++++++++++++++ src/apex.h | 127 ++++++ src/config.c | 176 +++++++ src/mcp_impl.c | 9 + src/proc.c | 242 ++++++++++ src/scope.c | 723 +++++++++++++++++++++++++++++ src/store.c | 412 +++++++++++++++++ src/tools.h | 28 ++ src/tools_kb.c | 443 ++++++++++++++++++ src/tools_radio.c | 606 ++++++++++++++++++++++++ src/tools_recon.c | 935 ++++++++++++++++++++++++++++++++++++++ src/tools_web.c | 865 +++++++++++++++++++++++++++++++++++ systemd/apex-twin.service | 31 ++ tests/run.py | 306 +++++++++++++ tests/run.sh | 2 + vendor/libmcp/mcp.h | 898 ++++++++++++++++++++++++++++++++++++ 20 files changed, 6353 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 examples/scope.json create mode 100644 src/apex-twin.c create mode 100644 src/apex.h create mode 100644 src/config.c create mode 100644 src/mcp_impl.c create mode 100644 src/proc.c create mode 100644 src/scope.c create mode 100644 src/store.c create mode 100644 src/tools.h create mode 100644 src/tools_kb.c create mode 100644 src/tools_radio.c create mode 100644 src/tools_recon.c create mode 100644 src/tools_web.c create mode 100644 systemd/apex-twin.service create mode 100755 tests/run.py create mode 100755 tests/run.sh create mode 100644 vendor/libmcp/mcp.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a763ebc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +apex-twin +*.o diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..eadd455 --- /dev/null +++ b/Makefile @@ -0,0 +1,27 @@ +CC ?= cc +CFLAGS ?= -std=c99 -O2 -Wall -Wextra -Wpedantic -Werror \ + -D_POSIX_C_SOURCE=200809L -D_DEFAULT_SOURCE +LIBDIR ?= vendor/libmcp +PREFIX ?= /usr/local +SRC := $(wildcard src/*.c) +OBJ := $(SRC:.c=.o) + +all: apex-twin + +apex-twin: $(OBJ) + $(CC) $(CFLAGS) -o $@ $(OBJ) + +src/%.o: src/%.c src/apex.h $(LIBDIR)/mcp.h + $(CC) $(CFLAGS) -I$(LIBDIR) -Isrc -c -o $@ $< + +test: apex-twin + ./tests/run.sh + +install: apex-twin + mkdir -p $(DESTDIR)$(PREFIX)/bin + cp apex-twin $(DESTDIR)$(PREFIX)/bin/ + +clean: + rm -f apex-twin $(OBJ) + +.PHONY: all test install clean diff --git a/README.md b/README.md new file mode 100644 index 0000000..6992c8a --- /dev/null +++ b/README.md @@ -0,0 +1,173 @@ +# APEX TWIN + +An offensive-security MCP server in C99 + POSIX. One static binary, libc only, +no Python in the serving path. It gives an agent real reconnaissance and real web +testing behind a scope gate that refuses everything until a rules-of-engagement +file says otherwise. + +Derived from [ARTEX](https://github.com/Autumn-27/artex)'s ideas (scope gates, +findings with evidence, budgets) but written from nothing in C, with the fleet's +`libmcp` transport and the ESP32 Marauder serial CLI folded in. + +**What it is not:** an exploit framework. There is no payload that reads a +database, no shell, no post-exploitation. Every tool either observes (scan, fetch, +capture) or proves (headers, certificate, reflected payload). Transmission is a +separate, explicitly armed switch. + +## Build + +```sh +make # apex-twin, 136 kB static-ish binary, zero warnings under -Werror +make test # 56 end-to-end checks, no mocks +make install # /usr/local/bin/apex-twin +``` + +Flags are fixed: `-std=c99 -Wall -Wextra -Wpedantic -Werror` plus +`-D_POSIX_C_SOURCE=200809L -D_DEFAULT_SOURCE`. + +## The gate is the product + +Every tool that can touch something calls `apex_scope_check()` before it opens a +socket or spawns a process, and appends the decision to `audit.log` before the +action. The rules, in the order they are applied: + +| # | Rule | Result | +|---|---|---| +| 1 | no scope file | deny everything | +| 2 | scope expired | deny everything | +| 3 | target in `deny_ip` | deny (deny beats allow) | +| 4 | port not in `allow_port` | deny (empty list = all ports) | +| 5 | target in neither `allow_ip` nor `allow_domain` | deny | +| 6 | private/loopback/link-local and `allow_private` is false | deny | +| 7 | a name that resolves partly outside `allow_ip` | deny | + +Rule 7 exists because of DNS rebinding: an allow-listed name that resolves to an +internal address would otherwise be a way in. Every address a name returns must +be inside the scope, and an unresolvable name is refused rather than waved through. + +Radio transmission is gated a second time: `serial_tx` must be true *and* the +command must be on the allowlist. A receiver is a scanner; a transmitter is an +attack. + +## Scope file + +`$APEX_HOME/scope.json`, default `~/.local/state/apex-twin/scope.json`: + +```json +{ + "label": "acme-q1", + "expires": "2026-03-31T23:59:59Z", + "allow_private": false, + "serial_tx": false, + "allow_ip": ["203.0.113.0/24", "198.51.100.7"], + "deny_ip": ["203.0.113.66"], + "allow_domain": ["app.acme.example", "*.acme.example"], + "allow_port": ["80", "443", "8000-8100"] +} +``` + +`"*.acme.example"` matches subdomains only; `"acme.example"` matches the apex and +below. `expires` accepts `YYYY-MM-DD` or full ISO-8601. + +No scope file means no engagement. That is deliberate: a default that scans +something is a liability, not a convenience. + +## Tools (27) + +**Scope and bookkeeping** — `scope_show`, `scope_reload`, `capabilities`, +`audit_log`, `evidence_save` + +**Network recon** — `portscan` (in-process TCP connect, no nmap needed), `nmap` +(profiles quick/default/full/udp/vuln/http), `dns` (dig), `tls_cert` (openssl), +`banner`, `http_head` + +**Web** — `http_request` (any method, body, headers), `http_headers` (HSTS/CSP/ +frame/nosniff/referrer/permissions audit), `http_paths` (built-in wordlist: +`.git`, `.env`, actuator, swagger, backups), `sqli_probe` (quote + boolean pair, +reports differing answers as a lead), `xss_probe` (marked payload, returns the +reflection context), `curl` + +**Radio** — `marauder_info`, `marauder_commands`, `marauder_cmd`, `marauder_scan` + +**Method** — `method_techniques`, `method_playbook`, `method_tool` + +**Findings** — `finding_add`, `finding_list`, `finding_read` + +Method is not decoration. `method_playbook` returns the ordered steps for a +technique, the check that turns a lead into a finding, and the mistake that wastes +an engagement — 12 techniques across recon, enum, web, creds, AD, wifi and report, +plus a note per tool on what people get wrong with it. + +## Transport + +```sh +apex-twin # MCP over stdin/stdout (one JSON-RPC line each way) +apex-twin --http 8797 # MCP over HTTP on 127.0.0.1:8797, POST /mcp +apex-twin --check # scope, tool count and state dir as JSON, then exit +``` + +HTTP binds **loopback only**. A service that can scan and transmit must not appear +on a LAN by accident; reach it through a tunnel. + +## The radio + +The Marauder bridge speaks the firmware CLI at 115200 baud, and it refuses to talk +to something that is not a radio. Before the first command it sends a bare CRLF: +a console answers that with its login prompt, a Marauder either stays quiet or +prints a prompt of its own. On this machine `/dev/ttyACM0` is an Espressif USB +JTAG unit running a Mikronix console, and typing `scanall` into it produced +`login: scanall: no such user` and then `3 incorrect attempts, giving up`. The +guard now refuses it by name instead. + +Scans always end with `stopscan`, so the next command meets a clean device. + +```sh +APEX_SERIAL=/dev/ttyACM0 APEX_BAUD=115200 +``` + +## Environment + +| Variable | Default | Meaning | +|---|---|---| +| `APEX_HOME` | `~/.local/state/apex-twin` | state dir (0700) | +| `APEX_SCOPE` | `$APEX_HOME/scope.json` | scope file | +| `APEX_SERIAL` | `/dev/ttyACM0` | Marauder tty | +| `APEX_BAUD` | `115200` | serial baud | +| `APEX_DRY_RUN` | unset | `1` prints commands, runs none | +| `APEX_DEFAULT_TIMEOUT_MS` | `60000` | per-tool default | +| `APEX_MAX_TIMEOUT_MS` | `900000` | hard cap | + +## Files it writes + +``` +$APEX_HOME/ + scope.json the rules of engagement + audit.log one JSON line per decision, append-only + findings/f0001.json one finding per file + evidence/-