From d35b1d0a0d37a6d2d1fa0126d3aeb97603f73a70 Mon Sep 17 00:00:00 2001 From: Jakob Friedl <71284620+jakobfriedl@users.noreply.github.com> Date: Wed, 28 May 2025 11:14:30 +0200 Subject: [PATCH] Update agent in database when sleep setting is changed to display latest checkin correctly. --- agents/monarch/commands/sleep.nim | 2 +- agents/monarch/http.nim | 1 - server/agent/commands.nim | 2 +- server/agent/commands/sleep.nim | 5 +++++ server/db/dbAgent.nim | 12 ++++++++++++ server/utils.nim | 8 ++++---- 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/agents/monarch/commands/sleep.nim b/agents/monarch/commands/sleep.nim index 47b8d61..c86fdd2 100644 --- a/agents/monarch/commands/sleep.nim +++ b/agents/monarch/commands/sleep.nim @@ -8,7 +8,7 @@ proc taskSleep*(delay: int): tuple[output: TaskResult, status: TaskStatus] = try: sleep(delay * 1000) - return ("\n", Completed) + return ("", Completed) except CatchableError as err: return (fmt"An error occured: {err.msg}" & "\n", Failed) \ No newline at end of file diff --git a/agents/monarch/http.nim b/agents/monarch/http.nim index 186afda..8543d77 100644 --- a/agents/monarch/http.nim +++ b/agents/monarch/http.nim @@ -43,7 +43,6 @@ proc getTasks*(config: AgentConfig, agent: string): seq[Task] = except CatchableError as err: # When the listener is not reachable, don't kill the application, but check in at the next time echo "[-] [getTasks]:", err.msg - return false finally: client.close() diff --git a/server/agent/commands.nim b/server/agent/commands.nim index 7e7dfc5..74a4e6f 100644 --- a/server/agent/commands.nim +++ b/server/agent/commands.nim @@ -11,7 +11,7 @@ export shell, sleep [ ] cd : Change directory [ ] ls/dir : List all files in directory (including hidden ones) [ ] cat/type : Display contents of a file - [ ] sleep : Set sleep obfuscation duration to a different value and persist that value in the agent + [~] sleep : Set sleep obfuscation duration to a different value and persist that value in the agent Post-exploitation ----------------- diff --git a/server/agent/commands/sleep.nim b/server/agent/commands/sleep.nim index ed8fd78..a6bb4a7 100644 --- a/server/agent/commands/sleep.nim +++ b/server/agent/commands/sleep.nim @@ -1,8 +1,13 @@ import nanoid, sequtils, strutils, strformat, terminal, times import ../../types +import ../../db/database proc taskExecuteSleep*(cq: Conquest, delay: int) = + # Update 'sleep' value in database + if not cq.dbUpdateSleep(cq.interactAgent.name, delay): + return + # Create a new task let date: string = now().format("dd-MM-yyyy HH:mm:ss") diff --git a/server/db/dbAgent.nim b/server/db/dbAgent.nim index 5e4e551..4158a33 100644 --- a/server/db/dbAgent.nim +++ b/server/db/dbAgent.nim @@ -122,6 +122,18 @@ proc dbUpdateCheckin*(cq: Conquest, agentName: string, timestamp: string): bool conquestDb.exec("UPDATE agents SET latestCheckin = ? WHERE name = ?", timestamp, agentName) + conquestDb.close() + return true + except: + cq.writeLine(fgRed, styleBright, "[-] ", getCurrentExceptionMsg()) + return false + +proc dbUpdateSleep*(cq: Conquest, agentName: string, delay: int): bool = + try: + let conquestDb = openDatabase(cq.dbPath, mode=dbReadWrite) + + conquestDb.exec("UPDATE agents SET sleep = ? WHERE name = ?", delay, agentName) + conquestDb.close() return true except: diff --git a/server/utils.nim b/server/utils.nim index 3d4df2a..402bd4b 100644 --- a/server/utils.nim +++ b/server/utils.nim @@ -101,7 +101,7 @@ proc drawTable*(cq: Conquest, listeners: seq[Listener]) = cq.writeLine(border(botLeft, botMid, botRight, widths)) # Calculate time since latest checking in format: Xd Xh Xm Xs -proc timeSince*(timestamp: DateTime): Cell = +proc timeSince*(agent: Agent, timestamp: DateTime): Cell = let now = now() @@ -127,8 +127,8 @@ proc timeSince*(timestamp: DateTime): Cell = return Cell( text: text.strip(), # When the agent is 'dead', meaning that the latest checkin occured - # more than 15 seconds ago, dim the text of the cell - style: if totalSeconds > 15: styleDim else: styleBright + # more than the agents sleep configuration, dim the text style + style: if totalSeconds > agent.sleep: styleDim else: styleBright ) proc drawTable*(cq: Conquest, agents: seq[Agent]) = @@ -154,7 +154,7 @@ proc drawTable*(cq: Conquest, agents: seq[Agent]) = Cell(text: a.os), Cell(text: a.process, fg: if a.elevated: fgRed else: fgWhite), Cell(text: $a.pid, fg: if a.elevated: fgRed else: fgWhite), - timeSince(a.latestCheckin) + a.timeSince(a.latestCheckin) ] # Highlight agents running within elevated processes