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,10 +1,15 @@
import prologue, json
import sequtils, strutils, times
import prologue, json, terminal, strformat
import sequtils, strutils, times, base64
import ./handlers
import ../utils
import ../[utils, globals]
import ../../common/types
proc encode(bytes: seq[seq[byte]]): string =
result = ""
for task in bytes:
result &= encode(task)
proc error404*(ctx: Context) {.async.} =
resp "", Http404
@@ -88,12 +93,34 @@ proc getTasks*(ctx: Context) {.async.} =
agent = ctx.getPathParams("agent")
try:
var response: seq[byte]
let tasks = getTasks(listener, agent)
resp $tasks
if tasks.len <= 0:
resp "", Http200
return
# Create response, containing number of tasks, as well as length and content of each task
# This makes it easier for the agent to parse the tasks
response.add(uint8(tasks.len))
for task in tasks:
response.add(uint32(task.len).toBytes())
response.add(task)
await ctx.respond(
code = Http200,
body = response.toString()
)
# Notify operator that agent collected tasks
{.cast(gcsafe).}:
let date = now().format("dd-MM-yyyy HH:mm:ss")
cq.writeLine(fgBlack, styleBright, fmt"[{date}] [*] ", resetStyle, fmt"{$response.len} bytes sent.")
except CatchableError:
resp "", Http404
#[
POST /{listener-uuid}/{agent-uuid}/{task-uuid}/results
Called from agent to post results of a task

View File

@@ -179,6 +179,8 @@ proc handleAgentCommand*(cq: Conquest, input: string) =
task = cq.parseTask(command, parsedArgs[1..^1])
taskData: seq[byte] = cq.serializeTask(task)
# cq.writeLine(taskData.toHexDump())
# Add task to queue
cq.interactAgent.tasks.add(taskData)
cq.writeLine(fgBlack, styleBright, fmt"[{date}] [*] ", resetStyle, fmt"Tasked agent to {command.description.toLowerAscii()}")

View File

@@ -9,31 +9,32 @@ proc serializeTask*(cq: Conquest, task: Task): seq[byte] =
# Serialize payload
packer
.addToPayload(task.taskId)
.addToPayload(task.agentId)
.addToPayload(task.listenerId)
.addToPayload(task.timestamp)
.addToPayload(task.command)
.addToPayload(task.argCount)
.add(task.taskId)
.add(task.agentId)
.add(task.listenerId)
.add(task.timestamp)
.add(task.command)
.add(task.argCount)
for arg in task.args:
packer.addArgument(arg)
let payload = packer.packPayload()
let payload = packer.pack()
packer.reset()
# TODO: Encrypt payload body
# Serialize header
packer
.addToHeader(task.header.magic)
.addToHeader(task.header.version)
.addToHeader(task.header.packetType)
.addToHeader(task.header.flags)
.addToHeader(task.header.seqNr)
.addToHeader(cast[uint32](payload.len))
.addDataToHeader(task.header.hmac)
.add(task.header.magic)
.add(task.header.version)
.add(task.header.packetType)
.add(task.header.flags)
.add(task.header.seqNr)
.add(cast[uint32](payload.len))
.addData(task.header.hmac)
let header = packer.packHeader()
let header = packer.pack()
# TODO: Calculate and patch HMAC

View File

@@ -26,6 +26,11 @@ proc uuidToUint32*(uuid: string): uint32 =
proc uuidToString*(uuid: uint32): string =
return uuid.toHex(8)
proc toString*(data: seq[byte]): string =
result = newString(data.len)
for i, b in data:
result[i] = char(b)
proc toHexDump*(data: seq[byte]): string =
for i, b in data:
result.add(b.toHex(2))
@@ -35,6 +40,20 @@ proc toHexDump*(data: seq[byte]): string =
else:
result.add(" ") # Regular space
proc toBytes*(value: uint16): seq[byte] =
return @[
byte(value and 0xFF),
byte((value shr 8) and 0xFF)
]
proc toBytes*(value: uint32): seq[byte] =
return @[
byte(value and 0xFF),
byte((value shr 8) and 0xFF),
byte((value shr 16) and 0xFF),
byte((value shr 24) and 0xFF)
]
# Function templates and overwrites
template writeLine*(cq: Conquest, args: varargs[untyped]) =
cq.prompt.writeLine(args)