Prevent database locking by not updating latest checkin in database and instead storing it only in memory
This commit is contained in:
@@ -18,7 +18,8 @@ proc register*(config: AgentConfig): string =
|
|||||||
"os": getOSVersion(),
|
"os": getOSVersion(),
|
||||||
"process": getProcessExe(),
|
"process": getProcessExe(),
|
||||||
"pid": getProcessId(),
|
"pid": getProcessId(),
|
||||||
"elevated": isElevated()
|
"elevated": isElevated(),
|
||||||
|
"sleep": config.sleep
|
||||||
}
|
}
|
||||||
echo $body
|
echo $body
|
||||||
|
|
||||||
|
|||||||
@@ -2,4 +2,4 @@
|
|||||||
-d:ListenerUuid="KPDHWZNT"
|
-d:ListenerUuid="KPDHWZNT"
|
||||||
-d:ListenerIp="localhost"
|
-d:ListenerIp="localhost"
|
||||||
-d:ListenerPort=7777
|
-d:ListenerPort=7777
|
||||||
-d:SleepDelay=10
|
-d:SleepDelay=0
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ proc agentList*(cq: Conquest, listener: string) =
|
|||||||
# If no argument is passed via -n, list all agents, otherwise only display agents connected to a specific listener
|
# If no argument is passed via -n, list all agents, otherwise only display agents connected to a specific listener
|
||||||
if listener == "":
|
if listener == "":
|
||||||
cq.drawTable(cq.dbGetAllAgents())
|
cq.drawTable(cq.dbGetAllAgents())
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Check if listener exists
|
# Check if listener exists
|
||||||
if not cq.dbListenerExists(listener.toUpperAscii):
|
if not cq.dbListenerExists(listener.toUpperAscii):
|
||||||
@@ -38,6 +39,7 @@ proc agentList*(cq: Conquest, listener: string) =
|
|||||||
|
|
||||||
cq.drawTable(cq.dbGetAllAgentsByListener(listener.toUpperAscii))
|
cq.drawTable(cq.dbGetAllAgentsByListener(listener.toUpperAscii))
|
||||||
|
|
||||||
|
|
||||||
# Display agent properties and details
|
# Display agent properties and details
|
||||||
proc agentInfo*(cq: Conquest, name: string) =
|
proc agentInfo*(cq: Conquest, name: string) =
|
||||||
# Check if agent supplied via -n parameter exists in database
|
# Check if agent supplied via -n parameter exists in database
|
||||||
@@ -202,8 +204,8 @@ proc getTasks*(listener, agent: string): JsonNode =
|
|||||||
|
|
||||||
# Update the last check-in date for the accessed agent
|
# Update the last check-in date for the accessed agent
|
||||||
cq.agents[agent.toUpperAscii].latestCheckin = now()
|
cq.agents[agent.toUpperAscii].latestCheckin = now()
|
||||||
if not cq.dbUpdateCheckin(agent.toUpperAscii, now().format("dd-MM-yyyy HH:mm:ss")):
|
# if not cq.dbUpdateCheckin(agent.toUpperAscii, now().format("dd-MM-yyyy HH:mm:ss")):
|
||||||
return nil
|
# return nil
|
||||||
|
|
||||||
# Return tasks in JSON format
|
# Return tasks in JSON format
|
||||||
return %cq.agents[agent.toUpperAscii].tasks.filterIt(it.status != Completed)
|
return %cq.agents[agent.toUpperAscii].tasks.filterIt(it.status != Completed)
|
||||||
|
|||||||
@@ -4,6 +4,10 @@ import ../../db/database
|
|||||||
|
|
||||||
proc taskExecuteSleep*(cq: Conquest, delay: int) =
|
proc taskExecuteSleep*(cq: Conquest, delay: int) =
|
||||||
|
|
||||||
|
if delay < 0:
|
||||||
|
cq.writeLine(fgRed, styleBright, "[-] Invalid sleep delay value.")
|
||||||
|
return
|
||||||
|
|
||||||
# Update 'sleep' value in database
|
# Update 'sleep' value in database
|
||||||
if not cq.dbUpdateSleep(cq.interactAgent.name, delay):
|
if not cq.dbUpdateSleep(cq.interactAgent.name, delay):
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ proc register*(ctx: Context) {.async.} =
|
|||||||
"os": "operating-system",
|
"os": "operating-system",
|
||||||
"process": "agent.exe",
|
"process": "agent.exe",
|
||||||
"pid": 1234,
|
"pid": 1234,
|
||||||
"elevated": false
|
"elevated": false.
|
||||||
|
"sleep": 10
|
||||||
}
|
}
|
||||||
]#
|
]#
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import prompt
|
import prompt
|
||||||
import prologue
|
import prologue
|
||||||
import tables
|
import tables, sequtils
|
||||||
import times
|
import times
|
||||||
import terminal
|
import terminal
|
||||||
|
|
||||||
@@ -25,6 +25,19 @@ type
|
|||||||
|
|
||||||
TaskResult* = string
|
TaskResult* = string
|
||||||
|
|
||||||
|
#[
|
||||||
|
TaskResult*[T] = ref object
|
||||||
|
data*: T
|
||||||
|
|
||||||
|
Task*[T] = ref object
|
||||||
|
id*: string
|
||||||
|
agent*: string
|
||||||
|
command*: TaskCommand
|
||||||
|
args*: seq[string]
|
||||||
|
result*: TaskResult[T]
|
||||||
|
status*: TaskStatus
|
||||||
|
]#
|
||||||
|
|
||||||
Task* = ref object
|
Task* = ref object
|
||||||
id*: string
|
id*: string
|
||||||
agent*: string
|
agent*: string
|
||||||
@@ -42,6 +55,7 @@ type
|
|||||||
process*: string
|
process*: string
|
||||||
pid*: int
|
pid*: int
|
||||||
elevated*: bool
|
elevated*: bool
|
||||||
|
sleep*: int
|
||||||
|
|
||||||
Agent* = ref object
|
Agent* = ref object
|
||||||
name*: string
|
name*: string
|
||||||
@@ -60,6 +74,7 @@ type
|
|||||||
firstCheckin*: DateTime
|
firstCheckin*: DateTime
|
||||||
latestCheckin*: DateTime
|
latestCheckin*: DateTime
|
||||||
|
|
||||||
|
# TODO: Take sleep value from agent registration data (set via nim.cfg file)
|
||||||
proc newAgent*(name, listener: string, firstCheckin: DateTime, postData: AgentRegistrationData): Agent =
|
proc newAgent*(name, listener: string, firstCheckin: DateTime, postData: AgentRegistrationData): Agent =
|
||||||
var agent = new Agent
|
var agent = new Agent
|
||||||
agent.name = name
|
agent.name = name
|
||||||
@@ -72,7 +87,7 @@ proc newAgent*(name, listener: string, firstCheckin: DateTime, postData: AgentRe
|
|||||||
agent.ip = postData.ip
|
agent.ip = postData.ip
|
||||||
agent.os = postData.os
|
agent.os = postData.os
|
||||||
agent.elevated = postData.elevated
|
agent.elevated = postData.elevated
|
||||||
agent.sleep = 10
|
agent.sleep = postData.sleep
|
||||||
agent.jitter = 0.2
|
agent.jitter = 0.2
|
||||||
agent.tasks = @[]
|
agent.tasks = @[]
|
||||||
agent.firstCheckin = firstCheckin
|
agent.firstCheckin = firstCheckin
|
||||||
@@ -136,6 +151,12 @@ proc delListener*(cq: Conquest, listenerName: string) =
|
|||||||
proc delAgent*(cq: Conquest, agentName: string) =
|
proc delAgent*(cq: Conquest, agentName: string) =
|
||||||
cq.agents.del(agentName)
|
cq.agents.del(agentName)
|
||||||
|
|
||||||
|
proc getAgentsAsSeq*(cq: Conquest): seq[Agent] =
|
||||||
|
var agents: seq[Agent] = @[]
|
||||||
|
for agent in cq.agents.values:
|
||||||
|
agents.add(agent)
|
||||||
|
return agents
|
||||||
|
|
||||||
proc initConquest*(dbPath: string): Conquest =
|
proc initConquest*(dbPath: string): Conquest =
|
||||||
var cq = new Conquest
|
var cq = new Conquest
|
||||||
var prompt = Prompt.init()
|
var prompt = Prompt.init()
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ proc drawTable*(cq: Conquest, listeners: seq[Listener]) =
|
|||||||
|
|
||||||
# Column headers and widths
|
# Column headers and widths
|
||||||
let headers = @["Name", "Address", "Port", "Protocol", "Agents"]
|
let headers = @["Name", "Address", "Port", "Protocol", "Agents"]
|
||||||
let widths = @[10, 17, 7, 10, 8]
|
let widths = @[8, 15, 5, 8, 6]
|
||||||
let headerCells = headers.mapIt(Cell(text: it, fg: fgWhite, bg: bgDefault))
|
let headerCells = headers.mapIt(Cell(text: it, fg: fgWhite, bg: bgDefault))
|
||||||
|
|
||||||
cq.writeLine(border(topLeft, topMid, topRight, widths))
|
cq.writeLine(border(topLeft, topMid, topRight, widths))
|
||||||
@@ -134,7 +134,7 @@ proc timeSince*(agent: Agent, timestamp: DateTime): Cell =
|
|||||||
proc drawTable*(cq: Conquest, agents: seq[Agent]) =
|
proc drawTable*(cq: Conquest, agents: seq[Agent]) =
|
||||||
|
|
||||||
let headers: seq[string] = @["Name", "Address", "Username", "Hostname", "Operating System", "Process", "PID", "Activity"]
|
let headers: seq[string] = @["Name", "Address", "Username", "Hostname", "Operating System", "Process", "PID", "Activity"]
|
||||||
let widths = @[10, 17, 15, 15, 18, 15, 7, 10]
|
let widths = @[8, 15, 15, 15, 16, 15, 5, 13]
|
||||||
let headerCells = headers.mapIt(Cell(text: it, fg: fgWhite, bg: bgDefault))
|
let headerCells = headers.mapIt(Cell(text: it, fg: fgWhite, bg: bgDefault))
|
||||||
|
|
||||||
cq.writeLine(border(topLeft, topMid, topRight, widths))
|
cq.writeLine(border(topLeft, topMid, topRight, widths))
|
||||||
@@ -154,7 +154,7 @@ proc drawTable*(cq: Conquest, agents: seq[Agent]) =
|
|||||||
Cell(text: a.os),
|
Cell(text: a.os),
|
||||||
Cell(text: a.process, fg: if a.elevated: fgRed else: fgWhite),
|
Cell(text: a.process, fg: if a.elevated: fgRed else: fgWhite),
|
||||||
Cell(text: $a.pid, fg: if a.elevated: fgRed else: fgWhite),
|
Cell(text: $a.pid, fg: if a.elevated: fgRed else: fgWhite),
|
||||||
a.timeSince(a.latestCheckin)
|
a.timeSince(cq.agents[a.name].latestCheckin)
|
||||||
]
|
]
|
||||||
|
|
||||||
# Highlight agents running within elevated processes
|
# Highlight agents running within elevated processes
|
||||||
|
|||||||
Reference in New Issue
Block a user