From adafe4633dbc205d7f9882008d30ae4961e446e9 Mon Sep 17 00:00:00 2001 From: Print3M <92022497+Print3M@users.noreply.github.com> Date: Tue, 19 Dec 2023 23:48:59 +0100 Subject: [PATCH] new --- process_enumeration_snapshot.c | 69 ++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 process_enumeration_snapshot.c diff --git a/process_enumeration_snapshot.c b/process_enumeration_snapshot.c new file mode 100644 index 0000000..b3c1c30 --- /dev/null +++ b/process_enumeration_snapshot.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include + +VOID ToLowercaseW(IN PWCHAR pSrc, OUT PWCHAR pDest) { + for (SIZE_T i = 0; i < wcslen(pSrc); i++) { + pDest[i] = (WCHAR) tolower(pSrc[i]); + pDest[i + 1] = '\0'; + } +} + +BOOL FindProcess(IN CONST PWSTR pProcName, OUT PHANDLE hProc, OUT PROCESSENTRY32* pProcEntry) { + /* + Return: + TRUE - if process has been found and opened (:pProcName and :hProc are populated) + FALSE - if something failed (reading :pProcName and :hProc is undefined behavior) + */ + HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + if (hSnap == INVALID_HANDLE_VALUE) { + printf("[!] CreateToolhelp32Snapshot error: %d \n", GetLastError()); + return FALSE; + } + + + if (!Process32First(hSnap, pProcEntry)) { + printf("[!] Process32First error: %d \n", GetLastError()); + return FALSE; + } + + // Prepare lowercase process name + WCHAR ProcessName[MAX_PATH]; + + do { + ToLowercaseW(pProcEntry->szExeFile, ProcessName); + + printf("Proc: %5d | %ls \n", pProcEntry->th32ProcessID, ProcessName); + + if (wcscmp(ProcessName, pProcName) == 0) { + *hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pProcEntry->th32ProcessID); + if (*hProc == NULL) { + printf("[!] OpenProcess error: %d \n", GetLastError()); + return FALSE; + } + + return TRUE; + } + } while (Process32Next(hSnap, pProcEntry)); + + return FALSE; +} + +INT main() { + HANDLE hProc = NULL; + PROCESSENTRY32 ProcEntry = { + // According to the documentation, this value must be initialized + .dwSize = sizeof(PROCESSENTRY32) + }; + + if (!FindProcess(L"msedge.exe", &hProc, &ProcEntry)) { + printf("[!] FindProcess failed \n"); + } else { + printf("[+] Process opened: (%d) %ls \n", ProcEntry.th32ProcessID, ProcEntry.szExeFile); + } + + // Exit + getchar(); + return 0; +} \ No newline at end of file