Advertisement
Guest User

Untitled

a guest
Jul 27th, 2012
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include <windows.h>
  2. #include <tlhelp32.h>
  3. #include <stdio.h>
  4. #include <wchar.h>
  5.  
  6. DWORD GetProcessIdByName(const wchar_t* name);
  7. void ListProcessThreads(DWORD owner_pid);
  8.  
  9. int main() {
  10.   DWORD pid = GetProcessIdByName(L"Gw2.exe");
  11.   if (pid == 0) return 1;
  12.  
  13.   ListProcessThreads(pid);
  14.   return 0;
  15. }
  16.  
  17. DWORD GetProcessIdByName(const wchar_t* name) {
  18.   HANDLE process_snapshot;
  19.   PROCESSENTRY32 process_entry;
  20.  
  21.   process_snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  22.   if(process_snapshot == INVALID_HANDLE_VALUE) return 0;
  23.  
  24.   process_entry.dwSize = sizeof(PROCESSENTRY32);
  25.  
  26.   if(!Process32First(process_snapshot, &process_entry)) return 0;
  27.  
  28.   do {
  29.     if (wcscmp(process_entry.szExeFile, name) == 0) {
  30.       CloseHandle(process_snapshot);
  31.       return process_entry.th32ProcessID;
  32.     }
  33.   } while(Process32Next( process_snapshot, &process_entry));
  34.  
  35.   CloseHandle(process_snapshot);
  36.   return 0;
  37. }
  38.  
  39. void PrintTIB(DWORD thread_id) {
  40.   HANDLE thread_handle = OpenThread(THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, FALSE, thread_id);
  41.   if (thread_handle == NULL) return;
  42.  
  43.   SuspendThread(thread_handle);
  44.  
  45.   CONTEXT context;
  46.   context.ContextFlags = CONTEXT_SEGMENTS;
  47.   if (!GetThreadContext(thread_handle, &context)) {
  48.     CloseHandle(thread_handle);
  49.     return;
  50.   }
  51.  
  52.   LDT_ENTRY ldtSel;
  53.   if (!GetThreadSelectorEntry(thread_handle, context.SegFs, &ldtSel)) return;
  54.      
  55.   ResumeThread(thread_handle);
  56.  
  57.   DWORD fs_base = (ldtSel.HighWord.Bits.BaseHi << 24 ) | ( ldtSel.HighWord.Bits.BaseMid << 16 ) | ( ldtSel.BaseLow );
  58.   fwprintf(stdout, L"[i] FS:[0] (TIB) is @ 0x%08X\n", fs_base);
  59. }
  60.  
  61. void ListProcessThreads(DWORD owner_pid) {
  62.   HANDLE thread_snapshot = INVALID_HANDLE_VALUE;
  63.   THREADENTRY32 thread_entry;
  64.  
  65.   thread_snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
  66.   if(thread_snapshot == INVALID_HANDLE_VALUE) return;
  67.  
  68.   thread_entry.dwSize = sizeof(THREADENTRY32);
  69.  
  70.   if(!Thread32First(thread_snapshot, &thread_entry )) return;
  71.  
  72.   do {
  73.     if (thread_entry.th32OwnerProcessID == owner_pid) {
  74.       fwprintf(stdout, L"[i] thread_id = %d \n", thread_entry.th32ThreadID);
  75.       PrintTIB(thread_entry.th32ThreadID);
  76.     }
  77.   } while(Thread32Next(thread_snapshot, &thread_entry ));
  78.  
  79.   CloseHandle(thread_snapshot);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement