26 static auto mod = GetModuleHandleA(
"ntdll.dll");
27 if (!mod)
return false;
29 static auto pNtQueryInformationThread =
reinterpret_cast<decltype(&NtQueryInformationThread)
>(GetProcAddress(mod,
"NtQueryInformationThread"));
30 if (!pNtQueryInformationThread)
return false;
32 const DWORD thAccess = THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT;
33 HANDLE hThread = OpenThread(thAccess, 0, tid);
35 hThread = OpenThread(THREAD_QUERY_INFORMATION, 0, tid);
36 if (!hThread)
return false;
39 ULONG returnedLen = 0;
42 status = pNtQueryInformationThread(hThread, ThreadQuerySetWin32StartAddress, &startAddr,
sizeof(LPVOID), &returnedLen);
43 if (status == 0 && returnedLen ==
sizeof(startAddr)) {
44 info.start_addr = (ULONGLONG)startAddr;
49 status = pNtQueryInformationThread(hThread, ThreadLastSystemCall, &syscallInfo,
sizeof(syscallInfo), &returnedLen);
50 if (status == 0 && returnedLen ==
sizeof(syscallInfo)) {
75 NTSTATUS status = STATUS_UNSUCCESSFUL;
76 while (status != STATUS_SUCCESS) {
78 status = NtQuerySystemInformation(SystemProcessInformation, bBuf.
buf, bBuf.
buf_size, &ret_len);
79 if (status == STATUS_INFO_LENGTH_MISMATCH) {
80 if (!bBuf.
alloc(ret_len)) {
88 if (status != STATUS_SUCCESS) {
93 SYSTEM_PROCESS_INFORMATION*
info = (SYSTEM_PROCESS_INFORMATION*)bBuf.
buf;
95 if (
info->UniqueProcessId == pid) {
99 if (!
info->NextEntryOffset) {
102 size_t record_size =
info->NextEntryOffset;
103 if (record_size <
sizeof(SYSTEM_PROCESS_INFORMATION)) {
106 std::cout <<
"The new version of SYSTEM_PROCESS_INFORMATION is not supported!\n";
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))) {
120 const size_t thread_count =
info->NumberOfThreads;
121 for (
size_t i = 0; i < thread_count; i++) {
124 auto itr = threads_info.find(tid);
125 if (itr == threads_info.end()) {
140 HANDLE hThreadSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
141 if (hThreadSnapShot == INVALID_HANDLE_VALUE) {
142 const DWORD err = GetLastError();
144 std::cerr <<
"[-] Could not create threads snapshot. Error: " << std::dec << err << std::endl;
148 THREADENTRY32 th32 = { 0 };
149 th32.dwSize =
sizeof(THREADENTRY32);
152 if (!Thread32First(hThreadSnapShot, &th32)) {
153 CloseHandle(hThreadSnapShot);
155 std::cerr <<
"[-] Could not enumerate thread. Error: " << GetLastError() << std::endl;
160 if (th32.th32OwnerProcessID != pid) {
163 const DWORD tid = th32.th32ThreadID;
164 auto itr = threads_info.find(tid);
165 if (itr == threads_info.end()) {
168 }
while (Thread32Next(hThreadSnapShot, &th32));
170 CloseHandle(hThreadSnapShot);