Cleaned up parts of the serialization by removing redundant code.

This commit is contained in:
Jakob Friedl
2025-07-28 21:29:47 +02:00
parent 882579b3cb
commit 0d54b3e64b
16 changed files with 185 additions and 199 deletions

View File

@@ -1,6 +1,90 @@
import ./manager
import ../common/[types, utils]
# Define function prototypes
proc executePwd(config: AgentConfig, task: Task): TaskResult
proc executeCd(config: AgentConfig, task: Task): TaskResult
proc executeDir(config: AgentConfig, task: Task): TaskResult
proc executeRm(config: AgentConfig, task: Task): TaskResult
proc executeRmdir(config: AgentConfig, task: Task): TaskResult
proc executeMove(config: AgentConfig, task: Task): TaskResult
proc executeCopy(config: AgentConfig, task: Task): TaskResult
# Command definitions
let commands* = @[
Command(
name: "pwd",
commandType: CMD_PWD,
description: "Retrieve current working directory.",
example: "pwd",
arguments: @[],
execute: executePwd
),
Command(
name: "cd",
commandType: CMD_CD,
description: "Change current working directory.",
example: "cd C:\\Windows\\Tasks",
arguments: @[
Argument(name: "directory", description: "Relative or absolute path of the directory to change to.", argumentType: STRING, isRequired: true)
],
execute: executeCd
),
Command(
name: "ls",
commandType: CMD_LS,
description: "List files and directories.",
example: "ls C:\\Users\\Administrator\\Desktop",
arguments: @[
Argument(name: "directory", description: "Relative or absolute path. Default: current working directory.", argumentType: STRING, isRequired: false)
],
execute: executeDir
),
Command(
name: "rm",
commandType: CMD_RM,
description: "Remove a file.",
example: "rm C:\\Windows\\Tasks\\payload.exe",
arguments: @[
Argument(name: "file", description: "Relative or absolute path to the file to delete.", argumentType: STRING, isRequired: true)
],
execute: executeRm
),
Command(
name: "rmdir",
commandType: CMD_RMDIR,
description: "Remove a directory.",
example: "rm C:\\Payloads",
arguments: @[
Argument(name: "directory", description: "Relative or absolute path to the directory to delete.", argumentType: STRING, isRequired: true)
],
execute: executeRmdir
),
Command(
name: "move",
commandType: CMD_MOVE,
description: "Move a file or directory.",
example: "move source.exe C:\\Windows\\Tasks\\destination.exe",
arguments: @[
Argument(name: "source", description: "Source file path.", argumentType: STRING, isRequired: true),
Argument(name: "destination", description: "Destination file path.", argumentType: STRING, isRequired: true)
],
execute: executeMove
),
Command(
name: "copy",
commandType: CMD_COPY,
description: "Copy a file or directory.",
example: "copy source.exe C:\\Windows\\Tasks\\destination.exe",
arguments: @[
Argument(name: "source", description: "Source file path.", argumentType: STRING, isRequired: true),
Argument(name: "destination", description: "Destination file path.", argumentType: STRING, isRequired: true)
],
execute: executeCopy
)
]
# Implementation of the execution functions
when defined(server):
proc executePwd(config: AgentConfig, task: Task): TaskResult = nil
proc executeCd(config: AgentConfig, task: Task): TaskResult = nil
@@ -10,7 +94,6 @@ when defined(server):
proc executeMove(config: AgentConfig, task: Task): TaskResult = nil
proc executeCopy(config: AgentConfig, task: Task): TaskResult = nil
# Implementation of the execution functions
when defined(agent):
import os, strutils, strformat, times, algorithm, winim
@@ -279,79 +362,4 @@ when defined(agent):
return createTaskResult(task, STATUS_COMPLETED, RESULT_NO_OUTPUT, @[])
except CatchableError as err:
return createTaskResult(task, STATUS_FAILED, RESULT_STRING, err.msg.toBytes())
# Command definitions
let commands* = @[
Command(
name: "pwd",
commandType: CMD_PWD,
description: "Retrieve current working directory.",
example: "pwd",
arguments: @[],
execute: executePwd
),
Command(
name: "cd",
commandType: CMD_CD,
description: "Change current working directory.",
example: "cd C:\\Windows\\Tasks",
arguments: @[
Argument(name: "directory", description: "Relative or absolute path of the directory to change to.", argumentType: STRING, isRequired: true)
],
execute: executeCd
),
Command(
name: "ls",
commandType: CMD_LS,
description: "List files and directories.",
example: "ls C:\\Users\\Administrator\\Desktop",
arguments: @[
Argument(name: "directory", description: "Relative or absolute path. Default: current working directory.", argumentType: STRING, isRequired: false)
],
execute: executeDir
),
Command(
name: "rm",
commandType: CMD_RM,
description: "Remove a file.",
example: "rm C:\\Windows\\Tasks\\payload.exe",
arguments: @[
Argument(name: "file", description: "Relative or absolute path to the file to delete.", argumentType: STRING, isRequired: true)
],
execute: executeRm
),
Command(
name: "rmdir",
commandType: CMD_RMDIR,
description: "Remove a directory.",
example: "rm C:\\Payloads",
arguments: @[
Argument(name: "directory", description: "Relative or absolute path to the directory to delete.", argumentType: STRING, isRequired: true)
],
execute: executeRmdir
),
Command(
name: "move",
commandType: CMD_MOVE,
description: "Move a file or directory.",
example: "move source.exe C:\\Windows\\Tasks\\destination.exe",
arguments: @[
Argument(name: "source", description: "Source file path.", argumentType: STRING, isRequired: true),
Argument(name: "destination", description: "Destination file path.", argumentType: STRING, isRequired: true)
],
execute: executeMove
),
Command(
name: "copy",
commandType: CMD_COPY,
description: "Copy a file or directory.",
example: "copy source.exe C:\\Windows\\Tasks\\destination.exe",
arguments: @[
Argument(name: "source", description: "Source file path.", argumentType: STRING, isRequired: true),
Argument(name: "destination", description: "Destination file path.", argumentType: STRING, isRequired: true)
],
execute: executeCopy
)
]
return createTaskResult(task, STATUS_FAILED, RESULT_STRING, err.msg.toBytes())

