13 BYTE* buffer =
nullptr;
14 ULONG buffer_size = 0;
17 NTSTATUS status = STATUS_UNSUCCESSFUL;
18 while (status != STATUS_SUCCESS) {
19 status = NtQuerySystemInformation(SystemProcessInformation, buffer, buffer_size, &ret_len);
20 if (status == STATUS_INFO_LENGTH_MISMATCH) {
24 buffer = (BYTE*)calloc(ret_len, 1);
28 buffer_size = ret_len;
34 if (status != STATUS_SUCCESS) {
40 SYSTEM_PROCESS_INFORMATION*
info = (SYSTEM_PROCESS_INFORMATION*)buffer;
42 if (
info->UniqueProcessId == pid) {
46 if (!
info->NextEntryOffset) {
49 size_t record_size =
info->NextEntryOffset;
50 if (record_size <
sizeof(SYSTEM_PROCESS_INFORMATION)) {
53 std::cout <<
"The new version of SYSTEM_PROCESS_INFORMATION is not supported!\n";
57 info = (SYSTEM_PROCESS_INFORMATION*)((ULONG_PTR)
info +
info->NextEntryOffset);
58 if (!peconv::validate_ptr(buffer, buffer_size,
info,
sizeof(SYSTEM_PROCESS_INFORMATION))) {
68 size_t thread_count =
info->NumberOfThreads;
69 for (
size_t i = 0; i < thread_count; i++) {
78 threads_info.push_back(threadi);
87 HANDLE hThreadSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
88 if (hThreadSnapShot == INVALID_HANDLE_VALUE) {
89 const DWORD err = GetLastError();
91 std::cerr <<
"[-] Could not create threads snapshot. Error: " << std::dec << err << std::endl;
95 THREADENTRY32 th32 = { 0 };
96 th32.dwSize =
sizeof(THREADENTRY32);
99 if (!Thread32First(hThreadSnapShot, &th32)) {
100 CloseHandle(hThreadSnapShot);
102 std::cerr <<
"[-] Could not enumerate thread. Error: " << GetLastError() << std::endl;
107 if (th32.th32OwnerProcessID != pid) {
112 threadi.
tid = th32.th32ThreadID;
114 threads_info.push_back(threadi);
116 }
while (Thread32Next(hThreadSnapShot, &th32));
118 CloseHandle(hThreadSnapShot);