Files
apex-twin/src/apex.h
T
lotherk 539b552c3d apex-twin 1.6.0: TLS, Redis, SNMP and DNS zone-transfer checks
Red, deep service checks (tools_proto.c):
- tls_audit: four openssl s_client probes (TLS 1.0/1.1/1.2/1.3) plus a
  -showcerts run, then openssl x509 for subject, issuer, validity and DNS SANs.
  Flags TLS 1.0/1.1 and RC4/3DES/NULL/EXPORT/MD5 ciphers. A failed handshake
  also prints a session block, so success is keyed on 'New, ..., Cipher is ...'
  and (NONE) counts as failure.
- redis_probe: PING on one connection; when it answers without AUTH, INFO and
  DBSIZE follow. Reports open access (audited), version, role, connected
  clients, OS and database size. No password guessing.
- snmp_probe: hand-built SNMPv1/v2c GET sysDescr.0 over UDP, two attempts per
  community (default public/private, max 4), raw answer saved as evidence. No
  net-snmp dependency.
- dns_zone_transfer: dig AXFR against one in-scope server for one in-scope
  zone; record counts per type, up to 200 raw records, full output as evidence,
  and explicit refused/unreachable reasons.

Shared gated_run(): gate, audit and run with the output returned to the caller,
so multi-command tools keep the gate invariant.

Tests: TLS server with a generated certificate, open and AUTH-required Redis
servers, a UDP SNMP agent answering only 'public', an AXFR server with
SOA/NS/A records plus a refusing variant, and the out-of-scope paths. 522
checks, ASan suite green, analyze clean (an analyzer uninitialised-field report
was removed by initialising the token array), release-check green including
distcheck 1.6.0.
2026-11-21 12:23:56 +00:00

242 lines
8.0 KiB
C

