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
threads_util.cpp
Go to the documentation of this file.
1#include "threads_util.h"
2
3#include <peconv.h>
4#include <tlhelp32.h>
5#include "../utils/ntddk.h"
6#include "custom_buffer.h"
7
8#ifdef _DEBUG
9#include <iostream>
10#endif
11
12
13namespace pesieve {
14 namespace util {
15
16 // Thread info structures:
22
23
25 {
26 static auto mod = GetModuleHandleA("ntdll.dll");
27 if (!mod) return false;
28
29 static auto pNtQueryInformationThread = reinterpret_cast<decltype(&NtQueryInformationThread)>(GetProcAddress(mod, "NtQueryInformationThread"));
30 if (!pNtQueryInformationThread) return false;
31
32 const DWORD thAccess = THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT;
33 HANDLE hThread = OpenThread(thAccess, 0, tid);
34 if (!hThread) {
35 hThread = OpenThread(THREAD_QUERY_INFORMATION, 0, tid);
36 if (!hThread) return false;
37 }
38 bool isOk = false;
39 ULONG returnedLen = 0;
40 LPVOID startAddr = 0;
41 NTSTATUS status = 0;
42 status = pNtQueryInformationThread(hThread, ThreadQuerySetWin32StartAddress, &startAddr, sizeof(LPVOID), &returnedLen);
43 if (status == 0 && returnedLen == sizeof(startAddr)) {
44 info.start_addr = (ULONGLONG)startAddr;
45 isOk = true;
46 }
47 returnedLen = 0;
48 THREAD_LAST_SYSCALL_INFORMATION syscallInfo = { 0 };
49 status = pNtQueryInformationThread(hThread, ThreadLastSystemCall, &syscallInfo, sizeof(syscallInfo), &returnedLen);
50 if (status == 0 && returnedLen == sizeof(syscallInfo)) {
51 info.last_syscall = syscallInfo.SystemCallNumber;
52 isOk = true;
53 }
54 CloseHandle(hThread);
55 return isOk;
56 }
57
58 }; // namespace util
59}; // namespace pesieve
60
61
62bool pesieve::util::query_threads_details(IN OUT std::map<DWORD, pesieve::util::thread_info>& threads_info)
63{
64 for (auto itr = threads_info.begin(); itr != threads_info.end(); ++itr) {
65 pesieve::util::thread_info& info = itr->second;
66 if (!query_thread_details(info.tid, info)) return false;
67 }
68 return true;
69}
70
71bool pesieve::util::fetch_threads_info(IN DWORD pid, OUT std::map<DWORD, thread_info>& threads_info)
72{
73 AutoBuffer bBuf;
74
75 NTSTATUS status = STATUS_UNSUCCESSFUL;
76 while (status != STATUS_SUCCESS) {
77 ULONG ret_len = 0;
78 status = NtQuerySystemInformation(SystemProcessInformation, bBuf.buf, bBuf.buf_size, &ret_len);
79 if (status == STATUS_INFO_LENGTH_MISMATCH) {
80 if (!bBuf.alloc(ret_len)) {
81 return false;
82 }
83 continue; // try again
84 }
85 break; //other error, or success
86 };
87
88 if (status != STATUS_SUCCESS) {
89 return false;
90 }
91
92 bool found = false;
93 SYSTEM_PROCESS_INFORMATION* info = (SYSTEM_PROCESS_INFORMATION*)bBuf.buf;
94 while (info) {
95 if (info->UniqueProcessId == pid) {
96 found = true;
97 break;
98 }
99 if (!info->NextEntryOffset) {
100 break;
101 }
102 size_t record_size = info->NextEntryOffset;
103 if (record_size < sizeof(SYSTEM_PROCESS_INFORMATION)) {
104 // Record size smaller than expected, probably it is an old system that doesn not support the new version of this API
105#ifdef _DEBUG
106 std::cout << "The new version of SYSTEM_PROCESS_INFORMATION is not supported!\n";
107#endif
108 break;
109 }
110 info = (SYSTEM_PROCESS_INFORMATION*)((ULONG_PTR)info + info->NextEntryOffset);
111 if (!peconv::validate_ptr(bBuf.buf, bBuf.buf_size, info, sizeof(SYSTEM_PROCESS_INFORMATION))) {
112 break;
113 }
114 }
115
116 if (!found) {
117 return false;
118 }
119
120 const size_t thread_count = info->NumberOfThreads;
121 for (size_t i = 0; i < thread_count; i++) {
122
123 const DWORD tid = MASK_TO_DWORD((ULONGLONG)info->Threads[i].ClientId.UniqueThread);
124 auto itr = threads_info.find(tid);
125 if (itr == threads_info.end()) {
126 threads_info[tid] = thread_info(tid);
127 }
128 thread_info &threadi = threads_info[tid];
129 threadi.is_extended = true;
130 threadi.ext.sys_start_addr = (ULONG_PTR)info->Threads[i].StartAddress;
131 threadi.ext.state = info->Threads[i].ThreadState;
132 threadi.ext.wait_reason = info->Threads[i].WaitReason;
133 threadi.ext.wait_time = info->Threads[i].WaitTime;
134 }
135 return true;
136}
137
138bool pesieve::util::fetch_threads_by_snapshot(IN DWORD pid, OUT std::map<DWORD, thread_info>& threads_info)
139{
140 HANDLE hThreadSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
141 if (hThreadSnapShot == INVALID_HANDLE_VALUE) {
142 const DWORD err = GetLastError();
143#ifdef _DEBUG
144 std::cerr << "[-] Could not create threads snapshot. Error: " << std::dec << err << std::endl;
145#endif
146 return false;
147 }
148 THREADENTRY32 th32 = { 0 };
149 th32.dwSize = sizeof(THREADENTRY32);
150
151 //check all threads in the process:
152 if (!Thread32First(hThreadSnapShot, &th32)) {
153 CloseHandle(hThreadSnapShot);
154#ifdef _DEBUG
155 std::cerr << "[-] Could not enumerate thread. Error: " << GetLastError() << std::endl;
156#endif
157 return false;
158 }
159 do {
160 if (th32.th32OwnerProcessID != pid) {
161 continue;
162 }
163 const DWORD tid = th32.th32ThreadID;
164 auto itr = threads_info.find(tid);
165 if (itr == threads_info.end()) {
166 threads_info[tid] = thread_info(tid);
167 }
168 } while (Thread32Next(hThreadSnapShot, &th32));
169
170 CloseHandle(hThreadSnapShot);
171 return true;
172}
#define MASK_TO_DWORD(val)
Definition iat_finder.h:9
bool fetch_threads_by_snapshot(IN DWORD pid, OUT std::map< DWORD, thread_info > &threads_info)
NTSTATUS(NTAPI *_RtlCreateProcessReflection)(HANDLE ProcessHandle
bool query_threads_details(IN OUT std::map< DWORD, thread_info > &threads_info)
bool query_thread_details(IN DWORD tid, OUT pesieve::util::thread_info &info)
struct pesieve::util::_THREAD_LAST_SYSCALL_INFORMATION THREAD_LAST_SYSCALL_INFORMATION
struct pesieve::util::_THREAD_LAST_SYSCALL_INFORMATION * PTHREAD_LAST_SYSCALL_INFORMATION
DWORD(__stdcall *_PssCaptureSnapshot)(HANDLE ProcessHandle
bool fetch_threads_info(IN DWORD pid, OUT std::map< DWORD, thread_info > &threads_info)
std::string info()
The string with the basic information about the scanner.
Definition pe_sieve.cpp:274
BYTE * alloc(size_t _buf_size)