Red: - smb_shares: after the anonymous SMB 2.1 session, TREE_CONNECT for 14 common share names (IPC$, C$, ADMIN$, users, data, backup, public, www, home, share, files, tmp, downloads, media); reports reachable shares and NT status. No listing and no file access. - email_security_audit: gated dig for SPF, DMARC (_dmarc), DKIM (selector._domainkey) and MX; reports records, SPF policy, DMARC p= and gaps (missing records, +all, p=none). Blue: - www_audit: /var/www, /srv/www, /usr/share/nginx/html (root prefix): world-writable files and dirs, .env/wp-config/config.php/.htpasswd/id_rsa, backups, .git directories and directories without an index. The tool table hit its 128-slot cap at 130 tools; g_tools is now 192. Tests: SMB share fixture (allow/deny), UDP DNS TXT/MX agent, web-root tree. 706 checks, ASan suite green, analyze clean, release-check green including distcheck 1.21.0.
4214 lines
122 KiB
C
4214 lines
122 KiB
C
/*
|
|
* tools_red.c - the offensive side of the suite beyond plain reconnaissance.
|
|
*
|
|
* Three tools, all behind the same gate:
|
|
*
|
|
* vuln_check non-destructive verification of exposed artifacts
|
|
* (.git/HEAD, .env, directory listing, phpinfo) with the
|
|
* response saved as evidence
|
|
* subdomain_enum DNS brute force under a name the scope allows
|
|
* cred_test HTTP Basic default-credential check, rate-limited, stops
|
|
* on lockout, and refuses an endpoint that answers without
|
|
* authentication
|
|
*
|
|
* None of them exploits anything. A check either observes a publicly reachable
|
|
* artifact or proves that a default password is accepted; the operator decides
|
|
* what becomes a finding.
|
|
*/
|
|
#include <arpa/inet.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <netdb.h>
|
|
#include <netinet/in.h>
|
|
#include <poll.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/time.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#include "apex.h"
|
|
#include "tools.h"
|
|
|
|
/* ------------------------------------------------------------- utilities */
|
|
|
|
static void sleep_ms(long ms)
|
|
{
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = ms / 1000;
|
|
ts.tv_nsec = (ms % 1000) * 1000000L;
|
|
nanosleep(&ts, NULL);
|
|
}
|
|
|
|
static int find_bytes(const char *hay, size_t n, const char *needle)
|
|
{
|
|
size_t nl = strlen(needle);
|
|
size_t i;
|
|
|
|
if (nl == 0 || n < nl)
|
|
return 0;
|
|
for (i = 0; i + nl <= n; i++)
|
|
if (memcmp(hay + i, needle, nl) == 0)
|
|
return 1;
|
|
return 0;
|
|
}
|
|
|
|
/* ------------------------------------------------------------- vuln_check */
|
|
|
|
typedef struct {
|
|
const char *id;
|
|
const char *suffix; /* appended to the base path */
|
|
const char *severity; /* what a confirmed hit would be worth */
|
|
} vuln_mod;
|
|
|
|
static const vuln_mod VULN_MODS[] = {
|
|
{ "git", "/.git/HEAD", "medium" },
|
|
{ "env", "/.env", "high" },
|
|
{ "listing", "/", "low" },
|
|
{ "phpinfo", "/phpinfo.php", "low" },
|
|
};
|
|
|
|
/* A .env file is lines of KEY=value; HTML pages with "=" do not qualify. */
|
|
static int env_looks_like_env(const char *body, size_t len, char *keys,
|
|
size_t keys_n)
|
|
{
|
|
size_t i = 0;
|
|
int good = 0;
|
|
size_t used = 0;
|
|
|
|
keys[0] = '\0';
|
|
while (i < len) {
|
|
size_t start = i;
|
|
size_t eq;
|
|
size_t k;
|
|
|
|
while (i < len && body[i] != '\n')
|
|
i++;
|
|
eq = start;
|
|
while (eq < i && body[eq] != '=')
|
|
eq++;
|
|
if (eq < i && eq > start + 1) {
|
|
int ok = 1;
|
|
|
|
for (k = start; k < eq; k++) {
|
|
unsigned char ch = (unsigned char)body[k];
|
|
|
|
if (!((ch >= 'A' && ch <= 'Z') ||
|
|
(ch >= 'a' && ch <= 'z') ||
|
|
(ch >= '0' && ch <= '9') || ch == '_')) {
|
|
ok = 0;
|
|
break;
|
|
}
|
|
}
|
|
if (ok) {
|
|
good++;
|
|
if (used + (eq - start) + 2 < keys_n && good <= 8) {
|
|
size_t n = eq - start;
|
|
|
|
if (used > 0)
|
|
keys[used++] = ',';
|
|
memcpy(keys + used, body + start, n);
|
|
used += n;
|
|
keys[used] = '\0';
|
|
}
|
|
}
|
|
}
|
|
if (i < len)
|
|
i++;
|
|
}
|
|
return good >= 2;
|
|
}
|
|
|
|
static void t_vuln_check(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
const char *base = mcp_arg_string(args, "path");
|
|
const char *only = mcp_arg_string(args, "checks");
|
|
int port = mcp_arg_int(args, "port", 80);
|
|
int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port));
|
|
char root[512];
|
|
size_t rl;
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int confirmed = 0;
|
|
size_t i;
|
|
|
|
(void)user;
|
|
if (target == NULL || *target == '\0') {
|
|
mcp_reply_error(c, "target is required");
|
|
return;
|
|
}
|
|
snprintf(root, sizeof(root), "%s", base != NULL && *base != '\0'
|
|
? base : "/");
|
|
rl = strlen(root);
|
|
while (rl > 0 && root[rl - 1] == '/')
|
|
root[--rl] = '\0';
|
|
if (!gated(c, "vuln_check", 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,\"checks\":[",
|
|
port, tls ? "true" : "false");
|
|
for (i = 0; i < sizeof(VULN_MODS) / sizeof(VULN_MODS[0]); i++) {
|
|
const vuln_mod *m = &VULN_MODS[i];
|
|
char req[700];
|
|
char status[256];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
int code;
|
|
int hit = 0;
|
|
char *evidence = NULL;
|
|
char keys[256];
|
|
mcp_buf snippet;
|
|
|
|
if (only != NULL && *only != '\0' && strstr(only, m->id) == NULL)
|
|
continue;
|
|
snprintf(req, sizeof(req), "%s%s", root, m->suffix);
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, tls, "GET", req, NULL, NULL,
|
|
10000, &resp, &len, status, sizeof(status)) != 0) {
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"id\":");
|
|
apex_json_str(&b, m->id);
|
|
mcp_buf_puts(&b, ",\"path\":");
|
|
apex_json_str(&b, req);
|
|
mcp_buf_puts(&b, ",\"status\":0,\"verdict\":\"error\"}");
|
|
continue;
|
|
}
|
|
code = apex_status_code(status);
|
|
keys[0] = '\0';
|
|
if (code == 200) {
|
|
if (strcmp(m->id, "git") == 0)
|
|
hit = find_bytes(resp, len, "ref: refs/");
|
|
else if (strcmp(m->id, "env") == 0)
|
|
hit = env_looks_like_env(resp, len, keys, sizeof(keys));
|
|
else if (strcmp(m->id, "listing") == 0)
|
|
hit = find_bytes(resp, len, "Index of /") &&
|
|
(find_bytes(resp, len, "Parent Directory") ||
|
|
find_bytes(resp, len, "<h1>Index"));
|
|
else if (strcmp(m->id, "phpinfo") == 0)
|
|
hit = find_bytes(resp, len, "PHP Version") ||
|
|
find_bytes(resp, len, "phpinfo()");
|
|
}
|
|
if (hit) {
|
|
char label[48];
|
|
|
|
snprintf(label, sizeof(label), "vuln-%s", m->id);
|
|
evidence = apex_evidence_save(label, resp, len);
|
|
confirmed++;
|
|
}
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"id\":");
|
|
apex_json_str(&b, m->id);
|
|
mcp_buf_puts(&b, ",\"path\":");
|
|
apex_json_str(&b, req);
|
|
mcp_buf_printf(&b, ",\"status\":%d,\"verdict\":\"%s\",\"severity\":",
|
|
code, hit ? "confirmed" : "not-found");
|
|
apex_json_str(&b, m->severity);
|
|
mcp_buf_puts(&b, ",\"evidence\":");
|
|
if (evidence != NULL)
|
|
apex_json_str(&b, evidence);
|
|
else
|
|
mcp_buf_puts(&b, "null");
|
|
if (hit && strcmp(m->id, "env") == 0) {
|
|
mcp_buf_puts(&b, ",\"keys\":");
|
|
apex_json_str(&b, keys);
|
|
} else if (hit) {
|
|
size_t n = len < 160 ? len : 160;
|
|
size_t k;
|
|
|
|
mcp_buf_init(&snippet);
|
|
for (k = 0; k < n; k++) {
|
|
char ch = resp[k];
|
|
|
|
if (ch == '\r' || ch == '\n' || ch == '\t')
|
|
ch = ' ';
|
|
if ((unsigned char)ch < 0x20)
|
|
ch = '.';
|
|
mcp_buf_add(&snippet, &ch, 1);
|
|
}
|
|
mcp_buf_puts(&b, ",\"snippet\":");
|
|
apex_json_str(&b, snippet.p != NULL ? snippet.p : "");
|
|
mcp_buf_free(&snippet);
|
|
}
|
|
mcp_buf_puts(&b, "}");
|
|
free(evidence);
|
|
free(resp);
|
|
}
|
|
mcp_buf_printf(&b, "],\"confirmed\":%d}", confirmed);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ---------------------------------------------------------- subdomain_enum */
|
|
|
|
static const char *SUBDOMAINS[] = {
|
|
"www", "mail", "ftp", "webmail", "smtp", "pop", "imap", "ns1", "ns2",
|
|
"dev", "test", "staging", "stage", "api", "admin", "portal", "vpn",
|
|
"git", "jenkins", "grafana", "jira", "confluence", "wiki", "docs",
|
|
"blog", "shop", "store", "db", "mysql", "postgres", "redis", "mongo",
|
|
"backup", "cdn", "static", "assets", "img", "m", "mobile", "app",
|
|
"beta", "demo", "old", "new", "internal", "intranet", "ldap", "sso",
|
|
"auth", "status", "monitor", "metrics", "prometheus", "kibana",
|
|
"vpn2", "remote", "gateway", "proxy", NULL
|
|
};
|
|
|
|
static int resolve_host(const char *host, char out[4][64], int *count)
|
|
{
|
|
struct addrinfo hints;
|
|
struct addrinfo *res = NULL;
|
|
struct addrinfo *p;
|
|
int n = 0;
|
|
int i;
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
hints.ai_family = AF_UNSPEC;
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
if (getaddrinfo(host, NULL, &hints, &res) != 0)
|
|
return -1;
|
|
for (p = res; p != NULL && n < 4; p = p->ai_next) {
|
|
char text[64];
|
|
int dup = 0;
|
|
|
|
if (p->ai_family == AF_INET)
|
|
inet_ntop(AF_INET, &((struct sockaddr_in *)p->ai_addr)->sin_addr,
|
|
text, sizeof(text));
|
|
else if (p->ai_family == AF_INET6)
|
|
inet_ntop(AF_INET6, &((struct sockaddr_in6 *)p->ai_addr)->sin6_addr,
|
|
text, sizeof(text));
|
|
else
|
|
continue;
|
|
for (i = 0; i < n; i++)
|
|
if (strcmp(out[i], text) == 0)
|
|
dup = 1;
|
|
if (!dup) {
|
|
snprintf(out[n], 64, "%s", text);
|
|
n++;
|
|
}
|
|
}
|
|
freeaddrinfo(res);
|
|
*count = n;
|
|
return n > 0 ? 0 : -1;
|
|
}
|
|
|
|
static void t_subdomain_enum(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *domain = mcp_arg_string(args, "domain");
|
|
const char *names_arg = mcp_arg_string(args, "names");
|
|
int limit = mcp_arg_int(args, "limit", 50);
|
|
char reason[512];
|
|
char *names_copy = NULL;
|
|
char *names_save = NULL;
|
|
char *next_name = NULL;
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int checked = 0;
|
|
int skipped = 0;
|
|
int found = 0;
|
|
int emitted = 0;
|
|
int i = 0;
|
|
|
|
(void)user;
|
|
if (domain == NULL || *domain == '\0') {
|
|
mcp_reply_error(c, "domain is required");
|
|
return;
|
|
}
|
|
if (limit <= 0 || limit > 200)
|
|
limit = 50;
|
|
if (!gated(c, "subdomain_enum", domain, 0))
|
|
return;
|
|
if (names_arg != NULL && *names_arg != '\0') {
|
|
names_copy = strdup(names_arg);
|
|
if (names_copy != NULL)
|
|
next_name = strtok_r(names_copy, ",", &names_save);
|
|
}
|
|
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"domain\":");
|
|
apex_json_str(&b, domain);
|
|
mcp_buf_puts(&b, ",\"found\":[");
|
|
for (;;) {
|
|
char full[300];
|
|
const char *name;
|
|
char addrs[4][64];
|
|
int naddr = 0;
|
|
|
|
if (names_copy != NULL) {
|
|
name = next_name;
|
|
if (name == NULL)
|
|
break;
|
|
next_name = strtok_r(NULL, ",", &names_save);
|
|
} else {
|
|
if (SUBDOMAINS[i] == NULL || i >= limit)
|
|
break;
|
|
name = SUBDOMAINS[i++];
|
|
}
|
|
while (*name == ' ' || *name == '\t')
|
|
name++;
|
|
if (*name == '\0')
|
|
continue;
|
|
if (strchr(name, '.') != NULL)
|
|
snprintf(full, sizeof(full), "%s", name);
|
|
else
|
|
snprintf(full, sizeof(full), "%s.%s", name, domain);
|
|
checked++;
|
|
if (!apex_scope_check(full, 0, reason, sizeof(reason))) {
|
|
skipped++;
|
|
continue;
|
|
}
|
|
if (resolve_host(full, addrs, &naddr) == 0) {
|
|
int k;
|
|
|
|
found++;
|
|
if (emitted >= 50)
|
|
continue;
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
emitted++;
|
|
mcp_buf_puts(&b, "{\"name\":");
|
|
apex_json_str(&b, full);
|
|
mcp_buf_puts(&b, ",\"addresses\":[");
|
|
for (k = 0; k < naddr; k++) {
|
|
if (k > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
apex_json_str(&b, addrs[k]);
|
|
}
|
|
mcp_buf_puts(&b, "]}");
|
|
}
|
|
if (checked >= 200)
|
|
break;
|
|
}
|
|
free(names_copy);
|
|
mcp_buf_printf(&b, "],\"checked\":%d,\"skipped\":%d,\"count\":%d}",
|
|
checked, skipped, found);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- cred_test */
|
|
|
|
static int split_list(char *text, char **out, int max)
|
|
{
|
|
int n = 0;
|
|
char *save = NULL;
|
|
char *tok = strtok_r(text, ",", &save);
|
|
|
|
while (tok != NULL && n < max) {
|
|
while (*tok == ' ' || *tok == '\t')
|
|
tok++;
|
|
if (*tok != '\0')
|
|
out[n++] = tok;
|
|
tok = strtok_r(NULL, ",", &save);
|
|
}
|
|
return n;
|
|
}
|
|
|
|
static void t_cred_test(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
const char *path = mcp_arg_string(args, "path");
|
|
const char *users_arg = mcp_arg_string(args, "users");
|
|
const char *pass_arg = mcp_arg_string(args, "passwords");
|
|
int port = mcp_arg_int(args, "port", 80);
|
|
int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port));
|
|
int max_attempts = mcp_arg_int(args, "max_attempts", 20);
|
|
char status[256];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
int code;
|
|
int found = 0;
|
|
int lockout = 0;
|
|
int attempts = 0;
|
|
char userbuf[256];
|
|
char passbuf[512];
|
|
char *users[12];
|
|
char *passes[12];
|
|
int nusers;
|
|
int npasses;
|
|
int u;
|
|
int p;
|
|
char found_user[64];
|
|
char found_pass[128];
|
|
mcp_buf b;
|
|
|
|
(void)user;
|
|
if (target == NULL || *target == '\0') {
|
|
mcp_reply_error(c, "target is required");
|
|
return;
|
|
}
|
|
if (path == NULL || *path == '\0')
|
|
path = "/";
|
|
if (path[0] != '/') {
|
|
mcp_reply_error(c, "path must start with /");
|
|
return;
|
|
}
|
|
if (max_attempts <= 0 || max_attempts > 40)
|
|
max_attempts = 20;
|
|
found_user[0] = '\0';
|
|
found_pass[0] = '\0';
|
|
if (!gated(c, "cred_test", target, port))
|
|
return;
|
|
|
|
/* A path that answers without credentials is not a login: there is
|
|
* nothing to test, and calling the first pair a success would be a lie. */
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, tls, "GET", path, NULL, NULL, 10000,
|
|
&resp, &len, status, sizeof(status)) == 0) {
|
|
code = apex_status_code(status);
|
|
free(resp);
|
|
resp = NULL;
|
|
if (code != 401 && code != 403) {
|
|
mcp_buf_init(&b);
|
|
mcp_buf_printf(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"error\":\"path answered %d "
|
|
"without authentication; nothing to test\","
|
|
"\"attempts\":0,\"found\":false,\"lockout\":false}",
|
|
port, code);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
return;
|
|
}
|
|
}
|
|
|
|
snprintf(userbuf, sizeof(userbuf), "%s",
|
|
users_arg != NULL && *users_arg != '\0' ? users_arg
|
|
: "admin,root,test");
|
|
snprintf(passbuf, sizeof(passbuf), "%s",
|
|
pass_arg != NULL && *pass_arg != '\0' ? pass_arg
|
|
: "admin,password,admin123,"
|
|
"root,toor,test");
|
|
nusers = split_list(userbuf, users, 12);
|
|
npasses = split_list(passbuf, passes, 12);
|
|
|
|
for (u = 0; u < nusers && !found && !lockout; u++) {
|
|
for (p = 0; p < npasses && !found && !lockout; p++) {
|
|
char pair[256];
|
|
char header[512];
|
|
char *b64;
|
|
|
|
if (attempts >= max_attempts)
|
|
break;
|
|
snprintf(pair, sizeof(pair), "%s:%s", users[u], passes[p]);
|
|
b64 = apex_base64_alloc(pair, strlen(pair));
|
|
if (b64 == NULL)
|
|
continue;
|
|
snprintf(header, sizeof(header),
|
|
"Authorization: Basic %s\r\n", b64);
|
|
free(b64);
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, tls, "GET", path, NULL, header,
|
|
10000, &resp, &len, status,
|
|
sizeof(status)) == 0) {
|
|
code = apex_status_code(status);
|
|
attempts++;
|
|
if (code == 429) {
|
|
lockout = 1;
|
|
} else if (code == 200 || code == 201 || code == 301 ||
|
|
code == 302) {
|
|
found = 1;
|
|
snprintf(found_user, sizeof(found_user), "%s",
|
|
users[u]);
|
|
snprintf(found_pass, sizeof(found_pass), "%s",
|
|
passes[p]);
|
|
}
|
|
free(resp);
|
|
resp = NULL;
|
|
}
|
|
if (!found && !lockout)
|
|
sleep_ms(200);
|
|
}
|
|
if (attempts >= max_attempts)
|
|
break;
|
|
}
|
|
apex_audit("cred_test", target, port, found,
|
|
found ? "default credentials accepted" : "no default credentials",
|
|
"HTTP Basic");
|
|
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"path\":", port);
|
|
apex_json_str(&b, path);
|
|
mcp_buf_printf(&b, ",\"attempts\":%d,\"found\":%s,\"lockout\":%s",
|
|
attempts, found ? "true" : "false",
|
|
lockout ? "true" : "false");
|
|
mcp_buf_puts(&b, ",\"username\":");
|
|
apex_json_str(&b, found_user);
|
|
mcp_buf_puts(&b, ",\"password\":");
|
|
apex_json_str(&b, found_pass);
|
|
mcp_buf_puts(&b, "}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ---------------------------------------------------------- vhost_discovery */
|
|
|
|
static const char *VHOSTS[] = {
|
|
"www", "mail", "admin", "api", "dev", "staging", "test", "blog", "shop",
|
|
"app", "portal", "vpn", "git", "jenkins", "grafana", "jira", "wiki",
|
|
"docs", "cdn", "static", "m", "mobile", "intranet", "internal", "sso",
|
|
"auth", "db", "backup", "old", "new", "demo", "beta", "status", "monitor",
|
|
NULL
|
|
};
|
|
|
|
static void title_of(const char *body, size_t len, char *out, size_t n)
|
|
{
|
|
size_t i;
|
|
|
|
out[0] = '\0';
|
|
for (i = 0; i + 6 < len; i++) {
|
|
if (strncasecmp(body + i, "<title", 6) == 0) {
|
|
size_t j = i + 6;
|
|
size_t k = 0;
|
|
|
|
while (j < len && body[j] != '>')
|
|
j++;
|
|
j++;
|
|
while (j < len && body[j] != '<' && k + 1 < n) {
|
|
char ch = body[j++];
|
|
|
|
if (ch != '\r' && ch != '\n' && ch != '\t')
|
|
out[k++] = ch;
|
|
}
|
|
out[k] = '\0';
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
static int read_all_fd(int fd, mcp_buf *b, size_t cap)
|
|
{
|
|
char buf[8192];
|
|
|
|
for (;;) {
|
|
ssize_t n = read(fd, buf, sizeof(buf));
|
|
|
|
if (n <= 0)
|
|
break;
|
|
if (b->len + (size_t)n > cap) {
|
|
mcp_buf_add(b, buf, cap - b->len);
|
|
break;
|
|
}
|
|
mcp_buf_add(b, buf, (size_t)n);
|
|
}
|
|
return b->len > 0 ? 0 : -1;
|
|
}
|
|
|
|
/* GET with an arbitrary Host header; connects to ip, not to the vhost name. */
|
|
static int http_with_host(const char *ip, int port, const char *host_hdr,
|
|
const char *path, char **out, size_t *out_len,
|
|
char *status, size_t sn)
|
|
{
|
|
int fd = tcp_connect_host(ip, port, 5000);
|
|
mcp_buf req;
|
|
mcp_buf resp;
|
|
struct timeval tv;
|
|
int rc;
|
|
|
|
if (fd < 0)
|
|
return -1;
|
|
tv.tv_sec = 8;
|
|
tv.tv_usec = 0;
|
|
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
|
mcp_buf_init(&req);
|
|
mcp_buf_printf(&req, "GET %s HTTP/1.1\r\nHost: %s\r\n", path, host_hdr);
|
|
mcp_buf_printf(&req, "User-Agent: apex-twin/%s\r\n", APEX_VERSION);
|
|
mcp_buf_puts(&req, "Accept: */*\r\nConnection: close\r\n\r\n");
|
|
if (write(fd, req.p, req.len) != (ssize_t)req.len) {
|
|
mcp_buf_free(&req);
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
mcp_buf_free(&req);
|
|
mcp_buf_init(&resp);
|
|
rc = read_all_fd(fd, &resp, 1u << 20);
|
|
close(fd);
|
|
if (rc != 0) {
|
|
mcp_buf_free(&resp);
|
|
return -1;
|
|
}
|
|
if (status != NULL && sn > 0) {
|
|
size_t k = 0;
|
|
|
|
while (resp.p[k] != '\0' && resp.p[k] != '\r' && k + 1 < sn) {
|
|
status[k] = resp.p[k];
|
|
k++;
|
|
}
|
|
status[k] = '\0';
|
|
}
|
|
*out = resp.p;
|
|
*out_len = resp.len;
|
|
resp.p = NULL;
|
|
mcp_buf_free(&resp);
|
|
return 0;
|
|
}
|
|
|
|
static void t_vhost_discovery(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
const char *base_host = mcp_arg_string(args, "base_host");
|
|
const char *names_arg = mcp_arg_string(args, "names");
|
|
const char *path = mcp_arg_string(args, "path");
|
|
int limit = mcp_arg_int(args, "limit", 40);
|
|
int delay = mcp_arg_int(args, "delay_ms", 100);
|
|
int port = mcp_arg_int(args, "port", 80);
|
|
char *names_copy = NULL;
|
|
char *names_save = NULL;
|
|
char *next_name = NULL;
|
|
char status[256];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
int base_status;
|
|
long base_len;
|
|
char base_title[128];
|
|
int checked = 0;
|
|
int hits = 0;
|
|
int first = 1;
|
|
int i = 0;
|
|
mcp_buf b;
|
|
|
|
(void)user;
|
|
if (target == NULL || *target == '\0') {
|
|
mcp_reply_error(c, "target is required");
|
|
return;
|
|
}
|
|
if (path == NULL || *path == '\0')
|
|
path = "/";
|
|
if (limit <= 0 || limit > 100)
|
|
limit = 40;
|
|
if (delay < 0 || delay > 1000)
|
|
delay = 100;
|
|
if (!gated(c, "vhost_discovery", target, port))
|
|
return;
|
|
if (base_host == NULL || *base_host == '\0')
|
|
base_host = target;
|
|
|
|
status[0] = '\0';
|
|
if (http_with_host(target, port, base_host, path, &resp, &len, status,
|
|
sizeof(status)) != 0) {
|
|
mcp_reply_error(c, "no baseline response");
|
|
return;
|
|
}
|
|
base_status = apex_status_code(status);
|
|
base_len = (long)len;
|
|
title_of(resp, len, base_title, sizeof(base_title));
|
|
free(resp);
|
|
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"base_host\":", port);
|
|
apex_json_str(&b, base_host);
|
|
mcp_buf_printf(&b, ",\"baseline\":{\"status\":%d,\"bytes\":%ld,\"title\":",
|
|
base_status, base_len);
|
|
apex_json_str(&b, base_title);
|
|
mcp_buf_puts(&b, "},\"hits\":[");
|
|
|
|
if (names_arg != NULL && *names_arg != '\0') {
|
|
names_copy = strdup(names_arg);
|
|
if (names_copy != NULL)
|
|
next_name = strtok_r(names_copy, ",", &names_save);
|
|
}
|
|
for (;;) {
|
|
const char *name;
|
|
char host[300];
|
|
char reason[160];
|
|
int differs = 0;
|
|
int code;
|
|
long blen;
|
|
char title[128];
|
|
char *evidence = NULL;
|
|
|
|
if (names_copy != NULL) {
|
|
name = next_name;
|
|
if (name == NULL)
|
|
break;
|
|
next_name = strtok_r(NULL, ",", &names_save);
|
|
} else {
|
|
if (VHOSTS[i] == NULL || i >= limit)
|
|
break;
|
|
name = VHOSTS[i++];
|
|
}
|
|
while (*name == ' ' || *name == '\t')
|
|
name++;
|
|
if (*name == '\0')
|
|
continue;
|
|
if (strchr(name, '.') != NULL)
|
|
snprintf(host, sizeof(host), "%s", name);
|
|
else
|
|
snprintf(host, sizeof(host), "%s.%s", name, base_host);
|
|
checked++;
|
|
status[0] = '\0';
|
|
if (http_with_host(target, port, host, path, &resp, &len, status,
|
|
sizeof(status)) != 0)
|
|
continue;
|
|
code = apex_status_code(status);
|
|
blen = (long)len;
|
|
title_of(resp, len, title, sizeof(title));
|
|
if (code != base_status) {
|
|
snprintf(reason, sizeof(reason), "status %d vs %d", code,
|
|
base_status);
|
|
differs = 1;
|
|
} else if (blen > base_len + 24 || blen < base_len - 24) {
|
|
snprintf(reason, sizeof(reason), "bytes %ld vs %ld", blen,
|
|
base_len);
|
|
differs = 1;
|
|
} else if (strcmp(title, base_title) != 0) {
|
|
snprintf(reason, sizeof(reason), "title differs");
|
|
differs = 1;
|
|
} else {
|
|
reason[0] = '\0';
|
|
}
|
|
if (differs) {
|
|
char label[64];
|
|
|
|
snprintf(label, sizeof(label), "vhost-%.40s", host);
|
|
evidence = apex_evidence_save(label, resp, len);
|
|
hits++;
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"host\":");
|
|
apex_json_str(&b, host);
|
|
mcp_buf_printf(&b, ",\"status\":%d,\"bytes\":%ld,\"title\":",
|
|
code, blen);
|
|
apex_json_str(&b, title);
|
|
mcp_buf_puts(&b, ",\"reason\":");
|
|
apex_json_str(&b, reason);
|
|
mcp_buf_puts(&b, ",\"evidence\":");
|
|
if (evidence != NULL)
|
|
apex_json_str(&b, evidence);
|
|
else
|
|
mcp_buf_puts(&b, "null");
|
|
mcp_buf_puts(&b, "}");
|
|
free(evidence);
|
|
}
|
|
free(resp);
|
|
resp = NULL;
|
|
if (delay > 0)
|
|
sleep_ms(delay);
|
|
}
|
|
free(names_copy);
|
|
mcp_buf_printf(&b, "],\"checked\":%d,\"count\":%d}", checked, hits);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- http_brute */
|
|
|
|
static const char *BRUTE_PATHS[] = {
|
|
"admin", "administrator", "login", "wp-admin", "wp-login.php", ".git/HEAD",
|
|
".env", "backup.zip", "backup.sql", "db.sql", "config.php", "config.json",
|
|
"robots.txt", "sitemap.xml", "api", "api/v1", "api/v2", "swagger.json",
|
|
"swagger-ui/", "openapi.json", "graphql", "health", "healthz", "metrics",
|
|
"debug", "console", "actuator", "actuator/env", "server-status",
|
|
"server-info", "phpinfo.php", "info.php", "test", "dev", "staging", "old",
|
|
"tmp", "uploads/", "backup/", "jenkins", "grafana", "kibana", "prometheus",
|
|
".well-known/security.txt", "favicon.ico", "index.php~", "web.config",
|
|
".svn/entries", NULL
|
|
};
|
|
|
|
static int brute_hit(int code)
|
|
{
|
|
return code == 200 || code == 201 || code == 204 || code == 301 ||
|
|
code == 302 || code == 307 || code == 308 || code == 401 ||
|
|
code == 403;
|
|
}
|
|
|
|
static void t_http_brute(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
const char *base = mcp_arg_string(args, "path");
|
|
const char *words_arg = mcp_arg_string(args, "words");
|
|
const char *wordlist = mcp_arg_string(args, "wordlist");
|
|
int port = mcp_arg_int(args, "port", 80);
|
|
int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port));
|
|
int limit = mcp_arg_int(args, "limit", 40);
|
|
int delay = mcp_arg_int(args, "delay_ms", 100);
|
|
char root[512];
|
|
size_t rl;
|
|
char *list[256];
|
|
int nlist = 0;
|
|
char *file_words = NULL;
|
|
mcp_buf b;
|
|
int tested = 0;
|
|
int hits = 0;
|
|
int evidence_saved = 0;
|
|
int first = 1;
|
|
int i;
|
|
|
|
(void)user;
|
|
if (target == NULL || *target == '\0') {
|
|
mcp_reply_error(c, "target is required");
|
|
return;
|
|
}
|
|
if (limit <= 0 || limit > 200)
|
|
limit = 40;
|
|
if (delay < 0 || delay > 1000)
|
|
delay = 100;
|
|
snprintf(root, sizeof(root), "%s", base != NULL && *base != '\0'
|
|
? base : "/");
|
|
rl = strlen(root);
|
|
while (rl > 0 && root[rl - 1] == '/')
|
|
root[--rl] = '\0';
|
|
if (!gated(c, "http_brute", target, port))
|
|
return;
|
|
|
|
if (words_arg != NULL && *words_arg != '\0') {
|
|
char *copy = strdup(words_arg);
|
|
char *save = NULL;
|
|
char *tok;
|
|
|
|
if (copy != NULL) {
|
|
tok = strtok_r(copy, ",", &save);
|
|
while (tok != NULL && nlist < 256) {
|
|
while (*tok == ' ' || *tok == '\t')
|
|
tok++;
|
|
if (*tok != '\0')
|
|
list[nlist++] = tok;
|
|
tok = strtok_r(NULL, ",", &save);
|
|
}
|
|
/* keep copy alive: list points into it */
|
|
file_words = copy;
|
|
}
|
|
} else if (wordlist != NULL && *wordlist != '\0') {
|
|
FILE *f = fopen(wordlist, "rb");
|
|
char line[512];
|
|
|
|
if (f == NULL) {
|
|
mcp_reply_error(c, "cannot read the wordlist");
|
|
return;
|
|
}
|
|
while (fgets(line, sizeof(line), f) != NULL && nlist < 256) {
|
|
char *p = line;
|
|
|
|
while (*p == ' ' || *p == '\t')
|
|
p++;
|
|
{
|
|
size_t n = strlen(p);
|
|
|
|
while (n > 0 && (p[n - 1] == '\n' || p[n - 1] == '\r'))
|
|
p[--n] = '\0';
|
|
}
|
|
if (*p == '\0' || *p == '#')
|
|
continue;
|
|
list[nlist] = strdup(p);
|
|
if (list[nlist] != NULL)
|
|
nlist++;
|
|
}
|
|
fclose(f);
|
|
} else {
|
|
for (i = 0; BRUTE_PATHS[i] != NULL && nlist < 256; i++)
|
|
list[nlist++] = (char *)BRUTE_PATHS[i];
|
|
}
|
|
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"hits\":[", port);
|
|
for (i = 0; i < nlist && tested < limit; i++) {
|
|
char req[900];
|
|
const char *word = list[i];
|
|
char status[256];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
int code;
|
|
|
|
while (*word == '/')
|
|
word++;
|
|
if (*word == '\0')
|
|
continue;
|
|
if (root[0] != '\0')
|
|
snprintf(req, sizeof(req), "%s/%s", root, word);
|
|
else
|
|
snprintf(req, sizeof(req), "/%s", word);
|
|
status[0] = '\0';
|
|
tested++;
|
|
if (apex_http_once(target, port, tls, "GET", req, NULL, NULL, 8000,
|
|
&resp, &len, status, sizeof(status)) != 0)
|
|
continue;
|
|
code = apex_status_code(status);
|
|
if (brute_hit(code)) {
|
|
hits++;
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"path\":");
|
|
apex_json_str(&b, req);
|
|
mcp_buf_printf(&b, ",\"status\":%d,\"bytes\":%lu", code,
|
|
(unsigned long)len);
|
|
if ((code == 200 || code == 201) && evidence_saved < 5) {
|
|
char *ev = apex_evidence_save("brute", resp, len);
|
|
|
|
evidence_saved++;
|
|
mcp_buf_puts(&b, ",\"evidence\":");
|
|
if (ev != NULL) {
|
|
apex_json_str(&b, ev);
|
|
free(ev);
|
|
} else {
|
|
mcp_buf_puts(&b, "null");
|
|
}
|
|
}
|
|
mcp_buf_puts(&b, "}");
|
|
}
|
|
free(resp);
|
|
if (delay > 0 && tested < limit)
|
|
sleep_ms(delay);
|
|
}
|
|
if (words_arg != NULL && *words_arg != '=' && file_words != NULL)
|
|
free(file_words);
|
|
if (wordlist != NULL && *wordlist != '\0') {
|
|
int k;
|
|
|
|
for (k = 0; k < nlist; k++)
|
|
free(list[k]);
|
|
}
|
|
mcp_buf_printf(&b, "],\"tested\":%d,\"count\":%d}", tested, hits);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- ssrf_probe */
|
|
|
|
static void url_enc(mcp_buf *b, const char *s)
|
|
{
|
|
static const char hex[] = "0123456789ABCDEF";
|
|
size_t i;
|
|
|
|
for (i = 0; s[i] != '\0'; i++) {
|
|
unsigned char c = (unsigned char)s[i];
|
|
|
|
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
|
|
(c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.' ||
|
|
c == '~') {
|
|
mcp_buf_add(b, (const char *)&c, 1);
|
|
} else {
|
|
char out[3] = { '%', hex[c >> 4], hex[c & 15] };
|
|
|
|
mcp_buf_add(b, out, 3);
|
|
}
|
|
}
|
|
}
|
|
|
|
static int cb_open(int *port)
|
|
{
|
|
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
struct sockaddr_in sa;
|
|
socklen_t sl = sizeof(sa);
|
|
int one = 1;
|
|
|
|
if (fd < 0)
|
|
return -1;
|
|
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
|
|
memset(&sa, 0, sizeof(sa));
|
|
sa.sin_family = AF_INET;
|
|
sa.sin_port = 0;
|
|
sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) != 0 ||
|
|
listen(fd, 8) != 0 ||
|
|
getsockname(fd, (struct sockaddr *)&sa, &sl) != 0) {
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
*port = ntohs(sa.sin_port);
|
|
return fd;
|
|
}
|
|
|
|
static int cb_wait(int ls, int wait_ms, char *req, size_t n)
|
|
{
|
|
struct pollfd p;
|
|
int c;
|
|
ssize_t r;
|
|
|
|
p.fd = ls;
|
|
p.events = POLLIN;
|
|
p.revents = 0;
|
|
if (poll(&p, 1, wait_ms) <= 0)
|
|
return 0;
|
|
c = accept(ls, NULL, NULL);
|
|
if (c < 0)
|
|
return 0;
|
|
r = read(c, req, n - 1);
|
|
close(c);
|
|
if (r <= 0)
|
|
return 0;
|
|
req[r] = '\0';
|
|
return 1;
|
|
}
|
|
|
|
static void t_ssrf_probe(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
const char *path = mcp_arg_string(args, "path");
|
|
const char *param = mcp_arg_string(args, "param");
|
|
const char *method = mcp_arg_string(args, "method");
|
|
const char *callback = mcp_arg_string(args, "callback_base");
|
|
int port = mcp_arg_int(args, "port", 80);
|
|
int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port));
|
|
int wait_ms = mcp_arg_int(args, "wait_ms", 1200);
|
|
int cbport = 0;
|
|
int ls = -1;
|
|
int is_post;
|
|
char token[64];
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int confirmed = 0;
|
|
int verified;
|
|
const char *hosts[3] = { "127.0.0.1", "localhost", "127.1" };
|
|
int v;
|
|
|
|
(void)user;
|
|
if (target == NULL || *target == '\0' || param == NULL || *param == '\0') {
|
|
mcp_reply_error(c, "target and param are required");
|
|
return;
|
|
}
|
|
if (path == NULL || *path == '\0')
|
|
path = "/";
|
|
if (path[0] != '/') {
|
|
mcp_reply_error(c, "path must start with /");
|
|
return;
|
|
}
|
|
is_post = method != NULL && strcasecmp(method, "POST") == 0;
|
|
if (wait_ms < 200 || wait_ms > 5000)
|
|
wait_ms = 1200;
|
|
if (!gated(c, "ssrf_probe", target, port))
|
|
return;
|
|
|
|
verified = callback == NULL || *callback == '\0';
|
|
if (verified) {
|
|
ls = cb_open(&cbport);
|
|
if (ls < 0) {
|
|
mcp_reply_error(c, "cannot open the callback listener");
|
|
return;
|
|
}
|
|
}
|
|
snprintf(token, sizeof(token), "apex%ld", (long)getpid());
|
|
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"param\":", port);
|
|
apex_json_str(&b, param);
|
|
mcp_buf_printf(&b, ",\"method\":\"%s\",\"verified\":%s",
|
|
is_post ? "POST" : "GET", verified ? "true" : "false");
|
|
if (verified)
|
|
mcp_buf_printf(&b, ",\"callback_port\":%d", cbport);
|
|
mcp_buf_puts(&b, ",\"callbacks\":[");
|
|
for (v = 0; v < 3 && (verified ? 1 : v < 1); v++) {
|
|
char url[600];
|
|
char inj[900];
|
|
char status[256];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
const char *base = callback;
|
|
char hostpart[300];
|
|
char tok[96];
|
|
|
|
snprintf(tok, sizeof(tok), "%s-%d-%d", token, (int)getpid(), v);
|
|
if (verified) {
|
|
snprintf(hostpart, sizeof(hostpart), "http://%s:%d", hosts[v],
|
|
cbport);
|
|
base = hostpart;
|
|
}
|
|
snprintf(url, sizeof(url), "%s/%s", base, tok);
|
|
{
|
|
mcp_buf enc;
|
|
mcp_buf target_buf;
|
|
|
|
mcp_buf_init(&enc);
|
|
url_enc(&enc, url);
|
|
mcp_buf_init(&target_buf);
|
|
if (is_post) {
|
|
mcp_buf_printf(&target_buf, "%s=%s", param,
|
|
enc.p != NULL ? enc.p : "");
|
|
snprintf(inj, sizeof(inj), "%s", target_buf.p);
|
|
} else {
|
|
const char *sep = strchr(path, '?') != NULL ? "&" : "?";
|
|
|
|
snprintf(inj, sizeof(inj), "%s%s%s=%s", path, sep, param,
|
|
enc.p != NULL ? enc.p : "");
|
|
}
|
|
mcp_buf_free(&enc);
|
|
mcp_buf_free(&target_buf);
|
|
}
|
|
status[0] = '\0';
|
|
(void)apex_http_once(target, port, tls, is_post ? "POST" : "GET",
|
|
is_post ? path : inj,
|
|
is_post ? inj : NULL, NULL, 10000, &resp, &len,
|
|
status, sizeof(status));
|
|
free(resp);
|
|
if (verified) {
|
|
char req[1024];
|
|
|
|
if (cb_wait(ls, wait_ms, req, sizeof(req))) {
|
|
if (strstr(req, tok) != NULL) {
|
|
char *ev = apex_evidence_save("ssrf-callback", req,
|
|
strlen(req));
|
|
size_t k;
|
|
size_t n = strlen(req);
|
|
|
|
for (k = 0; k < n; k++)
|
|
if (req[k] == '\r' || req[k] == '\n')
|
|
req[k] = ' ';
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
confirmed = 1;
|
|
mcp_buf_puts(&b, "{\"url\":");
|
|
apex_json_str(&b, url);
|
|
mcp_buf_puts(&b, ",\"request\":");
|
|
apex_json_str(&b, req);
|
|
mcp_buf_puts(&b, ",\"evidence\":");
|
|
if (ev != NULL)
|
|
apex_json_str(&b, ev);
|
|
else
|
|
mcp_buf_puts(&b, "null");
|
|
mcp_buf_puts(&b, "}");
|
|
free(ev);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
mcp_buf_printf(&b, "],\"confirmed\":%s}", confirmed ? "true" : "false");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
if (ls >= 0)
|
|
close(ls);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- jwt_audit */
|
|
|
|
static int b64url_val(int c)
|
|
{
|
|
if (c >= 'A' && c <= 'Z')
|
|
return c - 'A';
|
|
if (c >= 'a' && c <= 'z')
|
|
return c - 'a' + 26;
|
|
if (c >= '0' && c <= '9')
|
|
return c - '0' + 52;
|
|
if (c == '-')
|
|
return 62;
|
|
if (c == '_')
|
|
return 63;
|
|
return -1;
|
|
}
|
|
|
|
static int b64url_decode(const char *s, unsigned char *out, size_t max)
|
|
{
|
|
unsigned long acc = 0;
|
|
int bits = 0;
|
|
size_t w = 0;
|
|
|
|
for (; *s != '\0'; s++) {
|
|
int v;
|
|
|
|
if (*s == '=')
|
|
break;
|
|
v = b64url_val((unsigned char)*s);
|
|
if (v < 0)
|
|
return -1;
|
|
acc = (acc << 6) | (unsigned long)v;
|
|
bits += 6;
|
|
if (bits >= 8) {
|
|
bits -= 8;
|
|
if (w >= max)
|
|
return -1;
|
|
out[w++] = (unsigned char)((acc >> bits) & 0xff);
|
|
}
|
|
}
|
|
return (int)w;
|
|
}
|
|
|
|
static char *b64url_json(const char *seg)
|
|
{
|
|
char *text = calloc(1, 65537);
|
|
int n;
|
|
|
|
if (text == NULL)
|
|
return NULL;
|
|
n = b64url_decode(seg, (unsigned char *)text, 65536);
|
|
if (n < 0) {
|
|
free(text);
|
|
return NULL;
|
|
}
|
|
text[n] = '\0';
|
|
return text;
|
|
}
|
|
|
|
static int jstr(const char *json, const char *key, char *out, size_t n)
|
|
{
|
|
char pattern[64];
|
|
const char *p;
|
|
size_t i = 0;
|
|
|
|
snprintf(pattern, sizeof(pattern), "\"%s\":\"", key);
|
|
p = strstr(json, pattern);
|
|
if (p == NULL) {
|
|
out[0] = '\0';
|
|
return 0;
|
|
}
|
|
p += strlen(pattern);
|
|
while (*p != '\0' && *p != '"' && i + 1 < n)
|
|
out[i++] = *p++;
|
|
out[i] = '\0';
|
|
return 1;
|
|
}
|
|
|
|
static long jnum(const char *json, const char *key, int *found)
|
|
{
|
|
char pattern[64];
|
|
const char *p;
|
|
|
|
if (found != NULL)
|
|
*found = 0;
|
|
snprintf(pattern, sizeof(pattern), "\"%s\":", key);
|
|
p = strstr(json, pattern);
|
|
if (p == NULL)
|
|
return 0;
|
|
p += strlen(pattern);
|
|
while (*p == ' ')
|
|
p++;
|
|
if (found != NULL)
|
|
*found = 1;
|
|
return atol(p);
|
|
}
|
|
|
|
static void jwt_finding(mcp_buf *b, int *first, const char *id,
|
|
const char *severity, const char *detail)
|
|
{
|
|
if (!*first)
|
|
mcp_buf_puts(b, ",");
|
|
*first = 0;
|
|
mcp_buf_puts(b, "{\"id\":");
|
|
apex_json_str(b, id);
|
|
mcp_buf_puts(b, ",\"severity\":");
|
|
apex_json_str(b, severity);
|
|
mcp_buf_puts(b, ",\"detail\":");
|
|
apex_json_str(b, detail);
|
|
mcp_buf_puts(b, "}");
|
|
}
|
|
|
|
static void t_jwt_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *token = mcp_arg_string(args, "token");
|
|
const char *secret = mcp_arg_string(args, "secret");
|
|
char *copy;
|
|
char *save = NULL;
|
|
char *h;
|
|
char *p;
|
|
char *sig;
|
|
char alg[32];
|
|
char typ[32];
|
|
char kid[128];
|
|
char iss[128];
|
|
char aud[128];
|
|
char sub[128];
|
|
char *hjson;
|
|
char *pjson;
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int found;
|
|
long exp;
|
|
long nbf;
|
|
long iat;
|
|
time_t now = time(NULL);
|
|
const char *signature = "not-checked";
|
|
|
|
(void)user;
|
|
if (token == NULL || *token == '\0') {
|
|
mcp_reply_error(c, "token is required");
|
|
return;
|
|
}
|
|
if (strlen(token) > 60000) {
|
|
mcp_reply_error(c, "token too long");
|
|
return;
|
|
}
|
|
copy = strdup(token);
|
|
if (copy == NULL) {
|
|
mcp_reply_error(c, "out of memory");
|
|
return;
|
|
}
|
|
{
|
|
char *dot1 = strchr(copy, '.');
|
|
char *dot2 = dot1 != NULL ? strchr(dot1 + 1, '.') : NULL;
|
|
|
|
if (dot1 == NULL || dot2 == NULL) {
|
|
mcp_reply_error(c, "not a JWT: expected header.payload.signature");
|
|
free(copy);
|
|
return;
|
|
}
|
|
*dot1 = '\0';
|
|
*dot2 = '\0';
|
|
h = copy;
|
|
p = dot1 + 1;
|
|
sig = dot2 + 1; /* may be empty for alg=none */
|
|
}
|
|
(void)save;
|
|
hjson = b64url_json(h);
|
|
pjson = b64url_json(p);
|
|
if (hjson == NULL || pjson == NULL ||
|
|
hjson[0] != '{' || pjson[0] != '{') {
|
|
mcp_reply_error(c, "cannot decode the JWT segments");
|
|
free(hjson);
|
|
free(pjson);
|
|
free(copy);
|
|
return;
|
|
}
|
|
jstr(hjson, "alg", alg, sizeof(alg));
|
|
jstr(hjson, "typ", typ, sizeof(typ));
|
|
jstr(hjson, "kid", kid, sizeof(kid));
|
|
jstr(pjson, "iss", iss, sizeof(iss));
|
|
jstr(pjson, "aud", aud, sizeof(aud));
|
|
jstr(pjson, "sub", sub, sizeof(sub));
|
|
exp = jnum(pjson, "exp", &found);
|
|
if (!found)
|
|
exp = -1;
|
|
nbf = jnum(pjson, "nbf", NULL);
|
|
iat = jnum(pjson, "iat", NULL);
|
|
|
|
if (secret != NULL && *secret != '\0' && strcmp(alg, "HS256") == 0) {
|
|
unsigned char mac[32];
|
|
unsigned char given[512];
|
|
char signing[80000];
|
|
int glen;
|
|
int ok = 1;
|
|
int i;
|
|
|
|
snprintf(signing, sizeof(signing), "%s.%s", h, p);
|
|
apex_hmac_sha256(secret, strlen(secret), signing, strlen(signing),
|
|
mac);
|
|
glen = b64url_decode(sig, given, sizeof(given));
|
|
if (glen != 32) {
|
|
signature = "invalid";
|
|
} else {
|
|
for (i = 0; i < 32; i++)
|
|
if (mac[i] != given[i])
|
|
ok = 0;
|
|
signature = ok ? "valid" : "invalid";
|
|
}
|
|
}
|
|
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"header\":{\"alg\":");
|
|
apex_json_str(&b, alg);
|
|
mcp_buf_puts(&b, ",\"typ\":");
|
|
apex_json_str(&b, typ);
|
|
mcp_buf_puts(&b, ",\"kid\":");
|
|
apex_json_str(&b, kid);
|
|
mcp_buf_puts(&b, "},\"claims\":{\"iss\":");
|
|
apex_json_str(&b, iss);
|
|
mcp_buf_puts(&b, ",\"aud\":");
|
|
apex_json_str(&b, aud);
|
|
mcp_buf_puts(&b, ",\"sub\":");
|
|
apex_json_str(&b, sub);
|
|
if (found)
|
|
mcp_buf_printf(&b, ",\"exp\":%ld", exp);
|
|
else
|
|
mcp_buf_puts(&b, ",\"exp\":null");
|
|
mcp_buf_printf(&b, ",\"iat\":%ld,\"nbf\":%ld},\"findings\":[", iat,
|
|
nbf);
|
|
if (alg[0] == '\0')
|
|
jwt_finding(&b, &first, "alg-missing", "high",
|
|
"header has no alg");
|
|
else if (strcasecmp(alg, "none") == 0)
|
|
jwt_finding(&b, &first, "alg-none", "critical",
|
|
"alg=none: the token is unsigned");
|
|
if (kid[0] != '\0' && (strstr(kid, "/") != NULL ||
|
|
strstr(kid, "..") != NULL))
|
|
jwt_finding(&b, &first, "kid-path", "high",
|
|
"kid contains a path separator");
|
|
if (!found)
|
|
jwt_finding(&b, &first, "exp-missing", "medium",
|
|
"no exp claim: the token never expires");
|
|
else if (exp < (long)now)
|
|
jwt_finding(&b, &first, "expired", "high",
|
|
"the token is expired");
|
|
else if (exp > (long)now + 365L * 86400L)
|
|
jwt_finding(&b, &first, "long-lived", "low",
|
|
"the token lives longer than a year");
|
|
if (aud[0] == '\0')
|
|
jwt_finding(&b, &first, "aud-missing", "low",
|
|
"no audience claim");
|
|
if (iss[0] == '\0')
|
|
jwt_finding(&b, &first, "iss-missing", "low",
|
|
"no issuer claim");
|
|
mcp_buf_printf(&b, "],\"signature\":\"%s\"}", signature);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
free(hjson);
|
|
free(pjson);
|
|
free(copy);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ hash_id */
|
|
|
|
static int all_hex(const char *s, size_t n)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; i < n; i++)
|
|
if (!((s[i] >= '0' && s[i] <= '9') ||
|
|
(s[i] >= 'a' && s[i] <= 'f') ||
|
|
(s[i] >= 'A' && s[i] <= 'F')))
|
|
return 0;
|
|
return 1;
|
|
}
|
|
|
|
static void t_hash_id(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *value = mcp_arg_string(args, "value");
|
|
size_t n;
|
|
mcp_buf b;
|
|
int first = 1;
|
|
|
|
(void)user;
|
|
if (value == NULL || *value == '\0') {
|
|
mcp_reply_error(c, "value is required");
|
|
return;
|
|
}
|
|
n = strlen(value);
|
|
mcp_buf_init(&b);
|
|
mcp_buf_printf(&b, "{\"length\":%lu,\"candidates\":[",
|
|
(unsigned long)n);
|
|
#define CAND(fmt, note) do { \
|
|
if (!first) mcp_buf_puts(&b, ","); \
|
|
first = 0; \
|
|
mcp_buf_puts(&b, "{\"format\":"); \
|
|
apex_json_str(&b, fmt); \
|
|
mcp_buf_puts(&b, ",\"note\":"); \
|
|
apex_json_str(&b, note); \
|
|
mcp_buf_puts(&b, "}"); \
|
|
} while (0)
|
|
if (n == 32 && all_hex(value, n)) {
|
|
CAND("md5", "32 hex digits; also looks like NTLM");
|
|
CAND("ntlm", "same length as MD5");
|
|
} else if (n == 40 && all_hex(value, n)) {
|
|
CAND("sha1", "40 hex digits");
|
|
} else if (n == 64 && all_hex(value, n)) {
|
|
CAND("sha256", "64 hex digits");
|
|
} else if (n == 128 && all_hex(value, n)) {
|
|
CAND("sha512", "128 hex digits");
|
|
} else if (strncmp(value, "$2a$", 4) == 0 ||
|
|
strncmp(value, "$2b$", 4) == 0 ||
|
|
strncmp(value, "$2y$", 4) == 0) {
|
|
CAND("bcrypt", "bcrypt modular crypt format");
|
|
} else if (strncmp(value, "$argon2id$", 10) == 0) {
|
|
CAND("argon2id", "Argon2id PHC string");
|
|
} else if (strncmp(value, "$argon2i$", 9) == 0) {
|
|
CAND("argon2i", "Argon2i PHC string");
|
|
} else if (strncmp(value, "$1$", 3) == 0) {
|
|
CAND("md5crypt", "md5crypt modular crypt format");
|
|
} else if (strncmp(value, "$5$", 3) == 0) {
|
|
CAND("sha256crypt", "sha256crypt modular crypt format");
|
|
} else if (strncmp(value, "$6$", 3) == 0) {
|
|
CAND("sha512crypt", "sha512crypt modular crypt format");
|
|
} else if (strncmp(value, "eyJ", 3) == 0 && strchr(value, '.') != NULL) {
|
|
CAND("jwt", "looks like a JWT; use jwt_audit");
|
|
} else {
|
|
CAND("unknown", "no known hash format; may be plaintext");
|
|
}
|
|
#undef CAND
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- param_fuzz */
|
|
|
|
static const char *SQL_SIGS[] = {
|
|
"SQL syntax", "mysql_fetch", "mysqli", "ORA-0", "PostgreSQL",
|
|
"SQLite/JDBCDriver", "Unclosed quotation", "SQLSTATE",
|
|
"quoted string not properly terminated", "pg_query", NULL
|
|
};
|
|
|
|
static const char *STACK_SIGS[] = {
|
|
"Traceback (most recent call last)", "Exception in thread", "at java.",
|
|
"Fatal error:", "Warning: ", "System.Exception", NULL
|
|
};
|
|
|
|
static const char *TPL_SIGS[] = {
|
|
"TemplateSyntaxError", "Jinja2", "Twig\\", "Smarty", "Undefined variable",
|
|
"FreeMarker", "Velocity", NULL
|
|
};
|
|
|
|
static int any_sig(const char *body, size_t len, const char *const *sigs)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; sigs[i] != NULL; i++)
|
|
if (find_bytes(body, len, sigs[i]))
|
|
return 1;
|
|
return 0;
|
|
}
|
|
|
|
typedef struct {
|
|
const char *kind;
|
|
const char *value;
|
|
} fuzz_payload;
|
|
|
|
static void t_param_fuzz(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
const char *path = mcp_arg_string(args, "path");
|
|
const char *params_arg = mcp_arg_string(args, "params");
|
|
const char *method = mcp_arg_string(args, "method");
|
|
int port = mcp_arg_int(args, "port", 80);
|
|
int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port));
|
|
int delay = mcp_arg_int(args, "delay_ms", 100);
|
|
char params[10][64];
|
|
int nparams = 0;
|
|
char marker[48];
|
|
fuzz_payload payloads[3];
|
|
mcp_buf b;
|
|
int tested = 0;
|
|
int hits = 0;
|
|
int saved = 0;
|
|
int first = 1;
|
|
int i;
|
|
int p;
|
|
int is_post;
|
|
mcp_buf enc;
|
|
mcp_buf body;
|
|
|
|
(void)user;
|
|
if (target == NULL || *target == '\0' || params_arg == NULL ||
|
|
*params_arg == '\0') {
|
|
mcp_reply_error(c, "target and params are required");
|
|
return;
|
|
}
|
|
if (path == NULL || *path == '\0')
|
|
path = "/";
|
|
if (path[0] != '/') {
|
|
mcp_reply_error(c, "path must start with /");
|
|
return;
|
|
}
|
|
if (delay < 0 || delay > 1000)
|
|
delay = 100;
|
|
is_post = method != NULL && strcasecmp(method, "POST") == 0;
|
|
{
|
|
char *copy = strdup(params_arg);
|
|
char *save = NULL;
|
|
char *tok;
|
|
|
|
if (copy == NULL) {
|
|
mcp_reply_error(c, "out of memory");
|
|
return;
|
|
}
|
|
tok = strtok_r(copy, ",", &save);
|
|
while (tok != NULL && nparams < 10) {
|
|
while (*tok == ' ' || *tok == '\t')
|
|
tok++;
|
|
if (*tok != '\0') {
|
|
snprintf(params[nparams], sizeof(params[0]), "%.48s",
|
|
tok);
|
|
nparams++;
|
|
}
|
|
tok = strtok_r(NULL, ",", &save);
|
|
}
|
|
free(copy);
|
|
}
|
|
if (nparams == 0) {
|
|
mcp_reply_error(c, "no parameter names given");
|
|
return;
|
|
}
|
|
snprintf(marker, sizeof(marker), "apex%ldz", (long)getpid());
|
|
payloads[0].kind = "marker";
|
|
payloads[0].value = marker;
|
|
payloads[1].kind = "quote";
|
|
payloads[1].value = "'";
|
|
payloads[2].kind = "template";
|
|
payloads[2].value = "{{1337*7}}";
|
|
if (!gated(c, "param_fuzz", target, port))
|
|
return;
|
|
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"path\":", port);
|
|
apex_json_str(&b, path);
|
|
mcp_buf_printf(&b, ",\"method\":\"%s\",\"findings\":[",
|
|
is_post ? "POST" : "GET");
|
|
for (p = 0; p < nparams; p++) {
|
|
for (i = 0; i < 3; i++) {
|
|
char req[1800];
|
|
char status[256];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
int code;
|
|
int reflected;
|
|
int flags = 0;
|
|
int first_sig = 1;
|
|
mcp_buf signals;
|
|
char *evidence = NULL;
|
|
|
|
mcp_buf_init(&enc);
|
|
url_enc(&enc, payloads[i].value);
|
|
mcp_buf_init(&body);
|
|
if (is_post) {
|
|
mcp_buf_printf(&body, "%s=%s", params[p],
|
|
enc.p != NULL ? enc.p : "");
|
|
snprintf(req, sizeof(req), "%s", path);
|
|
} else {
|
|
const char *sep = strchr(path, '?') != NULL ? "&" : "?";
|
|
|
|
snprintf(req, sizeof(req), "%s%s%s=%s", path, sep,
|
|
params[p], enc.p != NULL ? enc.p : "");
|
|
}
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, tls, is_post ? "POST" : "GET",
|
|
req, is_post ? body.p : NULL,
|
|
is_post ? "Content-Type: "
|
|
"application/x-www-form-urlencoded\r\n"
|
|
: NULL,
|
|
8000, &resp, &len, status,
|
|
sizeof(status)) != 0) {
|
|
mcp_buf_free(&enc);
|
|
mcp_buf_free(&body);
|
|
continue;
|
|
}
|
|
tested++;
|
|
code = apex_status_code(status);
|
|
reflected = find_bytes(resp, len, payloads[i].value);
|
|
mcp_buf_init(&signals);
|
|
if (reflected && strcmp(payloads[i].kind, "marker") == 0) {
|
|
if (!first_sig)
|
|
mcp_buf_puts(&signals, ",");
|
|
first_sig = 0;
|
|
apex_json_str(&signals, "reflection");
|
|
flags++;
|
|
}
|
|
if (any_sig(resp, len, SQL_SIGS)) {
|
|
if (!first_sig)
|
|
mcp_buf_puts(&signals, ",");
|
|
first_sig = 0;
|
|
apex_json_str(&signals, "sql-error");
|
|
flags++;
|
|
}
|
|
if (any_sig(resp, len, STACK_SIGS)) {
|
|
if (!first_sig)
|
|
mcp_buf_puts(&signals, ",");
|
|
first_sig = 0;
|
|
apex_json_str(&signals, "stack-trace");
|
|
flags++;
|
|
}
|
|
if (any_sig(resp, len, TPL_SIGS)) {
|
|
if (!first_sig)
|
|
mcp_buf_puts(&signals, ",");
|
|
first_sig = 0;
|
|
apex_json_str(&signals, "template-error");
|
|
flags++;
|
|
}
|
|
if (strcmp(payloads[i].kind, "template") == 0 &&
|
|
find_bytes(resp, len, "9359")) {
|
|
if (!first_sig)
|
|
mcp_buf_puts(&signals, ",");
|
|
first_sig = 0;
|
|
apex_json_str(&signals, "template-eval");
|
|
flags++;
|
|
}
|
|
if (flags > 0) {
|
|
hits++;
|
|
if (saved < 3) {
|
|
char label[64];
|
|
|
|
snprintf(label, sizeof(label), "fuzz-%.20s-%.10s",
|
|
params[p], payloads[i].kind);
|
|
evidence = apex_evidence_save(label, resp, len);
|
|
saved++;
|
|
}
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"parameter\":");
|
|
apex_json_str(&b, params[p]);
|
|
mcp_buf_puts(&b, ",\"payload\":");
|
|
apex_json_str(&b, payloads[i].kind);
|
|
mcp_buf_printf(&b, ",\"status\":%d,\"bytes\":%lu,"
|
|
"\"reflected\":%s,\"signals\":[",
|
|
code, (unsigned long)len,
|
|
reflected ? "true" : "false");
|
|
if (signals.p != NULL)
|
|
mcp_buf_add(&b, signals.p, signals.len);
|
|
mcp_buf_puts(&b, "],\"evidence\":");
|
|
if (evidence != NULL)
|
|
apex_json_str(&b, evidence);
|
|
else
|
|
mcp_buf_puts(&b, "null");
|
|
mcp_buf_puts(&b, "}");
|
|
free(evidence);
|
|
}
|
|
mcp_buf_free(&signals);
|
|
mcp_buf_free(&enc);
|
|
mcp_buf_free(&body);
|
|
free(resp);
|
|
if (delay > 0 && !(p == nparams - 1 && i == 2))
|
|
sleep_ms(delay);
|
|
}
|
|
}
|
|
mcp_buf_printf(&b, "],\"tested\":%d,\"hits\":%d}", tested, hits);
|
|
apex_audit("param_fuzz", target, port, hits == 0,
|
|
hits == 0 ? "no reflection or error signature"
|
|
: "input handling signals found", path);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ------------------------------------------------------------- open_redirect */
|
|
|
|
static void response_location(const char *resp, size_t len, char *out, size_t n)
|
|
{
|
|
size_t i = 0;
|
|
|
|
out[0] = '\0';
|
|
while (i < len) {
|
|
size_t start = i;
|
|
size_t end;
|
|
size_t w = 0;
|
|
|
|
while (i < len && resp[i] != '\n')
|
|
i++;
|
|
end = i;
|
|
if (i < len)
|
|
i++;
|
|
if (end > start && resp[start] == '\r')
|
|
start++;
|
|
if (end >= start + 9 && strncasecmp(resp + start, "Location:", 9) == 0) {
|
|
const char *v = resp + start + 9;
|
|
|
|
while (v < resp + end && (*v == ' ' || *v == '\t'))
|
|
v++;
|
|
while (v < resp + end && *v != '\r' && *v != '\n' &&
|
|
w + 1 < n)
|
|
out[w++] = *v++;
|
|
while (w > 0 && (out[w - 1] == ' ' || out[w - 1] == '\t'))
|
|
w--;
|
|
out[w] = '\0';
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void t_open_redirect(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
const char *path = mcp_arg_string(args, "path");
|
|
const char *param = mcp_arg_string(args, "param");
|
|
const char *external = mcp_arg_string(args, "external");
|
|
int port = mcp_arg_int(args, "port", 80);
|
|
int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port));
|
|
char host[256];
|
|
char variant[2][600];
|
|
int nvariants = 1;
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int confirmed = 0;
|
|
int i;
|
|
char *evidence = NULL;
|
|
|
|
(void)user;
|
|
if (target == NULL || *target == '\0') {
|
|
mcp_reply_error(c, "target is required");
|
|
return;
|
|
}
|
|
if (path == NULL || *path == '\0')
|
|
path = "/";
|
|
if (path[0] != '/') {
|
|
mcp_reply_error(c, "path must start with /");
|
|
return;
|
|
}
|
|
if (param == NULL || *param == '\0')
|
|
param = "next";
|
|
if (external == NULL || *external == '\0')
|
|
external = "https://example.com/apex-redirect";
|
|
host[0] = '\0';
|
|
{
|
|
const char *scheme = strstr(external, "://");
|
|
|
|
if (scheme == NULL) {
|
|
mcp_reply_error(c, "external must be an absolute URL");
|
|
return;
|
|
}
|
|
scheme += 3;
|
|
{
|
|
size_t w = 0;
|
|
|
|
while (scheme[w] != '\0' && scheme[w] != '/' &&
|
|
scheme[w] != ':' && w + 1 < sizeof(host)) {
|
|
host[w] = scheme[w];
|
|
w++;
|
|
}
|
|
host[w] = '\0';
|
|
}
|
|
}
|
|
if (host[0] == '\0') {
|
|
mcp_reply_error(c, "cannot parse the external host");
|
|
return;
|
|
}
|
|
snprintf(variant[0], sizeof(variant[0]), "%s", external);
|
|
snprintf(variant[1], sizeof(variant[1]), "//%s/apex-redirect", host);
|
|
nvariants = 2;
|
|
if (!gated(c, "open_redirect", target, port))
|
|
return;
|
|
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"path\":", port);
|
|
apex_json_str(&b, path);
|
|
mcp_buf_puts(&b, ",\"param\":");
|
|
apex_json_str(&b, param);
|
|
mcp_buf_puts(&b, ",\"results\":[");
|
|
for (i = 0; i < nvariants; i++) {
|
|
char req[1800];
|
|
char status[256];
|
|
char location[600];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
int code;
|
|
const char *sep = strchr(path, '?') != NULL ? "&" : "?";
|
|
mcp_buf enc;
|
|
int hit = 0;
|
|
const char *via = "none";
|
|
|
|
mcp_buf_init(&enc);
|
|
url_enc(&enc, variant[i]);
|
|
snprintf(req, sizeof(req), "%s%s%s=%s", path, sep, param,
|
|
enc.p != NULL ? enc.p : "");
|
|
mcp_buf_free(&enc);
|
|
status[0] = '\0';
|
|
location[0] = '\0';
|
|
if (apex_http_once(target, port, tls, "GET", req, NULL, NULL, 8000,
|
|
&resp, &len, status, sizeof(status)) != 0)
|
|
continue;
|
|
code = apex_status_code(status);
|
|
response_location(resp, len, location, sizeof(location));
|
|
if (((code == 301 || code == 302 || code == 303 || code == 307 ||
|
|
code == 308) && strstr(location, host) != NULL)) {
|
|
hit = 1;
|
|
via = "location";
|
|
} else if (strstr(location, host) != NULL) {
|
|
hit = 1;
|
|
via = "location";
|
|
} else if (find_bytes(resp, len, "http-equiv") &&
|
|
find_bytes(resp, len, host)) {
|
|
hit = 1;
|
|
via = "meta-refresh";
|
|
}
|
|
if (hit) {
|
|
confirmed++;
|
|
if (evidence == NULL)
|
|
evidence = apex_evidence_save("redirect", resp, len);
|
|
}
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"value\":");
|
|
apex_json_str(&b, variant[i]);
|
|
mcp_buf_printf(&b, ",\"status\":%d,\"location\":", code);
|
|
apex_json_str(&b, location);
|
|
mcp_buf_printf(&b, ",\"redirected\":%s,\"via\":\"%s\"}",
|
|
hit ? "true" : "false", via);
|
|
free(resp);
|
|
}
|
|
mcp_buf_printf(&b, "],\"confirmed\":%d,\"evidence\":", confirmed);
|
|
if (evidence != NULL)
|
|
apex_json_str(&b, evidence);
|
|
else
|
|
mcp_buf_puts(&b, "null");
|
|
mcp_buf_puts(&b, "}");
|
|
apex_audit("open_redirect", target, port, confirmed == 0,
|
|
confirmed == 0 ? "no open redirect" : "redirect to an external host",
|
|
path);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
free(evidence);
|
|
}
|
|
|
|
/* ------------------------------------------------------ host_header_injection */
|
|
|
|
static void t_host_header_injection(mcp_conn *c, const mcp_json *args,
|
|
void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
const char *path = mcp_arg_string(args, "path");
|
|
const char *canary = mcp_arg_string(args, "canary");
|
|
int port = mcp_arg_int(args, "port", 80);
|
|
int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port));
|
|
char status[256];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
int base_status = 0;
|
|
long base_len = 0;
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int confirmed = 0;
|
|
int variant;
|
|
|
|
(void)user;
|
|
if (target == NULL || *target == '\0') {
|
|
mcp_reply_error(c, "target is required");
|
|
return;
|
|
}
|
|
if (path == NULL || *path == '\0')
|
|
path = "/";
|
|
if (path[0] != '/') {
|
|
mcp_reply_error(c, "path must start with /");
|
|
return;
|
|
}
|
|
if (canary == NULL || *canary == '\0')
|
|
canary = "apex-canary.invalid";
|
|
if (!gated(c, "host_header_injection", target, port))
|
|
return;
|
|
status[0] = '\0';
|
|
if (http_with_host(target, port, target, path, &resp, &len, status,
|
|
sizeof(status)) == 0) {
|
|
base_status = apex_status_code(status);
|
|
base_len = (long)len;
|
|
free(resp);
|
|
}
|
|
resp = NULL;
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"path\":", port);
|
|
apex_json_str(&b, path);
|
|
mcp_buf_puts(&b, ",\"canary\":");
|
|
apex_json_str(&b, canary);
|
|
mcp_buf_printf(&b, ",\"baseline\":{\"status\":%d,\"bytes\":%ld},"
|
|
"\"findings\":[", base_status, base_len);
|
|
for (variant = 0; variant < 2; variant++) {
|
|
const char *label = variant == 0 ? "Host" : "X-Forwarded-Host";
|
|
char location[600];
|
|
char *evidence = NULL;
|
|
int code;
|
|
int reflected;
|
|
int redirected;
|
|
|
|
status[0] = '\0';
|
|
location[0] = '\0';
|
|
if (variant == 0) {
|
|
if (http_with_host(target, port, canary, path, &resp, &len,
|
|
status, sizeof(status)) != 0)
|
|
continue;
|
|
} else {
|
|
char header[320];
|
|
|
|
snprintf(header, sizeof(header), "X-Forwarded-Host: %.200s\r\n",
|
|
canary);
|
|
if (apex_http_once(target, port, tls, "GET", path, NULL, header,
|
|
8000, &resp, &len, status,
|
|
sizeof(status)) != 0)
|
|
continue;
|
|
}
|
|
code = apex_status_code(status);
|
|
response_location(resp, len, location, sizeof(location));
|
|
reflected = find_bytes(resp, len, canary);
|
|
redirected = strstr(location, canary) != NULL;
|
|
if (reflected || redirected) {
|
|
confirmed++;
|
|
evidence = apex_evidence_save("host-header", resp, len);
|
|
}
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"header\":");
|
|
apex_json_str(&b, label);
|
|
mcp_buf_printf(&b, ",\"status\":%d,\"bytes\":%lu,"
|
|
"\"reflected\":%s,\"redirect\":%s,\"evidence\":",
|
|
code, (unsigned long)len,
|
|
reflected ? "true" : "false",
|
|
redirected ? "true" : "false");
|
|
if (evidence != NULL)
|
|
apex_json_str(&b, evidence);
|
|
else
|
|
mcp_buf_puts(&b, "null");
|
|
mcp_buf_puts(&b, "}");
|
|
free(evidence);
|
|
free(resp);
|
|
resp = NULL;
|
|
}
|
|
mcp_buf_printf(&b, "],\"confirmed\":%d}", confirmed);
|
|
apex_audit("host_header_injection", target, port, confirmed == 0,
|
|
confirmed == 0 ? "canary not reflected"
|
|
: "canary reflected or redirected",
|
|
path);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- ssti_probe */
|
|
|
|
typedef struct {
|
|
const char *kind;
|
|
const char *payload;
|
|
const char *expected;
|
|
const char *engine;
|
|
} ssti_payload;
|
|
|
|
static const ssti_payload SSTI_PAYLOADS[] = {
|
|
{ "jinja-twig", "{{1337*7}}", "9359", "Jinja2/Twig/Nunjucks" },
|
|
{ "el-freemarker", "${1337*7}", "9359", "FreeMarker/JSP EL/Thymeleaf" },
|
|
{ "jsf-ruby", "#{1337*7}", "9359", "JSF/Ruby" },
|
|
{ "erb-ejs", "<%= 1337*7 %>", "9359", "ERB/EJS" },
|
|
{ "jinja-string", "{{1337*'7'}}", "1337133713371337", "Jinja2 string repeat" },
|
|
};
|
|
|
|
static void t_ssti_probe(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
const char *path = mcp_arg_string(args, "path");
|
|
const char *param = mcp_arg_string(args, "param");
|
|
const char *method = mcp_arg_string(args, "method");
|
|
int port = mcp_arg_int(args, "port", 80);
|
|
int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port));
|
|
int delay = mcp_arg_int(args, "delay_ms", 100);
|
|
int is_post;
|
|
char marker[48];
|
|
int baseline_contaminated = 0;
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int tested = 0;
|
|
int evaluated = 0;
|
|
int saved = 0;
|
|
int i;
|
|
|
|
(void)user;
|
|
if (target == NULL || *target == '\0' || param == NULL || *param == '\0') {
|
|
mcp_reply_error(c, "target and param are required");
|
|
return;
|
|
}
|
|
if (path == NULL || *path == '\0')
|
|
path = "/";
|
|
if (path[0] != '/') {
|
|
mcp_reply_error(c, "path must start with /");
|
|
return;
|
|
}
|
|
if (delay < 0 || delay > 1000)
|
|
delay = 100;
|
|
is_post = method != NULL && strcasecmp(method, "POST") == 0;
|
|
if (!gated(c, "ssti_probe", target, port))
|
|
return;
|
|
snprintf(marker, sizeof(marker), "apexSSTI%ld", (long)getpid());
|
|
/* baseline: does the page already contain the expected numbers? */
|
|
{
|
|
char req[1200];
|
|
char status[256];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
mcp_buf enc;
|
|
|
|
mcp_buf_init(&enc);
|
|
url_enc(&enc, marker);
|
|
if (is_post)
|
|
snprintf(req, sizeof(req), "%s", path);
|
|
else {
|
|
const char *sep = strchr(path, '?') != NULL ? "&" : "?";
|
|
|
|
snprintf(req, sizeof(req), "%s%s%s=%s", path, sep, param,
|
|
enc.p != NULL ? enc.p : "");
|
|
}
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, tls, is_post ? "POST" : "GET", req,
|
|
is_post ? enc.p : NULL,
|
|
is_post ? "Content-Type: application/"
|
|
"x-www-form-urlencoded\r\n" : NULL,
|
|
8000, &resp, &len, status, sizeof(status)) == 0) {
|
|
if (find_bytes(resp, len, "9359") ||
|
|
find_bytes(resp, len, "133713371337"))
|
|
baseline_contaminated = 1;
|
|
free(resp);
|
|
}
|
|
mcp_buf_free(&enc);
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"path\":", port);
|
|
apex_json_str(&b, path);
|
|
mcp_buf_puts(&b, ",\"param\":");
|
|
apex_json_str(&b, param);
|
|
mcp_buf_printf(&b, ",\"method\":\"%s\",\"baseline_contaminated\":%s,"
|
|
"\"results\":[", is_post ? "POST" : "GET",
|
|
baseline_contaminated ? "true" : "false");
|
|
for (i = 0; i < (int)(sizeof(SSTI_PAYLOADS) / sizeof(SSTI_PAYLOADS[0]));
|
|
i++) {
|
|
char req[1600];
|
|
char status[256];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
int code;
|
|
int ev;
|
|
int reflected;
|
|
int err;
|
|
int flags = 0;
|
|
int first_sig = 1;
|
|
mcp_buf enc;
|
|
mcp_buf body;
|
|
mcp_buf signals;
|
|
char *evidence = NULL;
|
|
|
|
mcp_buf_init(&enc);
|
|
url_enc(&enc, SSTI_PAYLOADS[i].payload);
|
|
mcp_buf_init(&body);
|
|
if (is_post) {
|
|
mcp_buf_printf(&body, "%s=%s", param,
|
|
enc.p != NULL ? enc.p : "");
|
|
snprintf(req, sizeof(req), "%s", path);
|
|
} else {
|
|
const char *sep = strchr(path, '?') != NULL ? "&" : "?";
|
|
|
|
snprintf(req, sizeof(req), "%s%s%s=%s", path, sep, param,
|
|
enc.p != NULL ? enc.p : "");
|
|
}
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, tls, is_post ? "POST" : "GET", req,
|
|
is_post ? body.p : NULL,
|
|
is_post ? "Content-Type: application/"
|
|
"x-www-form-urlencoded\r\n" : NULL,
|
|
8000, &resp, &len, status, sizeof(status)) != 0) {
|
|
mcp_buf_free(&enc);
|
|
mcp_buf_free(&body);
|
|
continue;
|
|
}
|
|
tested++;
|
|
code = apex_status_code(status);
|
|
ev = find_bytes(resp, len, SSTI_PAYLOADS[i].expected) &&
|
|
!baseline_contaminated;
|
|
reflected = find_bytes(resp, len, SSTI_PAYLOADS[i].payload);
|
|
err = any_sig(resp, len, TPL_SIGS);
|
|
mcp_buf_init(&signals);
|
|
if (ev) {
|
|
apex_json_str(&signals, "evaluated");
|
|
flags++;
|
|
}
|
|
if (reflected) {
|
|
if (flags > 0)
|
|
mcp_buf_puts(&signals, ",");
|
|
apex_json_str(&signals, "reflected");
|
|
flags++;
|
|
}
|
|
if (err) {
|
|
if (flags > 0)
|
|
mcp_buf_puts(&signals, ",");
|
|
apex_json_str(&signals, "template-error");
|
|
flags++;
|
|
}
|
|
(void)first_sig;
|
|
if (ev) {
|
|
evaluated++;
|
|
if (saved < 3) {
|
|
char label[64];
|
|
|
|
snprintf(label, sizeof(label), "ssti-%.16s",
|
|
SSTI_PAYLOADS[i].kind);
|
|
evidence = apex_evidence_save(label, resp, len);
|
|
saved++;
|
|
}
|
|
}
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"payload\":");
|
|
apex_json_str(&b, SSTI_PAYLOADS[i].kind);
|
|
mcp_buf_printf(&b, ",\"status\":%d,\"bytes\":%lu,\"evaluated\":%s,"
|
|
"\"reflected\":%s,\"engine\":",
|
|
code, (unsigned long)len, ev ? "true" : "false",
|
|
reflected ? "true" : "false");
|
|
apex_json_str(&b, ev ? SSTI_PAYLOADS[i].engine : "");
|
|
mcp_buf_puts(&b, ",\"signals\":[");
|
|
if (signals.p != NULL)
|
|
mcp_buf_add(&b, signals.p, signals.len);
|
|
mcp_buf_puts(&b, "],\"evidence\":");
|
|
if (evidence != NULL)
|
|
apex_json_str(&b, evidence);
|
|
else
|
|
mcp_buf_puts(&b, "null");
|
|
mcp_buf_puts(&b, "}");
|
|
free(evidence);
|
|
mcp_buf_free(&signals);
|
|
mcp_buf_free(&enc);
|
|
mcp_buf_free(&body);
|
|
free(resp);
|
|
if (delay > 0 && i + 1 < (int)(sizeof(SSTI_PAYLOADS) /
|
|
sizeof(SSTI_PAYLOADS[0])))
|
|
sleep_ms(delay);
|
|
}
|
|
mcp_buf_printf(&b, "],\"tested\":%d,\"evaluated_count\":%d}",
|
|
tested, evaluated);
|
|
apex_audit("ssti_probe", target, port, evaluated == 0,
|
|
evaluated == 0 ? "no template evaluation"
|
|
: "template payload evaluated", path);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ xxe_probe */
|
|
|
|
static const char *XXE_SIGS[] = {
|
|
"DOCTYPE is disallowed", "External entity", "entity reference",
|
|
"XML parse error", "parser error", "SAXParseException",
|
|
"Invalid URI", "DTD is prohibited", NULL
|
|
};
|
|
|
|
static void t_xxe_probe(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
const char *path = mcp_arg_string(args, "path");
|
|
const char *field = mcp_arg_string(args, "post_field");
|
|
int port = mcp_arg_int(args, "port", 80);
|
|
int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port));
|
|
int wait_ms = mcp_arg_int(args, "wait_ms", 1200);
|
|
int cbport = 0;
|
|
int ls;
|
|
char token[64];
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int confirmed = 0;
|
|
int probe;
|
|
|
|
(void)user;
|
|
if (target == NULL || *target == '\0') {
|
|
mcp_reply_error(c, "target is required");
|
|
return;
|
|
}
|
|
if (path == NULL || *path == '\0')
|
|
path = "/";
|
|
if (path[0] != '/') {
|
|
mcp_reply_error(c, "path must start with /");
|
|
return;
|
|
}
|
|
if (wait_ms < 200 || wait_ms > 5000)
|
|
wait_ms = 1200;
|
|
if (!gated(c, "xxe_probe", target, port))
|
|
return;
|
|
ls = cb_open(&cbport);
|
|
if (ls < 0) {
|
|
mcp_reply_error(c, "cannot open the callback listener");
|
|
return;
|
|
}
|
|
snprintf(token, sizeof(token), "apex%ld", (long)getpid());
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"path\":", port);
|
|
apex_json_str(&b, path);
|
|
mcp_buf_printf(&b, ",\"callback_port\":%d,\"probes\":[", cbport);
|
|
for (probe = 0; probe < 2; probe++) {
|
|
char url[300];
|
|
char xml[1024];
|
|
char inj[4096];
|
|
char status[256];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
int code = 0;
|
|
int evaluated = 0;
|
|
int error_sig = 0;
|
|
int reflected = 0;
|
|
char *evidence = NULL;
|
|
mcp_buf enc;
|
|
|
|
snprintf(url, sizeof(url), "http://127.0.0.1:%d/%s-%d", cbport,
|
|
token, probe);
|
|
if (probe == 0) {
|
|
snprintf(xml, sizeof(xml),
|
|
"<?xml version=\"1.0\"?>\n"
|
|
"<!DOCTYPE foo [<!ENTITY xxe SYSTEM \"%s\">]>\n"
|
|
"<foo>&xxe;</foo>\n", url);
|
|
} else {
|
|
snprintf(xml, sizeof(xml),
|
|
"<?xml version=\"1.0\"?>\n"
|
|
"<!DOCTYPE foo [<!ENTITY xxe SYSTEM \"%s\">]>\n"
|
|
"<foo bar=\"&xxe;\"/>\n", url);
|
|
}
|
|
if (field != NULL && *field != '\0') {
|
|
mcp_buf_init(&enc);
|
|
url_enc(&enc, xml);
|
|
snprintf(inj, sizeof(inj), "%s=%s", field,
|
|
enc.p != NULL ? enc.p : "");
|
|
mcp_buf_free(&enc);
|
|
} else {
|
|
snprintf(inj, sizeof(inj), "%s", xml);
|
|
}
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, tls, "POST", path, inj,
|
|
(field != NULL && *field != '\0')
|
|
? "Content-Type: application/x-www-form-"
|
|
"urlencoded\r\n"
|
|
: "Content-Type: application/xml\r\n",
|
|
10000, &resp, &len, status,
|
|
sizeof(status)) == 0) {
|
|
char req[1024];
|
|
|
|
code = apex_status_code(status);
|
|
error_sig = any_sig(resp, len, XXE_SIGS);
|
|
reflected = find_bytes(resp, len, "&xxe;");
|
|
if (cb_wait(ls, wait_ms, req, sizeof(req))) {
|
|
char want[96];
|
|
|
|
snprintf(want, sizeof(want), "%s-%d", token, probe);
|
|
if (strstr(req, want) != NULL) {
|
|
evaluated = 1;
|
|
confirmed = 1;
|
|
evidence = apex_evidence_save("xxe-callback", req,
|
|
strlen(req));
|
|
}
|
|
}
|
|
free(resp);
|
|
}
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"kind\":");
|
|
apex_json_str(&b, probe == 0 ? "element" : "attribute");
|
|
mcp_buf_printf(&b, ",\"status\":%d,\"evaluated\":%s,"
|
|
"\"error_signature\":%s,\"reflected\":%s,"
|
|
"\"evidence\":", code,
|
|
evaluated ? "true" : "false",
|
|
error_sig ? "true" : "false",
|
|
reflected ? "true" : "false");
|
|
if (evidence != NULL)
|
|
apex_json_str(&b, evidence);
|
|
else
|
|
mcp_buf_puts(&b, "null");
|
|
mcp_buf_puts(&b, "}");
|
|
free(evidence);
|
|
}
|
|
close(ls);
|
|
mcp_buf_printf(&b, "],\"confirmed\":%s}", confirmed ? "true" : "false");
|
|
apex_audit("xxe_probe", target, port, confirmed,
|
|
confirmed ? "external entity was fetched"
|
|
: "no external entity callback", path);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ------------------------------------------------------------- graphql_probe */
|
|
|
|
static void gql_name_after(const char *text, const char *key, char *out,
|
|
size_t n)
|
|
{
|
|
const char *p = strstr(text, key);
|
|
const char *name;
|
|
size_t w = 0;
|
|
|
|
out[0] = '\0';
|
|
if (p == NULL)
|
|
return;
|
|
name = strstr(p, "\"name\":\"");
|
|
if (name == NULL)
|
|
return;
|
|
name += 8;
|
|
while (*name != '\0' && *name != '"' && w + 1 < n)
|
|
out[w++] = *name++;
|
|
out[w] = '\0';
|
|
}
|
|
|
|
static int gql_kind_count(const char *body, size_t len)
|
|
{
|
|
int count = 0;
|
|
size_t i;
|
|
|
|
for (i = 0; i + 8 <= len; i++)
|
|
if (memcmp(body + i, "\"kind\":\"", 8) == 0)
|
|
count++;
|
|
return count;
|
|
}
|
|
|
|
static void t_graphql_probe(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
const char *path = mcp_arg_string(args, "path");
|
|
int port = mcp_arg_int(args, "port", 80);
|
|
int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port));
|
|
static const char body[] =
|
|
"{\"query\":\"{__schema{queryType{name} mutationType{name} "
|
|
"types{name kind}}}\"}";
|
|
char status[256];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
int code = 0;
|
|
int introspection = 0;
|
|
int types = 0;
|
|
char query_type[128];
|
|
char mutation_type[128];
|
|
char reason[192];
|
|
char *evidence = NULL;
|
|
mcp_buf b;
|
|
|
|
(void)user;
|
|
if (target == NULL || *target == '\0') {
|
|
mcp_reply_error(c, "target is required");
|
|
return;
|
|
}
|
|
if (path == NULL || *path == '\0')
|
|
path = "/graphql";
|
|
if (path[0] != '/') {
|
|
mcp_reply_error(c, "path must start with /");
|
|
return;
|
|
}
|
|
if (!gated(c, "graphql_probe", target, port))
|
|
return;
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, tls, "POST", path, body,
|
|
"Content-Type: application/json\r\n", 10000, &resp,
|
|
&len, status, sizeof(status)) != 0) {
|
|
mcp_reply_error(c, "cannot reach the GraphQL endpoint");
|
|
return;
|
|
}
|
|
code = apex_status_code(status);
|
|
query_type[0] = mutation_type[0] = reason[0] = '\0';
|
|
if (find_bytes(resp, len, "__schema") &&
|
|
find_bytes(resp, len, "\"types\"")) {
|
|
introspection = 1;
|
|
types = gql_kind_count(resp, len);
|
|
gql_name_after(resp, "\"queryType\"", query_type,
|
|
sizeof(query_type));
|
|
gql_name_after(resp, "\"mutationType\"", mutation_type,
|
|
sizeof(mutation_type));
|
|
} else if (find_bytes(resp, len, "introspection") &&
|
|
(find_bytes(resp, len, "disabled") ||
|
|
find_bytes(resp, len, "not allowed") ||
|
|
find_bytes(resp, len, "forbidden") ||
|
|
find_bytes(resp, len, "not enabled"))) {
|
|
snprintf(reason, sizeof(reason), "%s",
|
|
"introspection is disabled");
|
|
} else if (code == 404) {
|
|
snprintf(reason, sizeof(reason), "%s", "endpoint not found");
|
|
} else {
|
|
snprintf(reason, sizeof(reason), "%s",
|
|
"no __schema in the response");
|
|
}
|
|
if (introspection)
|
|
evidence = apex_evidence_save("graphql", resp, len);
|
|
apex_audit("graphql_probe", target, port, !introspection,
|
|
introspection ? "GraphQL introspection enabled"
|
|
: "introspection not available", path);
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"path\":", port);
|
|
apex_json_str(&b, path);
|
|
mcp_buf_printf(&b, ",\"status\":%d,\"graphql\":%s,\"introspection\":%s,"
|
|
"\"types_count\":%d,\"query_type\":", code,
|
|
introspection ? "true" : "false",
|
|
introspection ? "true" : "false", types);
|
|
apex_json_str(&b, query_type);
|
|
mcp_buf_puts(&b, ",\"mutation_type\":");
|
|
apex_json_str(&b, mutation_type);
|
|
mcp_buf_puts(&b, ",\"reason\":");
|
|
apex_json_str(&b, reason);
|
|
mcp_buf_puts(&b, ",\"evidence\":");
|
|
if (evidence != NULL)
|
|
apex_json_str(&b, evidence);
|
|
else
|
|
mcp_buf_puts(&b, "null");
|
|
mcp_buf_puts(&b, "}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
free(evidence);
|
|
free(resp);
|
|
}
|
|
|
|
/* ---------------------------------------------------------- docker_api_probe */
|
|
|
|
static void json_field(const char *text, const char *key, char *out, size_t n)
|
|
{
|
|
char pattern[80];
|
|
const char *p;
|
|
size_t w = 0;
|
|
|
|
out[0] = '\0';
|
|
snprintf(pattern, sizeof(pattern), "\"%s\":\"", key);
|
|
p = strstr(text, pattern);
|
|
if (p == NULL)
|
|
return;
|
|
p += strlen(pattern);
|
|
while (*p != '\0' && *p != '"' && w + 1 < n) {
|
|
if (*p == '\\' && p[1] != '\0')
|
|
p++;
|
|
out[w++] = *p++;
|
|
}
|
|
out[w] = '\0';
|
|
}
|
|
|
|
static int count_needle(const char *text, const char *needle)
|
|
{
|
|
int count = 0;
|
|
const char *p = text;
|
|
|
|
while ((p = strstr(p, needle)) != NULL) {
|
|
count++;
|
|
p += strlen(needle);
|
|
}
|
|
return count;
|
|
}
|
|
|
|
static void collect_named(const char *text, const char *key,
|
|
char out[][128], int max, int *count)
|
|
{
|
|
char pattern[80];
|
|
const char *p;
|
|
int n = 0;
|
|
|
|
snprintf(pattern, sizeof(pattern), "\"%s\":\"", key);
|
|
p = text;
|
|
while (n < max && (p = strstr(p, pattern)) != NULL) {
|
|
size_t w = 0;
|
|
|
|
p += strlen(pattern);
|
|
while (*p != '\0' && *p != '"' && w + 1 < 128) {
|
|
if (*p == '\\' && p[1] != '\0')
|
|
p++;
|
|
out[n][w++] = *p++;
|
|
}
|
|
out[n][w] = '\0';
|
|
n++;
|
|
}
|
|
*count = n;
|
|
}
|
|
|
|
static void t_docker_api_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", 2375);
|
|
char status[256];
|
|
char *version = NULL;
|
|
char *containers = NULL;
|
|
size_t vlen = 0;
|
|
size_t clen = 0;
|
|
int vcode;
|
|
int ccode = 0;
|
|
char ver[64];
|
|
char api[64];
|
|
char os_name[64];
|
|
char names[10][128];
|
|
char image_names[10][128];
|
|
int nnames = 0;
|
|
int nimages = 0;
|
|
int count = 0;
|
|
int running = 0;
|
|
char *evidence = NULL;
|
|
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, "docker_api_probe", target, port))
|
|
return;
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, 0, "GET", "/version", NULL, NULL, 8000,
|
|
&version, &vlen, status, sizeof(status)) != 0) {
|
|
mcp_reply_error(c, "cannot reach the Docker API");
|
|
return;
|
|
}
|
|
vcode = apex_status_code(status);
|
|
ver[0] = api[0] = os_name[0] = '\0';
|
|
if (vcode == 200) {
|
|
json_field(version, "Version", ver, sizeof(ver));
|
|
json_field(version, "ApiVersion", api, sizeof(api));
|
|
json_field(version, "Os", os_name, sizeof(os_name));
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, 0, "GET", "/containers/json?all=1",
|
|
NULL, NULL, 10000, &containers, &clen, status,
|
|
sizeof(status)) == 0) {
|
|
ccode = apex_status_code(status);
|
|
count = count_needle(containers, "\"Id\":\"");
|
|
running = count_needle(containers, "\"State\":\"running\"");
|
|
{
|
|
const char *p = containers;
|
|
|
|
nnames = 0;
|
|
while (nnames < 10 &&
|
|
(p = strstr(p, "\"Names\":[")) != NULL) {
|
|
size_t w = 0;
|
|
|
|
p += 9;
|
|
if (*p == '"') {
|
|
p++;
|
|
while (*p != '\0' && *p != '"' && w + 1 < 128)
|
|
names[nnames][w++] = *p++;
|
|
names[nnames][w] = '\0';
|
|
nnames++;
|
|
}
|
|
}
|
|
nimages = 0;
|
|
collect_named(containers, "Image", image_names, 10,
|
|
&nimages);
|
|
}
|
|
}
|
|
}
|
|
if (vcode == 200 || vcode == 401 || vcode == 403)
|
|
apex_audit("docker_api_probe", target, port, vcode != 200,
|
|
vcode == 200 ? "unauthenticated Docker API"
|
|
: "Docker API requires authentication",
|
|
"/version");
|
|
evidence = apex_evidence_save("docker", version, vlen);
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"docker\":%s,\"protected\":%s,"
|
|
"\"version\":", port,
|
|
(vcode == 200 || vcode == 401 || vcode == 403)
|
|
? "true" : "false",
|
|
vcode == 401 || vcode == 403 ? "true" : "false");
|
|
apex_json_str(&b, ver);
|
|
mcp_buf_puts(&b, ",\"api_version\":");
|
|
apex_json_str(&b, api);
|
|
mcp_buf_puts(&b, ",\"os\":");
|
|
apex_json_str(&b, os_name);
|
|
mcp_buf_printf(&b, ",\"containers\":%d,\"running\":%d,\"containers_status\":%d,"
|
|
"\"names\":[", count, running, ccode);
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < nnames && i < 10; i++) {
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
apex_json_str(&b, names[i]);
|
|
}
|
|
}
|
|
mcp_buf_printf(&b, "],\"image_count\":%d,\"images\":[", nimages);
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < nimages && i < 10; i++) {
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
apex_json_str(&b, image_names[i]);
|
|
}
|
|
}
|
|
mcp_buf_puts(&b, "],\"evidence\":");
|
|
if (evidence != NULL)
|
|
apex_json_str(&b, evidence);
|
|
else
|
|
mcp_buf_puts(&b, "null");
|
|
mcp_buf_puts(&b, "}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
free(evidence);
|
|
free(version);
|
|
free(containers);
|
|
}
|
|
|
|
/* ------------------------------------------------------- elasticsearch_probe */
|
|
|
|
static void t_elasticsearch_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", 9200);
|
|
char status[256];
|
|
char *root = NULL;
|
|
char *health = NULL;
|
|
char *indices = NULL;
|
|
size_t rlen = 0;
|
|
size_t hlen = 0;
|
|
size_t ilen = 0;
|
|
int rcode;
|
|
int hcode = 0;
|
|
int icode = 0;
|
|
char cluster[128];
|
|
char version[64];
|
|
char tagline[128];
|
|
char hstatus[32];
|
|
char *evidence = NULL;
|
|
int index_count = 0;
|
|
int nodes = -1;
|
|
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, "elasticsearch_probe", target, port))
|
|
return;
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, 0, "GET", "/", NULL, NULL, 8000, &root,
|
|
&rlen, status, sizeof(status)) != 0) {
|
|
mcp_reply_error(c, "cannot reach the Elasticsearch API");
|
|
return;
|
|
}
|
|
rcode = apex_status_code(status);
|
|
cluster[0] = version[0] = tagline[0] = hstatus[0] = '\0';
|
|
if (rcode == 200) {
|
|
json_field(root, "cluster_name", cluster, sizeof(cluster));
|
|
json_field(root, "number", version, sizeof(version));
|
|
json_field(root, "tagline", tagline, sizeof(tagline));
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, 0, "GET", "/_cluster/health", NULL,
|
|
NULL, 8000, &health, &hlen, status,
|
|
sizeof(status)) == 0) {
|
|
hcode = apex_status_code(status);
|
|
json_field(health, "status", hstatus, sizeof(hstatus));
|
|
{
|
|
const char *p = strstr(health, "\"number_of_nodes\":");
|
|
int n;
|
|
|
|
if (p != NULL && sscanf(p + 18, "%d", &n) == 1)
|
|
nodes = n;
|
|
}
|
|
}
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, 0, "GET",
|
|
"/_cat/indices?format=json&h=index", NULL, NULL,
|
|
8000, &indices, &ilen, status,
|
|
sizeof(status)) == 0) {
|
|
icode = apex_status_code(status);
|
|
index_count = count_needle(indices, "\"index\":\"");
|
|
}
|
|
}
|
|
if (rcode == 200 || rcode == 401 || rcode == 403)
|
|
apex_audit("elasticsearch_probe", target, port, rcode != 200,
|
|
rcode == 200 ? "unauthenticated Elasticsearch API"
|
|
: "Elasticsearch requires authentication",
|
|
"/");
|
|
evidence = apex_evidence_save("elasticsearch", root, rlen);
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"elasticsearch\":%s,\"protected\":%s,"
|
|
"\"cluster_name\":", port,
|
|
(rcode == 200 || rcode == 401 || rcode == 403)
|
|
? "true" : "false",
|
|
rcode == 401 || rcode == 403 ? "true" : "false");
|
|
apex_json_str(&b, cluster);
|
|
mcp_buf_puts(&b, ",\"version\":");
|
|
apex_json_str(&b, version);
|
|
mcp_buf_puts(&b, ",\"tagline\":");
|
|
apex_json_str(&b, tagline);
|
|
mcp_buf_puts(&b, ",\"health\":");
|
|
apex_json_str(&b, hstatus);
|
|
mcp_buf_printf(&b, ",\"nodes\":%d,\"indices\":%d,\"health_status\":%d,"
|
|
"\"indices_status\":%d,\"evidence\":",
|
|
nodes, index_count, hcode, icode);
|
|
if (evidence != NULL)
|
|
apex_json_str(&b, evidence);
|
|
else
|
|
mcp_buf_puts(&b, "null");
|
|
mcp_buf_puts(&b, "}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
free(evidence);
|
|
free(root);
|
|
free(health);
|
|
free(indices);
|
|
}
|
|
|
|
/* -------------------------------------------------------------- http_methods */
|
|
|
|
static void response_header(const char *resp, size_t len, const char *name,
|
|
char *out, size_t n)
|
|
{
|
|
size_t i = 0;
|
|
size_t nl = strlen(name);
|
|
|
|
out[0] = '\0';
|
|
while (i < len) {
|
|
size_t start = i;
|
|
size_t end;
|
|
size_t w = 0;
|
|
|
|
while (i < len && resp[i] != '\n')
|
|
i++;
|
|
end = i;
|
|
if (i < len)
|
|
i++;
|
|
if (end > start && resp[start] == '\r')
|
|
start++;
|
|
if (end >= start + nl + 1 &&
|
|
strncasecmp(resp + start, name, nl) == 0 &&
|
|
resp[start + nl] == ':') {
|
|
const char *v = resp + start + nl + 1;
|
|
|
|
while (v < resp + end && (*v == ' ' || *v == '\t'))
|
|
v++;
|
|
while (v < resp + end && *v != '\r' && *v != '\n' &&
|
|
w + 1 < n)
|
|
out[w++] = *v++;
|
|
while (w > 0 && (out[w - 1] == ' ' || out[w - 1] == '\t'))
|
|
w--;
|
|
out[w] = '\0';
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
static int allow_has(const char *allow, const char *method)
|
|
{
|
|
const char *p = allow;
|
|
size_t ml = strlen(method);
|
|
|
|
while (*p != '\0') {
|
|
const char *end = p;
|
|
|
|
while (*end != '\0' && *end != ',')
|
|
end++;
|
|
while (*p == ' ' || *p == '\t')
|
|
p++;
|
|
if ((size_t)(end - p) == ml && strncasecmp(p, method, ml) == 0)
|
|
return 1;
|
|
p = *end == ',' ? end + 1 : end;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void t_http_methods(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
const char *path = mcp_arg_string(args, "path");
|
|
int port = mcp_arg_int(args, "port", 80);
|
|
int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port));
|
|
char status[256];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
int options_code = 0;
|
|
int trace_code = 0;
|
|
int trace_enabled = 0;
|
|
char allow[512];
|
|
char server[256];
|
|
mcp_buf b;
|
|
int first = 1;
|
|
|
|
(void)user;
|
|
if (target == NULL || *target == '\0') {
|
|
mcp_reply_error(c, "target is required");
|
|
return;
|
|
}
|
|
if (path == NULL || *path == '\0')
|
|
path = "/";
|
|
if (path[0] != '/') {
|
|
mcp_reply_error(c, "path must start with /");
|
|
return;
|
|
}
|
|
if (!gated(c, "http_methods", target, port))
|
|
return;
|
|
status[0] = '\0';
|
|
allow[0] = server[0] = '\0';
|
|
if (apex_http_once(target, port, tls, "OPTIONS", path, NULL, NULL, 8000,
|
|
&resp, &len, status, sizeof(status)) == 0) {
|
|
options_code = apex_status_code(status);
|
|
response_header(resp, len, "Allow", allow, sizeof(allow));
|
|
response_header(resp, len, "Server", server, sizeof(server));
|
|
free(resp);
|
|
resp = NULL;
|
|
}
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, tls, "TRACE", path, NULL, NULL, 8000,
|
|
&resp, &len, status, sizeof(status)) == 0) {
|
|
trace_code = apex_status_code(status);
|
|
if (trace_code == 200 && resp != NULL &&
|
|
find_bytes(resp, len, "TRACE "))
|
|
trace_enabled = 1;
|
|
free(resp);
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"path\":", port);
|
|
apex_json_str(&b, path);
|
|
mcp_buf_printf(&b, ",\"options_status\":%d,\"trace_status\":%d,"
|
|
"\"trace_enabled\":%s,\"server\":", options_code,
|
|
trace_code, trace_enabled ? "true" : "false");
|
|
apex_json_str(&b, server);
|
|
mcp_buf_puts(&b, ",\"allow\":[");
|
|
{
|
|
char copy[512];
|
|
char *save = NULL;
|
|
char *tok;
|
|
|
|
snprintf(copy, sizeof(copy), "%s", allow);
|
|
tok = strtok_r(copy, ",", &save);
|
|
while (tok != NULL) {
|
|
while (*tok == ' ' || *tok == '\t')
|
|
tok++;
|
|
if (*tok != '\0') {
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
apex_json_str(&b, tok);
|
|
}
|
|
tok = strtok_r(NULL, ",", &save);
|
|
}
|
|
}
|
|
mcp_buf_puts(&b, "],\"write_methods_allowed\":");
|
|
mcp_buf_printf(&b, "%s}",
|
|
(allow_has(allow, "PUT") || allow_has(allow, "DELETE"))
|
|
? "true" : "false");
|
|
apex_audit("http_methods", target, port, !trace_enabled,
|
|
trace_enabled ? "TRACE is enabled" : "TRACE not enabled", path);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ------------------------------------------------------------ websocket_probe */
|
|
|
|
static void t_websocket_probe(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
const char *path = mcp_arg_string(args, "path");
|
|
int port = mcp_arg_int(args, "port", 80);
|
|
int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port));
|
|
unsigned char raw[16];
|
|
char key[32];
|
|
char headers[512];
|
|
char status[256];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
int code = 0;
|
|
int upgraded = 0;
|
|
char upgrade[64];
|
|
char connection[128];
|
|
char accept[128];
|
|
mcp_buf b;
|
|
int fd;
|
|
|
|
(void)user;
|
|
if (target == NULL || *target == '\0') {
|
|
mcp_reply_error(c, "target is required");
|
|
return;
|
|
}
|
|
if (path == NULL || *path == '\0')
|
|
path = "/";
|
|
if (path[0] != '/') {
|
|
mcp_reply_error(c, "path must start with /");
|
|
return;
|
|
}
|
|
if (!gated(c, "websocket_probe", target, port))
|
|
return;
|
|
/* 16 bytes for the nonce, from /dev/urandom when available */
|
|
memset(raw, 0, sizeof(raw));
|
|
fd = open("/dev/urandom", O_RDONLY);
|
|
if (fd >= 0) {
|
|
ssize_t r = read(fd, raw, sizeof(raw));
|
|
|
|
(void)r;
|
|
close(fd);
|
|
}
|
|
{
|
|
char *b64 = apex_base64_alloc((const char *)raw, sizeof(raw));
|
|
|
|
if (b64 == NULL) {
|
|
mcp_reply_error(c, "out of memory");
|
|
return;
|
|
}
|
|
snprintf(key, sizeof(key), "%.28s", b64);
|
|
free(b64);
|
|
}
|
|
snprintf(headers, sizeof(headers),
|
|
"Upgrade: websocket\r\nConnection: Upgrade\r\n"
|
|
"Sec-WebSocket-Key: %s\r\nSec-WebSocket-Version: 13\r\n", key);
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, tls, "GET", path, NULL, headers, 8000,
|
|
&resp, &len, status, sizeof(status)) != 0) {
|
|
mcp_reply_error(c, "cannot reach the WebSocket endpoint");
|
|
return;
|
|
}
|
|
code = apex_status_code(status);
|
|
upgrade[0] = connection[0] = accept[0] = '\0';
|
|
response_header(resp, len, "Upgrade", upgrade, sizeof(upgrade));
|
|
response_header(resp, len, "Connection", connection, sizeof(connection));
|
|
response_header(resp, len, "Sec-WebSocket-Accept", accept, sizeof(accept));
|
|
upgraded = code == 101 &&
|
|
strcasecmp(upgrade, "websocket") == 0 &&
|
|
strcasestr(connection, "upgrade") != NULL;
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"path\":", port);
|
|
apex_json_str(&b, path);
|
|
mcp_buf_printf(&b, ",\"status\":%d,\"upgraded\":%s,\"upgrade\":",
|
|
code, upgraded ? "true" : "false");
|
|
apex_json_str(&b, upgrade);
|
|
mcp_buf_puts(&b, ",\"connection\":");
|
|
apex_json_str(&b, connection);
|
|
mcp_buf_puts(&b, ",\"accept\":");
|
|
apex_json_str(&b, accept);
|
|
mcp_buf_puts(&b, "}");
|
|
apex_audit("websocket_probe", target, port, code == 101,
|
|
code == 101 ? "WebSocket upgrade accepted"
|
|
: "no WebSocket upgrade", path);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
free(resp);
|
|
}
|
|
|
|
/* ---------------------------------------------------------- openapi_discovery */
|
|
|
|
static int openapi_path_count(const char *body)
|
|
{
|
|
const char *p = strstr(body, "\"paths\"");
|
|
int depth;
|
|
int in_string = 0;
|
|
int esc = 0;
|
|
int count = 0;
|
|
|
|
if (p == NULL)
|
|
return 0;
|
|
p = strchr(p, '{');
|
|
if (p == NULL)
|
|
return 0;
|
|
p++;
|
|
depth = 1;
|
|
while (*p != '\0' && depth > 0) {
|
|
char c = *p++;
|
|
|
|
if (in_string) {
|
|
if (esc)
|
|
esc = 0;
|
|
else if (c == '\\')
|
|
esc = 1;
|
|
else if (c == '"') {
|
|
in_string = 0;
|
|
if (depth == 1) {
|
|
const char *q = p;
|
|
|
|
while (*q == ' ' || *q == '\t' || *q == '\n' ||
|
|
*q == '\r')
|
|
q++;
|
|
if (*q == ':')
|
|
count++;
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
if (c == '"')
|
|
in_string = 1;
|
|
else if (c == '{')
|
|
depth++;
|
|
else if (c == '}')
|
|
depth--;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
static void t_openapi_discovery(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
static const char *paths[] = {
|
|
"/openapi.json", "/swagger.json", "/api-docs", "/v3/api-docs",
|
|
"/swagger/v1/swagger.json", "/api/openapi.json", NULL
|
|
};
|
|
const char *target = mcp_arg_string(args, "target");
|
|
int port = mcp_arg_int(args, "port", 80);
|
|
int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port));
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int found = 0;
|
|
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, "openapi_discovery", target, port))
|
|
return;
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"specs\":[", port);
|
|
for (i = 0; paths[i] != NULL; i++) {
|
|
char status[256];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
char version[64];
|
|
char title[128];
|
|
int code;
|
|
int paths_found;
|
|
char *evidence;
|
|
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, tls, "GET", paths[i], NULL, NULL,
|
|
8000, &resp, &len, status, sizeof(status)) != 0)
|
|
continue;
|
|
code = apex_status_code(status);
|
|
if (code != 200 || resp == NULL ||
|
|
(strstr(resp, "\"openapi\"") == NULL &&
|
|
strstr(resp, "\"swagger\"") == NULL) ||
|
|
strstr(resp, "\"paths\"") == NULL) {
|
|
free(resp);
|
|
continue;
|
|
}
|
|
version[0] = title[0] = '\0';
|
|
json_field(resp, "openapi", version, sizeof(version));
|
|
if (version[0] == '\0')
|
|
json_field(resp, "swagger", version, sizeof(version));
|
|
json_field(resp, "title", title, sizeof(title));
|
|
paths_found = openapi_path_count(resp);
|
|
evidence = apex_evidence_save("openapi", resp, len);
|
|
found++;
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"path\":");
|
|
apex_json_str(&b, paths[i]);
|
|
mcp_buf_puts(&b, ",\"version\":");
|
|
apex_json_str(&b, version);
|
|
mcp_buf_puts(&b, ",\"title\":");
|
|
apex_json_str(&b, title);
|
|
mcp_buf_printf(&b, ",\"paths\":%d,\"evidence\":", paths_found);
|
|
if (evidence != NULL)
|
|
apex_json_str(&b, evidence);
|
|
else
|
|
mcp_buf_puts(&b, "null");
|
|
mcp_buf_puts(&b, "}");
|
|
free(evidence);
|
|
free(resp);
|
|
}
|
|
mcp_buf_printf(&b, "],\"count\":%d}", found);
|
|
apex_audit("openapi_discovery", target, port, found == 0,
|
|
found == 0 ? "no OpenAPI document" : "OpenAPI document exposed",
|
|
"spec paths");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ----------------------------------------------------------------- csrf_audit */
|
|
|
|
static const char *find_ci(const char *hay, const char *end, const char *needle)
|
|
{
|
|
size_t nl = strlen(needle);
|
|
const char *p;
|
|
|
|
for (p = hay; p + nl <= end; p++)
|
|
if (strncasecmp(p, needle, nl) == 0)
|
|
return p;
|
|
return NULL;
|
|
}
|
|
|
|
static void html_attr(const char *start, const char *end, const char *name,
|
|
char *out, size_t n)
|
|
{
|
|
size_t nl = strlen(name);
|
|
const char *p = start;
|
|
size_t w = 0;
|
|
|
|
out[0] = '\0';
|
|
while (p < end) {
|
|
const char *hit = find_ci(p, end, name);
|
|
|
|
if (hit == NULL)
|
|
return;
|
|
p = hit + nl;
|
|
if (p >= end || *p != '=')
|
|
continue;
|
|
p++;
|
|
while (p < end && (*p == ' ' || *p == '\t'))
|
|
p++;
|
|
if (p < end && (*p == '"' || *p == '\'')) {
|
|
char quote = *p++;
|
|
|
|
while (p < end && *p != quote && w + 1 < n)
|
|
out[w++] = *p++;
|
|
out[w] = '\0';
|
|
return;
|
|
}
|
|
while (p < end && *p != ' ' && *p != '\t' && *p != '>' &&
|
|
w + 1 < n)
|
|
out[w++] = *p++;
|
|
out[w] = '\0';
|
|
return;
|
|
}
|
|
}
|
|
|
|
static void t_csrf_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
const char *path = mcp_arg_string(args, "path");
|
|
int port = mcp_arg_int(args, "port", 80);
|
|
int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port));
|
|
char status[256];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
int code = 0;
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int forms = 0;
|
|
int tokenless = 0;
|
|
char *evidence = NULL;
|
|
const char *p;
|
|
const char *end;
|
|
|
|
(void)user;
|
|
if (target == NULL || *target == '\0') {
|
|
mcp_reply_error(c, "target is required");
|
|
return;
|
|
}
|
|
if (path == NULL || *path == '\0')
|
|
path = "/";
|
|
if (path[0] != '/') {
|
|
mcp_reply_error(c, "path must start with /");
|
|
return;
|
|
}
|
|
if (!gated(c, "csrf_audit", target, port))
|
|
return;
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, tls, "GET", path, NULL, NULL, 8000,
|
|
&resp, &len, status, sizeof(status)) != 0) {
|
|
mcp_reply_error(c, "cannot fetch the page");
|
|
return;
|
|
}
|
|
code = apex_status_code(status);
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"path\":", port);
|
|
apex_json_str(&b, path);
|
|
mcp_buf_printf(&b, ",\"status\":%d,\"forms\":[", code);
|
|
end = resp + len;
|
|
p = resp;
|
|
while (forms < 20) {
|
|
const char *fs = find_ci(p, end, "<form");
|
|
const char *fe;
|
|
char action[256];
|
|
char method[16];
|
|
int inputs = 0;
|
|
int has_token = 0;
|
|
const char *ip;
|
|
|
|
if (fs == NULL)
|
|
break;
|
|
fe = find_ci(fs, end, "</form>");
|
|
if (fe == NULL)
|
|
fe = end;
|
|
html_attr(fs, fe, "action", action, sizeof(action));
|
|
html_attr(fs, fe, "method", method, sizeof(method));
|
|
if (method[0] == '\0')
|
|
snprintf(method, sizeof(method), "%s", "GET");
|
|
ip = fs;
|
|
while (ip < fe) {
|
|
const char *in = find_ci(ip, fe, "<input");
|
|
|
|
if (in == NULL)
|
|
break;
|
|
inputs++;
|
|
{
|
|
const char *ie = find_ci(in, fe, ">");
|
|
char name[128];
|
|
|
|
if (ie == NULL)
|
|
ie = fe;
|
|
html_attr(in, ie, "name", name, sizeof(name));
|
|
if (strcasestr(name, "csrf") != NULL ||
|
|
strcasestr(name, "token") != NULL ||
|
|
strcasestr(name, "nonce") != NULL)
|
|
has_token = 1;
|
|
ip = ie + 1;
|
|
}
|
|
}
|
|
forms++;
|
|
if (!has_token)
|
|
tokenless++;
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"action\":");
|
|
apex_json_str(&b, action);
|
|
mcp_buf_puts(&b, ",\"method\":");
|
|
apex_json_str(&b, method);
|
|
mcp_buf_printf(&b, ",\"inputs\":%d,\"has_token\":%s}", inputs,
|
|
has_token ? "true" : "false");
|
|
p = fe;
|
|
}
|
|
if (tokenless > 0)
|
|
evidence = apex_evidence_save("csrf", resp, len);
|
|
mcp_buf_printf(&b, "],\"form_count\":%d,\"tokenless\":%d,\"evidence\":",
|
|
forms, tokenless);
|
|
if (evidence != NULL)
|
|
apex_json_str(&b, evidence);
|
|
else
|
|
mcp_buf_puts(&b, "null");
|
|
mcp_buf_puts(&b, "}");
|
|
apex_audit("csrf_audit", target, port, tokenless == 0,
|
|
tokenless == 0 ? "forms carry a token"
|
|
: "forms without a CSRF token", path);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
free(evidence);
|
|
free(resp);
|
|
}
|
|
|
|
/* ------------------------------------------------------------ api_auth_audit */
|
|
|
|
static void t_api_auth_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
const char *endpoints_arg = mcp_arg_string(args, "endpoints");
|
|
int port = mcp_arg_int(args, "port", 80);
|
|
int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port));
|
|
char paths[20][256];
|
|
int npaths = 0;
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int tested = 0;
|
|
int open_count = 0;
|
|
int saved = 0;
|
|
int i;
|
|
|
|
(void)user;
|
|
if (target == NULL || *target == '\0' || endpoints_arg == NULL ||
|
|
*endpoints_arg == '\0') {
|
|
mcp_reply_error(c, "target and endpoints are required");
|
|
return;
|
|
}
|
|
{
|
|
char *copy = strdup(endpoints_arg);
|
|
char *save = NULL;
|
|
char *tok;
|
|
|
|
if (copy == NULL) {
|
|
mcp_reply_error(c, "out of memory");
|
|
return;
|
|
}
|
|
tok = strtok_r(copy, ",", &save);
|
|
while (tok != NULL && npaths < 20) {
|
|
while (*tok == ' ' || *tok == '\t')
|
|
tok++;
|
|
if (*tok != '\0') {
|
|
const char *p = *tok == '/' ? "" : "/";
|
|
|
|
snprintf(paths[npaths], sizeof(paths[0]), "%s%.200s", p,
|
|
tok);
|
|
npaths++;
|
|
}
|
|
tok = strtok_r(NULL, ",", &save);
|
|
}
|
|
free(copy);
|
|
}
|
|
if (npaths == 0) {
|
|
mcp_reply_error(c, "no endpoint paths given");
|
|
return;
|
|
}
|
|
if (!gated(c, "api_auth_audit", target, port))
|
|
return;
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"endpoints\":[", port);
|
|
for (i = 0; i < npaths; i++) {
|
|
char status[256];
|
|
char content_type[160];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
int code;
|
|
int open;
|
|
int auth_required;
|
|
int json;
|
|
char *evidence = NULL;
|
|
|
|
status[0] = '\0';
|
|
content_type[0] = '\0';
|
|
if (apex_http_once(target, port, tls, "GET", paths[i], NULL, NULL,
|
|
8000, &resp, &len, status,
|
|
sizeof(status)) != 0)
|
|
continue;
|
|
tested++;
|
|
code = apex_status_code(status);
|
|
response_header(resp, len, "Content-Type", content_type,
|
|
sizeof(content_type));
|
|
open = code >= 200 && code < 300;
|
|
auth_required = code == 401 || code == 403;
|
|
json = strcasestr(content_type, "json") != NULL;
|
|
if (open) {
|
|
open_count++;
|
|
if (saved < 3) {
|
|
char label[64];
|
|
|
|
snprintf(label, sizeof(label), "api-%.40s", paths[i]);
|
|
evidence = apex_evidence_save(label, resp, len);
|
|
saved++;
|
|
}
|
|
}
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"path\":");
|
|
apex_json_str(&b, paths[i]);
|
|
mcp_buf_printf(&b, ",\"status\":%d,\"bytes\":%lu,\"open\":%s,"
|
|
"\"auth_required\":%s,\"json\":%s,\"evidence\":",
|
|
code, (unsigned long)len, open ? "true" : "false",
|
|
auth_required ? "true" : "false",
|
|
json ? "true" : "false");
|
|
if (evidence != NULL)
|
|
apex_json_str(&b, evidence);
|
|
else
|
|
mcp_buf_puts(&b, "null");
|
|
mcp_buf_puts(&b, "}");
|
|
free(evidence);
|
|
free(resp);
|
|
}
|
|
mcp_buf_printf(&b, "],\"tested\":%d,\"open_count\":%d}", tested,
|
|
open_count);
|
|
apex_audit("api_auth_audit", target, port, open_count == 0,
|
|
open_count == 0 ? "endpoints require auth or are absent"
|
|
: "endpoints answer without auth", "list");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- cache_audit */
|
|
|
|
typedef struct {
|
|
int status;
|
|
char cache_control[256];
|
|
char pragma[64];
|
|
char age[32];
|
|
char vary[128];
|
|
char xcache[64];
|
|
} cache_info;
|
|
|
|
static void cache_probe(const char *target, int port, int tls,
|
|
const char *path, cache_info *ci)
|
|
{
|
|
char status[256];
|
|
char *resp = NULL;
|
|
size_t len = 0;
|
|
|
|
memset(ci, 0, sizeof(*ci));
|
|
status[0] = '\0';
|
|
if (apex_http_once(target, port, tls, "GET", path, NULL, NULL, 8000,
|
|
&resp, &len, status, sizeof(status)) != 0)
|
|
return;
|
|
ci->status = apex_status_code(status);
|
|
response_header(resp, len, "Cache-Control", ci->cache_control,
|
|
sizeof(ci->cache_control));
|
|
response_header(resp, len, "Pragma", ci->pragma, sizeof(ci->pragma));
|
|
response_header(resp, len, "Age", ci->age, sizeof(ci->age));
|
|
response_header(resp, len, "Vary", ci->vary, sizeof(ci->vary));
|
|
response_header(resp, len, "X-Cache", ci->xcache, sizeof(ci->xcache));
|
|
free(resp);
|
|
}
|
|
|
|
static int cache_is_cacheable(const cache_info *ci)
|
|
{
|
|
const char *m = strstr(ci->cache_control, "max-age=");
|
|
|
|
if (strcasestr(ci->cache_control, "public") != NULL)
|
|
return 1;
|
|
if (m != NULL && atoi(m + 8) > 0)
|
|
return 1;
|
|
if (strcasestr(ci->cache_control, "no-store") != NULL)
|
|
return 0;
|
|
if (atoi(ci->age) > 0)
|
|
return 1;
|
|
if (strcasestr(ci->xcache, "hit") != NULL)
|
|
return 1;
|
|
return 0;
|
|
}
|
|
|
|
static void t_cache_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
const char *path = mcp_arg_string(args, "path");
|
|
const char *param = mcp_arg_string(args, "param");
|
|
int port = mcp_arg_int(args, "port", 80);
|
|
int tls = mcp_arg_bool(args, "tls", apex_looks_tls(port));
|
|
char req[1200];
|
|
char marker[64];
|
|
cache_info clean;
|
|
cache_info param_req;
|
|
int cacheable;
|
|
mcp_buf b;
|
|
int findings = 0;
|
|
|
|
(void)user;
|
|
if (target == NULL || *target == '\0') {
|
|
mcp_reply_error(c, "target is required");
|
|
return;
|
|
}
|
|
if (path == NULL || *path == '\0')
|
|
path = "/";
|
|
if (path[0] != '/') {
|
|
mcp_reply_error(c, "path must start with /");
|
|
return;
|
|
}
|
|
if (param == NULL || *param == '\0')
|
|
param = "q";
|
|
if (!gated(c, "cache_audit", target, port))
|
|
return;
|
|
snprintf(marker, sizeof(marker), "apex%ld", (long)getpid());
|
|
cache_probe(target, port, tls, path, &clean);
|
|
snprintf(req, sizeof(req), "%s%s%s=%s", path,
|
|
strchr(path, '?') != NULL ? "&" : "?", param, marker);
|
|
cache_probe(target, port, tls, req, ¶m_req);
|
|
cacheable = cache_is_cacheable(¶m_req);
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"target\":");
|
|
apex_json_str(&b, target);
|
|
mcp_buf_printf(&b, ",\"port\":%d,\"path\":", port);
|
|
apex_json_str(&b, path);
|
|
mcp_buf_puts(&b, ",\"param\":");
|
|
apex_json_str(&b, param);
|
|
mcp_buf_puts(&b, ",\"clean\":{\"status\":");
|
|
mcp_buf_printf(&b, "%d,\"cache_control\":", clean.status);
|
|
apex_json_str(&b, clean.cache_control);
|
|
mcp_buf_puts(&b, ",\"age\":");
|
|
apex_json_str(&b, clean.age);
|
|
mcp_buf_puts(&b, ",\"vary\":");
|
|
apex_json_str(&b, clean.vary);
|
|
mcp_buf_puts(&b, "},\"parameterized\":{\"status\":");
|
|
mcp_buf_printf(&b, "%d,\"cache_control\":", param_req.status);
|
|
apex_json_str(&b, param_req.cache_control);
|
|
mcp_buf_puts(&b, ",\"age\":");
|
|
apex_json_str(&b, param_req.age);
|
|
mcp_buf_puts(&b, ",\"vary\":");
|
|
apex_json_str(&b, param_req.vary);
|
|
mcp_buf_printf(&b, "},\"cacheable\":%s,\"findings\":[",
|
|
cacheable ? "true" : "false");
|
|
if (cacheable) {
|
|
findings++;
|
|
mcp_buf_puts(&b, "{\"id\":\"parameterized-cacheable\","
|
|
"\"severity\":\"medium\",\"detail\":"
|
|
"\"a response that takes a parameter is cacheable\"}");
|
|
}
|
|
if (cacheable && param_req.vary[0] == '\0') {
|
|
if (findings > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
findings++;
|
|
mcp_buf_puts(&b, "{\"id\":\"missing-vary\",\"severity\":\"low\","
|
|
"\"detail\":\"cacheable response has no Vary header\"}");
|
|
}
|
|
mcp_buf_printf(&b, "],\"findings_count\":%d}", findings);
|
|
apex_audit("cache_audit", target, port, !cacheable,
|
|
cacheable ? "parameterized response is cacheable"
|
|
: "no parameterized caching", path);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ------------------------------------------------------- email_security_audit */
|
|
|
|
static char *email_dig(const char *name, const char *type,
|
|
const char *resolver, int port)
|
|
{
|
|
char portstr[16];
|
|
char at_server[300];
|
|
char *argv[12];
|
|
int i = 0;
|
|
apex_proc p;
|
|
char *out = NULL;
|
|
|
|
argv[i++] = "dig";
|
|
argv[i++] = "+short";
|
|
argv[i++] = "+time=3";
|
|
argv[i++] = "+tries=1";
|
|
if (port > 0) {
|
|
snprintf(portstr, sizeof(portstr), "%d", port);
|
|
argv[i++] = "-p";
|
|
argv[i++] = portstr;
|
|
}
|
|
if (resolver != NULL && *resolver != '\0') {
|
|
snprintf(at_server, sizeof(at_server), "@%.200s", resolver);
|
|
argv[i++] = at_server;
|
|
}
|
|
argv[i++] = (char *)name;
|
|
argv[i++] = (char *)type;
|
|
argv[i] = NULL;
|
|
if (apex_run(argv, 15000, g_cfg.max_output, &p) != 0)
|
|
return NULL;
|
|
if (p.exit_code == 0 && p.out != NULL && p.out_len > 0) {
|
|
out = strdup(p.out);
|
|
}
|
|
apex_proc_free(&p);
|
|
return out;
|
|
}
|
|
|
|
static void email_record_copy(const char *text, char *out, size_t n)
|
|
{
|
|
size_t w = 0;
|
|
|
|
out[0] = '\0';
|
|
while (*text == '"' || *text == ' ' || *text == '\t')
|
|
text++;
|
|
while (*text != '\0' && *text != '\n' && *text != '\r' &&
|
|
*text != '"' && w + 1 < n)
|
|
out[w++] = *text++;
|
|
out[w] = '\0';
|
|
}
|
|
|
|
static void email_finding(mcp_buf *b, int *first, const char *id,
|
|
const char *severity, const char *detail)
|
|
{
|
|
if (!*first)
|
|
mcp_buf_puts(b, ",");
|
|
*first = 0;
|
|
mcp_buf_puts(b, "{\"id\":");
|
|
apex_json_str(b, id);
|
|
mcp_buf_puts(b, ",\"severity\":");
|
|
apex_json_str(b, severity);
|
|
mcp_buf_puts(b, ",\"detail\":");
|
|
apex_json_str(b, detail);
|
|
mcp_buf_puts(b, "}");
|
|
}
|
|
|
|
static void t_email_security_audit(mcp_conn *c, const mcp_json *args,
|
|
void *user)
|
|
{
|
|
const char *domain = mcp_arg_string(args, "domain");
|
|
const char *selector = mcp_arg_string(args, "selector");
|
|
const char *resolver = mcp_arg_string(args, "resolver");
|
|
int port = mcp_arg_int(args, "port", 0);
|
|
char dmarc_name[320];
|
|
char dkim_name[400];
|
|
char reason[512];
|
|
char *spf_out = NULL;
|
|
char *dmarc_out = NULL;
|
|
char *dkim_out = NULL;
|
|
char *mx_out = NULL;
|
|
char spf_record[256];
|
|
char dmarc_record[256];
|
|
char dkim_record[256];
|
|
char spf_policy[32];
|
|
char dmarc_policy[32];
|
|
int spf = 0;
|
|
int dmarc = 0;
|
|
int dkim = 0;
|
|
int mx_count = 0;
|
|
char mx_records[10][128];
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int findings = 0;
|
|
int i;
|
|
|
|
(void)user;
|
|
if (domain == NULL || *domain == '\0') {
|
|
mcp_reply_error(c, "domain is required");
|
|
return;
|
|
}
|
|
if (selector == NULL || *selector == '\0')
|
|
selector = "default";
|
|
if (port < 0 || port > 65535) {
|
|
mcp_reply_error(c, "port out of range");
|
|
return;
|
|
}
|
|
if (!apex_scope_check(domain, 0, reason, sizeof(reason))) {
|
|
mcp_buf_init(&b);
|
|
mcp_buf_printf(&b, "REFUSED by scope gate: %s", reason);
|
|
mcp_reply_error(c, b.p);
|
|
mcp_buf_free(&b);
|
|
return;
|
|
}
|
|
if (resolver != NULL && *resolver != '\0' &&
|
|
!apex_scope_check(resolver, port, reason, sizeof(reason))) {
|
|
mcp_buf_init(&b);
|
|
mcp_buf_printf(&b, "resolver is outside the scope: %s", reason);
|
|
mcp_reply_error(c, b.p);
|
|
mcp_buf_free(&b);
|
|
return;
|
|
}
|
|
apex_audit("email_security_audit", domain, 0, 1, "SPF/DMARC/DKIM/MX",
|
|
"dig TXT/MX");
|
|
spf_out = email_dig(domain, "TXT", resolver, port);
|
|
snprintf(dmarc_name, sizeof(dmarc_name), "_dmarc.%.200s", domain);
|
|
dmarc_out = email_dig(dmarc_name, "TXT", resolver, port);
|
|
snprintf(dkim_name, sizeof(dkim_name), "%.40s._domainkey.%.200s",
|
|
selector, domain);
|
|
dkim_out = email_dig(dkim_name, "TXT", resolver, port);
|
|
mx_out = email_dig(domain, "MX", resolver, port);
|
|
spf_record[0] = dmarc_record[0] = dkim_record[0] = '\0';
|
|
spf_policy[0] = dmarc_policy[0] = '\0';
|
|
if (spf_out != NULL && strstr(spf_out, "v=spf1") != NULL) {
|
|
spf = 1;
|
|
email_record_copy(strstr(spf_out, "v=spf1"), spf_record,
|
|
sizeof(spf_record));
|
|
if (strstr(spf_record, "-all") != NULL)
|
|
snprintf(spf_policy, sizeof(spf_policy), "%s", "fail");
|
|
else if (strstr(spf_record, "~all") != NULL)
|
|
snprintf(spf_policy, sizeof(spf_policy), "%s", "softfail");
|
|
else if (strstr(spf_record, "?all") != NULL)
|
|
snprintf(spf_policy, sizeof(spf_policy), "%s", "neutral");
|
|
else if (strstr(spf_record, "+all") != NULL)
|
|
snprintf(spf_policy, sizeof(spf_policy), "%s", "pass");
|
|
else
|
|
snprintf(spf_policy, sizeof(spf_policy), "%s", "none");
|
|
}
|
|
if (dmarc_out != NULL && strstr(dmarc_out, "v=DMARC1") != NULL) {
|
|
const char *p;
|
|
|
|
dmarc = 1;
|
|
email_record_copy(strstr(dmarc_out, "v=DMARC1"), dmarc_record,
|
|
sizeof(dmarc_record));
|
|
p = strstr(dmarc_record, "p=");
|
|
if (p != NULL) {
|
|
size_t w = 0;
|
|
|
|
p += 2;
|
|
while (*p != '\0' && *p != ';' && *p != ' ' &&
|
|
w + 1 < sizeof(dmarc_policy))
|
|
dmarc_policy[w++] = *p++;
|
|
dmarc_policy[w] = '\0';
|
|
}
|
|
}
|
|
if (dkim_out != NULL &&
|
|
(strstr(dkim_out, "v=DKIM1") != NULL ||
|
|
strstr(dkim_out, "p=") != NULL)) {
|
|
dkim = 1;
|
|
email_record_copy(dkim_out, dkim_record, sizeof(dkim_record));
|
|
}
|
|
if (mx_out != NULL) {
|
|
char *copy = strdup(mx_out);
|
|
char *save = NULL;
|
|
char *line;
|
|
|
|
if (copy != NULL) {
|
|
line = strtok_r(copy, "\n", &save);
|
|
while (line != NULL) {
|
|
while (*line == ' ' || *line == '\t')
|
|
line++;
|
|
if (*line != '\0') {
|
|
mx_count++;
|
|
if (mx_count <= 10)
|
|
snprintf(mx_records[mx_count - 1],
|
|
sizeof(mx_records[0]), "%.100s",
|
|
line);
|
|
}
|
|
line = strtok_r(NULL, "\n", &save);
|
|
}
|
|
free(copy);
|
|
}
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"domain\":");
|
|
apex_json_str(&b, domain);
|
|
mcp_buf_puts(&b, ",\"selector\":");
|
|
apex_json_str(&b, selector);
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"domain\":");
|
|
apex_json_str(&b, domain);
|
|
mcp_buf_puts(&b, ",\"selector\":");
|
|
apex_json_str(&b, selector);
|
|
mcp_buf_puts(&b, ",\"resolver\":");
|
|
apex_json_str(&b, resolver != NULL ? resolver : "");
|
|
mcp_buf_puts(&b, ",\"spf\":{\"present\":");
|
|
mcp_buf_printf(&b, "%s,\"record\":", spf ? "true" : "false");
|
|
apex_json_str(&b, spf_record);
|
|
mcp_buf_puts(&b, ",\"policy\":");
|
|
apex_json_str(&b, spf_policy);
|
|
mcp_buf_puts(&b, "},\"dmarc\":{\"present\":");
|
|
mcp_buf_printf(&b, "%s,\"record\":", dmarc ? "true" : "false");
|
|
apex_json_str(&b, dmarc_record);
|
|
mcp_buf_puts(&b, ",\"policy\":");
|
|
apex_json_str(&b, dmarc_policy);
|
|
mcp_buf_puts(&b, "},\"dkim\":{\"present\":");
|
|
mcp_buf_printf(&b, "%s,\"record\":", dkim ? "true" : "false");
|
|
apex_json_str(&b, dkim_record);
|
|
mcp_buf_printf(&b, "},\"mx\":{\"count\":%d,\"records\":[", mx_count);
|
|
for (i = 0; i < mx_count && i < 10; i++) {
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
apex_json_str(&b, mx_records[i]);
|
|
}
|
|
mcp_buf_puts(&b, "]},\"findings\":[");
|
|
first = 1;
|
|
{
|
|
int count = 0;
|
|
|
|
if (!spf) {
|
|
email_finding(&b, &first, "missing-spf", "medium",
|
|
"no SPF record published");
|
|
count++;
|
|
}
|
|
if (spf && strcmp(spf_policy, "pass") == 0) {
|
|
email_finding(&b, &first, "spf-all-pass", "high",
|
|
"SPF ends with +all");
|
|
count++;
|
|
}
|
|
if (!dmarc) {
|
|
email_finding(&b, &first, "missing-dmarc", "medium",
|
|
"no DMARC record published");
|
|
count++;
|
|
}
|
|
if (dmarc && strcmp(dmarc_policy, "none") == 0) {
|
|
email_finding(&b, &first, "dmarc-none", "low",
|
|
"DMARC policy is none");
|
|
count++;
|
|
}
|
|
if (!dkim) {
|
|
email_finding(&b, &first, "missing-dkim", "low",
|
|
"no DKIM record for the selector");
|
|
count++;
|
|
}
|
|
if (mx_count == 0) {
|
|
email_finding(&b, &first, "missing-mx", "low",
|
|
"no MX record published");
|
|
count++;
|
|
}
|
|
findings = count;
|
|
}
|
|
mcp_buf_printf(&b, "],\"findings_count\":%d}", findings);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
free(spf_out);
|
|
free(dmarc_out);
|
|
free(dkim_out);
|
|
free(mx_out);
|
|
}
|
|
|
|
const mcp_tool TOOLS_RED[] = {
|
|
{ "vuln_check",
|
|
"Non-destructive verification of exposed artifacts on one web target: "
|
|
".git/HEAD (a real ref), .env (two or more KEY=value lines; values are "
|
|
"not echoed, only the key names), open directory listing, phpinfo. Each "
|
|
"confirmed hit saves the raw response as evidence and reports its "
|
|
"severity; nothing is exploited and nothing is written. Gate: the target "
|
|
"must be in scope.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 80\"},"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"base path, default /\"},"
|
|
"\"checks\":{\"type\":\"string\",\"description\":\"comma list: git,env,"
|
|
"listing,phpinfo; default all\"},"
|
|
"\"tls\":{\"type\":\"boolean\"}},"
|
|
"\"required\":[\"target\"]}",
|
|
t_vuln_check },
|
|
{ "subdomain_enum",
|
|
"Resolve a built-in list of common subdomains (or names you pass) under "
|
|
"one in-scope domain and report the ones that exist. Every name is "
|
|
"checked by the gate before it is resolved, so a wildcard scope does not "
|
|
"let a lookup escape the engagement. DNS only; no ports are touched.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"domain\":{\"type\":\"string\"},"
|
|
"\"names\":{\"type\":\"string\",\"description\":\"comma list of labels or "
|
|
"full host names; default built-in wordlist\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"wordlist cap, default "
|
|
"50, max 200\"}},"
|
|
"\"required\":[\"domain\"]}",
|
|
t_subdomain_enum },
|
|
{ "cred_test",
|
|
"Try a small default-credential list against one HTTP Basic endpoint. "
|
|
"Refuses a path that answers without authentication, stops on 429 "
|
|
"(rate limit), sleeps 200 ms between attempts, and caps at 20 attempts. "
|
|
"Found credentials are returned so the operator can record the finding; "
|
|
"the audit line never contains the password. Gate: the target must be "
|
|
"in scope.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 80\"},"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"default /\"},"
|
|
"\"users\":{\"type\":\"string\",\"description\":\"comma list, max 12\"},"
|
|
"\"passwords\":{\"type\":\"string\",\"description\":\"comma list, max 12;"
|
|
" at most 20 attempts are made\"},"
|
|
"\"max_attempts\":{\"type\":\"integer\",\"description\":\"default 20, max "
|
|
"40\"},"
|
|
"\"tls\":{\"type\":\"boolean\"}},"
|
|
"\"required\":[\"target\"]}",
|
|
t_cred_test },
|
|
{ "vhost_discovery",
|
|
"Host-header fuzzing against one in-scope IP: requests the base path "
|
|
"with each candidate name as Host (built-in list or names=) and reports "
|
|
"the ones whose status, byte count or <title> differs from the baseline. "
|
|
"Plain HTTP only (TLS SNI would need a resolver override). Each hit "
|
|
"saves the response as evidence; 100 ms between requests.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\",\"description\":\"IP in scope\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 80\"},"
|
|
"\"base_host\":{\"type\":\"string\",\"description\":\"Host header for "
|
|
"the baseline; default the target\"},"
|
|
"\"names\":{\"type\":\"string\",\"description\":\"comma list of vhost "
|
|
"names; default built-in list\"},"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"default /\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"default 40, max "
|
|
"100\"},"
|
|
"\"delay_ms\":{\"type\":\"integer\",\"description\":\"default "
|
|
"100\"}},"
|
|
"\"required\":[\"target\"]}",
|
|
t_vhost_discovery },
|
|
{ "http_brute",
|
|
"Probe a built-in path list, your own comma list, or a wordlist file for "
|
|
"hidden endpoints. Hits are 200/201/204/301/302/307/308/401/403; 404 is "
|
|
"ignored. 100 ms between requests, default 40 paths, max 200; the first "
|
|
"five 200/201 responses are saved as evidence. Gate: target in scope.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 80\"},"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"base path, default "
|
|
"/\"},"
|
|
"\"words\":{\"type\":\"string\",\"description\":\"comma list; wins "
|
|
"over wordlist\"},"
|
|
"\"wordlist\":{\"type\":\"string\",\"description\":\"file with one "
|
|
"path per line, # comments allowed\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"default 40, max "
|
|
"200\"},"
|
|
"\"delay_ms\":{\"type\":\"integer\",\"description\":\"default "
|
|
"100\"},"
|
|
"\"tls\":{\"type\":\"boolean\"}},"
|
|
"\"required\":[\"target\"]}",
|
|
t_http_brute },
|
|
{ "ssrf_probe",
|
|
"Inject one parameter with a URL that points at a callback: by default "
|
|
"a loopback listener this process opens (verified: a request arriving "
|
|
"there proves the target fetched it), or callback_base when the target "
|
|
"must reach an external collector (then only 'sent' is reported). Tries "
|
|
"127.0.0.1, localhost and 127.1 with unique tokens. GET query or POST "
|
|
"form. Only fetches to your own collector, never to metadata endpoints.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 80\"},"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"endpoint, default "
|
|
"/\"},"
|
|
"\"param\":{\"type\":\"string\",\"description\":\"parameter that "
|
|
"takes a URL\"},"
|
|
"\"method\":{\"type\":\"string\",\"enum\":[\"GET\",\"POST\"],"
|
|
"\"description\":\"default GET\"},"
|
|
"\"callback_base\":{\"type\":\"string\",\"description\":\"external "
|
|
"collector base URL; omit for the loopback listener\"},"
|
|
"\"wait_ms\":{\"type\":\"integer\",\"description\":\"callback wait, "
|
|
"default 1200\"},"
|
|
"\"tls\":{\"type\":\"boolean\"}},"
|
|
"\"required\":[\"target\",\"param\"]}",
|
|
t_ssrf_probe },
|
|
{ "jwt_audit",
|
|
"Audit a JWT offline: decode header and payload, report alg/typ/kid and "
|
|
"the claims iss/aud/sub/exp/iat/nbf, and flag alg=none, a missing exp, an "
|
|
"expired token, a path-like kid, a missing audience/issuer and a validity "
|
|
"longer than a year. With secret= it verifies an HS256 signature "
|
|
"(HMAC-SHA256) and says valid or invalid; nothing is sent anywhere.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"token\":{\"type\":\"string\"},"
|
|
"\"secret\":{\"type\":\"string\",\"description\":\"optional HS256 "
|
|
"secret for signature verification\"}},"
|
|
"\"required\":[\"token\"]}",
|
|
t_jwt_audit },
|
|
{ "hash_id",
|
|
"Identify a hash string by shape: MD5/NTLM, SHA-1, SHA-256, SHA-512, "
|
|
"bcrypt, Argon2id/Argon2i, md5crypt/sha256crypt/sha512crypt, or a JWT. "
|
|
"Reports candidates and a note; it does not crack anything.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"value\":{\"type\":\"string\"}},"
|
|
"\"required\":[\"value\"]}",
|
|
t_hash_id },
|
|
{ "param_fuzz",
|
|
"Inject three benign probes per parameter (a unique marker, a single "
|
|
"quote and {{1337*7}}) through GET query or POST form and report what "
|
|
"comes back: marker reflection, SQL error strings, stack traces, "
|
|
"template errors and template evaluation (9359). 100 ms apart, 10 "
|
|
"parameters max, first three hits saved as evidence. It proves handling "
|
|
"signals; it does not extract data. Gate: the target must be in scope.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 80\"},"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"default /\"},"
|
|
"\"params\":{\"type\":\"string\",\"description\":\"comma list, max 10\"},"
|
|
"\"method\":{\"type\":\"string\",\"enum\":[\"GET\",\"POST\"],"
|
|
"\"description\":\"default GET\"},"
|
|
"\"delay_ms\":{\"type\":\"integer\",\"description\":\"default 100\"},"
|
|
"\"tls\":{\"type\":\"boolean\"}},"
|
|
"\"required\":[\"target\",\"params\"]}",
|
|
t_param_fuzz },
|
|
{ "open_redirect",
|
|
"Send an external marker URL and a protocol-relative variant through one "
|
|
"redirect parameter and check the response Location header (or a meta "
|
|
"refresh) for the marker host. Reports each variant with status and "
|
|
"location, saves a confirmed hit as evidence. Gate: the target must be "
|
|
"in scope.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 80\"},"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"default /\"},"
|
|
"\"param\":{\"type\":\"string\",\"description\":\"default next\"},"
|
|
"\"external\":{\"type\":\"string\",\"description\":\"marker URL, "
|
|
"default https://example.com/apex-redirect\"},"
|
|
"\"tls\":{\"type\":\"boolean\"}},"
|
|
"\"required\":[\"target\"]}",
|
|
t_open_redirect },
|
|
{ "host_header_injection",
|
|
"Send a canary as Host (plain HTTP) and as X-Forwarded-Host, then report "
|
|
"whether it appears in the body or in a Location header, with the "
|
|
"baseline status and size for comparison. Each hit is saved as evidence. "
|
|
"Gate: the target must be in scope.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 80\"},"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"default /\"},"
|
|
"\"canary\":{\"type\":\"string\",\"description\":\"default "
|
|
"apex-canary.invalid\"},"
|
|
"\"tls\":{\"type\":\"boolean\"}},"
|
|
"\"required\":[\"target\"]}",
|
|
t_host_header_injection },
|
|
{ "ssti_probe",
|
|
"Send five template payloads ({{1337*7}}, ${1337*7}, #{1337*7}, "
|
|
"<%= 1337*7 %>, {{1337*'7'}}) through one parameter and check for the "
|
|
"evaluated result (9359 or a repeated 1337), a reflected payload or a "
|
|
"template error. A baseline request first clears pages that already "
|
|
"contain the numbers. GET query or POST form, evaluations saved as "
|
|
"evidence. Gate: the target must be in scope.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 80\"},"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"default /\"},"
|
|
"\"param\":{\"type\":\"string\"},"
|
|
"\"method\":{\"type\":\"string\",\"enum\":[\"GET\",\"POST\"],"
|
|
"\"description\":\"default GET\"},"
|
|
"\"delay_ms\":{\"type\":\"integer\",\"description\":\"default 100\"},"
|
|
"\"tls\":{\"type\":\"boolean\"}},"
|
|
"\"required\":[\"target\",\"param\"]}",
|
|
t_ssti_probe },
|
|
{ "xxe_probe",
|
|
"POST an XML body with a DOCTYPE external entity that points at a "
|
|
"loopback listener this process opens, in element and attribute form. A "
|
|
"callback proves the parser resolved the entity; the request line is "
|
|
"saved as evidence. Optionally wrap the XML in a form field. Also "
|
|
"reports parser error signatures and entity reflection. Gate: the target "
|
|
"must be in scope.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 80\"},"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"default /\"},"
|
|
"\"post_field\":{\"type\":\"string\",\"description\":\"form field "
|
|
"that carries the XML; default raw body\"},"
|
|
"\"wait_ms\":{\"type\":\"integer\",\"description\":\"default 1200\"},"
|
|
"\"tls\":{\"type\":\"boolean\"}},"
|
|
"\"required\":[\"target\"]}",
|
|
t_xxe_probe },
|
|
{ "graphql_probe",
|
|
"POST a compact introspection query to one endpoint and report whether "
|
|
"the schema is exposed: type count, query and mutation type names, the "
|
|
"HTTP status, and a reason when introspection is disabled or the "
|
|
"endpoint is missing. A successful schema dump is saved as evidence. "
|
|
"Gate: the target must be in scope.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 80\"},"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"default /graphql\"},"
|
|
"\"tls\":{\"type\":\"boolean\"}},"
|
|
"\"required\":[\"target\"]}",
|
|
t_graphql_probe },
|
|
{ "docker_api_probe",
|
|
"Query the Docker Engine API without credentials: /version (engine "
|
|
"version, API version, OS) and /containers/json?all=1 (container count, "
|
|
"running count, images). An open API is audited and the version reply is "
|
|
"saved as evidence; 401/403 is reported as protected. Plain HTTP (2375), "
|
|
"one or two GETs. Gate: the target must be in scope.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 2375\"}},"
|
|
"\"required\":[\"target\"]}",
|
|
t_docker_api_probe },
|
|
{ "elasticsearch_probe",
|
|
"Query an Elasticsearch HTTP API without credentials: GET / (cluster "
|
|
"name, version, tagline), /_cluster/health (status, node count) and "
|
|
"/_cat/indices (index count). An open API is audited and the root reply "
|
|
"is saved as evidence; 401/403 is reported as protected. Gate: the "
|
|
"target must be in scope.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 9200\"}},"
|
|
"\"required\":[\"target\"]}",
|
|
t_elasticsearch_probe },
|
|
{ "http_methods",
|
|
"Send OPTIONS and TRACE to one path and report the Allow list, the Server "
|
|
"header and whether TRACE echoes the request (cross-site tracing). It "
|
|
"names PUT/DELETE if Allow advertises them but never sends a write "
|
|
"method. Gate: the target must be in scope.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 80\"},"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"default /\"},"
|
|
"\"tls\":{\"type\":\"boolean\"}},"
|
|
"\"required\":[\"target\"]}",
|
|
t_http_methods },
|
|
{ "websocket_probe",
|
|
"Send a WebSocket upgrade handshake (random Sec-WebSocket-Key, version "
|
|
"13) and report whether the server answers 101 with Upgrade: websocket, "
|
|
"Connection: Upgrade and a Sec-WebSocket-Accept value. No frame is sent. "
|
|
"Gate: the target must be in scope.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 80\"},"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"default /\"},"
|
|
"\"tls\":{\"type\":\"boolean\"}},"
|
|
"\"required\":[\"target\"]}",
|
|
t_websocket_probe },
|
|
{ "openapi_discovery",
|
|
"Request the usual OpenAPI/Swagger locations and report every document "
|
|
"that comes back (version, info title, number of paths) with the body as "
|
|
"evidence. Six GETs, no credentials. Gate: the target must be in scope.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 80\"},"
|
|
"\"tls\":{\"type\":\"boolean\"}},"
|
|
"\"required\":[\"target\"]}",
|
|
t_openapi_discovery },
|
|
{ "csrf_audit",
|
|
"Fetch one page, list its forms (action, method, input count) and flag the "
|
|
"ones without an input whose name contains csrf, token or nonce. The page "
|
|
"is saved as evidence when a tokenless form is found. One GET. Gate: the "
|
|
"target must be in scope.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 80\"},"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"default /\"},"
|
|
"\"tls\":{\"type\":\"boolean\"}},"
|
|
"\"required\":[\"target\"]}",
|
|
t_csrf_audit },
|
|
{ "api_auth_audit",
|
|
"Request a list of API endpoints without credentials and report each "
|
|
"status, size, whether it answered 2xx, whether auth is demanded (401/"
|
|
"403) and whether the answer is JSON; open responses are saved as "
|
|
"evidence. One GET per endpoint. Gate: the target must be in scope.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 80\"},"
|
|
"\"endpoints\":{\"type\":\"string\",\"description\":\"comma list, "
|
|
"max 20\"},"
|
|
"\"tls\":{\"type\":\"boolean\"}},"
|
|
"\"required\":[\"target\",\"endpoints\"]}",
|
|
t_api_auth_audit },
|
|
{ "cache_audit",
|
|
"Fetch a path twice (clean and with a marker parameter) and compare "
|
|
"Cache-Control, Pragma, Age, Vary, X-Cache: flags a parameterized "
|
|
"response that is cacheable and a cacheable response without Vary. Two "
|
|
"GETs, no credentials. Gate: the target must be in scope.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"default 80\"},"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"default /\"},"
|
|
"\"param\":{\"type\":\"string\",\"description\":\"default q\"},"
|
|
"\"tls\":{\"type\":\"boolean\"}},"
|
|
"\"required\":[\"target\"]}",
|
|
t_cache_audit },
|
|
{ "email_security_audit",
|
|
"Check a domain's e-mail security DNS: SPF (v=spf1, -all/~all/?all/+all "
|
|
"policy), DMARC (_dmarc TXT, p= policy), DKIM (selector._domainkey TXT) "
|
|
"and MX records, queried with gated dig. Reports each record and the "
|
|
"gaps, e.g. missing SPF/DMARC or +all. Gate: domain (and resolver).",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"domain\":{\"type\":\"string\"},"
|
|
"\"selector\":{\"type\":\"string\",\"description\":\"default "
|
|
"default\"},"
|
|
"\"resolver\":{\"type\":\"string\",\"description\":\"optional DNS "
|
|
"server\"},"
|
|
"\"port\":{\"type\":\"integer\",\"description\":\"dig -p, default "
|
|
"0 (system resolver)\"}},"
|
|
"\"required\":[\"domain\"]}",
|
|
t_email_security_audit },
|
|
};
|
|
|
|
const size_t TOOLS_RED_N = sizeof(TOOLS_RED) / sizeof(TOOLS_RED[0]);
|