Red (tools_red.c): - vhost_discovery: Host-header fuzzing against one in-scope IP. Baseline status/bytes/title, then each built-in or caller-supplied name; a status change, a >24-byte difference or a different <title> is a hit and its response is saved as evidence. Plain HTTP only; TLS SNI would need a resolver override. 100 ms between names, 100 names max. - http_brute: hidden-path probing from the built-in list, a comma list, or a wordlist file (one path per line, # comments). Hits 200/201/204/301/302/307/ 308/401/403; 100 ms apart, 40 paths default, 200 max; the first five 200/201 bodies become evidence. Blue (tools_blue.c): - web_log_audit: nginx/apache access log. 2xx/3xx/4xx/5xx split, top source IPs, scanner user agents (sqlmap, nikto, nmap, gobuster, nuclei, hydra, ...), SQLi/traversal/XSS patterns after percent-decoding the URI (one entry per pattern) and IPs with a 4xx/5xx burst. Path limited to /var/log and the state dir. Tests: Host-routing fixture (admin/api/www), path-routing fixture with words and a wordlist file, and a synthetic access log with encoded SQLi, traversal, scanner UAs and a 4xx burst. 456 checks, ASan suite green, analyze clean, release-check green including distcheck 1.2.0.
1075 lines
31 KiB
C
1075 lines
31 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 <netdb.h>
|
|
#include <netinet/in.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);
|
|
}
|
|
|
|
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 },
|
|
};
|
|
|
|
const size_t TOOLS_RED_N = sizeof(TOOLS_RED) / sizeof(TOOLS_RED[0]);
|