diff --git a/CHANGELOG.md b/CHANGELOG.md index 196ec83..54a26cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,24 @@ fields, and state from an older version is read by a newer one. There are no git tags. The hash next to a version is the commit that introduced it; the current entry is the commit that adds this section. -## 1.50.0 — 2026-11-23 — this change +## 1.51.0 — 2026-11-23 — this change + +- **FreeRADIUS** (`freeradius_audit`): `clients.conf`, `radiusd.conf` and + `users`: a well-known shared secret, `require_message_authenticator = no` + (BlastRADIUS), `Auth-Type := Accept` and world-writable files. +- **containerd** (`containerd_audit`): `config.toml`: a TCP gRPC address + without `cert_file`, a `sandbox_image` without a digest, + `no_pivot_root = true` and a world-writable file. +- **NATS** (`nats_audit`): `nats-server.conf`: no authorization block, no TLS + block, an `http` monitoring listener on every address, debug logging and a + world-writable file. +- **Consul** (`consul_api_probe`): `/v1/agent/self` and + `/v1/catalog/services` without an ACL token; a 2xx JSON answer is + unauthenticated access, with the first answer saved as evidence. +- Tests with FreeRADIUS/containerd/NATS trees and two Consul HTTP fixtures + (open and 403). 1072 checks. Coverage 83.3%. + +## 1.50.0 — 2026-11-23 — ead28d4 - **Kafka** (`kafka_audit`): `server.properties`: a `PLAINTEXT` listener on `0.0.0.0`, a missing `authorizer.class.name`, diff --git a/README.md b/README.md index 3d1ebf4..778e5da 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,11 @@ no Python in the serving path. It gives an agent real reconnaissance and real we testing behind a scope gate that refuses everything until a rules-of-engagement file says otherwise. -**238 tools · 1060 end-to-end checks · 340 fuzz cases · 83.2% line coverage · ~863 KiB** +**242 tools · 1072 end-to-end checks · 340 fuzz cases · 83.3% line coverage · ~879 KiB** ``` $ ./apex-twin --check -{"version":"1.50.0","tools":238,...,"scope":{"loaded":true,...}} +{"version":"1.51.0","tools":242,...,"scope":{"loaded":true,...}} ``` APEX TWIN was inspired by [ARTEX](https://github.com/Autumn-27/artex) (scope @@ -149,7 +149,7 @@ closes a request whose head or body does not arrive within --- -## Tools (238) +## Tools (242) ### Scope and bookkeeping @@ -209,6 +209,7 @@ uploads and secret-file reads. It does not execute anything. | `zookeeper_4lw_probe` | `stat`/`mntr` answered without auth leaks version and state | | `etcd_api_probe` | `/version`, `/health`, `/v2/keys` without a token; 2xx is open access | | `docker_registry_probe` | `/v2/` and `/v2/_catalog` without auth; catalog saved as evidence | +| `consul_api_probe` | `/v1/agent/self` and `/v1/catalog/services` without a token | | `cookie_audit` | Secure/HttpOnly/SameSite per cookie, incl. `SameSite=None` without Secure | | `http_options` | `Allow`/`Public`; flags TRACE, PUT, DELETE | | `sqli_probe` | Quote and boolean payloads; differing answers are a **lead**, not proof | @@ -435,6 +436,9 @@ command in the allowlist: a receiver is a scanner, a transmitter is an attack. | `kafka_audit` | server.properties: PLAINTEXT listener, no authorizer, auto topics | | `powerdns_audit` | pdns/recursor: open recursor, webserver, API key in the file | | `varnish_audit` | VCL: open `acl purge`, management on 0.0.0.0 | +| `freeradius_audit` | clients.conf/users: default secret, no message authenticator | +| `containerd_audit` | config.toml: TCP gRPC without TLS, unpinned sandbox image | +| `nats_audit` | nats-server.conf: no authorization/TLS, open monitoring port | ### ESP32 @@ -723,6 +727,8 @@ The full threat model, trust boundaries and a hardening checklist are in | etcd API | three unauthenticated GETs, first answers as evidence | | kafka/powerdns/varnish | config reads only; optional root prefix | | registry | two unauthenticated GETs, catalog as evidence | +| freeradius/containerd/nats | config reads only; optional root prefix | +| Consul API | two unauthenticated GETs, first answer as evidence | | USB audit | 200 devices, sysfs reads only | --- @@ -734,7 +740,7 @@ Flags are fixed: `-std=c99 -Wall -Wextra -Wpedantic -Werror` plus ```sh make # apex-twin -make test # version check + gate audit + 1060 end-to-end checks +make test # version check + gate audit + 1072 end-to-end checks make version-check # the five places that carry a version must agree make gate-audit # static: every target-taking tool calls the gate make asan # apex-twin-asan with ASan + UBSan @@ -859,7 +865,7 @@ src/tools_red.c vuln_check, subdomain_enum, cred_test, vhost_discovery, dns_soa_mname_check, hsts_audit, tls_hostname_check, ftp_tls_probe, zookeeper_4lw_probe, etcd_api_probe, - docker_registry_probe + docker_registry_probe, consul_api_probe src/tools_blue.c hardening_audit, log_audit, ioc_scan, web_log_audit, detect_rules, integrity_baseline, mitre_map, process_snapshot, persistence_audit, net_baseline, @@ -902,7 +908,7 @@ examples/ scope.json ## Status -1.50.0. Built and run: 238 tools, 1060 checks, 340 fuzz cases and 83.2% line +1.51.0. Built and run: 242 tools, 1072 checks, 340 fuzz cases and 83.3% line coverage. The loop closes: a probe captures the exchange as a pair, a finding points at it, `finding_update` gives it a state, an asset records the host it came from, `cors_check`/`cookie_audit`/`http_options` turn a web answer into a diff --git a/SECURITY.md b/SECURITY.md index 3c98486..8b71424 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -9,7 +9,7 @@ problem. The supported line is the one printed by `apex-twin --version` for the tree you built; this document ships with that same tree. At the time of writing the line -is **1.50.x** (`src/apex.h`, `APEX_VERSION`). Security fixes land on the current +is **1.51.x** (`src/apex.h`, `APEX_VERSION`). Security fixes land on the current line. The 1.0 line promises that the on-disk JSON formats for findings, assets and traffic only gain fields, so state stays readable; older builds are still rebuilt rather than patched in place. @@ -66,6 +66,8 @@ only narrow what the proxy does (the scope gate still runs first). - **Firmware writing needs an explicit confirmation.** `esp_flash` refuses without `confirm=true`, validates an absolute image path and audits the attempt; it exists to provision the operator's own board. +- **FreeRADIUS, containerd and NATS audits read local files.** The Consul + probe sends two unauthenticated GETs and saves the first answer as evidence. - **Kafka, PowerDNS and Varnish audits read local files.** The registry probe sends two unauthenticated GETs and saves the catalog as evidence. - **iSCSI, rpcbind and kdump audits read local files.** The etcd probe sends diff --git a/docs/apex-twin.1 b/docs/apex-twin.1 index da110c3..8efeeb6 100644 --- a/docs/apex-twin.1 +++ b/docs/apex-twin.1 @@ -1,5 +1,5 @@ .\" apex-twin.1 - manual page for APEX TWIN -.TH APEX-TWIN 1 "2026-11-21" "apex-twin 1.50.0" "User Commands" +.TH APEX-TWIN 1 "2026-11-21" "apex-twin 1.51.0" "User Commands" .SH NAME apex-twin \- scope\-gated offensive\-security MCP server .SH SYNOPSIS @@ -46,7 +46,7 @@ sip_probe, telnet_probe, http_methods, websocket_probe, ike_probe, snmp_walk, ldap_search, openapi_discovery, csrf_audit, rpc_probe, ntp_probe, dnssec_check, smtp_starttls_probe, tls_ocsp_probe, alpn_probe, webdav_probe, dns_wildcard_check, dns_caa_check, sni_vhost_check, tls_client_auth_probe, kubernetes_api_probe, dns_ns_consistency, dns_dangling_cname, http_redirect_audit, websocket_origin_check, dns_fcrdns_check, dns_soa_mname_check, http_redirect_audit, -websocket_origin_check, hsts_audit, tls_hostname_check, ftp_tls_probe, zookeeper_4lw_probe, etcd_api_probe, docker_registry_probe), +websocket_origin_check, hsts_audit, tls_hostname_check, ftp_tls_probe, zookeeper_4lw_probe, etcd_api_probe, docker_registry_probe, consul_api_probe), defensive audits (hardening_audit, log_audit, ioc_scan, web_log_audit, detect_rules, integrity_baseline, mitre_map, process_snapshot, persistence_audit, @@ -70,7 +70,7 @@ ldap_client_audit, ssl_cert_audit, timesyncd_audit, resolv_conf_audit, journald_ coredump_audit, nftables_audit, dnsmasq_audit, nsswitch_audit, jenkins_audit, pgbouncer_audit, xinetd_audit, sssd_audit, avahi_audit, logind_audit, tftpd_hpa_audit, iscsi_audit, rpcbind_audit, kdump_audit, kafka_audit, powerdns_audit, -varnish_audit) +varnish_audit, freeradius_audit, containerd_audit, nats_audit) and ESP32 board tools (esp_info, esp_monitor, esp_read_flash, esp_flash, esp_partition_table, esp_nvs_parse, firmware_scan, esp_app_info, esp_image_verify). diff --git a/src/apex.h b/src/apex.h index ed3dfb0..d394367 100644 --- a/src/apex.h +++ b/src/apex.h @@ -15,7 +15,7 @@ #include "mcp.h" -#define APEX_VERSION "1.50.0" +#define APEX_VERSION "1.51.0" #define APEX_MAX_OUT (1u << 20) #define APEX_PATH_MAX 512 #define APEX_PATH_FULL 1024 diff --git a/src/tools_blue.c b/src/tools_blue.c index 6e8b9f3..2943070 100644 --- a/src/tools_blue.c +++ b/src/tools_blue.c @@ -14917,6 +14917,455 @@ static void t_varnish_audit(mcp_conn *c, const mcp_json *args, void *user) mcp_buf_free(&findings); } +/* ----------------------------------------------------------- freeradius_audit */ + +static int radius_default_secret(const char *value) +{ + static const char *weak[] = { "testing123", "secret", "radius", + "shared", "password", "changeme", NULL }; + int i; + + for (i = 0; weak[i] != NULL; i++) + if (strcasecmp(value, weak[i]) == 0) + return 1; + return 0; +} + +static void radius_scan_file(mcp_buf *findings, int *first, int *count, + const char *path, int *files, int *weak_secret, + int *no_msg_auth, int *accept_auth) +{ + static char text[65536]; + struct stat st; + char *copy; + char *save = NULL; + char *line; + + if (read_text(path, text, sizeof(text)) <= 0) + return; + (*files)++; + if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) { + add_finding(findings, first, "world-writable", "high", + "%s is writable by anyone", path); + (*count)++; + } + copy = strdup(text); + if (copy == NULL) + return; + line = strtok_r(copy, "\n", &save); + while (line != NULL) { + char *s = line; + + while (*s == ' ' || *s == '\t') + s++; + if (*s != '#' && *s != '\0') { + char *eq = strchr(s, '='); + int accept_line = strstr(s, "Auth-Type") != NULL && + strstr(s, "Accept") != NULL; + + if (accept_line) + *accept_auth = 1; + if (eq != NULL) { + char key[128]; + char *v = eq + 1; + + *eq = '\0'; + snprintf(key, sizeof(key), "%.120s", s); + { + char *k = key; + size_t kl; + + while (*k == ' ' || *k == '\t') + memmove(k, k + 1, strlen(k)); + kl = strlen(k); + while (kl > 0 && (k[kl - 1] == ' ' || + k[kl - 1] == '\t')) + k[--kl] = '\0'; + } + while (*v == ' ' || *v == '\t') + v++; + { + size_t vl = strlen(v); + + while (vl > 0 && (v[vl - 1] == ' ' || + v[vl - 1] == '\t' || + v[vl - 1] == '\r')) + v[--vl] = '\0'; + if (vl >= 2 && v[0] == '"' && + v[vl - 1] == '"') { + v++; + v[vl - 2] = '\0'; + } + } + if (strcasecmp(key, "secret") == 0 && + radius_default_secret(v)) + *weak_secret = 1; + else if (strcasecmp( + key, + "require_message_authenticator") == 0 && + strcasecmp(v, "no") == 0) + *no_msg_auth = 1; + } + } + line = strtok_r(NULL, "\n", &save); + } + free(copy); +} + +static void t_freeradius_audit(mcp_conn *c, const mcp_json *args, void *user) +{ + static const char *files[] = { "/etc/freeradius/3.0/clients.conf", + "/etc/freeradius/3.0/radiusd.conf", + "/etc/freeradius/3.0/users", + "/etc/freeradius/clients.conf", + "/etc/freeradius/users", NULL }; + const char *root = mcp_arg_string(args, "root"); + char path[1024]; + mcp_buf b; + mcp_buf findings; + int first = 1; + int count = 0; + int files_read = 0; + int weak_secret = 0; + int no_msg_auth = 0; + int accept_auth = 0; + int i; + + (void)user; + if (root == NULL) + root = ""; + if (strlen(root) > 400) { + mcp_reply_error(c, "root path too long"); + return; + } + mcp_buf_init(&findings); + for (i = 0; files[i] != NULL; i++) { + snprintf(path, sizeof(path), "%.400s%s", root, files[i]); + radius_scan_file(&findings, &first, &count, path, &files_read, + &weak_secret, &no_msg_auth, &accept_auth); + } + if (files_read == 0) { + mcp_buf_free(&findings); + mcp_reply_error(c, "cannot read a FreeRADIUS configuration"); + return; + } + if (weak_secret) { + add_finding(&findings, &first, "default-secret", "high", + "a client uses a well-known shared secret"); + count++; + } + if (no_msg_auth) { + add_finding(&findings, &first, "no-message-authenticator", + "high", + "require_message_authenticator is off (BlastRADIUS)"); + count++; + } + if (accept_auth) { + add_finding(&findings, &first, "accept-auth", "high", + "Auth-Type := Accept trusts every request"); + count++; + } + mcp_buf_init(&b); + mcp_buf_puts(&b, "{\"root\":"); + apex_json_str(&b, root); + mcp_buf_printf(&b, ",\"files\":%d,\"default_secret\":%s," + "\"message_authenticator\":%s,\"accept_auth\":%s," + "\"findings_count\":%d,\"findings\":[", + files_read, weak_secret ? "true" : "false", + no_msg_auth ? "false" : "true", + accept_auth ? "true" : "false", count); + if (findings.p != NULL) + mcp_buf_add(&b, findings.p, findings.len); + mcp_buf_puts(&b, "]}"); + mcp_reply_json(c, b.p); + mcp_buf_free(&b); + mcp_buf_free(&findings); +} + +/* ------------------------------------------------------------- containerd_audit */ + +static int toml_tcp_address(const char *address) +{ + if (address[0] == '/') + return 0; + if (strchr(address, ':') != NULL) + return 1; + return 0; +} + +static void t_containerd_audit(mcp_conn *c, const mcp_json *args, void *user) +{ + const char *root = mcp_arg_string(args, "root"); + char path[1024]; + static char text[65536]; + char address[256]; + char sandbox[512]; + char value[64]; + struct stat st; + mcp_buf b; + mcp_buf findings; + int first = 1; + int count = 0; + int tcp = 0; + int no_tls = 0; + int unpinned = 0; + int no_pivot = 0; + + (void)user; + if (root == NULL) + root = ""; + if (strlen(root) > 400) { + mcp_reply_error(c, "root path too long"); + return; + } + snprintf(path, sizeof(path), "%s/etc/containerd/config.toml", root); + if (read_text(path, text, sizeof(text)) <= 0) { + mcp_reply_error(c, "cannot read containerd config.toml"); + return; + } + address[0] = sandbox[0] = '\0'; + mcp_buf_init(&findings); + if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) { + add_finding(&findings, &first, "world-writable", "high", + "config.toml is writable by anyone"); + count++; + } + conf_value(text, "address", address, sizeof(address)); + conf_value(text, "sandbox_image", sandbox, sizeof(sandbox)); + if (address[0] != '\0' && toml_tcp_address(address)) { + tcp = 1; + if (!conf_value(text, "cert_file", value, sizeof(value)) || + value[0] == '\0') + no_tls = 1; + } + if (sandbox[0] != '\0' && strstr(sandbox, "@sha256:") == NULL) { + unpinned = 1; + add_finding(&findings, &first, "unpinned-sandbox-image", "low", + "sandbox_image is not pinned by digest"); + count++; + } + if (conf_value(text, "no_pivot_root", value, sizeof(value)) && + strcasecmp(value, "true") == 0) { + no_pivot = 1; + add_finding(&findings, &first, "no-pivot-root", "high", + "no_pivot_root weakens the container mount isolation"); + count++; + } + if (no_tls) { + add_finding(&findings, &first, "no-grpc-tls", "high", + "the TCP gRPC endpoint has no TLS certificate"); + count++; + } + mcp_buf_init(&b); + mcp_buf_puts(&b, "{\"root\":"); + apex_json_str(&b, root); + mcp_buf_puts(&b, ",\"address\":"); + apex_json_str(&b, address); + mcp_buf_puts(&b, ",\"sandbox_image\":"); + apex_json_str(&b, sandbox); + mcp_buf_printf(&b, ",\"tcp\":%s,\"grpc_tls\":%s,\"pinned\":%s," + "\"pivot_root\":%s,\"findings_count\":%d," + "\"findings\":[", + tcp ? "true" : "false", + no_tls ? "false" : "true", + unpinned ? "false" : "true", + no_pivot ? "false" : "true", count); + if (findings.p != NULL) + mcp_buf_add(&b, findings.p, findings.len); + mcp_buf_puts(&b, "]}"); + mcp_reply_json(c, b.p); + mcp_buf_free(&b); + mcp_buf_free(&findings); +} + +/* ----------------------------------------------------------------- nats_audit */ + +static int nats_has_block(const char *text, const char *name) +{ + char *copy = strdup(text); + char *save = NULL; + char *line; + size_t nl = strlen(name); + int found = 0; + + if (copy == NULL) + return 0; + line = strtok_r(copy, "\n", &save); + while (line != NULL) { + char *s = line; + + while (*s == ' ' || *s == '\t') + s++; + if (*s != '#' && strncmp(s, name, nl) == 0 && + (s[nl] == ' ' || s[nl] == '\t' || s[nl] == ':' || + s[nl] == '{')) { + found = 1; + break; + } + line = strtok_r(NULL, "\n", &save); + } + free(copy); + return found; +} + +static int nats_value(const char *text, const char *key, char *out, size_t n) +{ + char *copy = strdup(text); + char *save = NULL; + char *line; + size_t kl = strlen(key); + int found = 0; + + out[0] = '\0'; + if (copy == NULL) + return 0; + line = strtok_r(copy, "\n", &save); + while (line != NULL) { + char *s = line; + + while (*s == ' ' || *s == '\t') + s++; + if (*s != '#' && strncmp(s, key, kl) == 0 && + (s[kl] == ' ' || s[kl] == '\t' || s[kl] == ':' || + s[kl] == '=')) { + char *v = s + kl; + size_t vl; + + while (*v == ' ' || *v == '\t' || *v == ':' || *v == '=') + v++; + vl = strlen(v); + while (vl > 0 && (v[vl - 1] == ' ' || v[vl - 1] == '\t' || + v[vl - 1] == '\r')) + v[--vl] = '\0'; + if (vl >= 2 && v[0] == '"' && v[vl - 1] == '"') { + v++; + v[vl - 2] = '\0'; + } + { + size_t cn = strlen(v); + + if (cn >= n) + cn = n - 1; + memcpy(out, v, cn); + out[cn] = '\0'; + } + found = 1; + break; + } + line = strtok_r(NULL, "\n", &save); + } + free(copy); + return found; +} + +static int nats_http_all(const char *text) +{ + char *copy = strdup(text); + char *save = NULL; + char *line; + int found = 0; + + if (copy == NULL) + return 0; + line = strtok_r(copy, "\n", &save); + while (line != NULL) { + char *s = line; + + while (*s == ' ' || *s == '\t') + s++; + if (*s != '#' && strncmp(s, "http", 4) == 0 && s[4] == ':') { + char *v = s + 4; + + if (*v == ':') + v++; + while (*v == ' ' || *v == '\t') + v++; + if (*v == ':' || strstr(v, "0.0.0.0") != NULL) + found = 1; + } + line = strtok_r(NULL, "\n", &save); + } + free(copy); + return found; +} + +static void t_nats_audit(mcp_conn *c, const mcp_json *args, void *user) +{ + const char *root = mcp_arg_string(args, "root"); + char path[1024]; + static char text[65536]; + char value[64]; + struct stat st; + mcp_buf b; + mcp_buf findings; + int first = 1; + int count = 0; + int no_auth = 0; + int no_tls = 0; + int http_all = 0; + int debug = 0; + + (void)user; + if (root == NULL) + root = ""; + if (strlen(root) > 400) { + mcp_reply_error(c, "root path too long"); + return; + } + snprintf(path, sizeof(path), "%s/etc/nats/nats-server.conf", root); + if (read_text(path, text, sizeof(text)) <= 0) { + mcp_reply_error(c, "cannot read nats-server.conf"); + return; + } + mcp_buf_init(&findings); + if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) { + add_finding(&findings, &first, "world-writable", "high", + "nats-server.conf is writable by anyone"); + count++; + } + if (!nats_has_block(text, "authorization")) { + no_auth = 1; + add_finding(&findings, &first, "no-authorization", "high", + "any client may publish and subscribe"); + count++; + } + if (!nats_has_block(text, "tls")) { + no_tls = 1; + add_finding(&findings, &first, "no-tls", "high", + "the client port has no TLS"); + count++; + } + if (nats_http_all(text)) { + http_all = 1; + add_finding(&findings, &first, "monitoring-all", "medium", + "the monitoring port is reachable from every address"); + count++; + } + if (nats_value(text, "debug", value, sizeof(value)) && + strcasecmp(value, "true") == 0) { + debug = 1; + add_finding(&findings, &first, "debug-enabled", "low", + "debug logging is enabled"); + count++; + } + mcp_buf_init(&b); + mcp_buf_puts(&b, "{\"root\":"); + apex_json_str(&b, root); + mcp_buf_printf(&b, ",\"authorization\":%s,\"tls\":%s," + "\"monitoring_all\":%s,\"debug\":%s," + "\"findings_count\":%d,\"findings\":[", + no_auth ? "false" : "true", + no_tls ? "false" : "true", + http_all ? "true" : "false", + debug ? "true" : "false", count); + if (findings.p != NULL) + mcp_buf_add(&b, findings.p, findings.len); + mcp_buf_puts(&b, "]}"); + mcp_reply_json(c, b.p); + mcp_buf_free(&b); + mcp_buf_free(&findings); +} + const mcp_tool TOOLS_BLUE[] = { { "hardening_audit", "Read-only posture audit of the local host: sshd_config (root login, " @@ -15745,6 +16194,27 @@ const mcp_tool TOOLS_BLUE[] = { "{\"type\":\"object\",\"properties\":{" "\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}", t_varnish_audit }, + { "freeradius_audit", + "Parse clients.conf, radiusd.conf and users (root prefix, usual paths): " + "a well-known shared secret, require_message_authenticator=no, " + "Auth-Type := Accept and world-writable files. Read-only.", + "{\"type\":\"object\",\"properties\":{" + "\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}", + t_freeradius_audit }, + { "containerd_audit", + "Parse containerd config.toml (root prefix): a TCP gRPC address without " + "cert_file, sandbox_image without a sha256 digest, no_pivot_root=true " + "and a world-writable file. Read-only.", + "{\"type\":\"object\",\"properties\":{" + "\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}", + t_containerd_audit }, + { "nats_audit", + "Parse nats-server.conf (root prefix): no authorization block, no TLS " + "block, an http monitoring listener on every address, debug logging and " + "a world-writable file. Read-only.", + "{\"type\":\"object\",\"properties\":{" + "\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}", + t_nats_audit }, }; const size_t TOOLS_BLUE_N = sizeof(TOOLS_BLUE) / sizeof(TOOLS_BLUE[0]); diff --git a/src/tools_red.c b/src/tools_red.c index ba7009c..87bcf02 100644 --- a/src/tools_red.c +++ b/src/tools_red.c @@ -6622,6 +6622,93 @@ static void t_docker_registry_probe(mcp_conn *c, const mcp_json *args, mcp_buf_free(&b); } +/* --------------------------------------------------------- consul_api_probe */ + +static void t_consul_api_probe(mcp_conn *c, const mcp_json *args, void *user) +{ + static const char *paths[] = { "/v1/agent/self", + "/v1/catalog/services", NULL }; + const char *target = mcp_arg_string(args, "target"); + int port = mcp_arg_int(args, "port", 8500); + int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port)); + mcp_buf b; + int first = 1; + int tested = 0; + int open_count = 0; + char *evidence = NULL; + int i; + + (void)user; + if (target == NULL || *target == '\0') { + mcp_reply_error(c, "target is required"); + return; + } + if (port <= 0 || port > 65535) { + mcp_reply_error(c, "port out of range"); + return; + } + if (!gated(c, "consul_api_probe", target, port)) + return; + mcp_buf_init(&b); + mcp_buf_puts(&b, "{\"target\":"); + apex_json_str(&b, target); + mcp_buf_printf(&b, ",\"port\":%d,\"tls\":%s,\"endpoints\":[", port, + tls ? "true" : "false"); + for (i = 0; paths[i] != NULL; i++) { + char status[256]; + char *resp = NULL; + size_t len = 0; + int code; + int open; + int json = 0; + + status[0] = '\0'; + if (apex_http_once(target, port, tls, "GET", paths[i], NULL, NULL, + 8000, &resp, &len, status, + sizeof(status)) != 0) + continue; + tested++; + code = apex_status_code(status); + open = code >= 200 && code < 300; + if (resp != NULL && find_bytes(resp, len, "{")) + json = 1; + if (open) { + open_count++; + if (evidence == NULL) { + char label[64]; + + snprintf(label, sizeof(label), "consul-%.20s", + paths[i] + 4); + evidence = apex_evidence_save(label, resp, len); + } + } + if (!first) + mcp_buf_puts(&b, ","); + first = 0; + mcp_buf_puts(&b, "{\"path\":"); + apex_json_str(&b, paths[i]); + mcp_buf_printf(&b, ",\"status\":%d,\"bytes\":%lu,\"open\":%s," + "\"json\":%s}", + code, (unsigned long)len, + open ? "true" : "false", json ? "true" : "false"); + free(resp); + } + mcp_buf_printf(&b, "],\"tested\":%d,\"open_count\":%d,\"evidence\":", + tested, open_count); + if (evidence != NULL) + apex_json_str(&b, evidence); + else + mcp_buf_puts(&b, "null"); + mcp_buf_puts(&b, "}"); + apex_audit("consul_api_probe", target, port, open_count == 0, + open_count == 0 + ? "the Consul API requires an ACL token" + : "the Consul API answers without a token", "list"); + mcp_reply_json(c, b.p); + free(evidence); + mcp_buf_free(&b); +} + const mcp_tool TOOLS_RED[] = { { "vuln_check", "Non-destructive verification of exposed artifacts on one web target: " @@ -7165,6 +7252,16 @@ const mcp_tool TOOLS_RED[] = { "\"tls\":{\"type\":\"boolean\"}}," "\"required\":[\"target\"]}", t_docker_registry_probe }, + { "consul_api_probe", + "Query the Consul HTTP API without an ACL token: /v1/agent/self and " + "/v1/catalog/services. A 2xx JSON answer is unauthenticated access; the " + "first answer is saved as evidence. Gate: the target.", + "{\"type\":\"object\",\"properties\":{" + "\"target\":{\"type\":\"string\"}," + "\"port\":{\"type\":\"integer\",\"description\":\"default 8500\"}," + "\"tls\":{\"type\":\"boolean\"}}," + "\"required\":[\"target\"]}", + t_consul_api_probe }, { "dns_ns_consistency", "Fetch NS records for a domain from the parent resolver and from its own " "first nameserver (resolved to an address), then report names present on " diff --git a/tests/gate_audit.py b/tests/gate_audit.py index 1606622..b038e5c 100755 --- a/tests/gate_audit.py +++ b/tests/gate_audit.py @@ -21,7 +21,7 @@ import re import sys HERE = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -EXPECTED_TOOLS = 238 +EXPECTED_TOOLS = 242 GATE = ("gated(", "gated_cmd(", "gated_run(", "apex_scope_check(") NET = ("http_once(", "tcp_connect_host(", "tcp_probe(", "apex_run(") # Tools whose "target" argument filters local records; they never touch it. diff --git a/tests/run.py b/tests/run.py index 248a198..f9a1cdd 100755 --- a/tests/run.py +++ b/tests/run.py @@ -134,7 +134,7 @@ res, _ = run([ ], home=empty) check("tools/list answers", any(r.get("id") == 1 for r in res)) tools = [r for r in res if r.get("id") == 1][0]["result"]["tools"] -check("238 tools registered", len(tools) == 238, len(tools)) +check("242 tools registered", len(tools) == 242, len(tools)) names = {t["name"] for t in tools} for need in ("portscan", "nmap", "http_request", "marauder_scan", "method_playbook", "finding_add", "finding_update", "scope_check", @@ -1560,7 +1560,7 @@ try: check("http initialize", init["result"]["serverInfo"]["name"] == "apex-twin", init) listing = post({"jsonrpc": "2.0", "id": 2, "method": "tools/list"}) - check("http tools/list", len(listing["result"]["tools"]) == 238) + check("http tools/list", len(listing["result"]["tools"]) == 242) shown = post(call(3, "scope_show")) check("http scope_show", '"loaded":true' in shown["result"]["content"][0]["text"]) # the id must be the request's id, not one found inside the arguments @@ -10900,6 +10900,195 @@ shutil.rmtree(vr_root, ignore_errors=True) shutil.rmtree(vr_good_root, ignore_errors=True) shutil.rmtree(cfg22_home, ignore_errors=True) +print("\n== FreeRADIUS, containerd, NATS and Consul ==\n") + +rad_root = tempfile.mkdtemp(prefix="apex-rad-") +os.makedirs(os.path.join(rad_root, "etc", "freeradius", "3.0")) +rad_clients = os.path.join(rad_root, "etc", "freeradius", "3.0", + "clients.conf") +with open(rad_clients, "w") as f: + f.write("client test {\n") + f.write(" ipaddr = 10.0.0.0/8\n") + f.write(" secret = testing123\n") + f.write(" require_message_authenticator = no\n") + f.write("}\n") +os.chmod(rad_clients, 0o666) +with open(os.path.join(rad_root, "etc", "freeradius", "3.0", "users"), + "w") as f: + f.write("DEFAULT Auth-Type := Accept\n") +rad_good_root = tempfile.mkdtemp(prefix="apex-radg-") +os.makedirs(os.path.join(rad_good_root, "etc", "freeradius", "3.0")) +with open(os.path.join(rad_good_root, "etc", "freeradius", "3.0", + "clients.conf"), "w") as f: + f.write("client test {\n") + f.write(" ipaddr = 10.0.0.0/8\n") + f.write(" secret = a-very-long-random-shared-secret\n") + f.write(" require_message_authenticator = yes\n") + f.write("}\n") +with open(os.path.join(rad_good_root, "etc", "freeradius", "3.0", "users"), + "w") as f: + f.write("alice Cleartext-Password := \"secret\"\n") + +ct_root = tempfile.mkdtemp(prefix="apex-ct-") +os.makedirs(os.path.join(ct_root, "etc", "containerd")) +ct_bad = os.path.join(ct_root, "etc", "containerd", "config.toml") +with open(ct_bad, "w") as f: + f.write("[grpc]\n") + f.write(' address = "0.0.0.0:1234"\n') + f.write("[plugins.\"io.containerd.grpc.v1.cri\"]\n") + f.write(' sandbox_image = "registry.k8s.io/pause:3.9"\n') + f.write(" no_pivot_root = true\n") +os.chmod(ct_bad, 0o666) +ct_good_root = tempfile.mkdtemp(prefix="apex-ctg-") +os.makedirs(os.path.join(ct_good_root, "etc", "containerd")) +with open(os.path.join(ct_good_root, "etc", "containerd", "config.toml"), + "w") as f: + f.write("[grpc]\n") + f.write(' address = "/run/containerd/containerd.sock"\n') + f.write("[plugins.\"io.containerd.grpc.v1.cri\"]\n") + f.write(' sandbox_image = "registry.k8s.io/pause@sha256:' + '0123456789abcdef"\n') + f.write(" no_pivot_root = false\n") + +nt_root = tempfile.mkdtemp(prefix="apex-nt-") +os.makedirs(os.path.join(nt_root, "etc", "nats")) +nt_bad = os.path.join(nt_root, "etc", "nats", "nats-server.conf") +with open(nt_bad, "w") as f: + f.write("port: 4222\n") + f.write("http: 0.0.0.0:8222\n") + f.write("debug: true\n") +os.chmod(nt_bad, 0o666) +nt_good_root = tempfile.mkdtemp(prefix="apex-ntg-") +os.makedirs(os.path.join(nt_good_root, "etc", "nats")) +with open(os.path.join(nt_good_root, "etc", "nats", + "nats-server.conf"), "w") as f: + f.write("port: 4222\n") + f.write("http: 127.0.0.1:8222\n") + f.write("debug: false\n") + f.write("authorization {\n timeout: 2\n}\n") + f.write("tls {\n cert_file: \"/etc/nats/server.pem\"\n}\n") + + +class _ConsulHandler(http.server.BaseHTTPRequestHandler): + def do_GET(self): + path = urllib.parse.urlsplit(self.path).path + docs = { + "/v1/agent/self": b'{"Config":{"Datacenter":"dc1"},"Member":{}}', + "/v1/catalog/services": b'{"consul":[],"web":["http"]}', + } + body = docs.get(path, b'{"error":"not found"}') + code = 200 if path in docs else 404 + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + + def log_message(self, *args): + pass + + +class _ConsulAuthHandler(http.server.BaseHTTPRequestHandler): + def do_GET(self): + body = b'{"error":"ACL token required"}' + self.send_response(403) + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + + def log_message(self, *args): + pass + + +consul_srv = http.server.HTTPServer(("127.0.0.1", 0), _ConsulHandler) +consul_port = consul_srv.server_address[1] +threading.Thread(target=consul_srv.serve_forever, daemon=True).start() +consul_auth_srv = http.server.HTTPServer(("127.0.0.1", 0), + _ConsulAuthHandler) +consul_auth_port = consul_auth_srv.server_address[1] +threading.Thread(target=consul_auth_srv.serve_forever, daemon=True).start() + +cfg23_home = tempfile.mkdtemp(prefix="apex-cfg23-home-") +write_scope(cfg23_home, {"label": "cfg23", "expires": "2099-12-31T23:59:59Z", + "allow_private": True, "allow_ip": ["127.0.0.1"], + "allow_port": [str(consul_port), + str(consul_auth_port)]}) +res, _ = run([ + call(1, "freeradius_audit", root=rad_root), + call(2, "freeradius_audit", root=rad_good_root), + call(3, "containerd_audit", root=ct_root), + call(4, "containerd_audit", root=ct_good_root), + call(5, "nats_audit", root=nt_root), + call(6, "nats_audit", root=nt_good_root), + call(7, "consul_api_probe", target="127.0.0.1", port=consul_port, + tls=False), + call(8, "consul_api_probe", target="127.0.0.1", port=consul_auth_port, + tls=False), + call(9, "consul_api_probe", target="198.51.100.9", port=8500), + call(10, "freeradius_audit", root="/nonexistent-rad"), + call(11, "containerd_audit", root="/nonexistent-ct"), + call(12, "nats_audit", root="/nonexistent-nt"), +], home=cfg23_home) +by_id = {r.get("id"): r for r in res} +rad = json.loads(text(by_id[1])) +rad_kinds = {f["id"] for f in rad["findings"]} +check("freeradius_audit flags default secret, BlastRADIUS and Accept", + {"world-writable", "default-secret", "no-message-authenticator", + "accept-auth"} <= rad_kinds and rad["default_secret"] is True and + rad["accept_auth"] is True, rad) +rad_good = json.loads(text(by_id[2])) +check("freeradius_audit accepts a strong client", + rad_good["findings_count"] == 0 and + rad_good["default_secret"] is False and + rad_good["message_authenticator"] is True, rad_good) +ct = json.loads(text(by_id[3])) +ct_kinds = {f["id"] for f in ct["findings"]} +check("containerd_audit flags TCP gRPC, unpinned image and pivot root", + {"world-writable", "no-grpc-tls", "unpinned-sandbox-image", + "no-pivot-root"} <= ct_kinds and ct["tcp"] is True and + ct["pinned"] is False, ct) +ct_good = json.loads(text(by_id[4])) +check("containerd_audit accepts a unix socket and a pinned image", + ct_good["findings_count"] == 0 and ct_good["tcp"] is False and + ct_good["pinned"] is True, ct_good) +nt = json.loads(text(by_id[5])) +nt_kinds = {f["id"] for f in nt["findings"]} +check("nats_audit flags no auth, no TLS and an open monitor", + {"world-writable", "no-authorization", "no-tls", "monitoring-all", + "debug-enabled"} <= nt_kinds and nt["authorization"] is False, nt) +nt_good = json.loads(text(by_id[6])) +check("nats_audit accepts auth, TLS and a loopback monitor", + nt_good["findings_count"] == 0 and nt_good["authorization"] is True and + nt_good["tls"] is True and nt_good["monitoring_all"] is False, nt_good) +consul = json.loads(text(by_id[7])) +check("consul_api_probe finds the unauthenticated API", + consul["open_count"] == 2 and consul["tested"] == 2 and + consul["evidence"] and os.path.isfile(consul["evidence"]), consul) +consul_auth = json.loads(text(by_id[8])) +check("consul_api_probe reports a token-protected API", + consul_auth["open_count"] == 0 and + all(e["status"] == 403 for e in consul_auth["endpoints"]), + consul_auth) +check("consul_api_probe refuses an out-of-scope target", err(by_id[9]), + text(by_id[9])) +check("freeradius_audit reports a missing config", err(by_id[10]), + text(by_id[10])) +check("containerd_audit reports a missing config", err(by_id[11]), + text(by_id[11])) +check("nats_audit reports a missing config", err(by_id[12]), + text(by_id[12])) +consul_srv.shutdown() +consul_srv.server_close() +consul_auth_srv.shutdown() +consul_auth_srv.server_close() +shutil.rmtree(rad_root, ignore_errors=True) +shutil.rmtree(rad_good_root, ignore_errors=True) +shutil.rmtree(ct_root, ignore_errors=True) +shutil.rmtree(ct_good_root, ignore_errors=True) +shutil.rmtree(nt_root, ignore_errors=True) +shutil.rmtree(nt_good_root, ignore_errors=True) +shutil.rmtree(cfg23_home, ignore_errors=True) + print("\n== esp32 tools ==") @@ -10912,6 +11101,7 @@ print("\n== esp32 tools ==") + esp_home = tempfile.mkdtemp(prefix="apex-esp-") write_scope(esp_home, {"label": "esp", "expires": "2099-12-31T23:59:59Z"}) esp_m, esp_s = pty.openpty()