Move command registry from components/command/ into components/core/. New modules: mod_canbus, mod_honeypot, mod_fallback, mod_redteam, mod_ota. Replace mod_proxy with tun_core (multiplexed SOCKS5 tunnel). Kconfig extended with per-module settings and async worker config.
240 lines
5.8 KiB
C
240 lines
5.8 KiB
C
#pragma once
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <stdarg.h>
|
|
#include <inttypes.h>
|
|
#include <stdio.h>
|
|
|
|
#include "sdkconfig.h"
|
|
#include "esp_log.h"
|
|
#include "esp_err.h"
|
|
|
|
/* >>> CRITIQUE <<< */
|
|
#include "c2.pb.h" /* c2_Command, c2_AgentMsgType */
|
|
|
|
/* ============================================================
|
|
* GLOBAL DEFINES
|
|
* ============================================================ */
|
|
|
|
#define MAX_ARGS 10
|
|
#define MAX_RESPONSE_SIZE 1024
|
|
|
|
/* ============================================================
|
|
* LOG HELPERS
|
|
* ============================================================ */
|
|
#ifdef CONFIG_LOG_COLORS
|
|
#define ESPILON_LOG_PURPLE "\033[0;35m"
|
|
#define ESPILON_LOG_RESET "\033[0m"
|
|
#else
|
|
#define ESPILON_LOG_PURPLE ""
|
|
#define ESPILON_LOG_RESET ""
|
|
#endif
|
|
|
|
static inline void espilon_log_purple(
|
|
const char *tag,
|
|
const char *fmt,
|
|
...
|
|
) {
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
|
|
printf(ESPILON_LOG_PURPLE "I (%" PRIu32 ") %s: ",
|
|
(uint32_t)esp_log_timestamp(), tag);
|
|
vprintf(fmt, args);
|
|
printf(ESPILON_LOG_RESET "\n");
|
|
|
|
va_end(args);
|
|
}
|
|
|
|
#define ESPILON_LOGI_PURPLE(tag, fmt, ...) \
|
|
espilon_log_purple(tag, fmt, ##__VA_ARGS__)
|
|
|
|
/* Socket TCP global */
|
|
extern int sock;
|
|
|
|
/* ============================================================
|
|
* COM INIT
|
|
* ============================================================ */
|
|
|
|
bool com_init(void);
|
|
|
|
/* ============================================================
|
|
* CRYPTO API (ChaCha20-Poly1305 AEAD + HKDF)
|
|
* ============================================================ */
|
|
|
|
/* Init crypto: read master key from factory NVS, derive via HKDF-SHA256 */
|
|
bool crypto_init(void);
|
|
|
|
/*
|
|
* Encrypt (AEAD). Output: nonce[12] || ciphertext || tag[16]
|
|
* Returns total output length, or -1 on error.
|
|
*/
|
|
int crypto_encrypt(const uint8_t *plain, size_t plain_len,
|
|
uint8_t *out, size_t out_cap);
|
|
|
|
/*
|
|
* Decrypt + verify (AEAD). Input: nonce[12] || ciphertext || tag[16]
|
|
* Returns plaintext length, or -1 on error / auth failure.
|
|
*/
|
|
int crypto_decrypt(const uint8_t *in, size_t in_len,
|
|
uint8_t *out, size_t out_cap);
|
|
|
|
/* Base64 helpers */
|
|
char *base64_decode(const char *input, size_t *output_len);
|
|
char *base64_encode(const unsigned char *input, size_t input_len);
|
|
|
|
/* C2 decode + decrypt + protobuf + exec */
|
|
bool c2_decode_and_exec(const char *frame);
|
|
/* ============================================================
|
|
* ESP → C2 Messaging API
|
|
* ============================================================ */
|
|
|
|
bool agent_send(
|
|
c2_AgentMsgType type,
|
|
const char *source,
|
|
const char *request_id,
|
|
const void *data,
|
|
size_t len,
|
|
bool eof
|
|
);
|
|
|
|
/* Helpers globaux */
|
|
bool msg_info(
|
|
const char *src,
|
|
const char *msg,
|
|
const char *req
|
|
);
|
|
|
|
bool msg_error(
|
|
const char *src,
|
|
const char *msg,
|
|
const char *req
|
|
);
|
|
|
|
bool msg_data(
|
|
const char *src,
|
|
const void *data,
|
|
size_t len,
|
|
bool eof,
|
|
const char *req
|
|
);
|
|
|
|
/* ============================================================
|
|
* DEVICE
|
|
* ============================================================ */
|
|
|
|
bool device_id_matches(
|
|
const char *local_id,
|
|
const char *target_id
|
|
);
|
|
|
|
/* ============================================================
|
|
* CORE PROCESSING (C2 → ESP)
|
|
* ============================================================ */
|
|
|
|
void process_command(
|
|
const c2_Command *cmd
|
|
);
|
|
|
|
/*
|
|
* Compat legacy optionnel
|
|
*/
|
|
void process_command_from_buffer(
|
|
uint8_t *buffer,
|
|
size_t len
|
|
);
|
|
|
|
/* ============================================================
|
|
* COMMAND REGISTRY & DISPATCH
|
|
* ============================================================ */
|
|
|
|
#define MAX_COMMANDS 72
|
|
#define MAX_ASYNC_ARGS 8
|
|
#define MAX_ASYNC_ARG_LEN 64
|
|
|
|
typedef esp_err_t (*command_handler_t)(
|
|
int argc,
|
|
char **argv,
|
|
const char *request_id,
|
|
void *ctx
|
|
);
|
|
|
|
typedef struct {
|
|
const char *name;
|
|
const char *sub;
|
|
const char *help;
|
|
int min_args;
|
|
int max_args;
|
|
command_handler_t handler;
|
|
void *ctx;
|
|
bool async;
|
|
} command_t;
|
|
|
|
void command_register(const command_t *cmd);
|
|
void command_log_registry_summary(void);
|
|
void command_process_pb(const c2_Command *cmd);
|
|
void command_async_init(void);
|
|
void command_async_enqueue(const command_t *cmd, const c2_Command *pb_cmd, int argv_offset);
|
|
|
|
/* ============================================================
|
|
* WIFI
|
|
* ============================================================ */
|
|
#ifdef CONFIG_NETWORK_WIFI
|
|
void wifi_init(void);
|
|
void tcp_client_task(void *pvParameters);
|
|
void wifi_pause_reconnect(void);
|
|
void wifi_resume_reconnect(void);
|
|
#endif
|
|
|
|
/* Fallback: when true, WiFi.c skips its own reconnect logic */
|
|
#include <stdatomic.h>
|
|
extern atomic_bool fb_active;
|
|
|
|
/* FakeAP: when true, WiFi.c skips reconnect to avoid interference */
|
|
#ifdef CONFIG_MODULE_FAKEAP
|
|
extern atomic_bool fakeap_active;
|
|
#endif
|
|
|
|
/* ============================================================
|
|
* GPRS
|
|
* ============================================================ */
|
|
|
|
#if defined(CONFIG_NETWORK_GPRS) || defined(CONFIG_FB_GPRS_FALLBACK)
|
|
#define BUFF_SIZE 1024
|
|
#define UART_NUM UART_NUM_1
|
|
#define TXD_PIN CONFIG_GPRS_TXD_PIN
|
|
#define RXD_PIN CONFIG_GPRS_RXD_PIN
|
|
#define PWR_KEY CONFIG_GPRS_PWR_KEY
|
|
#define PWR_EN CONFIG_GPRS_PWR_EN
|
|
#define RESET CONFIG_GPRS_RESET_PIN
|
|
#define LED_GPIO CONFIG_GPRS_LED_GPIO
|
|
|
|
void setup_uart(void);
|
|
void setup_modem(void);
|
|
|
|
bool connect_gprs(void);
|
|
bool connect_tcp(void);
|
|
bool connect_tcp_to(const char *ip, int port);
|
|
|
|
bool gprs_send(const void *buf, size_t len);
|
|
void gprs_rx_poll(void);
|
|
void close_tcp_connection(void);
|
|
|
|
void send_at_command(const char *cmd);
|
|
#endif
|
|
|
|
#ifdef CONFIG_NETWORK_GPRS
|
|
void gprs_client_task(void *pvParameters);
|
|
#endif
|
|
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|