Blue: - php_config_audit: /etc/php.ini or /etc/php/*/*/php.ini (root prefix): expose_php, display_errors, allow_url_include/fopen, empty disable_functions, session.cookie_secure Off and world-writable files. - dovecot_audit: dovecot.conf and conf.d: ssl = no, disable_plaintext_auth = no, PLAIN in auth_mechanisms and world-writable drop-ins. - openvpn_audit: server.conf and server/*.conf: missing tls-auth/tls-crypt, BF-CBC/DES/none cipher, SHA1/MD5 HMAC, duplicate-cn and world-writable files. Red: - smtp_starttls_probe: EHLO, STARTTLS, 220 and a real upgrade verified with openssl s_client -starttls smtp; reports the negotiated TLS version and cipher. No authentication and no mail. Tests with vulnerable and hardened PHP/Dovecot/OpenVPN trees and a Python SMTP server that upgrades to TLS plus one that does not. 817 checks, ASan suite green, analyze clean, release-check green including distcheck 1.31.0.
8440 lines
227 KiB
C
8440 lines
227 KiB
C
/*
|
|
* tools_blue.c - the defensive side: posture, logs, indicators.
|
|
*
|
|
* hardening_audit read-only local audit: sshd, sysctl, shadow perms,
|
|
* world-writable dirs in /etc, listening sockets, firewall
|
|
* config, SUID binaries. Scored, with findings.
|
|
* log_audit auth-log brute-force summary: failures per IP, targeted
|
|
* users, accepted logins, threshold flag.
|
|
* ioc_scan walk a directory for common persistence and
|
|
* remote-execution strings (reverse shells, pipes to a
|
|
* shell, setuid chmod, systemd units under /tmp).
|
|
*
|
|
* None of these touch a network or change anything on disk. They are the
|
|
* "did anyone notice" half of an engagement: run them on the host you defend.
|
|
*/
|
|
#include <ctype.h>
|
|
#include <dirent.h>
|
|
#include <errno.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
#include <fcntl.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/xattr.h>
|
|
#include <unistd.h>
|
|
|
|
#include "apex.h"
|
|
#include "tools.h"
|
|
|
|
/* ------------------------------------------------------------- utilities */
|
|
|
|
static int read_text(const char *path, char *buf, size_t cap)
|
|
{
|
|
FILE *f;
|
|
size_t n;
|
|
|
|
if (cap == 0)
|
|
return -1;
|
|
f = fopen(path, "rb");
|
|
if (f == NULL)
|
|
return -1;
|
|
n = fread(buf, 1, cap - 1, f);
|
|
fclose(f);
|
|
buf[n] = '\0';
|
|
return (int)n;
|
|
}
|
|
|
|
static void trim_eol(char *s)
|
|
{
|
|
size_t n = strlen(s);
|
|
|
|
while (n > 0 && (s[n - 1] == '\n' || s[n - 1] == '\r' ||
|
|
s[n - 1] == ' ' || s[n - 1] == '\t'))
|
|
s[--n] = '\0';
|
|
}
|
|
|
|
static void add_finding(mcp_buf *b, int *first, const char *id,
|
|
const char *severity, const char *fmt, ...)
|
|
{
|
|
va_list ap;
|
|
char detail[512];
|
|
|
|
va_start(ap, fmt);
|
|
vsnprintf(detail, sizeof(detail), fmt, ap);
|
|
va_end(ap);
|
|
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, "}");
|
|
}
|
|
|
|
/* "PermitRootLogin yes" -> value; keys are case-insensitive, # is a comment */
|
|
static int sshd_value(const char *text, const char *key, char *out, size_t n)
|
|
{
|
|
const char *p = text;
|
|
size_t kl = strlen(key);
|
|
|
|
while (p != NULL && *p != '\0') {
|
|
const char *eol = strchr(p, '\n');
|
|
const char *s = p;
|
|
size_t len = eol != NULL ? (size_t)(eol - p) : strlen(p);
|
|
size_t i = 0;
|
|
|
|
while (*s == ' ' || *s == '\t')
|
|
s++;
|
|
if (*s != '#' && (size_t)(p + len - s) > kl &&
|
|
strncasecmp(s, key, kl) == 0 &&
|
|
(s[kl] == ' ' || s[kl] == '\t')) {
|
|
const char *v = s + kl;
|
|
size_t vl;
|
|
|
|
while (*v == ' ' || *v == '\t')
|
|
v++;
|
|
vl = (size_t)(p + len - v);
|
|
if (vl >= n)
|
|
vl = n - 1;
|
|
memcpy(out, v, vl);
|
|
out[vl] = '\0';
|
|
trim_eol(out);
|
|
return 1;
|
|
}
|
|
p = eol != NULL ? eol + 1 : NULL;
|
|
(void)i;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int file_size(const char *path, long *size)
|
|
{
|
|
struct stat st;
|
|
|
|
if (stat(path, &st) != 0)
|
|
return -1;
|
|
*size = (long)st.st_size;
|
|
return 0;
|
|
}
|
|
|
|
static int sysctl_int(const char *path, int dflt)
|
|
{
|
|
char buf[64];
|
|
|
|
if (read_text(path, buf, sizeof(buf)) < 0)
|
|
return dflt;
|
|
return atoi(buf);
|
|
}
|
|
|
|
static int count_world_writable_etc(int *first_list)
|
|
{
|
|
DIR *d = opendir("/etc");
|
|
struct dirent *ent;
|
|
int count = 0;
|
|
|
|
if (d == NULL)
|
|
return 0;
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char path[512];
|
|
struct stat st;
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
snprintf(path, sizeof(path), "/etc/%s", ent->d_name);
|
|
if (lstat(path, &st) != 0 || !S_ISDIR(st.st_mode))
|
|
continue;
|
|
if ((st.st_mode & S_IWOTH) != 0) {
|
|
if (count < 8)
|
|
first_list[count] = 1;
|
|
count++;
|
|
}
|
|
}
|
|
closedir(d);
|
|
return count;
|
|
}
|
|
|
|
static int listening_ports(int ports[64])
|
|
{
|
|
static const char *files[2] = { "/proc/net/tcp", "/proc/net/tcp6" };
|
|
int n = 0;
|
|
int f;
|
|
|
|
for (f = 0; f < 2; f++) {
|
|
FILE *fp = fopen(files[f], "rb");
|
|
char line[512];
|
|
|
|
if (fp == NULL)
|
|
continue;
|
|
if (fgets(line, sizeof(line), fp) == NULL) { /* header */
|
|
fclose(fp);
|
|
continue;
|
|
}
|
|
while (fgets(line, sizeof(line), fp) != NULL) {
|
|
const char *c1 = strchr(line, ':');
|
|
const char *c2 = c1 != NULL ? strchr(c1 + 1, ':') : NULL;
|
|
char *end = NULL;
|
|
unsigned long port;
|
|
int i;
|
|
int dup = 0;
|
|
|
|
if (c2 == NULL || strstr(line, " 0A ") == NULL)
|
|
continue;
|
|
port = strtoul(c2 + 1, &end, 16);
|
|
if (end == NULL || port == 0 || port > 65535)
|
|
continue;
|
|
for (i = 0; i < n; i++)
|
|
if ((int)port == ports[i])
|
|
dup = 1;
|
|
if (!dup && n < 64)
|
|
ports[n++] = (int)port;
|
|
}
|
|
fclose(fp);
|
|
}
|
|
return n;
|
|
}
|
|
|
|
static int suid_binaries(char names[8][64])
|
|
{
|
|
static const char *dirs[] = { "/usr/bin", "/usr/sbin", "/bin", "/sbin",
|
|
NULL };
|
|
int count = 0;
|
|
int d;
|
|
|
|
for (d = 0; dirs[d] != NULL; d++) {
|
|
DIR *dp = opendir(dirs[d]);
|
|
struct dirent *ent;
|
|
|
|
if (dp == NULL)
|
|
continue;
|
|
while ((ent = readdir(dp)) != NULL) {
|
|
char path[512];
|
|
struct stat st;
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
snprintf(path, sizeof(path), "%s/%s", dirs[d], ent->d_name);
|
|
if (lstat(path, &st) != 0 || !S_ISREG(st.st_mode))
|
|
continue;
|
|
if ((st.st_mode & S_ISUID) != 0) {
|
|
if (count < 8) {
|
|
size_t nl = strlen(ent->d_name);
|
|
|
|
if (nl > 63)
|
|
nl = 63;
|
|
memcpy(names[count], ent->d_name, nl);
|
|
names[count][nl] = '\0';
|
|
}
|
|
count++;
|
|
}
|
|
}
|
|
closedir(dp);
|
|
}
|
|
return count;
|
|
}
|
|
|
|
static void t_hardening_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
static char config[65536];
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int score = 100;
|
|
int ww = 0;
|
|
int ww_list[8];
|
|
int ports[64];
|
|
int nports;
|
|
int suid;
|
|
char suid_names[8][64];
|
|
int findings = 0;
|
|
char value[128];
|
|
|
|
(void)args;
|
|
(void)user;
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"findings\":[");
|
|
|
|
if (read_text("/etc/ssh/sshd_config", config, sizeof(config)) > 0) {
|
|
if (sshd_value(config, "PermitRootLogin", value, sizeof(value)) &&
|
|
strcasecmp(value, "yes") == 0) {
|
|
add_finding(&b, &first, "ssh-root-login", "high",
|
|
"sshd_config allows root login");
|
|
score -= 12;
|
|
findings++;
|
|
}
|
|
if (sshd_value(config, "PasswordAuthentication", value,
|
|
sizeof(value)) &&
|
|
strcasecmp(value, "yes") == 0) {
|
|
add_finding(&b, &first, "ssh-password-auth", "medium",
|
|
"sshd_config allows password authentication");
|
|
score -= 6;
|
|
findings++;
|
|
}
|
|
if (sshd_value(config, "PermitEmptyPasswords", value,
|
|
sizeof(value)) &&
|
|
strcasecmp(value, "yes") == 0) {
|
|
add_finding(&b, &first, "ssh-empty-passwords", "high",
|
|
"sshd_config permits empty passwords");
|
|
score -= 12;
|
|
findings++;
|
|
}
|
|
}
|
|
|
|
if (sysctl_int("/proc/sys/net/ipv4/ip_forward", 0) == 1) {
|
|
add_finding(&b, &first, "ip-forward", "medium",
|
|
"net.ipv4.ip_forward=1 (host routes packets)");
|
|
score -= 6;
|
|
findings++;
|
|
}
|
|
if (sysctl_int("/proc/sys/net/ipv4/tcp_syncookies", 1) == 0) {
|
|
add_finding(&b, &first, "syncookies-off", "medium",
|
|
"net.ipv4.tcp_syncookies=0 (SYN flood protection off)");
|
|
score -= 6;
|
|
findings++;
|
|
}
|
|
if (sysctl_int("/proc/sys/net/ipv4/conf/all/rp_filter", 1) == 0) {
|
|
add_finding(&b, &first, "rp-filter-off", "low",
|
|
"net.ipv4.conf.all.rp_filter=0 (no source validation)");
|
|
score -= 2;
|
|
findings++;
|
|
}
|
|
|
|
{
|
|
struct stat st;
|
|
|
|
if (stat("/etc/shadow", &st) == 0 && (st.st_mode & 0077) != 0) {
|
|
add_finding(&b, &first, "shadow-perms", "critical",
|
|
"/etc/shadow is readable beyond root (mode %04o)",
|
|
(unsigned)(st.st_mode & 07777));
|
|
score -= 20;
|
|
findings++;
|
|
}
|
|
}
|
|
|
|
ww = count_world_writable_etc(ww_list);
|
|
if (ww > 0) {
|
|
add_finding(&b, &first, "etc-world-writable", "medium",
|
|
"%d world-writable director%s under /etc", ww,
|
|
ww == 1 ? "y" : "ies");
|
|
score -= 6;
|
|
findings++;
|
|
}
|
|
|
|
{
|
|
long size = 0;
|
|
int have = 0;
|
|
|
|
if (file_size("/etc/nftables.conf", &size) == 0 && size > 0)
|
|
have = 1;
|
|
if (file_size("/etc/iptables/rules.v4", &size) == 0 && size > 0)
|
|
have = 1;
|
|
if (!have) {
|
|
add_finding(&b, &first, "no-firewall-config", "medium",
|
|
"no non-empty nftables/iptables rule file found");
|
|
score -= 6;
|
|
findings++;
|
|
}
|
|
}
|
|
|
|
suid = suid_binaries(suid_names);
|
|
if (suid > 40) {
|
|
add_finding(&b, &first, "suid-count", "low",
|
|
"%d SUID binaries in the system paths", suid);
|
|
score -= 2;
|
|
findings++;
|
|
}
|
|
nports = listening_ports(ports);
|
|
if (score < 0)
|
|
score = 0;
|
|
|
|
mcp_buf_printf(&b, "],\"score\":%d,\"findings_count\":%d,"
|
|
"\"counts\":{\"suid\":%d,\"listening\":%d,"
|
|
"\"world_writable\":%d},\"listening_ports\":[",
|
|
score, findings, suid, nports, ww);
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < nports; i++) {
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
mcp_buf_printf(&b, "%d", ports[i]);
|
|
}
|
|
}
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* --------------------------------------------------------------- log_audit */
|
|
|
|
typedef struct {
|
|
char ip[64];
|
|
int count;
|
|
} ip_count;
|
|
|
|
static int allowed_log_path(const char *path)
|
|
{
|
|
size_t hl;
|
|
|
|
if (path == NULL || path[0] != '/')
|
|
return 0;
|
|
if (strncmp(path, "/var/log/", 9) == 0)
|
|
return 1;
|
|
hl = strlen(g_cfg.home);
|
|
return hl > 0 && strncmp(path, g_cfg.home, hl) == 0;
|
|
}
|
|
|
|
static void t_log_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *path = mcp_arg_string(args, "path");
|
|
int limit = mcp_arg_int(args, "limit", 5000);
|
|
FILE *f;
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
ip_count ips[64];
|
|
int nips = 0;
|
|
char users[8][64];
|
|
int nusers = 0;
|
|
int lines = 0;
|
|
int failed = 0;
|
|
int invalid = 0;
|
|
int accepted = 0;
|
|
int failures_auth = 0;
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int i;
|
|
|
|
(void)user;
|
|
if (path == NULL || *path == '\0')
|
|
path = "/var/log/auth.log";
|
|
if (!allowed_log_path(path)) {
|
|
mcp_reply_error(c, "path must be under /var/log/ or the state dir");
|
|
return;
|
|
}
|
|
if (limit <= 0 || limit > 50000)
|
|
limit = 5000;
|
|
memset(ips, 0, sizeof(ips));
|
|
f = fopen(path, "rb");
|
|
if (f == NULL) {
|
|
mcp_buf_init(&b);
|
|
mcp_buf_printf(&b, "cannot read %s: %s", path, strerror(errno));
|
|
mcp_reply_error(c, b.p);
|
|
mcp_buf_free(&b);
|
|
return;
|
|
}
|
|
while (lines < limit && getline(&line, &cap, f) >= 0) {
|
|
char *from;
|
|
char ip[64];
|
|
|
|
lines++;
|
|
if (strstr(line, "Failed password") != NULL) {
|
|
failed++;
|
|
from = strstr(line, " from ");
|
|
if (from != NULL) {
|
|
size_t n = 0;
|
|
|
|
from += 6;
|
|
while (from[n] != '\0' && from[n] != ' ' &&
|
|
from[n] != '\n' && n + 1 < sizeof(ip)) {
|
|
ip[n] = from[n];
|
|
n++;
|
|
}
|
|
ip[n] = '\0';
|
|
if (n > 0) {
|
|
int hit = -1;
|
|
|
|
for (i = 0; i < nips; i++)
|
|
if (strcmp(ips[i].ip, ip) == 0)
|
|
hit = i;
|
|
if (hit < 0 && nips < 64) {
|
|
snprintf(ips[nips].ip, sizeof(ips[nips].ip),
|
|
"%s", ip);
|
|
ips[nips].count = 1;
|
|
nips++;
|
|
} else if (hit >= 0) {
|
|
ips[hit].count++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (strstr(line, "Invalid user") != NULL) {
|
|
char *u = strstr(line, "Invalid user ");
|
|
|
|
invalid++;
|
|
if (u != NULL && nusers < 8) {
|
|
size_t n = 0;
|
|
|
|
u += 13;
|
|
while (u[n] != '\0' && u[n] != ' ' && u[n] != '\n' &&
|
|
n + 1 < sizeof(users[0])) {
|
|
users[nusers][n] = u[n];
|
|
n++;
|
|
}
|
|
users[nusers][n] = '\0';
|
|
if (n > 0)
|
|
nusers++;
|
|
}
|
|
}
|
|
if (strstr(line, "Accepted password") != NULL ||
|
|
strstr(line, "Accepted publickey") != NULL)
|
|
accepted++;
|
|
if (strstr(line, "authentication failure") != NULL)
|
|
failures_auth++;
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"path\":");
|
|
apex_json_str(&b, path);
|
|
mcp_buf_printf(&b, ",\"lines\":%d,\"failed\":%d,\"invalid_users\":%d,"
|
|
"\"accepted\":%d,\"auth_failures\":%d,\"brute_force\":[",
|
|
lines, failed, invalid, accepted, failures_auth);
|
|
for (i = 0; i < nips; i++) {
|
|
if (ips[i].count < 5)
|
|
continue;
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"ip\":");
|
|
apex_json_str(&b, ips[i].ip);
|
|
mcp_buf_printf(&b, ",\"failures\":%d}", ips[i].count);
|
|
}
|
|
mcp_buf_puts(&b, "],\"targeted_users\":[");
|
|
for (i = 0; i < nusers; i++) {
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
apex_json_str(&b, users[i]);
|
|
}
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- ioc_scan */
|
|
|
|
typedef struct {
|
|
mcp_buf *b;
|
|
int first;
|
|
int count;
|
|
int truncated;
|
|
int files;
|
|
int max_files;
|
|
} ioc_ctx;
|
|
|
|
static int ioc_line(ioc_ctx *ctx, const char *path, int lineno,
|
|
const char *line, const char *pattern)
|
|
{
|
|
char snippet[140];
|
|
size_t n = 0;
|
|
|
|
if (ctx->count >= 100) {
|
|
ctx->truncated = 1;
|
|
return 0;
|
|
}
|
|
while (line[n] != '\0' && line[n] != '\n' && n + 1 < sizeof(snippet)) {
|
|
char ch = line[n];
|
|
|
|
snippet[n] = (ch == '\t') ? ' ' : ch;
|
|
n++;
|
|
}
|
|
while (n > 0 && (snippet[n - 1] == ' ' || snippet[n - 1] == '\r'))
|
|
n--;
|
|
snippet[n] = '\0';
|
|
if (!ctx->first)
|
|
mcp_buf_puts(ctx->b, ",");
|
|
ctx->first = 0;
|
|
ctx->count++;
|
|
mcp_buf_puts(ctx->b, "{\"path\":");
|
|
apex_json_str(ctx->b, path);
|
|
mcp_buf_printf(ctx->b, ",\"line\":%d,\"pattern\":", lineno);
|
|
apex_json_str(ctx->b, pattern);
|
|
mcp_buf_puts(ctx->b, ",\"snippet\":");
|
|
apex_json_str(ctx->b, snippet);
|
|
mcp_buf_puts(ctx->b, "}");
|
|
return 1;
|
|
}
|
|
|
|
static void scan_file(ioc_ctx *ctx, const char *path)
|
|
{
|
|
FILE *f;
|
|
char *buf;
|
|
long size = 0;
|
|
int lineno = 0;
|
|
char *line;
|
|
char *save = NULL;
|
|
|
|
if (ctx->files >= ctx->max_files) {
|
|
ctx->truncated = 1;
|
|
return;
|
|
}
|
|
if (file_size(path, &size) != 0 || size <= 0 || size > (1L << 20))
|
|
return;
|
|
f = fopen(path, "rb");
|
|
if (f == NULL)
|
|
return;
|
|
buf = malloc((size_t)size + 1);
|
|
if (buf == NULL) {
|
|
fclose(f);
|
|
return;
|
|
}
|
|
if (fread(buf, 1, (size_t)size, f) != (size_t)size) {
|
|
free(buf);
|
|
fclose(f);
|
|
return;
|
|
}
|
|
fclose(f);
|
|
buf[size] = '\0';
|
|
ctx->files++;
|
|
{
|
|
long k;
|
|
|
|
for (k = 0; k < size && k < 512; k++)
|
|
if (buf[k] == '\0') {
|
|
free(buf);
|
|
return; /* binary */
|
|
}
|
|
}
|
|
|
|
line = strtok_r(buf, "\n", &save);
|
|
while (line != NULL) {
|
|
lineno++;
|
|
if (strstr(line, "/dev/tcp/") != NULL)
|
|
ioc_line(ctx, path, lineno, line, "reverse-shell");
|
|
else if (strstr(line, "bash -i") != NULL &&
|
|
strchr(line, '>') != NULL)
|
|
ioc_line(ctx, path, lineno, line, "reverse-shell");
|
|
else if (strstr(line, "base64") != NULL &&
|
|
strstr(line, "-d") != NULL && strstr(line, "sh") != NULL)
|
|
ioc_line(ctx, path, lineno, line, "decode-and-run");
|
|
else if ((strstr(line, "curl ") != NULL ||
|
|
strstr(line, "wget ") != NULL) &&
|
|
(strstr(line, "| sh") != NULL || strstr(line, "|sh") != NULL ||
|
|
strstr(line, "| bash") != NULL))
|
|
ioc_line(ctx, path, lineno, line, "remote-code");
|
|
else if (strstr(line, "nc -e") != NULL ||
|
|
strstr(line, "ncat -e") != NULL ||
|
|
(strstr(line, "socat") != NULL &&
|
|
strstr(line, "EXEC:") != NULL))
|
|
ioc_line(ctx, path, lineno, line, "netcat-exec");
|
|
else if (strstr(line, "chmod 4755") != NULL ||
|
|
strstr(line, "chmod u+s") != NULL)
|
|
ioc_line(ctx, path, lineno, line, "setuid");
|
|
else if (strstr(line, "ExecStart=/tmp/") != NULL ||
|
|
strstr(line, "ExecStart=/dev/shm/") != NULL)
|
|
ioc_line(ctx, path, lineno, line, "systemd-tmp");
|
|
else if (strstr(line, "authorized_keys") != NULL &&
|
|
(strchr(line, '>') != NULL ||
|
|
strstr(line, "tee") != NULL))
|
|
ioc_line(ctx, path, lineno, line, "ssh-key-write");
|
|
line = strtok_r(NULL, "\n", &save);
|
|
}
|
|
free(buf);
|
|
}
|
|
|
|
static void scan_dir(ioc_ctx *ctx, const char *dir, int depth)
|
|
{
|
|
DIR *d;
|
|
struct dirent *ent;
|
|
|
|
if (depth > 6)
|
|
return;
|
|
d = opendir(dir);
|
|
if (d == NULL)
|
|
return;
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char path[1024];
|
|
struct stat st;
|
|
|
|
if (ctx->files >= ctx->max_files)
|
|
break;
|
|
if (ent->d_name[0] == '.' &&
|
|
(ent->d_name[1] == '\0' ||
|
|
(ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
|
|
continue;
|
|
{
|
|
size_t dl = strlen(dir);
|
|
size_t nl = strlen(ent->d_name);
|
|
|
|
if (dl + nl + 2 > sizeof(path))
|
|
continue;
|
|
memcpy(path, dir, dl);
|
|
path[dl] = '/';
|
|
memcpy(path + dl + 1, ent->d_name, nl + 1);
|
|
}
|
|
if (lstat(path, &st) != 0)
|
|
continue;
|
|
if (S_ISLNK(st.st_mode))
|
|
continue;
|
|
if (S_ISDIR(st.st_mode))
|
|
scan_dir(ctx, path, depth + 1);
|
|
else if (S_ISREG(st.st_mode))
|
|
scan_file(ctx, path);
|
|
}
|
|
closedir(d);
|
|
}
|
|
|
|
static void t_ioc_scan(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *path = mcp_arg_string(args, "path");
|
|
int max_files = mcp_arg_int(args, "limit", 2000);
|
|
struct stat st;
|
|
mcp_buf b;
|
|
ioc_ctx ctx;
|
|
|
|
(void)user;
|
|
if (path == NULL || *path == '\0')
|
|
path = "/etc";
|
|
if (path[0] != '/') {
|
|
mcp_reply_error(c, "path must be absolute");
|
|
return;
|
|
}
|
|
if (stat(path, &st) != 0 || !S_ISDIR(st.st_mode)) {
|
|
mcp_reply_error(c, "path is not a directory");
|
|
return;
|
|
}
|
|
if (max_files <= 0 || max_files > 20000)
|
|
max_files = 2000;
|
|
mcp_buf_init(&b);
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.b = &b;
|
|
ctx.first = 1;
|
|
ctx.max_files = max_files;
|
|
|
|
mcp_buf_puts(&b, "{\"path\":");
|
|
apex_json_str(&b, path);
|
|
mcp_buf_puts(&b, ",\"findings\":[");
|
|
scan_dir(&ctx, path, 0);
|
|
mcp_buf_printf(&b, "],\"files_scanned\":%d,\"findings_count\":%d,"
|
|
"\"truncated\":%s}", ctx.files, ctx.count,
|
|
ctx.truncated ? "true" : "false");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ----------------------------------------------------------- web_log_audit */
|
|
|
|
typedef struct {
|
|
char ip[64];
|
|
int total;
|
|
int c4xx;
|
|
int c5xx;
|
|
} web_ip;
|
|
|
|
static const char *SCANNERS[] = {
|
|
"sqlmap", "nikto", "nmap", "masscan", "gobuster", "dirbuster", "ffuf",
|
|
"nuclei", "hydra", "zgrab", "acunetix", "nessus", "wpscan", "joomscan",
|
|
NULL
|
|
};
|
|
|
|
static const char *WEB_PATTERNS[] = {
|
|
"' or ", "union select", "../", "/etc/passwd", "<script", "onerror=",
|
|
"javascript:", "%3cscript", "sleep(", "benchmark(", NULL
|
|
};
|
|
|
|
static int hexval(int c)
|
|
{
|
|
if (c >= '0' && c <= '9')
|
|
return c - '0';
|
|
if (c >= 'a' && c <= 'f')
|
|
return c - 'a' + 10;
|
|
if (c >= 'A' && c <= 'F')
|
|
return c - 'A' + 10;
|
|
return -1;
|
|
}
|
|
|
|
/* Percent-decode so log patterns see what the client actually sent. */
|
|
static void url_decode(char *dst, size_t n, const char *src)
|
|
{
|
|
size_t w = 0;
|
|
|
|
while (*src != '\0' && w + 1 < n) {
|
|
if (*src == '%' && src[1] != '\0' && src[2] != '\0') {
|
|
int hi = hexval((unsigned char)src[1]);
|
|
int lo = hexval((unsigned char)src[2]);
|
|
|
|
if (hi >= 0 && lo >= 0) {
|
|
dst[w++] = (char)((hi << 4) | lo);
|
|
src += 3;
|
|
continue;
|
|
}
|
|
}
|
|
dst[w++] = (*src == '+') ? ' ' : *src;
|
|
src++;
|
|
}
|
|
dst[w] = '\0';
|
|
}
|
|
|
|
static void lower_copy(char *dst, size_t n, const char *src)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; src[i] != '\0' && i + 1 < n; i++)
|
|
dst[i] = (char)tolower((unsigned char)src[i]);
|
|
dst[i] = '\0';
|
|
}
|
|
|
|
static void t_web_log_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *path = mcp_arg_string(args, "path");
|
|
int limit = mcp_arg_int(args, "limit", 5000);
|
|
FILE *f;
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
web_ip ips[64];
|
|
int nips = 0;
|
|
int scanner_count[16];
|
|
int lines = 0;
|
|
int requests = 0;
|
|
int c2 = 0;
|
|
int c3 = 0;
|
|
int c4 = 0;
|
|
int c5 = 0;
|
|
char suspicious[10][160];
|
|
char suspicious_pat[10][24];
|
|
int nsusp = 0;
|
|
int used[64];
|
|
mcp_buf b;
|
|
int i;
|
|
int printed = 0;
|
|
|
|
(void)user;
|
|
if (path == NULL || *path == '\0')
|
|
path = "/var/log/nginx/access.log";
|
|
if (!allowed_log_path(path)) {
|
|
mcp_reply_error(c, "path must be under /var/log/ or the state dir");
|
|
return;
|
|
}
|
|
if (limit <= 0 || limit > 50000)
|
|
limit = 5000;
|
|
memset(ips, 0, sizeof(ips));
|
|
memset(scanner_count, 0, sizeof(scanner_count));
|
|
memset(used, 0, sizeof(used));
|
|
f = fopen(path, "rb");
|
|
if (f == NULL) {
|
|
mcp_buf_init(&b);
|
|
mcp_buf_printf(&b, "cannot read %s: %s", path, strerror(errno));
|
|
mcp_reply_error(c, b.p);
|
|
mcp_buf_free(&b);
|
|
return;
|
|
}
|
|
while (lines < limit && getline(&line, &cap, f) >= 0) {
|
|
char ip[64];
|
|
char req[1024];
|
|
char *q1;
|
|
char *q2;
|
|
char *after;
|
|
char *uri;
|
|
char *sp;
|
|
int code;
|
|
int k;
|
|
int hitip = -1;
|
|
|
|
lines++;
|
|
if (sscanf(line, "%63s", ip) != 1)
|
|
continue;
|
|
q1 = strchr(line, '"');
|
|
if (q1 == NULL)
|
|
continue;
|
|
q2 = strchr(q1 + 1, '"');
|
|
if (q2 == NULL)
|
|
continue;
|
|
{
|
|
size_t n = (size_t)(q2 - q1 - 1);
|
|
|
|
if (n >= sizeof(req))
|
|
n = sizeof(req) - 1;
|
|
memcpy(req, q1 + 1, n);
|
|
req[n] = '\0';
|
|
}
|
|
sp = strchr(req, ' ');
|
|
if (sp == NULL)
|
|
continue;
|
|
*sp = '\0';
|
|
uri = sp + 1;
|
|
sp = strchr(uri, ' ');
|
|
if (sp != NULL)
|
|
*sp = '\0';
|
|
after = q2 + 1;
|
|
while (*after == ' ')
|
|
after++;
|
|
code = atoi(after);
|
|
if (code <= 0)
|
|
continue;
|
|
requests++;
|
|
if (code >= 200 && code < 300)
|
|
c2++;
|
|
else if (code >= 300 && code < 400)
|
|
c3++;
|
|
else if (code >= 400 && code < 500)
|
|
c4++;
|
|
else if (code >= 500)
|
|
c5++;
|
|
for (k = 0; k < nips; k++)
|
|
if (strcmp(ips[k].ip, ip) == 0)
|
|
hitip = k;
|
|
if (hitip < 0 && nips < 64) {
|
|
snprintf(ips[nips].ip, sizeof(ips[nips].ip), "%s", ip);
|
|
hitip = nips++;
|
|
}
|
|
if (hitip >= 0) {
|
|
ips[hitip].total++;
|
|
if (code >= 400 && code < 500)
|
|
ips[hitip].c4xx++;
|
|
if (code >= 500)
|
|
ips[hitip].c5xx++;
|
|
}
|
|
{
|
|
/* user agent is the last quoted string on the line */
|
|
char *q = q2 + 1;
|
|
char *last1 = NULL;
|
|
char *last2 = NULL;
|
|
|
|
while ((q = strchr(q, '"')) != NULL) {
|
|
char *e = strchr(q + 1, '"');
|
|
|
|
if (e == NULL)
|
|
break;
|
|
last1 = q + 1;
|
|
last2 = e;
|
|
q = e + 1;
|
|
}
|
|
if (last1 != NULL && last2 != NULL) {
|
|
char lc[256];
|
|
size_t n = (size_t)(last2 - last1);
|
|
|
|
if (n >= sizeof(lc))
|
|
n = sizeof(lc) - 1;
|
|
memcpy(lc, last1, n);
|
|
lc[n] = '\0';
|
|
lower_copy(lc, sizeof(lc), lc);
|
|
for (k = 0; SCANNERS[k] != NULL; k++)
|
|
if (strstr(lc, SCANNERS[k]) != NULL)
|
|
scanner_count[k]++;
|
|
}
|
|
}
|
|
{
|
|
char decoded[1024];
|
|
char low[1024];
|
|
|
|
url_decode(decoded, sizeof(decoded), uri);
|
|
lower_copy(low, sizeof(low), decoded);
|
|
for (k = 0; WEB_PATTERNS[k] != NULL; k++) {
|
|
int m;
|
|
int seen = 0;
|
|
|
|
if (strstr(low, WEB_PATTERNS[k]) == NULL)
|
|
continue;
|
|
for (m = 0; m < nsusp; m++)
|
|
if (strcmp(suspicious_pat[m], WEB_PATTERNS[k]) == 0)
|
|
seen = 1;
|
|
if (!seen && nsusp < 10) {
|
|
snprintf(suspicious[nsusp], sizeof(suspicious[0]),
|
|
"%s", uri);
|
|
snprintf(suspicious_pat[nsusp],
|
|
sizeof(suspicious_pat[0]), "%s",
|
|
WEB_PATTERNS[k]);
|
|
nsusp++;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"path\":");
|
|
apex_json_str(&b, path);
|
|
mcp_buf_printf(&b, ",\"lines\":%d,\"requests\":%d,"
|
|
"\"status\":{\"2xx\":%d,\"3xx\":%d,\"4xx\":%d,\"5xx\":%d},"
|
|
"\"top_ips\":[", lines, requests, c2, c3, c4, c5);
|
|
for (i = 0; i < 10 && i < nips; i++) {
|
|
int best = -1;
|
|
int k;
|
|
|
|
for (k = 0; k < nips; k++)
|
|
if (!used[k] && (best < 0 || ips[k].total > ips[best].total))
|
|
best = k;
|
|
if (best < 0)
|
|
break;
|
|
used[best] = 1;
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
mcp_buf_puts(&b, "{\"ip\":");
|
|
apex_json_str(&b, ips[best].ip);
|
|
mcp_buf_printf(&b, ",\"count\":%d}", ips[best].total);
|
|
}
|
|
mcp_buf_puts(&b, "],\"scanners\":[");
|
|
for (i = 0; SCANNERS[i] != NULL; i++) {
|
|
if (scanner_count[i] == 0)
|
|
continue;
|
|
if (printed++ > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
mcp_buf_puts(&b, "{\"name\":");
|
|
apex_json_str(&b, SCANNERS[i]);
|
|
mcp_buf_printf(&b, ",\"count\":%d}", scanner_count[i]);
|
|
}
|
|
mcp_buf_puts(&b, "],\"suspicious\":[");
|
|
for (i = 0; i < nsusp; i++) {
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
mcp_buf_puts(&b, "{\"uri\":");
|
|
apex_json_str(&b, suspicious[i]);
|
|
mcp_buf_puts(&b, ",\"pattern\":");
|
|
apex_json_str(&b, suspicious_pat[i]);
|
|
mcp_buf_puts(&b, "}");
|
|
}
|
|
mcp_buf_puts(&b, "],\"burst_ips\":[");
|
|
printed = 0;
|
|
for (i = 0; i < nips; i++) {
|
|
if (ips[i].c4xx < 20 && ips[i].c5xx < 5)
|
|
continue;
|
|
if (printed++ > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
mcp_buf_puts(&b, "{\"ip\":");
|
|
apex_json_str(&b, ips[i].ip);
|
|
mcp_buf_printf(&b, ",\"4xx\":%d,\"5xx\":%d}", ips[i].c4xx,
|
|
ips[i].c5xx);
|
|
}
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ------------------------------------------------------------- detect_rules */
|
|
|
|
static const char *sigma_level(const char *sev)
|
|
{
|
|
if (strcasecmp(sev, "critical") == 0)
|
|
return "critical";
|
|
if (strcasecmp(sev, "high") == 0)
|
|
return "high";
|
|
if (strcasecmp(sev, "medium") == 0)
|
|
return "medium";
|
|
if (strcasecmp(sev, "low") == 0)
|
|
return "low";
|
|
return "informational";
|
|
}
|
|
|
|
/* One printable line: no quotes, no backslashes, no control characters. */
|
|
static void clean_text(char *dst, size_t n, const char *src)
|
|
{
|
|
size_t w = 0;
|
|
|
|
if (src == NULL)
|
|
src = "";
|
|
for (; *src != '\0' && w + 1 < n; src++) {
|
|
unsigned char c = (unsigned char)*src;
|
|
|
|
if (c < 0x20)
|
|
c = ' ';
|
|
if (c == '"' || c == '\\' || c == ';')
|
|
c = '\'';
|
|
dst[w++] = (char)c;
|
|
}
|
|
dst[w] = '\0';
|
|
}
|
|
|
|
static void t_detect_rules(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
int limit = mcp_arg_int(args, "limit", 100);
|
|
apex_finding *rows;
|
|
int n;
|
|
int i;
|
|
int generated = 0;
|
|
char dir[APEX_PATH_FULL];
|
|
mcp_buf files;
|
|
mcp_buf b;
|
|
|
|
(void)user;
|
|
if (limit <= 0 || limit > 500)
|
|
limit = 100;
|
|
rows = calloc(1024, sizeof(*rows));
|
|
if (rows == NULL) {
|
|
mcp_reply_error(c, "out of memory");
|
|
return;
|
|
}
|
|
n = apex_finding_scan(rows, 1024);
|
|
snprintf(dir, sizeof(dir), "%s/detections", g_cfg.home);
|
|
apex_mkdir_p(dir, 0700);
|
|
mcp_buf_init(&files);
|
|
for (i = 0; i < n && generated < limit; i++) {
|
|
const apex_finding *f = &rows[i];
|
|
char title[256];
|
|
char host[256];
|
|
char path[APEX_PATH_FULL];
|
|
char name[64];
|
|
mcp_buf rule;
|
|
|
|
if (target != NULL && *target != '\0' &&
|
|
strcmp(f->target, target) != 0)
|
|
continue;
|
|
clean_text(title, sizeof(title), f->title);
|
|
clean_text(host, sizeof(host), f->target);
|
|
|
|
/* Sigma */
|
|
snprintf(name, sizeof(name), "sigma-%s.yml", f->id);
|
|
if (apex_path_join(path, sizeof(path), dir, name) == 0) {
|
|
mcp_buf_init(&rule);
|
|
mcp_buf_printf(&rule,
|
|
"title: \"APEX %s: %s\"\nid: apex-%s\n"
|
|
"status: experimental\n"
|
|
"description: \"%s on %s (%s) - template, tune the "
|
|
"selection to your log schema\"\n"
|
|
"author: APEX TWIN\nlogsource:\n category: web\n"
|
|
"detection:\n selection:\n url|contains: \"%s\"\n"
|
|
" condition: selection\nfalsepositives:\n"
|
|
" - \"template\"\nlevel: %s\n",
|
|
f->id, title, f->id, title, host, f->severity, host,
|
|
sigma_level(f->severity));
|
|
if (apex_write_file(path, rule.p, rule.len, 0600) == 0) {
|
|
generated++;
|
|
if (files.len > 0)
|
|
mcp_buf_puts(&files, ",");
|
|
mcp_buf_puts(&files, "\"");
|
|
mcp_buf_puts(&files, name);
|
|
mcp_buf_puts(&files, "\"");
|
|
}
|
|
mcp_buf_free(&rule);
|
|
}
|
|
|
|
/* Suricata */
|
|
snprintf(name, sizeof(name), "suricata-%s.rules", f->id);
|
|
if (apex_path_join(path, sizeof(path), dir, name) == 0) {
|
|
int sid = 9000000 + atoi(f->id[0] == 'f' ? f->id + 1 : f->id);
|
|
|
|
mcp_buf_init(&rule);
|
|
mcp_buf_printf(&rule,
|
|
"alert http any any -> any any (msg:\"APEX %s %s "
|
|
"(%s)\"; flow:to_server,established; "
|
|
"content:\"%s\"; http_host; sid:%d; rev:1;)\n",
|
|
f->id, title, host, host, sid);
|
|
if (apex_write_file(path, rule.p, rule.len, 0600) == 0) {
|
|
generated++;
|
|
if (files.len > 0)
|
|
mcp_buf_puts(&files, ",");
|
|
mcp_buf_puts(&files, "\"");
|
|
mcp_buf_puts(&files, name);
|
|
mcp_buf_puts(&files, "\"");
|
|
}
|
|
mcp_buf_free(&rule);
|
|
}
|
|
|
|
/* YARA */
|
|
snprintf(name, sizeof(name), "yara-%s.yar", f->id);
|
|
if (apex_path_join(path, sizeof(path), dir, name) == 0) {
|
|
mcp_buf_init(&rule);
|
|
mcp_buf_printf(&rule,
|
|
"rule APEX_%s\n{\n meta:\n"
|
|
" description = \"%s on %s\"\n"
|
|
" severity = \"%s\"\n"
|
|
" source = \"apex-twin\"\n"
|
|
" strings:\n $target = \"%s\" ascii wide "
|
|
"nocase\n condition:\n $target\n}\n",
|
|
f->id, title, host, sigma_level(f->severity), host);
|
|
if (apex_write_file(path, rule.p, rule.len, 0600) == 0) {
|
|
generated++;
|
|
if (files.len > 0)
|
|
mcp_buf_puts(&files, ",");
|
|
mcp_buf_puts(&files, "\"");
|
|
mcp_buf_puts(&files, name);
|
|
mcp_buf_puts(&files, "\"");
|
|
}
|
|
mcp_buf_free(&rule);
|
|
}
|
|
}
|
|
free(rows);
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"dir\":");
|
|
apex_json_str(&b, dir);
|
|
mcp_buf_printf(&b, ",\"generated\":%d,\"files\":[", generated);
|
|
if (files.p != NULL)
|
|
mcp_buf_add(&b, files.p, files.len);
|
|
mcp_buf_puts(&b, "],\"note\":\"these are skeletons: set the real "
|
|
"selection/flow and test them before deployment\"}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&files);
|
|
}
|
|
|
|
/* --------------------------------------------------------- integrity_baseline */
|
|
|
|
#define BASE_MAX 5000
|
|
|
|
typedef struct {
|
|
char path[512];
|
|
long size;
|
|
char hex[65];
|
|
int seen;
|
|
} base_entry;
|
|
|
|
typedef struct {
|
|
int create;
|
|
base_entry *entries;
|
|
int n;
|
|
int max;
|
|
mcp_buf *lines;
|
|
int changed;
|
|
int added;
|
|
int missing;
|
|
int checked;
|
|
mcp_buf *detail;
|
|
} integ_ctx;
|
|
|
|
static int hash_file(const char *path, char hex[65], long *size)
|
|
{
|
|
struct stat st;
|
|
FILE *f;
|
|
char *buf;
|
|
|
|
if (stat(path, &st) != 0 || !S_ISREG(st.st_mode))
|
|
return -1;
|
|
if (st.st_size < 0 || st.st_size > (1L << 20))
|
|
return -1;
|
|
f = fopen(path, "rb");
|
|
if (f == NULL)
|
|
return -1;
|
|
buf = malloc((size_t)st.st_size + 1);
|
|
if (buf == NULL) {
|
|
fclose(f);
|
|
return -1;
|
|
}
|
|
if (st.st_size > 0 &&
|
|
fread(buf, 1, (size_t)st.st_size, f) != (size_t)st.st_size) {
|
|
free(buf);
|
|
fclose(f);
|
|
return -1;
|
|
}
|
|
fclose(f);
|
|
apex_sha256(buf, (size_t)st.st_size, hex);
|
|
free(buf);
|
|
*size = (long)st.st_size;
|
|
return 0;
|
|
}
|
|
|
|
static void detail_add(integ_ctx *ctx, const char *path, const char *what)
|
|
{
|
|
if (ctx->detail->len > 0)
|
|
mcp_buf_puts(ctx->detail, ",");
|
|
mcp_buf_puts(ctx->detail, "{\"path\":");
|
|
apex_json_str(ctx->detail, path);
|
|
mcp_buf_puts(ctx->detail, ",\"what\":");
|
|
apex_json_str(ctx->detail, what);
|
|
mcp_buf_puts(ctx->detail, "}");
|
|
}
|
|
|
|
static void integ_file(integ_ctx *ctx, const char *path)
|
|
{
|
|
char hex[65];
|
|
long size;
|
|
|
|
if (ctx->checked >= ctx->max)
|
|
return;
|
|
if (hash_file(path, hex, &size) != 0)
|
|
return;
|
|
ctx->checked++;
|
|
if (ctx->create) {
|
|
mcp_buf_puts(ctx->lines, "{\"path\":");
|
|
apex_json_str(ctx->lines, path);
|
|
mcp_buf_printf(ctx->lines, ",\"size\":%ld,\"sha256\":\"%s\"}\n",
|
|
size, hex);
|
|
return;
|
|
}
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < ctx->n; i++) {
|
|
base_entry *e = &ctx->entries[i];
|
|
|
|
if (strcmp(e->path, path) != 0)
|
|
continue;
|
|
e->seen = 1;
|
|
if (e->size != size || strcmp(e->hex, hex) != 0) {
|
|
ctx->changed++;
|
|
if (ctx->changed <= 20)
|
|
detail_add(ctx, path, "changed");
|
|
}
|
|
return;
|
|
}
|
|
ctx->added++;
|
|
if (ctx->added <= 20)
|
|
detail_add(ctx, path, "added");
|
|
}
|
|
}
|
|
|
|
static void integ_walk(integ_ctx *ctx, const char *dir, int depth)
|
|
{
|
|
DIR *d;
|
|
struct dirent *ent;
|
|
|
|
if (depth > 8 || ctx->checked >= ctx->max)
|
|
return;
|
|
d = opendir(dir);
|
|
if (d == NULL)
|
|
return;
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char path[1024];
|
|
struct stat st;
|
|
size_t dl = strlen(dir);
|
|
size_t nl = strlen(ent->d_name);
|
|
|
|
if (ctx->checked >= ctx->max)
|
|
break;
|
|
if (ent->d_name[0] == '.' &&
|
|
(ent->d_name[1] == '\0' ||
|
|
(ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
|
|
continue;
|
|
if (dl + nl + 2 > sizeof(path))
|
|
continue;
|
|
memcpy(path, dir, dl);
|
|
path[dl] = '/';
|
|
memcpy(path + dl + 1, ent->d_name, nl + 1);
|
|
if (lstat(path, &st) != 0)
|
|
continue;
|
|
if (S_ISLNK(st.st_mode))
|
|
continue;
|
|
if (S_ISDIR(st.st_mode))
|
|
integ_walk(ctx, path, depth + 1);
|
|
else if (S_ISREG(st.st_mode))
|
|
integ_file(ctx, path);
|
|
}
|
|
closedir(d);
|
|
}
|
|
|
|
static int json_str_field(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 json_num_field(const char *json, const char *key)
|
|
{
|
|
char pattern[64];
|
|
const char *p;
|
|
|
|
snprintf(pattern, sizeof(pattern), "\"%s\":", key);
|
|
p = strstr(json, pattern);
|
|
if (p == NULL)
|
|
return 0;
|
|
return atol(p + strlen(pattern));
|
|
}
|
|
|
|
static void t_integrity_baseline(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *action = mcp_arg_string(args, "action");
|
|
const char *path = mcp_arg_string(args, "path");
|
|
const char *baseline = mcp_arg_string(args, "baseline");
|
|
int limit = mcp_arg_int(args, "limit", 1000);
|
|
char base_path[APEX_PATH_FULL];
|
|
integ_ctx ctx;
|
|
mcp_buf lines;
|
|
mcp_buf detail;
|
|
mcp_buf b;
|
|
int create;
|
|
|
|
(void)user;
|
|
if (path == NULL || *path == '\0')
|
|
path = "/etc";
|
|
if (action == NULL || *action == '\0')
|
|
action = "verify";
|
|
create = strcmp(action, "create") == 0;
|
|
if (!create && strcmp(action, "verify") != 0) {
|
|
mcp_reply_error(c, "action must be create or verify");
|
|
return;
|
|
}
|
|
if (limit <= 0 || limit > BASE_MAX)
|
|
limit = 1000;
|
|
if (baseline != NULL && *baseline != '\0')
|
|
snprintf(base_path, sizeof(base_path), "%s", baseline);
|
|
else
|
|
snprintf(base_path, sizeof(base_path), "%s/baseline.jsonl",
|
|
g_cfg.home);
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.create = create;
|
|
ctx.max = limit;
|
|
mcp_buf_init(&lines);
|
|
mcp_buf_init(&detail);
|
|
if (create) {
|
|
ctx.lines = &lines;
|
|
integ_walk(&ctx, path, 0);
|
|
if (apex_write_file(base_path, lines.p != NULL ? lines.p : "",
|
|
lines.len, 0600) != 0) {
|
|
mcp_reply_error(c, "cannot write the baseline");
|
|
mcp_buf_free(&lines);
|
|
mcp_buf_free(&detail);
|
|
return;
|
|
}
|
|
} else {
|
|
FILE *f = fopen(base_path, "rb");
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
|
|
if (f == NULL) {
|
|
mcp_reply_error(c, "no baseline; run action=create first");
|
|
mcp_buf_free(&lines);
|
|
mcp_buf_free(&detail);
|
|
return;
|
|
}
|
|
ctx.entries = calloc(BASE_MAX, sizeof(*ctx.entries));
|
|
if (ctx.entries == NULL) {
|
|
fclose(f);
|
|
mcp_reply_error(c, "out of memory");
|
|
mcp_buf_free(&lines);
|
|
mcp_buf_free(&detail);
|
|
return;
|
|
}
|
|
while (ctx.n < BASE_MAX && getline(&line, &cap, f) >= 0) {
|
|
base_entry *e = &ctx.entries[ctx.n];
|
|
|
|
if (!json_str_field(line, "path", e->path, sizeof(e->path)))
|
|
continue;
|
|
e->size = json_num_field(line, "size");
|
|
if (!json_str_field(line, "sha256", e->hex, sizeof(e->hex)))
|
|
continue;
|
|
e->seen = 0;
|
|
ctx.n++;
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
ctx.detail = &detail;
|
|
integ_walk(&ctx, path, 0);
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < ctx.n; i++)
|
|
if (!ctx.entries[i].seen) {
|
|
ctx.missing++;
|
|
if (ctx.missing <= 20)
|
|
detail_add(&ctx, ctx.entries[i].path,
|
|
"missing");
|
|
}
|
|
}
|
|
free(ctx.entries);
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"action\":");
|
|
apex_json_str(&b, create ? "create" : "verify");
|
|
mcp_buf_puts(&b, ",\"path\":");
|
|
apex_json_str(&b, path);
|
|
mcp_buf_puts(&b, ",\"baseline\":");
|
|
apex_json_str(&b, base_path);
|
|
if (create)
|
|
mcp_buf_printf(&b, ",\"files\":%d}", ctx.checked);
|
|
else
|
|
mcp_buf_printf(&b, ",\"checked\":%d,\"changed\":%d,\"missing\":%d,"
|
|
"\"added\":%d,\"details\":[%s]}", ctx.checked,
|
|
ctx.changed, ctx.missing, ctx.added,
|
|
detail.p != NULL ? detail.p : "");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&lines);
|
|
mcp_buf_free(&detail);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- mitre_map */
|
|
|
|
typedef struct {
|
|
const char *needle;
|
|
const char *id;
|
|
const char *name;
|
|
} mitre_rule;
|
|
|
|
static const mitre_rule MITRE[] = {
|
|
{ "sql", "T1190", "Exploit Public-Facing Application" },
|
|
{ "xss", "T1059.007", "JavaScript" },
|
|
{ "cross-site", "T1059.007", "JavaScript" },
|
|
{ "default credential", "T1078", "Valid Accounts" },
|
|
{ "password", "T1078", "Valid Accounts" },
|
|
{ "exposed", "T1213", "Data from Information Repositories" },
|
|
{ ".git", "T1213", "Data from Information Repositories" },
|
|
{ "directory listing", "T1213", "Data from Information Repositories" },
|
|
{ "backup", "T1213", "Data from Information Repositories" },
|
|
{ "ssrf", "T1190", "Exploit Public-Facing Application" },
|
|
{ "hsts", "T1557", "Adversary-in-the-Middle" },
|
|
{ "cors", "T1185", "Browser Session Hijacking" },
|
|
{ "port", "T1046", "Network Service Discovery" },
|
|
{ "subdomain", "T1590", "Gather Victim Network Information" },
|
|
{ NULL, NULL, NULL }
|
|
};
|
|
|
|
typedef struct {
|
|
int rule;
|
|
int count;
|
|
char ids[512];
|
|
} mitre_hit;
|
|
|
|
static void t_mitre_map(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *target = mcp_arg_string(args, "target");
|
|
apex_finding *rows;
|
|
int n;
|
|
int i;
|
|
int r;
|
|
int mapped = 0;
|
|
mitre_hit hits[32];
|
|
int nhits = 0;
|
|
mcp_buf b;
|
|
|
|
(void)user;
|
|
rows = calloc(1024, sizeof(*rows));
|
|
if (rows == NULL) {
|
|
mcp_reply_error(c, "out of memory");
|
|
return;
|
|
}
|
|
n = apex_finding_scan(rows, 1024);
|
|
memset(hits, 0, sizeof(hits));
|
|
for (i = 0; i < n; i++) {
|
|
const apex_finding *f = &rows[i];
|
|
char hay[1400];
|
|
int matched = 0;
|
|
|
|
if (target != NULL && *target != '\0' &&
|
|
strcmp(f->target, target) != 0)
|
|
continue;
|
|
snprintf(hay, sizeof(hay), "%s %s %s %s %s", f->title, f->tool,
|
|
f->tags, f->cwe, f->severity);
|
|
{
|
|
size_t k;
|
|
|
|
for (k = 0; hay[k] != '\0'; k++)
|
|
hay[k] = (char)tolower((unsigned char)hay[k]);
|
|
}
|
|
for (r = 0; MITRE[r].needle != NULL; r++) {
|
|
int h;
|
|
int known = -1;
|
|
|
|
if (strstr(hay, MITRE[r].needle) == NULL)
|
|
continue;
|
|
matched = 1;
|
|
for (h = 0; h < nhits; h++)
|
|
if (hits[h].rule == r)
|
|
known = h;
|
|
if (known < 0 && nhits < 32) {
|
|
known = nhits++;
|
|
hits[known].rule = r;
|
|
hits[known].count = 0;
|
|
hits[known].ids[0] = '\0';
|
|
}
|
|
if (known >= 0) {
|
|
hits[known].count++;
|
|
if (strlen(hits[known].ids) + strlen(f->id) + 2 <
|
|
sizeof(hits[known].ids)) {
|
|
if (hits[known].ids[0] != '\0')
|
|
strcat(hits[known].ids, ",");
|
|
strcat(hits[known].ids, f->id);
|
|
}
|
|
}
|
|
}
|
|
if (matched)
|
|
mapped++;
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_printf(&b, "{\"findings\":%d,\"mapped\":%d,\"unmapped\":%d,"
|
|
"\"techniques\":[", n, mapped, n - mapped);
|
|
for (i = 0; i < nhits; i++) {
|
|
const mitre_rule *mr = &MITRE[hits[i].rule];
|
|
char ids[512];
|
|
char *save = NULL;
|
|
char *tok;
|
|
int first = 1;
|
|
|
|
snprintf(ids, sizeof(ids), "%s", hits[i].ids);
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
mcp_buf_puts(&b, "{\"id\":");
|
|
apex_json_str(&b, mr->id);
|
|
mcp_buf_puts(&b, ",\"name\":");
|
|
apex_json_str(&b, mr->name);
|
|
mcp_buf_printf(&b, ",\"count\":%d,\"findings\":[", hits[i].count);
|
|
tok = strtok_r(ids, ",", &save);
|
|
while (tok != NULL) {
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
apex_json_str(&b, tok);
|
|
tok = strtok_r(NULL, ",", &save);
|
|
}
|
|
mcp_buf_puts(&b, "]}");
|
|
}
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
free(rows);
|
|
}
|
|
|
|
/* ----------------------------------------------------------- process_snapshot */
|
|
|
|
static int proc_fd_count(int pid)
|
|
{
|
|
char path[64];
|
|
DIR *d;
|
|
struct dirent *ent;
|
|
int n = 0;
|
|
|
|
snprintf(path, sizeof(path), "/proc/%d/fd", pid);
|
|
d = opendir(path);
|
|
if (d == NULL)
|
|
return 0;
|
|
while ((ent = readdir(d)) != NULL)
|
|
if (ent->d_name[0] != '.')
|
|
n++;
|
|
closedir(d);
|
|
return n;
|
|
}
|
|
|
|
static void proc_cmdline(int pid, char *out, size_t n)
|
|
{
|
|
char path[64];
|
|
FILE *f;
|
|
size_t got;
|
|
size_t i;
|
|
|
|
out[0] = '\0';
|
|
snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
|
|
f = fopen(path, "rb");
|
|
if (f == NULL)
|
|
return;
|
|
got = fread(out, 1, n - 1, f);
|
|
fclose(f);
|
|
out[got] = '\0';
|
|
for (i = 0; i < got; i++)
|
|
if (out[i] == '\0')
|
|
out[i] = ' ';
|
|
}
|
|
|
|
static void proc_exe(int pid, char *out, size_t n)
|
|
{
|
|
char path[64];
|
|
ssize_t r;
|
|
|
|
out[0] = '\0';
|
|
snprintf(path, sizeof(path), "/proc/%d/exe", pid);
|
|
r = readlink(path, out, n - 1);
|
|
if (r > 0)
|
|
out[r] = '\0';
|
|
else
|
|
out[0] = '\0';
|
|
}
|
|
|
|
static void proc_comm(int pid, char *out, size_t n)
|
|
{
|
|
char path[64];
|
|
FILE *f;
|
|
|
|
out[0] = '\0';
|
|
snprintf(path, sizeof(path), "/proc/%d/comm", pid);
|
|
f = fopen(path, "rb");
|
|
if (f == NULL)
|
|
return;
|
|
if (fgets(out, (int)n, f) == NULL)
|
|
out[0] = '\0';
|
|
fclose(f);
|
|
trim_eol(out);
|
|
}
|
|
|
|
static int proc_suspicious(const char *exe, const char *cmd)
|
|
{
|
|
static const char *patterns[] = {
|
|
"nc -e", "ncat -e", "/dev/tcp/", "base64 -d", "socat",
|
|
"bash -i", "sh -i", NULL
|
|
};
|
|
int i;
|
|
|
|
if (exe[0] != '\0' &&
|
|
(strncmp(exe, "/tmp/", 5) == 0 ||
|
|
strncmp(exe, "/dev/shm/", 9) == 0 ||
|
|
strncmp(exe, "/var/tmp/", 9) == 0))
|
|
return 1;
|
|
if (strstr(exe, "(deleted)") != NULL)
|
|
return 1;
|
|
for (i = 0; patterns[i] != NULL; i++)
|
|
if (strstr(cmd, patterns[i]) != NULL)
|
|
return 1;
|
|
return 0;
|
|
}
|
|
|
|
static void t_process_snapshot(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
DIR *d;
|
|
struct dirent *ent;
|
|
int processes = 0;
|
|
int suspicious = 0;
|
|
int emitted = 0;
|
|
mcp_buf b;
|
|
|
|
(void)args;
|
|
(void)user;
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"findings\":[");
|
|
d = opendir("/proc");
|
|
if (d == NULL) {
|
|
mcp_buf_free(&b);
|
|
mcp_reply_error(c, "cannot read /proc");
|
|
return;
|
|
}
|
|
while ((ent = readdir(d)) != NULL && processes < 4000) {
|
|
int pid;
|
|
char comm[64];
|
|
char exe[512];
|
|
char cmd[1024];
|
|
int fds;
|
|
|
|
if (ent->d_name[0] < '0' || ent->d_name[0] > '9')
|
|
continue;
|
|
pid = atoi(ent->d_name);
|
|
if (pid <= 0)
|
|
continue;
|
|
processes++;
|
|
proc_comm(pid, comm, sizeof(comm));
|
|
proc_exe(pid, exe, sizeof(exe));
|
|
proc_cmdline(pid, cmd, sizeof(cmd));
|
|
fds = proc_fd_count(pid);
|
|
if (proc_suspicious(exe, cmd) || fds > 256) {
|
|
suspicious++;
|
|
if (emitted < 100) {
|
|
const char *why = proc_suspicious(exe, cmd)
|
|
? "suspicious executable or command"
|
|
: "more than 256 open files";
|
|
|
|
if (emitted > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
emitted++;
|
|
mcp_buf_printf(&b, "{\"pid\":%d,\"name\":", pid);
|
|
apex_json_str(&b, comm);
|
|
mcp_buf_puts(&b, ",\"exe\":");
|
|
apex_json_str(&b, exe);
|
|
mcp_buf_printf(&b, ",\"fds\":%d,\"reason\":", fds);
|
|
apex_json_str(&b, why);
|
|
mcp_buf_puts(&b, "}");
|
|
}
|
|
}
|
|
}
|
|
closedir(d);
|
|
mcp_buf_printf(&b, "],\"processes\":%d,\"findings_count\":%d}",
|
|
processes, suspicious);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* -------------------------------------------------------- persistence_audit */
|
|
|
|
typedef struct {
|
|
mcp_buf *b;
|
|
int first;
|
|
int count;
|
|
int files;
|
|
int max_files;
|
|
int cron_files;
|
|
int timers;
|
|
int at_jobs;
|
|
int truncated;
|
|
} persist_ctx;
|
|
|
|
static const char *PERSIST_PATTERNS[] = {
|
|
"/tmp/", "/dev/shm/", "/var/tmp/",
|
|
"base64 -d", "nc -e", "ncat -e", "socat", "bash -i",
|
|
"chmod 4755", "chmod u+s", NULL
|
|
};
|
|
|
|
static void persist_hit(persist_ctx *ctx, const char *path, int lineno,
|
|
const char *line, const char *pattern)
|
|
{
|
|
char snippet[160];
|
|
size_t n = 0;
|
|
|
|
if (ctx->count >= 100) {
|
|
ctx->truncated = 1;
|
|
return;
|
|
}
|
|
while (line[n] != '\0' && line[n] != '\n' && n + 1 < sizeof(snippet)) {
|
|
char ch = line[n];
|
|
|
|
snippet[n] = (ch == '\t') ? ' ' : ch;
|
|
n++;
|
|
}
|
|
snippet[n] = '\0';
|
|
if (!ctx->first)
|
|
mcp_buf_puts(ctx->b, ",");
|
|
ctx->first = 0;
|
|
ctx->count++;
|
|
mcp_buf_puts(ctx->b, "{\"path\":");
|
|
apex_json_str(ctx->b, path);
|
|
mcp_buf_printf(ctx->b, ",\"line\":%d,\"pattern\":", lineno);
|
|
apex_json_str(ctx->b, pattern);
|
|
mcp_buf_puts(ctx->b, ",\"snippet\":");
|
|
apex_json_str(ctx->b, snippet);
|
|
mcp_buf_puts(ctx->b, "}");
|
|
}
|
|
|
|
/* mode: 0 cron, 1 systemd units, 2 at jobs */
|
|
static void persist_file(persist_ctx *ctx, const char *path, int mode)
|
|
{
|
|
FILE *f;
|
|
char *buf;
|
|
long size = 0;
|
|
char *save = NULL;
|
|
char *line;
|
|
int lineno = 0;
|
|
|
|
if (ctx->files >= ctx->max_files) {
|
|
ctx->truncated = 1;
|
|
return;
|
|
}
|
|
if (file_size(path, &size) != 0 || size <= 0 || size > (1L << 20))
|
|
return;
|
|
f = fopen(path, "rb");
|
|
if (f == NULL)
|
|
return;
|
|
buf = malloc((size_t)size + 1);
|
|
if (buf == NULL) {
|
|
fclose(f);
|
|
return;
|
|
}
|
|
if (fread(buf, 1, (size_t)size, f) != (size_t)size) {
|
|
free(buf);
|
|
fclose(f);
|
|
return;
|
|
}
|
|
fclose(f);
|
|
buf[size] = '\0';
|
|
{
|
|
long k;
|
|
|
|
for (k = 0; k < size && k < 512; k++)
|
|
if (buf[k] == '\0') {
|
|
free(buf);
|
|
return; /* binary */
|
|
}
|
|
}
|
|
ctx->files++;
|
|
if (mode == 0)
|
|
ctx->cron_files++;
|
|
else if (mode == 1) {
|
|
size_t nl = strlen(path);
|
|
|
|
if (nl > 6 && strcmp(path + nl - 6, ".timer") == 0)
|
|
ctx->timers++;
|
|
} else if (mode == 2) {
|
|
ctx->at_jobs++;
|
|
}
|
|
|
|
line = strtok_r(buf, "\n", &save);
|
|
while (line != NULL) {
|
|
const char *found = NULL;
|
|
int i;
|
|
|
|
lineno++;
|
|
if (strstr(line, "curl ") != NULL || strstr(line, "wget ") != NULL) {
|
|
if (strstr(line, "| sh") != NULL ||
|
|
strstr(line, "|sh") != NULL ||
|
|
strstr(line, "| bash") != NULL)
|
|
found = "remote-code";
|
|
}
|
|
if (found == NULL) {
|
|
for (i = 0; PERSIST_PATTERNS[i] != NULL; i++)
|
|
if (strstr(line, PERSIST_PATTERNS[i]) != NULL) {
|
|
found = (strcmp(PERSIST_PATTERNS[i], "base64 -d") == 0)
|
|
? "decode-and-run"
|
|
: PERSIST_PATTERNS[i];
|
|
break;
|
|
}
|
|
}
|
|
if (found != NULL)
|
|
persist_hit(ctx, path, lineno, line, found);
|
|
line = strtok_r(NULL, "\n", &save);
|
|
}
|
|
free(buf);
|
|
}
|
|
|
|
static void persist_dir(persist_ctx *ctx, const char *dir, int depth, int mode)
|
|
{
|
|
DIR *d;
|
|
struct dirent *ent;
|
|
|
|
if (depth > 2 || ctx->files >= ctx->max_files)
|
|
return;
|
|
d = opendir(dir);
|
|
if (d == NULL)
|
|
return;
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char path[1024];
|
|
struct stat st;
|
|
size_t dl = strlen(dir);
|
|
size_t nl = strlen(ent->d_name);
|
|
|
|
if (ctx->files >= ctx->max_files)
|
|
break;
|
|
if (ent->d_name[0] == '.' &&
|
|
(ent->d_name[1] == '\0' ||
|
|
(ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
|
|
continue;
|
|
if (dl + nl + 2 > sizeof(path))
|
|
continue;
|
|
memcpy(path, dir, dl);
|
|
path[dl] = '/';
|
|
memcpy(path + dl + 1, ent->d_name, nl + 1);
|
|
if (lstat(path, &st) != 0)
|
|
continue;
|
|
if (S_ISLNK(st.st_mode))
|
|
continue;
|
|
if (S_ISDIR(st.st_mode)) {
|
|
persist_dir(ctx, path, depth + 1, mode);
|
|
continue;
|
|
}
|
|
if (!S_ISREG(st.st_mode))
|
|
continue;
|
|
if (mode == 1 && !(nl > 8 && strcmp(path + strlen(path) - 8,
|
|
".service") == 0) &&
|
|
!(nl > 6 && strcmp(path + strlen(path) - 6, ".timer") == 0))
|
|
continue;
|
|
persist_file(ctx, path, mode);
|
|
}
|
|
closedir(d);
|
|
}
|
|
|
|
static void t_persistence_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
int limit = mcp_arg_int(args, "limit", 2000);
|
|
char base[512];
|
|
size_t bl;
|
|
char path[1024];
|
|
persist_ctx ctx;
|
|
mcp_buf b, findings;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) >= sizeof(base) - 2) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(base, sizeof(base), "%s", root);
|
|
bl = strlen(base);
|
|
while (bl > 0 && base[bl - 1] == '/')
|
|
base[--bl] = '\0';
|
|
if (limit <= 0 || limit > 20000)
|
|
limit = 2000;
|
|
mcp_buf_init(&findings);
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.b = &findings;
|
|
ctx.first = 1;
|
|
ctx.max_files = limit;
|
|
|
|
#define PERSIST_PATH(fmt, mode) do { \
|
|
snprintf(path, sizeof(path), "%s" fmt, base); \
|
|
persist_file(&ctx, path, mode); \
|
|
} while (0)
|
|
#define PERSIST_DIR(fmt, mode) do { \
|
|
snprintf(path, sizeof(path), "%s" fmt, base); \
|
|
persist_dir(&ctx, path, 0, mode); \
|
|
} while (0)
|
|
PERSIST_PATH("/etc/crontab", 0);
|
|
PERSIST_DIR("/etc/cron.d", 0);
|
|
PERSIST_DIR("/var/spool/cron/crontabs", 0);
|
|
PERSIST_DIR("/var/spool/cron/atjobs", 2);
|
|
PERSIST_DIR("/etc/systemd/system", 1);
|
|
#undef PERSIST_PATH
|
|
#undef PERSIST_DIR
|
|
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, base);
|
|
mcp_buf_printf(&b, ",\"files_scanned\":%d,\"cron_files\":%d,"
|
|
"\"timers\":%d,\"at_jobs\":%d,\"findings_count\":%d,"
|
|
"\"truncated\":%s,\"findings\":[",
|
|
ctx.files, ctx.cron_files, ctx.timers, ctx.at_jobs,
|
|
ctx.count, ctx.truncated ? "true" : "false");
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* -------------------------------------------------------------- net_baseline */
|
|
|
|
typedef struct {
|
|
int port;
|
|
unsigned long inode;
|
|
char process[64];
|
|
int seen;
|
|
} net_entry;
|
|
|
|
static int net_collect(net_entry *ents, int max)
|
|
{
|
|
static const char *files[2] = { "/proc/net/tcp", "/proc/net/tcp6" };
|
|
int n = 0;
|
|
int fi;
|
|
|
|
for (fi = 0; fi < 2; fi++) {
|
|
FILE *f = fopen(files[fi], "rb");
|
|
char line[1024];
|
|
|
|
if (f == NULL)
|
|
continue;
|
|
if (fgets(line, sizeof(line), f) == NULL) {
|
|
fclose(f);
|
|
continue;
|
|
}
|
|
while (fgets(line, sizeof(line), f) != NULL && n < max) {
|
|
char *fields[12];
|
|
int nf = 0;
|
|
char *save = NULL;
|
|
char *tok;
|
|
char *colon;
|
|
int port;
|
|
unsigned long ino;
|
|
int i;
|
|
int dup = 0;
|
|
|
|
for (tok = strtok_r(line, " \t\n", &save); tok != NULL &&
|
|
nf < 12; tok = strtok_r(NULL, " \t\n", &save))
|
|
fields[nf++] = tok;
|
|
if (nf < 10)
|
|
continue;
|
|
if (strcmp(fields[3], "0A") != 0)
|
|
continue;
|
|
colon = strrchr(fields[1], ':');
|
|
if (colon == NULL)
|
|
continue;
|
|
port = (int)strtoul(colon + 1, NULL, 16);
|
|
if (port <= 0 || port > 65535)
|
|
continue;
|
|
ino = strtoul(fields[9], NULL, 10);
|
|
for (i = 0; i < n; i++)
|
|
if (ents[i].port == port)
|
|
dup = 1;
|
|
if (dup)
|
|
continue;
|
|
ents[n].port = port;
|
|
ents[n].inode = ino;
|
|
ents[n].process[0] = '\0';
|
|
ents[n].seen = 0;
|
|
n++;
|
|
}
|
|
fclose(f);
|
|
}
|
|
return n;
|
|
}
|
|
|
|
static void net_process_names(net_entry *ents, int n)
|
|
{
|
|
DIR *proc;
|
|
struct dirent *pe;
|
|
int found = 0;
|
|
|
|
if (n == 0)
|
|
return;
|
|
proc = opendir("/proc");
|
|
if (proc == NULL)
|
|
return;
|
|
while ((pe = readdir(proc)) != NULL && found < n) {
|
|
char fdpath[64];
|
|
DIR *fds;
|
|
struct dirent *fe;
|
|
|
|
if (pe->d_name[0] < '0' || pe->d_name[0] > '9')
|
|
continue;
|
|
snprintf(fdpath, sizeof(fdpath), "/proc/%.32s/fd", pe->d_name);
|
|
fds = opendir(fdpath);
|
|
if (fds == NULL)
|
|
continue;
|
|
while ((fe = readdir(fds)) != NULL && found < n) {
|
|
char full[128];
|
|
char link[128];
|
|
ssize_t r;
|
|
unsigned long ino;
|
|
int i;
|
|
|
|
if (fe->d_name[0] == '.')
|
|
continue;
|
|
snprintf(full, sizeof(full), "/proc/%.32s/fd/%.48s",
|
|
pe->d_name, fe->d_name);
|
|
r = readlink(full, link, sizeof(link) - 1);
|
|
if (r <= 0)
|
|
continue;
|
|
link[r] = '\0';
|
|
if (strncmp(link, "socket:[", 8) != 0)
|
|
continue;
|
|
ino = strtoul(link + 8, NULL, 10);
|
|
for (i = 0; i < n; i++) {
|
|
char commpath[64];
|
|
char comm[64];
|
|
|
|
if (ents[i].inode != ino || ents[i].process[0] != '\0')
|
|
continue;
|
|
snprintf(commpath, sizeof(commpath), "/proc/%.32s/comm",
|
|
pe->d_name);
|
|
if (read_text(commpath, comm, sizeof(comm)) > 0) {
|
|
trim_eol(comm);
|
|
snprintf(ents[i].process,
|
|
sizeof(ents[i].process), "%.48s", comm);
|
|
} else {
|
|
snprintf(ents[i].process,
|
|
sizeof(ents[i].process), "%s", "-");
|
|
}
|
|
found++;
|
|
break;
|
|
}
|
|
}
|
|
closedir(fds);
|
|
}
|
|
closedir(proc);
|
|
}
|
|
|
|
static int net_write_baseline(const char *path, net_entry *ents, int n)
|
|
{
|
|
mcp_buf b;
|
|
int i;
|
|
int rc;
|
|
|
|
mcp_buf_init(&b);
|
|
for (i = 0; i < n; i++) {
|
|
mcp_buf_printf(&b, "{\"port\":%d,\"process\":", ents[i].port);
|
|
apex_json_str(&b, ents[i].process);
|
|
mcp_buf_puts(&b, "}\n");
|
|
}
|
|
rc = apex_write_file(path, b.p != NULL ? b.p : "", b.len, 0600);
|
|
mcp_buf_free(&b);
|
|
return rc;
|
|
}
|
|
|
|
static void t_net_baseline(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *action = mcp_arg_string(args, "action");
|
|
const char *baseline = mcp_arg_string(args, "baseline");
|
|
int limit = mcp_arg_int(args, "limit", 500);
|
|
char base_path[APEX_PATH_FULL];
|
|
net_entry current[512];
|
|
net_entry base[512];
|
|
int ncur;
|
|
int nbase = 0;
|
|
mcp_buf b;
|
|
int create;
|
|
int i;
|
|
|
|
(void)user;
|
|
if (action == NULL || *action == '\0')
|
|
action = "verify";
|
|
create = strcmp(action, "create") == 0;
|
|
if (!create && strcmp(action, "verify") != 0) {
|
|
mcp_reply_error(c, "action must be create or verify");
|
|
return;
|
|
}
|
|
if (limit <= 0 || limit > 500)
|
|
limit = 500;
|
|
if (baseline != NULL && *baseline != '\0')
|
|
snprintf(base_path, sizeof(base_path), "%s", baseline);
|
|
else
|
|
snprintf(base_path, sizeof(base_path), "%s/net-baseline.jsonl",
|
|
g_cfg.home);
|
|
ncur = net_collect(current, limit);
|
|
net_process_names(current, ncur);
|
|
if (create) {
|
|
if (net_write_baseline(base_path, current, ncur) != 0) {
|
|
mcp_reply_error(c, "cannot write the network baseline");
|
|
return;
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"action\":\"create\",\"baseline\":");
|
|
apex_json_str(&b, base_path);
|
|
mcp_buf_printf(&b, ",\"listening\":%d,\"ports\":[", ncur);
|
|
for (i = 0; i < ncur; i++) {
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
mcp_buf_puts(&b, "{\"port\":");
|
|
mcp_buf_printf(&b, "%d,\"process\":", current[i].port);
|
|
apex_json_str(&b, current[i].process);
|
|
mcp_buf_puts(&b, "}");
|
|
}
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
return;
|
|
}
|
|
{
|
|
FILE *f = fopen(base_path, "rb");
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
|
|
if (f == NULL) {
|
|
mcp_reply_error(c, "no network baseline; run action=create");
|
|
return;
|
|
}
|
|
while (nbase < 512 && getline(&line, &cap, f) >= 0) {
|
|
int port = (int)json_num_field(line, "port");
|
|
|
|
if (port <= 0 || port > 65535)
|
|
continue;
|
|
base[nbase].port = port;
|
|
base[nbase].inode = 0;
|
|
base[nbase].seen = 0;
|
|
json_str_field(line, "process", base[nbase].process,
|
|
sizeof(base[nbase].process));
|
|
nbase++;
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"action\":\"verify\",\"baseline\":");
|
|
apex_json_str(&b, base_path);
|
|
mcp_buf_printf(&b, ",\"listening\":%d,\"new\":[", ncur);
|
|
{
|
|
int first = 1;
|
|
|
|
for (i = 0; i < ncur; i++) {
|
|
int j;
|
|
int known = 0;
|
|
|
|
for (j = 0; j < nbase; j++)
|
|
if (base[j].port == current[i].port)
|
|
known = 1;
|
|
if (known)
|
|
continue;
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"port\":");
|
|
mcp_buf_printf(&b, "%d,\"process\":", current[i].port);
|
|
apex_json_str(&b, current[i].process);
|
|
mcp_buf_puts(&b, "}");
|
|
}
|
|
}
|
|
mcp_buf_puts(&b, "],\"gone\":[");
|
|
{
|
|
int first = 1;
|
|
|
|
for (i = 0; i < nbase; i++) {
|
|
int j;
|
|
int seen = 0;
|
|
|
|
for (j = 0; j < ncur; j++)
|
|
if (current[j].port == base[i].port)
|
|
seen = 1;
|
|
if (seen)
|
|
continue;
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_printf(&b, "{\"port\":%d,\"process\":", base[i].port);
|
|
apex_json_str(&b, base[i].process);
|
|
mcp_buf_puts(&b, "}");
|
|
}
|
|
}
|
|
mcp_buf_puts(&b, "],\"changed\":[");
|
|
{
|
|
int first = 1;
|
|
|
|
for (i = 0; i < ncur; i++) {
|
|
int j;
|
|
|
|
for (j = 0; j < nbase; j++) {
|
|
if (base[j].port != current[i].port)
|
|
continue;
|
|
if (base[j].process[0] != '\0' &&
|
|
current[i].process[0] != '\0' &&
|
|
strcmp(base[j].process, current[i].process) != 0) {
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_printf(&b, "{\"port\":%d,\"was\":",
|
|
current[i].port);
|
|
apex_json_str(&b, base[j].process);
|
|
mcp_buf_puts(&b, ",\"now\":");
|
|
apex_json_str(&b, current[i].process);
|
|
mcp_buf_puts(&b, "}");
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
mcp_buf_printf(&b, "],\"baseline_ports\":%d}", nbase);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ------------------------------------------------------------ ssh_key_audit */
|
|
|
|
typedef struct {
|
|
mcp_buf *b;
|
|
int first;
|
|
int count;
|
|
int files;
|
|
int keys;
|
|
int max_files;
|
|
char blobs[64][256];
|
|
char where[64][256];
|
|
int nblobs;
|
|
} key_ctx;
|
|
|
|
static void key_finding(key_ctx *ctx, const char *path, int line,
|
|
const char *kind, const char *severity,
|
|
const char *detail)
|
|
{
|
|
if (ctx->count >= 100)
|
|
return;
|
|
if (!ctx->first)
|
|
mcp_buf_puts(ctx->b, ",");
|
|
ctx->first = 0;
|
|
ctx->count++;
|
|
mcp_buf_puts(ctx->b, "{\"path\":");
|
|
apex_json_str(ctx->b, path);
|
|
mcp_buf_printf(ctx->b, ",\"line\":%d,\"kind\":", line);
|
|
apex_json_str(ctx->b, kind);
|
|
mcp_buf_puts(ctx->b, ",\"severity\":");
|
|
apex_json_str(ctx->b, severity);
|
|
mcp_buf_puts(ctx->b, ",\"detail\":");
|
|
apex_json_str(ctx->b, detail);
|
|
mcp_buf_puts(ctx->b, "}");
|
|
}
|
|
|
|
static int ssh_key_type(const char *token)
|
|
{
|
|
return strncmp(token, "ssh-", 4) == 0 ||
|
|
strncmp(token, "ecdsa-", 6) == 0 ||
|
|
strncmp(token, "sk-ssh-", 7) == 0 ||
|
|
strncmp(token, "sk-ecdsa-", 9) == 0;
|
|
}
|
|
|
|
static void key_scan_file(key_ctx *ctx, const char *path)
|
|
{
|
|
FILE *f;
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
int lineno = 0;
|
|
|
|
if (ctx->files >= ctx->max_files)
|
|
return;
|
|
f = fopen(path, "rb");
|
|
if (f == NULL)
|
|
return;
|
|
ctx->files++;
|
|
while (getline(&line, &cap, f) >= 0) {
|
|
char *tokens[16];
|
|
int ntok = 0;
|
|
char *save = NULL;
|
|
char *tok;
|
|
char *copy;
|
|
int kidx = -1;
|
|
int i;
|
|
char options[512];
|
|
size_t olen = 0;
|
|
int restricted;
|
|
|
|
lineno++;
|
|
while (line[0] == ' ' || line[0] == '\t')
|
|
memmove(line, line + 1, strlen(line));
|
|
trim_eol(line);
|
|
if (line[0] == '\0' || line[0] == '#')
|
|
continue;
|
|
copy = strdup(line);
|
|
if (copy == NULL)
|
|
continue;
|
|
tok = strtok_r(copy, " \t", &save);
|
|
while (tok != NULL && ntok < 16) {
|
|
tokens[ntok++] = tok;
|
|
tok = strtok_r(NULL, " \t", &save);
|
|
}
|
|
for (i = 0; i < ntok; i++)
|
|
if (ssh_key_type(tokens[i])) {
|
|
kidx = i;
|
|
break;
|
|
}
|
|
if (kidx < 0) {
|
|
free(copy);
|
|
continue;
|
|
}
|
|
ctx->keys++;
|
|
options[0] = '\0';
|
|
for (i = 0; i < kidx; i++) {
|
|
size_t tl = strlen(tokens[i]);
|
|
|
|
if (olen + tl + 2 < sizeof(options)) {
|
|
if (olen > 0)
|
|
options[olen++] = ' ';
|
|
memcpy(options + olen, tokens[i], tl);
|
|
olen += tl;
|
|
options[olen] = '\0';
|
|
}
|
|
}
|
|
restricted = strstr(options, "from=") != NULL ||
|
|
strstr(options, "command=") != NULL ||
|
|
strstr(options, "restrict") != NULL ||
|
|
strstr(options, "no-pty") != NULL ||
|
|
strstr(options, "no-port-forwarding") != NULL ||
|
|
strstr(options, "no-agent-forwarding") != NULL;
|
|
if (!restricted)
|
|
key_finding(ctx, path, lineno, "unrestricted", "medium",
|
|
"key has no from= or command= restriction");
|
|
if (strcmp(tokens[kidx], "ssh-rsa") == 0)
|
|
key_finding(ctx, path, lineno, "weak-key-type", "low",
|
|
"ssh-rsa uses SHA-1 signatures");
|
|
if (strcmp(tokens[kidx], "ssh-dss") == 0)
|
|
key_finding(ctx, path, lineno, "weak-key-type", "medium",
|
|
"ssh-dss is deprecated");
|
|
if (kidx + 1 < ntok) {
|
|
const char *blob = tokens[kidx + 1];
|
|
int j;
|
|
int dup = 0;
|
|
|
|
for (j = 0; j < ctx->nblobs; j++)
|
|
if (strcmp(ctx->blobs[j], blob) == 0)
|
|
dup = 1;
|
|
if (dup) {
|
|
char detail[320];
|
|
|
|
snprintf(detail, sizeof(detail),
|
|
"the same key is authorized more than once");
|
|
key_finding(ctx, path, lineno, "duplicate-key", "low",
|
|
detail);
|
|
} else if (ctx->nblobs < 64) {
|
|
snprintf(ctx->blobs[ctx->nblobs],
|
|
sizeof(ctx->blobs[0]), "%.240s", blob);
|
|
snprintf(ctx->where[ctx->nblobs],
|
|
sizeof(ctx->where[0]), "%.200s:%d", path,
|
|
lineno);
|
|
ctx->nblobs++;
|
|
}
|
|
}
|
|
free(copy);
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
}
|
|
|
|
static void key_private_check(key_ctx *ctx, const char *path)
|
|
{
|
|
struct stat st;
|
|
char detail[192];
|
|
|
|
if (stat(path, &st) != 0 || !S_ISREG(st.st_mode))
|
|
return;
|
|
if ((st.st_mode & 0077) == 0)
|
|
return;
|
|
snprintf(detail, sizeof(detail),
|
|
"private key is accessible beyond its owner (mode %04o)",
|
|
(unsigned)(st.st_mode & 07777));
|
|
key_finding(ctx, path, 0, "private-key-permissions", "high", detail);
|
|
}
|
|
|
|
static void key_scan_ssh_dir(key_ctx *ctx, const char *dir)
|
|
{
|
|
DIR *d = opendir(dir);
|
|
struct dirent *ent;
|
|
|
|
if (d == NULL)
|
|
return;
|
|
while ((ent = readdir(d)) != NULL && ctx->files < ctx->max_files) {
|
|
char path[1024];
|
|
size_t dl = strlen(dir);
|
|
size_t nl = strlen(ent->d_name);
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
if (dl + nl + 2 > sizeof(path))
|
|
continue;
|
|
memcpy(path, dir, dl);
|
|
path[dl] = '/';
|
|
memcpy(path + dl + 1, ent->d_name, nl + 1);
|
|
if (strcmp(ent->d_name, "authorized_keys") == 0)
|
|
key_scan_file(ctx, path);
|
|
else if (strncmp(ent->d_name, "id_", 3) == 0 &&
|
|
strstr(ent->d_name, ".pub") == NULL)
|
|
key_private_check(ctx, path);
|
|
}
|
|
closedir(d);
|
|
}
|
|
|
|
static void key_scan_homes(key_ctx *ctx, const char *home_root)
|
|
{
|
|
DIR *d = opendir(home_root);
|
|
struct dirent *ent;
|
|
|
|
if (d == NULL)
|
|
return;
|
|
while ((ent = readdir(d)) != NULL && ctx->files < ctx->max_files) {
|
|
char sshdir[1024];
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
snprintf(sshdir, sizeof(sshdir), "%.800s/%.160s/.ssh", home_root,
|
|
ent->d_name);
|
|
key_scan_ssh_dir(ctx, sshdir);
|
|
}
|
|
closedir(d);
|
|
}
|
|
|
|
static void t_ssh_key_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
int limit = mcp_arg_int(args, "limit", 200);
|
|
char base[512];
|
|
size_t bl;
|
|
char path[1024];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
key_ctx ctx;
|
|
DIR *d;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) >= sizeof(base) - 2) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(base, sizeof(base), "%s", root);
|
|
bl = strlen(base);
|
|
while (bl > 0 && base[bl - 1] == '/')
|
|
base[--bl] = '\0';
|
|
if (limit <= 0 || limit > 2000)
|
|
limit = 200;
|
|
mcp_buf_init(&findings);
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.b = &findings;
|
|
ctx.first = 1;
|
|
ctx.max_files = limit;
|
|
|
|
snprintf(path, sizeof(path), "%s/etc/ssh/authorized_keys", base);
|
|
key_scan_file(&ctx, path);
|
|
snprintf(path, sizeof(path), "%s/root/.ssh", base);
|
|
key_scan_ssh_dir(&ctx, path);
|
|
snprintf(path, sizeof(path), "%s/home", base);
|
|
key_scan_homes(&ctx, path);
|
|
snprintf(path, sizeof(path), "%.450s%.450s/.ssh", base, g_cfg.home);
|
|
key_scan_ssh_dir(&ctx, path);
|
|
/* host keys in /etc/ssh */
|
|
snprintf(path, sizeof(path), "%s/etc/ssh", base);
|
|
d = opendir(path);
|
|
if (d != NULL) {
|
|
struct dirent *ent;
|
|
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char full[1024];
|
|
|
|
if (strncmp(ent->d_name, "ssh_host_", 9) != 0)
|
|
continue;
|
|
if (strstr(ent->d_name, "_key") == NULL)
|
|
continue;
|
|
snprintf(full, sizeof(full), "%.800s/%.160s", path,
|
|
ent->d_name);
|
|
key_private_check(&ctx, full);
|
|
}
|
|
closedir(d);
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, base);
|
|
mcp_buf_printf(&b, ",\"files_scanned\":%d,\"keys\":%d,"
|
|
"\"findings_count\":%d,\"findings\":[",
|
|
ctx.files, ctx.keys, ctx.count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* ------------------------------------------------------------- suid_baseline */
|
|
|
|
typedef struct {
|
|
char path[512];
|
|
char mode[8];
|
|
char hex[65];
|
|
int seen;
|
|
} suid_entry;
|
|
|
|
typedef struct {
|
|
suid_entry *e;
|
|
int n;
|
|
int max;
|
|
int truncated;
|
|
} suid_ctx;
|
|
|
|
static void suid_scan_dir(suid_ctx *ctx, const char *dir)
|
|
{
|
|
DIR *d = opendir(dir);
|
|
struct dirent *ent;
|
|
|
|
if (d == NULL)
|
|
return;
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char path[1024];
|
|
struct stat st;
|
|
long size = 0;
|
|
|
|
if (ctx->n >= ctx->max) {
|
|
ctx->truncated = 1;
|
|
break;
|
|
}
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
snprintf(path, sizeof(path), "%.800s/%.160s", dir, ent->d_name);
|
|
if (lstat(path, &st) != 0 || !S_ISREG(st.st_mode))
|
|
continue;
|
|
if ((st.st_mode & (S_ISUID | S_ISGID)) == 0)
|
|
continue;
|
|
snprintf(ctx->e[ctx->n].path, sizeof(ctx->e[0].path), "%.500s",
|
|
path);
|
|
snprintf(ctx->e[ctx->n].mode, sizeof(ctx->e[0].mode), "%04o",
|
|
(unsigned)(st.st_mode & 07777));
|
|
ctx->e[ctx->n].hex[0] = '\0';
|
|
if (hash_file(path, ctx->e[ctx->n].hex, &size) != 0)
|
|
ctx->e[ctx->n].hex[0] = '\0';
|
|
ctx->e[ctx->n].seen = 0;
|
|
ctx->n++;
|
|
}
|
|
closedir(d);
|
|
}
|
|
|
|
static void t_suid_baseline(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
static const char *dirs[] = {
|
|
"/usr/bin", "/usr/sbin", "/usr/local/bin", "/usr/local/sbin",
|
|
"/bin", "/sbin", NULL
|
|
};
|
|
const char *action = mcp_arg_string(args, "action");
|
|
const char *baseline = mcp_arg_string(args, "baseline");
|
|
const char *root = mcp_arg_string(args, "root");
|
|
int limit = mcp_arg_int(args, "limit", 2000);
|
|
char base[512];
|
|
size_t bl;
|
|
char base_path[APEX_PATH_FULL];
|
|
char path[1024];
|
|
suid_entry *current = NULL;
|
|
suid_entry *base_entries = NULL;
|
|
int ncur = 0;
|
|
int nbase = 0;
|
|
int create;
|
|
int i;
|
|
mcp_buf b;
|
|
|
|
(void)user;
|
|
if (action == NULL || *action == '\0')
|
|
action = "verify";
|
|
create = strcmp(action, "create") == 0;
|
|
if (!create && strcmp(action, "verify") != 0) {
|
|
mcp_reply_error(c, "action must be create or verify");
|
|
return;
|
|
}
|
|
if (limit <= 0 || limit > 10000)
|
|
limit = 2000;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) >= sizeof(base) - 2) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(base, sizeof(base), "%s", root);
|
|
bl = strlen(base);
|
|
while (bl > 0 && base[bl - 1] == '/')
|
|
base[--bl] = '\0';
|
|
if (baseline != NULL && *baseline != '\0')
|
|
snprintf(base_path, sizeof(base_path), "%s", baseline);
|
|
else
|
|
snprintf(base_path, sizeof(base_path), "%s/suid-baseline.jsonl",
|
|
g_cfg.home);
|
|
current = calloc((size_t)limit, sizeof(*current));
|
|
base_entries = calloc(10000, sizeof(*base_entries));
|
|
if (current == NULL || base_entries == NULL) {
|
|
free(current);
|
|
free(base_entries);
|
|
mcp_reply_error(c, "out of memory");
|
|
return;
|
|
}
|
|
{
|
|
suid_ctx ctx;
|
|
int d;
|
|
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.e = current;
|
|
ctx.max = limit;
|
|
for (d = 0; dirs[d] != NULL; d++) {
|
|
struct stat dst;
|
|
|
|
snprintf(path, sizeof(path), "%s%s", base, dirs[d]);
|
|
if (lstat(path, &dst) != 0 || !S_ISDIR(dst.st_mode) ||
|
|
S_ISLNK(dst.st_mode))
|
|
continue;
|
|
suid_scan_dir(&ctx, path);
|
|
}
|
|
ncur = ctx.n;
|
|
}
|
|
if (create) {
|
|
mcp_buf lines;
|
|
|
|
mcp_buf_init(&lines);
|
|
for (i = 0; i < ncur; i++) {
|
|
mcp_buf_puts(&lines, "{\"path\":");
|
|
apex_json_str(&lines, current[i].path);
|
|
mcp_buf_printf(&lines, ",\"mode\":\"%s\",\"sha256\":\"%s\"}\n",
|
|
current[i].mode, current[i].hex);
|
|
}
|
|
if (apex_write_file(base_path, lines.p != NULL ? lines.p : "",
|
|
lines.len, 0600) != 0) {
|
|
mcp_buf_free(&lines);
|
|
free(current);
|
|
free(base_entries);
|
|
mcp_reply_error(c, "cannot write the SUID baseline");
|
|
return;
|
|
}
|
|
mcp_buf_free(&lines);
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"action\":\"create\",\"baseline\":");
|
|
apex_json_str(&b, base_path);
|
|
mcp_buf_printf(&b, ",\"suid\":%d,\"entries\":[", ncur);
|
|
for (i = 0; i < ncur && i < 200; i++) {
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
mcp_buf_puts(&b, "{\"path\":");
|
|
apex_json_str(&b, current[i].path);
|
|
mcp_buf_printf(&b, ",\"mode\":\"%s\"}", current[i].mode);
|
|
}
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
free(current);
|
|
free(base_entries);
|
|
return;
|
|
}
|
|
{
|
|
FILE *f = fopen(base_path, "rb");
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
|
|
if (f == NULL) {
|
|
free(current);
|
|
free(base_entries);
|
|
mcp_reply_error(c, "no SUID baseline; run action=create");
|
|
return;
|
|
}
|
|
while (nbase < 10000 && getline(&line, &cap, f) >= 0) {
|
|
if (!json_str_field(line, "path", base_entries[nbase].path,
|
|
sizeof(base_entries[nbase].path)))
|
|
continue;
|
|
if (!json_str_field(line, "mode", base_entries[nbase].mode,
|
|
sizeof(base_entries[nbase].mode)))
|
|
continue;
|
|
if (!json_str_field(line, "sha256", base_entries[nbase].hex,
|
|
sizeof(base_entries[nbase].hex)))
|
|
base_entries[nbase].hex[0] = '\0';
|
|
base_entries[nbase].seen = 0;
|
|
nbase++;
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"action\":\"verify\",\"baseline\":");
|
|
apex_json_str(&b, base_path);
|
|
mcp_buf_printf(&b, ",\"suid\":%d,\"new\":[", ncur);
|
|
{
|
|
int first = 1;
|
|
|
|
for (i = 0; i < ncur; i++) {
|
|
int j;
|
|
int known = 0;
|
|
|
|
for (j = 0; j < nbase; j++)
|
|
if (strcmp(base_entries[j].path, current[i].path) == 0)
|
|
known = 1;
|
|
if (known)
|
|
continue;
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"path\":");
|
|
apex_json_str(&b, current[i].path);
|
|
mcp_buf_printf(&b, ",\"mode\":\"%s\"}", current[i].mode);
|
|
}
|
|
}
|
|
mcp_buf_puts(&b, "],\"gone\":[");
|
|
{
|
|
int first = 1;
|
|
|
|
for (i = 0; i < nbase; i++) {
|
|
int j;
|
|
int seen = 0;
|
|
|
|
for (j = 0; j < ncur; j++)
|
|
if (strcmp(current[j].path, base_entries[i].path) == 0)
|
|
seen = 1;
|
|
if (seen)
|
|
continue;
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
apex_json_str(&b, base_entries[i].path);
|
|
}
|
|
}
|
|
mcp_buf_puts(&b, "],\"changed\":[");
|
|
{
|
|
int first = 1;
|
|
|
|
for (i = 0; i < ncur; i++) {
|
|
int j;
|
|
|
|
for (j = 0; j < nbase; j++) {
|
|
int mode_changed;
|
|
int hash_changed;
|
|
|
|
if (strcmp(base_entries[j].path, current[i].path) != 0)
|
|
continue;
|
|
mode_changed = strcmp(base_entries[j].mode,
|
|
current[i].mode) != 0;
|
|
hash_changed =
|
|
base_entries[j].hex[0] != '\0' &&
|
|
current[i].hex[0] != '\0' &&
|
|
strcmp(base_entries[j].hex, current[i].hex) != 0;
|
|
if (mode_changed || hash_changed) {
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"path\":");
|
|
apex_json_str(&b, current[i].path);
|
|
mcp_buf_printf(&b, ",\"was_mode\":\"%s\","
|
|
"\"now_mode\":\"%s\",\"was_sha256\":"
|
|
"\"%s\",\"now_sha256\":\"%s\"}",
|
|
base_entries[j].mode, current[i].mode,
|
|
base_entries[j].hex, current[i].hex);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
mcp_buf_printf(&b, "],\"baseline_entries\":%d}", nbase);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
free(current);
|
|
free(base_entries);
|
|
}
|
|
|
|
/* -------------------------------------------------------------- cap_baseline */
|
|
|
|
typedef struct {
|
|
char path[512];
|
|
char caps[192];
|
|
int seen;
|
|
} cap_entry;
|
|
|
|
typedef struct {
|
|
cap_entry *e;
|
|
int n;
|
|
int max;
|
|
} cap_ctx;
|
|
|
|
static void cap_scan_dir(cap_ctx *ctx, const char *dir)
|
|
{
|
|
DIR *d = opendir(dir);
|
|
struct dirent *ent;
|
|
|
|
if (d == NULL)
|
|
return;
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char path[1024];
|
|
struct stat st;
|
|
unsigned char raw[64];
|
|
ssize_t got;
|
|
int i;
|
|
size_t w = 0;
|
|
|
|
if (ctx->n >= ctx->max)
|
|
break;
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
snprintf(path, sizeof(path), "%.800s/%.160s", dir, ent->d_name);
|
|
if (lstat(path, &st) != 0 || !S_ISREG(st.st_mode))
|
|
continue;
|
|
got = getxattr(path, "security.capability", raw, sizeof(raw));
|
|
if (got <= 0)
|
|
continue;
|
|
snprintf(ctx->e[ctx->n].path, sizeof(ctx->e[0].path), "%.500s",
|
|
path);
|
|
for (i = 0; i < (int)got && w + 3 < sizeof(ctx->e[0].caps); i++) {
|
|
snprintf(ctx->e[ctx->n].caps + w,
|
|
sizeof(ctx->e[0].caps) - w, "%02x", raw[i]);
|
|
w += 2;
|
|
}
|
|
ctx->e[ctx->n].caps[w] = '\0';
|
|
ctx->e[ctx->n].seen = 0;
|
|
ctx->n++;
|
|
}
|
|
closedir(d);
|
|
}
|
|
|
|
static void t_cap_baseline(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
static const char *dirs[] = {
|
|
"/usr/bin", "/usr/sbin", "/usr/local/bin", "/usr/local/sbin",
|
|
"/bin", "/sbin", NULL
|
|
};
|
|
const char *action = mcp_arg_string(args, "action");
|
|
const char *baseline = mcp_arg_string(args, "baseline");
|
|
const char *root = mcp_arg_string(args, "root");
|
|
int limit = mcp_arg_int(args, "limit", 2000);
|
|
char base[512];
|
|
size_t bl;
|
|
char base_path[APEX_PATH_FULL];
|
|
char path[1024];
|
|
cap_entry *current = NULL;
|
|
cap_entry *base_entries = NULL;
|
|
int ncur;
|
|
int nbase = 0;
|
|
int create;
|
|
int i;
|
|
mcp_buf b;
|
|
|
|
(void)user;
|
|
if (action == NULL || *action == '\0')
|
|
action = "verify";
|
|
create = strcmp(action, "create") == 0;
|
|
if (!create && strcmp(action, "verify") != 0) {
|
|
mcp_reply_error(c, "action must be create or verify");
|
|
return;
|
|
}
|
|
if (limit <= 0 || limit > 10000)
|
|
limit = 2000;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) >= sizeof(base) - 2) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(base, sizeof(base), "%s", root);
|
|
bl = strlen(base);
|
|
while (bl > 0 && base[bl - 1] == '/')
|
|
base[--bl] = '\0';
|
|
if (baseline != NULL && *baseline != '\0')
|
|
snprintf(base_path, sizeof(base_path), "%s", baseline);
|
|
else
|
|
snprintf(base_path, sizeof(base_path), "%s/cap-baseline.jsonl",
|
|
g_cfg.home);
|
|
current = calloc((size_t)limit, sizeof(*current));
|
|
base_entries = calloc(10000, sizeof(*base_entries));
|
|
if (current == NULL || base_entries == NULL) {
|
|
free(current);
|
|
free(base_entries);
|
|
mcp_reply_error(c, "out of memory");
|
|
return;
|
|
}
|
|
{
|
|
cap_ctx ctx;
|
|
int d;
|
|
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.e = current;
|
|
ctx.max = limit;
|
|
for (d = 0; dirs[d] != NULL; d++) {
|
|
struct stat dst;
|
|
|
|
snprintf(path, sizeof(path), "%s%s", base, dirs[d]);
|
|
if (lstat(path, &dst) != 0 || !S_ISDIR(dst.st_mode) ||
|
|
S_ISLNK(dst.st_mode))
|
|
continue;
|
|
cap_scan_dir(&ctx, path);
|
|
}
|
|
ncur = ctx.n;
|
|
}
|
|
if (create) {
|
|
mcp_buf lines;
|
|
|
|
mcp_buf_init(&lines);
|
|
for (i = 0; i < ncur; i++) {
|
|
mcp_buf_puts(&lines, "{\"path\":");
|
|
apex_json_str(&lines, current[i].path);
|
|
mcp_buf_puts(&lines, ",\"caps\":");
|
|
apex_json_str(&lines, current[i].caps);
|
|
mcp_buf_puts(&lines, "}\n");
|
|
}
|
|
if (apex_write_file(base_path, lines.p != NULL ? lines.p : "",
|
|
lines.len, 0600) != 0) {
|
|
mcp_buf_free(&lines);
|
|
free(current);
|
|
free(base_entries);
|
|
mcp_reply_error(c, "cannot write the capability baseline");
|
|
return;
|
|
}
|
|
mcp_buf_free(&lines);
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"action\":\"create\",\"baseline\":");
|
|
apex_json_str(&b, base_path);
|
|
mcp_buf_printf(&b, ",\"capabilities\":%d}", ncur);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
free(current);
|
|
free(base_entries);
|
|
return;
|
|
}
|
|
{
|
|
FILE *f = fopen(base_path, "rb");
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
|
|
if (f == NULL) {
|
|
free(current);
|
|
free(base_entries);
|
|
mcp_reply_error(c, "no capability baseline; run action=create");
|
|
return;
|
|
}
|
|
while (nbase < 10000 && getline(&line, &cap, f) >= 0) {
|
|
if (!json_str_field(line, "path", base_entries[nbase].path,
|
|
sizeof(base_entries[nbase].path)))
|
|
continue;
|
|
if (!json_str_field(line, "caps", base_entries[nbase].caps,
|
|
sizeof(base_entries[nbase].caps)))
|
|
continue;
|
|
base_entries[nbase].seen = 0;
|
|
nbase++;
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"action\":\"verify\",\"baseline\":");
|
|
apex_json_str(&b, base_path);
|
|
mcp_buf_printf(&b, ",\"capabilities\":%d,\"new\":[", ncur);
|
|
{
|
|
int first = 1;
|
|
|
|
for (i = 0; i < ncur; i++) {
|
|
int j;
|
|
int known = 0;
|
|
|
|
for (j = 0; j < nbase; j++)
|
|
if (strcmp(base_entries[j].path, current[i].path) == 0)
|
|
known = 1;
|
|
if (known)
|
|
continue;
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"path\":");
|
|
apex_json_str(&b, current[i].path);
|
|
mcp_buf_puts(&b, ",\"caps\":");
|
|
apex_json_str(&b, current[i].caps);
|
|
mcp_buf_puts(&b, "}");
|
|
}
|
|
}
|
|
mcp_buf_puts(&b, "],\"gone\":[");
|
|
{
|
|
int first = 1;
|
|
|
|
for (i = 0; i < nbase; i++) {
|
|
int j;
|
|
int seen = 0;
|
|
|
|
for (j = 0; j < ncur; j++)
|
|
if (strcmp(current[j].path, base_entries[i].path) == 0)
|
|
seen = 1;
|
|
if (seen)
|
|
continue;
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
apex_json_str(&b, base_entries[i].path);
|
|
}
|
|
}
|
|
mcp_buf_puts(&b, "],\"changed\":[");
|
|
{
|
|
int first = 1;
|
|
|
|
for (i = 0; i < ncur; i++) {
|
|
int j;
|
|
|
|
for (j = 0; j < nbase; j++) {
|
|
if (strcmp(base_entries[j].path, current[i].path) != 0)
|
|
continue;
|
|
if (strcmp(base_entries[j].caps, current[i].caps) != 0) {
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"path\":");
|
|
apex_json_str(&b, current[i].path);
|
|
mcp_buf_puts(&b, ",\"was\":");
|
|
apex_json_str(&b, base_entries[j].caps);
|
|
mcp_buf_puts(&b, ",\"now\":");
|
|
apex_json_str(&b, current[i].caps);
|
|
mcp_buf_puts(&b, "}");
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
mcp_buf_printf(&b, "],\"baseline_entries\":%d}", nbase);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
free(current);
|
|
free(base_entries);
|
|
}
|
|
|
|
/* --------------------------------------------------------------- module_audit */
|
|
|
|
typedef struct {
|
|
char name[64];
|
|
char taint[32];
|
|
} mod_entry;
|
|
|
|
static const struct {
|
|
int bit;
|
|
const char *name;
|
|
} TAINT_BITS[] = {
|
|
{ 0, "proprietary" },
|
|
{ 1, "forced-module" },
|
|
{ 2, "cpu-out-of-spec" },
|
|
{ 3, "forced-unload" },
|
|
{ 4, "machine-check" },
|
|
{ 5, "bad-page" },
|
|
{ 6, "user" },
|
|
{ 7, "die" },
|
|
{ 8, "acpi-override" },
|
|
{ 9, "warn" },
|
|
{ 10, "staging" },
|
|
{ 11, "firmware-workaround" },
|
|
{ 12, "out-of-tree" },
|
|
{ 13, "unsigned" },
|
|
{ 14, "softlockup" },
|
|
{ 15, "livepatch" },
|
|
{ 16, "aux" },
|
|
{ 17, "randstruct" },
|
|
{ 18, "test" },
|
|
{ -1, NULL }
|
|
};
|
|
|
|
static void t_module_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *modules_path = mcp_arg_string(args, "modules");
|
|
const char *sysfs = mcp_arg_string(args, "sysfs");
|
|
const char *tainted_path = mcp_arg_string(args, "tainted");
|
|
int limit = mcp_arg_int(args, "limit", 500);
|
|
mod_entry *mods = NULL;
|
|
int nmods = 0;
|
|
FILE *f;
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
int tainted = 0;
|
|
int out_of_tree = 0;
|
|
int unsigned_mods = 0;
|
|
int findings = 0;
|
|
char kernel_taint[32];
|
|
unsigned long taint_value = 0;
|
|
mcp_buf b;
|
|
|
|
(void)user;
|
|
if (modules_path == NULL || *modules_path == '\0')
|
|
modules_path = "/proc/modules";
|
|
if (sysfs == NULL || *sysfs == '\0')
|
|
sysfs = "/sys/module";
|
|
if (tainted_path == NULL || *tainted_path == '\0')
|
|
tainted_path = "/proc/sys/kernel/tainted";
|
|
if (limit <= 0 || limit > 5000)
|
|
limit = 500;
|
|
mods = calloc((size_t)limit, sizeof(*mods));
|
|
if (mods == NULL) {
|
|
mcp_reply_error(c, "out of memory");
|
|
return;
|
|
}
|
|
f = fopen(modules_path, "rb");
|
|
if (f == NULL) {
|
|
free(mods);
|
|
mcp_reply_error(c, "cannot read the module list");
|
|
return;
|
|
}
|
|
while (nmods < limit && getline(&line, &cap, f) >= 0) {
|
|
char name[64];
|
|
int w = 0;
|
|
|
|
while (line[w] == ' ' || line[w] == '\t')
|
|
w++;
|
|
{
|
|
int k = 0;
|
|
|
|
while (line[w] != '\0' && line[w] != ' ' &&
|
|
line[w] != '\t' && line[w] != '\n' &&
|
|
k + 1 < (int)sizeof(name))
|
|
name[k++] = line[w++];
|
|
name[k] = '\0';
|
|
}
|
|
if (name[0] == '\0')
|
|
continue;
|
|
snprintf(mods[nmods].name, sizeof(mods[nmods].name), "%s", name);
|
|
mods[nmods].taint[0] = '\0';
|
|
{
|
|
char path[512];
|
|
char buf[64];
|
|
|
|
snprintf(path, sizeof(path), "%.400s/%.60s/taint", sysfs, name);
|
|
if (read_text(path, buf, sizeof(buf)) > 0) {
|
|
trim_eol(buf);
|
|
snprintf(mods[nmods].taint,
|
|
sizeof(mods[nmods].taint), "%.24s", buf);
|
|
}
|
|
}
|
|
nmods++;
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
kernel_taint[0] = '\0';
|
|
if (read_text(tainted_path, kernel_taint, sizeof(kernel_taint)) > 0) {
|
|
trim_eol(kernel_taint);
|
|
taint_value = strtoul(kernel_taint, NULL, 10);
|
|
}
|
|
for (int i = 0; i < nmods; i++) {
|
|
if (mods[i].taint[0] == '\0')
|
|
continue;
|
|
tainted++;
|
|
if (strchr(mods[i].taint, 'O') != NULL)
|
|
out_of_tree++;
|
|
if (strchr(mods[i].taint, 'E') != NULL)
|
|
unsigned_mods++;
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"modules_path\":");
|
|
apex_json_str(&b, modules_path);
|
|
mcp_buf_printf(&b, ",\"modules\":%d,\"tainted_modules\":%d,"
|
|
"\"out_of_tree\":%d,\"unsigned\":%d,"
|
|
"\"kernel_tainted\":%lu,\"kernel_flags\":[",
|
|
nmods, tainted, out_of_tree, unsigned_mods, taint_value);
|
|
{
|
|
int first = 1;
|
|
int t;
|
|
|
|
for (t = 0; TAINT_BITS[t].bit >= 0; t++) {
|
|
if ((taint_value & (1UL << TAINT_BITS[t].bit)) == 0)
|
|
continue;
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
apex_json_str(&b, TAINT_BITS[t].name);
|
|
}
|
|
}
|
|
mcp_buf_puts(&b, "],\"findings\":[");
|
|
{
|
|
int first = 1;
|
|
|
|
for (int i = 0; i < nmods; i++) {
|
|
if (mods[i].taint[0] == '\0')
|
|
continue;
|
|
findings++;
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"module\":");
|
|
apex_json_str(&b, mods[i].name);
|
|
mcp_buf_puts(&b, ",\"taint\":");
|
|
apex_json_str(&b, mods[i].taint);
|
|
mcp_buf_puts(&b, "}");
|
|
}
|
|
}
|
|
mcp_buf_printf(&b, "],\"findings_count\":%d}", findings);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
free(mods);
|
|
}
|
|
|
|
/* ---------------------------------------------------------- package_integrity */
|
|
|
|
static void pi_add(mcp_buf *files, int *first, int *modified, int *emitted,
|
|
int limit, const char *line)
|
|
{
|
|
char copy[1024];
|
|
char *flags;
|
|
char *path;
|
|
size_t n;
|
|
|
|
while (*line == ' ' || *line == '\t')
|
|
line++;
|
|
n = strlen(line);
|
|
while (n > 0 && (line[n - 1] == '\n' || line[n - 1] == '\r' ||
|
|
line[n - 1] == ' ' || line[n - 1] == '\t'))
|
|
n--;
|
|
if (n == 0)
|
|
return;
|
|
if (n >= sizeof(copy))
|
|
n = sizeof(copy) - 1;
|
|
memcpy(copy, line, n);
|
|
copy[n] = '\0';
|
|
flags = copy;
|
|
path = strchr(flags, ' ');
|
|
if (path == NULL)
|
|
path = strchr(flags, '\t');
|
|
if (path == NULL)
|
|
return;
|
|
*path++ = '\0';
|
|
while (*path == ' ' || *path == '\t')
|
|
path++;
|
|
if (*path == '\0')
|
|
return;
|
|
(*modified)++;
|
|
if (*emitted >= limit)
|
|
return;
|
|
if (!*first)
|
|
mcp_buf_puts(files, ",");
|
|
*first = 0;
|
|
(*emitted)++;
|
|
mcp_buf_puts(files, "{\"flags\":");
|
|
apex_json_str(files, flags);
|
|
mcp_buf_puts(files, ",\"path\":");
|
|
apex_json_str(files, path);
|
|
mcp_buf_puts(files, "}");
|
|
}
|
|
|
|
static void t_package_integrity(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *manager = mcp_arg_string(args, "manager");
|
|
const char *package = mcp_arg_string(args, "package");
|
|
const char *output = mcp_arg_string(args, "output");
|
|
int limit = mcp_arg_int(args, "limit", 200);
|
|
const char *tool = NULL;
|
|
char *argv[5];
|
|
apex_proc p;
|
|
int rc;
|
|
int exit_code = 0;
|
|
mcp_buf files;
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int modified = 0;
|
|
int emitted = 0;
|
|
|
|
(void)user;
|
|
if (limit <= 0 || limit > 5000)
|
|
limit = 200;
|
|
if (manager == NULL || *manager == '\0')
|
|
manager = "auto";
|
|
if (output != NULL && *output != '\0') {
|
|
FILE *f = fopen(output, "rb");
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
|
|
if (f == NULL) {
|
|
mcp_reply_error(c, "cannot read the saved verify output");
|
|
return;
|
|
}
|
|
tool = strcmp(manager, "rpm") == 0 ? "rpm" : "dpkg";
|
|
mcp_buf_init(&files);
|
|
while (getline(&line, &cap, f) >= 0)
|
|
pi_add(&files, &first, &modified, &emitted, limit, line);
|
|
free(line);
|
|
fclose(f);
|
|
} else {
|
|
if (strcmp(manager, "auto") == 0) {
|
|
if (apex_have("dpkg"))
|
|
tool = "dpkg";
|
|
else if (apex_have("rpm"))
|
|
tool = "rpm";
|
|
} else if (strcmp(manager, "dpkg") == 0 && apex_have("dpkg")) {
|
|
tool = "dpkg";
|
|
} else if (strcmp(manager, "rpm") == 0 && apex_have("rpm")) {
|
|
tool = "rpm";
|
|
}
|
|
if (tool == NULL) {
|
|
mcp_reply_error(c,
|
|
"neither dpkg nor rpm is available (or the "
|
|
"requested manager is not installed)");
|
|
return;
|
|
}
|
|
if (strcmp(tool, "dpkg") == 0) {
|
|
argv[0] = "dpkg";
|
|
argv[1] = "-V";
|
|
argv[2] = (package != NULL && *package != '\0')
|
|
? (char *)package : NULL;
|
|
argv[3] = NULL;
|
|
} else {
|
|
argv[0] = "rpm";
|
|
argv[1] = (package != NULL && *package != '\0') ? "-V" : "-Va";
|
|
argv[2] = (package != NULL && *package != '\0')
|
|
? (char *)package : NULL;
|
|
argv[3] = NULL;
|
|
}
|
|
rc = apex_run(argv, 300000, g_cfg.max_output, &p);
|
|
if (rc != 0) {
|
|
mcp_reply_error(c, "cannot run the package verifier");
|
|
return;
|
|
}
|
|
exit_code = p.exit_code;
|
|
mcp_buf_init(&files);
|
|
if (p.out != NULL) {
|
|
char *copy = strdup(p.out);
|
|
char *save = NULL;
|
|
char *line;
|
|
|
|
if (copy != NULL) {
|
|
line = strtok_r(copy, "\n", &save);
|
|
while (line != NULL) {
|
|
pi_add(&files, &first, &modified, &emitted, limit,
|
|
line);
|
|
line = strtok_r(NULL, "\n", &save);
|
|
}
|
|
free(copy);
|
|
}
|
|
}
|
|
apex_proc_free(&p);
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"tool\":");
|
|
apex_json_str(&b, tool);
|
|
mcp_buf_printf(&b, ",\"package\":");
|
|
apex_json_str(&b, package != NULL ? package : "");
|
|
mcp_buf_printf(&b, ",\"exit\":%d,\"modified\":%d,\"truncated\":%s,"
|
|
"\"files\":[", exit_code, modified,
|
|
emitted < modified ? "true" : "false");
|
|
if (files.p != NULL)
|
|
mcp_buf_add(&b, files.p, files.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&files);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- mount_audit */
|
|
|
|
static int mount_has_opt(const char *opts, const char *want)
|
|
{
|
|
const char *p = opts;
|
|
size_t wl = strlen(want);
|
|
|
|
while (p != NULL && *p != '\0') {
|
|
const char *comma = strchr(p, ',');
|
|
size_t len = comma != NULL ? (size_t)(comma - p) : strlen(p);
|
|
|
|
if (len == wl && strncmp(p, want, wl) == 0)
|
|
return 1;
|
|
p = comma != NULL ? comma + 1 : NULL;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int mount_needs(const char *mount, const char **opts, int extra_exec)
|
|
{
|
|
if (strcmp(mount, "/tmp") == 0 || strncmp(mount, "/tmp/", 5) == 0 ||
|
|
strcmp(mount, "/var/tmp") == 0 ||
|
|
strncmp(mount, "/var/tmp/", 9) == 0 ||
|
|
strcmp(mount, "/dev/shm") == 0 ||
|
|
strncmp(mount, "/dev/shm/", 9) == 0) {
|
|
opts[0] = "nosuid";
|
|
opts[1] = "nodev";
|
|
opts[2] = extra_exec ? "noexec" : NULL;
|
|
return 1;
|
|
}
|
|
if (strcmp(mount, "/home") == 0 || strncmp(mount, "/home/", 6) == 0 ||
|
|
strncmp(mount, "/run/user", 9) == 0 ||
|
|
strcmp(mount, "/media") == 0 || strncmp(mount, "/media/", 7) == 0 ||
|
|
strcmp(mount, "/mnt") == 0 || strncmp(mount, "/mnt/", 5) == 0) {
|
|
opts[0] = "nosuid";
|
|
opts[1] = "nodev";
|
|
opts[2] = NULL;
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void t_mount_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *path = mcp_arg_string(args, "path");
|
|
int limit = mcp_arg_int(args, "limit", 500);
|
|
FILE *f;
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int mounts = 0;
|
|
int findings = 0;
|
|
|
|
(void)user;
|
|
if (path == NULL || *path == '\0')
|
|
path = "/proc/mounts";
|
|
if (limit <= 0 || limit > 5000)
|
|
limit = 500;
|
|
f = fopen(path, "rb");
|
|
if (f == NULL) {
|
|
mcp_reply_error(c, "cannot read the mount table");
|
|
return;
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"path\":");
|
|
apex_json_str(&b, path);
|
|
mcp_buf_puts(&b, ",\"findings\":[");
|
|
while (mounts < limit && getline(&line, &cap, f) >= 0) {
|
|
char dev[256];
|
|
char mount[512];
|
|
char fstype[64];
|
|
char opts[1024];
|
|
const char *needed[3];
|
|
const char *missing[3];
|
|
int nmissing = 0;
|
|
int extra_exec;
|
|
int k;
|
|
|
|
if (sscanf(line, "%255s %511s %63s %1023s", dev, mount, fstype,
|
|
opts) != 4)
|
|
continue;
|
|
mounts++;
|
|
extra_exec = 1;
|
|
if (!mount_needs(mount, needed, extra_exec))
|
|
continue;
|
|
for (k = 0; k < 3 && needed[k] != NULL; k++)
|
|
if (!mount_has_opt(opts, needed[k]))
|
|
missing[nmissing++] = needed[k];
|
|
if (nmissing == 0)
|
|
continue;
|
|
findings++;
|
|
if (!first)
|
|
mcp_buf_puts(&b, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&b, "{\"mount\":");
|
|
apex_json_str(&b, mount);
|
|
mcp_buf_puts(&b, ",\"fstype\":");
|
|
apex_json_str(&b, fstype);
|
|
mcp_buf_puts(&b, ",\"missing\":[");
|
|
for (k = 0; k < nmissing; k++) {
|
|
if (k > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
apex_json_str(&b, missing[k]);
|
|
}
|
|
mcp_buf_printf(&b, "],\"severity\":\"%s\"}",
|
|
nmissing >= 2 ? "medium" : "low");
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
mcp_buf_printf(&b, "],\"mounts\":%d,\"findings_count\":%d}", mounts,
|
|
findings);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ------------------------------------------------------------- firewall_audit */
|
|
|
|
typedef struct {
|
|
int chains;
|
|
int rules;
|
|
char accepts[32][64];
|
|
int naccepts;
|
|
} fw_ctx;
|
|
|
|
static void fw_accept(fw_ctx *ctx, const char *name)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < ctx->naccepts; i++)
|
|
if (strcmp(ctx->accepts[i], name) == 0)
|
|
return;
|
|
if (ctx->naccepts < 32)
|
|
snprintf(ctx->accepts[ctx->naccepts++], sizeof(ctx->accepts[0]),
|
|
"%.48s", name);
|
|
}
|
|
|
|
static void fw_parse_nft(char *copy, fw_ctx *ctx)
|
|
{
|
|
char *save = NULL;
|
|
char *line;
|
|
char current[64];
|
|
|
|
current[0] = '\0';
|
|
line = strtok_r(copy, "\n", &save);
|
|
while (line != NULL) {
|
|
char *t = line;
|
|
size_t n;
|
|
|
|
while (*t == ' ' || *t == '\t')
|
|
t++;
|
|
n = strlen(t);
|
|
while (n > 0 && (t[n - 1] == '\r' || t[n - 1] == ' '))
|
|
t[--n] = '\0';
|
|
if (strncmp(t, "chain ", 6) == 0) {
|
|
size_t w = 0;
|
|
|
|
t += 6;
|
|
while (*t != '\0' && *t != ' ' && *t != '{' &&
|
|
w + 1 < sizeof(current))
|
|
current[w++] = *t++;
|
|
current[w] = '\0';
|
|
ctx->chains++;
|
|
} else if (strstr(t, "policy accept") != NULL) {
|
|
if (current[0] != '\0')
|
|
fw_accept(ctx, current);
|
|
} else if (*t != '\0' && *t != '}' &&
|
|
strncmp(t, "type ", 5) != 0 &&
|
|
strncmp(t, "policy ", 7) != 0 &&
|
|
strncmp(t, "table ", 6) != 0) {
|
|
ctx->rules++;
|
|
}
|
|
line = strtok_r(NULL, "\n", &save);
|
|
}
|
|
}
|
|
|
|
static void fw_parse_iptables(char *copy, fw_ctx *ctx)
|
|
{
|
|
char *save = NULL;
|
|
char *line;
|
|
|
|
line = strtok_r(copy, "\n", &save);
|
|
while (line != NULL) {
|
|
if (line[0] == ':') {
|
|
char name[64];
|
|
char policy[32];
|
|
|
|
if (sscanf(line + 1, "%63s %31s", name, policy) == 2) {
|
|
ctx->chains++;
|
|
if (strcmp(policy, "ACCEPT") == 0)
|
|
fw_accept(ctx, name);
|
|
}
|
|
} else if (strncmp(line, "-A ", 3) == 0 ||
|
|
strncmp(line, "-I ", 3) == 0) {
|
|
ctx->rules++;
|
|
}
|
|
line = strtok_r(NULL, "\n", &save);
|
|
}
|
|
}
|
|
|
|
static void t_firewall_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *backend = mcp_arg_string(args, "backend");
|
|
const char *output = mcp_arg_string(args, "output");
|
|
int limit = mcp_arg_int(args, "limit", 500);
|
|
char *argv[5];
|
|
apex_proc p;
|
|
int rc;
|
|
int exit_code = 0;
|
|
int nft = 0;
|
|
char *text = NULL;
|
|
size_t text_len = 0;
|
|
fw_ctx ctx;
|
|
mcp_buf b;
|
|
int i;
|
|
|
|
(void)user;
|
|
if (backend == NULL || *backend == '\0')
|
|
backend = "auto";
|
|
if (limit <= 0 || limit > 100000)
|
|
limit = 500;
|
|
(void)limit;
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
if (output != NULL && *output != '\0') {
|
|
FILE *f = fopen(output, "rb");
|
|
long size;
|
|
struct stat st;
|
|
|
|
if (f == NULL || stat(output, &st) != 0 || st.st_size <= 0 ||
|
|
st.st_size > (4L << 20)) {
|
|
if (f != NULL)
|
|
fclose(f);
|
|
mcp_reply_error(c, "cannot read the saved ruleset");
|
|
return;
|
|
}
|
|
size = (long)st.st_size;
|
|
text = malloc((size_t)size + 1);
|
|
if (text == NULL) {
|
|
fclose(f);
|
|
mcp_reply_error(c, "out of memory");
|
|
return;
|
|
}
|
|
if (fread(text, 1, (size_t)size, f) != (size_t)size) {
|
|
free(text);
|
|
fclose(f);
|
|
mcp_reply_error(c, "cannot read the saved ruleset");
|
|
return;
|
|
}
|
|
fclose(f);
|
|
text[size] = '\0';
|
|
text_len = (size_t)size;
|
|
nft = strncmp(text, "*", 1) != 0;
|
|
if (strcmp(backend, "nft") == 0)
|
|
nft = 1;
|
|
else if (strcmp(backend, "iptables") == 0)
|
|
nft = 0;
|
|
} else {
|
|
if (strcmp(backend, "auto") == 0) {
|
|
if (apex_have("nft"))
|
|
nft = 1;
|
|
else if (apex_have("iptables-save"))
|
|
nft = 0;
|
|
else {
|
|
mcp_reply_error(c, "neither nft nor iptables-save is available");
|
|
return;
|
|
}
|
|
} else if (strcmp(backend, "nft") == 0 && apex_have("nft")) {
|
|
nft = 1;
|
|
} else if (strcmp(backend, "iptables") == 0 &&
|
|
apex_have("iptables-save")) {
|
|
nft = 0;
|
|
} else {
|
|
mcp_reply_error(c, "the requested firewall tool is not available");
|
|
return;
|
|
}
|
|
if (nft) {
|
|
argv[0] = "nft";
|
|
argv[1] = "list";
|
|
argv[2] = "ruleset";
|
|
argv[3] = NULL;
|
|
} else {
|
|
argv[0] = "iptables-save";
|
|
argv[1] = NULL;
|
|
}
|
|
rc = apex_run(argv, 20000, g_cfg.max_output, &p);
|
|
if (rc != 0) {
|
|
mcp_reply_error(c, "cannot run the firewall tool");
|
|
return;
|
|
}
|
|
exit_code = p.exit_code;
|
|
if (p.out == NULL || p.out_len == 0) {
|
|
mcp_reply_error(c, p.exit_code == 0
|
|
? "the firewall tool returned nothing"
|
|
: "cannot read the firewall ruleset");
|
|
apex_proc_free(&p);
|
|
return;
|
|
}
|
|
text = strdup(p.out);
|
|
text_len = p.out_len;
|
|
apex_proc_free(&p);
|
|
if (text == NULL) {
|
|
mcp_reply_error(c, "out of memory");
|
|
return;
|
|
}
|
|
}
|
|
if (nft)
|
|
fw_parse_nft(text, &ctx);
|
|
else
|
|
fw_parse_iptables(text, &ctx);
|
|
mcp_buf_init(&b);
|
|
mcp_buf_printf(&b, "{\"backend\":\"%s\",\"source\":\"%s\",\"exit\":%d,"
|
|
"\"chains\":%d,\"rules\":%d,\"accept_policies\":[",
|
|
nft ? "nft" : "iptables",
|
|
output != NULL && *output != '\0' ? "file" : "live",
|
|
exit_code, ctx.chains, ctx.rules);
|
|
for (i = 0; i < ctx.naccepts; i++) {
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
apex_json_str(&b, ctx.accepts[i]);
|
|
}
|
|
mcp_buf_printf(&b, "],\"findings_count\":%d}", ctx.naccepts);
|
|
free(text);
|
|
(void)text_len;
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ------------------------------------------------------------- systemd_audit */
|
|
|
|
typedef struct {
|
|
mcp_buf *b;
|
|
int first;
|
|
int count;
|
|
int units;
|
|
int enabled;
|
|
int max_units;
|
|
} sd_ctx;
|
|
|
|
static void sd_finding(sd_ctx *ctx, const char *path, const char *kind,
|
|
const char *severity, const char *detail)
|
|
{
|
|
if (ctx->count >= 100)
|
|
return;
|
|
if (!ctx->first)
|
|
mcp_buf_puts(ctx->b, ",");
|
|
ctx->first = 0;
|
|
ctx->count++;
|
|
mcp_buf_puts(ctx->b, "{\"path\":");
|
|
apex_json_str(ctx->b, path);
|
|
mcp_buf_puts(ctx->b, ",\"kind\":");
|
|
apex_json_str(ctx->b, kind);
|
|
mcp_buf_puts(ctx->b, ",\"severity\":");
|
|
apex_json_str(ctx->b, severity);
|
|
mcp_buf_puts(ctx->b, ",\"detail\":");
|
|
apex_json_str(ctx->b, detail);
|
|
mcp_buf_puts(ctx->b, "}");
|
|
}
|
|
|
|
static int sd_unit_name(const char *name)
|
|
{
|
|
static const char *suffixes[] = { ".service", ".timer", ".socket",
|
|
".mount", ".target", NULL };
|
|
int i;
|
|
|
|
for (i = 0; suffixes[i] != NULL; i++) {
|
|
size_t nl = strlen(name);
|
|
size_t sl = strlen(suffixes[i]);
|
|
|
|
if (nl > sl && strcmp(name + nl - sl, suffixes[i]) == 0)
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void sd_scan_file(sd_ctx *ctx, const char *path)
|
|
{
|
|
static char text[65536];
|
|
struct stat st;
|
|
int is_service;
|
|
const char *exec;
|
|
const char *tmp_path;
|
|
|
|
if (ctx->units >= ctx->max_units)
|
|
return;
|
|
if (strstr(path, ".wants/") != NULL || strstr(path, ".requires/") != NULL)
|
|
return;
|
|
if (stat(path, &st) != 0 || !S_ISREG(st.st_mode))
|
|
return;
|
|
is_service = strstr(path, ".service") != NULL &&
|
|
strcmp(path + strlen(path) - 8, ".service") == 0;
|
|
ctx->units++;
|
|
if (read_text(path, text, sizeof(text)) < 0)
|
|
return;
|
|
if ((st.st_mode & 0002) != 0)
|
|
sd_finding(ctx, path, "world-writable-unit", "high",
|
|
"unit file is writable by anyone");
|
|
exec = strstr(text, "ExecStart=");
|
|
if (exec != NULL) {
|
|
const char *eol = strchr(exec, '\n');
|
|
|
|
tmp_path = strstr(exec, "/tmp/");
|
|
if (tmp_path == NULL)
|
|
tmp_path = strstr(exec, "/dev/shm/");
|
|
if (tmp_path == NULL)
|
|
tmp_path = strstr(exec, "/var/tmp/");
|
|
if (tmp_path != NULL && (eol == NULL || tmp_path < eol))
|
|
sd_finding(ctx, path, "exec-start-temp", "medium",
|
|
"ExecStart runs a binary from a temporary directory");
|
|
}
|
|
if (is_service && strstr(text, "User=") == NULL)
|
|
sd_finding(ctx, path, "runs-as-root", "low",
|
|
"service has no User= and runs as root");
|
|
}
|
|
|
|
static void sd_scan_dir(sd_ctx *ctx, const char *dir)
|
|
{
|
|
DIR *d = opendir(dir);
|
|
struct dirent *ent;
|
|
|
|
if (d == NULL)
|
|
return;
|
|
while ((ent = readdir(d)) != NULL && ctx->units < ctx->max_units) {
|
|
char path[1024];
|
|
struct stat st;
|
|
size_t dl = strlen(dir);
|
|
size_t nl = strlen(ent->d_name);
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
if (dl + nl + 2 > sizeof(path))
|
|
continue;
|
|
memcpy(path, dir, dl);
|
|
path[dl] = '/';
|
|
memcpy(path + dl + 1, ent->d_name, nl + 1);
|
|
if (lstat(path, &st) != 0)
|
|
continue;
|
|
if (S_ISDIR(st.st_mode)) {
|
|
if (strstr(path, ".wants") != NULL ||
|
|
strstr(path, ".requires") != NULL) {
|
|
DIR *wd = opendir(path);
|
|
struct dirent *we;
|
|
|
|
if (wd == NULL)
|
|
continue;
|
|
while ((we = readdir(wd)) != NULL) {
|
|
if (we->d_name[0] == '.')
|
|
continue;
|
|
ctx->enabled++;
|
|
}
|
|
closedir(wd);
|
|
}
|
|
continue;
|
|
}
|
|
if (S_ISLNK(st.st_mode))
|
|
continue;
|
|
if (sd_unit_name(ent->d_name))
|
|
sd_scan_file(ctx, path);
|
|
}
|
|
closedir(d);
|
|
}
|
|
|
|
static void t_systemd_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
int limit = mcp_arg_int(args, "limit", 500);
|
|
char base[512];
|
|
char path[1024];
|
|
size_t bl;
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
sd_ctx ctx;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) >= sizeof(base) - 2) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(base, sizeof(base), "%s", root);
|
|
bl = strlen(base);
|
|
while (bl > 0 && base[bl - 1] == '/')
|
|
base[--bl] = '\0';
|
|
if (limit <= 0 || limit > 5000)
|
|
limit = 500;
|
|
mcp_buf_init(&findings);
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.b = &findings;
|
|
ctx.first = 1;
|
|
ctx.max_units = limit;
|
|
snprintf(path, sizeof(path), "%s/etc/systemd/system", base);
|
|
sd_scan_dir(&ctx, path);
|
|
snprintf(path, sizeof(path), "%s/usr/lib/systemd/system", base);
|
|
sd_scan_dir(&ctx, path);
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, base);
|
|
mcp_buf_printf(&b, ",\"units\":%d,\"enabled_links\":%d,"
|
|
"\"findings_count\":%d,\"findings\":[",
|
|
ctx.units, ctx.enabled, ctx.count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* ------------------------------------------------------------ kernel_hardening */
|
|
|
|
typedef struct {
|
|
const char *id;
|
|
const char *path;
|
|
const char *expect;
|
|
const char *severity;
|
|
int kind; /* 0 exact, 1 at-least, 2 not-pipe */
|
|
int want;
|
|
} kh_check;
|
|
|
|
static const kh_check KH_CHECKS[] = {
|
|
{ "aslr", "/proc/sys/kernel/randomize_va_space", "2", "high", 0, 2 },
|
|
{ "kptr-restrict", "/proc/sys/kernel/kptr_restrict", "1 or 2", "medium", 1, 1 },
|
|
{ "dmesg-restrict", "/proc/sys/kernel/dmesg_restrict", "1", "medium", 0, 1 },
|
|
{ "perf-paranoid", "/proc/sys/kernel/perf_event_paranoid", ">= 2", "medium", 1, 2 },
|
|
{ "unprivileged-bpf", "/proc/sys/kernel/unprivileged_bpf_disabled", "1", "medium", 1, 1 },
|
|
{ "ptrace-scope", "/proc/sys/kernel/yama/ptrace_scope", ">= 1", "medium", 1, 1 },
|
|
{ "protected-hardlinks", "/proc/sys/fs/protected_hardlinks", "1", "medium", 0, 1 },
|
|
{ "protected-symlinks", "/proc/sys/fs/protected_symlinks", "1", "medium", 0, 1 },
|
|
{ "suid-dumpable", "/proc/sys/fs/suid_dumpable", "0", "high", 0, 0 },
|
|
{ "modules-disabled", "/proc/sys/kernel/modules_disabled", "1", "low", 0, 1 },
|
|
{ "bpf-jit-harden", "/proc/sys/net/core/bpf_jit_harden", "1 or 2", "low", 1, 1 },
|
|
{ "core-pattern", "/proc/sys/kernel/core_pattern", "no pipe", "high", 2, 0 },
|
|
};
|
|
|
|
static void t_kernel_hardening(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
int limit = mcp_arg_int(args, "limit", 64);
|
|
char base[512];
|
|
size_t bl;
|
|
char path[1024];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
int first = 1;
|
|
int count = 0;
|
|
int score = 100;
|
|
int i;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) >= sizeof(base) - 2) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(base, sizeof(base), "%s", root);
|
|
bl = strlen(base);
|
|
while (bl > 0 && base[bl - 1] == '/')
|
|
base[--bl] = '\0';
|
|
if (limit <= 0 || limit > 64)
|
|
limit = 64;
|
|
mcp_buf_init(&findings);
|
|
for (i = 0; i < (int)(sizeof(KH_CHECKS) / sizeof(KH_CHECKS[0])) &&
|
|
i < limit; i++) {
|
|
char value[128];
|
|
int v;
|
|
int ok = 0;
|
|
|
|
snprintf(path, sizeof(path), "%s%s", base, KH_CHECKS[i].path);
|
|
if (read_text(path, value, sizeof(value)) <= 0)
|
|
continue;
|
|
trim_eol(value);
|
|
v = atoi(value);
|
|
if (KH_CHECKS[i].kind == 0)
|
|
ok = v == KH_CHECKS[i].want;
|
|
else if (KH_CHECKS[i].kind == 1)
|
|
ok = v >= KH_CHECKS[i].want;
|
|
else
|
|
ok = value[0] != '|';
|
|
if (ok)
|
|
continue;
|
|
count++;
|
|
if (count >= 2)
|
|
score -= 8;
|
|
else
|
|
score -= 10;
|
|
if (!first)
|
|
mcp_buf_puts(&findings, ",");
|
|
first = 0;
|
|
mcp_buf_puts(&findings, "{\"id\":");
|
|
apex_json_str(&findings, KH_CHECKS[i].id);
|
|
mcp_buf_puts(&findings, ",\"severity\":");
|
|
apex_json_str(&findings, KH_CHECKS[i].severity);
|
|
mcp_buf_puts(&findings, ",\"value\":");
|
|
apex_json_str(&findings, value);
|
|
mcp_buf_puts(&findings, ",\"expected\":");
|
|
apex_json_str(&findings, KH_CHECKS[i].expect);
|
|
mcp_buf_puts(&findings, "}");
|
|
}
|
|
if (score < 0)
|
|
score = 0;
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, base);
|
|
mcp_buf_printf(&b, ",\"score\":%d,\"findings_count\":%d,\"findings\":[",
|
|
score, count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* -------------------------------------------------------------- sudoers_audit */
|
|
|
|
typedef struct {
|
|
mcp_buf *b;
|
|
int first;
|
|
int count;
|
|
int files;
|
|
int rules;
|
|
} sudo_ctx;
|
|
|
|
static void sudo_finding(sudo_ctx *ctx, const char *path, int line,
|
|
const char *kind, const char *severity,
|
|
const char *detail)
|
|
{
|
|
if (ctx->count >= 100)
|
|
return;
|
|
if (!ctx->first)
|
|
mcp_buf_puts(ctx->b, ",");
|
|
ctx->first = 0;
|
|
ctx->count++;
|
|
mcp_buf_puts(ctx->b, "{\"path\":");
|
|
apex_json_str(ctx->b, path);
|
|
mcp_buf_printf(ctx->b, ",\"line\":%d,\"kind\":", line);
|
|
apex_json_str(ctx->b, kind);
|
|
mcp_buf_puts(ctx->b, ",\"severity\":");
|
|
apex_json_str(ctx->b, severity);
|
|
mcp_buf_puts(ctx->b, ",\"detail\":");
|
|
apex_json_str(ctx->b, detail);
|
|
mcp_buf_puts(ctx->b, "}");
|
|
}
|
|
|
|
static int sudo_direct_user(const char *line)
|
|
{
|
|
static const char *prefixes[] = {
|
|
"Defaults", "User_Alias", "Runas_Alias", "Host_Alias",
|
|
"Cmnd_Alias", "#include", "#includedir", "%", "+", NULL
|
|
};
|
|
int i;
|
|
|
|
if (line[0] == '#' || line[0] == '\0')
|
|
return 0;
|
|
for (i = 0; prefixes[i] != NULL; i++)
|
|
if (strncmp(line, prefixes[i], strlen(prefixes[i])) == 0)
|
|
return 0;
|
|
return isalpha((unsigned char)line[0]) != 0;
|
|
}
|
|
|
|
static void sudo_scan_file(sudo_ctx *ctx, const char *path)
|
|
{
|
|
FILE *f;
|
|
struct stat st;
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
int lineno = 0;
|
|
|
|
if (ctx->files >= 200)
|
|
return;
|
|
f = fopen(path, "rb");
|
|
if (f == NULL)
|
|
return;
|
|
ctx->files++;
|
|
if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0)
|
|
sudo_finding(ctx, path, 0, "world-writable", "high",
|
|
"sudoers file is writable by anyone");
|
|
while (getline(&line, &cap, f) >= 0) {
|
|
char *t = line;
|
|
size_t n;
|
|
|
|
lineno++;
|
|
while (*t == ' ' || *t == '\t')
|
|
t++;
|
|
n = strlen(t);
|
|
while (n > 0 && (t[n - 1] == '\n' || t[n - 1] == '\r'))
|
|
t[--n] = '\0';
|
|
if (t[0] == '\0' || t[0] == '#')
|
|
continue;
|
|
ctx->rules++;
|
|
if (strstr(t, "NOPASSWD") != NULL)
|
|
sudo_finding(ctx, path, lineno, "nopasswd", "medium",
|
|
"rule allows running sudo without a password");
|
|
if (strstr(t, "!authenticate") != NULL)
|
|
sudo_finding(ctx, path, lineno, "no-authenticate", "high",
|
|
"rule disables authentication");
|
|
if (strstr(t, "ALL=(ALL") != NULL &&
|
|
strstr(t, "NOPASSWD") == NULL)
|
|
sudo_finding(ctx, path, lineno, "broad-all", "medium",
|
|
"rule grants every command as every user");
|
|
if (sudo_direct_user(t) && strstr(t, "ALL") != NULL)
|
|
sudo_finding(ctx, path, lineno, "direct-user", "low",
|
|
"rule names a user directly instead of an alias");
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
}
|
|
|
|
static void t_sudoers_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
char base[512];
|
|
char path[1024];
|
|
size_t bl;
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
sudo_ctx ctx;
|
|
DIR *d;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) >= sizeof(base) - 2) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(base, sizeof(base), "%s", root);
|
|
bl = strlen(base);
|
|
while (bl > 0 && base[bl - 1] == '/')
|
|
base[--bl] = '\0';
|
|
mcp_buf_init(&findings);
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.b = &findings;
|
|
ctx.first = 1;
|
|
snprintf(path, sizeof(path), "%s/etc/sudoers", base);
|
|
sudo_scan_file(&ctx, path);
|
|
snprintf(path, sizeof(path), "%s/etc/sudoers.d", base);
|
|
d = opendir(path);
|
|
if (d != NULL) {
|
|
struct dirent *ent;
|
|
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char full[1024];
|
|
struct stat st;
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
snprintf(full, sizeof(full), "%.800s/%.160s", path,
|
|
ent->d_name);
|
|
if (stat(full, &st) != 0 || !S_ISREG(st.st_mode))
|
|
continue;
|
|
sudo_scan_file(&ctx, full);
|
|
}
|
|
closedir(d);
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, base);
|
|
mcp_buf_printf(&b, ",\"files\":%d,\"rules\":%d,\"findings_count\":%d,"
|
|
"\"findings\":[", ctx.files, ctx.rules, ctx.count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* --------------------------------------------------------------- auditd_audit */
|
|
|
|
typedef struct {
|
|
char name[160];
|
|
int count;
|
|
} audit_cnt;
|
|
|
|
static void audit_count(audit_cnt *arr, int *n, int max, const char *name)
|
|
{
|
|
int i;
|
|
|
|
if (name == NULL || *name == '\0')
|
|
return;
|
|
for (i = 0; i < *n; i++)
|
|
if (strcmp(arr[i].name, name) == 0) {
|
|
arr[i].count++;
|
|
return;
|
|
}
|
|
if (*n < max) {
|
|
snprintf(arr[*n].name, sizeof(arr[0].name), "%.120s", name);
|
|
arr[*n].count = 1;
|
|
(*n)++;
|
|
}
|
|
}
|
|
|
|
static int audit_value(const char *line, const char *key, char *out, size_t n)
|
|
{
|
|
const char *p = strstr(line, key);
|
|
size_t w = 0;
|
|
|
|
out[0] = '\0';
|
|
if (p == NULL)
|
|
return 0;
|
|
p += strlen(key);
|
|
if (*p == '"') {
|
|
p++;
|
|
while (*p != '\0' && *p != '"' && w + 1 < n)
|
|
out[w++] = *p++;
|
|
} else {
|
|
while (*p != '\0' && *p != ' ' && *p != '\n' && *p != '\r' &&
|
|
w + 1 < n)
|
|
out[w++] = *p++;
|
|
}
|
|
out[w] = '\0';
|
|
return w > 0;
|
|
}
|
|
|
|
static void audit_count_json(mcp_buf *b, const audit_cnt *arr, int n,
|
|
const char *key)
|
|
{
|
|
int i;
|
|
|
|
mcp_buf_puts(b, "\"");
|
|
mcp_buf_puts(b, key);
|
|
mcp_buf_puts(b, "\":[");
|
|
for (i = 0; i < n; i++) {
|
|
if (i > 0)
|
|
mcp_buf_puts(b, ",");
|
|
mcp_buf_puts(b, "{\"name\":");
|
|
apex_json_str(b, arr[i].name);
|
|
mcp_buf_printf(b, ",\"count\":%d}", arr[i].count);
|
|
}
|
|
mcp_buf_puts(b, "]");
|
|
}
|
|
|
|
static void t_auditd_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *path = mcp_arg_string(args, "path");
|
|
int limit = mcp_arg_int(args, "limit", 5000);
|
|
FILE *f;
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
int lines = 0;
|
|
int avc = 0;
|
|
int failed_auth = 0;
|
|
audit_cnt types[24];
|
|
audit_cnt exes[12];
|
|
audit_cnt keys[12];
|
|
int ntypes = 0;
|
|
int nexes = 0;
|
|
int nkeys = 0;
|
|
mcp_buf b;
|
|
|
|
(void)user;
|
|
if (path == NULL || *path == '\0')
|
|
path = "/var/log/audit/audit.log";
|
|
if (!allowed_log_path(path)) {
|
|
mcp_reply_error(c, "path must be under /var/log/ or the state dir");
|
|
return;
|
|
}
|
|
if (limit <= 0 || limit > 50000)
|
|
limit = 5000;
|
|
memset(types, 0, sizeof(types));
|
|
memset(exes, 0, sizeof(exes));
|
|
memset(keys, 0, sizeof(keys));
|
|
f = fopen(path, "rb");
|
|
if (f == NULL) {
|
|
mcp_buf_init(&b);
|
|
mcp_buf_printf(&b, "cannot read %s: %s", path, strerror(errno));
|
|
mcp_reply_error(c, b.p);
|
|
mcp_buf_free(&b);
|
|
return;
|
|
}
|
|
while (lines < limit && getline(&line, &cap, f) >= 0) {
|
|
char type[64];
|
|
char res[32];
|
|
char exe[160];
|
|
char key[160];
|
|
|
|
lines++;
|
|
if (!audit_value(line, "type=", type, sizeof(type)))
|
|
snprintf(type, sizeof(type), "%s", "UNKNOWN");
|
|
audit_count(types, &ntypes, 24, type);
|
|
if (strcmp(type, "AVC") == 0)
|
|
avc++;
|
|
if (audit_value(line, "res=", res, sizeof(res)) &&
|
|
strcmp(res, "failed") == 0 &&
|
|
(strncmp(type, "USER_AUTH", 9) == 0 ||
|
|
strncmp(type, "USER_LOGIN", 10) == 0 ||
|
|
strncmp(type, "USER_ACCT", 9) == 0))
|
|
failed_auth++;
|
|
if (audit_value(line, "exe=", exe, sizeof(exe)))
|
|
audit_count(exes, &nexes, 12, exe);
|
|
if (audit_value(line, "key=", key, sizeof(key)) &&
|
|
strcmp(key, "(null)") != 0)
|
|
audit_count(keys, &nkeys, 12, key);
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"path\":");
|
|
apex_json_str(&b, path);
|
|
mcp_buf_printf(&b, ",\"lines\":%d,\"avc_denials\":%d,"
|
|
"\"failed_auth\":%d,", lines, avc, failed_auth);
|
|
audit_count_json(&b, types, ntypes, "types");
|
|
mcp_buf_puts(&b, ",");
|
|
audit_count_json(&b, exes, nexes, "top_exe");
|
|
mcp_buf_puts(&b, ",");
|
|
audit_count_json(&b, keys, nkeys, "keys");
|
|
mcp_buf_puts(&b, "}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ----------------------------------------------------------- ssh_config_audit */
|
|
|
|
static void ssh_finding(mcp_buf *b, int *first, const char *id,
|
|
const char *severity, const char *value,
|
|
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, ",\"value\":");
|
|
apex_json_str(b, value);
|
|
mcp_buf_puts(b, ",\"detail\":");
|
|
apex_json_str(b, detail);
|
|
mcp_buf_puts(b, "}");
|
|
}
|
|
|
|
static void t_ssh_config_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
static char config[65536];
|
|
char path[1024];
|
|
char value[256];
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int count = 0;
|
|
int score = 100;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) > 400) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(path, sizeof(path), "%s/etc/ssh/sshd_config", root);
|
|
if (read_text(path, config, sizeof(config)) <= 0) {
|
|
mcp_reply_error(c, "cannot read sshd_config");
|
|
return;
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"path\":");
|
|
apex_json_str(&b, path);
|
|
mcp_buf_puts(&b, ",\"findings\":[");
|
|
if (sshd_value(config, "PermitRootLogin", value, sizeof(value)) &&
|
|
strcasecmp(value, "yes") == 0) {
|
|
ssh_finding(&b, &first, "root-login", "high", value,
|
|
"root may log in directly over SSH");
|
|
score -= 12;
|
|
count++;
|
|
}
|
|
if (sshd_value(config, "PasswordAuthentication", value, sizeof(value)) &&
|
|
strcasecmp(value, "yes") == 0) {
|
|
ssh_finding(&b, &first, "password-auth", "medium", value,
|
|
"password authentication is enabled");
|
|
score -= 6;
|
|
count++;
|
|
}
|
|
if (sshd_value(config, "PermitEmptyPasswords", value, sizeof(value)) &&
|
|
strcasecmp(value, "yes") == 0) {
|
|
ssh_finding(&b, &first, "empty-passwords", "high", value,
|
|
"empty passwords are accepted");
|
|
score -= 12;
|
|
count++;
|
|
}
|
|
if (sshd_value(config, "X11Forwarding", value, sizeof(value)) &&
|
|
strcasecmp(value, "yes") == 0) {
|
|
ssh_finding(&b, &first, "x11-forwarding", "low", value,
|
|
"X11 forwarding is enabled");
|
|
score -= 2;
|
|
count++;
|
|
}
|
|
if (sshd_value(config, "GatewayPorts", value, sizeof(value)) &&
|
|
strcasecmp(value, "yes") == 0) {
|
|
ssh_finding(&b, &first, "gateway-ports", "high", value,
|
|
"remote forwards may bind non-loopback addresses");
|
|
score -= 10;
|
|
count++;
|
|
}
|
|
if (sshd_value(config, "PermitTunnel", value, sizeof(value)) &&
|
|
strcasecmp(value, "yes") == 0) {
|
|
ssh_finding(&b, &first, "permit-tunnel", "medium", value,
|
|
"tun/tap tunnelling is allowed");
|
|
score -= 6;
|
|
count++;
|
|
}
|
|
if (sshd_value(config, "MaxAuthTries", value, sizeof(value)) &&
|
|
atoi(value) > 4) {
|
|
ssh_finding(&b, &first, "max-auth-tries", "medium", value,
|
|
"more than four authentication tries per connection");
|
|
score -= 4;
|
|
count++;
|
|
}
|
|
if (sshd_value(config, "Ciphers", value, sizeof(value)) &&
|
|
(strstr(value, "3des") != NULL || strstr(value, "arcfour") != NULL ||
|
|
strstr(value, "blowfish") != NULL || strstr(value, "cbc") != NULL)) {
|
|
ssh_finding(&b, &first, "weak-ciphers", "medium", value,
|
|
"cipher list contains legacy ciphers");
|
|
score -= 6;
|
|
count++;
|
|
}
|
|
if (sshd_value(config, "MACs", value, sizeof(value)) &&
|
|
(strstr(value, "md5") != NULL || strstr(value, "sha1-96") != NULL)) {
|
|
ssh_finding(&b, &first, "weak-macs", "medium", value,
|
|
"MAC list contains md5 or truncated sha1");
|
|
score -= 6;
|
|
count++;
|
|
}
|
|
if (sshd_value(config, "KexAlgorithms", value, sizeof(value)) &&
|
|
(strstr(value, "group1") != NULL ||
|
|
strstr(value, "group14-sha1") != NULL)) {
|
|
ssh_finding(&b, &first, "weak-kex", "medium", value,
|
|
"key exchange offers group1 or group14-sha1");
|
|
score -= 6;
|
|
count++;
|
|
}
|
|
if (sshd_value(config, "AllowTcpForwarding", value, sizeof(value)) &&
|
|
strcasecmp(value, "yes") == 0) {
|
|
ssh_finding(&b, &first, "tcp-forwarding", "low", value,
|
|
"TCP forwarding is allowed");
|
|
score -= 2;
|
|
count++;
|
|
}
|
|
if (score < 0)
|
|
score = 0;
|
|
mcp_buf_printf(&b, "],\"score\":%d,\"findings_count\":%d}", score,
|
|
count);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ----------------------------------------------------------------- pam_audit */
|
|
|
|
typedef struct {
|
|
mcp_buf *b;
|
|
int first;
|
|
int count;
|
|
int files;
|
|
int rules;
|
|
int faillock;
|
|
} pam_ctx;
|
|
|
|
static void pam_finding(pam_ctx *ctx, const char *path, int line,
|
|
const char *kind, const char *severity,
|
|
const char *detail)
|
|
{
|
|
if (ctx->count >= 100)
|
|
return;
|
|
if (!ctx->first)
|
|
mcp_buf_puts(ctx->b, ",");
|
|
ctx->first = 0;
|
|
ctx->count++;
|
|
mcp_buf_puts(ctx->b, "{\"path\":");
|
|
apex_json_str(ctx->b, path);
|
|
mcp_buf_printf(ctx->b, ",\"line\":%d,\"kind\":", line);
|
|
apex_json_str(ctx->b, kind);
|
|
mcp_buf_puts(ctx->b, ",\"severity\":");
|
|
apex_json_str(ctx->b, severity);
|
|
mcp_buf_puts(ctx->b, ",\"detail\":");
|
|
apex_json_str(ctx->b, detail);
|
|
mcp_buf_puts(ctx->b, "}");
|
|
}
|
|
|
|
static void pam_scan_file(pam_ctx *ctx, const char *path)
|
|
{
|
|
FILE *f;
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
int lineno = 0;
|
|
|
|
if (ctx->files >= 100)
|
|
return;
|
|
f = fopen(path, "rb");
|
|
if (f == NULL)
|
|
return;
|
|
ctx->files++;
|
|
while (getline(&line, &cap, f) >= 0) {
|
|
char *t = line;
|
|
|
|
lineno++;
|
|
while (*t == ' ' || *t == '\t')
|
|
t++;
|
|
if (*t == '\0' || *t == '#')
|
|
continue;
|
|
ctx->rules++;
|
|
if (strstr(t, "pam_permit.so") != NULL)
|
|
pam_finding(ctx, path, lineno, "pam-permit", "high",
|
|
"pam_permit always succeeds");
|
|
if (strstr(t, "pam_unix.so") != NULL &&
|
|
strstr(t, "nullok") != NULL)
|
|
pam_finding(ctx, path, lineno, "pam-nullok", "high",
|
|
"pam_unix accepts empty passwords (nullok)");
|
|
if (strstr(t, "pam_exec.so") != NULL)
|
|
pam_finding(ctx, path, lineno, "pam-exec", "medium",
|
|
"pam_exec runs an external program during auth");
|
|
if (strstr(t, "pam_faillock.so") != NULL ||
|
|
strstr(t, "pam_tally2.so") != NULL)
|
|
ctx->faillock = 1;
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
}
|
|
|
|
static void t_pam_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
char path[1024];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
pam_ctx ctx;
|
|
DIR *d;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) > 400) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(path, sizeof(path), "%s/etc/pam.d", root);
|
|
d = opendir(path);
|
|
if (d == NULL) {
|
|
mcp_reply_error(c, "cannot read /etc/pam.d");
|
|
return;
|
|
}
|
|
mcp_buf_init(&findings);
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.b = &findings;
|
|
ctx.first = 1;
|
|
{
|
|
struct dirent *ent;
|
|
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char full[1024];
|
|
struct stat st;
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
snprintf(full, sizeof(full), "%.800s/%.160s", path,
|
|
ent->d_name);
|
|
if (stat(full, &st) != 0 || !S_ISREG(st.st_mode))
|
|
continue;
|
|
pam_scan_file(&ctx, full);
|
|
}
|
|
closedir(d);
|
|
}
|
|
if (ctx.files > 0 && !ctx.faillock)
|
|
pam_finding(&ctx, path, 0, "no-faillock", "medium",
|
|
"no pam_faillock/pam_tally2 lockout configured");
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, root);
|
|
mcp_buf_printf(&b, ",\"files\":%d,\"rules\":%d,\"faillock\":%s,"
|
|
"\"findings_count\":%d,\"findings\":[",
|
|
ctx.files, ctx.rules, ctx.faillock ? "true" : "false",
|
|
ctx.count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* --------------------------------------------------------------- log_timeline */
|
|
|
|
typedef struct {
|
|
char key[24];
|
|
char source[12];
|
|
char line[240];
|
|
} tl_entry;
|
|
|
|
typedef struct {
|
|
tl_entry *entries;
|
|
int count;
|
|
int max;
|
|
} tl_ctx;
|
|
|
|
static int tl_month(const char *name)
|
|
{
|
|
static const char *months[] = { "Jan", "Feb", "Mar", "Apr", "May",
|
|
"Jun", "Jul", "Aug", "Sep", "Oct",
|
|
"Nov", "Dec" };
|
|
int i;
|
|
|
|
for (i = 0; i < 12; i++)
|
|
if (strncasecmp(name, months[i], 3) == 0)
|
|
return i;
|
|
return -1;
|
|
}
|
|
|
|
static int tl_key_auth(const char *line, int year, char *key, size_t n)
|
|
{
|
|
char mon[8];
|
|
int day;
|
|
int h;
|
|
int mi;
|
|
int sec;
|
|
int m;
|
|
|
|
if (sscanf(line, "%7s %d %d:%d:%d", mon, &day, &h, &mi, &sec) != 5)
|
|
return 0;
|
|
m = tl_month(mon);
|
|
if (m < 0 || day < 1 || day > 31)
|
|
return 0;
|
|
snprintf(key, n, "%04d%02d%02d%02d%02d%02d", year, m + 1, day, h, mi,
|
|
sec);
|
|
return 1;
|
|
}
|
|
|
|
static int tl_key_access(const char *line, char *key, size_t n)
|
|
{
|
|
const char *p = strchr(line, '[');
|
|
char mon[8];
|
|
int day;
|
|
int year;
|
|
int h;
|
|
int mi;
|
|
int sec;
|
|
int m;
|
|
|
|
if (p == NULL)
|
|
return 0;
|
|
if (sscanf(p + 1, "%d/%3s/%d:%d:%d:%d", &day, mon, &year, &h, &mi,
|
|
&sec) != 6)
|
|
return 0;
|
|
m = tl_month(mon);
|
|
if (m < 0)
|
|
return 0;
|
|
snprintf(key, n, "%04d%02d%02d%02d%02d%02d", year, m + 1, day, h, mi,
|
|
sec);
|
|
return 1;
|
|
}
|
|
|
|
static void tl_add(tl_ctx *ctx, const char *key, const char *source,
|
|
const char *line)
|
|
{
|
|
size_t w = 0;
|
|
|
|
if (ctx->count >= ctx->max)
|
|
return;
|
|
snprintf(ctx->entries[ctx->count].key,
|
|
sizeof(ctx->entries[0].key), "%s", key);
|
|
snprintf(ctx->entries[ctx->count].source,
|
|
sizeof(ctx->entries[0].source), "%s", source);
|
|
while (line[w] != '\0' && line[w] != '\n' && line[w] != '\r' &&
|
|
w + 1 < sizeof(ctx->entries[0].line)) {
|
|
char ch = line[w];
|
|
|
|
if ((unsigned char)ch < 0x20)
|
|
ch = ' ';
|
|
ctx->entries[ctx->count].line[w] = ch;
|
|
w++;
|
|
}
|
|
ctx->entries[ctx->count].line[w] = '\0';
|
|
ctx->count++;
|
|
}
|
|
|
|
static void tl_read(tl_ctx *ctx, const char *path, int access, int year)
|
|
{
|
|
FILE *f = fopen(path, "rb");
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
int lines = 0;
|
|
|
|
if (f == NULL)
|
|
return;
|
|
while (lines < 5000 && getline(&line, &cap, f) >= 0) {
|
|
char key[24];
|
|
|
|
lines++;
|
|
if (access ? tl_key_access(line, key, sizeof(key))
|
|
: tl_key_auth(line, year, key, sizeof(key)))
|
|
tl_add(ctx, key, access ? "access" : "auth", line);
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
}
|
|
|
|
static int tl_cmp(const void *a, const void *b)
|
|
{
|
|
const tl_entry *x = a;
|
|
const tl_entry *y = b;
|
|
int c = strcmp(x->key, y->key);
|
|
|
|
if (c != 0)
|
|
return c;
|
|
return strcmp(x->source, y->source);
|
|
}
|
|
|
|
static void t_log_timeline(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *auth_path = mcp_arg_string(args, "auth");
|
|
const char *access_path = mcp_arg_string(args, "access");
|
|
int limit = mcp_arg_int(args, "limit", 200);
|
|
tl_ctx ctx;
|
|
time_t now = time(NULL);
|
|
struct tm tmv = *gmtime(&now);
|
|
mcp_buf b;
|
|
int i;
|
|
|
|
(void)user;
|
|
if (auth_path == NULL || *auth_path == '\0')
|
|
auth_path = "/var/log/auth.log";
|
|
if (access_path == NULL || *access_path == '\0')
|
|
access_path = "/var/log/nginx/access.log";
|
|
if (!allowed_log_path(auth_path) || !allowed_log_path(access_path)) {
|
|
mcp_reply_error(c, "paths must be under /var/log/ or the state dir");
|
|
return;
|
|
}
|
|
if (limit <= 0 || limit > 1000)
|
|
limit = 200;
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.max = 2000;
|
|
ctx.entries = calloc((size_t)ctx.max, sizeof(*ctx.entries));
|
|
if (ctx.entries == NULL) {
|
|
mcp_reply_error(c, "out of memory");
|
|
return;
|
|
}
|
|
tl_read(&ctx, auth_path, 0, tmv.tm_year + 1900);
|
|
tl_read(&ctx, access_path, 1, tmv.tm_year + 1900);
|
|
qsort(ctx.entries, (size_t)ctx.count, sizeof(*ctx.entries), tl_cmp);
|
|
if (ctx.count > limit)
|
|
ctx.count = limit;
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"auth\":");
|
|
apex_json_str(&b, auth_path);
|
|
mcp_buf_puts(&b, ",\"access\":");
|
|
apex_json_str(&b, access_path);
|
|
mcp_buf_printf(&b, ",\"count\":%d,\"entries\":[", ctx.count);
|
|
for (i = 0; i < ctx.count; i++) {
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
mcp_buf_puts(&b, "{\"time\":");
|
|
apex_json_str(&b, ctx.entries[i].key);
|
|
mcp_buf_puts(&b, ",\"source\":");
|
|
apex_json_str(&b, ctx.entries[i].source);
|
|
mcp_buf_puts(&b, ",\"line\":");
|
|
apex_json_str(&b, ctx.entries[i].line);
|
|
mcp_buf_puts(&b, "}");
|
|
}
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
free(ctx.entries);
|
|
}
|
|
|
|
/* --------------------------------------------------------------- docker_audit */
|
|
|
|
static void docker_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_docker_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
char base[512];
|
|
size_t bl;
|
|
char path[1024];
|
|
mcp_buf b;
|
|
int first = 1;
|
|
int findings = 0;
|
|
struct stat st;
|
|
int socket_exists = 0;
|
|
unsigned int socket_mode = 0;
|
|
int world_writable = 0;
|
|
int group_exists = 0;
|
|
char members[10][64];
|
|
int nmembers = 0;
|
|
int daemon_exists = 0;
|
|
int hosts_tcp = 0;
|
|
int tls_on = 0;
|
|
int insecure = 0;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) >= sizeof(base) - 2) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(base, sizeof(base), "%s", root);
|
|
bl = strlen(base);
|
|
while (bl > 0 && base[bl - 1] == '/')
|
|
base[--bl] = '\0';
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, base);
|
|
snprintf(path, sizeof(path), "%s/var/run/docker.sock", base);
|
|
if (stat(path, &st) == 0) {
|
|
socket_exists = 1;
|
|
socket_mode = (unsigned int)(st.st_mode & 07777);
|
|
world_writable = (st.st_mode & 0002) != 0;
|
|
}
|
|
mcp_buf_puts(&b, ",\"socket\":{\"path\":");
|
|
apex_json_str(&b, path);
|
|
mcp_buf_printf(&b, ",\"exists\":%s,\"mode\":\"%04o\","
|
|
"\"world_writable\":%s}", socket_exists ? "true" : "false",
|
|
socket_mode, world_writable ? "true" : "false");
|
|
snprintf(path, sizeof(path), "%s/etc/group", base);
|
|
{
|
|
FILE *f = fopen(path, "rb");
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
|
|
if (f != NULL) {
|
|
while (getline(&line, &cap, f) >= 0) {
|
|
if (strncmp(line, "docker:", 7) == 0) {
|
|
char *fields[6];
|
|
int nf = 0;
|
|
char *save = NULL;
|
|
char *tok;
|
|
char *copy = strdup(line);
|
|
char *fsave = NULL;
|
|
char *member;
|
|
|
|
group_exists = 1;
|
|
if (copy == NULL)
|
|
continue;
|
|
tok = strtok_r(copy, ":", &save);
|
|
while (tok != NULL && nf < 6) {
|
|
fields[nf++] = tok;
|
|
tok = strtok_r(NULL, ":", &save);
|
|
}
|
|
if (nf >= 4) {
|
|
member = strtok_r(fields[3], ",\n", &fsave);
|
|
while (member != NULL && nmembers < 10) {
|
|
if (*member != '\0' && *member != '\n')
|
|
snprintf(members[nmembers++],
|
|
sizeof(members[0]), "%.48s",
|
|
member);
|
|
member = strtok_r(NULL, ",\n", &fsave);
|
|
}
|
|
}
|
|
free(copy);
|
|
break;
|
|
}
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
}
|
|
}
|
|
mcp_buf_printf(&b, ",\"docker_group\":{\"exists\":%s,\"members\":[",
|
|
group_exists ? "true" : "false");
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < nmembers; i++) {
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
apex_json_str(&b, members[i]);
|
|
}
|
|
}
|
|
mcp_buf_puts(&b, "]}");
|
|
snprintf(path, sizeof(path), "%s/etc/docker/daemon.json", base);
|
|
{
|
|
static char config[65536];
|
|
|
|
if (read_text(path, config, sizeof(config)) > 0) {
|
|
daemon_exists = 1;
|
|
hosts_tcp = strstr(config, "tcp://") != NULL;
|
|
tls_on = strstr(config, "\"tls\"") != NULL &&
|
|
strstr(config, "true") != NULL;
|
|
insecure = strstr(config, "insecure-registries") != NULL;
|
|
}
|
|
}
|
|
mcp_buf_puts(&b, ",\"daemon\":{\"path\":");
|
|
apex_json_str(&b, path);
|
|
mcp_buf_printf(&b, ",\"exists\":%s,\"hosts_tcp\":%s,\"tls\":%s,"
|
|
"\"insecure_registries\":%s}", daemon_exists ? "true" : "false",
|
|
hosts_tcp ? "true" : "false", tls_on ? "true" : "false",
|
|
insecure ? "true" : "false");
|
|
mcp_buf_puts(&b, ",\"findings\":[");
|
|
if (world_writable) {
|
|
docker_finding(&b, &first, "socket-world-writable", "high",
|
|
"the Docker socket is writable by any local user");
|
|
findings++;
|
|
}
|
|
if (group_exists && nmembers > 0) {
|
|
docker_finding(&b, &first, "docker-group-members", "medium",
|
|
"docker group members can become root through the daemon");
|
|
findings++;
|
|
}
|
|
if (hosts_tcp) {
|
|
docker_finding(&b, &first, "api-tcp", "high",
|
|
"the daemon listens on a TCP address");
|
|
findings++;
|
|
if (!tls_on) {
|
|
docker_finding(&b, &first, "tls-off", "high",
|
|
"the TCP API has no TLS configured");
|
|
findings++;
|
|
}
|
|
}
|
|
if (insecure) {
|
|
docker_finding(&b, &first, "insecure-registries", "medium",
|
|
"insecure registries are allowed");
|
|
findings++;
|
|
}
|
|
mcp_buf_printf(&b, "],\"findings_count\":%d}", findings);
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
}
|
|
|
|
/* ----------------------------------------------------------------- www_audit */
|
|
|
|
typedef struct {
|
|
mcp_buf *b;
|
|
int first;
|
|
int count;
|
|
int files;
|
|
int max_files;
|
|
} www_ctx;
|
|
|
|
static void www_finding(www_ctx *ctx, const char *path, const char *kind,
|
|
const char *severity, const char *detail)
|
|
{
|
|
if (ctx->count >= 100)
|
|
return;
|
|
if (!ctx->first)
|
|
mcp_buf_puts(ctx->b, ",");
|
|
ctx->first = 0;
|
|
ctx->count++;
|
|
mcp_buf_puts(ctx->b, "{\"path\":");
|
|
apex_json_str(ctx->b, path);
|
|
mcp_buf_puts(ctx->b, ",\"kind\":");
|
|
apex_json_str(ctx->b, kind);
|
|
mcp_buf_puts(ctx->b, ",\"severity\":");
|
|
apex_json_str(ctx->b, severity);
|
|
mcp_buf_puts(ctx->b, ",\"detail\":");
|
|
apex_json_str(ctx->b, detail);
|
|
mcp_buf_puts(ctx->b, "}");
|
|
}
|
|
|
|
static int www_ends_with(const char *name, const char *suffix)
|
|
{
|
|
size_t nl = strlen(name);
|
|
size_t sl = strlen(suffix);
|
|
|
|
return nl > sl && strcmp(name + nl - sl, suffix) == 0;
|
|
}
|
|
|
|
static int www_has_index(const char *dir)
|
|
{
|
|
static const char *names[] = { "index.html", "index.htm", "index.php",
|
|
NULL };
|
|
int i;
|
|
|
|
for (i = 0; names[i] != NULL; i++) {
|
|
char path[1024];
|
|
struct stat st;
|
|
|
|
snprintf(path, sizeof(path), "%.800s/%.160s", dir, names[i]);
|
|
if (stat(path, &st) == 0 && S_ISREG(st.st_mode))
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void www_walk(www_ctx *ctx, const char *dir, int depth)
|
|
{
|
|
DIR *d;
|
|
struct dirent *ent;
|
|
|
|
if (depth > 4 || ctx->files >= ctx->max_files)
|
|
return;
|
|
d = opendir(dir);
|
|
if (d == NULL)
|
|
return;
|
|
{
|
|
const char *slash = strrchr(dir, '/');
|
|
const char *base = slash != NULL ? slash + 1 : dir;
|
|
|
|
if (depth >= 1 && depth <= 2 && strcmp(base, ".git") != 0 &&
|
|
!www_has_index(dir))
|
|
www_finding(ctx, dir, "no-index", "low",
|
|
"directory has no index file (listing may be exposed)");
|
|
}
|
|
while ((ent = readdir(d)) != NULL && ctx->files < ctx->max_files) {
|
|
char path[1024];
|
|
struct stat st;
|
|
size_t dl = strlen(dir);
|
|
size_t nl = strlen(ent->d_name);
|
|
|
|
if (ent->d_name[0] == '.' &&
|
|
(ent->d_name[1] == '\0' ||
|
|
(ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
|
|
continue;
|
|
if (dl + nl + 2 > sizeof(path))
|
|
continue;
|
|
memcpy(path, dir, dl);
|
|
path[dl] = '/';
|
|
memcpy(path + dl + 1, ent->d_name, nl + 1);
|
|
if (lstat(path, &st) != 0)
|
|
continue;
|
|
if (S_ISLNK(st.st_mode))
|
|
continue;
|
|
if (S_ISDIR(st.st_mode)) {
|
|
if ((st.st_mode & 0002) != 0)
|
|
www_finding(ctx, path, "world-writable", "high",
|
|
"web directory is writable by anyone");
|
|
if (strcmp(ent->d_name, ".git") == 0)
|
|
www_finding(ctx, path, "git-dir", "high",
|
|
"repository metadata is inside the web root");
|
|
www_walk(ctx, path, depth + 1);
|
|
continue;
|
|
}
|
|
if (!S_ISREG(st.st_mode))
|
|
continue;
|
|
ctx->files++;
|
|
if ((st.st_mode & 0002) != 0)
|
|
www_finding(ctx, path, "world-writable", "high",
|
|
"web content is writable by anyone");
|
|
if (strcmp(ent->d_name, ".env") == 0)
|
|
www_finding(ctx, path, "env-file", "high",
|
|
"environment file inside the web root");
|
|
if (strcmp(ent->d_name, "wp-config.php") == 0 ||
|
|
strcmp(ent->d_name, "config.php") == 0)
|
|
www_finding(ctx, path, "config-file", "high",
|
|
"application configuration is downloadable");
|
|
if (strcmp(ent->d_name, ".htpasswd") == 0 ||
|
|
strcmp(ent->d_name, "id_rsa") == 0)
|
|
www_finding(ctx, path, "secret-file", "high",
|
|
"credential material inside the web root");
|
|
if (www_ends_with(ent->d_name, ".bak") ||
|
|
www_ends_with(ent->d_name, ".old") ||
|
|
www_ends_with(ent->d_name, ".sql") ||
|
|
www_ends_with(ent->d_name, ".zip") ||
|
|
www_ends_with(ent->d_name, ".tar.gz"))
|
|
www_finding(ctx, path, "backup-file", "medium",
|
|
"backup or dump file inside the web root");
|
|
}
|
|
closedir(d);
|
|
}
|
|
|
|
static void t_www_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
static const char *roots[] = { "/var/www", "/srv/www",
|
|
"/usr/share/nginx/html", NULL };
|
|
const char *root = mcp_arg_string(args, "root");
|
|
int limit = mcp_arg_int(args, "limit", 2000);
|
|
char base[512];
|
|
char path[1024];
|
|
size_t bl;
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
www_ctx ctx;
|
|
int i;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) >= sizeof(base) - 2) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(base, sizeof(base), "%s", root);
|
|
bl = strlen(base);
|
|
while (bl > 0 && base[bl - 1] == '/')
|
|
base[--bl] = '\0';
|
|
if (limit <= 0 || limit > 20000)
|
|
limit = 2000;
|
|
mcp_buf_init(&findings);
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.b = &findings;
|
|
ctx.first = 1;
|
|
ctx.max_files = limit;
|
|
for (i = 0; roots[i] != NULL; i++) {
|
|
struct stat st;
|
|
|
|
snprintf(path, sizeof(path), "%s%s", base, roots[i]);
|
|
if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
|
|
www_walk(&ctx, path, 0);
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, base);
|
|
mcp_buf_printf(&b, ",\"files\":%d,\"findings_count\":%d,\"findings\":[",
|
|
ctx.files, ctx.count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* -------------------------------------------------------------- apparmor_audit */
|
|
|
|
typedef struct {
|
|
mcp_buf *b;
|
|
int first;
|
|
int count;
|
|
} aa_ctx;
|
|
|
|
static void aa_finding(aa_ctx *ctx, const char *id, const char *severity,
|
|
const char *detail)
|
|
{
|
|
if (ctx->count >= 100)
|
|
return;
|
|
if (!ctx->first)
|
|
mcp_buf_puts(ctx->b, ",");
|
|
ctx->first = 0;
|
|
ctx->count++;
|
|
mcp_buf_puts(ctx->b, "{\"id\":");
|
|
apex_json_str(ctx->b, id);
|
|
mcp_buf_puts(ctx->b, ",\"severity\":");
|
|
apex_json_str(ctx->b, severity);
|
|
mcp_buf_puts(ctx->b, ",\"detail\":");
|
|
apex_json_str(ctx->b, detail);
|
|
mcp_buf_puts(ctx->b, "}");
|
|
}
|
|
|
|
static void t_apparmor_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
static char text[65536];
|
|
char path[1024];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
aa_ctx ctx;
|
|
int available = 0;
|
|
int profiles = 0;
|
|
int enforce = 0;
|
|
int complain = 0;
|
|
char complain_names[20][128];
|
|
int ncomplain = 0;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) > 400) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(path, sizeof(path),
|
|
"%s/sys/kernel/security/apparmor/profiles", root);
|
|
mcp_buf_init(&findings);
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.b = &findings;
|
|
ctx.first = 1;
|
|
if (read_text(path, text, sizeof(text)) >= 0) {
|
|
char *copy = strdup(text);
|
|
char *save = NULL;
|
|
char *line;
|
|
|
|
available = 1;
|
|
if (copy != NULL) {
|
|
line = strtok_r(copy, "\n", &save);
|
|
while (line != NULL) {
|
|
char *open = strchr(line, '(');
|
|
|
|
if (open != NULL && strstr(line, "(enforce)") != NULL) {
|
|
profiles++;
|
|
enforce++;
|
|
} else if (open != NULL &&
|
|
strstr(line, "(complain)") != NULL) {
|
|
size_t n = (size_t)(open - line);
|
|
|
|
profiles++;
|
|
complain++;
|
|
while (n > 0 && (line[n - 1] == ' ' ||
|
|
line[n - 1] == '\t'))
|
|
n--;
|
|
if (ncomplain < 20 && n < sizeof(complain_names[0])) {
|
|
memcpy(complain_names[ncomplain], line, n);
|
|
complain_names[ncomplain][n] = '\0';
|
|
ncomplain++;
|
|
}
|
|
}
|
|
line = strtok_r(NULL, "\n", &save);
|
|
}
|
|
free(copy);
|
|
}
|
|
}
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < ncomplain; i++) {
|
|
char detail[192];
|
|
|
|
snprintf(detail, sizeof(detail),
|
|
"profile %.120s is in complain mode", complain_names[i]);
|
|
aa_finding(&ctx, "complain-profile", "medium", detail);
|
|
}
|
|
}
|
|
if (available && profiles == 0)
|
|
aa_finding(&ctx, "no-profiles", "low",
|
|
"AppArmor is loaded but no profile is active");
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, root);
|
|
mcp_buf_printf(&b, ",\"available\":%s,\"profiles\":%d,\"enforce\":%d,"
|
|
"\"complain\":%d,\"complain_profiles\":[",
|
|
available ? "true" : "false", profiles, enforce, complain);
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < ncomplain; i++) {
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
apex_json_str(&b, complain_names[i]);
|
|
}
|
|
}
|
|
mcp_buf_printf(&b, "],\"findings_count\":%d,\"findings\":[", ctx.count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- selinux_audit */
|
|
|
|
static void t_selinux_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
char path[1024];
|
|
char buf[128];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
int first = 1;
|
|
int count = 0;
|
|
int present = 0;
|
|
int enforce = 0;
|
|
char mode[32];
|
|
char policy[64];
|
|
int booleans = 0;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) > 400) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
mode[0] = policy[0] = '\0';
|
|
mcp_buf_init(&findings);
|
|
snprintf(path, sizeof(path), "%s/sys/fs/selinux/enforce", root);
|
|
if (read_text(path, buf, sizeof(buf)) > 0) {
|
|
trim_eol(buf);
|
|
present = 1;
|
|
enforce = atoi(buf) == 1;
|
|
}
|
|
snprintf(path, sizeof(path), "%s/etc/selinux/config", root);
|
|
if (read_text(path, buf, sizeof(buf)) > 0) {
|
|
char *line = buf;
|
|
|
|
while (*line != '\0') {
|
|
char *eol = strchr(line, '\n');
|
|
size_t len = eol != NULL ? (size_t)(eol - line) : strlen(line);
|
|
|
|
if (len > 8 && strncmp(line, "SELINUX=", 8) == 0) {
|
|
size_t n = len - 8;
|
|
|
|
if (n >= sizeof(mode))
|
|
n = sizeof(mode) - 1;
|
|
memcpy(mode, line + 8, n);
|
|
mode[n] = '\0';
|
|
} else if (len > 12 && strncmp(line, "SELINUXTYPE=", 12) == 0) {
|
|
size_t n = len - 12;
|
|
|
|
if (n >= sizeof(policy))
|
|
n = sizeof(policy) - 1;
|
|
memcpy(policy, line + 12, n);
|
|
policy[n] = '\0';
|
|
}
|
|
line = eol != NULL ? eol + 1 : line + len;
|
|
}
|
|
}
|
|
if (mode[0] == '\0')
|
|
snprintf(mode, sizeof(mode), "%s",
|
|
present ? (enforce ? "enforcing" : "permissive")
|
|
: "unknown");
|
|
snprintf(path, sizeof(path), "%s/sys/fs/selinux/booleans", root);
|
|
{
|
|
DIR *d = opendir(path);
|
|
struct dirent *ent;
|
|
|
|
if (d != NULL) {
|
|
while ((ent = readdir(d)) != NULL) {
|
|
if (ent->d_name[0] != '.')
|
|
booleans++;
|
|
}
|
|
closedir(d);
|
|
}
|
|
}
|
|
if (!present) {
|
|
if (!first)
|
|
mcp_buf_puts(&findings, ",");
|
|
first = 0;
|
|
count++;
|
|
mcp_buf_puts(&findings, "{\"id\":\"not-present\",\"severity\":\"low\","
|
|
"\"detail\":\"SELinux is not available on this host\"}");
|
|
} else if (strcmp(mode, "disabled") == 0) {
|
|
if (!first)
|
|
mcp_buf_puts(&findings, ",");
|
|
first = 0;
|
|
count++;
|
|
mcp_buf_puts(&findings, "{\"id\":\"disabled\",\"severity\":\"high\","
|
|
"\"detail\":\"SELinux is configured disabled\"}");
|
|
} else if (strcmp(mode, "permissive") == 0 || !enforce) {
|
|
if (!first)
|
|
mcp_buf_puts(&findings, ",");
|
|
first = 0;
|
|
count++;
|
|
mcp_buf_puts(&findings, "{\"id\":\"permissive\",\"severity\":\"medium\","
|
|
"\"detail\":\"SELinux only logs, it does not enforce\"}");
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, root);
|
|
mcp_buf_printf(&b, ",\"present\":%s,\"enforce\":%s,\"mode\":",
|
|
present ? "true" : "false", enforce ? "true" : "false");
|
|
apex_json_str(&b, mode);
|
|
mcp_buf_puts(&b, ",\"policy_type\":");
|
|
apex_json_str(&b, policy);
|
|
mcp_buf_printf(&b, ",\"booleans\":%d,\"findings_count\":%d,\"findings\":[",
|
|
booleans, count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- audit_rules */
|
|
|
|
typedef struct {
|
|
char watches[32][128];
|
|
int nwatches;
|
|
int rules;
|
|
int files;
|
|
int immutable;
|
|
int execve;
|
|
} ar_ctx;
|
|
|
|
static void ar_read_file(ar_ctx *ctx, const char *path)
|
|
{
|
|
FILE *f = fopen(path, "rb");
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
|
|
if (f == NULL)
|
|
return;
|
|
ctx->files++;
|
|
while (getline(&line, &cap, f) >= 0) {
|
|
char *t = line;
|
|
size_t n;
|
|
|
|
while (*t == ' ' || *t == '\t')
|
|
t++;
|
|
n = strlen(t);
|
|
while (n > 0 && (t[n - 1] == '\n' || t[n - 1] == '\r'))
|
|
t[--n] = '\0';
|
|
if (*t == '\0' || *t == '#')
|
|
continue;
|
|
ctx->rules++;
|
|
if (strstr(t, "-e 2") != NULL)
|
|
ctx->immutable = 1;
|
|
if (strstr(t, "execve") != NULL)
|
|
ctx->execve = 1;
|
|
if (strncmp(t, "-w ", 3) == 0) {
|
|
char p[128];
|
|
int w = 0;
|
|
|
|
t += 3;
|
|
while (*t == ' ' || *t == '\t')
|
|
t++;
|
|
while (*t != '\0' && *t != ' ' && *t != '\t' &&
|
|
w + 1 < (int)sizeof(p))
|
|
p[w++] = *t++;
|
|
p[w] = '\0';
|
|
if (w > 0 && ctx->nwatches < 32)
|
|
snprintf(ctx->watches[ctx->nwatches++],
|
|
sizeof(ctx->watches[0]), "%s", p);
|
|
}
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
}
|
|
|
|
static void t_audit_rules(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
static const char *critical[] = {
|
|
"/etc/passwd", "/etc/shadow", "/etc/sudoers", "/etc/group", NULL
|
|
};
|
|
char base[512];
|
|
char path[1024];
|
|
size_t bl;
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
int first = 1;
|
|
int count = 0;
|
|
ar_ctx ctx;
|
|
int i;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) >= sizeof(base) - 2) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(base, sizeof(base), "%s", root);
|
|
bl = strlen(base);
|
|
while (bl > 0 && base[bl - 1] == '/')
|
|
base[--bl] = '\0';
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
snprintf(path, sizeof(path), "%s/etc/audit/audit.rules", base);
|
|
ar_read_file(&ctx, path);
|
|
snprintf(path, sizeof(path), "%s/etc/audit/rules.d", base);
|
|
{
|
|
DIR *d = opendir(path);
|
|
struct dirent *ent;
|
|
|
|
if (d != NULL) {
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char full[1024];
|
|
size_t nl = strlen(ent->d_name);
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
if (nl < 7 ||
|
|
strcmp(ent->d_name + nl - 6, ".rules") != 0)
|
|
continue;
|
|
snprintf(full, sizeof(full), "%.800s/%.160s", path,
|
|
ent->d_name);
|
|
ar_read_file(&ctx, full);
|
|
}
|
|
closedir(d);
|
|
}
|
|
}
|
|
mcp_buf_init(&findings);
|
|
for (i = 0; critical[i] != NULL; i++) {
|
|
int j;
|
|
int seen = 0;
|
|
|
|
for (j = 0; j < ctx.nwatches; j++)
|
|
if (strcmp(ctx.watches[j], critical[i]) == 0)
|
|
seen = 1;
|
|
if (seen)
|
|
continue;
|
|
if (!first)
|
|
mcp_buf_puts(&findings, ",");
|
|
first = 0;
|
|
count++;
|
|
mcp_buf_puts(&findings, "{\"id\":\"missing-watch\",\"severity\":"
|
|
"\"medium\",\"detail\":");
|
|
{
|
|
char detail[160];
|
|
|
|
snprintf(detail, sizeof(detail),
|
|
"no audit watch on %s", critical[i]);
|
|
apex_json_str(&findings, detail);
|
|
}
|
|
mcp_buf_puts(&findings, "}");
|
|
}
|
|
if (!ctx.immutable) {
|
|
if (!first)
|
|
mcp_buf_puts(&findings, ",");
|
|
first = 0;
|
|
count++;
|
|
mcp_buf_puts(&findings, "{\"id\":\"not-immutable\",\"severity\":"
|
|
"\"low\",\"detail\":\"rules are not locked with -e 2\"}");
|
|
}
|
|
if (!ctx.execve) {
|
|
if (!first)
|
|
mcp_buf_puts(&findings, ",");
|
|
first = 0;
|
|
count++;
|
|
mcp_buf_puts(&findings, "{\"id\":\"no-execve\",\"severity\":"
|
|
"\"medium\",\"detail\":\"no execve rule\"}");
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, base);
|
|
mcp_buf_printf(&b, ",\"files\":%d,\"rules\":%d,\"immutable\":%s,"
|
|
"\"execve\":%s,\"watches\":[", ctx.files, ctx.rules,
|
|
ctx.immutable ? "true" : "false",
|
|
ctx.execve ? "true" : "false");
|
|
for (i = 0; i < ctx.nwatches; i++) {
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
apex_json_str(&b, ctx.watches[i]);
|
|
}
|
|
mcp_buf_printf(&b, "],\"findings_count\":%d,\"findings\":[", count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ usb_audit */
|
|
|
|
typedef struct {
|
|
char name[64];
|
|
char vendor[16];
|
|
char product_id[16];
|
|
char manufacturer[128];
|
|
char product[128];
|
|
char serial[64];
|
|
int storage;
|
|
} usb_dev;
|
|
|
|
static void usb_read_field(const char *dir, const char *field, char *out,
|
|
size_t n)
|
|
{
|
|
char path[1024];
|
|
|
|
out[0] = '\0';
|
|
snprintf(path, sizeof(path), "%.800s/%.100s", dir, field);
|
|
if (read_text(path, out, n) > 0)
|
|
trim_eol(out);
|
|
}
|
|
|
|
static void t_usb_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
int limit = mcp_arg_int(args, "limit", 200);
|
|
char base[512];
|
|
char dir[1024];
|
|
DIR *d;
|
|
struct dirent *ent;
|
|
usb_dev *devs;
|
|
int ndev = 0;
|
|
int storage = 0;
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
int first = 1;
|
|
int findings_count = 0;
|
|
int i;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) >= sizeof(base) - 2) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(base, sizeof(base), "%s", root);
|
|
snprintf(dir, sizeof(dir), "%s/sys/bus/usb/devices", base);
|
|
if (limit <= 0 || limit > 1000)
|
|
limit = 200;
|
|
devs = calloc((size_t)limit, sizeof(*devs));
|
|
if (devs == NULL) {
|
|
mcp_reply_error(c, "out of memory");
|
|
return;
|
|
}
|
|
d = opendir(dir);
|
|
if (d == NULL) {
|
|
free(devs);
|
|
mcp_reply_error(c, "cannot read the USB device tree");
|
|
return;
|
|
}
|
|
while ((ent = readdir(d)) != NULL && ndev < limit) {
|
|
char devdir[1024];
|
|
char path[1024];
|
|
char cls[16];
|
|
usb_dev *u;
|
|
|
|
if (ent->d_name[0] == '.' || strchr(ent->d_name, ':') != NULL)
|
|
continue;
|
|
if (strlen(ent->d_name) + 2 > sizeof(devdir))
|
|
continue;
|
|
snprintf(devdir, sizeof(devdir), "%.800s/%.100s", dir,
|
|
ent->d_name);
|
|
snprintf(path, sizeof(path), "%.900s/idVendor", devdir);
|
|
if (read_text(path, cls, sizeof(cls)) <= 0)
|
|
continue;
|
|
u = &devs[ndev];
|
|
snprintf(u->name, sizeof(u->name), "%.48s", ent->d_name);
|
|
usb_read_field(devdir, "idVendor", u->vendor,
|
|
sizeof(u->vendor));
|
|
usb_read_field(devdir, "idProduct", u->product_id,
|
|
sizeof(u->product_id));
|
|
usb_read_field(devdir, "manufacturer", u->manufacturer,
|
|
sizeof(u->manufacturer));
|
|
usb_read_field(devdir, "product", u->product, sizeof(u->product));
|
|
usb_read_field(devdir, "serial", u->serial, sizeof(u->serial));
|
|
u->storage = 0;
|
|
{
|
|
DIR *sub = opendir(dir);
|
|
struct dirent *se;
|
|
|
|
if (sub != NULL) {
|
|
while ((se = readdir(sub)) != NULL) {
|
|
char ifdir[1024];
|
|
char ifpath[1024];
|
|
char ifcls[16];
|
|
size_t nl = strlen(ent->d_name);
|
|
|
|
if (strncmp(se->d_name, ent->d_name, nl) != 0 ||
|
|
se->d_name[nl] != ':')
|
|
continue;
|
|
snprintf(ifdir, sizeof(ifdir), "%.800s/%.100s",
|
|
dir, se->d_name);
|
|
snprintf(ifpath, sizeof(ifpath),
|
|
"%.900s/bInterfaceClass", ifdir);
|
|
if (read_text(ifpath, ifcls, sizeof(ifcls)) > 0) {
|
|
trim_eol(ifcls);
|
|
if (strcmp(ifcls, "08") == 0)
|
|
u->storage = 1;
|
|
}
|
|
}
|
|
closedir(sub);
|
|
}
|
|
}
|
|
if (u->storage)
|
|
storage++;
|
|
ndev++;
|
|
}
|
|
closedir(d);
|
|
mcp_buf_init(&findings);
|
|
for (i = 0; i < ndev; i++) {
|
|
if (!devs[i].storage)
|
|
continue;
|
|
if (!first)
|
|
mcp_buf_puts(&findings, ",");
|
|
first = 0;
|
|
findings_count++;
|
|
mcp_buf_puts(&findings, "{\"id\":\"usb-storage\",\"severity\":"
|
|
"\"medium\",\"path\":");
|
|
apex_json_str(&findings, devs[i].name);
|
|
mcp_buf_puts(&findings, ",\"detail\":");
|
|
apex_json_str(&findings, "USB mass-storage interface present");
|
|
mcp_buf_puts(&findings, "}");
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, base);
|
|
mcp_buf_printf(&b, ",\"devices\":%d,\"storage\":%d,\"entries\":[", ndev,
|
|
storage);
|
|
for (i = 0; i < ndev; i++) {
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
mcp_buf_puts(&b, "{\"name\":");
|
|
apex_json_str(&b, devs[i].name);
|
|
mcp_buf_puts(&b, ",\"vendor\":");
|
|
apex_json_str(&b, devs[i].vendor);
|
|
mcp_buf_puts(&b, ",\"product_id\":");
|
|
apex_json_str(&b, devs[i].product_id);
|
|
mcp_buf_puts(&b, ",\"manufacturer\":");
|
|
apex_json_str(&b, devs[i].manufacturer);
|
|
mcp_buf_puts(&b, ",\"product\":");
|
|
apex_json_str(&b, devs[i].product);
|
|
mcp_buf_puts(&b, ",\"serial\":");
|
|
apex_json_str(&b, devs[i].serial);
|
|
mcp_buf_printf(&b, ",\"storage\":%s}",
|
|
devs[i].storage ? "true" : "false");
|
|
}
|
|
mcp_buf_printf(&b, "],\"findings_count\":%d,\"findings\":[",
|
|
findings_count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
free(devs);
|
|
}
|
|
|
|
/* -------------------------------------------------------- ssh_known_hosts_audit */
|
|
|
|
typedef struct {
|
|
mcp_buf *b;
|
|
int first;
|
|
int count;
|
|
int files;
|
|
int hosts;
|
|
int hashed;
|
|
int plain;
|
|
int weak;
|
|
int duplicates;
|
|
char seen[128][200];
|
|
int nseen;
|
|
} kh_ctx;
|
|
|
|
static void kh_finding(kh_ctx *ctx, const char *path, int line,
|
|
const char *kind, const char *severity,
|
|
const char *detail)
|
|
{
|
|
if (ctx->count >= 100)
|
|
return;
|
|
if (!ctx->first)
|
|
mcp_buf_puts(ctx->b, ",");
|
|
ctx->first = 0;
|
|
ctx->count++;
|
|
mcp_buf_puts(ctx->b, "{\"path\":");
|
|
apex_json_str(ctx->b, path);
|
|
mcp_buf_printf(ctx->b, ",\"line\":%d,\"kind\":", line);
|
|
apex_json_str(ctx->b, kind);
|
|
mcp_buf_puts(ctx->b, ",\"severity\":");
|
|
apex_json_str(ctx->b, severity);
|
|
mcp_buf_puts(ctx->b, ",\"detail\":");
|
|
apex_json_str(ctx->b, detail);
|
|
mcp_buf_puts(ctx->b, "}");
|
|
}
|
|
|
|
static void kh_scan_file(kh_ctx *ctx, const char *path)
|
|
{
|
|
FILE *f;
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
int lineno = 0;
|
|
|
|
if (ctx->files >= 100)
|
|
return;
|
|
f = fopen(path, "rb");
|
|
if (f == NULL)
|
|
return;
|
|
ctx->files++;
|
|
while (getline(&line, &cap, f) >= 0) {
|
|
char *tokens[4];
|
|
int ntok = 0;
|
|
char *save = NULL;
|
|
char *tok;
|
|
char *copy;
|
|
char sig[200];
|
|
|
|
lineno++;
|
|
while (line[0] == ' ' || line[0] == '\t')
|
|
memmove(line, line + 1, strlen(line));
|
|
trim_eol(line);
|
|
if (line[0] == '\0' || line[0] == '#')
|
|
continue;
|
|
copy = strdup(line);
|
|
if (copy == NULL)
|
|
continue;
|
|
tok = strtok_r(copy, " \t", &save);
|
|
while (tok != NULL && ntok < 4) {
|
|
tokens[ntok++] = tok;
|
|
tok = strtok_r(NULL, " \t", &save);
|
|
}
|
|
if (ntok >= 2) {
|
|
ctx->hosts++;
|
|
if (strncmp(tokens[0], "|1|", 3) == 0)
|
|
ctx->hashed++;
|
|
else
|
|
ctx->plain++;
|
|
if (strcmp(tokens[1], "ssh-rsa") == 0 ||
|
|
strcmp(tokens[1], "ssh-dss") == 0) {
|
|
ctx->weak++;
|
|
kh_finding(ctx, path, lineno, "weak-key-type", "low",
|
|
"known_hosts entry uses ssh-rsa or ssh-dss");
|
|
}
|
|
snprintf(sig, sizeof(sig), "%.60s|%.20s|%.80s", tokens[0],
|
|
tokens[1], ntok >= 3 ? tokens[2] : "");
|
|
{
|
|
int i;
|
|
int dup = 0;
|
|
|
|
for (i = 0; i < ctx->nseen; i++)
|
|
if (strcmp(ctx->seen[i], sig) == 0)
|
|
dup = 1;
|
|
if (dup) {
|
|
ctx->duplicates++;
|
|
kh_finding(ctx, path, lineno, "duplicate", "low",
|
|
"the same host key appears more than once");
|
|
} else if (ctx->nseen < 128) {
|
|
snprintf(ctx->seen[ctx->nseen],
|
|
sizeof(ctx->seen[0]), "%s", sig);
|
|
ctx->nseen++;
|
|
}
|
|
}
|
|
}
|
|
free(copy);
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
}
|
|
|
|
static void t_ssh_known_hosts_audit(mcp_conn *c, const mcp_json *args,
|
|
void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
char base[512];
|
|
char path[1024];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
kh_ctx ctx;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) >= sizeof(base) - 2) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(base, sizeof(base), "%s", root);
|
|
mcp_buf_init(&findings);
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.b = &findings;
|
|
ctx.first = 1;
|
|
snprintf(path, sizeof(path), "%s/etc/ssh/ssh_known_hosts", base);
|
|
kh_scan_file(&ctx, path);
|
|
snprintf(path, sizeof(path), "%s/etc/ssh/ssh_known_hosts2", base);
|
|
kh_scan_file(&ctx, path);
|
|
snprintf(path, sizeof(path), "%s/root/.ssh/known_hosts", base);
|
|
kh_scan_file(&ctx, path);
|
|
snprintf(path, sizeof(path), "%s/home", base);
|
|
{
|
|
DIR *d = opendir(path);
|
|
struct dirent *ent;
|
|
|
|
if (d != NULL) {
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char kh[1024];
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
snprintf(kh, sizeof(kh), "%.700s/%.160s/.ssh/known_hosts",
|
|
path, ent->d_name);
|
|
kh_scan_file(&ctx, kh);
|
|
}
|
|
closedir(d);
|
|
}
|
|
}
|
|
snprintf(path, sizeof(path), "%.450s%.450s/.ssh/known_hosts", base,
|
|
g_cfg.home);
|
|
kh_scan_file(&ctx, path);
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, base);
|
|
mcp_buf_printf(&b, ",\"files\":%d,\"hosts\":%d,\"hashed\":%d,"
|
|
"\"plain\":%d,\"weak\":%d,\"duplicates\":%d,"
|
|
"\"findings_count\":%d,\"findings\":[",
|
|
ctx.files, ctx.hosts, ctx.hashed, ctx.plain, ctx.weak,
|
|
ctx.duplicates, ctx.count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* -------------------------------------------------------------- logrotate_audit */
|
|
|
|
typedef struct {
|
|
mcp_buf *b;
|
|
int first;
|
|
int count;
|
|
int files;
|
|
} lr_ctx;
|
|
|
|
static void lr_finding(lr_ctx *ctx, const char *path, int line,
|
|
const char *kind, const char *severity,
|
|
const char *detail)
|
|
{
|
|
if (ctx->count >= 100)
|
|
return;
|
|
if (!ctx->first)
|
|
mcp_buf_puts(ctx->b, ",");
|
|
ctx->first = 0;
|
|
ctx->count++;
|
|
mcp_buf_puts(ctx->b, "{\"path\":");
|
|
apex_json_str(ctx->b, path);
|
|
mcp_buf_printf(ctx->b, ",\"line\":%d,\"kind\":", line);
|
|
apex_json_str(ctx->b, kind);
|
|
mcp_buf_puts(ctx->b, ",\"severity\":");
|
|
apex_json_str(ctx->b, severity);
|
|
mcp_buf_puts(ctx->b, ",\"detail\":");
|
|
apex_json_str(ctx->b, detail);
|
|
mcp_buf_puts(ctx->b, "}");
|
|
}
|
|
|
|
static void lr_scan_file(lr_ctx *ctx, const char *path)
|
|
{
|
|
FILE *f;
|
|
struct stat st;
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
int lineno = 0;
|
|
int compress = 0;
|
|
int in_post = 0;
|
|
|
|
if (ctx->files >= 100)
|
|
return;
|
|
f = fopen(path, "rb");
|
|
if (f == NULL)
|
|
return;
|
|
ctx->files++;
|
|
if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0)
|
|
lr_finding(ctx, path, 0, "world-writable", "high",
|
|
"logrotate file is writable by anyone");
|
|
while (getline(&line, &cap, f) >= 0) {
|
|
char *t = line;
|
|
|
|
lineno++;
|
|
while (*t == ' ' || *t == '\t')
|
|
t++;
|
|
if (*t == '\0' || *t == '#')
|
|
continue;
|
|
if (strncmp(t, "compress", 8) == 0)
|
|
compress = 1;
|
|
if (strncmp(t, "rotate ", 7) == 0) {
|
|
int n = atoi(t + 7);
|
|
|
|
if (n <= 2)
|
|
lr_finding(ctx, path, lineno, "rotate-low", "low",
|
|
"only one or two old logs are kept");
|
|
}
|
|
if (strncmp(t, "postrotate", 10) == 0)
|
|
in_post = 1;
|
|
else if (strncmp(t, "endscript", 9) == 0)
|
|
in_post = 0;
|
|
else if (in_post &&
|
|
(strstr(t, "/tmp/") != NULL ||
|
|
strstr(t, "/dev/shm/") != NULL ||
|
|
strstr(t, "curl ") != NULL ||
|
|
strstr(t, "wget ") != NULL))
|
|
lr_finding(ctx, path, lineno, "unsafe-postrotate", "medium",
|
|
"postrotate runs from a temp path or downloads code");
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
if (!compress)
|
|
lr_finding(ctx, path, 0, "no-compress", "low",
|
|
"configuration does not compress rotated logs");
|
|
}
|
|
|
|
static void t_logrotate_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
char base[512];
|
|
char path[1024];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
lr_ctx ctx;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) >= sizeof(base) - 2) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(base, sizeof(base), "%s", root);
|
|
mcp_buf_init(&findings);
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.b = &findings;
|
|
ctx.first = 1;
|
|
snprintf(path, sizeof(path), "%s/etc/logrotate.conf", base);
|
|
lr_scan_file(&ctx, path);
|
|
snprintf(path, sizeof(path), "%s/etc/logrotate.d", base);
|
|
{
|
|
DIR *d = opendir(path);
|
|
struct dirent *ent;
|
|
|
|
if (d != NULL) {
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char full[1024];
|
|
struct stat st;
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
snprintf(full, sizeof(full), "%.800s/%.160s", path,
|
|
ent->d_name);
|
|
if (stat(full, &st) != 0 || !S_ISREG(st.st_mode))
|
|
continue;
|
|
lr_scan_file(&ctx, full);
|
|
}
|
|
closedir(d);
|
|
}
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, base);
|
|
mcp_buf_printf(&b, ",\"files\":%d,\"findings_count\":%d,\"findings\":[",
|
|
ctx.files, ctx.count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* ---------------------------------------------------------- nfs_exports_audit */
|
|
|
|
typedef struct {
|
|
mcp_buf *b;
|
|
int first;
|
|
int count;
|
|
int files;
|
|
int exports;
|
|
} nfs_ctx;
|
|
|
|
static void nfs_finding(nfs_ctx *ctx, const char *path, int line,
|
|
const char *kind, const char *severity,
|
|
const char *detail)
|
|
{
|
|
if (ctx->count >= 100)
|
|
return;
|
|
if (!ctx->first)
|
|
mcp_buf_puts(ctx->b, ",");
|
|
ctx->first = 0;
|
|
ctx->count++;
|
|
mcp_buf_puts(ctx->b, "{\"path\":");
|
|
apex_json_str(ctx->b, path);
|
|
mcp_buf_printf(ctx->b, ",\"line\":%d,\"kind\":", line);
|
|
apex_json_str(ctx->b, kind);
|
|
mcp_buf_puts(ctx->b, ",\"severity\":");
|
|
apex_json_str(ctx->b, severity);
|
|
mcp_buf_puts(ctx->b, ",\"detail\":");
|
|
apex_json_str(ctx->b, detail);
|
|
mcp_buf_puts(ctx->b, "}");
|
|
}
|
|
|
|
static void nfs_scan_file(nfs_ctx *ctx, const char *path)
|
|
{
|
|
FILE *f;
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
int lineno = 0;
|
|
|
|
if (ctx->files >= 100)
|
|
return;
|
|
f = fopen(path, "rb");
|
|
if (f == NULL)
|
|
return;
|
|
ctx->files++;
|
|
while (getline(&line, &cap, f) >= 0) {
|
|
char *t = line;
|
|
char *save = NULL;
|
|
char *tok;
|
|
char *copy;
|
|
int world = 0;
|
|
int writable = 0;
|
|
int no_root = 0;
|
|
|
|
lineno++;
|
|
while (*t == ' ' || *t == '\t')
|
|
t++;
|
|
trim_eol(t);
|
|
if (*t == '\0' || *t == '#')
|
|
continue;
|
|
ctx->exports++;
|
|
copy = strdup(t);
|
|
if (copy == NULL)
|
|
continue;
|
|
tok = strtok_r(copy, " \t", &save);
|
|
tok = strtok_r(NULL, " \t", &save);
|
|
while (tok != NULL) {
|
|
char opts[512];
|
|
size_t w = 0;
|
|
const char *paren = strchr(tok, '(');
|
|
|
|
opts[0] = '\0';
|
|
if (paren != NULL) {
|
|
const char *p = paren + 1;
|
|
|
|
while (*p != '\0' && *p != ')' && w + 1 < sizeof(opts))
|
|
opts[w++] = *p++;
|
|
opts[w] = '\0';
|
|
}
|
|
{
|
|
size_t hl = paren != NULL ? (size_t)(paren - tok)
|
|
: strlen(tok);
|
|
|
|
if (hl == 1 && tok[0] == '*')
|
|
world = 1;
|
|
}
|
|
if (!mount_has_opt(opts, "ro"))
|
|
writable = 1;
|
|
if (mount_has_opt(opts, "no_root_squash"))
|
|
no_root = 1;
|
|
tok = strtok_r(NULL, " \t", &save);
|
|
}
|
|
if (world)
|
|
nfs_finding(ctx, path, lineno, "world-export", "high",
|
|
"export is open to every host (*)");
|
|
if (writable)
|
|
nfs_finding(ctx, path, lineno, "writable", "medium",
|
|
"export is writable (no ro option)");
|
|
if (no_root)
|
|
nfs_finding(ctx, path, lineno, "no-root-squash", "high",
|
|
"root is not squashed on this export");
|
|
free(copy);
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
}
|
|
|
|
static void t_nfs_exports_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
char base[512];
|
|
char path[1024];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
nfs_ctx ctx;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) >= sizeof(base) - 2) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(base, sizeof(base), "%s", root);
|
|
mcp_buf_init(&findings);
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.b = &findings;
|
|
ctx.first = 1;
|
|
snprintf(path, sizeof(path), "%s/etc/exports", base);
|
|
nfs_scan_file(&ctx, path);
|
|
snprintf(path, sizeof(path), "%s/etc/exports.d", base);
|
|
{
|
|
DIR *d = opendir(path);
|
|
struct dirent *ent;
|
|
|
|
if (d != NULL) {
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char full[1024];
|
|
struct stat st;
|
|
size_t nl = strlen(ent->d_name);
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
if (nl < 9 ||
|
|
strcmp(ent->d_name + nl - 8, ".exports") != 0)
|
|
continue;
|
|
snprintf(full, sizeof(full), "%.800s/%.160s", path,
|
|
ent->d_name);
|
|
if (stat(full, &st) != 0 || !S_ISREG(st.st_mode))
|
|
continue;
|
|
nfs_scan_file(&ctx, full);
|
|
}
|
|
closedir(d);
|
|
}
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, base);
|
|
mcp_buf_printf(&b, ",\"files\":%d,\"exports\":%d,\"findings_count\":%d,"
|
|
"\"findings\":[", ctx.files, ctx.exports, ctx.count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* ----------------------------------------------------------------- samba_audit */
|
|
|
|
typedef struct {
|
|
char name[64];
|
|
char path[256];
|
|
int line;
|
|
int guest;
|
|
int writable;
|
|
int valid_users;
|
|
int active;
|
|
} smb_share;
|
|
|
|
typedef struct {
|
|
mcp_buf *b;
|
|
int first;
|
|
int count;
|
|
int shares;
|
|
} smb_ctx;
|
|
|
|
static void smb_finding(smb_ctx *ctx, const smb_share *sh, const char *kind,
|
|
const char *severity, const char *detail)
|
|
{
|
|
if (ctx->count >= 100)
|
|
return;
|
|
if (!ctx->first)
|
|
mcp_buf_puts(ctx->b, ",");
|
|
ctx->first = 0;
|
|
ctx->count++;
|
|
mcp_buf_puts(ctx->b, "{\"section\":");
|
|
apex_json_str(ctx->b, sh->name);
|
|
mcp_buf_printf(ctx->b, ",\"line\":%d,\"kind\":", sh->line);
|
|
apex_json_str(ctx->b, kind);
|
|
mcp_buf_puts(ctx->b, ",\"severity\":");
|
|
apex_json_str(ctx->b, severity);
|
|
mcp_buf_puts(ctx->b, ",\"detail\":");
|
|
apex_json_str(ctx->b, detail);
|
|
mcp_buf_puts(ctx->b, "}");
|
|
}
|
|
|
|
static void smb_flush(smb_ctx *ctx, smb_share *sh)
|
|
{
|
|
if (!sh->active)
|
|
return;
|
|
sh->active = 0;
|
|
if (strcmp(sh->name, "global") == 0)
|
|
return;
|
|
ctx->shares++;
|
|
if (sh->path[0] != '\0' && strcmp(sh->path, "/") == 0)
|
|
smb_finding(ctx, sh, "root-share", "high",
|
|
"share exposes the filesystem root");
|
|
if (sh->guest)
|
|
smb_finding(ctx, sh, "guest-access", "high",
|
|
"share allows guest access");
|
|
if (sh->writable)
|
|
smb_finding(ctx, sh, "writable-share", "medium",
|
|
"share is writable");
|
|
if (sh->guest && !sh->valid_users)
|
|
smb_finding(ctx, sh, "no-valid-users", "low",
|
|
"guest share has no valid users restriction");
|
|
}
|
|
|
|
static void smb_scan_file(smb_ctx *ctx, const char *path)
|
|
{
|
|
FILE *f = fopen(path, "rb");
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
int lineno = 0;
|
|
smb_share sh;
|
|
|
|
memset(&sh, 0, sizeof(sh));
|
|
if (f == NULL)
|
|
return;
|
|
while (getline(&line, &cap, f) >= 0) {
|
|
char *t = line;
|
|
size_t n;
|
|
|
|
lineno++;
|
|
while (*t == ' ' || *t == '\t')
|
|
t++;
|
|
n = strlen(t);
|
|
while (n > 0 && (t[n - 1] == '\n' || t[n - 1] == '\r'))
|
|
t[--n] = '\0';
|
|
if (*t == '\0' || *t == '#' || *t == ';')
|
|
continue;
|
|
if (*t == '[') {
|
|
char *close = strchr(t, ']');
|
|
|
|
smb_flush(ctx, &sh);
|
|
memset(&sh, 0, sizeof(sh));
|
|
if (close != NULL) {
|
|
size_t w = (size_t)(close - t - 1);
|
|
|
|
if (w >= sizeof(sh.name))
|
|
w = sizeof(sh.name) - 1;
|
|
memcpy(sh.name, t + 1, w);
|
|
sh.name[w] = '\0';
|
|
sh.line = lineno;
|
|
sh.active = 1;
|
|
}
|
|
continue;
|
|
}
|
|
if (!sh.active)
|
|
continue;
|
|
{
|
|
char *eq = strchr(t, '=');
|
|
char *key;
|
|
char *value;
|
|
size_t kl;
|
|
|
|
if (eq == NULL)
|
|
continue;
|
|
*eq = '\0';
|
|
key = t;
|
|
value = eq + 1;
|
|
kl = strlen(key);
|
|
while (kl > 0 && (key[kl - 1] == ' ' || key[kl - 1] == '\t'))
|
|
key[--kl] = '\0';
|
|
while (*value == ' ' || *value == '\t')
|
|
value++;
|
|
if (strcasecmp(key, "guest ok") == 0 ||
|
|
strcasecmp(key, "public") == 0) {
|
|
if (strcasecmp(value, "yes") == 0 ||
|
|
strcasecmp(value, "true") == 0)
|
|
sh.guest = 1;
|
|
} else if (strcasecmp(key, "writable") == 0 ||
|
|
strcasecmp(key, "writeable") == 0) {
|
|
if (strcasecmp(value, "yes") == 0)
|
|
sh.writable = 1;
|
|
} else if (strcasecmp(key, "read only") == 0) {
|
|
if (strcasecmp(value, "no") == 0)
|
|
sh.writable = 1;
|
|
} else if (strcasecmp(key, "valid users") == 0) {
|
|
if (*value != '\0')
|
|
sh.valid_users = 1;
|
|
} else if (strcasecmp(key, "path") == 0) {
|
|
snprintf(sh.path, sizeof(sh.path), "%.200s", value);
|
|
}
|
|
}
|
|
}
|
|
smb_flush(ctx, &sh);
|
|
free(line);
|
|
fclose(f);
|
|
}
|
|
|
|
static void t_samba_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
char base[512];
|
|
char path[1024];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
smb_ctx ctx;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) >= sizeof(base) - 2) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(base, sizeof(base), "%s", root);
|
|
mcp_buf_init(&findings);
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.b = &findings;
|
|
ctx.first = 1;
|
|
snprintf(path, sizeof(path), "%s/etc/samba/smb.conf", base);
|
|
smb_scan_file(&ctx, path);
|
|
snprintf(path, sizeof(path), "%s/etc/smb.conf", base);
|
|
smb_scan_file(&ctx, path);
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, base);
|
|
mcp_buf_printf(&b, ",\"shares\":%d,\"findings_count\":%d,\"findings\":[",
|
|
ctx.shares, ctx.count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* --------------------------------------------------------------- snmpd_audit */
|
|
|
|
typedef struct {
|
|
char community[64];
|
|
char mode[16];
|
|
} snmp_comm;
|
|
|
|
typedef struct {
|
|
mcp_buf *b;
|
|
int first;
|
|
int count;
|
|
int files;
|
|
snmp_comm comms[20];
|
|
int ncomms;
|
|
} snmpd_ctx;
|
|
|
|
static void snmpd_finding(snmpd_ctx *ctx, const char *path, int line,
|
|
const char *kind, const char *severity,
|
|
const char *detail)
|
|
{
|
|
if (ctx->count >= 100)
|
|
return;
|
|
if (!ctx->first)
|
|
mcp_buf_puts(ctx->b, ",");
|
|
ctx->first = 0;
|
|
ctx->count++;
|
|
mcp_buf_puts(ctx->b, "{\"path\":");
|
|
apex_json_str(ctx->b, path);
|
|
mcp_buf_printf(ctx->b, ",\"line\":%d,\"kind\":", line);
|
|
apex_json_str(ctx->b, kind);
|
|
mcp_buf_puts(ctx->b, ",\"severity\":");
|
|
apex_json_str(ctx->b, severity);
|
|
mcp_buf_puts(ctx->b, ",\"detail\":");
|
|
apex_json_str(ctx->b, detail);
|
|
mcp_buf_puts(ctx->b, "}");
|
|
}
|
|
|
|
static void snmpd_comm_add(snmpd_ctx *ctx, const char *community,
|
|
const char *mode)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < ctx->ncomms; i++)
|
|
if (strcmp(ctx->comms[i].community, community) == 0 &&
|
|
strcmp(ctx->comms[i].mode, mode) == 0)
|
|
return;
|
|
if (ctx->ncomms < 20) {
|
|
snprintf(ctx->comms[ctx->ncomms].community,
|
|
sizeof(ctx->comms[0].community), "%.48s", community);
|
|
snprintf(ctx->comms[ctx->ncomms].mode,
|
|
sizeof(ctx->comms[0].mode), "%.12s", mode);
|
|
ctx->ncomms++;
|
|
}
|
|
}
|
|
|
|
static int snmpd_weak(const char *community)
|
|
{
|
|
return strcmp(community, "public") == 0 ||
|
|
strcmp(community, "private") == 0 ||
|
|
strcmp(community, "default") == 0;
|
|
}
|
|
|
|
static void snmpd_scan_file(snmpd_ctx *ctx, const char *path)
|
|
{
|
|
FILE *f = fopen(path, "rb");
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
int lineno = 0;
|
|
|
|
if (f == NULL)
|
|
return;
|
|
ctx->files++;
|
|
while (getline(&line, &cap, f) >= 0) {
|
|
char *t = line;
|
|
char *tokens[6];
|
|
int ntok = 0;
|
|
char *save = NULL;
|
|
char *tok;
|
|
|
|
lineno++;
|
|
while (*t == ' ' || *t == '\t')
|
|
t++;
|
|
trim_eol(t);
|
|
if (*t == '\0' || *t == '#')
|
|
continue;
|
|
tok = strtok_r(t, " \t", &save);
|
|
while (tok != NULL && ntok < 6) {
|
|
tokens[ntok++] = tok;
|
|
tok = strtok_r(NULL, " \t", &save);
|
|
}
|
|
if (ntok < 2)
|
|
continue;
|
|
if (strcmp(tokens[0], "rocommunity") == 0 ||
|
|
strcmp(tokens[0], "rocommunity6") == 0) {
|
|
snmpd_comm_add(ctx, tokens[1], "ro");
|
|
if (snmpd_weak(tokens[1]))
|
|
snmpd_finding(ctx, path, lineno, "weak-community",
|
|
"medium",
|
|
"read-only community is public/private/default");
|
|
} else if (strcmp(tokens[0], "rwcommunity") == 0 ||
|
|
strcmp(tokens[0], "rwcommunity6") == 0) {
|
|
snmpd_comm_add(ctx, tokens[1], "rw");
|
|
snmpd_finding(ctx, path, lineno, "rw-community", "high",
|
|
"write community is configured");
|
|
} else if (strcmp(tokens[0], "community") == 0) {
|
|
snmpd_comm_add(ctx, tokens[1], "v1");
|
|
if (snmpd_weak(tokens[1]))
|
|
snmpd_finding(ctx, path, lineno, "weak-community",
|
|
"medium",
|
|
"SNMPv1 community is public/private/default");
|
|
} else if (strcmp(tokens[0], "com2sec") == 0 ||
|
|
strcmp(tokens[0], "com2sec6") == 0) {
|
|
if (ntok >= 4) {
|
|
snmpd_comm_add(ctx, tokens[3], "com2sec");
|
|
if (snmpd_weak(tokens[3]))
|
|
snmpd_finding(ctx, path, lineno, "weak-community",
|
|
"medium",
|
|
"com2sec uses a public/private/default community");
|
|
if (strcmp(tokens[2], "default") == 0)
|
|
snmpd_finding(ctx, path, lineno, "default-source",
|
|
"medium",
|
|
"com2sec accepts the community from any source");
|
|
}
|
|
}
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
}
|
|
|
|
static void t_snmpd_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
char base[512];
|
|
char path[1024];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
snmpd_ctx ctx;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) >= sizeof(base) - 2) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(base, sizeof(base), "%s", root);
|
|
mcp_buf_init(&findings);
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.b = &findings;
|
|
ctx.first = 1;
|
|
snprintf(path, sizeof(path), "%s/etc/snmp/snmpd.conf", base);
|
|
snmpd_scan_file(&ctx, path);
|
|
snprintf(path, sizeof(path), "%s/etc/snmpd.conf", base);
|
|
snmpd_scan_file(&ctx, path);
|
|
snprintf(path, sizeof(path), "%s/etc/snmp/snmpd.conf.d", base);
|
|
{
|
|
DIR *d = opendir(path);
|
|
struct dirent *ent;
|
|
|
|
if (d != NULL) {
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char full[1024];
|
|
size_t nl = strlen(ent->d_name);
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
if (nl < 6 || strcmp(ent->d_name + nl - 5, ".conf") != 0)
|
|
continue;
|
|
snprintf(full, sizeof(full), "%.800s/%.160s", path,
|
|
ent->d_name);
|
|
snmpd_scan_file(&ctx, full);
|
|
}
|
|
closedir(d);
|
|
}
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, base);
|
|
mcp_buf_printf(&b, ",\"files\":%d,\"communities\":[", ctx.files);
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < ctx.ncomms; i++) {
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
mcp_buf_puts(&b, "{\"community\":");
|
|
apex_json_str(&b, ctx.comms[i].community);
|
|
mcp_buf_puts(&b, ",\"mode\":");
|
|
apex_json_str(&b, ctx.comms[i].mode);
|
|
mcp_buf_puts(&b, "}");
|
|
}
|
|
}
|
|
mcp_buf_printf(&b, "],\"findings_count\":%d,\"findings\":[", ctx.count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* -------------------------------------------------------------- rsyslog_audit */
|
|
|
|
typedef struct {
|
|
mcp_buf *b;
|
|
int first;
|
|
int count;
|
|
int files;
|
|
int auth_logged;
|
|
char targets[10][256];
|
|
int ntargets;
|
|
} rsys_ctx;
|
|
|
|
static void rsys_finding(rsys_ctx *ctx, const char *path, int line,
|
|
const char *kind, const char *severity,
|
|
const char *detail)
|
|
{
|
|
if (ctx->count >= 100)
|
|
return;
|
|
if (!ctx->first)
|
|
mcp_buf_puts(ctx->b, ",");
|
|
ctx->first = 0;
|
|
ctx->count++;
|
|
mcp_buf_puts(ctx->b, "{\"path\":");
|
|
apex_json_str(ctx->b, path);
|
|
mcp_buf_printf(ctx->b, ",\"line\":%d,\"kind\":", line);
|
|
apex_json_str(ctx->b, kind);
|
|
mcp_buf_puts(ctx->b, ",\"severity\":");
|
|
apex_json_str(ctx->b, severity);
|
|
mcp_buf_puts(ctx->b, ",\"detail\":");
|
|
apex_json_str(ctx->b, detail);
|
|
mcp_buf_puts(ctx->b, "}");
|
|
}
|
|
|
|
static void rsys_target_add(rsys_ctx *ctx, const char *target)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < ctx->ntargets; i++)
|
|
if (strcmp(ctx->targets[i], target) == 0)
|
|
return;
|
|
if (ctx->ntargets < 10)
|
|
snprintf(ctx->targets[ctx->ntargets++],
|
|
sizeof(ctx->targets[0]), "%.200s", target);
|
|
}
|
|
|
|
static void rsys_scan_file(rsys_ctx *ctx, const char *path)
|
|
{
|
|
FILE *f = fopen(path, "rb");
|
|
struct stat st;
|
|
char *line = NULL;
|
|
size_t cap = 0;
|
|
|
|
if (f == NULL)
|
|
return;
|
|
ctx->files++;
|
|
if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0)
|
|
rsys_finding(ctx, path, 0, "world-writable", "high",
|
|
"rsyslog configuration is writable by anyone");
|
|
while (getline(&line, &cap, f) >= 0) {
|
|
char *t = line;
|
|
const char *at;
|
|
|
|
while (*t == ' ' || *t == '\t')
|
|
t++;
|
|
trim_eol(t);
|
|
if (*t == '\0' || *t == '#')
|
|
continue;
|
|
if (strstr(t, "auth,authpriv") != NULL ||
|
|
strstr(t, "authpriv.*") != NULL ||
|
|
strstr(t, "auth.*") != NULL)
|
|
ctx->auth_logged = 1;
|
|
at = strchr(t, '@');
|
|
if (at != NULL && at[1] != '\0') {
|
|
char target[256];
|
|
size_t w = 0;
|
|
|
|
while (*at != '\0' && *at != ' ' && *at != '\t' &&
|
|
w + 1 < sizeof(target))
|
|
target[w++] = *at++;
|
|
target[w] = '\0';
|
|
if (strchr(target, '.') != NULL)
|
|
rsys_target_add(ctx, target);
|
|
}
|
|
}
|
|
free(line);
|
|
fclose(f);
|
|
}
|
|
|
|
static void t_rsyslog_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
char base[512];
|
|
char path[1024];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
rsys_ctx ctx;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) >= sizeof(base) - 2) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(base, sizeof(base), "%s", root);
|
|
mcp_buf_init(&findings);
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.b = &findings;
|
|
ctx.first = 1;
|
|
snprintf(path, sizeof(path), "%s/etc/rsyslog.conf", base);
|
|
rsys_scan_file(&ctx, path);
|
|
snprintf(path, sizeof(path), "%s/etc/rsyslog.d", base);
|
|
{
|
|
DIR *d = opendir(path);
|
|
struct dirent *ent;
|
|
|
|
if (d != NULL) {
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char full[1024];
|
|
size_t nl = strlen(ent->d_name);
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
if (nl < 6 || strcmp(ent->d_name + nl - 5, ".conf") != 0)
|
|
continue;
|
|
snprintf(full, sizeof(full), "%.800s/%.160s", path,
|
|
ent->d_name);
|
|
rsys_scan_file(&ctx, full);
|
|
}
|
|
closedir(d);
|
|
}
|
|
}
|
|
if (!ctx.auth_logged)
|
|
rsys_finding(&ctx, base, 0, "no-auth-logging", "medium",
|
|
"no dedicated auth/authpriv log rule");
|
|
if (ctx.ntargets == 0)
|
|
rsys_finding(&ctx, base, 0, "no-remote-logging", "low",
|
|
"logs are not forwarded to another host");
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, base);
|
|
mcp_buf_printf(&b, ",\"files\":%d,\"auth_logged\":%s,\"targets\":[",
|
|
ctx.files, ctx.auth_logged ? "true" : "false");
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < ctx.ntargets; i++) {
|
|
if (i > 0)
|
|
mcp_buf_puts(&b, ",");
|
|
apex_json_str(&b, ctx.targets[i]);
|
|
}
|
|
}
|
|
mcp_buf_printf(&b, "],\"findings_count\":%d,\"findings\":[", ctx.count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* -------------------------------------------------------------- postfix_audit */
|
|
|
|
/*
|
|
* One directive reader for the config audits below. It accepts both
|
|
* "key value" (nginx, redis) and "key = value" (postfix), strips a trailing
|
|
* semicolon and surrounding quotes, and skips comments.
|
|
*/
|
|
static int conf_value(const char *text, const char *key, char *out, size_t n)
|
|
{
|
|
const char *p = text;
|
|
size_t kl = strlen(key);
|
|
|
|
out[0] = '\0';
|
|
while (p != NULL && *p != '\0') {
|
|
const char *eol = strchr(p, '\n');
|
|
size_t len = eol != NULL ? (size_t)(eol - p) : strlen(p);
|
|
char line[1024];
|
|
char *s;
|
|
char *v;
|
|
size_t vl;
|
|
|
|
if (len >= sizeof(line))
|
|
len = sizeof(line) - 1;
|
|
memcpy(line, p, len);
|
|
line[len] = '\0';
|
|
s = line;
|
|
while (*s == ' ' || *s == '\t')
|
|
s++;
|
|
if (*s != '#' && strncasecmp(s, key, kl) == 0 &&
|
|
(s[kl] == ' ' || s[kl] == '\t' || s[kl] == '=' ||
|
|
s[kl] == '\0')) {
|
|
v = s + kl;
|
|
while (*v == ' ' || *v == '\t' || *v == '=')
|
|
v++;
|
|
vl = strlen(v);
|
|
while (vl > 0 && (v[vl - 1] == ';' || v[vl - 1] == ' ' ||
|
|
v[vl - 1] == '\t' || v[vl - 1] == '\r'))
|
|
v[--vl] = '\0';
|
|
if (vl >= 2 && v[0] == '"' && v[vl - 1] == '"') {
|
|
v++;
|
|
vl -= 2;
|
|
}
|
|
if (vl >= n)
|
|
vl = n - 1;
|
|
memcpy(out, v, vl);
|
|
out[vl] = '\0';
|
|
return 1;
|
|
}
|
|
p = eol != NULL ? eol + 1 : NULL;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int conf_has_word(const char *list, const char *word)
|
|
{
|
|
const char *p = list;
|
|
size_t wl = strlen(word);
|
|
|
|
while (*p != '\0') {
|
|
const char *end;
|
|
size_t len;
|
|
|
|
while (*p == ' ' || *p == '\t' || *p == ',')
|
|
p++;
|
|
end = p;
|
|
while (*end != '\0' && *end != ' ' && *end != '\t' && *end != ',')
|
|
end++;
|
|
len = (size_t)(end - p);
|
|
if (len == wl && strncasecmp(p, word, wl) == 0)
|
|
return 1;
|
|
p = end;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void t_postfix_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
static char config[65536];
|
|
char path[1024];
|
|
char mynetworks[512];
|
|
char relay[512];
|
|
char recipient[512];
|
|
char tls[128];
|
|
char myhostname[256];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
int first = 1;
|
|
int count = 0;
|
|
struct stat st;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) > 400) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(path, sizeof(path), "%s/etc/postfix/main.cf", root);
|
|
if (read_text(path, config, sizeof(config)) <= 0) {
|
|
mcp_reply_error(c, "cannot read postfix main.cf");
|
|
return;
|
|
}
|
|
mynetworks[0] = relay[0] = recipient[0] = '\0';
|
|
tls[0] = myhostname[0] = '\0';
|
|
conf_value(config, "mynetworks", mynetworks, sizeof(mynetworks));
|
|
conf_value(config, "smtpd_relay_restrictions", relay, sizeof(relay));
|
|
conf_value(config, "smtpd_recipient_restrictions", recipient,
|
|
sizeof(recipient));
|
|
conf_value(config, "smtpd_tls_security_level", tls, sizeof(tls));
|
|
conf_value(config, "myhostname", myhostname, sizeof(myhostname));
|
|
mcp_buf_init(&findings);
|
|
if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) {
|
|
add_finding(&findings, &first, "world-writable", "high",
|
|
"postfix configuration is writable by anyone");
|
|
count++;
|
|
}
|
|
if (strstr(mynetworks, "0.0.0.0/0") != NULL ||
|
|
strstr(mynetworks, "::/0") != NULL) {
|
|
add_finding(&findings, &first, "mynetworks-all", "high",
|
|
"mynetworks covers every address");
|
|
count++;
|
|
}
|
|
if (relay[0] != '\0') {
|
|
if (!conf_has_word(relay, "reject_unauth_destination")) {
|
|
add_finding(&findings, &first, "relay-open", "high",
|
|
"smtpd_relay_restrictions has no "
|
|
"reject_unauth_destination");
|
|
count++;
|
|
}
|
|
} else if (recipient[0] != '\0') {
|
|
if (!conf_has_word(recipient, "reject_unauth_destination")) {
|
|
add_finding(&findings, &first, "relay-open", "high",
|
|
"smtpd_recipient_restrictions has no "
|
|
"reject_unauth_destination");
|
|
count++;
|
|
}
|
|
} else {
|
|
add_finding(&findings, &first, "relay-unrestricted", "high",
|
|
"no relay restriction list is configured");
|
|
count++;
|
|
}
|
|
if (tls[0] == '\0' || strcasecmp(tls, "none") == 0 ||
|
|
strcasecmp(tls, "may") == 0) {
|
|
add_finding(&findings, &first, "tls-optional", "medium",
|
|
tls[0] == '\0'
|
|
? "smtpd_tls_security_level is not set"
|
|
: "smtpd_tls_security_level does not require TLS");
|
|
count++;
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, root);
|
|
mcp_buf_puts(&b, ",\"path\":");
|
|
apex_json_str(&b, path);
|
|
mcp_buf_puts(&b, ",\"directives\":{\"myhostname\":");
|
|
apex_json_str(&b, myhostname);
|
|
mcp_buf_puts(&b, ",\"mynetworks\":");
|
|
apex_json_str(&b, mynetworks);
|
|
mcp_buf_puts(&b, ",\"smtpd_relay_restrictions\":");
|
|
apex_json_str(&b, relay);
|
|
mcp_buf_puts(&b, ",\"smtpd_tls_security_level\":");
|
|
apex_json_str(&b, tls);
|
|
mcp_buf_puts(&b, "},\"findings_count\":");
|
|
mcp_buf_printf(&b, "%d,\"findings\":[", count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- nginx_audit */
|
|
|
|
static void nginx_scan_file(mcp_buf *findings, int *first, int *count,
|
|
const char *path, int *tokens_off,
|
|
int *tokens_seen, int *autoindex, int *weak_proto,
|
|
int *weak_cipher)
|
|
{
|
|
static char text[65536];
|
|
char value[512];
|
|
struct stat st;
|
|
|
|
if (read_text(path, text, sizeof(text)) < 0)
|
|
return;
|
|
if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) {
|
|
add_finding(findings, first, "world-writable", "high",
|
|
"%s is writable by anyone", path);
|
|
(*count)++;
|
|
}
|
|
if (conf_value(text, "server_tokens", value, sizeof(value))) {
|
|
*tokens_seen = 1;
|
|
if (strcasecmp(value, "off") == 0)
|
|
*tokens_off = 1;
|
|
}
|
|
if (conf_value(text, "autoindex", value, sizeof(value)) &&
|
|
strcasecmp(value, "on") == 0) {
|
|
if (!*autoindex)
|
|
add_finding(findings, first, "autoindex", "medium",
|
|
"%s enables directory listing", path);
|
|
*autoindex = 1;
|
|
(*count)++;
|
|
}
|
|
if (conf_value(text, "ssl_protocols", value, sizeof(value))) {
|
|
if (conf_has_word(value, "TLSv1") ||
|
|
conf_has_word(value, "TLSv1.1")) {
|
|
if (!*weak_proto)
|
|
add_finding(findings, first, "weak-protocol",
|
|
"high",
|
|
"%s allows TLSv1/TLSv1.1", path);
|
|
*weak_proto = 1;
|
|
(*count)++;
|
|
}
|
|
}
|
|
if (conf_value(text, "ssl_ciphers", value, sizeof(value))) {
|
|
if (strstr(value, "RC4") != NULL ||
|
|
strstr(value, "3DES") != NULL ||
|
|
strstr(value, "NULL") != NULL ||
|
|
strstr(value, "EXPORT") != NULL ||
|
|
strstr(value, "MD5") != NULL) {
|
|
if (!*weak_cipher)
|
|
add_finding(findings, first, "weak-cipher",
|
|
"high",
|
|
"%s allows legacy TLS ciphers", path);
|
|
*weak_cipher = 1;
|
|
(*count)++;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void t_nginx_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
char path[1024];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
int first = 1;
|
|
int count = 0;
|
|
int files = 0;
|
|
int tokens_seen = 0;
|
|
int tokens_off = 0;
|
|
int autoindex = 0;
|
|
int weak_proto = 0;
|
|
int weak_cipher = 0;
|
|
static const char *dirs[] = { "/etc/nginx/conf.d",
|
|
"/etc/nginx/sites-enabled", NULL };
|
|
int i;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) > 400) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(path, sizeof(path), "%s/etc/nginx/nginx.conf", root);
|
|
{
|
|
struct stat st;
|
|
|
|
if (stat(path, &st) != 0) {
|
|
mcp_reply_error(c, "cannot read nginx.conf");
|
|
return;
|
|
}
|
|
}
|
|
mcp_buf_init(&findings);
|
|
nginx_scan_file(&findings, &first, &count, path, &tokens_off,
|
|
&tokens_seen, &autoindex, &weak_proto, &weak_cipher);
|
|
files++;
|
|
for (i = 0; dirs[i] != NULL; i++) {
|
|
DIR *d;
|
|
struct dirent *ent;
|
|
|
|
snprintf(path, sizeof(path), "%.400s%s", root, dirs[i]);
|
|
d = opendir(path);
|
|
if (d == NULL)
|
|
continue;
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char full[1024];
|
|
struct stat st;
|
|
size_t nl = strlen(ent->d_name);
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
if (nl < 6 || strcmp(ent->d_name + nl - 5, ".conf") != 0)
|
|
continue;
|
|
snprintf(full, sizeof(full), "%.800s/%.160s", path,
|
|
ent->d_name);
|
|
if (stat(full, &st) != 0 || !S_ISREG(st.st_mode))
|
|
continue;
|
|
nginx_scan_file(&findings, &first, &count, full,
|
|
&tokens_off, &tokens_seen, &autoindex,
|
|
&weak_proto, &weak_cipher);
|
|
files++;
|
|
}
|
|
closedir(d);
|
|
}
|
|
if (!tokens_off) {
|
|
add_finding(&findings, &first, "server-tokens", "medium",
|
|
tokens_seen
|
|
? "server_tokens is enabled"
|
|
: "server_tokens is not set to off");
|
|
count++;
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, root);
|
|
mcp_buf_printf(&b, ",\"files\":%d,\"server_tokens_off\":%s,"
|
|
"\"autoindex\":%s,\"weak_protocols\":%s,"
|
|
"\"weak_ciphers\":%s,\"findings_count\":%d,"
|
|
"\"findings\":[",
|
|
files, tokens_off ? "true" : "false",
|
|
autoindex ? "true" : "false",
|
|
weak_proto ? "true" : "false",
|
|
weak_cipher ? "true" : "false", count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* --------------------------------------------------------- redis_config_audit */
|
|
|
|
static void t_redis_config_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
static char config[65536];
|
|
char path[1024];
|
|
char value[512];
|
|
char bind_addr[512];
|
|
char dir[512];
|
|
char debug[64];
|
|
char protected_mode[64];
|
|
int pass_set = 0;
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
int first = 1;
|
|
int count = 0;
|
|
struct stat st;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) > 400) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(path, sizeof(path), "%s/etc/redis/redis.conf", root);
|
|
if (read_text(path, config, sizeof(config)) <= 0) {
|
|
snprintf(path, sizeof(path), "%s/etc/redis.conf", root);
|
|
if (read_text(path, config, sizeof(config)) <= 0) {
|
|
mcp_reply_error(c, "cannot read redis.conf");
|
|
return;
|
|
}
|
|
}
|
|
bind_addr[0] = dir[0] = '\0';
|
|
debug[0] = protected_mode[0] = '\0';
|
|
if (conf_value(config, "requirepass", value, sizeof(value)) &&
|
|
value[0] != '\0')
|
|
pass_set = 1;
|
|
conf_value(config, "bind", bind_addr, sizeof(bind_addr));
|
|
conf_value(config, "dir", dir, sizeof(dir));
|
|
conf_value(config, "enable-debug-command", debug, sizeof(debug));
|
|
if (debug[0] == '\0')
|
|
conf_value(config, "enable-command", debug, sizeof(debug));
|
|
conf_value(config, "protected-mode", protected_mode,
|
|
sizeof(protected_mode));
|
|
mcp_buf_init(&findings);
|
|
if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) {
|
|
add_finding(&findings, &first, "world-writable", "high",
|
|
"redis configuration is writable by anyone");
|
|
count++;
|
|
}
|
|
if (!pass_set) {
|
|
add_finding(&findings, &first, "no-requirepass", "high",
|
|
"no requirepass is configured");
|
|
count++;
|
|
}
|
|
if (strcasecmp(protected_mode, "no") == 0) {
|
|
add_finding(&findings, &first, "protected-mode-off", "high",
|
|
"protected-mode is disabled");
|
|
count++;
|
|
}
|
|
if (conf_has_word(bind_addr, "0.0.0.0") ||
|
|
conf_has_word(bind_addr, "*") ||
|
|
conf_has_word(bind_addr, "::")) {
|
|
add_finding(&findings, &first, "bind-all", "high",
|
|
"redis binds to every interface");
|
|
count++;
|
|
}
|
|
if (strcasecmp(debug, "yes") == 0) {
|
|
add_finding(&findings, &first, "debug-command", "medium",
|
|
"the debug command is enabled");
|
|
count++;
|
|
}
|
|
if (strncmp(dir, "/tmp", 4) == 0 || strncmp(dir, "/dev/shm", 8) == 0) {
|
|
add_finding(&findings, &first, "temp-dir", "medium",
|
|
"redis writes its data into a temporary directory");
|
|
count++;
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, root);
|
|
mcp_buf_puts(&b, ",\"path\":");
|
|
apex_json_str(&b, path);
|
|
mcp_buf_printf(&b, ",\"requirepass_set\":%s,\"protected_mode\":",
|
|
pass_set ? "true" : "false");
|
|
apex_json_str(&b, protected_mode);
|
|
mcp_buf_puts(&b, ",\"bind\":");
|
|
apex_json_str(&b, bind_addr);
|
|
mcp_buf_puts(&b, ",\"dir\":");
|
|
apex_json_str(&b, dir);
|
|
mcp_buf_printf(&b, ",\"findings_count\":%d,\"findings\":[", count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* -------------------------------------------------------- mysql_config_audit */
|
|
|
|
static void mysql_scan_dir(mcp_buf *findings, int *first, int *count,
|
|
const char *dir, const char *suffix, int *files,
|
|
int *bind_all, int *skip_grant, int *local_infile,
|
|
int *secure_empty, char *bind_value,
|
|
size_t bind_n)
|
|
{
|
|
DIR *d;
|
|
struct dirent *ent;
|
|
|
|
d = opendir(dir);
|
|
if (d == NULL)
|
|
return;
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char path[1024];
|
|
char text[32768];
|
|
char value[512];
|
|
struct stat st;
|
|
size_t nl = strlen(ent->d_name);
|
|
size_t sl = strlen(suffix);
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
if (nl <= sl || strcmp(ent->d_name + nl - sl, suffix) != 0)
|
|
continue;
|
|
snprintf(path, sizeof(path), "%.800s/%.160s", dir, ent->d_name);
|
|
if (stat(path, &st) != 0 || !S_ISREG(st.st_mode))
|
|
continue;
|
|
if (read_text(path, text, sizeof(text)) < 0)
|
|
continue;
|
|
(*files)++;
|
|
if ((st.st_mode & 0002) != 0) {
|
|
add_finding(findings, first, "world-writable", "high",
|
|
"%s is writable by anyone", path);
|
|
(*count)++;
|
|
}
|
|
if (conf_value(text, "bind-address", value, sizeof(value))) {
|
|
snprintf(bind_value, bind_n, "%.200s", value);
|
|
if (conf_has_word(value, "0.0.0.0") ||
|
|
conf_has_word(value, "*") ||
|
|
conf_has_word(value, "::")) {
|
|
*bind_all = 1;
|
|
}
|
|
}
|
|
if (conf_value(text, "skip-grant-tables", value, sizeof(value)))
|
|
*skip_grant = 1;
|
|
if (conf_value(text, "local-infile", value, sizeof(value)) &&
|
|
(strcasecmp(value, "1") == 0 || strcasecmp(value, "on") == 0))
|
|
*local_infile = 1;
|
|
if (conf_value(text, "secure-file-priv", value, sizeof(value)) &&
|
|
value[0] == '\0')
|
|
*secure_empty = 1;
|
|
}
|
|
closedir(d);
|
|
}
|
|
|
|
static void t_mysql_config_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
static const char *dirs[] = { "/etc/mysql/my.cnf.d",
|
|
"/etc/mysql/conf.d",
|
|
"/etc/mysql/mariadb.conf.d", NULL };
|
|
const char *root = mcp_arg_string(args, "root");
|
|
static char text[65536];
|
|
char path[1024];
|
|
char bind_value[256];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
int first = 1;
|
|
int count = 0;
|
|
int files = 0;
|
|
int bind_all = 0;
|
|
int skip_grant = 0;
|
|
int local_infile = 0;
|
|
int secure_empty = 0;
|
|
int i;
|
|
struct stat st;
|
|
char value[512];
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) > 400) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
bind_value[0] = '\0';
|
|
mcp_buf_init(&findings);
|
|
snprintf(path, sizeof(path), "%s/etc/mysql/my.cnf", root);
|
|
if (read_text(path, text, sizeof(text)) > 0) {
|
|
files++;
|
|
if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) {
|
|
add_finding(&findings, &first, "world-writable", "high",
|
|
"%s is writable by anyone", path);
|
|
count++;
|
|
}
|
|
if (!conf_value(text, "bind-address", value, sizeof(value)))
|
|
conf_value(text, "bind_address", value, sizeof(value));
|
|
if (value[0] != '\0') {
|
|
snprintf(bind_value, sizeof(bind_value), "%.200s", value);
|
|
if (conf_has_word(value, "0.0.0.0") ||
|
|
conf_has_word(value, "*") ||
|
|
conf_has_word(value, "::"))
|
|
bind_all = 1;
|
|
}
|
|
if (conf_value(text, "skip-grant-tables", value, sizeof(value)))
|
|
skip_grant = 1;
|
|
if (conf_value(text, "local-infile", value, sizeof(value)) &&
|
|
(strcasecmp(value, "1") == 0 || strcasecmp(value, "on") == 0))
|
|
local_infile = 1;
|
|
if (conf_value(text, "secure-file-priv", value, sizeof(value)) &&
|
|
value[0] == '\0')
|
|
secure_empty = 1;
|
|
}
|
|
for (i = 0; dirs[i] != NULL; i++) {
|
|
snprintf(path, sizeof(path), "%.400s%s", root, dirs[i]);
|
|
mysql_scan_dir(&findings, &first, &count, path, ".cnf", &files,
|
|
&bind_all, &skip_grant, &local_infile,
|
|
&secure_empty, bind_value, sizeof(bind_value));
|
|
}
|
|
if (files == 0) {
|
|
mcp_reply_error(c, "cannot read a MySQL configuration");
|
|
return;
|
|
}
|
|
if (bind_all) {
|
|
add_finding(&findings, &first, "bind-all", "high",
|
|
"MySQL listens on every interface");
|
|
count++;
|
|
}
|
|
if (skip_grant) {
|
|
add_finding(&findings, &first, "skip-grant-tables", "high",
|
|
"the privilege tables are bypassed");
|
|
count++;
|
|
}
|
|
if (local_infile) {
|
|
add_finding(&findings, &first, "local-infile", "medium",
|
|
"clients may read local files");
|
|
count++;
|
|
}
|
|
if (secure_empty) {
|
|
add_finding(&findings, &first, "secure-file-priv-empty", "medium",
|
|
"secure-file-priv is empty and restricts nothing");
|
|
count++;
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, root);
|
|
mcp_buf_printf(&b, ",\"files\":%d,\"bind_address\":", files);
|
|
apex_json_str(&b, bind_value);
|
|
mcp_buf_printf(&b, ",\"skip_grant_tables\":%s,\"local_infile\":%s,"
|
|
"\"findings_count\":%d,\"findings\":[",
|
|
skip_grant ? "true" : "false",
|
|
local_infile ? "true" : "false", count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* --------------------------------------------------------------- apache_audit */
|
|
|
|
static void apache_scan_file(mcp_buf *findings, int *first, int *count,
|
|
const char *path, int *files, int *tokens_leak,
|
|
int *indexes, int *override_all,
|
|
int *weak_protocol, int *signature)
|
|
{
|
|
static char text[65536];
|
|
char value[512];
|
|
struct stat st;
|
|
|
|
if (read_text(path, text, sizeof(text)) < 0)
|
|
return;
|
|
(*files)++;
|
|
if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) {
|
|
add_finding(findings, first, "world-writable", "high",
|
|
"%s is writable by anyone", path);
|
|
(*count)++;
|
|
}
|
|
if (conf_value(text, "ServerTokens", value, sizeof(value))) {
|
|
if (strstr(value, "Full") != NULL ||
|
|
strstr(value, "OS") != NULL ||
|
|
strstr(value, "Minor") != NULL) {
|
|
if (!*tokens_leak)
|
|
add_finding(findings, first, "server-tokens",
|
|
"medium",
|
|
"%s exposes the full server banner",
|
|
path);
|
|
*tokens_leak = 1;
|
|
(*count)++;
|
|
}
|
|
}
|
|
if (conf_value(text, "Options", value, sizeof(value)) &&
|
|
conf_has_word(value, "Indexes")) {
|
|
if (!*indexes)
|
|
add_finding(findings, first, "options-indexes", "medium",
|
|
"%s allows directory listing", path);
|
|
*indexes = 1;
|
|
(*count)++;
|
|
}
|
|
if (conf_value(text, "AllowOverride", value, sizeof(value)) &&
|
|
strcasecmp(value, "All") == 0) {
|
|
if (!*override_all)
|
|
add_finding(findings, first, "allowoverride-all", "medium",
|
|
"%s lets .htaccess override everything", path);
|
|
*override_all = 1;
|
|
(*count)++;
|
|
}
|
|
if (conf_value(text, "SSLProtocol", value, sizeof(value))) {
|
|
if (conf_has_word(value, "SSLv3") ||
|
|
conf_has_word(value, "+SSLv3") ||
|
|
conf_has_word(value, "TLSv1") ||
|
|
conf_has_word(value, "+TLSv1") ||
|
|
conf_has_word(value, "TLSv1.1") ||
|
|
conf_has_word(value, "+TLSv1.1")) {
|
|
if (!*weak_protocol)
|
|
add_finding(findings, first, "weak-protocol",
|
|
"high",
|
|
"%s allows SSLv3/TLSv1", path);
|
|
*weak_protocol = 1;
|
|
(*count)++;
|
|
}
|
|
}
|
|
if (conf_value(text, "ServerSignature", value, sizeof(value)) &&
|
|
strcasecmp(value, "On") == 0) {
|
|
if (!*signature)
|
|
add_finding(findings, first, "server-signature", "low",
|
|
"%s adds a server signature to errors", path);
|
|
*signature = 1;
|
|
(*count)++;
|
|
}
|
|
}
|
|
|
|
static void apache_scan_dir(mcp_buf *findings, int *first, int *count,
|
|
const char *dir, int *files, int *tokens_leak,
|
|
int *indexes, int *override_all,
|
|
int *weak_protocol, int *signature)
|
|
{
|
|
DIR *d;
|
|
struct dirent *ent;
|
|
|
|
d = opendir(dir);
|
|
if (d == NULL)
|
|
return;
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char path[1024];
|
|
struct stat st;
|
|
size_t nl = strlen(ent->d_name);
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
if (nl < 6 || strcmp(ent->d_name + nl - 5, ".conf") != 0)
|
|
continue;
|
|
snprintf(path, sizeof(path), "%.800s/%.160s", dir, ent->d_name);
|
|
if (stat(path, &st) != 0 || !S_ISREG(st.st_mode))
|
|
continue;
|
|
apache_scan_file(findings, first, count, path, files, tokens_leak,
|
|
indexes, override_all, weak_protocol,
|
|
signature);
|
|
}
|
|
closedir(d);
|
|
}
|
|
|
|
static void t_apache_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
static const char *dirs[] = { "/etc/apache2/sites-enabled",
|
|
"/etc/apache2/conf-enabled",
|
|
"/etc/httpd/conf.d", NULL };
|
|
const char *root = mcp_arg_string(args, "root");
|
|
char path[1024];
|
|
char value[512];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
int first = 1;
|
|
int count = 0;
|
|
int files = 0;
|
|
int tokens_leak = 0;
|
|
int indexes = 0;
|
|
int override_all = 0;
|
|
int weak_protocol = 0;
|
|
int signature = 0;
|
|
int i;
|
|
char server_tokens[128];
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) > 400) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
server_tokens[0] = '\0';
|
|
mcp_buf_init(&findings);
|
|
snprintf(path, sizeof(path), "%s/etc/apache2/apache2.conf", root);
|
|
if (access(path, R_OK) != 0) {
|
|
snprintf(path, sizeof(path), "%s/etc/httpd/conf/httpd.conf",
|
|
root);
|
|
}
|
|
if (read_text(path, value, sizeof(value)) <= 0) {
|
|
mcp_buf_free(&findings);
|
|
mcp_reply_error(c, "cannot read the Apache configuration");
|
|
return;
|
|
}
|
|
apache_scan_file(&findings, &first, &count, path, &files, &tokens_leak,
|
|
&indexes, &override_all, &weak_protocol, &signature);
|
|
{
|
|
static char text[65536];
|
|
|
|
if (read_text(path, text, sizeof(text)) > 0 &&
|
|
conf_value(text, "ServerTokens", server_tokens,
|
|
sizeof(server_tokens)) == 0)
|
|
server_tokens[0] = '\0';
|
|
}
|
|
for (i = 0; dirs[i] != NULL; i++) {
|
|
snprintf(path, sizeof(path), "%.400s%s", root, dirs[i]);
|
|
apache_scan_dir(&findings, &first, &count, path, &files,
|
|
&tokens_leak, &indexes, &override_all,
|
|
&weak_protocol, &signature);
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, root);
|
|
mcp_buf_printf(&b, ",\"files\":%d,\"server_tokens\":", files);
|
|
apex_json_str(&b, server_tokens);
|
|
mcp_buf_printf(&b, ",\"options_indexes\":%s,\"allowoverride_all\":%s,"
|
|
"\"findings_count\":%d,\"findings\":[",
|
|
indexes ? "true" : "false",
|
|
override_all ? "true" : "false", count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* -------------------------------------------------------------- haproxy_audit */
|
|
|
|
static void t_haproxy_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
static char config[65536];
|
|
char path[1024];
|
|
char *line;
|
|
char *save = NULL;
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
int first = 1;
|
|
int count = 0;
|
|
int stats_socket = 0;
|
|
int socket_mode_600 = 0;
|
|
int stats_enable = 0;
|
|
int stats_auth = 0;
|
|
int tls_plain = 0;
|
|
struct stat st;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) > 400) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
snprintf(path, sizeof(path), "%s/etc/haproxy/haproxy.cfg", root);
|
|
if (read_text(path, config, sizeof(config)) <= 0) {
|
|
mcp_reply_error(c, "cannot read haproxy.cfg");
|
|
return;
|
|
}
|
|
mcp_buf_init(&findings);
|
|
if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) {
|
|
add_finding(&findings, &first, "world-writable", "high",
|
|
"haproxy configuration is writable by anyone");
|
|
count++;
|
|
}
|
|
line = strtok_r(config, "\n", &save);
|
|
while (line != NULL) {
|
|
while (*line == ' ' || *line == '\t')
|
|
line++;
|
|
if (strncmp(line, "stats socket", 12) == 0) {
|
|
stats_socket = 1;
|
|
if (strstr(line, "mode 600") != NULL)
|
|
socket_mode_600 = 1;
|
|
}
|
|
if (strncmp(line, "stats enable", 12) == 0 ||
|
|
strncmp(line, "stats uri", 9) == 0)
|
|
stats_enable = 1;
|
|
if (strncmp(line, "stats auth", 10) == 0) {
|
|
char *v = line + 10;
|
|
|
|
while (*v == ' ' || *v == '\t')
|
|
v++;
|
|
if (*v != '\0')
|
|
stats_auth = 1;
|
|
}
|
|
if (strncmp(line, "bind", 4) == 0 &&
|
|
strstr(line, ":443") != NULL && strstr(line, "ssl") == NULL)
|
|
tls_plain = 1;
|
|
line = strtok_r(NULL, "\n", &save);
|
|
}
|
|
if (stats_socket && !socket_mode_600) {
|
|
add_finding(&findings, &first, "stats-socket-mode", "medium",
|
|
"the stats socket has no mode 600");
|
|
count++;
|
|
}
|
|
if (stats_enable && !stats_auth) {
|
|
add_finding(&findings, &first, "stats-no-auth", "high",
|
|
"the stats page has no authentication");
|
|
count++;
|
|
}
|
|
if (tls_plain) {
|
|
add_finding(&findings, &first, "bind-443-no-ssl", "medium",
|
|
"a bind on port 443 has no ssl keyword");
|
|
count++;
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, root);
|
|
mcp_buf_printf(&b, ",\"stats_socket\":%s,\"stats_auth\":%s,"
|
|
"\"stats_enabled\":%s,\"findings_count\":%d,"
|
|
"\"findings\":[",
|
|
stats_socket ? "true" : "false",
|
|
stats_auth ? "true" : "false",
|
|
stats_enable ? "true" : "false", count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* ------------------------------------------------------------- php_config_audit */
|
|
|
|
static int php_find_ini(const char *root, char *out, size_t n)
|
|
{
|
|
static const char *fixed[] = { "/etc/php.ini", "/etc/php/php.ini",
|
|
NULL };
|
|
char dir[1024];
|
|
int i;
|
|
|
|
for (i = 0; fixed[i] != NULL; i++) {
|
|
snprintf(out, n, "%.400s%s", root, fixed[i]);
|
|
if (access(out, R_OK) == 0)
|
|
return 1;
|
|
}
|
|
snprintf(dir, sizeof(dir), "%.400s/etc/php", root);
|
|
{
|
|
DIR *d = opendir(dir);
|
|
struct dirent *ver;
|
|
|
|
if (d == NULL)
|
|
return 0;
|
|
while ((ver = readdir(d)) != NULL) {
|
|
char vdir[1024];
|
|
DIR *vd;
|
|
struct dirent *sapi;
|
|
|
|
if (ver->d_name[0] == '.')
|
|
continue;
|
|
snprintf(vdir, sizeof(vdir), "%.800s/%.160s", dir,
|
|
ver->d_name);
|
|
vd = opendir(vdir);
|
|
if (vd == NULL)
|
|
continue;
|
|
while ((sapi = readdir(vd)) != NULL) {
|
|
char cand[1024];
|
|
|
|
if (sapi->d_name[0] == '.')
|
|
continue;
|
|
snprintf(cand, sizeof(cand), "%.800s/%.160s/php.ini",
|
|
vdir, sapi->d_name);
|
|
if (access(cand, R_OK) == 0) {
|
|
snprintf(out, n, "%s", cand);
|
|
closedir(vd);
|
|
closedir(d);
|
|
return 1;
|
|
}
|
|
}
|
|
closedir(vd);
|
|
}
|
|
closedir(d);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void t_php_config_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
static char config[65536];
|
|
char path[1024];
|
|
char value[512];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
int first = 1;
|
|
int count = 0;
|
|
int expose = 0;
|
|
int display = 0;
|
|
struct stat st;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) > 400) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
if (!php_find_ini(root, path, sizeof(path))) {
|
|
mcp_reply_error(c, "cannot read a php.ini");
|
|
return;
|
|
}
|
|
if (read_text(path, config, sizeof(config)) <= 0) {
|
|
mcp_reply_error(c, "cannot read a php.ini");
|
|
return;
|
|
}
|
|
mcp_buf_init(&findings);
|
|
if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) {
|
|
add_finding(&findings, &first, "world-writable", "high",
|
|
"php.ini is writable by anyone");
|
|
count++;
|
|
}
|
|
if (conf_value(config, "expose_php", value, sizeof(value)))
|
|
expose = strcasecmp(value, "On") == 0;
|
|
else
|
|
expose = 1;
|
|
if (expose) {
|
|
add_finding(&findings, &first, "expose-php", "low",
|
|
"PHP discloses its version in HTTP headers");
|
|
count++;
|
|
}
|
|
if (conf_value(config, "display_errors", value, sizeof(value)) &&
|
|
strcasecmp(value, "On") == 0) {
|
|
add_finding(&findings, &first, "display-errors", "medium",
|
|
"errors are printed to visitors");
|
|
display = 1;
|
|
count++;
|
|
}
|
|
if (conf_value(config, "allow_url_include", value, sizeof(value)) &&
|
|
strcasecmp(value, "On") == 0) {
|
|
add_finding(&findings, &first, "allow-url-include", "high",
|
|
"remote URLs may be included as code");
|
|
count++;
|
|
}
|
|
if (conf_value(config, "allow_url_fopen", value, sizeof(value)) &&
|
|
strcasecmp(value, "On") == 0) {
|
|
add_finding(&findings, &first, "allow-url-fopen", "low",
|
|
"remote URLs may be opened as files");
|
|
count++;
|
|
}
|
|
if (conf_value(config, "disable_functions", value, sizeof(value))) {
|
|
if (value[0] == '\0') {
|
|
add_finding(&findings, &first, "no-disable-functions",
|
|
"medium",
|
|
"no functions are disabled");
|
|
count++;
|
|
}
|
|
} else {
|
|
add_finding(&findings, &first, "no-disable-functions", "medium",
|
|
"disable_functions is not set");
|
|
count++;
|
|
}
|
|
if (conf_value(config, "session.cookie_secure", value,
|
|
sizeof(value)) &&
|
|
strcasecmp(value, "Off") == 0) {
|
|
add_finding(&findings, &first, "insecure-cookie", "low",
|
|
"session cookies may travel over plain HTTP");
|
|
count++;
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, root);
|
|
mcp_buf_puts(&b, ",\"path\":");
|
|
apex_json_str(&b, path);
|
|
mcp_buf_printf(&b, ",\"expose_php\":%s,\"display_errors\":%s,"
|
|
"\"findings_count\":%d,\"findings\":[",
|
|
expose ? "true" : "false", display ? "true" : "false",
|
|
count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* -------------------------------------------------------------- dovecot_audit */
|
|
|
|
static void dovecot_scan_file(mcp_buf *findings, int *first, int *count,
|
|
const char *path, int *files, int *ssl_off,
|
|
int *plaintext_ok, int *plain_mech)
|
|
{
|
|
static char text[65536];
|
|
char value[512];
|
|
struct stat st;
|
|
|
|
if (read_text(path, text, sizeof(text)) < 0)
|
|
return;
|
|
(*files)++;
|
|
if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) {
|
|
add_finding(findings, first, "world-writable", "high",
|
|
"%s is writable by anyone", path);
|
|
(*count)++;
|
|
}
|
|
if (conf_value(text, "ssl", value, sizeof(value)) &&
|
|
strcasecmp(value, "no") == 0) {
|
|
if (!*ssl_off)
|
|
add_finding(findings, first, "ssl-off", "high",
|
|
"%s disables TLS", path);
|
|
*ssl_off = 1;
|
|
(*count)++;
|
|
}
|
|
if (conf_value(text, "disable_plaintext_auth", value, sizeof(value)) &&
|
|
strcasecmp(value, "no") == 0) {
|
|
if (!*plaintext_ok)
|
|
add_finding(findings, first, "plaintext-auth", "high",
|
|
"%s allows plaintext authentication", path);
|
|
*plaintext_ok = 1;
|
|
(*count)++;
|
|
}
|
|
if (conf_value(text, "auth_mechanisms", value, sizeof(value))) {
|
|
if (conf_has_word(value, "plain") ||
|
|
conf_has_word(value, "PLAIN")) {
|
|
if (!*plain_mech)
|
|
add_finding(findings, first, "plain-mechanism",
|
|
"medium",
|
|
"%s offers PLAIN authentication", path);
|
|
*plain_mech = 1;
|
|
(*count)++;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void dovecot_scan_dir(mcp_buf *findings, int *first, int *count,
|
|
const char *dir, int *files, int *ssl_off,
|
|
int *plaintext_ok, int *plain_mech)
|
|
{
|
|
DIR *d = opendir(dir);
|
|
struct dirent *ent;
|
|
|
|
if (d == NULL)
|
|
return;
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char path[1024];
|
|
struct stat st;
|
|
size_t nl = strlen(ent->d_name);
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
if (nl < 6 || strcmp(ent->d_name + nl - 5, ".conf") != 0)
|
|
continue;
|
|
snprintf(path, sizeof(path), "%.800s/%.160s", dir, ent->d_name);
|
|
if (stat(path, &st) != 0 || !S_ISREG(st.st_mode))
|
|
continue;
|
|
dovecot_scan_file(findings, first, count, path, files, ssl_off,
|
|
plaintext_ok, plain_mech);
|
|
}
|
|
closedir(d);
|
|
}
|
|
|
|
static void t_dovecot_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
char path[1024];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
int first = 1;
|
|
int count = 0;
|
|
int files = 0;
|
|
int ssl_off = 0;
|
|
int plaintext_ok = 0;
|
|
int plain_mech = 0;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) > 400) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
mcp_buf_init(&findings);
|
|
snprintf(path, sizeof(path), "%s/etc/dovecot/dovecot.conf", root);
|
|
dovecot_scan_file(&findings, &first, &count, path, &files, &ssl_off,
|
|
&plaintext_ok, &plain_mech);
|
|
snprintf(path, sizeof(path), "%s/etc/dovecot/conf.d", root);
|
|
dovecot_scan_dir(&findings, &first, &count, path, &files, &ssl_off,
|
|
&plaintext_ok, &plain_mech);
|
|
if (files == 0) {
|
|
mcp_buf_free(&findings);
|
|
mcp_reply_error(c, "cannot read the Dovecot configuration");
|
|
return;
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, root);
|
|
mcp_buf_printf(&b, ",\"files\":%d,\"ssl_off\":%s,\"plaintext_auth\":%s,"
|
|
"\"findings_count\":%d,\"findings\":[",
|
|
files, ssl_off ? "true" : "false",
|
|
plaintext_ok ? "true" : "false", count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
/* -------------------------------------------------------------- openvpn_audit */
|
|
|
|
static void openvpn_scan_file(mcp_buf *findings, int *first, int *count,
|
|
const char *path, int *files,
|
|
int *no_tls, int *weak_cipher, int *weak_auth,
|
|
int *dup_cn)
|
|
{
|
|
static char text[65536];
|
|
char value[512];
|
|
struct stat st;
|
|
int has_tls = 0;
|
|
|
|
if (read_text(path, text, sizeof(text)) < 0)
|
|
return;
|
|
(*files)++;
|
|
if (stat(path, &st) == 0 && (st.st_mode & 0002) != 0) {
|
|
add_finding(findings, first, "world-writable", "high",
|
|
"%s is writable by anyone", path);
|
|
(*count)++;
|
|
}
|
|
has_tls = conf_value(text, "tls-auth", value, sizeof(value)) ||
|
|
conf_value(text, "tls-crypt", value, sizeof(value)) ||
|
|
conf_value(text, "tls-crypt-v2", value, sizeof(value));
|
|
if (!has_tls) {
|
|
if (!*no_tls)
|
|
add_finding(findings, first, "no-tls-auth", "medium",
|
|
"%s has no tls-auth/tls-crypt", path);
|
|
*no_tls = 1;
|
|
(*count)++;
|
|
}
|
|
if (conf_value(text, "cipher", value, sizeof(value))) {
|
|
if (strstr(value, "BF-CBC") != NULL ||
|
|
strstr(value, "DES") != NULL ||
|
|
strcasecmp(value, "none") == 0) {
|
|
if (!*weak_cipher)
|
|
add_finding(findings, first, "weak-cipher", "high",
|
|
"%s uses a legacy or no cipher", path);
|
|
*weak_cipher = 1;
|
|
(*count)++;
|
|
}
|
|
}
|
|
if (conf_value(text, "auth", value, sizeof(value))) {
|
|
if (strcasecmp(value, "SHA1") == 0 ||
|
|
strcasecmp(value, "MD5") == 0) {
|
|
if (!*weak_auth)
|
|
add_finding(findings, first, "weak-auth", "medium",
|
|
"%s uses a weak HMAC", path);
|
|
*weak_auth = 1;
|
|
(*count)++;
|
|
}
|
|
}
|
|
if (conf_value(text, "duplicate-cn", value, sizeof(value))) {
|
|
if (!*dup_cn)
|
|
add_finding(findings, first, "duplicate-cn", "medium",
|
|
"%s lets several clients share a certificate",
|
|
path);
|
|
*dup_cn = 1;
|
|
(*count)++;
|
|
}
|
|
}
|
|
|
|
static void openvpn_scan_dir(mcp_buf *findings, int *first, int *count,
|
|
const char *dir, int *files, int *no_tls,
|
|
int *weak_cipher, int *weak_auth, int *dup_cn)
|
|
{
|
|
DIR *d = opendir(dir);
|
|
struct dirent *ent;
|
|
|
|
if (d == NULL)
|
|
return;
|
|
while ((ent = readdir(d)) != NULL) {
|
|
char path[1024];
|
|
struct stat st;
|
|
size_t nl = strlen(ent->d_name);
|
|
|
|
if (ent->d_name[0] == '.')
|
|
continue;
|
|
if (nl < 6 || strcmp(ent->d_name + nl - 5, ".conf") != 0)
|
|
continue;
|
|
snprintf(path, sizeof(path), "%.800s/%.160s", dir, ent->d_name);
|
|
if (stat(path, &st) != 0 || !S_ISREG(st.st_mode))
|
|
continue;
|
|
openvpn_scan_file(findings, first, count, path, files, no_tls,
|
|
weak_cipher, weak_auth, dup_cn);
|
|
}
|
|
closedir(d);
|
|
}
|
|
|
|
static void t_openvpn_audit(mcp_conn *c, const mcp_json *args, void *user)
|
|
{
|
|
const char *root = mcp_arg_string(args, "root");
|
|
char path[1024];
|
|
mcp_buf b;
|
|
mcp_buf findings;
|
|
int first = 1;
|
|
int count = 0;
|
|
int files = 0;
|
|
int no_tls = 0;
|
|
int weak_cipher = 0;
|
|
int weak_auth = 0;
|
|
int dup_cn = 0;
|
|
|
|
(void)user;
|
|
if (root == NULL)
|
|
root = "";
|
|
if (strlen(root) > 400) {
|
|
mcp_reply_error(c, "root path too long");
|
|
return;
|
|
}
|
|
mcp_buf_init(&findings);
|
|
snprintf(path, sizeof(path), "%s/etc/openvpn/server.conf", root);
|
|
openvpn_scan_file(&findings, &first, &count, path, &files, &no_tls,
|
|
&weak_cipher, &weak_auth, &dup_cn);
|
|
snprintf(path, sizeof(path), "%s/etc/openvpn/server", root);
|
|
openvpn_scan_dir(&findings, &first, &count, path, &files, &no_tls,
|
|
&weak_cipher, &weak_auth, &dup_cn);
|
|
if (files == 0) {
|
|
mcp_buf_free(&findings);
|
|
mcp_reply_error(c, "cannot read an OpenVPN server config");
|
|
return;
|
|
}
|
|
mcp_buf_init(&b);
|
|
mcp_buf_puts(&b, "{\"root\":");
|
|
apex_json_str(&b, root);
|
|
mcp_buf_printf(&b, ",\"files\":%d,\"tls_auth\":%s,\"findings_count\":%d,"
|
|
"\"findings\":[", files, no_tls ? "false" : "true", count);
|
|
if (findings.p != NULL)
|
|
mcp_buf_add(&b, findings.p, findings.len);
|
|
mcp_buf_puts(&b, "]}");
|
|
mcp_reply_json(c, b.p);
|
|
mcp_buf_free(&b);
|
|
mcp_buf_free(&findings);
|
|
}
|
|
|
|
const mcp_tool TOOLS_BLUE[] = {
|
|
{ "hardening_audit",
|
|
"Read-only posture audit of the local host: sshd_config (root login, "
|
|
"password auth, empty passwords), sysctl (ip_forward, syncookies, "
|
|
"rp_filter), /etc/shadow mode, world-writable directories in /etc, "
|
|
"listening TCP sockets, firewall rule files, SUID binaries. Returns a "
|
|
"score, findings with severities, and counts. Changes nothing and "
|
|
"touches no network target.",
|
|
"{\"type\":\"object\",\"properties\":{}}",
|
|
t_hardening_audit },
|
|
{ "log_audit",
|
|
"Summarise an auth log: failed passwords per source IP (flagged at five "
|
|
"or more), invalid users targeted, accepted logins, PAM authentication "
|
|
"failures. Read-only; the path must be under /var/log/ or the state "
|
|
"directory.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"default /var/log/auth.log\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"lines to read, default "
|
|
"5000, max 50000\"}}}",
|
|
t_log_audit },
|
|
{ "ioc_scan",
|
|
"Walk a directory (default /etc) for common persistence and remote-code "
|
|
"strings: /dev/tcp reverse shells, bash -i, base64|sh, curl|sh, nc -e, "
|
|
"socat EXEC:, setuid chmod, systemd units under /tmp or /dev/shm, "
|
|
"writes to authorized_keys. Skips binaries and symlinks, caps at 100 "
|
|
"findings, changes nothing.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"absolute directory, "
|
|
"default /etc\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"files to scan, default "
|
|
"2000, max 20000\"}}}",
|
|
t_ioc_scan },
|
|
{ "web_log_audit",
|
|
"Read an nginx/apache access log: request count, 2xx/3xx/4xx/5xx split, "
|
|
"top source IPs, known scanner user agents (sqlmap, nikto, nmap, "
|
|
"gobuster, nuclei, ...), SQLi/traversal/XSS patterns in the URI, and "
|
|
"source IPs with a 4xx/5xx burst. Read-only; the path must be under "
|
|
"/var/log/ or the state directory.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"default "
|
|
"/var/log/nginx/access.log\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"lines to read, "
|
|
"default 5000, max 50000\"}}}",
|
|
t_web_log_audit },
|
|
{ "detect_rules",
|
|
"Turn recorded findings into detection skeletons: one Sigma rule, one "
|
|
"Suricata rule and one YARA rule per finding under "
|
|
"$APEX_HOME/detections/. The rules carry the finding id, title, target "
|
|
"and severity; they are templates and must be tuned and tested before "
|
|
"deployment. Nothing is loaded into a sensor.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\",\"description\":\"only findings for "
|
|
"this target\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"default 100, max "
|
|
"500\"}}}",
|
|
t_detect_rules },
|
|
{ "integrity_baseline",
|
|
"Create or verify a SHA-256 baseline of a directory tree "
|
|
"($APEX_HOME/baseline.jsonl by default). create hashes every regular "
|
|
"file up to 1 MiB (1000 files default); verify re-hashes them and "
|
|
"reports changed, missing and added files. No external tools and no "
|
|
"database.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"action\":{\"type\":\"string\",\"enum\":[\"create\",\"verify\"],"
|
|
"\"description\":\"default verify\"},"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"directory, default "
|
|
"/etc\"},"
|
|
"\"baseline\":{\"type\":\"string\",\"description\":\"default "
|
|
"$APEX_HOME/baseline.jsonl\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"max files, "
|
|
"default 1000\"}}}",
|
|
t_integrity_baseline },
|
|
{ "mitre_map",
|
|
"Map recorded findings to MITRE ATT&CK techniques by keywords in the "
|
|
"title, tool, tags and CWE (SQLi/SSRF -> T1190, XSS -> T1059.007, default "
|
|
"credentials -> T1078, exposed repositories/backups -> T1213, missing "
|
|
"HSTS -> T1557, CORS -> T1185, port scanning -> T1046). Reports the "
|
|
"mapped count, unmapped count and the finding ids per technique.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"target\":{\"type\":\"string\",\"description\":\"only findings for "
|
|
"this target\"}}}",
|
|
t_mitre_map },
|
|
{ "process_snapshot",
|
|
"Read-only snapshot of /proc: for every process report name, exe and open "
|
|
"fd count, and flag executables under /tmp, /dev/shm or /var/tmp, deleted "
|
|
"executables, reverse-shell style command lines (nc -e, /dev/tcp, "
|
|
"base64 -d, socat, bash -i) and more than 256 open files.",
|
|
"{\"type\":\"object\",\"properties\":{}}",
|
|
t_process_snapshot },
|
|
{ "persistence_audit",
|
|
"Read cron, systemd and at persistence on the local host: /etc/crontab, "
|
|
"/etc/cron.d, /var/spool/cron/crontabs, /var/spool/cron/atjobs and "
|
|
"/etc/systemd/system (services and timers). Flags entries that run from "
|
|
"/tmp, /dev/shm or /var/tmp, pipe curl/wget into a shell, decode base64 "
|
|
"into a shell, use nc -e/socat, bash -i, or setuid chmod. Read-only; the "
|
|
"optional root prefix lets you audit a mounted image.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix for the paths, "
|
|
"default empty (the live host)\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"files to read, "
|
|
"default 2000\"}}}",
|
|
t_persistence_audit },
|
|
{ "net_baseline",
|
|
"Create or verify a baseline of listening TCP ports and the process that "
|
|
"owns each socket (from /proc/net/tcp{,6} and /proc/<pid>/fd). Verify "
|
|
"reports new ports, ports that disappeared and ports whose process "
|
|
"changed, which catches a rogue service between two points in time. The "
|
|
"default baseline is $APEX_HOME/net-baseline.jsonl. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"action\":{\"type\":\"string\",\"enum\":[\"create\",\"verify\"],"
|
|
"\"description\":\"default verify\"},"
|
|
"\"baseline\":{\"type\":\"string\",\"description\":\"default "
|
|
"$APEX_HOME/net-baseline.jsonl\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"max ports, default "
|
|
"500\"}}}",
|
|
t_net_baseline },
|
|
{ "ssh_key_audit",
|
|
"Audit SSH key material on the local host (optional root prefix for a "
|
|
"mounted image): authorized_keys under /etc/ssh, /root/.ssh, "
|
|
"/home/*/.ssh and the state dir, plus private keys (id_* and "
|
|
"/etc/ssh/ssh_host_*_key). Flags unrestricted keys (no from=/command=/"
|
|
"restrict), ssh-rsa and ssh-dss key types, duplicate keys and private "
|
|
"keys readable beyond their owner. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"key files to read, "
|
|
"default 200\"}}}",
|
|
t_ssh_key_audit },
|
|
{ "suid_baseline",
|
|
"Create or verify a SHA-256 baseline of SUID/SGID files under /usr/bin, "
|
|
"/usr/sbin, /usr/local/bin, /usr/local/sbin, /bin and /sbin (symlinked "
|
|
"directories are skipped). Verify reports new setuid files, vanished "
|
|
"ones and files whose mode or hash changed, which catches a planted "
|
|
"setuid binary. The default baseline is $APEX_HOME/suid-baseline.jsonl; "
|
|
"the optional root prefix audits a mounted image. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"action\":{\"type\":\"string\",\"enum\":[\"create\",\"verify\"],"
|
|
"\"description\":\"default verify\"},"
|
|
"\"baseline\":{\"type\":\"string\",\"description\":\"default "
|
|
"$APEX_HOME/suid-baseline.jsonl\"},"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"max entries, default "
|
|
"2000\"}}}",
|
|
t_suid_baseline },
|
|
{ "cap_baseline",
|
|
"Create or verify a baseline of Linux file capabilities (the "
|
|
"security.capability xattr) under /usr/bin, /usr/sbin, /usr/local/bin, "
|
|
"/usr/local/sbin, /bin and /sbin. Verify reports new capability-bearing "
|
|
"files, vanished ones and changed capability sets, which catches a "
|
|
"binary granted CAP_SETUID or CAP_NET_RAW. The default baseline is "
|
|
"$APEX_HOME/cap-baseline.jsonl; the optional root prefix audits a "
|
|
"mounted image. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"action\":{\"type\":\"string\",\"enum\":[\"create\",\"verify\"],"
|
|
"\"description\":\"default verify\"},"
|
|
"\"baseline\":{\"type\":\"string\",\"description\":\"default "
|
|
"$APEX_HOME/cap-baseline.jsonl\"},"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"max entries, default "
|
|
"2000\"}}}",
|
|
t_cap_baseline },
|
|
{ "module_audit",
|
|
"Read the loaded kernel modules (/proc/modules), the per-module taint "
|
|
"letters (/sys/module/<name>/taint) and the kernel taint value "
|
|
"(/proc/sys/kernel/tainted), decode the taint bits (proprietary, forced, "
|
|
"staging, out-of-tree, unsigned, ...) and list every tainted module. The "
|
|
"three paths can be overridden to audit a mounted image. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"modules\":{\"type\":\"string\",\"description\":\"default "
|
|
"/proc/modules\"},"
|
|
"\"sysfs\":{\"type\":\"string\",\"description\":\"default "
|
|
"/sys/module\"},"
|
|
"\"tainted\":{\"type\":\"string\",\"description\":\"default "
|
|
"/proc/sys/kernel/tainted\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"max modules, default "
|
|
"500\"}}}",
|
|
t_module_audit },
|
|
{ "package_integrity",
|
|
"Verify installed package files against the package database: dpkg -V "
|
|
"(optionally for one package) or rpm -Va/-V. Reports each changed file "
|
|
"with its status flags. An optional output file parses a verify run "
|
|
"captured earlier from a mounted image; no network. Local process only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"manager\":{\"type\":\"string\",\"enum\":[\"auto\",\"dpkg\",\"rpm\"],"
|
|
"\"description\":\"default auto\"},"
|
|
"\"package\":{\"type\":\"string\",\"description\":\"verify one package "
|
|
"instead of all\"},"
|
|
"\"output\":{\"type\":\"string\",\"description\":\"parse a saved verify "
|
|
"output instead of running\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"files to return, "
|
|
"default 200\"}}}",
|
|
t_package_integrity },
|
|
{ "mount_audit",
|
|
"Parse a mount table (/proc/mounts by default, or a saved copy from a "
|
|
"mounted image) and flag filesystems that should be hardened: /tmp, "
|
|
"/var/tmp and /dev/shm without nosuid, nodev or noexec; /home, /run/user, "
|
|
"/media and /mnt without nosuid or nodev. Reports each mount with its "
|
|
"missing options and a severity. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"default "
|
|
"/proc/mounts\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"mounts to read, "
|
|
"default 500\"}}}",
|
|
t_mount_audit },
|
|
{ "firewall_audit",
|
|
"Read the local firewall ruleset with nft list ruleset or iptables-save "
|
|
"(or parse a saved dump) and report the chain count, rule count and every "
|
|
"chain whose default policy is ACCEPT, which is where a missing rule "
|
|
"becomes a hole. Local process only; no network.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"backend\":{\"type\":\"string\",\"enum\":[\"auto\",\"nft\","
|
|
"\"iptables\"],\"description\":\"default auto\"},"
|
|
"\"output\":{\"type\":\"string\",\"description\":\"parse a saved "
|
|
"ruleset instead of running\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"reserved, default "
|
|
"500\"}}}",
|
|
t_firewall_audit },
|
|
{ "systemd_audit",
|
|
"Audit systemd units under /etc/systemd/system and /usr/lib/systemd/system "
|
|
"(optional root prefix for a mounted image): ExecStart pointing into "
|
|
"/tmp, /dev/shm or /var/tmp, service units without User= (running as "
|
|
"root), world-writable unit files, and the number of .wants/.requires "
|
|
"links. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"max units, default "
|
|
"500\"}}}",
|
|
t_systemd_audit },
|
|
{ "kernel_hardening",
|
|
"Read the kernel hardening sysctls (ASLR, kptr_restrict, dmesg_restrict, "
|
|
"perf_event_paranoid, unprivileged_bpf_disabled, ptrace_scope, "
|
|
"protected_hardlinks/symlinks, suid_dumpable, modules_disabled, "
|
|
"bpf_jit_harden) and core_pattern, score them and report every weak "
|
|
"value. The optional root prefix audits a mounted image. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"checks, default 64\"}}}",
|
|
t_kernel_hardening },
|
|
{ "sudoers_audit",
|
|
"Parse /etc/sudoers and /etc/sudoers.d (optional root prefix) and flag "
|
|
"NOPASSWD, !authenticate, broad ALL=(ALL) grants, direct user rules and "
|
|
"world-writable sudoers files, with file and line. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_sudoers_audit },
|
|
{ "auditd_audit",
|
|
"Summarise an auditd log: lines read, event type counts, AVC denials, "
|
|
"failed USER_AUTH/USER_LOGIN/USER_ACCT events, the most frequent exe "
|
|
"paths and audit keys. Read-only; the path must be under /var/log/ or the "
|
|
"state directory.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"default "
|
|
"/var/log/audit/audit.log\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"lines, default "
|
|
"5000\"}}}",
|
|
t_auditd_audit },
|
|
{ "ssh_config_audit",
|
|
"Score an sshd_config (optional root prefix): PermitRootLogin, "
|
|
"PasswordAuthentication, PermitEmptyPasswords, X11Forwarding, "
|
|
"GatewayPorts, PermitTunnel, MaxAuthTries and legacy Ciphers/MACs/"
|
|
"KexAlgorithms, with a finding per weak value. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_ssh_config_audit },
|
|
{ "pam_audit",
|
|
"Scan /etc/pam.d (optional root prefix) for pam_permit, pam_unix nullok "
|
|
"and pam_exec, and report whether pam_faillock or pam_tally2 provides "
|
|
"lockout. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_pam_audit },
|
|
{ "log_timeline",
|
|
"Merge an auth log and an access log into one chronology: both timestamps "
|
|
"are normalised to YYYYMMDDHHMMSS (auth lines get the current year), the "
|
|
"combined list is sorted and the first N entries are returned with their "
|
|
"source. Paths are limited to /var/log and the state dir. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"auth\":{\"type\":\"string\",\"description\":\"default "
|
|
"/var/log/auth.log\"},"
|
|
"\"access\":{\"type\":\"string\",\"description\":\"default "
|
|
"/var/log/nginx/access.log\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"entries, default "
|
|
"200\"}}}",
|
|
t_log_timeline },
|
|
{ "docker_audit",
|
|
"Audit the local Docker setup (optional root prefix): the docker.sock "
|
|
"mode (world-writable is critical), members of the docker group in "
|
|
"/etc/group, and daemon.json (TCP hosts, TLS, insecure registries). No "
|
|
"daemon call and no container access; read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_docker_audit },
|
|
{ "www_audit",
|
|
"Walk the usual web roots (/var/www, /srv/www, /usr/share/nginx/html; "
|
|
"optional root prefix for a mounted image) and flag world-writable "
|
|
"content, .env/wp-config/config.php/.htpasswd/id_rsa, backup and dump "
|
|
"files, .git directories and directories without an index file. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"files, default "
|
|
"2000\"}}}",
|
|
t_www_audit },
|
|
{ "apparmor_audit",
|
|
"Read the AppArmor profile list (root prefix supported), count enforce "
|
|
"and complain profiles and name the profiles that only log. Flags an "
|
|
"AppArmor without any active profile. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_apparmor_audit },
|
|
{ "selinux_audit",
|
|
"Read the SELinux state (root prefix supported): /sys/fs/selinux/enforce, "
|
|
"/etc/selinux/config (SELINUX=, SELINUXTYPE=) and the boolean count. "
|
|
"Flags disabled, permissive and a host without SELinux. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_selinux_audit },
|
|
{ "audit_rules",
|
|
"Parse /etc/audit/audit.rules and /etc/audit/rules.d/*.rules (root prefix "
|
|
"supported): rule count, -e 2 immutability, an execve rule and watches on "
|
|
"/etc/passwd, /etc/shadow, /etc/sudoers and /etc/group. Reports missing "
|
|
"watches, a missing execve rule and unlocked rules. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_audit_rules },
|
|
{ "usb_audit",
|
|
"Read /sys/bus/usb/devices (root prefix supported): vendor/product IDs, "
|
|
"manufacturer, product, serial and whether an interface reports class 08 "
|
|
"(mass storage). Storage devices are flagged; no device is opened or "
|
|
"mounted. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"devices, default "
|
|
"200\"}}}",
|
|
t_usb_audit },
|
|
{ "ssh_known_hosts_audit",
|
|
"Audit known_hosts files (/etc/ssh/ssh_known_hosts, root and user .ssh, "
|
|
"optional root prefix): count entries, hashed vs plain hosts, ssh-rsa/"
|
|
"ssh-dss key types and duplicate host keys. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_ssh_known_hosts_audit },
|
|
{ "logrotate_audit",
|
|
"Parse /etc/logrotate.conf and /etc/logrotate.d (root prefix): "
|
|
"world-writable configs, rotate counts of two or fewer, missing compress "
|
|
"and postrotate commands that run from /tmp or download code. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_logrotate_audit },
|
|
{ "nfs_exports_audit",
|
|
"Parse /etc/exports and /etc/exports.d (root prefix): count exports and "
|
|
"flag world exports (*), writable exports without ro and exports with "
|
|
"no_root_squash, with file and line. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_nfs_exports_audit },
|
|
{ "samba_audit",
|
|
"Parse smb.conf (root prefix) and report shares that expose /, allow guest "
|
|
"access, are writable or allow guests without valid users. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_samba_audit },
|
|
{ "snmpd_audit",
|
|
"Parse snmpd.conf (root prefix): rocommunity/rwcommunity/community/"
|
|
"com2sec, listing each community with its mode and flagging rw "
|
|
"communities, public/private/default strings and com2sec sources set to "
|
|
"default. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_snmpd_audit },
|
|
{ "rsyslog_audit",
|
|
"Parse rsyslog.conf and rsyslog.d (root prefix): remote targets (@@ TCP, "
|
|
"@ UDP), world-writable configuration files and whether a dedicated "
|
|
"auth/authpriv rule exists. Flags missing auth logging and missing remote "
|
|
"forwarding. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_rsyslog_audit },
|
|
{ "postfix_audit",
|
|
"Parse /etc/postfix/main.cf (root prefix): mynetworks covering every "
|
|
"address, relay restrictions without reject_unauth_destination, optional "
|
|
"TLS and world-writable configuration. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_postfix_audit },
|
|
{ "nginx_audit",
|
|
"Parse /etc/nginx/nginx.conf, conf.d and sites-enabled (root prefix): "
|
|
"server_tokens, autoindex, TLSv1/1.1 in ssl_protocols, legacy ciphers and "
|
|
"world-writable files. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_nginx_audit },
|
|
{ "redis_config_audit",
|
|
"Parse /etc/redis/redis.conf (root prefix): missing requirepass, "
|
|
"protected-mode no, bind on every address, enabled debug command and a "
|
|
"data directory in /tmp or /dev/shm, plus world-writable configs. "
|
|
"Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_redis_config_audit },
|
|
{ "mysql_config_audit",
|
|
"Parse /etc/mysql/my.cnf and my.cnf.d/conf.d (root prefix): bind-address "
|
|
"on every interface, skip-grant-tables, local-infile, an empty "
|
|
"secure-file-priv and world-writable files. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_mysql_config_audit },
|
|
{ "apache_audit",
|
|
"Parse apache2.conf/httpd.conf, sites-enabled and conf-enabled (root "
|
|
"prefix): ServerTokens Full/OS, Options Indexes, AllowOverride All, "
|
|
"SSLv3/TLSv1 in SSLProtocol, ServerSignature On and world-writable "
|
|
"files. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_apache_audit },
|
|
{ "haproxy_audit",
|
|
"Parse haproxy.cfg (root prefix): stats socket without mode 600, a stats "
|
|
"page without stats auth, a bind on port 443 without ssl and a "
|
|
"world-writable config. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_haproxy_audit },
|
|
{ "php_config_audit",
|
|
"Read a php.ini (root prefix; /etc/php.ini or /etc/php/*/*/php.ini): "
|
|
"expose_php, display_errors, allow_url_include/fopen, disable_functions, "
|
|
"session.cookie_secure and world-writable files. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_php_config_audit },
|
|
{ "dovecot_audit",
|
|
"Parse dovecot.conf and conf.d (root prefix): ssl = no, "
|
|
"disable_plaintext_auth = no, PLAIN in auth_mechanisms and "
|
|
"world-writable files. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_dovecot_audit },
|
|
{ "openvpn_audit",
|
|
"Parse server.conf and server/*.conf (root prefix): missing "
|
|
"tls-auth/tls-crypt, BF-CBC/DES/none cipher, SHA1/MD5 HMAC, duplicate-cn "
|
|
"and world-writable files. Read-only.",
|
|
"{\"type\":\"object\",\"properties\":{"
|
|
"\"root\":{\"type\":\"string\",\"description\":\"prefix, default empty\"}}}",
|
|
t_openvpn_audit },
|
|
};
|
|
|
|
const size_t TOOLS_BLUE_N = sizeof(TOOLS_BLUE) / sizeof(TOOLS_BLUE[0]);
|