Reworked logging system to work with new GUI.

This commit is contained in:
Jakob Friedl
2025-10-02 13:51:04 +02:00
parent ab48bc5795
commit d02808a6d3
5 changed files with 46 additions and 27 deletions

View File

@@ -186,7 +186,7 @@ proc handleAgentCommand*(component: ConsoleComponent, connection: WsConnection,
command = getCommandByName(parsedArgs[0])
task = createTask(component.agent.agentId, component.agent.listenerId, command, parsedArgs[1..^1])
connection.sendAgentTask(component.agent.agentId, task)
connection.sendAgentTask(component.agent.agentId, input, task)
component.addItem(LOG_INFO, fmt"Tasked agent to {command.description.toLowerAscii()} ({Uuid.toString(task.taskId)})")
except CatchableError:

View File

@@ -48,12 +48,13 @@ proc sendAgentBuild*(connection: WsConnection, buildInformation: AgentBuildInfor
)
connection.ws.sendEvent(event, connection.sessionKey)
proc sendAgentTask*(connection: WsConnection, agentId: string, task: Task) =
proc sendAgentTask*(connection: WsConnection, agentId: string, command: string, task: Task) =
let event = Event(
eventType: CLIENT_AGENT_TASK,
timestamp: now().toTime().toUnix(),
data: %*{
"agentId": agentId,
"command": command,
"task": task
}
)

View File

@@ -11,11 +11,11 @@ proc makeAgentLogDirectory*(cq: Conquest, agentId: string): bool =
except OSError:
return false
proc log*(cq: Conquest, agentId: string = "", logEntry: string) =
proc log*(logEntry: string, agentId: string = "") =
# Write log entry to file
var logFile: string
if agentId.isEmptyOrWhitespace():
logFile = fmt"{CONQUEST_ROOT}/data/logs/events.log"
logFile = fmt"{CONQUEST_ROOT}/data/logs/teamserver.log"
else:
logFile = fmt"{CONQUEST_ROOT}/data/logs/{agentId}/session.log"
let file = open(logFile, fmAppend)
@@ -39,7 +39,6 @@ proc getTimestamp*(): string =
# Function templates and overwrites
template writeLine*(cq: Conquest, args: varargs[untyped] = "") =
stdout.styledWriteLine(args)
# cq.log(extractStrings($(args)))
# Wrapper functions for logging/console output
template info*(cq: Conquest, args: varargs[untyped] = "") =

View File

@@ -1,10 +1,9 @@
import terminal, parsetoml, json, math, base64, times
import strutils, strformat, system, tables
import ./core/[listener, builder]
import ./core/[listener, logger, builder]
import ./globals
import ./db/database
import ./core/logger
import ../common/[types, crypto, utils, profile, event]
import ./websocket
import mummy, mummy/routers
@@ -66,13 +65,17 @@ proc websocketHandler(ws: WebSocket, event: WebSocketEvent, message: Message) {.
cq.client.sendListener(listener)
for id, agent in cq.agents:
cq.client.sendAgent(agent)
cq.client.sendEventlogItem(LOG_SUCCESS_SHORT, "CQ-V1")
cq.client.sendEventlogItem(LOG_SUCCESS_SHORT, "Connected to Conquest team server.")
of CLIENT_AGENT_TASK:
let agentId = event.data["agentId"].getStr()
let command = event.data["command"].getStr()
let task = event.data["task"].to(Task)
cq.agents[agentId].tasks.add(task)
let timestamp = event.timestamp.fromUnix().local().format("dd-MM-yyyy HH:mm:ss")
log(fmt"[{timestamp}]{$LOG_COMMAND}{command}", agentId)
of CLIENT_LISTENER_START:
let listener = event.data.to(UIListener)
cq.listenerStart(listener.listenerId, listener.address, listener.port, listener.protocol)
@@ -133,10 +136,6 @@ proc startServer*(profilePath: string) =
cq.info("Using profile \"", profile.getString("name"), "\" (", profilePath ,").")
except CatchableError as err:
echo err.msg
quit(0)
# Initialize database
cq.dbInit()
for agent in cq.dbGetAllAgents():
@@ -156,6 +155,11 @@ proc startServer*(profilePath: string) =
let server = newServer(router, websocketHandler, maxBodyLen = 1024 * 1024 * 1024, maxMessageLen = 1024 * 1024 * 1024)
server.serve(Port(cq.profile.getInt("team-server.port")), "0.0.0.0")
except CatchableError as err:
echo err.msg
quit(0)
# Conquest framework entry point
when isMainModule:
import cligen; dispatch startServer

View File

@@ -1,5 +1,6 @@
import times, json, base64, parsetoml
import times, json, base64, parsetoml, strformat
import ../common/[types, utils, event]
import ./core/logger
export sendHeartbeat, recvEvent
proc `%`*(agent: Agent): JsonNode =
@@ -61,6 +62,11 @@ proc sendEventlogItem*(client: WsConnection, logType: LogType, message: string)
"message": message
}
)
# Log event
let timestamp = event.timestamp.fromUnix().local().format("dd-MM-yyyy HH:mm:ss")
log(fmt"[{timestamp}]{$logType}{message}")
if client != nil:
client.ws.sendEvent(event, client.sessionKey)
@@ -101,6 +107,7 @@ proc sendAgentPayload*(client: WsConnection, bytes: seq[byte]) =
"payload": encode(bytes)
}
)
if client != nil:
client.ws.sendEvent(event, client.sessionKey)
@@ -114,6 +121,14 @@ proc sendConsoleItem*(client: WsConnection, agentId: string, logType: LogType, m
"message": message
}
)
# Log agent console item
let timestamp = event.timestamp.fromUnix().local().format("dd-MM-yyyy HH:mm:ss")
if logType != LOG_OUTPUT:
log(fmt"[{timestamp}]{$logType}{message}", agentId)
else:
log(message, agentId)
if client != nil:
client.ws.sendEvent(event, client.sessionKey)