diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f2afef..34d6a5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,20 @@ # Changelog +## 1.95.0 - 2026-11-24 - this change + +- **SpamAssassin** (`spamassassin_audit`): `local.cf`/`local.conf` with + `required_score 0` (every mail passes), `report_safe 0` (the original + message is forwarded), `skip_rbl_checks 1`, a rule scored 0, and a + world-writable file. +- **OpenDMARC** (`opendmarc_audit`): `opendmarc.conf` with `RejectFailures` + off or absent, `FailureReports off`, `IgnoreAuthenticatedClients yes` + (every logged-in sender skips DMARC) and a TCP `Socket`. +- **amavis** (`amavis_audit`): `amavisd.conf` plus `conf.d` fragments with + `$final_virus_destiny`/`$final_spam_destiny` set to `D_PASS`, + `$bypass_spam_checks`/`$bypass_virus_checks` on, and no `$virus_admin`. +- **Bitbucket** (`bitbucket_probe`): `/rest/api/1.0/projects` on 7990 + without a token; the project list is saved as evidence. + ## 1.94.0 - 2026-11-24 - this change - **opendkim** (`opendkim_audit`): `opendkim.conf` and `/etc/opendkim` under diff --git a/README.md b/README.md index 4bb77e1..08735b5 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. -**406 tools · 1501 end-to-end checks · 240 fuzz cases · 85.1% line coverage · ~1041 KiB** +**410 tools · 1512 end-to-end checks · 240 fuzz cases · 85.1% line coverage · ~1041 KiB** ``` $ ./apex-twin --check -{"version":"1.94.0","tools":406,...,"scope":{"loaded":true,...}} +{"version":"1.95.0","tools":410,...,"scope":{"loaded":true,...}} ``` APEX TWIN was inspired by [ARTEX](https://github.com/Autumn-27/artex) (scope @@ -246,6 +246,7 @@ uploads and secret-file reads. It does not execute anything. | `jaeger_probe` | `/api/services` on 16686 without auth; service list as evidence | | `solr_probe` | `/solr/admin/cores` on 8983 without auth; core list as evidence | | `confluence_probe` | `/rest/api/space` on 8090 without a token; space list as evidence | +| `bitbucket_probe` | `/rest/api/1.0/projects` on 7990 without a token; project list as evidence | | `jupyter_probe` | `/api/kernels` on 8888 without a token; sessions as evidence | | `jira_probe` | `/rest/api/2/project` on 8080 without auth; project list as evidence | | `ollama_probe` | `/api/tags` on 11434 without auth; pulled model list as evidence | @@ -564,6 +565,9 @@ does. | `rc_local_audit` | rc.local: remote pipe, /tmp reference, missing exit | | `opendkim_audit` | opendkim.conf: inet socket, RequireSafeKeys off, Mode without `s` | | `rspamd_audit` | rspamd UCL: wildcard bind_socket, clear-text password, secure_ip 0.0.0.0/0 | +| `spamassassin_audit` | local.cf: `required_score 0`, `report_safe 0`, RBL skipped, dead rule | +| `opendmarc_audit` | opendmarc.conf: `RejectFailures` off, reports off, auth clients skipped | +| `amavis_audit` | amavisd.conf: virus/spam `D_PASS`, bypass flags, no virus admin | | `crypttab_audit` | crypttab: readable key file, `none` key, discard | | `useradd_audit` | useradd defaults: empty INACTIVE, bad SHELL, writable skel | | `aide_audit` | AIDE: `!` on /etc or /bin, md5-only rules, db in /tmp | @@ -1104,7 +1108,8 @@ src/tools_blue.c hardening_audit, log_audit, ioc_scan, web_log_audit, anacrontab_audit, motd_audit, syslog_ng_audit, ntpd_audit, inetd_audit, ld_so_conf_audit, rc_local_audit, opendkim_audit, - rspamd_audit, nscd_audit, + rspamd_audit, spamassassin_audit, opendmarc_audit, + amavis_audit, nscd_audit, crypttab_audit, useradd_audit, aide_audit, faillock_audit, networkd_audit, namespace_audit, ssh_client_audit, ufw_audit, monit_audit, @@ -1160,7 +1165,7 @@ examples/ scope.json ## Status -1.94.0. Built and run: 406 tools, 1501 checks, 240 fuzz cases and 85.1% line +1.95.0. Built and run: 410 tools, 1512 checks, 240 fuzz cases and 85.1% 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 2f58c8e..191647c 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.94.x** (`src/apex.h`, `APEX_VERSION`). Security fixes land on the current +is **1.95.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 d1a11f8..4c0a828 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-24" "apex-twin 1.94.0" "User Commands" +.TH APEX-TWIN 1 "2026-11-24" "apex-twin 1.95.0" "User Commands" .SH NAME apex-twin \- scope\-gated offensive\-security MCP server .SH SYNOPSIS @@ -45,7 +45,7 @@ graphql_probe, docker_api_probe, elasticsearch_probe, imap_probe, pop3_probe, 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, 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, argocd_probe, kafka_connect_probe, sonarqube_probe, keycloak_probe, rundeck_probe, zabbix_probe, datasette_probe, airflow_probe, gitlab_probe, jupyter_probe, jira_probe, ollama_probe, cadvisor_probe, teamcity_probe, superset_probe, vault_probe, nomad_probe, coredns_probe, openbmc_probe, schema_registry_probe, loki_probe, tempo_probe, jaeger_probe, solr_probe, confluence_probe, +websocket_origin_check, dns_fcrdns_check, dns_soa_mname_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, argocd_probe, kafka_connect_probe, sonarqube_probe, keycloak_probe, rundeck_probe, zabbix_probe, datasette_probe, airflow_probe, gitlab_probe, jupyter_probe, jira_probe, ollama_probe, cadvisor_probe, teamcity_probe, superset_probe, vault_probe, nomad_probe, coredns_probe, openbmc_probe, schema_registry_probe, loki_probe, tempo_probe, jaeger_probe, solr_probe, confluence_probe, bitbucket_probe, nuclei_scan, nikto_scan, ffuf_scan, gobuster_scan, sqlmap_scan), defensive audits (hardening_audit, log_audit, ioc_scan, web_log_audit, detect_rules, @@ -88,7 +88,7 @@ fstab_audit, modprobe_audit, grub_audit, limits_audit, access_conf_audit, pwquality_audit, pam_env_audit, hosts_audit, dnf_repo_audit, anacrontab_audit, motd_audit, syslog_ng_audit, ntpd_audit, inetd_audit, -ld_so_conf_audit, rc_local_audit, opendkim_audit, rspamd_audit, +ld_so_conf_audit, rc_local_audit, opendkim_audit, spamassassin_audit, opendmarc_audit, amavis_audit, rspamd_audit, crypttab_audit, useradd_audit, aide_audit, nscd_audit, faillock_audit, networkd_audit, namespace_audit, ssh_client_audit, ufw_audit, monit_audit, clamav_audit, diff --git a/src/apex.h b/src/apex.h index 2d43936..45f601c 100644 --- a/src/apex.h +++ b/src/apex.h @@ -15,7 +15,7 @@ #include "mcp.h" -#define APEX_VERSION "1.94.0" +#define APEX_VERSION "1.95.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 0488d4d..ef7d7bf 100644 --- a/src/tools_blue.c +++ b/src/tools_blue.c @@ -31641,6 +31641,521 @@ static void t_rspamd_audit(mcp_conn *c, const mcp_json *args, void *user) mcp_buf_free(&findings); } +/* ---------------------------------------------------------- mail filter audit */ + +/* Shared state for opendmarc_audit and amavis_audit; both watch a handful of + * switches that decide whether a failing message is still delivered. */ +struct opendmarc_state { + int files; + int has_reject; + int no_reject; + int reports_off; + int ignore_auth; + int has_socket; + int inet_socket; +}; + +struct amavis_state { + int files; + int virus_pass; + int spam_pass; + int bypass_spam; + int bypass_virus; + int no_virus_admin; +}; + +/* ---------------------------------------------------------- spamassassin_audit */ + +/* SpamAssassin config is "key value" in local.cf (SpamAssassin) or + * "key = value" (some wrappers); both spellings appear, so a helper strips an + * optional '=' after the key. Three settings disable the filter wholesale: + * a required_score at or below zero, "skip_rbl_checks 1" and a rule scored 0 + * (the rule fires but never reaches the threshold on its own). */ +static int sa_key_value(const char *line, const char *key, char *out, size_t n) +{ + size_t kl = strlen(key); + + if (strncasecmp(line, key, kl) != 0) + return 0; + if (line[kl] != ' ' && line[kl] != '\t' && line[kl] != '=') + return 0; + line += kl; + while (*line == ' ' || *line == '\t') + line++; + if (*line == '=') + line++; + while (*line == ' ' || *line == '\t') + line++; + if (n == 0) + return 0; + n--; + while (*line != '\0' && *line != '\n' && *line != '\r' && n > 0) { + *out++ = *line++; + n--; + } + *out = '\0'; + return 1; +} + +static void spamassassin_scan_file(mcp_buf *findings, int *first, int *count, + const char *path, int *files, + int *score_off, int *report_plain, + int *rbl_skipped, int *dead_rules) +{ + static char text[65536]; + struct stat sb; + const char *p; + + if (read_text(path, text, sizeof(text)) < 0) + return; + (*files)++; + if (stat(path, &sb) == 0 && (sb.st_mode & 0002) != 0) { + add_finding(findings, first, "world-writable", "high", + "%s is writable by anyone", path); + (*count)++; + } + p = text; + 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 value[256]; + char *s; + + if (len >= sizeof(line)) + len = sizeof(line) - 1; + memcpy(line, p, len); + line[len] = '\0'; + p = eol != NULL ? eol + 1 : NULL; + s = line; + while (*s == ' ' || *s == '\t') + s++; + if (*s == '#' || *s == '\0') + continue; + if (sa_key_value(s, "required_score", value, sizeof(value))) { + if (strtod(value, NULL) <= 0.0) { + *score_off = 1; + add_finding(findings, first, "score-off", "high", + "%s sets required_score %s: every mail " + "passes the filter", path, value); + (*count)++; + } + } else if (sa_key_value(s, "report_safe", value, + sizeof(value))) { + if (strcmp(value, "0") == 0) { + *report_plain = 1; + add_finding(findings, first, "report-unsafe", + "medium", + "%s sets report_safe 0: spam is forwarded " + "as the original message, so links and " + "attachments reach the user", path); + (*count)++; + } + } else if (sa_key_value(s, "skip_rbl_checks", value, + sizeof(value))) { + if (strcmp(value, "1") == 0) { + *rbl_skipped = 1; + add_finding(findings, first, "rbl-skipped", "medium", + "%s sets skip_rbl_checks 1: DNS blocklists " + "are never queried", path); + (*count)++; + } + } else if (strncasecmp(s, "score ", 6) == 0) { + char rule[128]; + char num[64]; + char *sp = s + 6; + + while (*sp == ' ' || *sp == '\t') + sp++; + snprintf(rule, sizeof(rule), "%.100s", sp); + { + char *cut = strpbrk(rule, " \t"); + + if (cut != NULL) + *cut = '\0'; + } + sp += strlen(rule); + while (*sp == ' ' || *sp == '\t') + sp++; + snprintf(num, sizeof(num), "%.40s", sp); + if (rule[0] != '\0' && strtod(num, NULL) == 0.0) { + (*dead_rules)++; + add_finding(findings, first, "dead-rule", "low", + "%s scores %s at 0: the rule never adds " + "weight", path, rule); + (*count)++; + } + } + } +} + +static void t_spamassassin_audit(mcp_conn *c, const mcp_json *args, + void *user) +{ + const char *root = mcp_arg_string(args, "root"); + char base[512]; + char path[1024]; + size_t bl; + mcp_buf b; + mcp_buf findings; + int count = 0; + int first = 1; + int files = 0; + int score_off = 0; + int report_plain = 0; + int rbl_skipped = 0; + int dead_rules = 0; + + (void)user; + if (root == NULL) + root = ""; + if (strlen(root) >= sizeof(base) - 2) { + mcp_reply_error(c, "root path too long"); + return; + } + snprintf(base, sizeof(base), "%s", root); + bl = strlen(base); + while (bl > 0 && base[bl - 1] == '/') + base[--bl] = '\0'; + mcp_buf_init(&findings); + + snprintf(path, sizeof(path), "%s/etc/spamassassin/local.cf", base); + spamassassin_scan_file(&findings, &first, &count, path, &files, + &score_off, &report_plain, &rbl_skipped, + &dead_rules); + snprintf(path, sizeof(path), "%s/etc/spamassassin/local.conf", base); + spamassassin_scan_file(&findings, &first, &count, path, &files, + &score_off, &report_plain, &rbl_skipped, + &dead_rules); + snprintf(path, sizeof(path), "%s/etc/mail/spamassassin/local.cf", base); + spamassassin_scan_file(&findings, &first, &count, path, &files, + &score_off, &report_plain, &rbl_skipped, + &dead_rules); + + if (files == 0) { + add_finding(&findings, &first, "no-config", "info", + "no SpamAssassin configuration found"); + count = 1; + } else if (count == 0) { + add_finding(&findings, &first, "no-findings", "info", + "the SpamAssassin configuration carries no weak " + "setting"); + count = 1; + } + mcp_buf_init(&b); + mcp_buf_puts(&b, "{\"root\":"); + apex_json_str(&b, base); + mcp_buf_printf(&b, ",\"files\":%d,\"score_off\":%s," + "\"report_plain\":%s,\"rbl_skipped\":%s," + "\"dead_rules\":%d,\"findings_count\":%d,\"findings\":[", + files, score_off ? "true" : "false", + report_plain ? "true" : "false", + rbl_skipped ? "true" : "false", dead_rules, 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); +} + +/* ------------------------------------------------------------ opendmarc_audit */ + +/* opendmarc.conf is "Key value" with '#' comments. The daemon only rejects + * mail when RejectFailures is on, so that line decides whether DMARC does + * anything. FailureReports off means the domain owner never learns about + * forgery, IgnoreAuthenticatedClients yes skips every logged-in sender, and + * an inet: Socket exposes the milter to the network. */ +static void opendmarc_scan_file(mcp_buf *findings, int *first, int *count, + const char *path, struct opendmarc_state *st) +{ + static char text[65536]; + char value[512]; + struct stat sb; + + if (read_text(path, text, sizeof(text)) < 0) + return; + st->files++; + if (stat(path, &sb) == 0 && (sb.st_mode & 0002) != 0) { + add_finding(findings, first, "world-writable", "high", + "%s is writable by anyone", path); + (*count)++; + } + if (conf_value(text, "RejectFailures", value, sizeof(value))) { + st->has_reject = 1; + if (strcasecmp(value, "false") == 0 || strcmp(value, "0") == 0) { + st->no_reject = 1; + add_finding(findings, first, "no-reject", "high", + "%s sets RejectFailures %s: a message that " + "fails DMARC is delivered anyway", path, value); + (*count)++; + } + } + if (conf_value(text, "FailureReports", value, sizeof(value))) { + if (strcasecmp(value, "off") == 0) { + st->reports_off = 1; + add_finding(findings, first, "reports-off", "medium", + "%s sets FailureReports off: the domain owner " + "never learns about forged mail", path); + (*count)++; + } + } + if (conf_value(text, "IgnoreAuthenticatedClients", value, + sizeof(value))) { + if (strcasecmp(value, "true") == 0 || + strcasecmp(value, "yes") == 0 || strcmp(value, "1") == 0) { + st->ignore_auth = 1; + add_finding(findings, first, "ignore-authenticated", + "medium", + "%s sets IgnoreAuthenticatedClients yes: every " + "logged-in sender skips the DMARC check", path); + (*count)++; + } + } + if (conf_value(text, "Socket", value, sizeof(value))) { + st->has_socket = 1; + if (strncasecmp(value, "inet:", 5) == 0 || + strncasecmp(value, "inet6:", 6) == 0) { + st->inet_socket = 1; + add_finding(findings, first, "inet-socket", "medium", + "%s binds the milter to a network socket (%s), " + "so any host that reaches the port can submit " + "mail", path, value); + (*count)++; + } + } +} + +static void t_opendmarc_audit(mcp_conn *c, const mcp_json *args, void *user) +{ + const char *root = mcp_arg_string(args, "root"); + char base[512]; + char path[1024]; + size_t bl; + mcp_buf b; + mcp_buf findings; + struct opendmarc_state st; + int count = 0; + int first = 1; + + (void)user; + if (root == NULL) + root = ""; + if (strlen(root) >= sizeof(base) - 2) { + mcp_reply_error(c, "root path too long"); + return; + } + snprintf(base, sizeof(base), "%s", root); + bl = strlen(base); + while (bl > 0 && base[bl - 1] == '/') + base[--bl] = '\0'; + memset(&st, 0, sizeof(st)); + mcp_buf_init(&findings); + + snprintf(path, sizeof(path), "%s/etc/opendmarc.conf", base); + opendmarc_scan_file(&findings, &first, &count, path, &st); + snprintf(path, sizeof(path), "%s/etc/opendmarc/opendmarc.conf", base); + opendmarc_scan_file(&findings, &first, &count, path, &st); + + if (st.files == 0) { + add_finding(&findings, &first, "no-config", "info", + "no opendmarc configuration found"); + count = 1; + } else { + if (!st.has_reject) { + add_finding(&findings, &first, "reject-default", "info", + "no RejectFailures line: the daemon default " + "(off) applies, failing mail is delivered"); + count++; + } + if (!st.has_socket) { + add_finding(&findings, &first, "no-socket", "info", + "no Socket line: the milter default socket " + "is used"); + count++; + } + } + if (count == 0) { + add_finding(&findings, &first, "no-findings", "info", + "the DMARC configuration carries no weak setting"); + count = 1; + } + mcp_buf_init(&b); + mcp_buf_puts(&b, "{\"root\":"); + apex_json_str(&b, base); + mcp_buf_printf(&b, ",\"files\":%d,\"no_reject\":%s," + "\"reports_off\":%s,\"ignore_auth\":%s," + "\"inet_socket\":%s,\"findings_count\":%d,\"findings\":[", + st.files, st.no_reject ? "true" : "false", + st.reports_off ? "true" : "false", + st.ignore_auth ? "true" : "false", + st.inet_socket ? "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); +} + +/* --------------------------------------------------------------- amavis_audit */ + +/* amavis keeps its policy in Perl fragments under /etc/amavis/conf.d, one + * "$name = value;" per line. The destiny variables decide what happens to a + * message: D_PASS delivers it, so a virus or spam destined to D_PASS reaches + * the user. $sa_kill_level <= $sa_tag_level disables spam blocking, and a + * bypass switch turns a check off entirely. */ +static void amavis_scan_file(mcp_buf *findings, int *first, int *count, + const char *path, struct amavis_state *st) +{ + static char text[65536]; + struct stat sb; + const char *p; + + if (read_text(path, text, sizeof(text)) < 0) + return; + st->files++; + if (stat(path, &sb) == 0 && (sb.st_mode & 0002) != 0) { + add_finding(findings, first, "world-writable", "high", + "%s is writable by anyone", path); + (*count)++; + } + p = text; + 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'; + p = eol != NULL ? eol + 1 : NULL; + s = line; + while (*s == ' ' || *s == '\t') + s++; + if (*s != '$') + continue; + s++; + if (strncmp(s, "final_virus_destiny", 19) == 0 && + strstr(s, "D_PASS") != NULL) { + st->virus_pass = 1; + add_finding(findings, first, "virus-pass", "critical", + "%s sends a virus-positive mail to the " + "recipient (D_PASS)", path); + (*count)++; + } else if (strncmp(s, "final_spam_destiny", 18) == 0 && + strstr(s, "D_PASS") != NULL) { + st->spam_pass = 1; + add_finding(findings, first, "spam-pass", "high", + "%s delivers spam instead of quarantining " + "it (D_PASS)", path); + (*count)++; + } else if (strncmp(s, "bypass_spam_checks", 18) == 0 && + strstr(s, "1") != NULL) { + st->bypass_spam = 1; + add_finding(findings, first, "bypass-spam", "high", + "%s turns the spam check off " + "($bypass_spam_checks)", path); + (*count)++; + } else if (strncmp(s, "bypass_virus_checks", 19) == 0 && + strstr(s, "1") != NULL) { + st->bypass_virus = 1; + add_finding(findings, first, "bypass-virus", "critical", + "%s turns the virus check off " + "($bypass_virus_checks)", path); + (*count)++; + } else if (strncmp(s, "virus_admin", 11) == 0 && + (strstr(s, "undef") != NULL || strstr(s, "\"\"") != NULL)) { + st->no_virus_admin = 1; + add_finding(findings, first, "no-virus-admin", "medium", + "%s sets no virus administrator: a virus " + "finding reaches nobody", path); + (*count)++; + } + } +} + +static void amavis_scan_dir(mcp_buf *findings, int *first, int *count, + const char *dir, struct amavis_state *st) +{ + DIR *d = opendir(dir); + struct dirent *ent; + + if (d == NULL) + return; + while ((ent = readdir(d)) != NULL) { + char path[1024]; + + if (ent->d_name[0] == '.' || ent->d_name[0] == '~') + continue; + snprintf(path, sizeof(path), "%.800s/%.160s", dir, ent->d_name); + amavis_scan_file(findings, first, count, path, st); + } + closedir(d); +} + +static void t_amavis_audit(mcp_conn *c, const mcp_json *args, void *user) +{ + const char *root = mcp_arg_string(args, "root"); + char base[512]; + char path[1024]; + size_t bl; + mcp_buf b; + mcp_buf findings; + struct amavis_state st; + int count = 0; + int first = 1; + + (void)user; + if (root == NULL) + root = ""; + if (strlen(root) >= sizeof(base) - 2) { + mcp_reply_error(c, "root path too long"); + return; + } + snprintf(base, sizeof(base), "%s", root); + bl = strlen(base); + while (bl > 0 && base[bl - 1] == '/') + base[--bl] = '\0'; + memset(&st, 0, sizeof(st)); + mcp_buf_init(&findings); + + snprintf(path, sizeof(path), "%s/etc/amavis/amavisd.conf", base); + amavis_scan_file(&findings, &first, &count, path, &st); + snprintf(path, sizeof(path), "%s/etc/amavis/conf.d", base); + amavis_scan_dir(&findings, &first, &count, path, &st); + + if (st.files == 0) { + add_finding(&findings, &first, "no-config", "info", + "no amavis configuration found"); + count = 1; + } else if (count == 0) { + add_finding(&findings, &first, "no-findings", "info", + "the amavis configuration carries no weak setting"); + count = 1; + } + mcp_buf_init(&b); + mcp_buf_puts(&b, "{\"root\":"); + apex_json_str(&b, base); + mcp_buf_printf(&b, ",\"files\":%d,\"virus_pass\":%s," + "\"spam_pass\":%s,\"bypass_spam\":%s," + "\"bypass_virus\":%s,\"no_virus_admin\":%s," + "\"findings_count\":%d,\"findings\":[", + st.files, st.virus_pass ? "true" : "false", + st.spam_pass ? "true" : "false", + st.bypass_spam ? "true" : "false", + st.bypass_virus ? "true" : "false", + st.no_virus_admin ? "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, " @@ -33361,6 +33876,29 @@ const mcp_tool TOOLS_BLUE[] = { "{\"type\":\"object\",\"properties\":{" "\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}", t_systemd_socket_audit }, + { "spamassassin_audit", + "Parse SpamAssassin local.cf/local.conf (root prefix): a " + "required_score at or below 0 (the filter lets everything through), " + "report_safe 0 (spam forwarded as the original message), " + "skip_rbl_checks 1 and a rule scored 0. Read-only.", + "{\"type\":\"object\",\"properties\":{" + "\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}", + t_spamassassin_audit }, + { "opendmarc_audit", + "Parse opendmarc.conf (root prefix): RejectFailures off or absent, " + "FailureReports off, IgnoreAuthenticatedClients yes and an inet: " + "milter socket. Read-only.", + "{\"type\":\"object\",\"properties\":{" + "\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}", + t_opendmarc_audit }, + { "amavis_audit", + "Parse amavisd.conf and conf.d fragments (root prefix): a " + "final_virus_destiny of D_PASS, final_spam_destiny D_PASS, " + "$bypass_spam_checks or $bypass_virus_checks set and a missing virus " + "administrator. Read-only.", + "{\"type\":\"object\",\"properties\":{" + "\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}", + t_amavis_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 244e45b..779d3ae 100644 --- a/src/tools_red.c +++ b/src/tools_red.c @@ -10545,6 +10545,92 @@ static void t_confluence_probe(mcp_conn *c, const mcp_json *args, void *user) mcp_buf_free(&b); } +/* ---------------------------------------------------------- bitbucket_probe */ + +/* Bitbucket Server on 7990: /rest/api/1.0/projects answers without a token + * when the instance allows anonymous access, and it lists every project with + * key, name and public flag. /rest/api/1.0/application-properties answers on + * any instance, so only a 2xx with a body on the project list means open. */ +static void t_bitbucket_probe(mcp_conn *c, const mcp_json *args, void *user) +{ + static const char *paths[] = { "/rest/api/1.0/application-properties", + "/rest/api/1.0/projects?limit=25", NULL }; + const char *target = mcp_arg_string(args, "target"); + int port = mcp_arg_int(args, "port", 7990); + 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 projects_open = 0; + char *evidence = NULL; + char label[64]; + 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, "bitbucket_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 = 0; + int projects = strstr(paths[i], "/projects") != NULL; + + status[0] = '\0'; + if (apex_http_once(target, port, tls, "GET", paths[i], NULL, NULL, + 8000, &resp, &len, status, sizeof(status)) == 0) { + tested++; + code = apex_status_code(status); + if (code >= 200 && code < 300 && resp != NULL && len > 0) { + open_count++; + if (projects) + projects_open = 1; + if (evidence == NULL && projects) { + label[0] = '\0'; + snprintf(label, sizeof(label), "bitbucket-%d", i); + 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}", code); + free(resp); + } + mcp_buf_printf(&b, "],\"tested\":%d,\"open_count\":%d," + "\"projects_open\":%s,\"evidence\":", + tested, open_count, projects_open ? "true" : "false"); + if (evidence != NULL) + apex_json_str(&b, evidence); + else + mcp_buf_puts(&b, "null"); + mcp_buf_puts(&b, "}"); + apex_audit("bitbucket_probe", target, port, !projects_open, + projects_open + ? "the Bitbucket project API answers without credentials" + : "the Bitbucket project API requires credentials", "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: " @@ -11497,6 +11583,17 @@ const mcp_tool TOOLS_RED[] = { "\"tls\":{\"type\":\"boolean\"}}," "\"required\":[\"target\"]}", t_confluence_probe }, + { "bitbucket_probe", + "Bitbucket Server on 7990: /rest/api/1.0/projects without a token when " + "anonymous access is on; the project list (key, name, public flag) is " + "saved as evidence. Also reports whether the instance answers the " + "application-properties endpoint.", + "{\"type\":\"object\",\"properties\":{" + "\"target\":{\"type\":\"string\"}," + "\"port\":{\"type\":\"integer\",\"description\":\"default 7990\"}," + "\"tls\":{\"type\":\"boolean\"}}," + "\"required\":[\"target\"]}", + t_bitbucket_probe }, { "ollama_probe", "Query Ollama (default 11434) without credentials: /api/version, " "/api/tags and /api/ps. A 2xx on /api/tags lists every pulled model and " diff --git a/tests/gate_audit.py b/tests/gate_audit.py index 4d1eb28..aec9ae9 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 = 406 +EXPECTED_TOOLS = 410 GATE = ("gated(", "gated_cmd(", "gated_run(", "apex_scope_check(") # Handlers may hand argv to a shared runner instead of calling the gate # themselves. That runner has to call the gate; both halves are checked diff --git a/tests/run.py b/tests/run.py index ddc24ab..324ef3f 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("406 tools registered", len(tools) == 406, len(tools)) +check("410 tools registered", len(tools) == 410, 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"]) == 406) + check("http tools/list", len(listing["result"]["tools"]) == 410) 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 @@ -18478,6 +18478,193 @@ for d in (dk_root, dk_good_root, dk_empty_root, rm_root, rm_good_root, rm_empty_root, cfg65_home): shutil.rmtree(d, ignore_errors=True) +# --- 1.95.0: spamassassin, opendmarc and amavis audits plus the Bitbucket probe --- +print("\n== spamassassin, opendmarc, amavis and Bitbucket ==") +sa_root = tempfile.mkdtemp(prefix="apex-sa-") +os.makedirs(os.path.join(sa_root, "etc", "spamassassin")) +sa_bad = os.path.join(sa_root, "etc", "spamassassin", "local.cf") +with open(sa_bad, "w") as f: + f.write("required_score 0\n") + f.write("report_safe 0\n") + f.write("skip_rbl_checks 1\n") + f.write("score BAYES_99 0\n") + f.write("score SPF_FAIL 2.5\n") +os.chmod(sa_bad, 0o666) + +sa_good_root = tempfile.mkdtemp(prefix="apex-sag-") +os.makedirs(os.path.join(sa_good_root, "etc", "spamassassin")) +with open(os.path.join(sa_good_root, "etc", "spamassassin", "local.cf"), + "w") as f: + f.write("required_score 5.0\n") + f.write("report_safe 1\n") + f.write("score BAYES_99 3.5\n") + +dm_root = tempfile.mkdtemp(prefix="apex-dm-") +os.makedirs(os.path.join(dm_root, "etc")) +with open(os.path.join(dm_root, "etc", "opendmarc.conf"), "w") as f: + f.write("RejectFailures false\n") + f.write("FailureReports off\n") + f.write("IgnoreAuthenticatedClients yes\n") + f.write("Socket inet:8893@127.0.0.1\n") + +dm_good_root = tempfile.mkdtemp(prefix="apex-dmg-") +os.makedirs(os.path.join(dm_good_root, "etc")) +with open(os.path.join(dm_good_root, "etc", "opendmarc.conf"), "w") as f: + f.write("RejectFailures true\n") + f.write("FailureReports on\n") + f.write("Socket local:/run/opendmarc/opendmarc.sock\n") + +av_root = tempfile.mkdtemp(prefix="apex-av-") +os.makedirs(os.path.join(av_root, "etc", "amavis", "conf.d")) +with open(os.path.join(av_root, "etc", "amavis", "conf.d", "50-user"), + "w") as f: + f.write("$final_virus_destiny = D_PASS;\n") + f.write("$final_spam_destiny = D_PASS;\n") + f.write("$bypass_spam_checks = 1;\n") + f.write("$virus_admin = undef;\n") + +av_good_root = tempfile.mkdtemp(prefix="apex-avg-") +os.makedirs(os.path.join(av_good_root, "etc", "amavis", "conf.d")) +with open(os.path.join(av_good_root, "etc", "amavis", "conf.d", "50-user"), + "w") as f: + f.write("$final_virus_destiny = D_DISCARD;\n") + f.write("$final_spam_destiny = D_BOUNCE;\n") + f.write('$virus_admin = "postmaster\\@example.test";\n') + + +class _BitbucketHandler(http.server.BaseHTTPRequestHandler): + def do_GET(self): + path = urllib.parse.urlsplit(self.path).path + if path == "/rest/api/1.0/application-properties": + body = b'{"version":"8.19.4","buildNumber":"801004"}' + self.send_response(200) + elif path == "/rest/api/1.0/projects": + body = (b'{"size":2,"values":[{"key":"OPS","name":"Operations",' + b'"public":true},{"key":"DEV","name":"Development",' + b'"public":false}]}') + self.send_response(200) + else: + body = b'{"errors":[{"message":"not found"}]}' + self.send_response(404) + 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 _BitbucketAuthHandler(_BitbucketHandler): + def do_GET(self): + body = b'{"errors":[{"message":"unauthorized"}]}' + self.send_response(401) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + + +bb_srv = http.server.HTTPServer(("127.0.0.1", 0), _BitbucketHandler) +bb_port = bb_srv.server_address[1] +bb_auth_srv = http.server.HTTPServer(("127.0.0.1", 0), + _BitbucketAuthHandler) +bb_auth_port = bb_auth_srv.server_address[1] +for _s in (bb_srv, bb_auth_srv): + threading.Thread(target=_s.serve_forever, daemon=True).start() + +cfg66_home = tempfile.mkdtemp(prefix="apex-cfg66-home-") +write_scope(cfg66_home, + {"label": "cfg66", "expires": "2099-01-01T00:00:00Z", + "allow_private": True, "allow_ip": ["127.0.0.1"], + "allow_port": [str(bb_port), str(bb_auth_port)]}) + +sa_empty_root = tempfile.mkdtemp(prefix="apex-sae-") +dm_empty_root = tempfile.mkdtemp(prefix="apex-dme-") +av_empty_root = tempfile.mkdtemp(prefix="apex-ave-") +res, _ = run([ + call(1, "spamassassin_audit", root=sa_root), + call(2, "spamassassin_audit", root=sa_good_root), + call(3, "spamassassin_audit", root=sa_empty_root), + call(4, "opendmarc_audit", root=dm_root), + call(5, "opendmarc_audit", root=dm_good_root), + call(6, "opendmarc_audit", root=dm_empty_root), + call(7, "amavis_audit", root=av_root), + call(8, "amavis_audit", root=av_good_root), + call(9, "amavis_audit", root=av_empty_root), + call(10, "bitbucket_probe", target="127.0.0.1", port=bb_port, tls=False), + call(11, "bitbucket_probe", target="127.0.0.1", port=bb_auth_port, + tls=False), +], home=cfg66_home) +by_id = {r["id"]: r for r in res if "id" in r} + +sa = json.loads(text(by_id[1])) +sa_ids = {f["id"] for f in sa["findings"]} +check("spamassassin_audit flags a zero score, unsafe reports, skipped RBL and " + "a dead rule", + {"world-writable", "score-off", "report-unsafe", "rbl-skipped", + "dead-rule"} <= sa_ids and sa["files"] == 1 and + sa["score_off"] is True and sa["report_plain"] is True and + sa["rbl_skipped"] is True and sa["dead_rules"] == 1, sa) +sa_good = json.loads(text(by_id[2])) +check("spamassassin_audit accepts a live threshold and safe reports", + {f["id"] for f in sa_good["findings"]} == {"no-findings"} and + sa_good["score_off"] is False and sa_good["dead_rules"] == 0, sa_good) +sa_empty = json.loads(text(by_id[3])) +check("spamassassin_audit reports an empty tree as no-config", + sa_empty["files"] == 0 and + [f["id"] for f in sa_empty["findings"]] == ["no-config"], sa_empty) + +dm = json.loads(text(by_id[4])) +dm_ids = {f["id"] for f in dm["findings"]} +check("opendmarc_audit flags no reject, off reports, skipped auth clients and " + "an inet socket", + {"no-reject", "reports-off", "ignore-authenticated", + "inet-socket"} <= dm_ids and dm["files"] == 1 and + dm["no_reject"] is True and dm["reports_off"] is True and + dm["ignore_auth"] is True and dm["inet_socket"] is True, dm) +dm_good = json.loads(text(by_id[5])) +check("opendmarc_audit accepts rejecting and reporting over a unix socket", + {f["id"] for f in dm_good["findings"]} == {"no-findings"} and + dm_good["no_reject"] is False and dm_good["inet_socket"] is False, + dm_good) +dm_empty = json.loads(text(by_id[6])) +check("opendmarc_audit reports an empty tree as no-config", + dm_empty["files"] == 0 and + [f["id"] for f in dm_empty["findings"]] == ["no-config"], dm_empty) + +av = json.loads(text(by_id[7])) +av_ids = {f["id"] for f in av["findings"]} +check("amavis_audit flags passing on virus and spam, a bypass and no admin", + {"virus-pass", "spam-pass", "bypass-spam", + "no-virus-admin"} <= av_ids and av["files"] == 1 and + av["virus_pass"] is True and av["spam_pass"] is True and + av["bypass_spam"] is True and av["no_virus_admin"] is True, av) +av_good = json.loads(text(by_id[8])) +check("amavis_audit accepts discard/bounce with an administrator", + {f["id"] for f in av_good["findings"]} == {"no-findings"} and + av_good["virus_pass"] is False and + av_good["no_virus_admin"] is False, av_good) +av_empty = json.loads(text(by_id[9])) +check("amavis_audit reports an empty tree as no-config", + av_empty["files"] == 0 and + [f["id"] for f in av_empty["findings"]] == ["no-config"], av_empty) + +bb = json.loads(text(by_id[10])) +check("bitbucket_probe finds an open project API and saves evidence", + bb["projects_open"] is True and bb["open_count"] == 2 and + bb["evidence"] is not None and os.path.isfile(bb["evidence"]), bb) +bb_auth = json.loads(text(by_id[11])) +check("bitbucket_probe reports a closed project API as not open", + bb_auth["projects_open"] is False and bb_auth["evidence"] is None and + all(e["status"] == 401 for e in bb_auth["endpoints"]), bb_auth) + +for _s in (bb_srv, bb_auth_srv): + _s.shutdown() +for d in (sa_root, sa_good_root, sa_empty_root, dm_root, dm_good_root, + dm_empty_root, av_root, av_good_root, av_empty_root, cfg66_home): + shutil.rmtree(d, ignore_errors=True) + # --- 1.84.0: gated wrappers for external scanners (mock binaries) --- print("\n== external scanner wrappers ==\n")