/*
* APEX TWIN - C99/POSIX offensive-security MCP server.
*
* One process, one dispatch table, one scope gate in front of every action.
* The gate is the product: a tool that cannot name a target inside the loaded
* scope does not run, and every decision is appended to the audit log before
* the command is spawned.
*/
#ifndef APEX_H
#define APEX_H
#include <stddef.h>
#include <sys/types.h>
#include <time.h>
#include "mcp.h"
#define APEX_VERSION "1.6.0"
#define APEX_MAX_OUT (1u << 20)
#define APEX_PATH_MAX 512
#define APEX_PATH_FULL 1024
#define APEX_MAX_CIDR 64
#define APEX_MAX_DOMAIN 64
#define APEX_MAX_PORTS 32
/* ------------------------------------------------------------------ scope */
typedef struct {
int family; /* AF_INET or AF_INET6 */
unsigned char addr[16];
int bits;
} apex_cidr;
typedef struct {
char suffix[256]; /* "example.com", stored lower case, no dot prefix */
int sub_only; /* "*.example.com": the apex itself is not matched */
} apex_domain;
typedef struct {
int lo;
int hi;
} apex_portrange;
typedef struct {
int loaded;
int allow_private; /* RFC1918 / loopback / link-local permitted */
int serial_tx; /* transmitting radio commands armed */
time_t expires; /* 0 = no expiry */
char source[APEX_PATH_MAX];
char label[128];
apex_cidr allow_ip[APEX_MAX_CIDR];
int n_allow_ip;
apex_cidr deny_ip[APEX_MAX_CIDR];
int n_deny_ip;
apex_domain allow_domain[APEX_MAX_DOMAIN];
int n_allow_domain;
apex_portrange allow_port[APEX_MAX_PORTS];
int n_allow_port;
} apex_scope;
typedef struct {
char home[APEX_PATH_MAX];
char scope_file[APEX_PATH_MAX];
char audit_file[APEX_PATH_MAX];
char finding_dir[APEX_PATH_MAX];
char evidence_dir[APEX_PATH_MAX];
char report_dir[APEX_PATH_MAX];
char asset_dir[APEX_PATH_MAX];
char traffic_dir[APEX_PATH_MAX];
char webhook[1024];
char webhook_min[16];
char rewrite_file[APEX_PATH_MAX];
int proxy_max_children;
char serial[128];
int baud;
int default_timeout_ms;
int max_timeout_ms;
int audit_max_bytes;
int http_max_children;
int http_head_timeout_ms;
int evidence_max_bytes;
size_t max_output;
int dry_run;
} apex_cfg;
extern apex_cfg g_cfg;
extern apex_scope g_scope;
int apex_mkdir_p(const char *path, mode_t mode);
/* dir + "/" + name into dst. -1 when it does not fit, never truncated. */
int apex_path_join(char *dst, size_t n, const char *dir, const char *name);
int apex_write_file(const char *path, const char *data, size_t len, mode_t mode);
void apex_config_init(void);
int apex_scope_load(void);
/* 1 = allowed, 0 = denied. reason is always filled. */
int apex_scope_check(const char *target, int port, char *reason, size_t n);
void apex_scope_json(mcp_buf *b);
/* --------------------------------------------------------------- process */
typedef struct {
int exit_code; /* -1 spawn failed, -2 killed by signal */
int signal_num;
int timed_out;
int truncated;
long elapsed_ms;
char *out;
size_t out_len;
} apex_proc;
int apex_run(char *const argv[], int timeout_ms, size_t max_out, apex_proc *p);
void apex_proc_free(apex_proc *p);
int apex_have(const char *prog);
char *apex_join_argv(char *const argv[]);
char *apex_strdup(const char *s);
/* SHA-256 in lowercase hex (64 characters plus NUL), raw, and HMAC-SHA256. */
void apex_sha256(const void *data, size_t len, char hex[65]);
void apex_sha256_raw(const void *data, size_t len, unsigned char out[32]);
void apex_hmac_sha256(const void *key, size_t klen, const void *msg,
size_t mlen, unsigned char out[32]);
/* ----------------------------------------------------------------- store */
/* One finding as it sits on disk. Long fields are truncated, never overflowed;
* a report is rendered from these, not from parsing JSON a second time. */
typedef struct {
char id[16];
char ts[40];
char updated[40]; /* empty until the first update */
char target[256];
char title[512];
char severity[16];
char confidence[16];
char status[24]; /* open | confirmed | false-positive | fixed */
char tool[64];
char cwe[32];
char remediation[1024];
char tags[256];
char evidence[512];
char note[1024]; /* why the status changed, latest only */
} apex_finding;
void apex_audit(const char *tool, const char *target, int port, int allowed,
const char *reason, const char *cmd);
char *apex_finding_add(const char *target, const char *title, const char *severity,
const char *confidence, const char *tool, const char *evidence,
const char *cwe, const char *remediation, const char *tags);
/*
* Change one finding in place. NULL or "" leaves a field alone; status and
* severity are validated. Returns 0 on success, -1 when the finding does not
* exist, -2 when a value is not accepted.
*/
int apex_finding_update(const char *id, const char *status, const char *severity,
const char *confidence, const char *remediation,
const char *tags, const char *note);
int apex_finding_status_valid(const char *status);
int apex_finding_scan(apex_finding *out, int max);
/* Find a finding by target and title, both trimmed and case-insensitive.
* Returns a malloc'd id or NULL. */
char *apex_finding_find(const char *target, const char *title);
/* Load one finding by id. 0 = found, -1 = not found. */
int apex_finding_get(const char *id, apex_finding *out);
int apex_finding_list(mcp_buf *b, const char *target_filter, int limit);
int apex_finding_read(const char *id, mcp_buf *b);
/* ---------------------------------------------------------------- assets */
/*
* One host, domain or IP as it was seen, with the services observed on it.
* Assets are created through the gate (the handler checks the target first) and
* merged by target: a second observation updates the record instead of making
* a second one.
*/
typedef struct {
char id[16];
char ts[40];
char updated[40];
char target[256];
char label[256];
char kind[16]; /* host | ip | domain */
char ports[512]; /* comma separated, unioned on merge */
char services[1024]; /* latest observation */
char tags[256];
char note[1024];
} apex_asset;
char *apex_asset_add(const char *target, const char *label, const char *kind,
const char *ports, const char *services, const char *tags,
const char *note);
int apex_asset_list(mcp_buf *b, const char *target_filter, int limit);
int apex_asset_read(const char *id, mcp_buf *b);
int apex_asset_update(const char *id, const char *label, const char *ports,
const char *services, const char *tags, const char *note);
int apex_asset_scan(apex_asset *out, int max);
/* --------------------------------------------------------------- traffic */
/*
* One request/response pair as it happened, with the bytes on disk. This is the
* traffic store without the proxy: a capture is a proof, not a memory.
*/
typedef struct {
char id[16];
char ts[40];
char target[256];
int port;
int tls;
char method[16];
char path[512];
int status;
long request_bytes;
long response_bytes;
char raw[APEX_PATH_FULL];
char note[256];
} apex_traffic;
char *apex_traffic_add(const char *target, int port, int tls, const char *method,
const char *path, int status, const char *request,
size_t request_len, const char *response,
size_t response_len, const char *note);
int apex_traffic_list(mcp_buf *b, const char *target_filter, int limit);
/* metadata plus the raw pair, as text or base64 */
int apex_traffic_read(const char *id, mcp_buf *b);
char *apex_evidence_save(const char *label, const char *data, size_t len);
/* newest first; one {"name","bytes","mtime"} per evidence blob */
int apex_evidence_list(mcp_buf *b, int limit);
/* read one blob back: {"name","bytes","encoding":"text"|"base64","data"} */
int apex_evidence_read(const char *name, mcp_buf *b);
int apex_audit_tail(mcp_buf *b, int limit);
/* ------------------------------------------------------------------ util */
char *apex_now_iso(void);
void apex_json_str(mcp_buf *b, const char *s);
int apex_port_in_scope(int port);
int apex_scope_expired(void);
const char *apex_home(void);
#endif /* APEX_H */