Started work on websocket communication: Parsing/Serialization of WebSocket packets.
This commit is contained in:
@@ -1,41 +1,41 @@
|
||||
[Window][Sessions [Table View]]
|
||||
Pos=10,43
|
||||
Size=1533,946
|
||||
Size=2016,548
|
||||
Collapsed=0
|
||||
DockId=0x00000003,0
|
||||
|
||||
[Window][Listeners]
|
||||
Pos=10,43
|
||||
Size=1533,946
|
||||
Pos=10,593
|
||||
Size=2528,804
|
||||
Collapsed=0
|
||||
DockId=0x00000003,1
|
||||
DockId=0x00000006,0
|
||||
|
||||
[Window][Eventlog]
|
||||
Pos=1545,43
|
||||
Size=353,946
|
||||
Pos=2028,43
|
||||
Size=510,548
|
||||
Collapsed=0
|
||||
DockId=0x00000004,0
|
||||
|
||||
[Window][Dear ImGui Demo]
|
||||
Pos=1545,43
|
||||
Size=353,946
|
||||
Pos=2028,43
|
||||
Size=510,548
|
||||
Collapsed=0
|
||||
DockId=0x00000004,1
|
||||
|
||||
[Window][Dockspace]
|
||||
Pos=0,0
|
||||
Size=1908,999
|
||||
Size=2548,1407
|
||||
Collapsed=0
|
||||
|
||||
[Window][[FACEDEAD] bob@LAPTOP-02]
|
||||
Pos=956,326
|
||||
Size=942,663
|
||||
Pos=10,593
|
||||
Size=2528,804
|
||||
Collapsed=0
|
||||
DockId=0x00000005,0
|
||||
DockId=0x00000006,1
|
||||
|
||||
[Window][[C9D8E7F6] charlie@SERVER-03]
|
||||
Pos=10,434
|
||||
Size=1888,555
|
||||
Pos=10,593
|
||||
Size=2528,804
|
||||
Collapsed=0
|
||||
DockId=0x00000006,1
|
||||
|
||||
@@ -45,16 +45,16 @@ Size=400,400
|
||||
Collapsed=0
|
||||
|
||||
[Window][[G1H2I3J5] diana@WORKSTATION-04]
|
||||
Pos=10,434
|
||||
Size=1888,555
|
||||
Pos=10,593
|
||||
Size=2528,804
|
||||
Collapsed=0
|
||||
DockId=0x00000006,0
|
||||
DockId=0x00000006,1
|
||||
|
||||
[Window][[DEADBEEF] alice@DESKTOP-01]
|
||||
Pos=10,402
|
||||
Size=1888,587
|
||||
Pos=10,716
|
||||
Size=2848,969
|
||||
Collapsed=0
|
||||
DockId=0x00000005,1
|
||||
DockId=0x00000006,2
|
||||
|
||||
[Window][Example: Console]
|
||||
Pos=10,572
|
||||
@@ -110,8 +110,8 @@ Size=76,76
|
||||
Collapsed=0
|
||||
|
||||
[Window][Start Listener]
|
||||
Pos=704,387
|
||||
Size=500,225
|
||||
Pos=955,591
|
||||
Size=637,225
|
||||
Collapsed=0
|
||||
|
||||
[Table][0x32886A44,8]
|
||||
@@ -136,9 +136,9 @@ Column 3 Weight=0.9746
|
||||
|
||||
[Docking][Data]
|
||||
DockNode ID=0x00000009 Pos=100,200 Size=754,103 Selected=0x64D005CF
|
||||
DockSpace ID=0x85940918 Window=0x260A4489 Pos=10,43 Size=1888,946 Split=Y
|
||||
DockNode ID=0x00000005 Parent=0x85940918 SizeRef=1888,389 Split=X
|
||||
DockNode ID=0x00000003 Parent=0x00000005 SizeRef=1533,159 CentralNode=1 Selected=0x61E02D75
|
||||
DockNode ID=0x00000004 Parent=0x00000005 SizeRef=353,159 Selected=0x5E5F7166
|
||||
DockNode ID=0x00000006 Parent=0x85940918 SizeRef=1888,555 Selected=0x65D642C0
|
||||
DockSpace ID=0x85940918 Window=0x260A4489 Pos=10,43 Size=2528,1354 Split=Y
|
||||
DockNode ID=0x00000005 Parent=0x85940918 SizeRef=1888,548 Split=X
|
||||
DockNode ID=0x00000003 Parent=0x00000005 SizeRef=1376,159 CentralNode=1 Selected=0x61E02D75
|
||||
DockNode ID=0x00000004 Parent=0x00000005 SizeRef=510,159 Selected=0x5E5F7166
|
||||
DockNode ID=0x00000006 Parent=0x85940918 SizeRef=1888,804 Selected=0x6BE22050
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import tables
|
||||
import whisky
|
||||
import tables, strutils
|
||||
import ./utils/appImGui
|
||||
import ./views/[dockspace, sessions, listeners, eventlog, console]
|
||||
import ../common/[types, utils]
|
||||
import ./websocket
|
||||
|
||||
proc main() =
|
||||
var app = createApp(1024, 800, imnodes = true, title = "Conquest", docking = true)
|
||||
@@ -35,6 +38,10 @@ proc main() =
|
||||
|
||||
let io = igGetIO()
|
||||
|
||||
# Initiate WebSocket connection
|
||||
let ws = newWebSocket("ws://localhost:12345")
|
||||
defer: ws.close()
|
||||
|
||||
# main loop
|
||||
while not app.handle.windowShouldClose:
|
||||
pollEvents()
|
||||
@@ -44,10 +51,24 @@ proc main() =
|
||||
continue
|
||||
newFrame()
|
||||
|
||||
#[
|
||||
WebSocket communication with the team server
|
||||
]#
|
||||
# Continuously send heartbeat messages
|
||||
ws.sendHeartbeat()
|
||||
|
||||
# Receive and parse websocket response message
|
||||
let message = ws.receiveMessage().get()
|
||||
case message.getMessageType()
|
||||
of CLIENT_EVENT_LOG:
|
||||
message.receiveEventlogItem(addr eventlog)
|
||||
else: discard
|
||||
|
||||
|
||||
# Draw/update UI components/views
|
||||
dockspace.draw(addr showConquest, views, addr dockTop, addr dockBottom, addr dockTopLeft, addr dockTopRight)
|
||||
if showSessionsTable: sessionsTable.draw(addr showSessionsTable)
|
||||
if showListeners: listenersTable.draw(addr showListeners)
|
||||
if showListeners: listenersTable.draw(addr showListeners, ws)
|
||||
if showEventlog: eventlog.draw(addr showEventlog)
|
||||
|
||||
# Show console windows
|
||||
@@ -56,7 +77,7 @@ proc main() =
|
||||
if console.showConsole:
|
||||
# Ensure that new console windows are docked to the bottom panel by default
|
||||
igSetNextWindowDockID(dockBottom, ImGuiCond_FirstUseEver.int32)
|
||||
console.draw()
|
||||
console.draw(ws)
|
||||
newConsoleTable[agentId] = console
|
||||
|
||||
# Update the consoles table with only those sessions that have not been closed yet
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import whisky
|
||||
import strformat, strutils, times
|
||||
import imguin/[cimgui, glfw_opengl, simple]
|
||||
import ../utils/[appImGui, colors]
|
||||
@@ -113,11 +114,11 @@ proc callback(data: ptr ImGuiInputTextCallbackData): cint {.cdecl.} =
|
||||
#[
|
||||
API to add new console item
|
||||
]#
|
||||
proc addItem*(component: ConsoleComponent, itemType: LogType, data: string) =
|
||||
proc addItem*(component: ConsoleComponent, itemType: LogType, data: string, timestamp: int64 = now().toTime().toUnix()) =
|
||||
|
||||
for line in data.split("\n"):
|
||||
component.console.items.add(ConsoleItem(
|
||||
timestamp: if itemType == LOG_OUTPUT: 0 else: now().toTime().toUnix(),
|
||||
timestamp: if itemType == LOG_OUTPUT: 0 else: timestamp,
|
||||
itemType: itemType,
|
||||
text: line
|
||||
))
|
||||
@@ -148,7 +149,7 @@ proc print(item: ConsoleItem) =
|
||||
igSameLine(0.0f, 0.0f)
|
||||
igTextUnformatted(item.text.cstring, nil)
|
||||
|
||||
proc draw*(component: ConsoleComponent) =
|
||||
proc draw*(component: ConsoleComponent, ws: WebSocket) =
|
||||
igBegin(fmt"[{component.agent.agentId}] {component.agent.username}@{component.agent.hostname}", addr component.showConsole, 0)
|
||||
defer: igEnd()
|
||||
|
||||
@@ -251,22 +252,25 @@ proc draw*(component: ConsoleComponent) =
|
||||
let inputFlags = ImGuiInputTextFlags_EnterReturnsTrue.int32 or ImGuiInputTextFlags_EscapeClearsAll.int32 or ImGuiInputTextFlags_CallbackHistory.int32 or ImGuiInputTextFlags_CallbackCompletion.int32
|
||||
if igInputText("##Input", addr component.inputBuffer[0], MAX_INPUT_LENGTH, inputFlags, callback, cast[pointer](component)):
|
||||
|
||||
let command = $(addr component.inputBuffer[0]).cstring
|
||||
component.addItem(LOG_COMMAND, command)
|
||||
let command = ($(addr component.inputBuffer[0])).strip()
|
||||
if not command.isEmptyOrWhitespace():
|
||||
|
||||
component.addItem(LOG_COMMAND, command)
|
||||
|
||||
# For testing
|
||||
component.addItem(LOG_ERROR, "error message")
|
||||
component.addItem(LOG_SUCCESS, "success message")
|
||||
component.addItem(LOG_INFO, "info message")
|
||||
component.addItem(LOG_WARNING, "warning message")
|
||||
component.addItem(LOG_OUTPUT, "error message\nLong output\n\tindented output\nasdasd")
|
||||
# For testing
|
||||
# component.addItem(LOG_ERROR, "error message")
|
||||
# component.addItem(LOG_SUCCESS, "success message")
|
||||
# component.addItem(LOG_INFO, "info message")
|
||||
# component.addItem(LOG_WARNING, "warning message")
|
||||
# component.addItem(LOG_OUTPUT, "error message\nLong output\n\tindented output\nasdasd")
|
||||
|
||||
# TODO: Handle command execution
|
||||
# console.handleCommand(command)
|
||||
# TODO: Handle command execution
|
||||
# console.handleCommand(command)
|
||||
ws.send("CMD:" & component.agent.agentId & ":" & command)
|
||||
|
||||
# Add command to console history
|
||||
component.history.add(command)
|
||||
component.historyPosition = -1
|
||||
# Add command to console history
|
||||
component.history.add(command)
|
||||
component.historyPosition = -1
|
||||
|
||||
zeroMem(addr component.inputBuffer[0], MAX_INPUT_LENGTH)
|
||||
focusInput = true
|
||||
|
||||
@@ -4,7 +4,7 @@ import ../utils/[appImGui, colors]
|
||||
import ../../common/types
|
||||
|
||||
type
|
||||
EventlogComponent = ref object of RootObj
|
||||
EventlogComponent* = ref object of RootObj
|
||||
title: string
|
||||
log*: ConsoleItems
|
||||
textSelect: ptr TextSelect
|
||||
@@ -41,11 +41,11 @@ proc Eventlog*(title: string): EventlogComponent =
|
||||
#[
|
||||
API to add new log entry
|
||||
]#
|
||||
proc addItem*(component: EventlogComponent, itemType: LogType, data: string) =
|
||||
proc addItem*(component: EventlogComponent, itemType: LogType, data: string, timestamp: int64 = now().toTime().toUnix()) =
|
||||
|
||||
for line in data.split("\n"):
|
||||
component.log.items.add(ConsoleItem(
|
||||
timestamp: if itemType == LOG_OUTPUT: 0 else: now().toTime().toUnix(),
|
||||
timestamp: if itemType == LOG_OUTPUT: 0 else: timestamp,
|
||||
itemType: itemType,
|
||||
text: line
|
||||
))
|
||||
|
||||
@@ -3,11 +3,12 @@ import imguin/[cimgui, glfw_opengl, simple]
|
||||
import ../utils/appImGui
|
||||
import ../../common/[types, utils]
|
||||
import ./modals/startListener
|
||||
import whisky
|
||||
|
||||
type
|
||||
ListenersTableComponent = ref object of RootObj
|
||||
ListenersTableComponent* = ref object of RootObj
|
||||
title: string
|
||||
listeners: seq[Listener]
|
||||
listeners*: seq[Listener]
|
||||
selection: ptr ImGuiSelectionBasicStorage
|
||||
startListenerModal: ListenerModalComponent
|
||||
|
||||
@@ -33,7 +34,7 @@ proc ListenersTable*(title: string): ListenersTableComponent =
|
||||
result.selection = ImGuiSelectionBasicStorage_ImGuiSelectionBasicStorage()
|
||||
result.startListenerModal = ListenerModal()
|
||||
|
||||
proc draw*(component: ListenersTableComponent, showComponent: ptr bool) =
|
||||
proc draw*(component: ListenersTableComponent, showComponent: ptr bool, ws: WebSocket) =
|
||||
igBegin(component.title, showComponent, 0)
|
||||
defer: igEnd()
|
||||
|
||||
@@ -46,7 +47,7 @@ proc draw*(component: ListenersTableComponent, showComponent: ptr bool) =
|
||||
let listener = component.startListenerModal.draw()
|
||||
if listener != nil:
|
||||
# TODO: Start listener
|
||||
|
||||
ws.send("Starting listener: " & listener.listenerId)
|
||||
component.listeners.add(listener)
|
||||
|
||||
#[
|
||||
|
||||
@@ -6,9 +6,9 @@ import ../utils/appImGui
|
||||
import ../../common/[types, utils]
|
||||
|
||||
type
|
||||
SessionsTableComponent = ref object of RootObj
|
||||
SessionsTableComponent* = ref object of RootObj
|
||||
title: string
|
||||
agents: seq[Agent]
|
||||
agents*: seq[Agent]
|
||||
selection: ptr ImGuiSelectionBasicStorage
|
||||
consoles: ptr Table[string, ConsoleComponent]
|
||||
|
||||
|
||||
135
src/client/websocket.nim
Normal file
135
src/client/websocket.nim
Normal file
@@ -0,0 +1,135 @@
|
||||
import times, tables
|
||||
import ../common/[types, utils, serialize]
|
||||
import views/[sessions, listeners, console, eventlog]
|
||||
import whisky
|
||||
|
||||
#[
|
||||
[ Sending Functions ]
|
||||
Client -> Server
|
||||
- Heartbeat
|
||||
- ListenerStart
|
||||
- ListenerStop
|
||||
- AgentBuild
|
||||
- AgentCommand
|
||||
]#
|
||||
proc sendHeartbeat*(ws: WebSocket) =
|
||||
var packer = Packer.init()
|
||||
|
||||
packer.add(cast[uint8](CLIENT_HEARTBEAT))
|
||||
let data = packer.pack()
|
||||
|
||||
ws.send(Bytes.toString(data), BinaryMessage)
|
||||
|
||||
proc sendStartListener*(ws: WebSocket, listener: Listener) =
|
||||
var packer = Packer.init()
|
||||
|
||||
packer.add(cast[uint8](CLIENT_LISTENER_START))
|
||||
packer.add(string.toUUid(listener.listenerId))
|
||||
packer.addDataWithLengthPrefix(string.toBytes(listener.address))
|
||||
packer.add(cast[uint16](listener.port))
|
||||
packer.add(cast[uint8](listener.protocol))
|
||||
|
||||
let data = packer.pack()
|
||||
|
||||
ws.send(Bytes.toString(data), BinaryMessage)
|
||||
|
||||
proc sendStopListener*(ws: WebSocket, listenerId: string) =
|
||||
var packer = Packer.init()
|
||||
|
||||
packer.add(cast[uint8](CLIENT_LISTENER_STOP))
|
||||
packer.add(string.toUuid(listenerId))
|
||||
let data = packer.pack()
|
||||
|
||||
ws.send(Bytes.toString(data), BinaryMessage)
|
||||
|
||||
proc sendAgentCommand*(ws: WebSocket, agentId: string, command: string) =
|
||||
var packer = Packer.init()
|
||||
|
||||
packer.add(cast[uint8](CLIENT_AGENT_COMMAND))
|
||||
packer.add(string.toUuid(agentId))
|
||||
packer.addDataWithLengthPrefix(string.toBytes(command))
|
||||
let data = packer.pack()
|
||||
|
||||
ws.send(Bytes.toString(data), BinaryMessage)
|
||||
|
||||
proc sendAgentBuild*(ws: WebSocket, listenerId: string, sleepDelay: int, sleepMask: SleepObfuscationTechnique, spoofStack: bool, modules: uint32) =
|
||||
var packer = Packer.init()
|
||||
|
||||
packer.add(cast[uint8](CLIENT_AGENT_BUILD))
|
||||
packer.add(string.toUuid(listenerId))
|
||||
packer.add(cast[uint32](sleepDelay))
|
||||
packer.add(cast[uint8](sleepMask))
|
||||
packer.add(cast[uint8](spoofStack))
|
||||
packer.add(modules)
|
||||
let data = packer.pack()
|
||||
|
||||
ws.send(Bytes.toString(data), BinaryMessage)
|
||||
|
||||
#[
|
||||
[ Retrieval Functions ]
|
||||
Server -> Client
|
||||
]#
|
||||
proc getMessageType*(message: Message): WsMessageAction =
|
||||
var unpacker = Unpacker.init(message.data)
|
||||
return cast[WsMessageAction](unpacker.getUint8())
|
||||
|
||||
proc receiveAgentPayload*(message: Message): seq[byte] =
|
||||
var unpacker = Unpacker.init(message.data)
|
||||
|
||||
discard unpacker.getUint8()
|
||||
return string.toBytes(unpacker.getDataWithLengthPrefix())
|
||||
|
||||
proc receiveAgentConnection*(message: Message, sessions: ptr SessionsTableComponent) =
|
||||
var unpacker = Unpacker.init(message.data)
|
||||
|
||||
discard unpacker.getUint8()
|
||||
let agent = Agent(
|
||||
agentId: Uuid.toString(unpacker.getUint32()),
|
||||
listenerId: Uuid.toString(unpacker.getUint32()),
|
||||
username: unpacker.getDataWithLengthPrefix(),
|
||||
hostname: unpacker.getDataWithLengthPrefix(),
|
||||
domain: unpacker.getDataWithLengthPrefix(),
|
||||
ip: unpacker.getDataWithLengthPrefix(),
|
||||
os: unpacker.getDataWithLengthPrefix(),
|
||||
process: unpacker.getDataWithLengthPrefix(),
|
||||
pid: int(unpacker.getUint32()),
|
||||
elevated: unpacker.getUint8() != 0,
|
||||
sleep: int(unpacker.getUint32()),
|
||||
tasks: @[],
|
||||
firstCheckin: cast[int64](unpacker.getUint32()).fromUnix().utc(),
|
||||
latestCheckin: now(),
|
||||
)
|
||||
|
||||
sessions.agents.add(agent)
|
||||
|
||||
proc receiveAgentCheckin*(message: Message, sessions: ptr SessionsTableComponent)=
|
||||
var unpacker = Unpacker.init(message.data)
|
||||
|
||||
discard unpacker.getUint8()
|
||||
let agentId = Uuid.toString(unpacker.getUint32())
|
||||
let timestamp = cast[int64](unpacker.getUint32())
|
||||
|
||||
# TODO: Update checkin
|
||||
|
||||
proc receiveConsoleItem*(message: Message, consoles: ptr Table[string, ConsoleComponent]) =
|
||||
var unpacker = Unpacker.init(message.data)
|
||||
|
||||
discard unpacker.getUint8()
|
||||
let
|
||||
agentId = Uuid.toString(unpacker.getUint32())
|
||||
logType = cast[LogType](unpacker.getUint8())
|
||||
timestamp = cast[int64](unpacker.getUint32())
|
||||
message = unpacker.getDataWithLengthPrefix()
|
||||
|
||||
consoles[][agentId].addItem(logType, message, timestamp)
|
||||
|
||||
proc receiveEventlogItem*(message: Message, eventlog: ptr EventlogComponent) =
|
||||
var unpacker = Unpacker.init(message.data)
|
||||
|
||||
discard unpacker.getUint8()
|
||||
let
|
||||
logType = cast[LogType](unpacker.getUint8())
|
||||
timestamp = cast[int64](unpacker.getUint32())
|
||||
message = unpacker.getDataWithLengthPrefix()
|
||||
|
||||
eventlog[].addItem(logType, message, timestamp)
|
||||
Reference in New Issue
Block a user