Advertisement
Guest User

GetFinalPathNameByHandle

a guest
Oct 21st, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <tchar.h>
  5.  
  6. #define START_ALLOC                 0x1000
  7. #define STATUS_INFO_LENGTH_MISMATCH 0xC0000004
  8. #define SystemHandleInformation     0x10
  9.  
  10. typedef long(__stdcall *NtQSI)(
  11.     ULONG  SystemInformationClass,
  12.     PVOID  SystemInformation,
  13.     ULONG  SystemInformationLength,
  14.     PULONG ReturnLength
  15.     );
  16.  
  17. typedef struct _SYSTEM_HANDLE_ENTRY {
  18.     ULONG  OwnerPid;
  19.     BYTE   ObjectType;
  20.     BYTE   HandleFlags;
  21.     USHORT HandleValue;
  22.     PVOID  ObjectPointer;
  23.     ACCESS_MASK  AccessMask;
  24. } SYSTEM_HANDLE_ENTRY, *PSYSTEM_HANDLE_ENTRY;
  25.  
  26. int main()
  27. {
  28.     HMODULE hNtDll = NULL;
  29.     NtQSI   pNtQSI = NULL;
  30.     PVOID   pMem = NULL;
  31.     ULONG   allocSize = START_ALLOC;
  32.     ULONG   retVal = 0;
  33.     // --------------------------------
  34.     ULONG   hCount = 0;
  35.     PSYSTEM_HANDLE_ENTRY hFirstEntry = NULL;
  36.     // --------------------------------
  37.     ULONG   i;
  38.  
  39.     hNtDll = LoadLibraryA("NTDLL.dll");
  40.  
  41.     if (!hNtDll)
  42.         return 1;
  43.  
  44.     pNtQSI = (NtQSI)GetProcAddress(hNtDll, "NtQuerySystemInformation");
  45.  
  46.     if (!pNtQSI) {
  47.         FreeLibrary(hNtDll);
  48.         return 2;
  49.     }
  50.  
  51.     pMem = malloc(allocSize);
  52.  
  53.     while (pNtQSI(SystemHandleInformation, pMem, allocSize, &retVal)
  54.         == STATUS_INFO_LENGTH_MISMATCH) {
  55.  
  56.         pMem = realloc(pMem, allocSize *= 2);
  57.     }
  58.  
  59.     hCount = *(ULONG*)pMem;
  60.     hFirstEntry = (PSYSTEM_HANDLE_ENTRY)((PBYTE)pMem + 4);
  61.  
  62.     for (i = 0; i < hCount; ++i)
  63.     if (hFirstEntry[i].ObjectType == 28)
  64.     {
  65.         TCHAR Path[MAX_PATH];
  66.         DWORD dwret = GetFinalPathNameByHandle((HANDLE)hFirstEntry[i].HandleValue, Path, MAX_PATH, VOLUME_NAME_NT);
  67.         _tprintf(TEXT("\nThe final path is: %s\n"), Path);
  68.         //printf("PID: %d\tHandle: %d\r\n", hFirstEntry[i].OwnerPid, hFirstEntry[i].HandleValue);
  69.     }
  70.  
  71.     free(pMem);
  72.     FreeLibrary(hNtDll);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement