Last active
October 11, 2024 01:28
-
-
Save NtRaiseHardError/df8075c303d89ef9dc6003f5d0171dd1 to your computer and use it in GitHub Desktop.
PoC code to demonstrate clipboard monitoring in Windows using an event-based listener.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PoC code to demonstrate clipboard monitoring in Windows | |
// using an event-based listener. | |
#include <stdio.h> | |
#include <Windows.h> | |
#define CLASS_NAME L"MY_CLASS" | |
#define WINDOW_NAME L"MY_WINDOW" | |
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { | |
HANDLE hClipData = NULL; | |
LPVOID data = NULL; | |
switch (uMsg) { | |
case WM_CLIPBOARDUPDATE: | |
if (IsClipboardFormatAvailable(CF_TEXT)) { | |
if (OpenClipboard(hwnd)) { | |
hClipData = GetClipboardData(CF_TEXT); | |
data = GlobalLock(hClipData); | |
// Print the clipboard data. | |
printf("%s\n\n", (LPSTR)data); | |
GlobalUnlock(hClipData); | |
CloseClipboard(); | |
} | |
} | |
break; | |
default: | |
break; | |
} | |
return DefWindowProc(hwnd, uMsg, wParam, lParam);; | |
} | |
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShow) { | |
WNDCLASSEX wc; | |
HWND hwnd = NULL; | |
// Create console to print clipboard data. | |
AllocConsole(); | |
AttachConsole(GetCurrentProcessId()); | |
freopen("CON", "w", stdout); | |
ZeroMemory(&wc, sizeof(WNDCLASSEX)); | |
wc.cbSize = sizeof(WNDCLASSEX); | |
wc.lpfnWndProc = WindowProc; | |
wc.hInstance = hInstance; | |
wc.lpszClassName = CLASS_NAME; | |
// Create invisible window. | |
RegisterClassEx(&wc); | |
hwnd = CreateWindowEx(0, | |
CLASS_NAME, | |
WINDOW_NAME, | |
0, | |
0, | |
0, | |
0, | |
0, | |
NULL, | |
NULL, | |
hInstance, | |
NULL | |
); | |
// Add listener. | |
AddClipboardFormatListener(hwnd); | |
MSG msg; | |
while (GetMessage(&msg, 0, 0, 0) > 0) { | |
TranslateMessage(&msg); | |
DispatchMessage(&msg); | |
} | |
RemoveClipboardFormatListener(hwnd); | |
FreeConsole(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment