apex-twin 1.31.0: PHP, Dovecot and OpenVPN audits, SMTP STARTTLS probe

Blue:
- php_config_audit: /etc/php.ini or /etc/php/*/*/php.ini (root prefix):
  expose_php, display_errors, allow_url_include/fopen, empty
  disable_functions, session.cookie_secure Off and world-writable files.
- dovecot_audit: dovecot.conf and conf.d: ssl = no,
  disable_plaintext_auth = no, PLAIN in auth_mechanisms and world-writable
  drop-ins.
- openvpn_audit: server.conf and server/*.conf: missing tls-auth/tls-crypt,
  BF-CBC/DES/none cipher, SHA1/MD5 HMAC, duplicate-cn and world-writable
  files.

Red:
- smtp_starttls_probe: EHLO, STARTTLS, 220 and a real upgrade verified with
  openssl s_client -starttls smtp; reports the negotiated TLS version and
  cipher. No authentication and no mail.

Tests with vulnerable and hardened PHP/Dovecot/OpenVPN trees and a Python SMTP
server that upgrades to TLS plus one that does not. 817 checks, ASan suite
green, analyze clean, release-check green including distcheck 1.31.0.
This commit is contained in:
lotherk committed 2026-11-21 20:14:12 +00:00
1 parent 874a32de08
commit 409c80faf2
9 files changed
+817 -18

No files matched your search

+20 -1
View File
@@ -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.30.0 — 2026-11-21 — this change
## 1.31.0 — 2026-11-21 — this change
- **PHP** (`php_config_audit`): finds /etc/php.ini or /etc/php/*/*/php.ini
(root prefix) and flags expose_php, display_errors, allow_url_include,
allow_url_fopen, an empty disable_functions, session.cookie_secure Off and
world-writable files.
- **Dovecot** (`dovecot_audit`): dovecot.conf and conf.d: ssl = no,
disable_plaintext_auth = no, PLAIN in auth_mechanisms and world-writable
drop-ins.
- **OpenVPN** (`openvpn_audit`): server.conf and server/*.conf: missing
tls-auth/tls-crypt, BF-CBC/DES/none, SHA1/MD5 HMAC, duplicate-cn and
world-writable files.
- **SMTP STARTTLS** (`smtp_starttls_probe`): EHLO for STARTTLS, sends it and,
on 220, verifies the upgrade with openssl s_client -starttls smtp, reporting
the negotiated TLS version and cipher. No authentication and no mail.
- Tests with vulnerable and hardened PHP/Dovecot/OpenVPN trees and a Python
SMTP server that upgrades to TLS (plus one that does not). 817 checks.
Coverage 81.3%.
## 1.30.0 — 2026-11-21 — 874a32d
- **MySQL** (`mysql_config_audit`): my.cnf and my.cnf.d/conf.d (root prefix):
bind-address on every interface, skip-grant-tables, local-infile, an empty
+16 -8
View File
@@ -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.
**159 tools · 802 end-to-end checks · 340 fuzz cases · 81.2% line coverage · ~647 KiB**
**163 tools · 817 end-to-end checks · 340 fuzz cases · 81.3% line coverage · ~660 KiB**
```
$ ./apex-twin --check
{"version":"1.30.0","tools":159,...,"scope":{"loaded":true,...}}
{"version":"1.31.0","tools":163,...,"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 (159)
## Tools (163)
### Scope and bookkeeping
@@ -294,6 +294,7 @@ command in the allowlist: a receiver is a scanner, a transmitter is an attack.
| `kerberos_discovery` | DNS SRV for _kerberos._tcp/_udp and _kpasswd._tcp: KDC priority/weight/port/target |
| `winrm_probe` | GET /wsman: 401/405 with Negotiate/NTLM/Kerberos identifies WinRM; evidence on hit |
| `dnssec_check` | DNSKEY/DS with +dnssec: signed, algorithm, key bits; flags unsigned/RSASHA1/short keys |
| `smtp_starttls_probe` | EHLO/STARTTLS and a real TLS upgrade via openssl s_client -starttls smtp |
| `imap_probe` | Banner, CAPABILITY list, STARTTLS and LOGINDISABLED; no LOGIN |
| `pop3_probe` | Banner, CAPA list, STLS flag; no USER/PASS |
| `sip_probe` | One UDP OPTIONS: status line, Server header, Allow methods |
@@ -356,6 +357,9 @@ command in the allowlist: a receiver is a scanner, a transmitter is an attack.
| `mysql_config_audit` | my.cnf: bind-all, skip-grant-tables, local-infile, empty secure-file-priv, writable files |
| `apache_audit` | Apache: ServerTokens Full/OS, Options Indexes, AllowOverride All, SSLv3/TLSv1, signature |
| `haproxy_audit` | haproxy.cfg: stats socket mode, stats auth, bind 443 without ssl, writable config |
| `php_config_audit` | php.ini: expose_php, display_errors, URL include/fopen, disable_functions, cookie_secure |
| `dovecot_audit` | dovecot.conf: ssl = no, plaintext auth, PLAIN mechanism, writable drop-ins |
| `openvpn_audit` | server.conf: no tls-auth/tls-crypt, BF-CBC/DES, SHA1/MD5 HMAC, duplicate-cn |
### ESP32
@@ -602,6 +606,8 @@ The full threat model, trust boundaries and a hardening checklist are in
| postfix/nginx/redis | config reads only; optional root prefix |
| mysql/apache/haproxy | config reads only; optional root prefix |
| DNSSEC | two gated dig queries |
| php/dovecot/openvpn | config reads only; optional root prefix |
| SMTP STARTTLS | one handshake, no auth, no mail |
| USB audit | 200 devices, sysfs reads only |
---
@@ -613,7 +619,7 @@ Flags are fixed: `-std=c99 -Wall -Wextra -Wpedantic -Werror` plus
```sh
make # apex-twin
make test # version check + gate audit + 802 end-to-end checks
make test # version check + gate audit + 817 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
@@ -728,7 +734,8 @@ src/tools_red.c vuln_check, subdomain_enum, cred_test, vhost_discovery,
email_security_audit, smtp_open_relay, dns_open_resolver,
crlf_probe, dns_version_bind, oidc_discovery,
security_txt, http2_probe, ssh_hostkey_fingerprint,
kerberos_discovery, winrm_probe, dnssec_check
kerberos_discovery, winrm_probe, dnssec_check,
smtp_starttls_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,
@@ -771,7 +778,7 @@ examples/ scope.json
## Status
1.30.0. Built and run: 159 tools, 802 checks, 340 fuzz cases and 81.2% line
1.31.0. Built and run: 163 tools, 817 checks, 340 fuzz cases and 81.3% line
coverage. The loop closes: a probe captures the exchange as a pair, a finding
points at it, `finding_update` gives it a state, an asset records the host it
came from, `cors_check`/`cookie_audit`/`http_options` turn a web answer into a
@@ -817,8 +824,9 @@ audit; 1.25 added OIDC discovery, security.txt and known-hosts checks; 1.26 adde
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; 1.29 added postfix, nginx and redis config audits;
1.30 added MySQL, Apache and HAProxy config audits plus a DNSSEC check, again
without changing an existing file format.
1.30 added MySQL, Apache and HAProxy config audits plus a DNSSEC check; 1.31
added PHP, Dovecot and OpenVPN audits plus an SMTP STARTTLS check, again without
changing an existing file format.
Not yet wired into a fleet router, and there is no TLS interception.
+4 -1
View File
@@ -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.30.x** (`src/apex.h`, `APEX_VERSION`). Security fixes land on the current
is **1.31.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,9 @@ 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.
- **STARTTLS is a handshake, not a login.** `smtp_starttls_probe` sends EHLO,
STARTTLS and reads the TLS handshake; it never authenticates and never sends
mail. PHP, Dovecot and OpenVPN audits read configuration files.
- **DNSSEC checks are two DNS queries.** `dnssec_check` asks for DNSKEY and
DS records of an in-scope domain and decodes the public key size; it never
validates a chain or contacts a registrar.
+5 -4
View File
@@ -1,5 +1,5 @@
.\" apex-twin.1 - manual page for APEX TWIN
.TH APEX-TWIN 1 "2026-11-21" "apex-twin 1.30.0" "User Commands"
.TH APEX-TWIN 1 "2026-11-21" "apex-twin 1.31.0" "User Commands"
.SH NAME
apex-twin \- scope\-gated offensive\-security MCP server
.SH SYNOPSIS
@@ -43,7 +43,8 @@ memcached_probe, param_fuzz, open_redirect, host_header_injection, ssti_probe,
xxe_probe, ldap_probe, rdp_probe, smb_probe, vnc_probe, mqtt_probe,
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), defensive
ldap_search, openapi_discovery, csrf_audit, rpc_probe, ntp_probe, dnssec_check, smtp_starttls_probe),
defensive
audits (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,
@@ -52,8 +53,8 @@ kernel_hardening, sudoers_audit, auditd_audit, ssh_config_audit,
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,
mysql_config_audit, apache_audit, haproxy_audit,
redis_config_audit) and ESP32 board tools (esp_info, esp_monitor,
mysql_config_audit, apache_audit, haproxy_audit, redis_config_audit,
php_config_audit, dovecot_audit, openvpn_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
+1 -1
View File
@@ -15,7 +15,7 @@
#include "mcp.h"
#define APEX_VERSION "1.30.0"
#define APEX_VERSION "1.31.0"
#define APEX_MAX_OUT (1u << 20)
#define APEX_PATH_MAX 512
#define APEX_PATH_FULL 1024
+429
View File
@@ -7584,6 +7584,414 @@ static void t_haproxy_audit(mcp_conn *c, const mcp_json *args, void *user)
mcp_buf_free(&findings);
}
/* ------------------------------------------------------------- php_config_audit */
static int php_find_ini(const char *root, char *out, size_t n)
{
static const char *fixed[] = { "/etc/php.ini", "/etc/php/php.ini",
NULL };
char dir[1024];
int i;
for (i = 0; fixed[i] != NULL; i++) {
snprintf(out, n, "%.400s%s", root, fixed[i]);
if (access(out, R_OK) == 0)
return 1;
}
snprintf(dir, sizeof(dir), "%.400s/etc/php", root);
{
DIR *d = opendir(dir);
struct dirent *ver;
if (d == NULL)
return 0;
while ((ver = readdir(d)) != NULL) {
char vdir[1024];
DIR *vd;
struct dirent *sapi;
if (ver->d_name[0] == '.')
continue;
snprintf(vdir, sizeof(vdir), "%.800s/%.160s", dir,
ver->d_name);
vd = opendir(vdir);
if (vd == NULL)
continue;
while ((sapi = readdir(vd)) != NULL) {
char cand[1024];
if (sapi->d_name[0] == '.')
continue;
snprintf(cand, sizeof(cand), "%.800s/%.160s/php.ini",
vdir, sapi->d_name);
if (access(cand, R_OK) == 0) {
snprintf(out, n, "%s", cand);
closedir(vd);
closedir(d);
return 1;
}
}
closedir(vd);
}
closedir(d);
}
return 0;
}
static void t_php_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];
mcp_buf b;
mcp_buf findings;
int first = 1;
int count = 0;
int expose = 0;
int display = 0;
struct stat st;
(void)user;
if (root == NULL)
root = "";
if (strlen(root) > 400) {
mcp_reply_error(c, "root path too long");
return;
}
if (!php_find_ini(root, path, sizeof(path))) {
mcp_reply_error(c, "cannot read a php.ini");
return;
}
if (read_text(path, config, sizeof(config)) <= 0) {
mcp_reply_error(c, "cannot read a php.ini");
return;
}
mcp_buf_init(&findings);
if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) {
add_finding(&findings, &first, "world-writable", "high",
"php.ini is writable by anyone");
count++;
}
if (conf_value(config, "expose_php", value, sizeof(value)))
expose = strcasecmp(value, "On") == 0;
else
expose = 1;
if (expose) {
add_finding(&findings, &first, "expose-php", "low",
"PHP discloses its version in HTTP headers");
count++;
}
if (conf_value(config, "display_errors", value, sizeof(value)) &&
strcasecmp(value, "On") == 0) {
add_finding(&findings, &first, "display-errors", "medium",
"errors are printed to visitors");
display = 1;
count++;
}
if (conf_value(config, "allow_url_include", value, sizeof(value)) &&
strcasecmp(value, "On") == 0) {
add_finding(&findings, &first, "allow-url-include", "high",
"remote URLs may be included as code");
count++;
}
if (conf_value(config, "allow_url_fopen", value, sizeof(value)) &&
strcasecmp(value, "On") == 0) {
add_finding(&findings, &first, "allow-url-fopen", "low",
"remote URLs may be opened as files");
count++;
}
if (conf_value(config, "disable_functions", value, sizeof(value))) {
if (value[0] == '\0') {
add_finding(&findings, &first, "no-disable-functions",
"medium",
"no functions are disabled");
count++;
}
} else {
add_finding(&findings, &first, "no-disable-functions", "medium",
"disable_functions is not set");
count++;
}
if (conf_value(config, "session.cookie_secure", value,
sizeof(value)) &&
strcasecmp(value, "Off") == 0) {
add_finding(&findings, &first, "insecure-cookie", "low",
"session cookies may travel over plain HTTP");
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, ",\"expose_php\":%s,\"display_errors\":%s,"
"\"findings_count\":%d,\"findings\":[",
expose ? "true" : "false", display ? "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);
}
/* -------------------------------------------------------------- dovecot_audit */
static void dovecot_scan_file(mcp_buf *findings, int *first, int *count,
const char *path, int *files, int *ssl_off,
int *plaintext_ok, int *plain_mech)
{
static char text[65536];
char value[512];
struct stat st;
if (read_text(path, text, sizeof(text)) < 0)
return;
(*files)++;
if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) {
add_finding(findings, first, "world-writable", "high",
"%s is writable by anyone", path);
(*count)++;
}
if (conf_value(text, "ssl", value, sizeof(value)) &&
strcasecmp(value, "no") == 0) {
if (!*ssl_off)
add_finding(findings, first, "ssl-off", "high",
"%s disables TLS", path);
*ssl_off = 1;
(*count)++;
}
if (conf_value(text, "disable_plaintext_auth", value, sizeof(value)) &&
strcasecmp(value, "no") == 0) {
if (!*plaintext_ok)
add_finding(findings, first, "plaintext-auth", "high",
"%s allows plaintext authentication", path);
*plaintext_ok = 1;
(*count)++;
}
if (conf_value(text, "auth_mechanisms", value, sizeof(value))) {
if (conf_has_word(value, "plain") ||
conf_has_word(value, "PLAIN")) {
if (!*plain_mech)
add_finding(findings, first, "plain-mechanism",
"medium",
"%s offers PLAIN authentication", path);
*plain_mech = 1;
(*count)++;
}
}
}
static void dovecot_scan_dir(mcp_buf *findings, int *first, int *count,
const char *dir, int *files, int *ssl_off,
int *plaintext_ok, int *plain_mech)
{
DIR *d = opendir(dir);
struct dirent *ent;
if (d == NULL)
return;
while ((ent = readdir(d)) != NULL) {
char path[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(path, sizeof(path), "%.800s/%.160s", dir, ent->d_name);
if (stat(path, &st) != 0 || !S_ISREG(st.st_mode))
continue;
dovecot_scan_file(findings, first, count, path, files, ssl_off,
plaintext_ok, plain_mech);
}
closedir(d);
}
static void t_dovecot_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 ssl_off = 0;
int plaintext_ok = 0;
int plain_mech = 0;
(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/dovecot/dovecot.conf", root);
dovecot_scan_file(&findings, &first, &count, path, &files, &ssl_off,
&plaintext_ok, &plain_mech);
snprintf(path, sizeof(path), "%s/etc/dovecot/conf.d", root);
dovecot_scan_dir(&findings, &first, &count, path, &files, &ssl_off,
&plaintext_ok, &plain_mech);
if (files == 0) {
mcp_buf_free(&findings);
mcp_reply_error(c, "cannot read the Dovecot configuration");
return;
}
mcp_buf_init(&b);
mcp_buf_puts(&b, "{\"root\":");
apex_json_str(&b, root);
mcp_buf_printf(&b, ",\"files\":%d,\"ssl_off\":%s,\"plaintext_auth\":%s,"
"\"findings_count\":%d,\"findings\":[",
files, ssl_off ? "true" : "false",
plaintext_ok ? "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);
}
/* -------------------------------------------------------------- openvpn_audit */
static void openvpn_scan_file(mcp_buf *findings, int *first, int *count,
const char *path, int *files,
int *no_tls, int *weak_cipher, int *weak_auth,
int *dup_cn)
{
static char text[65536];
char value[512];
struct stat st;
int has_tls = 0;
if (read_text(path, text, sizeof(text)) < 0)
return;
(*files)++;
if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) {
add_finding(findings, first, "world-writable", "high",
"%s is writable by anyone", path);
(*count)++;
}
has_tls = conf_value(text, "tls-auth", value, sizeof(value)) ||
conf_value(text, "tls-crypt", value, sizeof(value)) ||
conf_value(text, "tls-crypt-v2", value, sizeof(value));
if (!has_tls) {
if (!*no_tls)
add_finding(findings, first, "no-tls-auth", "medium",
"%s has no tls-auth/tls-crypt", path);
*no_tls = 1;
(*count)++;
}
if (conf_value(text, "cipher", value, sizeof(value))) {
if (strstr(value, "BF-CBC") != NULL ||
strstr(value, "DES") != NULL ||
strcasecmp(value, "none") == 0) {
if (!*weak_cipher)
add_finding(findings, first, "weak-cipher", "high",
"%s uses a legacy or no cipher", path);
*weak_cipher = 1;
(*count)++;
}
}
if (conf_value(text, "auth", value, sizeof(value))) {
if (strcasecmp(value, "SHA1") == 0 ||
strcasecmp(value, "MD5") == 0) {
if (!*weak_auth)
add_finding(findings, first, "weak-auth", "medium",
"%s uses a weak HMAC", path);
*weak_auth = 1;
(*count)++;
}
}
if (conf_value(text, "duplicate-cn", value, sizeof(value))) {
if (!*dup_cn)
add_finding(findings, first, "duplicate-cn", "medium",
"%s lets several clients share a certificate",
path);
*dup_cn = 1;
(*count)++;
}
}
static void openvpn_scan_dir(mcp_buf *findings, int *first, int *count,
const char *dir, int *files, int *no_tls,
int *weak_cipher, int *weak_auth, int *dup_cn)
{
DIR *d = opendir(dir);
struct dirent *ent;
if (d == NULL)
return;
while ((ent = readdir(d)) != NULL) {
char path[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(path, sizeof(path), "%.800s/%.160s", dir, ent->d_name);
if (stat(path, &st) != 0 || !S_ISREG(st.st_mode))
continue;
openvpn_scan_file(findings, first, count, path, files, no_tls,
weak_cipher, weak_auth, dup_cn);
}
closedir(d);
}
static void t_openvpn_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 no_tls = 0;
int weak_cipher = 0;
int weak_auth = 0;
int dup_cn = 0;
(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/openvpn/server.conf", root);
openvpn_scan_file(&findings, &first, &count, path, &files, &no_tls,
&weak_cipher, &weak_auth, &dup_cn);
snprintf(path, sizeof(path), "%s/etc/openvpn/server", root);
openvpn_scan_dir(&findings, &first, &count, path, &files, &no_tls,
&weak_cipher, &weak_auth, &dup_cn);
if (files == 0) {
mcp_buf_free(&findings);
mcp_reply_error(c, "cannot read an OpenVPN server config");
return;
}
mcp_buf_init(&b);
mcp_buf_puts(&b, "{\"root\":");
apex_json_str(&b, root);
mcp_buf_printf(&b, ",\"files\":%d,\"tls_auth\":%s,\"findings_count\":%d,"
"\"findings\":[", files, no_tls ? "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);
}
const mcp_tool TOOLS_BLUE[] = {
{ "hardening_audit",
"Read-only posture audit of the local host: sshd_config (root login, "
@@ -8005,6 +8413,27 @@ const mcp_tool TOOLS_BLUE[] = {
"{\"type\":\"object\",\"properties\":{"
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
t_haproxy_audit },
{ "php_config_audit",
"Read a php.ini (root prefix; /etc/php.ini or /etc/php/*/*/php.ini): "
"expose_php, display_errors, allow_url_include/fopen, disable_functions, "
"session.cookie_secure and world-writable files. Read-only.",
"{\"type\":\"object\",\"properties\":{"
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
t_php_config_audit },
{ "dovecot_audit",
"Parse dovecot.conf and conf.d (root prefix): ssl = no, "
"disable_plaintext_auth = no, PLAIN in auth_mechanisms and "
"world-writable files. Read-only.",
"{\"type\":\"object\",\"properties\":{"
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
t_dovecot_audit },
{ "openvpn_audit",
"Parse server.conf and server/*.conf (root prefix): missing "
"tls-auth/tls-crypt, BF-CBC/DES/none cipher, SHA1/MD5 HMAC, duplicate-cn "
"and world-writable files. Read-only.",
"{\"type\":\"object\",\"properties\":{"
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
t_openvpn_audit },
};
const size_t TOOLS_BLUE_N = sizeof(TOOLS_BLUE) / sizeof(TOOLS_BLUE[0]);
+135
View File
@@ -5097,6 +5097,132 @@ static void t_http2_probe(mcp_conn *c, const mcp_json *args, void *user)
mcp_buf_free(&b);
}
/* --------------------------------------------------------- smtp_starttls_probe */
static void t_smtp_starttls_probe(mcp_conn *c, const mcp_json *args, void *user)
{
const char *target = mcp_arg_string(args, "target");
int port = mcp_arg_int(args, "port", 25);
char banner[512];
char reply[8192];
char features[8][64];
char protocol[64];
char cipher[128];
char conn[320];
int nfeat = 0;
int banner_code;
int ehlo_code = 0;
int starttls_code = 0;
int advertised = 0;
int accepted = 0;
int established = 0;
int fd;
mcp_buf b;
(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, "smtp_starttls_probe", target, port))
return;
fd = tcp_connect_host(target, port, 8000);
if (fd < 0) {
mcp_reply_error(c, "cannot connect to the SMTP service");
return;
}
banner[0] = '\0';
banner_code = (read_line(fd, banner, sizeof(banner)) >= 0)
? reply_code(banner) : 0;
if (banner_code != 220) {
close(fd);
mcp_reply_error(c, "no SMTP banner");
return;
}
if (write_all(fd, "EHLO apex-twin\r\n") == 0) {
if (read_reply(fd, reply, sizeof(reply)) > 0) {
char *save = NULL;
char *line = strtok_r(reply, "\n", &save);
ehlo_code = reply_code(line);
line = strtok_r(NULL, "\n", &save);
while (line != NULL && nfeat < 8) {
if (reply_feature(line, features[nfeat],
sizeof(features[0])) == 0) {
if (strcasecmp(features[nfeat],
"STARTTLS") == 0)
advertised = 1;
nfeat++;
}
line = strtok_r(NULL, "\n", &save);
}
}
}
if (advertised && write_all(fd, "STARTTLS\r\n") == 0) {
if (read_reply(fd, reply, sizeof(reply)) > 0) {
starttls_code = reply_code(reply);
accepted = starttls_code == 220;
}
}
close(fd);
if (accepted) {
char *argv[12];
int k = 0;
apex_proc p;
if (!apex_have("openssl")) {
mcp_reply_error(c, "openssl is not installed on this host");
return;
}
tls_conn_string(target, port, conn, sizeof(conn));
argv[k++] = "openssl";
argv[k++] = "s_client";
argv[k++] = "-starttls";
argv[k++] = "smtp";
argv[k++] = "-connect";
argv[k++] = conn;
argv[k++] = "-servername";
argv[k++] = (char *)target;
argv[k] = NULL;
protocol[0] = cipher[0] = '\0';
if (apex_run(argv, 20000, g_cfg.max_output, &p) == 0) {
if (p.out != NULL) {
tls_summary(p.out, protocol, sizeof(protocol), cipher,
sizeof(cipher));
established = protocol[0] != '\0' && cipher[0] != '\0';
}
apex_proc_free(&p);
}
} else {
protocol[0] = cipher[0] = '\0';
}
mcp_buf_init(&b);
mcp_buf_puts(&b, "{\"target\":");
apex_json_str(&b, target);
mcp_buf_printf(&b, ",\"port\":%d,\"banner_code\":%d,\"ehlo_code\":%d,"
"\"starttls_advertised\":%s,\"starttls_code\":%d,"
"\"starttls_accepted\":%s,\"tls_established\":%s,"
"\"protocol\":", port, banner_code, ehlo_code,
advertised ? "true" : "false", starttls_code,
accepted ? "true" : "false",
established ? "true" : "false");
apex_json_str(&b, protocol);
mcp_buf_puts(&b, ",\"cipher\":");
apex_json_str(&b, cipher);
mcp_buf_puts(&b, "}");
apex_audit("smtp_starttls_probe", target, port, established,
established ? "STARTTLS upgraded to TLS"
: (advertised ? "STARTTLS offered, no TLS"
: "STARTTLS not offered"),
advertised ? "STARTTLS" : "EHLO");
mcp_reply_json(c, b.p);
mcp_buf_free(&b);
}
const mcp_tool TOOLS_PROTO[] = {
{ "ftp_probe",
"One-connection FTP check: read the 220 banner, ask FEAT, then try the "
@@ -5405,6 +5531,15 @@ const mcp_tool TOOLS_PROTO[] = {
"\"tls\":{\"type\":\"boolean\"}},"
"\"required\":[\"target\"]}",
t_http2_probe },
{ "smtp_starttls_probe",
"EHLO an SMTP server, check for STARTTLS, send it and, when accepted, "
"verify the upgrade with openssl s_client -starttls smtp, reporting the "
"TLS version and cipher. No authentication and no mail. Gate: target.",
"{\"type\":\"object\",\"properties\":{"
"\"target\":{\"type\":\"string\"},"
"\"port\":{\"type\":\"integer\",\"description\":\"default 25\"}},"
"\"required\":[\"target\"]}",
t_smtp_starttls_probe },
};
const size_t TOOLS_PROTO_N = sizeof(TOOLS_PROTO) / sizeof(TOOLS_PROTO[0]);
+1 -1
View File
@@ -21,7 +21,7 @@ import re
import sys
HERE = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
EXPECTED_TOOLS = 159
EXPECTED_TOOLS = 163
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.
+206 -2
View File
@@ -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("159 tools registered", len(tools) == 159, len(tools))
check("163 tools registered", len(tools) == 163, 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"]) == 159)
check("http tools/list", len(listing["result"]["tools"]) == 163)
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
@@ -7256,6 +7256,210 @@ shutil.rmtree(ha_root, ignore_errors=True)
shutil.rmtree(ha_good_root, ignore_errors=True)
shutil.rmtree(cfg2_home, ignore_errors=True)
print("\n== PHP, Dovecot, OpenVPN and SMTP STARTTLS ==\n")
php_root = tempfile.mkdtemp(prefix="apex-php-")
os.makedirs(os.path.join(php_root, "etc"), exist_ok=True)
php_bad = os.path.join(php_root, "etc", "php.ini")
with open(php_bad, "w") as f:
f.write("expose_php = On\n")
f.write("display_errors = On\n")
f.write("allow_url_include = On\n")
f.write("allow_url_fopen = On\n")
f.write("disable_functions =\n")
f.write("session.cookie_secure = Off\n")
os.chmod(php_bad, 0o666)
php_good_root = tempfile.mkdtemp(prefix="apex-phpg-")
os.makedirs(os.path.join(php_good_root, "etc"))
with open(os.path.join(php_good_root, "etc", "php.ini"), "w") as f:
f.write("expose_php = Off\n")
f.write("display_errors = Off\n")
f.write("allow_url_include = Off\n")
f.write("allow_url_fopen = Off\n")
f.write("disable_functions = exec,passthru,shell_exec\n")
f.write("session.cookie_secure = On\n")
dv_root = tempfile.mkdtemp(prefix="apex-dv-")
os.makedirs(os.path.join(dv_root, "etc", "dovecot", "conf.d"))
with open(os.path.join(dv_root, "etc", "dovecot", "dovecot.conf"),
"w") as f:
f.write("ssl = no\n")
f.write("disable_plaintext_auth = no\n")
f.write("auth_mechanisms = plain login\n")
dv_extra = os.path.join(dv_root, "etc", "dovecot", "conf.d", "10-auth.conf")
with open(dv_extra, "w") as f:
f.write("auth_mechanisms = plain\n")
os.chmod(dv_extra, 0o666)
dv_good_root = tempfile.mkdtemp(prefix="apex-dvg-")
os.makedirs(os.path.join(dv_good_root, "etc", "dovecot"))
with open(os.path.join(dv_good_root, "etc", "dovecot", "dovecot.conf"),
"w") as f:
f.write("ssl = required\n")
f.write("disable_plaintext_auth = yes\n")
f.write("auth_mechanisms = scram-sha-256\n")
ov_root = tempfile.mkdtemp(prefix="apex-ov-")
os.makedirs(os.path.join(ov_root, "etc", "openvpn"))
ov_bad = os.path.join(ov_root, "etc", "openvpn", "server.conf")
with open(ov_bad, "w") as f:
f.write("port 1194\nproto udp\n")
f.write("cipher BF-CBC\n")
f.write("auth SHA1\n")
f.write("duplicate-cn\n")
os.chmod(ov_bad, 0o666)
ov_good_root = tempfile.mkdtemp(prefix="apex-ovg-")
os.makedirs(os.path.join(ov_good_root, "etc", "openvpn"))
with open(os.path.join(ov_good_root, "etc", "openvpn", "server.conf"),
"w") as f:
f.write("port 1194\nproto udp\n")
f.write("tls-crypt ta.key\n")
f.write("cipher AES-256-GCM\n")
f.write("auth SHA256\n")
tls_sec_dir = tempfile.mkdtemp(prefix="apex-smtptls-")
tls_sec_cert = os.path.join(tls_sec_dir, "cert.pem")
tls_sec_key = os.path.join(tls_sec_dir, "key.pem")
subprocess.run(["openssl", "req", "-x509", "-newkey", "rsa:2048", "-nodes",
"-keyout", tls_sec_key, "-out", tls_sec_cert, "-days", "2",
"-subj", "/CN=localhost"],
check=True, capture_output=True)
def read_smtp_line(sock):
buf = b""
while not buf.endswith(b"\n"):
ch = sock.recv(1)
if not ch:
return None
buf += ch
return buf
def smtp_starttls_handler(cert, key, advertise):
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ctx.load_cert_chain(cert, key)
def handle(conn):
conn.sendall(b"220 test ESMTP\r\n")
while True:
line = read_smtp_line(conn)
if not line:
return
up = line.strip().upper()
if up.startswith(b"EHLO"):
if advertise:
conn.sendall(b"250-test greets you\r\n"
b"250-STARTTLS\r\n250 SIZE 1000\r\n")
else:
conn.sendall(b"250-test greets you\r\n"
b"250 SIZE 1000\r\n")
elif up.startswith(b"STARTTLS") and advertise:
conn.sendall(b"220 go ahead\r\n")
try:
tls = ctx.wrap_socket(conn, server_side=True)
try:
tls.recv(1)
except OSError:
pass
tls.close()
except (ssl.SSLError, OSError):
pass
return
elif up.startswith(b"QUIT"):
conn.sendall(b"221 bye\r\n")
return
else:
conn.sendall(b"250 ok\r\n")
return handle
smtp_tls_srv, smtp_tls_port = start_line_server(
smtp_starttls_handler(tls_sec_cert, tls_sec_key, True))
smtp_plain_srv, smtp_plain_port = start_line_server(
smtp_starttls_handler(tls_sec_cert, tls_sec_key, False))
cfg3_home = tempfile.mkdtemp(prefix="apex-cfg3-home-")
write_scope(cfg3_home, {"label": "cfg3", "expires": "2099-12-31T23:59:59Z",
"allow_private": True, "allow_ip": ["127.0.0.1"],
"allow_port": [str(smtp_tls_port),
str(smtp_plain_port)]})
res, _ = run([
call(1, "php_config_audit", root=php_root),
call(2, "php_config_audit", root=php_good_root),
call(3, "dovecot_audit", root=dv_root),
call(4, "dovecot_audit", root=dv_good_root),
call(5, "openvpn_audit", root=ov_root),
call(6, "openvpn_audit", root=ov_good_root),
call(7, "smtp_starttls_probe", target="127.0.0.1", port=smtp_tls_port),
call(8, "smtp_starttls_probe", target="127.0.0.1", port=smtp_plain_port),
call(9, "php_config_audit", root="/nonexistent-php"),
call(10, "dovecot_audit", root="/nonexistent-dv"),
call(11, "openvpn_audit", root="/nonexistent-ov"),
call(12, "smtp_starttls_probe", target="198.51.100.9", port=25),
], home=cfg3_home)
by_id = {r.get("id"): r for r in res}
php = json.loads(text(by_id[1]))
php_kinds = {f["id"] for f in php["findings"]}
check("php_config_audit flags the unsafe settings",
{"world-writable", "expose-php", "display-errors",
"allow-url-include", "allow-url-fopen", "no-disable-functions",
"insecure-cookie"} <= php_kinds, php)
php_good = json.loads(text(by_id[2]))
check("php_config_audit accepts a hardened php.ini",
php_good["findings_count"] == 0 and
php_good["expose_php"] is False, php_good)
dv = json.loads(text(by_id[3]))
dv_kinds = {f["id"] for f in dv["findings"]}
check("dovecot_audit flags TLS off and plaintext auth",
{"ssl-off", "plaintext-auth", "plain-mechanism"} <= dv_kinds, dv)
check("dovecot_audit flags a world-writable drop-in",
"world-writable" in dv_kinds and dv["files"] == 2, dv)
dv_good = json.loads(text(by_id[4]))
check("dovecot_audit accepts a hardened config",
dv_good["findings_count"] == 0 and dv_good["ssl_off"] is False,
dv_good)
ov = json.loads(text(by_id[5]))
ov_kinds = {f["id"] for f in ov["findings"]}
check("openvpn_audit flags tls-auth, cipher, HMAC and duplicate-cn",
{"no-tls-auth", "weak-cipher", "weak-auth", "duplicate-cn"} <= ov_kinds,
ov)
check("openvpn_audit flags a world-writable config",
"world-writable" in {f["id"] for f in ov["findings"]}, ov)
ov_good = json.loads(text(by_id[6]))
check("openvpn_audit accepts a hardened config",
ov_good["findings_count"] == 0 and ov_good["tls_auth"] is True,
ov_good)
st = json.loads(text(by_id[7]))
check("smtp_starttls_probe upgrades to TLS",
st["starttls_advertised"] is True and st["starttls_accepted"] is True and
st["tls_established"] is True and st["protocol"].startswith("TLSv1."),
st)
check("smtp_starttls_probe reports the negotiated cipher",
st["cipher"] != "", st)
st_no = json.loads(text(by_id[8]))
check("smtp_starttls_probe reports a server without STARTTLS",
st_no["starttls_advertised"] is False and
st_no["tls_established"] is False, st_no)
check("php_config_audit reports a missing php.ini", err(by_id[9]),
text(by_id[9]))
check("dovecot_audit reports a missing config", err(by_id[10]),
text(by_id[10]))
check("openvpn_audit reports a missing config", err(by_id[11]),
text(by_id[11]))
check("smtp_starttls_probe refuses an out-of-scope target", err(by_id[12]),
text(by_id[12]))
smtp_tls_srv.close()
smtp_plain_srv.close()
shutil.rmtree(php_root, ignore_errors=True)
shutil.rmtree(php_good_root, ignore_errors=True)
shutil.rmtree(dv_root, ignore_errors=True)
shutil.rmtree(dv_good_root, ignore_errors=True)
shutil.rmtree(ov_root, ignore_errors=True)
shutil.rmtree(ov_good_root, ignore_errors=True)
shutil.rmtree(tls_sec_dir, ignore_errors=True)
shutil.rmtree(cfg3_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"})