PE-sieve
Scans all running processes. Recognizes and dumps a variety of potentially malicious implants (replaced/implanted PEs, shellcodes, hooks, in-memory patches).
Loading...
Searching...
No Matches
process_util.cpp
Go to the documentation of this file.
1#include "process_util.h"
2#include <iostream>
3
4namespace pesieve {
5 namespace util {
7
12
14 {
15 const char kernel32_dll[] = "kernel32.dll";
16 if (!g_kernel32Hndl) {
18 }
19 if (!g_kernel32Hndl) {
21 }
22 return g_kernel32Hndl;
23 }
24 };
25};
26
27BOOL pesieve::util::is_process_wow64(IN HANDLE processHandle, OUT BOOL* isProcWow64)
28{
29 if (isProcWow64) {
30 (*isProcWow64) = FALSE; //set default output value: FALSE
31 }
32 if (!g_IsWow64Process) {
33 HMODULE kernelLib = get_kernel32_hndl();
34 if (!kernelLib) return FALSE;
35
36 FARPROC procPtr = GetProcAddress(kernelLib, "IsWow64Process");
37 if (!procPtr) return FALSE;
38
39 g_IsWow64Process = (BOOL(WINAPI *)(IN HANDLE, OUT PBOOL))procPtr;
40 }
41 if (!g_IsWow64Process) {
42 return FALSE;
43 }
44 return g_IsWow64Process(processHandle, isProcWow64);
45}
46
47bool pesieve::util::is_process_64bit(IN HANDLE process)
48{
49 BOOL isScanner32bit = TRUE;
50#ifdef _WIN64 //is the scanner 64 bit?
51 isScanner32bit = FALSE;
52#endif
53 BOOL isScannerWow64 = FALSE;
54 pesieve::util::is_process_wow64(GetCurrentProcess(), &isScannerWow64);
55
56 const BOOL isSystem64bit = !isScanner32bit || isScannerWow64;
57 if (!isSystem64bit) {
58 //the system is not 64 bit, so for sure the app is 32 bit
59 return false;
60 }
61
62 BOOL isProcessWow = FALSE;
63 pesieve::util::is_process_wow64(process, &isProcessWow);
64
65 if (isProcessWow) {
66 // the system is 64 bit, and the process runs as Wow64, so it is 32 bit
67 return false;
68 }
69 // the system is 64 bit, and the process runs NOT as Wow64, so it is 64 bit
70 return true;
71}
72
73BOOL pesieve::util::wow64_get_thread_context(IN HANDLE hThread, IN OUT PWOW64_CONTEXT lpContext)
74{
75#ifdef _WIN64
76 if (!g_Wow64GetThreadContext) {
77 HMODULE kernelLib = get_kernel32_hndl();
78 if (!kernelLib) return FALSE;
79
80 FARPROC procPtr = GetProcAddress(get_kernel32_hndl(), "Wow64GetThreadContext");
81 if (!procPtr) return FALSE;
82
83 g_Wow64GetThreadContext = (BOOL(WINAPI*)(IN HANDLE, IN OUT PWOW64_CONTEXT))procPtr;
84 }
85 return g_Wow64GetThreadContext(hThread, lpContext);
86#else
87 return FALSE;
88#endif
89}
90
92{
93 if (!g_Wow64DisableWow64FsRedirection) {
94 HMODULE kernelLib = get_kernel32_hndl();
95 if (!kernelLib) return FALSE;
96
97 FARPROC procPtr = GetProcAddress(kernelLib, "Wow64DisableWow64FsRedirection");
98 if (!procPtr) return FALSE;
99
100 g_Wow64DisableWow64FsRedirection = (BOOL(WINAPI *) (OUT PVOID*))procPtr;
101 }
102 if (!g_Wow64DisableWow64FsRedirection) {
103 return FALSE;
104 }
105 return g_Wow64DisableWow64FsRedirection(OldValue);
106}
107
109{
110 if (!g_Wow64RevertWow64FsRedirection) {
111 HMODULE kernelLib = get_kernel32_hndl();
112 if (!kernelLib) return FALSE;
113
114 FARPROC procPtr = GetProcAddress(kernelLib, "Wow64RevertWow64FsRedirection");
115 if (!procPtr) return FALSE;
116
117 g_Wow64RevertWow64FsRedirection = (BOOL(WINAPI *) (IN PVOID))procPtr;
118 }
119 if (!g_Wow64RevertWow64FsRedirection) {
120 return FALSE;
121 }
122 return g_Wow64RevertWow64FsRedirection(OldValue);
123}
bool is_process_64bit(IN HANDLE process)
BOOL wow64_disable_fs_redirection(OUT PVOID *OldValue)
BOOL is_process_wow64(IN HANDLE processHandle, OUT BOOL *isProcWow64)
IN OUT PWOW64_CONTEXT lpContext
HMODULE get_kernel32_hndl()
BOOL(CALLBACK *_MiniDumpWriteDump)(HANDLE hProcess
HMODULE g_kernel32Hndl
BOOL wow64_get_thread_context(IN HANDLE hThread, IN OUT PWOW64_CONTEXT lpContext)
BOOL wow64_revert_fs_redirection(IN PVOID OldValue)
size_t fill_iat(BYTE *vBuf, size_t vBufSize, IN const peconv::ExportsMapper *exportsMap, IN OUT IATBlock &iat, IN ThunkFoundCallback *callback)
Definition iat_finder.h:31