diff --git a/CHANGELOG.md b/CHANGELOG.md index be3c31f..24ce6b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,23 @@ 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.68.0 — 2026-11-23 — this change +## 1.69.0 — 2026-11-23 — this change + +- **Podman** (`podman_audit`): `registries.conf`: a `[[registry]]` block with + `insecure = true` or an `http://` location, `short-name-mode = "disabled"`, + and `policy.json` missing or without `signedBy`. +- **BuildKit** (`buildkitd_audit`): `buildkitd.toml`: a `[registry]` table with + `insecure = true`, a `tcp://` address without a CA, no rootless worker. +- **supervisord** (`supervisor_audit`): an `[inet_http_server]` beyond + localhost, the same without a `username`, `[program:*]` with `user=root`. +- **Portainer** (`portainer_probe`): `/api/status` and + `/api/users/admin/check` without credentials; a 2xx admin check is an open + API and is saved as evidence. Reports whether setup is still open. +- All four also flag world-writable files. +- Tests with registries/policy, buildkitd.toml, supervisord.conf and two + Portainer fixtures (open and 403). 1290 checks. + +## 1.68.0 — 2026-11-23 — 98c7ae7 - **git config** (`gitconfig_audit`): `/etc/gitconfig`: `http.sslVerify=false`, a URL rewrite to `http://`, `credential.helper=store` and a world-writable @@ -20,7 +36,7 @@ it; the current entry is the commit that adds this section. - **Harbor** (`harbor_probe`): `/api/v2.0/health` and `/api/v2.0/projects` without credentials; a 2xx project list is an open API, saved as evidence. - Tests with git/npm/pip config trees and two Harbor fixtures (open and 401). - 1278 checks. Coverage 84.6%. + 1278 checks. Coverage 84.7%. ## 1.67.0 — 2026-11-23 — 7b853ad diff --git a/README.md b/README.md index c673116..236f5ef 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. -**310 tools · 1278 end-to-end checks · 340 fuzz cases · 84.6% line coverage · ~1041 KiB** +**314 tools · 1290 end-to-end checks · 240 fuzz cases · 84.7% line coverage · ~1041 KiB** ``` $ ./apex-twin --check -{"version":"1.68.0","tools":310,...,"scope":{"loaded":true,...}} +{"version":"1.69.0","tools":314,...,"scope":{"loaded":true,...}} ``` APEX TWIN was inspired by [ARTEX](https://github.com/Autumn-27/artex) (scope @@ -227,6 +227,7 @@ uploads and secret-file reads. It does not execute anything. | `nexus_probe` | `/service/rest/v1/repositories` without auth; list as evidence | | `gitea_api_probe` | `/api/v1/admin/users` without auth; admin list as evidence | | `harbor_probe` | `/api/v2.0/projects` without auth; project list as evidence | +| `portainer_probe` | `/api/status` and `/api/users/admin/check`; setup-open or unauth admin check | | `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 | @@ -507,6 +508,9 @@ command in the allowlist: a receiver is a scanner, a transmitter is an attack. | `gitconfig_audit` | gitconfig: sslVerify off, HTTP rewrite, store helper | | `npmrc_audit` | npmrc: strict-ssl off, HTTP registry, cleartext token | | `pip_conf_audit` | pip.conf: HTTP index, trusted-host override | +| `podman_audit` | registries.conf: insecure or http registry, unsigned policy | +| `buildkitd_audit` | buildkitd.toml: insecure registry, tcp without CA, no rootless | +| `supervisor_audit` | supervisord.conf: exposed web UI, no username, root programs | ### ESP32 @@ -974,13 +978,14 @@ src/tools_red.c vuln_check, subdomain_enum, cred_test, vhost_discovery, influxdb_probe, clickhouse_probe, kibana_api_probe, logstash_api_probe, amqp_probe, tls_chain_probe, smtp_auth_probe, jenkins_api_probe, nexus_probe, - gitea_api_probe, harbor_probe + gitea_api_probe, harbor_probe, portainer_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, ssh_key_audit, suid_baseline, cap_baseline, module_audit, package_integrity, mount_audit, firewall_audit, - systemd_audit + systemd_audit (and ~160 more service config audits, + newest: podman_audit, buildkitd_audit, supervisor_audit) src/tools_esp.c esp_info, esp_monitor, esp_read_flash, esp_flash, esp_partition_table, esp_nvs_parse, firmware_scan, esp_app_info, esp_image_verify @@ -1017,7 +1022,7 @@ examples/ scope.json ## Status -1.68.0. Built and run: 310 tools, 1278 checks, 340 fuzz cases and 84.6% line +1.69.0. Built and run: 314 tools, 1290 checks, 240 fuzz cases and 84.7% 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 0c4b0e2..c010141 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.68.x** (`src/apex.h`, `APEX_VERSION`). Security fixes land on the current +is **1.69.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. diff --git a/docs/apex-twin.1 b/docs/apex-twin.1 index c8d9588..e2409e0 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.68.0" "User Commands" +.TH APEX-TWIN 1 "2026-11-21" "apex-twin 1.69.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, consul_api_probe, couchdb_probe, prometheus_api_probe, rabbitmq_management_probe, grafana_api_probe, minio_admin_probe, nats_monitoring_probe, influxdb_probe, clickhouse_probe, kibana_api_probe, logstash_api_probe, amqp_probe, tls_chain_probe, smtp_auth_probe, jenkins_api_probe, nexus_probe, gitea_api_probe, harbor_probe), +websocket_origin_check, hsts_audit, tls_hostname_check, ftp_tls_probe, zookeeper_4lw_probe, etcd_api_probe, docker_registry_probe, consul_api_probe, couchdb_probe, prometheus_api_probe, rabbitmq_management_probe, grafana_api_probe, minio_admin_probe, nats_monitoring_probe, influxdb_probe, clickhouse_probe, kibana_api_probe, logstash_api_probe, amqp_probe, tls_chain_probe, smtp_auth_probe, jenkins_api_probe, nexus_probe, gitea_api_probe, harbor_probe, portainer_probe), defensive audits (hardening_audit, log_audit, ioc_scan, web_log_audit, detect_rules, integrity_baseline, mitre_map, process_snapshot, persistence_audit, @@ -82,7 +82,8 @@ openssl_cnf_audit, curlrc_audit, wgetrc_audit, resolved_audit, udev_audit, issue_audit, usbguard_audit, certbot_audit, wpa_supplicant_audit, bluetooth_audit, networkmanager_audit, dbus_audit, passwd_audit, authorized_keys_audit, ld_preload_audit, gitconfig_audit, npmrc_audit, -pip_conf_audit) +pip_conf_audit, podman_audit, buildkitd_audit, +supervisor_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 b70396c..9fbd24f 100644 --- a/src/apex.h +++ b/src/apex.h @@ -15,7 +15,7 @@ #include "mcp.h" -#define APEX_VERSION "1.68.0" +#define APEX_VERSION "1.69.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 fe25107..8b6cb5c 100644 --- a/src/tools_blue.c +++ b/src/tools_blue.c @@ -20736,6 +20736,500 @@ static void t_pip_conf_audit(mcp_conn *c, const mcp_json *args, void *user) mcp_buf_free(&findings); } +/* -------------------------------------------------------------- podman_audit */ + +/* registries.conf repeats [[registry]] blocks; a flat key/value lookup would + * mix them up. This reader hands back one block at a time. */ +struct podman_registry { + int insecure; + int plain_http; + int seen; +}; + +static int podman_block_next(const char *text, size_t *offset, + struct podman_registry *reg) +{ + const char *p; + int in_block = 0; + + reg->insecure = 0; + reg->plain_http = 0; + reg->seen = 0; + for (p = text + *offset; *p != '\0'; ) { + const char *eol = strchr(p, '\n'); + size_t len = eol != NULL ? (size_t)(eol - p) : strlen(p); + char line[1024]; + char *s; + + if (len >= sizeof(line)) + len = sizeof(line) - 1; + memcpy(line, p, len); + line[len] = '\0'; + s = line; + while (*s == ' ' || *s == '\t') + s++; + if (*s == '[') { + if (in_block) { + *offset = (size_t)(p - text); + return 1; + } + if (strncmp(s, "[[registry]]", 12) == 0) + in_block = 1; + } else if (in_block && *s != '#' && *s != ';') { + char *eq = strchr(s, '='); + + if (eq != NULL) { + char *v = eq + 1; + size_t vl; + size_t kl; + + *eq = '\0'; + kl = strlen(s); + while (kl > 0 && (s[kl - 1] == ' ' || + s[kl - 1] == '\t')) + s[--kl] = '\0'; + while (*v == ' ' || *v == '\t') + 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++; + vl -= 2; + v[vl] = '\0'; + } + if (strcasecmp(s, "insecure") == 0 && + strcasecmp(v, "true") == 0) + reg->insecure = 1; + if (strcasecmp(s, "location") == 0) { + reg->seen = 1; + if (strncmp(v, "http://", 7) == 0) + reg->plain_http = 1; + } + } + } + if (eol == NULL) + break; + p = eol + 1; + } + *offset = strlen(text); + return in_block; +} + +static void t_podman_audit(mcp_conn *c, const mcp_json *args, void *user) +{ + const char *root = mcp_arg_string(args, "root"); + char path[1024]; + static char text[131072]; + struct stat st; + mcp_buf b; + mcp_buf findings; + int first = 1; + int count = 0; + int files_read = 0; + int insecure = 0; + int plain_http = 0; + int short_names = 0; + int policy_missing = 1; + int policy_unsigned = 0; + size_t offset = 0; + size_t textlen; + + (void)user; + if (root == NULL) + root = ""; + if (strlen(root) > 400) { + mcp_reply_error(c, "root path too long"); + return; + } + mcp_buf_init(&findings); + snprintf(path, sizeof(path), "%s/etc/containers/registries.conf", root); + if (read_text(path, text, sizeof(text)) > 0) { + files_read++; + if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) { + add_finding(&findings, &first, "world-writable", "high", + "%s is writable by anyone", path); + count++; + } + if (strcasestr(text, "short-name-mode") != NULL && + strstr(text, "disabled") != NULL) + short_names = 1; + textlen = strlen(text); + while (offset < textlen) { + struct podman_registry reg; + + if (!podman_block_next(text, &offset, ®)) + break; + if (reg.insecure) + insecure++; + if (reg.plain_http) + plain_http++; + } + } + snprintf(path, sizeof(path), "%s/etc/containers/policy.json", root); + if (read_text(path, text, sizeof(text)) > 0) { + policy_missing = 0; + files_read++; + if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) { + add_finding(&findings, &first, "world-writable", "high", + "%s is writable by anyone", path); + count++; + } + if (strstr(text, "signedBy") == NULL) + policy_unsigned = 1; + } + if (files_read == 0) { + mcp_buf_free(&findings); + mcp_reply_error(c, "cannot read a containers configuration"); + return; + } + if (insecure > 0) { + add_finding(&findings, &first, "insecure-registry", "high", + "%d registry block(s) skip TLS verification", insecure); + count++; + } + if (plain_http > 0) { + add_finding(&findings, &first, "plain-http-registry", "medium", + "%d registry block(s) use an http:// location", + plain_http); + count++; + } + if (short_names) { + add_finding(&findings, &first, "short-name-disabled", "low", + "short-name-mode=disabled resolves a bare name against any registry"); + count++; + } + if (policy_missing) { + add_finding(&findings, &first, "no-policy", "medium", + "policy.json is missing, so pulled images are not verified"); + count++; + } else if (policy_unsigned) { + add_finding(&findings, &first, "unsigned-policy", "medium", + "policy.json carries no signedBy, so no signature is checked"); + count++; + } + mcp_buf_init(&b); + mcp_buf_puts(&b, "{\"root\":"); + apex_json_str(&b, root); + mcp_buf_printf(&b, ",\"files\":%d,\"insecure_registries\":%d," + "\"plain_http_registries\":%d,\"short_name_mode\":\"%s\"," + "\"policy\":\"%s\",\"signature_verified\":%s," + "\"findings_count\":%d,\"findings\":[", + files_read, insecure, plain_http, + short_names ? "disabled" : "enforcing", + policy_missing ? "missing" : "present", + (policy_missing || policy_unsigned) ? "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); +} + +/* ------------------------------------------------------------ buildkitd_audit */ + +/* buildkitd.toml nests its keys in tables ([registry."host"], [worker.oci]), + * so a key only means something together with the header above it. */ +static void t_buildkitd_audit(mcp_conn *c, const mcp_json *args, void *user) +{ + const char *root = mcp_arg_string(args, "root"); + char path[1024]; + static char text[131072]; + struct stat st; + mcp_buf b; + mcp_buf findings; + int first = 1; + int count = 0; + int insecure = 0; + int plain_tcp = 0; + int rootless = 0; + int table_registry = 0; + int table_worker = 0; + char *copy; + char *save = NULL; + char *line; + + (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/buildkit/buildkitd.toml", root); + if (read_text(path, text, sizeof(text)) <= 0) { + mcp_reply_error(c, "cannot read buildkitd.toml"); + return; + } + mcp_buf_init(&findings); + if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) { + add_finding(&findings, &first, "world-writable", "high", + "buildkitd.toml is writable by anyone"); + count++; + } + copy = strdup(text); + if (copy == NULL) { + mcp_buf_free(&findings); + mcp_reply_error(c, "out of memory"); + return; + } + line = strtok_r(copy, "\n", &save); + while (line != NULL) { + char *s = line; + + while (*s == ' ' || *s == '\t') + s++; + if (*s == '[') { + table_registry = strncmp(s, "[registry", 9) == 0; + table_worker = strncmp(s, "[worker", 7) == 0; + } else if (*s != '#' && *s != ';' && *s != '\0') { + char *eq = strchr(s, '='); + + if (eq != NULL) { + char *v = eq + 1; + size_t vl; + size_t kl; + + *eq = '\0'; + kl = strlen(s); + while (kl > 0 && (s[kl - 1] == ' ' || + s[kl - 1] == '\t')) + s[--kl] = '\0'; + while (*v == ' ' || *v == '\t') + v++; + vl = strlen(v); + while (vl > 0 && (v[vl - 1] == ' ' || + v[vl - 1] == '\t' || + v[vl - 1] == '\r' || + v[vl - 1] == '"')) + v[--vl] = '\0'; + if (v[0] == '"') + v++; + if (table_registry && + strcasecmp(s, "insecure") == 0 && + strcasecmp(v, "true") == 0) + insecure++; + if (table_registry && + strcasecmp(s, "address") == 0 && + strstr(v, "tcp://") != NULL && + strstr(text, "ca=") == NULL) + plain_tcp = 1; + if (table_worker && + strcasecmp(s, "rootless") == 0 && + strcasecmp(v, "true") == 0) + rootless = 1; + } + } + line = strtok_r(NULL, "\n", &save); + } + free(copy); + if (insecure > 0) { + add_finding(&findings, &first, "insecure-registry", "high", + "%d registry table(s) skip TLS verification", insecure); + count++; + } + if (plain_tcp) { + add_finding(&findings, &first, "registry-without-ca", "high", + "a registry is reached over tcp:// without a CA"); + count++; + } + if (!rootless) { + add_finding(&findings, &first, "not-rootless", "low", + "no worker sets rootless=true"); + count++; + } + mcp_buf_init(&b); + mcp_buf_puts(&b, "{\"root\":"); + apex_json_str(&b, root); + mcp_buf_printf(&b, ",\"insecure_registries\":%d," + "\"plain_tcp_registry\":%s,\"rootless\":%s," + "\"findings_count\":%d,\"findings\":[", + insecure, plain_tcp ? "true" : "false", + rootless ? "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); +} + +/* ----------------------------------------------------------- supervisor_audit */ + +/* supervisord.conf is INI with repeating [program:*] sections, so a value is + * only meaningful together with the header above it. */ +struct supervisor_state { + char server_url[256]; + int has_username; + int root_programs; +}; + +static void supervisor_walk(const char *text, struct supervisor_state *st) +{ + const char *p = text; + char section[128]; + + section[0] = '\0'; + while (p != NULL && *p != '\0') { + const char *eol = strchr(p, '\n'); + size_t len = eol != NULL ? (size_t)(eol - p) : strlen(p); + char line[1024]; + char *s; + + if (len >= sizeof(line)) + len = sizeof(line) - 1; + memcpy(line, p, len); + line[len] = '\0'; + s = line; + while (*s == ' ' || *s == '\t') + s++; + if (*s == '[') { + char *close = strchr(s, ']'); + size_t cl; + + if (close == NULL) { + p = eol != NULL ? eol + 1 : NULL; + continue; + } + cl = (size_t)(close - s - 1); + if (cl >= sizeof(section)) + cl = sizeof(section) - 1; + memcpy(section, s + 1, cl); + section[cl] = '\0'; + } else if (*s != '#' && *s != ';' && *s != '\0') { + char *sep = strpbrk(s, "=:"); + char *v; + size_t kl; + + if (sep != NULL) { + char key[128]; + + *sep = '\0'; + kl = strlen(s); + if (kl >= sizeof(key)) + kl = sizeof(key) - 1; + memcpy(key, s, kl); + key[kl] = '\0'; + while (kl > 0 && (key[kl - 1] == ' ' || + key[kl - 1] == '\t')) + key[--kl] = '\0'; + v = sep + 1; + while (*v == ' ' || *v == '\t') + v++; + if (strcasecmp(section, "inet_http_server") == 0) { + if (strcasecmp(key, "serverurl") == 0 || + strcasecmp(key, "port") == 0) { + size_t vl = strlen(v); + + if (vl >= sizeof(st->server_url)) + vl = sizeof(st->server_url) - 1; + memcpy(st->server_url, v, vl); + st->server_url[vl] = '\0'; + } + if (strcasecmp(key, "username") == 0 && + v[0] != '\0') + st->has_username = 1; + } + if (strncmp(section, "program", 7) == 0 && + strcasecmp(key, "user") == 0 && + strcmp(v, "root") == 0) + st->root_programs++; + } + } + p = eol != NULL ? eol + 1 : NULL; + } +} + +static void t_supervisor_audit(mcp_conn *c, const mcp_json *args, void *user) +{ + static const char *files[] = { "/etc/supervisor/supervisord.conf", + "/etc/supervisord.conf", NULL }; + const char *root = mcp_arg_string(args, "root"); + char path[1024]; + static char text[131072]; + struct stat st; + mcp_buf b; + mcp_buf findings; + int first = 1; + int count = 0; + int files_read = 0; + int http_exposed = 0; + int http_no_auth = 0; + int root_programs = 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++) { + struct supervisor_state state; + + snprintf(path, sizeof(path), "%.400s%s", root, files[i]); + if (read_text(path, text, sizeof(text)) <= 0) + continue; + files_read++; + if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) { + add_finding(&findings, &first, "world-writable", "high", + "%s is writable by anyone", path); + count++; + } + memset(&state, 0, sizeof(state)); + supervisor_walk(text, &state); + root_programs += state.root_programs; + if (state.server_url[0] != '\0' && + strstr(state.server_url, "127.0.0.1") == NULL && + strstr(state.server_url, "localhost") == NULL && + strstr(state.server_url, "[::1]") == NULL) { + http_exposed = 1; + if (!state.has_username) + http_no_auth = 1; + } + } + if (files_read == 0) { + mcp_buf_free(&findings); + mcp_reply_error(c, "cannot read a supervisord configuration"); + return; + } + if (http_exposed) { + add_finding(&findings, &first, "http-exposed", "high", + "the supervisord web interface listens beyond localhost"); + count++; + } + if (http_no_auth) { + add_finding(&findings, &first, "http-no-auth", "critical", + "the web interface has no username"); + count++; + } + if (root_programs > 0) { + add_finding(&findings, &first, "root-program", "medium", + "%d program(s) run as root", root_programs); + count++; + } + mcp_buf_init(&b); + mcp_buf_puts(&b, "{\"root\":"); + apex_json_str(&b, root); + mcp_buf_printf(&b, ",\"files\":%d,\"http_exposed\":%s," + "\"http_auth\":%s,\"root_programs\":%d," + "\"findings_count\":%d,\"findings\":[", + files_read, http_exposed ? "true" : "false", + http_no_auth ? "false" : "true", root_programs, 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, " @@ -21932,6 +22426,28 @@ const mcp_tool TOOLS_BLUE[] = { "{\"type\":\"object\",\"properties\":{" "\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}", t_pip_conf_audit }, + { "podman_audit", + "Parse registries.conf and policy.json (root prefix): a [[registry]] " + "block with insecure=true or an http:// location, short-name-mode=" + "disabled, a missing policy.json or one without signedBy, and " + "world-writable files. Read-only.", + "{\"type\":\"object\",\"properties\":{" + "\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}", + t_podman_audit }, + { "buildkitd_audit", + "Parse buildkitd.toml (root prefix): a [registry] table with " + "insecure=true, a tcp:// address without a CA, no rootless worker and " + "a world-writable file. Read-only.", + "{\"type\":\"object\",\"properties\":{" + "\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}", + t_buildkitd_audit }, + { "supervisor_audit", + "Parse supervisord.conf (root prefix): an [inet_http_server] beyond " + "localhost, the same without a username, [program:*] running as root " + "and world-writable files. Read-only.", + "{\"type\":\"object\",\"properties\":{" + "\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}", + t_supervisor_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 2076f9c..64de5b2 100644 --- a/src/tools_red.c +++ b/src/tools_red.c @@ -8202,6 +8202,103 @@ static void t_harbor_probe(mcp_conn *c, const mcp_json *args, void *user) mcp_buf_free(&b); } +/* ------------------------------------------------------------- portainer_probe */ + +static void t_portainer_probe(mcp_conn *c, const mcp_json *args, void *user) +{ + static const char *paths[] = { "/api/status", + "/api/users/admin/check", NULL }; + const char *target = mcp_arg_string(args, "target"); + int port = mcp_arg_int(args, "port", 9000); + int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port)); + mcp_buf b; + + int first = 1; + int tested = 0; + int open_count = 0; + int admin_check = 0; + int setup_allowed = 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, "portainer_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; + + 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 (open) + open_count++; + if (open && strstr(paths[i], "admin") != NULL && + (resp == NULL || strstr(resp, "\"authorization\"") == NULL)) { + admin_check = 1; + if (evidence == NULL) { + /* A 204 carries no body, so the status line is the + * proof: it says the check answered 2xx without auth. */ + if (len > 0 && resp != NULL) + evidence = apex_evidence_save( + "portainer-admin-check", resp, len); + else + evidence = apex_evidence_save( + "portainer-admin-check", status, + strlen(status)); + } + } + if (open && strstr(paths[i], "/api/status") != NULL && + resp != NULL && strstr(resp, "true") != NULL) + setup_allowed = 1; + 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}", + code, (unsigned long)len, + open ? "true" : "false"); + free(resp); + } + mcp_buf_printf(&b, "],\"tested\":%d,\"open_count\":%d," + "\"setup_open\":%s,\"evidence\":", + tested, open_count, setup_allowed ? "true" : "false"); + if (evidence != NULL) + apex_json_str(&b, evidence); + else + mcp_buf_puts(&b, "null"); + mcp_buf_puts(&b, "}"); + apex_audit("portainer_probe", target, port, !admin_check, + admin_check + ? "the Portainer admin check answers without authentication" + : "the Portainer admin check requires authentication", "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: " @@ -8904,6 +9001,17 @@ const mcp_tool TOOLS_RED[] = { "\"tls\":{\"type\":\"boolean\"}}," "\"required\":[\"target\"]}", t_harbor_probe }, + { "portainer_probe", + "Get /api/status and /api/users/admin/check without credentials; a 2xx " + "admin check without an authorization error is an unauthenticated " + "Portainer API and is saved as evidence. Also reports whether the " + "instance still allows setup. Gate: the target.", + "{\"type\":\"object\",\"properties\":{" + "\"target\":{\"type\":\"string\"}," + "\"port\":{\"type\":\"integer\",\"description\":\"default 9000\"}," + "\"tls\":{\"type\":\"boolean\"}}," + "\"required\":[\"target\"]}", + t_portainer_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 0098dd0..eafe3be 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 = 310 +EXPECTED_TOOLS = 314 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 9ef8133..79a77b0 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("310 tools registered", len(tools) == 310, len(tools)) +check("314 tools registered", len(tools) == 314, 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"]) == 310) + check("http tools/list", len(listing["result"]["tools"]) == 314) 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 @@ -14106,6 +14106,206 @@ shutil.rmtree(pp_root, ignore_errors=True) shutil.rmtree(pp_good_root, ignore_errors=True) shutil.rmtree(cfg40_home, ignore_errors=True) +print("\n== podman, buildkit, supervisor and Portainer ==\n") + +pdm_root = tempfile.mkdtemp(prefix="apex-pdm-") +os.makedirs(os.path.join(pdm_root, "etc", "containers")) +pdm_bad = os.path.join(pdm_root, "etc", "containers", "registries.conf") +with open(pdm_bad, "w") as f: + f.write('short-name-mode = "disabled"\n') + f.write("[[registry]]\n") + f.write('location = "https://registry.example.test"\n') + f.write("insecure = false\n") + f.write("[[registry]]\n") + f.write('location = "http://mirror.example.test"\n') + f.write("insecure = true\n") +os.chmod(pdm_bad, 0o666) +pdm_good_root = tempfile.mkdtemp(prefix="apex-pdmg-") +os.makedirs(os.path.join(pdm_good_root, "etc", "containers")) +with open(os.path.join(pdm_good_root, "etc", "containers", + "registries.conf"), "w") as f: + f.write('short-name-mode = "enforcing"\n') + f.write("[[registry]]\n") + f.write('location = "https://registry.example.test"\n') + f.write("insecure = false\n") +with open(os.path.join(pdm_good_root, "etc", "containers", + "policy.json"), "w") as f: + f.write('{"default":[{"type":"reject"}],') + f.write('"transports":{"docker":{"registry.example.test":') + f.write('[{"type":"signedBy","keyType":"atomic",') + f.write('"keyPath":"/etc/pki/containers/example.pub"}]}}}') + +bk_root = tempfile.mkdtemp(prefix="apex-bk-") +os.makedirs(os.path.join(bk_root, "etc", "buildkit")) +bk_bad = os.path.join(bk_root, "etc", "buildkit", "buildkitd.toml") +with open(bk_bad, "w") as f: + f.write("[registry.example.test]\n") + f.write(" insecure = true\n") + f.write(" address = \"tcp://registry.example.test\"\n") + f.write("[worker.oci]\n") + f.write(" enabled = true\n") +os.chmod(bk_bad, 0o666) + +bk_good_root = tempfile.mkdtemp(prefix="apex-bkg-") +os.makedirs(os.path.join(bk_good_root, "etc", "buildkit")) +with open(os.path.join(bk_good_root, "etc", "buildkit", + "buildkitd.toml"), "w") as f: + f.write("[registry.example.test]\n") + f.write(" insecure = false\n") + f.write(" ca = [\"/etc/buildkit/ca.pem\"]\n") + f.write("[worker.oci]\n") + f.write(" rootless = true\n") + +sv_root = tempfile.mkdtemp(prefix="apex-sv-") +os.makedirs(os.path.join(sv_root, "etc", "supervisor")) +sv_bad = os.path.join(sv_root, "etc", "supervisor", "supervisord.conf") +with open(sv_bad, "w") as f: + f.write("[inet_http_server]\n") + f.write("port=http://0.0.0.0:9001\n") + f.write("\n[program:worker]\n") + f.write("command=/usr/local/bin/worker\n") + f.write("user=root\n") +os.chmod(sv_bad, 0o666) + +sv_good_root = tempfile.mkdtemp(prefix="apex-svg-") +os.makedirs(os.path.join(sv_good_root, "etc", "supervisor")) +with open(os.path.join(sv_good_root, "etc", "supervisor", + "supervisord.conf"), "w") as f: + f.write("[inet_http_server]\n") + f.write("port=http://127.0.0.1:9001\n") + f.write("username=admin\n") + f.write("password=secret\n") + f.write("\n[program:worker]\n") + f.write("command=/usr/local/bin/worker\n") + f.write("user=www-data\n") + +class _PortainerHandler(http.server.BaseHTTPRequestHandler): + def do_GET(self): + path = urllib.parse.urlsplit(self.path).path + if path == "/api/status": + body = b'{"Version":"2.19.4","Setup":true}' + self.send_response(200) + elif path == "/api/users/admin/check": + body = b"" + self.send_response(204) + else: + body = b'{"message":"Unauthorized"}' + self.send_response(403) + 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 _PortainerAuthHandler(http.server.BaseHTTPRequestHandler): + def do_GET(self): + body = b'{"message":"Unauthorized","details":"not logged in"}' + self.send_response(403) + 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 + +pt_srv = http.server.HTTPServer(("127.0.0.1", 0), _PortainerHandler) +pt_port = pt_srv.server_address[1] +threading.Thread(target=pt_srv.serve_forever, daemon=True).start() +pt_auth_srv = http.server.HTTPServer(("127.0.0.1", 0), + _PortainerAuthHandler) +pt_auth_port = pt_auth_srv.server_address[1] +threading.Thread(target=pt_auth_srv.serve_forever, daemon=True).start() + +cfg41_home = tempfile.mkdtemp(prefix="apex-cfg41-home-") +write_scope(cfg41_home, {"label": "cfg41", "expires": "2099-12-31T23:59:59Z", + "allow_private": True, "allow_ip": ["127.0.0.1"], + "allow_port": [str(pt_port), str(pt_auth_port)]}) +res, _ = run([ + call(1, "podman_audit", root=pdm_root), + call(2, "podman_audit", root=pdm_good_root), + call(3, "buildkitd_audit", root=bk_root), + call(4, "buildkitd_audit", root=bk_good_root), + call(5, "supervisor_audit", root=sv_root), + call(6, "supervisor_audit", root=sv_good_root), + call(7, "portainer_probe", target="127.0.0.1", port=pt_port, tls=False), + call(8, "portainer_probe", target="127.0.0.1", port=pt_auth_port, + tls=False), + call(9, "portainer_probe", target="198.51.100.9", port=9000), + call(10, "podman_audit", root="/nonexistent-pdm"), + call(11, "buildkitd_audit", root="/nonexistent-bk"), + call(12, "supervisor_audit", root="/nonexistent-sv"), +], home=cfg41_home) + +by_id = {r.get("id"): r for r in res} +pdm = json.loads(text(by_id[1])) +pdm_kinds = {f["id"] for f in pdm["findings"]} +check("podman_audit flags insecure and plain HTTP registry blocks", + {"world-writable", "insecure-registry", "plain-http-registry", + "short-name-disabled", "no-policy"} <= pdm_kinds and + pdm["insecure_registries"] == 1 and + pdm["plain_http_registries"] == 1, pdm) +pdm_good = json.loads(text(by_id[2])) +check("podman_audit accepts a signed policy and a TLS registry", + pdm_good["findings_count"] == 0 and + pdm_good["insecure_registries"] == 0 and + pdm_good["signature_verified"] is True, pdm_good) + +bk = json.loads(text(by_id[3])) +bk_kinds = {f["id"] for f in bk["findings"]} +check("buildkitd_audit flags insecure registry, tcp without CA, no rootless", + {"world-writable", "insecure-registry", "registry-without-ca", + "not-rootless"} <= bk_kinds and bk["insecure_registries"] == 1 and + bk["rootless"] is False, bk) +bk_good = json.loads(text(by_id[4])) +check("buildkitd_audit accepts a CA and a rootless worker", + bk_good["findings_count"] == 0 and + bk_good["plain_tcp_registry"] is False and + bk_good["rootless"] is True, bk_good) + +sv = json.loads(text(by_id[5])) +sv_kinds = {f["id"] for f in sv["findings"]} +check("supervisor_audit flags an exposed interface, no auth, root program", + {"world-writable", "http-exposed", "http-no-auth", + "root-program"} <= sv_kinds and sv["http_exposed"] is True and + sv["http_auth"] is False and sv["root_programs"] == 1, sv) +sv_good = json.loads(text(by_id[6])) +check("supervisor_audit accepts a loopback interface with a username", + sv_good["findings_count"] == 0 and + sv_good["http_exposed"] is False and + sv_good["http_auth"] is True and sv_good["root_programs"] == 0, + sv_good) + +pt = json.loads(text(by_id[7])) +check("portainer_probe finds the open admin check", + pt["setup_open"] is True and pt["evidence"] and + os.path.isfile(pt["evidence"]), pt) +pt_auth = json.loads(text(by_id[8])) +check("portainer_probe reports a protected instance", + pt_auth["setup_open"] is False and pt_auth["evidence"] is None and + all(e["status"] == 403 for e in pt_auth["endpoints"]), pt_auth) +check("portainer_probe refuses an out-of-scope target", err(by_id[9]), + text(by_id[9])) +check("podman_audit reports a missing configuration", err(by_id[10]), + text(by_id[10])) +check("buildkitd_audit reports a missing configuration", err(by_id[11]), + text(by_id[11])) +check("supervisor_audit reports a missing configuration", err(by_id[12]), + text(by_id[12])) +pt_srv.shutdown() +pt_srv.server_close() +pt_auth_srv.shutdown() +pt_auth_srv.server_close() +shutil.rmtree(pdm_root, ignore_errors=True) +shutil.rmtree(pdm_good_root, ignore_errors=True) +shutil.rmtree(bk_root, ignore_errors=True) +shutil.rmtree(bk_good_root, ignore_errors=True) +shutil.rmtree(sv_root, ignore_errors=True) +shutil.rmtree(sv_good_root, ignore_errors=True) +shutil.rmtree(cfg41_home, ignore_errors=True) + print("\n== esp32 tools ==")