Refactored random byte generation functions.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import httpclient, json, strformat, strutils, asyncdispatch, base64, tables, parsetoml, random
|
import httpclient, json, strformat, strutils, asyncdispatch, base64, tables, parsetoml, random
|
||||||
|
|
||||||
import ../../common/[types, utils, profile]
|
import ../../common/[types, utils, profile]
|
||||||
import sugar
|
|
||||||
proc httpGet*(ctx: AgentCtx, heartbeat: seq[byte]): string =
|
proc httpGet*(ctx: AgentCtx, heartbeat: seq[byte]): string =
|
||||||
|
|
||||||
let client = newAsyncHttpClient(userAgent = ctx.profile.getString("agent.user-agent"))
|
let client = newAsyncHttpClient(userAgent = ctx.profile.getString("agent.user-agent"))
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ proc createHeartbeat*(ctx: AgentCtx): Heartbeat =
|
|||||||
size: 0'u32,
|
size: 0'u32,
|
||||||
agentId: string.toUuid(ctx.agentId),
|
agentId: string.toUuid(ctx.agentId),
|
||||||
seqNr: 0'u32,
|
seqNr: 0'u32,
|
||||||
iv: generateIV(),
|
iv: generateBytes(Iv),
|
||||||
gmac: default(AuthenticationTag)
|
gmac: default(AuthenticationTag)
|
||||||
),
|
),
|
||||||
listenerId: string.toUuid(ctx.listenerId),
|
listenerId: string.toUuid(ctx.listenerId),
|
||||||
|
|||||||
@@ -203,7 +203,7 @@ proc collectAgentMetadata*(ctx: AgentCtx): AgentRegistrationData =
|
|||||||
size: 0'u32,
|
size: 0'u32,
|
||||||
agentId: string.toUuid(ctx.agentId),
|
agentId: string.toUuid(ctx.agentId),
|
||||||
seqNr: nextSequence(string.toUuid(ctx.agentId)),
|
seqNr: nextSequence(string.toUuid(ctx.agentId)),
|
||||||
iv: generateIV(),
|
iv: generateBytes(Iv),
|
||||||
gmac: default(AuthenticationTag)
|
gmac: default(AuthenticationTag)
|
||||||
),
|
),
|
||||||
agentPublicKey: ctx.agentPublicKey,
|
agentPublicKey: ctx.agentPublicKey,
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ proc createTaskResult*(task: Task, status: StatusType, resultType: ResultType, r
|
|||||||
size: 0'u32,
|
size: 0'u32,
|
||||||
agentId: task.header.agentId,
|
agentId: task.header.agentId,
|
||||||
seqNr: nextSequence(task.header.agentId),
|
seqNr: nextSequence(task.header.agentId),
|
||||||
iv: generateIV(),
|
iv: generateBytes(Iv),
|
||||||
gmac: default(array[16, byte])
|
gmac: default(array[16, byte])
|
||||||
),
|
),
|
||||||
taskId: task.taskId,
|
taskId: task.taskId,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import system
|
import macros, system
|
||||||
import nimcrypto
|
import nimcrypto
|
||||||
|
|
||||||
import ./[utils, types]
|
import ./[utils, types]
|
||||||
@@ -7,18 +7,11 @@ import ./[utils, types]
|
|||||||
Symmetric AES256 GCM encryption for secure C2 traffic
|
Symmetric AES256 GCM encryption for secure C2 traffic
|
||||||
Ensures both confidentiality and integrity of the packet
|
Ensures both confidentiality and integrity of the packet
|
||||||
]#
|
]#
|
||||||
proc generateIV*(): Iv =
|
proc generateBytes*(T: typedesc[Key | Iv]): array =
|
||||||
# Generate a random 98-bit (12-byte) initialization vector for AES-256 GCM mode
|
var bytes: T
|
||||||
var iv: Iv
|
if randomBytes(bytes) != sizeof(T):
|
||||||
if randomBytes(iv) != sizeof(Iv):
|
raise newException(CatchableError, "Failed to generate byte array.")
|
||||||
raise newException(CatchableError, "Failed to generate IV.")
|
return bytes
|
||||||
return iv
|
|
||||||
|
|
||||||
proc generateKey*(): Key =
|
|
||||||
var key: Key
|
|
||||||
if randomBytes(key) != sizeof(Key):
|
|
||||||
raise newException(CatchableError, "Failed to generate IV.")
|
|
||||||
return key
|
|
||||||
|
|
||||||
proc encrypt*(key: Key, iv: Iv, data: seq[byte], sequenceNumber: uint32 = 0): (seq[byte], AuthenticationTag) =
|
proc encrypt*(key: Key, iv: Iv, data: seq[byte], sequenceNumber: uint32 = 0): (seq[byte], AuthenticationTag) =
|
||||||
|
|
||||||
@@ -97,7 +90,7 @@ proc wipeKey*(data: var openArray[byte]) =
|
|||||||
|
|
||||||
# Key pair generation
|
# Key pair generation
|
||||||
proc generateKeyPair*(): KeyPair =
|
proc generateKeyPair*(): KeyPair =
|
||||||
let privateKey = generateKey()
|
let privateKey = generateBytes(Key)
|
||||||
return KeyPair(
|
return KeyPair(
|
||||||
privateKey: privateKey,
|
privateKey: privateKey,
|
||||||
publicKey: getPublicKey(privateKey)
|
publicKey: getPublicKey(privateKey)
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ proc serializeConfiguration(cq: Conquest, listener: Listener, sleep: int): seq[b
|
|||||||
packer.reset()
|
packer.reset()
|
||||||
|
|
||||||
# Encrypt profile configuration data with a newly generated encryption key
|
# Encrypt profile configuration data with a newly generated encryption key
|
||||||
var aesKey = generateKey()
|
var aesKey = generateBytes(Key)
|
||||||
let iv = generateIV()
|
let iv = generateBytes(Iv)
|
||||||
|
|
||||||
let (encData, gmac) = encrypt(aesKey, iv, data)
|
let (encData, gmac) = encrypt(aesKey, iv, data)
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import times, strformat, strutils, prompt, terminal
|
|||||||
import std/[dirs, paths]
|
import std/[dirs, paths]
|
||||||
|
|
||||||
import ../globals
|
import ../globals
|
||||||
import ../../common/[types, profile]
|
import ../../common/types
|
||||||
|
|
||||||
proc makeAgentLogDirectory*(cq: Conquest, agentId: string): bool =
|
proc makeAgentLogDirectory*(cq: Conquest, agentId: string): bool =
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import prompt, terminal, argparse, parsetoml
|
|||||||
import strutils, strformat, system, tables
|
import strutils, strformat, system, tables
|
||||||
|
|
||||||
import ./[agent, listener, builder]
|
import ./[agent, listener, builder]
|
||||||
import ../[globals, utils]
|
import ../globals
|
||||||
import ../db/database
|
import ../db/database
|
||||||
import ../core/logger
|
import ../core/logger
|
||||||
import ../../common/[types, crypto, profile]
|
import ../../common/[types, crypto, profile]
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import os
|
|
||||||
import ../common/types
|
import ../common/types
|
||||||
|
|
||||||
# Global server context
|
# Global server context
|
||||||
|
|||||||
@@ -106,7 +106,7 @@ proc createTask*(cq: Conquest, command: Command, arguments: seq[string]): Task =
|
|||||||
taskHeader.size = 0'u32
|
taskHeader.size = 0'u32
|
||||||
taskHeader.agentId = string.toUuid(cq.interactAgent.agentId)
|
taskHeader.agentId = string.toUuid(cq.interactAgent.agentId)
|
||||||
taskHeader.seqNr = nextSequence(taskHeader.agentId)
|
taskHeader.seqNr = nextSequence(taskHeader.agentId)
|
||||||
taskHeader.iv = generateIV() # Generate a random IV for AES-256 GCM
|
taskHeader.iv = generateBytes(Iv) # Generate a random IV for AES-256 GCM
|
||||||
taskHeader.gmac = default(AuthenticationTag)
|
taskHeader.gmac = default(AuthenticationTag)
|
||||||
|
|
||||||
task.header = taskHeader
|
task.header = taskHeader
|
||||||
|
|||||||
Reference in New Issue
Block a user