View File

@@ -1,10 +1,28 @@
import ./manager
import ../common/[types, utils]
# Define function prototype
proc executeShell(config: AgentConfig, task: Task): TaskResult
# Command definition (as seq[Command])
let commands*: seq[Command] = @[
Command(
name: "shell",
commandType: CMD_SHELL,
description: "Execute a shell command and retrieve the output.",
example: "shell whoami /all",
arguments: @[
Argument(name: "command", description: "Command to be executed.", argumentType: STRING, isRequired: true),
Argument(name: "arguments", description: "Arguments to be passed to the command.", argumentType: STRING, isRequired: false)
],
execute: executeShell
)
]
# Implement execution functions
when defined(server):
proc executeShell(config: AgentConfig, task: Task): TaskResult = nil
# Implement execution functions
when defined(agent):
import ../agent/core/taskresult
@@ -38,18 +56,3 @@ when defined(agent):
except CatchableError as err:
return createTaskResult(task, STATUS_FAILED, RESULT_STRING, err.msg.toBytes())
# Command definition (as seq[Command])
let commands*: seq[Command] = @[
Command(
name: "shell",
commandType: CMD_SHELL,
description: "Execute a shell command and retrieve the output.",
example: "shell whoami /all",
arguments: @[
Argument(name: "command", description: "Command to be executed.", argumentType: STRING, isRequired: true),
Argument(name: "arguments", description: "Arguments to be passed to the command.", argumentType: STRING, isRequired: false)
],
execute: executeShell
)
]

View File

@@ -1,10 +1,27 @@
import ./manager
import ../common/[types, utils]
# Define function prototype
proc executeSleep(config: AgentConfig, task: Task): TaskResult
# Command definition (as seq[Command])
let commands* = @[
Command(
name: "sleep",
commandType: CMD_SLEEP,
description: "Update sleep delay configuration.",
example: "sleep 5",
arguments: @[
Argument(name: "delay", description: "Delay in seconds.", argumentType: INT, isRequired: true)
],
execute: executeSleep
)
]
# Implement execution functions
when defined(server):
proc executeSleep(config: AgentConfig, task: Task): TaskResult = nil
# Implement execution functions
when defined(agent):
import os, strutils, strformat
@@ -26,18 +43,3 @@ when defined(agent):
except CatchableError as err:
return createTaskResult(task, STATUS_FAILED, RESULT_STRING, err.msg.toBytes())
# Command definition (as seq[Command])
let commands* = @[
Command(
name: "sleep",
commandType: CMD_SLEEP,
description: "Update sleep delay configuration.",
example: "sleep 5",
arguments: @[
Argument(name: "delay", description: "Delay in seconds.", argumentType: INT, isRequired: true)
],
execute: executeSleep
)
]