2025-10-01 15:27:06 +02:00
|
|
|
import times, json, base64, parsetoml
|
2025-10-01 21:57:26 +02:00
|
|
|
import ../common/[types, utils, event]
|
2025-09-27 13:54:12 +02:00
|
|
|
export sendHeartbeat, recvEvent
|
2025-09-25 19:22:17 +02:00
|
|
|
|
2025-10-01 15:27:06 +02:00
|
|
|
proc `%`*(agent: Agent): JsonNode =
|
|
|
|
|
result = newJObject()
|
|
|
|
|
result["agentId"] = %agent.agentId
|
|
|
|
|
result["listenerId"] = %agent.listenerId
|
|
|
|
|
result["username"] = %agent.username
|
|
|
|
|
result["hostname"] = %agent.hostname
|
|
|
|
|
result["domain"] = %agent.domain
|
2025-10-02 10:25:37 +02:00
|
|
|
result["ipInternal"] = %agent.ipInternal
|
|
|
|
|
result["ipExternal"] = %agent.ipExternal
|
2025-10-01 15:27:06 +02:00
|
|
|
result["os"] = %agent.os
|
|
|
|
|
result["process"] = %agent.process
|
|
|
|
|
result["pid"] = %agent.pid
|
|
|
|
|
result["elevated"] = %agent.elevated
|
|
|
|
|
result["sleep"] = %agent.sleep
|
2025-10-02 10:25:37 +02:00
|
|
|
result["modules"] = %agent.modules
|
|
|
|
|
result["firstCheckin"] = %agent.firstCheckin
|
|
|
|
|
result["latestCheckin"] = %agent.latestCheckin
|
2025-10-01 15:27:06 +02:00
|
|
|
|
|
|
|
|
proc `%`*(listener: Listener): JsonNode =
|
|
|
|
|
result = newJObject()
|
|
|
|
|
result["listenerId"] = %listener.listenerId
|
|
|
|
|
result["address"] = %listener.address
|
|
|
|
|
result["port"] = %listener.port
|
|
|
|
|
result["protocol"] = %listener.protocol
|
|
|
|
|
|
2025-09-25 19:22:17 +02:00
|
|
|
#[
|
|
|
|
|
Server -> Client
|
|
|
|
|
]#
|
2025-10-01 21:57:26 +02:00
|
|
|
proc sendPublicKey*(client: WsConnection, publicKey: Key) =
|
|
|
|
|
let event = Event(
|
|
|
|
|
eventType: CLIENT_KEY_EXCHANGE,
|
|
|
|
|
timestamp: now().toTime().toUnix(),
|
|
|
|
|
data: %*{
|
|
|
|
|
"publicKey": encode(Bytes.toString(publicKey))
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
if client != nil:
|
|
|
|
|
client.ws.sendEvent(event, client.sessionKey)
|
|
|
|
|
|
|
|
|
|
proc sendProfile*(client: WsConnection, profile: Profile) =
|
2025-09-25 19:22:17 +02:00
|
|
|
let event = Event(
|
|
|
|
|
eventType: CLIENT_PROFILE,
|
|
|
|
|
timestamp: now().toTime().toUnix(),
|
|
|
|
|
data: %*{
|
|
|
|
|
"profile": profile.toTomlString()
|
|
|
|
|
}
|
|
|
|
|
)
|
2025-09-26 15:30:14 +02:00
|
|
|
if client != nil:
|
2025-10-01 21:57:26 +02:00
|
|
|
client.ws.sendEvent(event, client.sessionKey)
|
2025-09-25 19:22:17 +02:00
|
|
|
|
2025-10-01 21:57:26 +02:00
|
|
|
proc sendEventlogItem*(client: WsConnection, logType: LogType, message: string) =
|
2025-09-25 19:22:17 +02:00
|
|
|
let event = Event(
|
|
|
|
|
eventType: CLIENT_EVENTLOG_ITEM,
|
|
|
|
|
timestamp: now().toTime().toUnix(),
|
|
|
|
|
data: %*{
|
|
|
|
|
"logType": cast[uint8](logType),
|
|
|
|
|
"message": message
|
|
|
|
|
}
|
|
|
|
|
)
|
2025-09-26 15:30:14 +02:00
|
|
|
if client != nil:
|
2025-10-01 21:57:26 +02:00
|
|
|
client.ws.sendEvent(event, client.sessionKey)
|
2025-09-25 19:22:17 +02:00
|
|
|
|
2025-10-01 21:57:26 +02:00
|
|
|
proc sendAgent*(client: WsConnection, agent: Agent) =
|
2025-09-25 19:22:17 +02:00
|
|
|
let event = Event(
|
|
|
|
|
eventType: CLIENT_AGENT_ADD,
|
|
|
|
|
timestamp: now().toTime().toUnix(),
|
|
|
|
|
data: %agent
|
|
|
|
|
)
|
2025-09-26 15:30:14 +02:00
|
|
|
if client != nil:
|
2025-10-01 21:57:26 +02:00
|
|
|
client.ws.sendEvent(event, client.sessionKey)
|
2025-09-25 19:22:17 +02:00
|
|
|
|
2025-10-01 21:57:26 +02:00
|
|
|
proc sendListener*(client: WsConnection, listener: Listener) =
|
2025-09-25 19:22:17 +02:00
|
|
|
let event = Event(
|
|
|
|
|
eventType: CLIENT_LISTENER_ADD,
|
|
|
|
|
timestamp: now().toTime().toUnix(),
|
|
|
|
|
data: %listener
|
|
|
|
|
)
|
2025-09-26 15:30:14 +02:00
|
|
|
if client != nil:
|
2025-10-01 21:57:26 +02:00
|
|
|
client.ws.sendEvent(event, client.sessionKey)
|
2025-09-25 19:22:17 +02:00
|
|
|
|
2025-10-01 21:57:26 +02:00
|
|
|
proc sendAgentCheckin*(client: WsConnection, agentId: string) =
|
2025-09-25 19:22:17 +02:00
|
|
|
let event = Event(
|
|
|
|
|
eventType: CLIENT_AGENT_CHECKIN,
|
|
|
|
|
timestamp: now().toTime().toUnix(),
|
|
|
|
|
data: %*{
|
|
|
|
|
"agentId": agentId
|
|
|
|
|
}
|
|
|
|
|
)
|
2025-09-26 15:30:14 +02:00
|
|
|
if client != nil:
|
2025-10-01 21:57:26 +02:00
|
|
|
client.ws.sendEvent(event, client.sessionKey)
|
2025-09-25 19:22:17 +02:00
|
|
|
|
2025-10-01 21:57:26 +02:00
|
|
|
proc sendAgentPayload*(client: WsConnection, bytes: seq[byte]) =
|
2025-09-25 19:22:17 +02:00
|
|
|
let event = Event(
|
|
|
|
|
eventType: CLIENT_AGENT_PAYLOAD,
|
|
|
|
|
timestamp: now().toTime().toUnix(),
|
|
|
|
|
data: %*{
|
|
|
|
|
"payload": encode(bytes)
|
|
|
|
|
}
|
|
|
|
|
)
|
2025-09-26 15:30:14 +02:00
|
|
|
if client != nil:
|
2025-10-01 21:57:26 +02:00
|
|
|
client.ws.sendEvent(event, client.sessionKey)
|
2025-09-25 19:22:17 +02:00
|
|
|
|
2025-10-01 21:57:26 +02:00
|
|
|
proc sendConsoleItem*(client: WsConnection, agentId: string, logType: LogType, message: string) =
|
2025-09-25 19:22:17 +02:00
|
|
|
let event = Event(
|
|
|
|
|
eventType: CLIENT_CONSOLE_ITEM,
|
|
|
|
|
timestamp: now().toTime().toUnix(),
|
|
|
|
|
data: %*{
|
|
|
|
|
"agentId": agentId,
|
|
|
|
|
"logType": cast[uint8](logType),
|
|
|
|
|
"message": message
|
|
|
|
|
}
|
|
|
|
|
)
|
2025-09-26 15:30:14 +02:00
|
|
|
if client != nil:
|
2025-10-01 21:57:26 +02:00
|
|
|
client.ws.sendEvent(event, client.sessionKey)
|