diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cbb806..e262805 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,26 @@ 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.28.0 — 2026-11-21 — this change +## 1.29.0 — 2026-11-21 — this change + +- **Postfix** (`postfix_audit`): parses main.cf (root prefix) for mynetworks + covering 0.0.0.0/0 or ::/0, relay restrictions without + reject_unauth_destination (relay list, recipient list or neither), an + optional TLS level (unset, none, may) and world-writable configuration. +- **nginx** (`nginx_audit`): parses nginx.conf, conf.d/*.conf and + sites-enabled/*.conf: server_tokens not off, autoindex on, TLSv1/TLSv1.1 in + ssl_protocols, legacy ciphers (RC4/3DES/NULL/EXPORT/MD5) and world-writable + files. +- **Redis** (`redis_config_audit`): parses redis.conf: missing requirepass, + protected-mode no, bind on 0.0.0.0/*/::, enabled debug command, data + directory in /tmp or /dev/shm and world-writable config. The password value + is never echoed, only whether it is set. +- One shared `conf_value` reader accepts `key value`, `key = value`, skips + comments and strips semicolons and quotes. +- Tests with vulnerable and hardened trees for all three daemons. 786 checks. + Coverage 81.2%. + +## 1.28.0 — 2026-11-21 — 4d21af6 - **WinRM** (`winrm_probe`): one unauthenticated GET to /wsman (5985, TLS on 5986); 401/405 with a Negotiate/NTLM/Kerberos challenge identifies WinRM; diff --git a/README.md b/README.md index 4a41a76..ccaf47c 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. -**152 tools · 776 end-to-end checks · 340 fuzz cases · 81.0% line coverage · ~626 KiB** +**155 tools · 786 end-to-end checks · 340 fuzz cases · 81.2% line coverage · ~631 KiB** ``` $ ./apex-twin --check -{"version":"1.28.0","tools":152,...,"scope":{"loaded":true,...}} +{"version":"1.29.0","tools":155,...,"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 (152) +## Tools (155) ### Scope and bookkeeping @@ -349,6 +349,9 @@ command in the allowlist: a receiver is a scanner, a transmitter is an attack. | `samba_audit` | smb.conf shares: root path, guest access, writable, no valid users | | `snmpd_audit` | snmpd.conf communities: ro/rw/com2sec, public/private, default source | | `rsyslog_audit` | rsyslog remote targets, world-writable configs, missing auth logging | +| `postfix_audit` | main.cf: mynetworks all-addresses, relay without reject_unauth_destination, optional TLS, writable config | +| `nginx_audit` | nginx.conf/conf.d/sites-enabled: server_tokens, autoindex, TLSv1/1.1, legacy ciphers, writable files | +| `redis_config_audit` | redis.conf: no requirepass, protected-mode no, bind-all, debug command, /tmp data dir | ### ESP32 @@ -592,6 +595,7 @@ The full threat model, trust boundaries and a hardening checklist are in | NFS/Samba | config reads only; optional root prefix | | WinRM | one unauthenticated GET; no auth attempt | | snmpd/rsyslog | config reads only; optional root prefix | +| postfix/nginx/redis | config reads only; optional root prefix | | USB audit | 200 devices, sysfs reads only | --- @@ -603,7 +607,7 @@ Flags are fixed: `-std=c99 -Wall -Wextra -Wpedantic -Werror` plus ```sh make # apex-twin -make test # version check + gate audit + 776 end-to-end checks +make test # version check + gate audit + 786 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 @@ -761,7 +765,7 @@ examples/ scope.json ## Status -1.28.0. Built and run: 152 tools, 776 checks, 340 fuzz cases and 81.0% line +1.29.0. Built and run: 155 tools, 786 checks, 340 fuzz cases and 81.2% 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 @@ -806,7 +810,8 @@ checks; 1.24 added NTP monlist and DNS version disclosure checks plus a USB audit; 1.25 added OIDC discovery, security.txt and known-hosts checks; 1.26 added HTTP/2 probing, SSH host-key fingerprints and a logrotate audit; 1.27 added Kerberos discovery, NFS export and Samba audits; 1.28 added a WinRM probe and -snmpd/rsyslog config audits, again without changing an existing file format. +snmpd/rsyslog config audits; 1.29 added postfix, nginx and redis config audits, +again without changing an existing file format. Not yet wired into a fleet router, and there is no TLS interception. diff --git a/SECURITY.md b/SECURITY.md index 9450856..23d3a98 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.28.x** (`src/apex.h`, `APEX_VERSION`). Security fixes land on the current +is **1.29.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,10 @@ 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. +- **Daemon configs are audited read-only.** `postfix_audit`, `nginx_audit` + and `redis_config_audit` parse their configuration files and never signal, + reload or talk to the daemon. Redis passwords are reported as present or + absent, never echoed. - **Config audits are local reads.** `snmpd_audit` and `rsyslog_audit` parse configuration files; `winrm_probe` issues one unauthenticated GET to /wsman. - **Exports and Samba checks are local reads.** `nfs_exports_audit` and diff --git a/docs/apex-twin.1 b/docs/apex-twin.1 index 0c2e1b1..37a0de8 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.28.0" "User Commands" +.TH APEX-TWIN 1 "2026-11-21" "apex-twin 1.29.0" "User Commands" .SH NAME apex-twin \- scope\-gated offensive\-security MCP server .SH SYNOPSIS @@ -49,8 +49,12 @@ 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, kernel_hardening, sudoers_audit, auditd_audit, ssh_config_audit, -pam_audit) and ESP32 board tools (esp_info, esp_monitor, esp_read_flash, esp_flash, -esp_partition_table, esp_nvs_parse, firmware_scan). +pam_audit, ssh_known_hosts_audit, logrotate_audit, nfs_exports_audit, +samba_audit, snmpd_audit, rsyslog_audit, apparmor_audit, selinux_audit, +usb_audit, docker_audit, www_audit, audit_rules, postfix_audit, nginx_audit, +redis_config_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). .SH MODES .TP .B apex-twin diff --git a/src/apex.h b/src/apex.h index 46e9fc7..396c0ea 100644 --- a/src/apex.h +++ b/src/apex.h @@ -15,7 +15,7 @@ #include "mcp.h" -#define APEX_VERSION "1.28.0" +#define APEX_VERSION "1.29.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 d44245b..b612c9b 100644 --- a/src/tools_blue.c +++ b/src/tools_blue.c @@ -6729,6 +6729,428 @@ static void t_rsyslog_audit(mcp_conn *c, const mcp_json *args, void *user) mcp_buf_free(&findings); } +/* -------------------------------------------------------------- postfix_audit */ + +/* + * One directive reader for the config audits below. It accepts both + * "key value" (nginx, redis) and "key = value" (postfix), strips a trailing + * semicolon and surrounding quotes, and skips comments. + */ +static int conf_value(const char *text, const char *key, char *out, size_t n) +{ + const char *p = text; + size_t kl = strlen(key); + + out[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; + char *v; + size_t vl; + + if (len >= sizeof(line)) + len = sizeof(line) - 1; + memcpy(line, p, len); + line[len] = '\0'; + s = line; + while (*s == ' ' || *s == '\t') + s++; + if (*s != '#' && strncasecmp(s, key, kl) == 0 && + (s[kl] == ' ' || s[kl] == '\t' || s[kl] == '=')) { + v = s + kl; + while (*v == ' ' || *v == '\t' || *v == '=') + v++; + vl = strlen(v); + while (vl > 0 && (v[vl - 1] == ';' || 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; + } + if (vl >= n) + vl = n - 1; + memcpy(out, v, vl); + out[vl] = '\0'; + return 1; + } + p = eol != NULL ? eol + 1 : NULL; + } + return 0; +} + +static int conf_has_word(const char *list, const char *word) +{ + const char *p = list; + size_t wl = strlen(word); + + while (*p != '\0') { + const char *end; + size_t len; + + while (*p == ' ' || *p == '\t' || *p == ',') + p++; + end = p; + while (*end != '\0' && *end != ' ' && *end != '\t' && *end != ',') + end++; + len = (size_t)(end - p); + if (len == wl && strncasecmp(p, word, wl) == 0) + return 1; + p = end; + } + return 0; +} + +static void t_postfix_audit(mcp_conn *c, const mcp_json *args, void *user) +{ + const char *root = mcp_arg_string(args, "root"); + static char config[65536]; + char path[1024]; + char mynetworks[512]; + char relay[512]; + char recipient[512]; + char tls[128]; + char myhostname[256]; + mcp_buf b; + mcp_buf findings; + int first = 1; + int count = 0; + struct stat st; + + (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/postfix/main.cf", root); + if (read_text(path, config, sizeof(config)) <= 0) { + mcp_reply_error(c, "cannot read postfix main.cf"); + return; + } + mynetworks[0] = relay[0] = recipient[0] = '\0'; + tls[0] = myhostname[0] = '\0'; + conf_value(config, "mynetworks", mynetworks, sizeof(mynetworks)); + conf_value(config, "smtpd_relay_restrictions", relay, sizeof(relay)); + conf_value(config, "smtpd_recipient_restrictions", recipient, + sizeof(recipient)); + conf_value(config, "smtpd_tls_security_level", tls, sizeof(tls)); + conf_value(config, "myhostname", myhostname, sizeof(myhostname)); + mcp_buf_init(&findings); + if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) { + add_finding(&findings, &first, "world-writable", "high", + "postfix configuration is writable by anyone"); + count++; + } + if (strstr(mynetworks, "0.0.0.0/0") != NULL || + strstr(mynetworks, "::/0") != NULL) { + add_finding(&findings, &first, "mynetworks-all", "high", + "mynetworks covers every address"); + count++; + } + if (relay[0] != '\0') { + if (!conf_has_word(relay, "reject_unauth_destination")) { + add_finding(&findings, &first, "relay-open", "high", + "smtpd_relay_restrictions has no " + "reject_unauth_destination"); + count++; + } + } else if (recipient[0] != '\0') { + if (!conf_has_word(recipient, "reject_unauth_destination")) { + add_finding(&findings, &first, "relay-open", "high", + "smtpd_recipient_restrictions has no " + "reject_unauth_destination"); + count++; + } + } else { + add_finding(&findings, &first, "relay-unrestricted", "high", + "no relay restriction list is configured"); + count++; + } + if (tls[0] == '\0' || strcasecmp(tls, "none") == 0 || + strcasecmp(tls, "may") == 0) { + add_finding(&findings, &first, "tls-optional", "medium", + tls[0] == '\0' + ? "smtpd_tls_security_level is not set" + : "smtpd_tls_security_level does not require TLS"); + count++; + } + mcp_buf_init(&b); + mcp_buf_puts(&b, "{\"root\":"); + apex_json_str(&b, root); + mcp_buf_puts(&b, ",\"path\":"); + apex_json_str(&b, path); + mcp_buf_puts(&b, ",\"directives\":{\"myhostname\":"); + apex_json_str(&b, myhostname); + mcp_buf_puts(&b, ",\"mynetworks\":"); + apex_json_str(&b, mynetworks); + mcp_buf_puts(&b, ",\"smtpd_relay_restrictions\":"); + apex_json_str(&b, relay); + mcp_buf_puts(&b, ",\"smtpd_tls_security_level\":"); + apex_json_str(&b, tls); + mcp_buf_puts(&b, "},\"findings_count\":"); + mcp_buf_printf(&b, "%d,\"findings\":[", 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); +} + +/* ---------------------------------------------------------------- nginx_audit */ + +static void nginx_scan_file(mcp_buf *findings, int *first, int *count, + const char *path, int *tokens_off, + int *tokens_seen, int *autoindex, int *weak_proto, + int *weak_cipher) +{ + static char text[65536]; + char value[512]; + struct stat st; + + if (read_text(path, text, sizeof(text)) < 0) + return; + 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 (conf_value(text, "server_tokens", value, sizeof(value))) { + *tokens_seen = 1; + if (strcasecmp(value, "off") == 0) + *tokens_off = 1; + } + if (conf_value(text, "autoindex", value, sizeof(value)) && + strcasecmp(value, "on") == 0) { + if (!*autoindex) + add_finding(findings, first, "autoindex", "medium", + "%s enables directory listing", path); + *autoindex = 1; + (*count)++; + } + if (conf_value(text, "ssl_protocols", value, sizeof(value))) { + if (conf_has_word(value, "TLSv1") || + conf_has_word(value, "TLSv1.1")) { + if (!*weak_proto) + add_finding(findings, first, "weak-protocol", + "high", + "%s allows TLSv1/TLSv1.1", path); + *weak_proto = 1; + (*count)++; + } + } + if (conf_value(text, "ssl_ciphers", value, sizeof(value))) { + if (strstr(value, "RC4") != NULL || + strstr(value, "3DES") != NULL || + strstr(value, "NULL") != NULL || + strstr(value, "EXPORT") != NULL || + strstr(value, "MD5") != NULL) { + if (!*weak_cipher) + add_finding(findings, first, "weak-cipher", + "high", + "%s allows legacy TLS ciphers", path); + *weak_cipher = 1; + (*count)++; + } + } +} + +static void t_nginx_audit(mcp_conn *c, const mcp_json *args, void *user) +{ + 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 = 0; + int tokens_seen = 0; + int tokens_off = 0; + int autoindex = 0; + int weak_proto = 0; + int weak_cipher = 0; + static const char *dirs[] = { "/etc/nginx/conf.d", + "/etc/nginx/sites-enabled", NULL }; + int i; + + (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/nginx/nginx.conf", root); + { + struct stat st; + + if (stat(path, &st) != 0) { + mcp_reply_error(c, "cannot read nginx.conf"); + return; + } + } + mcp_buf_init(&findings); + nginx_scan_file(&findings, &first, &count, path, &tokens_off, + &tokens_seen, &autoindex, &weak_proto, &weak_cipher); + files++; + for (i = 0; dirs[i] != NULL; i++) { + DIR *d; + struct dirent *ent; + + snprintf(path, sizeof(path), "%.400s%s", root, dirs[i]); + d = opendir(path); + if (d == NULL) + continue; + while ((ent = readdir(d)) != NULL) { + char full[1024]; + struct stat st; + size_t nl = strlen(ent->d_name); + + if (ent->d_name[0] == '.') + continue; + if (nl < 6 || strcmp(ent->d_name + nl - 5, ".conf") != 0) + continue; + snprintf(full, sizeof(full), "%.800s/%.160s", path, + ent->d_name); + if (stat(full, &st) != 0 || !S_ISREG(st.st_mode)) + continue; + nginx_scan_file(&findings, &first, &count, full, + &tokens_off, &tokens_seen, &autoindex, + &weak_proto, &weak_cipher); + files++; + } + closedir(d); + } + if (!tokens_off) { + add_finding(&findings, &first, "server-tokens", "medium", + tokens_seen + ? "server_tokens is enabled" + : "server_tokens is not set to off"); + count++; + } + mcp_buf_init(&b); + mcp_buf_puts(&b, "{\"root\":"); + apex_json_str(&b, root); + mcp_buf_printf(&b, ",\"files\":%d,\"server_tokens_off\":%s," + "\"autoindex\":%s,\"weak_protocols\":%s," + "\"weak_ciphers\":%s,\"findings_count\":%d," + "\"findings\":[", + files, tokens_off ? "true" : "false", + autoindex ? "true" : "false", + weak_proto ? "true" : "false", + weak_cipher ? "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); +} + +/* --------------------------------------------------------- redis_config_audit */ + +static void t_redis_config_audit(mcp_conn *c, const mcp_json *args, void *user) +{ + const char *root = mcp_arg_string(args, "root"); + static char config[65536]; + char path[1024]; + char value[512]; + char bind_addr[512]; + char dir[512]; + char debug[64]; + char protected_mode[64]; + int pass_set = 0; + mcp_buf b; + mcp_buf findings; + int first = 1; + int count = 0; + struct stat st; + + (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/redis/redis.conf", root); + if (read_text(path, config, sizeof(config)) <= 0) { + snprintf(path, sizeof(path), "%s/etc/redis.conf", root); + if (read_text(path, config, sizeof(config)) <= 0) { + mcp_reply_error(c, "cannot read redis.conf"); + return; + } + } + bind_addr[0] = dir[0] = '\0'; + debug[0] = protected_mode[0] = '\0'; + if (conf_value(config, "requirepass", value, sizeof(value)) && + value[0] != '\0') + pass_set = 1; + conf_value(config, "bind", bind_addr, sizeof(bind_addr)); + conf_value(config, "dir", dir, sizeof(dir)); + conf_value(config, "enable-debug-command", debug, sizeof(debug)); + if (debug[0] == '\0') + conf_value(config, "enable-command", debug, sizeof(debug)); + conf_value(config, "protected-mode", protected_mode, + sizeof(protected_mode)); + mcp_buf_init(&findings); + if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) { + add_finding(&findings, &first, "world-writable", "high", + "redis configuration is writable by anyone"); + count++; + } + if (!pass_set) { + add_finding(&findings, &first, "no-requirepass", "high", + "no requirepass is configured"); + count++; + } + if (strcasecmp(protected_mode, "no") == 0) { + add_finding(&findings, &first, "protected-mode-off", "high", + "protected-mode is disabled"); + count++; + } + if (conf_has_word(bind_addr, "0.0.0.0") || + conf_has_word(bind_addr, "*") || + conf_has_word(bind_addr, "::")) { + add_finding(&findings, &first, "bind-all", "high", + "redis binds to every interface"); + count++; + } + if (strcasecmp(debug, "yes") == 0) { + add_finding(&findings, &first, "debug-command", "medium", + "the debug command is enabled"); + count++; + } + if (strncmp(dir, "/tmp", 4) == 0 || strncmp(dir, "/dev/shm", 8) == 0) { + add_finding(&findings, &first, "temp-dir", "medium", + "redis writes its data into a temporary directory"); + count++; + } + mcp_buf_init(&b); + mcp_buf_puts(&b, "{\"root\":"); + apex_json_str(&b, root); + mcp_buf_puts(&b, ",\"path\":"); + apex_json_str(&b, path); + mcp_buf_printf(&b, ",\"requirepass_set\":%s,\"protected_mode\":", + pass_set ? "true" : "false"); + apex_json_str(&b, protected_mode); + mcp_buf_puts(&b, ",\"bind\":"); + apex_json_str(&b, bind_addr); + mcp_buf_puts(&b, ",\"dir\":"); + apex_json_str(&b, dir); + mcp_buf_printf(&b, ",\"findings_count\":%d,\"findings\":[", 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, " @@ -7106,6 +7528,28 @@ const mcp_tool TOOLS_BLUE[] = { "{\"type\":\"object\",\"properties\":{" "\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}", t_rsyslog_audit }, + { "postfix_audit", + "Parse /etc/postfix/main.cf (root prefix): mynetworks covering every " + "address, relay restrictions without reject_unauth_destination, optional " + "TLS and world-writable configuration. Read-only.", + "{\"type\":\"object\",\"properties\":{" + "\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}", + t_postfix_audit }, + { "nginx_audit", + "Parse /etc/nginx/nginx.conf, conf.d and sites-enabled (root prefix): " + "server_tokens, autoindex, TLSv1/1.1 in ssl_protocols, legacy ciphers and " + "world-writable files. Read-only.", + "{\"type\":\"object\",\"properties\":{" + "\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}", + t_nginx_audit }, + { "redis_config_audit", + "Parse /etc/redis/redis.conf (root prefix): missing requirepass, " + "protected-mode no, bind on every address, enabled debug command and a " + "data directory in /tmp or /dev/shm, plus world-writable configs. " + "Read-only.", + "{\"type\":\"object\",\"properties\":{" + "\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}", + t_redis_config_audit }, }; const size_t TOOLS_BLUE_N = sizeof(TOOLS_BLUE) / sizeof(TOOLS_BLUE[0]); diff --git a/tests/gate_audit.py b/tests/gate_audit.py index 0e006ef..cd3539f 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 = 152 +EXPECTED_TOOLS = 155 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 d93f3b7..b98c022 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("152 tools registered", len(tools) == 152, len(tools)) +check("155 tools registered", len(tools) == 155, 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"]) == 152) + check("http tools/list", len(listing["result"]["tools"]) == 155) 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 @@ -6931,6 +6931,123 @@ shutil.rmtree(rsys_root, ignore_errors=True) shutil.rmtree(rsys_sparse, ignore_errors=True) shutil.rmtree(logs_home, ignore_errors=True) +print("\n== postfix, nginx and redis configuration ==\n") + +pf_root = tempfile.mkdtemp(prefix="apex-pf-") +os.makedirs(os.path.join(pf_root, "etc", "postfix")) +pf_bad = os.path.join(pf_root, "etc", "postfix", "main.cf") +with open(pf_bad, "w") as f: + f.write("myhostname = mail.example.test\n") + f.write("mynetworks = 127.0.0.0/8 0.0.0.0/0\n") + f.write("smtpd_relay_restrictions = permit_mynetworks\n") + f.write("smtpd_tls_security_level = may\n") +pf_good_root = tempfile.mkdtemp(prefix="apex-pfg-") +os.makedirs(os.path.join(pf_good_root, "etc", "postfix")) +with open(os.path.join(pf_good_root, "etc", "postfix", "main.cf"), + "w") as f: + f.write("myhostname = mail.example.test\n") + f.write("mynetworks = 127.0.0.0/8\n") + f.write("smtpd_relay_restrictions = permit_mynetworks, " + "reject_unauth_destination\n") + f.write("smtpd_tls_security_level = encrypt\n") + +ng_root = tempfile.mkdtemp(prefix="apex-ng-") +os.makedirs(os.path.join(ng_root, "etc", "nginx", "conf.d")) +os.makedirs(os.path.join(ng_root, "etc", "nginx", "sites-enabled")) +with open(os.path.join(ng_root, "etc", "nginx", "nginx.conf"), "w") as f: + f.write("server_tokens on;\n") + f.write("ssl_protocols TLSv1 TLSv1.2;\n") +ng_site = os.path.join(ng_root, "etc", "nginx", "conf.d", "site.conf") +with open(ng_site, "w") as f: + f.write("autoindex on;\n") + f.write('ssl_ciphers "RC4-SHA:3DES-CBC3-SHA";\n') +os.chmod(ng_site, 0o666) +ng_good_root = tempfile.mkdtemp(prefix="apex-ngg-") +os.makedirs(os.path.join(ng_good_root, "etc", "nginx")) +with open(os.path.join(ng_good_root, "etc", "nginx", "nginx.conf"), + "w") as f: + f.write("server_tokens off;\n") + f.write("ssl_protocols TLSv1.2 TLSv1.3;\n") + f.write('ssl_ciphers "ECDHE+AESGCM:ECDHE+CHACHA20";\n') +os.makedirs(os.path.join(ng_good_root, "etc", "nginx", "conf.d")) + +rd_root = tempfile.mkdtemp(prefix="apex-rd-") +os.makedirs(os.path.join(rd_root, "etc", "redis")) +rd_bad = os.path.join(rd_root, "etc", "redis", "redis.conf") +with open(rd_bad, "w") as f: + f.write("bind 0.0.0.0\n") + f.write("protected-mode no\n") + f.write("enable-debug-command yes\n") + f.write("dir /tmp\n") +os.chmod(rd_bad, 0o666) +rd_good_root = tempfile.mkdtemp(prefix="apex-rdg-") +os.makedirs(os.path.join(rd_good_root, "etc", "redis")) +with open(os.path.join(rd_good_root, "etc", "redis", "redis.conf"), + "w") as f: + f.write("bind 127.0.0.1 ::1\n") + f.write("protected-mode yes\n") + f.write("requirepass s3cret-value\n") + f.write("dir /var/lib/redis\n") + +cfg_home = tempfile.mkdtemp(prefix="apex-cfg-home-") +write_scope(cfg_home, {"label": "cfg", "expires": "2099-12-31T23:59:59Z"}) +res, _ = run([ + call(1, "postfix_audit", root=pf_root), + call(2, "postfix_audit", root=pf_good_root), + call(3, "nginx_audit", root=ng_root), + call(4, "nginx_audit", root=ng_good_root), + call(5, "redis_config_audit", root=rd_root), + call(6, "redis_config_audit", root=rd_good_root), + call(7, "postfix_audit", root="/nonexistent-pf"), + call(8, "nginx_audit", root="/nonexistent-ng"), + call(9, "redis_config_audit", root="/nonexistent-rd"), +], home=cfg_home) +by_id = {r.get("id"): r for r in res} +pf = json.loads(text(by_id[1])) +pf_kinds = {f["id"] for f in pf["findings"]} +check("postfix_audit flags the open relay and weak TLS", + {"mynetworks-all", "relay-open", "tls-optional"} <= pf_kinds and + pf["directives"]["mynetworks"] == "127.0.0.0/8 0.0.0.0/0", pf) +pf_good = json.loads(text(by_id[2])) +check("postfix_audit accepts a hardened config", + pf_good["findings_count"] == 0 and + pf_good["directives"]["smtpd_tls_security_level"] == "encrypt", + pf_good) +ng = json.loads(text(by_id[3])) +ng_kinds = {f["id"] for f in ng["findings"]} +check("nginx_audit flags tokens, autoindex, protocols and ciphers", + {"server-tokens", "autoindex", "weak-protocol", "weak-cipher"} <= + ng_kinds, ng) +check("nginx_audit flags a world-writable config", + "world-writable" in ng_kinds and ng["files"] == 2, ng) +ng_good = json.loads(text(by_id[4])) +check("nginx_audit accepts a hardened config", + ng_good["findings_count"] == 0 and + ng_good["server_tokens_off"] is True, ng_good) +rd = json.loads(text(by_id[5])) +rd_kinds = {f["id"] for f in rd["findings"]} +check("redis_config_audit flags the exposed instance", + {"no-requirepass", "protected-mode-off", "bind-all", "debug-command", + "temp-dir", "world-writable"} <= rd_kinds, rd) +rd_good = json.loads(text(by_id[6])) +check("redis_config_audit accepts a hardened config", + rd_good["findings_count"] == 0 and + rd_good["requirepass_set"] is True and + rd_good["protected_mode"] == "yes", rd_good) +check("postfix_audit reports a missing config", err(by_id[7]), + text(by_id[7])) +check("nginx_audit reports a missing config", err(by_id[8]), + text(by_id[8])) +check("redis_config_audit reports a missing config", err(by_id[9]), + text(by_id[9])) +shutil.rmtree(pf_root, ignore_errors=True) +shutil.rmtree(pf_good_root, ignore_errors=True) +shutil.rmtree(ng_root, ignore_errors=True) +shutil.rmtree(ng_good_root, ignore_errors=True) +shutil.rmtree(rd_root, ignore_errors=True) +shutil.rmtree(rd_good_root, ignore_errors=True) +shutil.rmtree(cfg_home, ignore_errors=True) + print("\n== esp32 tools ==") esp_home = tempfile.mkdtemp(prefix="apex-esp-") write_scope(esp_home, {"label": "esp", "expires": "2099-12-31T23:59:59Z"})