findhandle.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <shlwapi.h>
  4. #include <Psapi.h>
  5. #include "findhandle.h"
  6. #include "beacon.h"
  7. #pragma comment(lib, "shlwapi")
  8. //Code from: https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  9. HRESULT BeaconPrintToStreamW(_In_z_ LPCWSTR lpwFormat, ...) {
  10. HRESULT hr = S_FALSE;
  11. va_list argList;
  12. DWORD dwWritten = 0;
  13. if (g_lpStream <= (LPSTREAM)1) {
  14. hr = OLE32$CreateStreamOnHGlobal(NULL, TRUE, &g_lpStream);
  15. if (FAILED(hr)) {
  16. return hr;
  17. }
  18. }
  19. if (g_lpwPrintBuffer <= (LPWSTR)1) {
  20. g_lpwPrintBuffer = (LPWSTR)MSVCRT$calloc(MAX_STRING, sizeof(WCHAR));
  21. if (g_lpwPrintBuffer == NULL) {
  22. hr = E_FAIL;
  23. goto CleanUp;
  24. }
  25. }
  26. va_start(argList, lpwFormat);
  27. if (!MSVCRT$_vsnwprintf_s(g_lpwPrintBuffer, MAX_STRING, MAX_STRING -1, lpwFormat, argList)) {
  28. hr = E_FAIL;
  29. goto CleanUp;
  30. }
  31. if (g_lpStream != NULL) {
  32. if (FAILED(hr = g_lpStream->lpVtbl->Write(g_lpStream, g_lpwPrintBuffer, (ULONG)MSVCRT$wcslen(g_lpwPrintBuffer) * sizeof(WCHAR), &dwWritten))) {
  33. goto CleanUp;
  34. }
  35. }
  36. hr = S_OK;
  37. CleanUp:
  38. if (g_lpwPrintBuffer != NULL) {
  39. MSVCRT$memset(g_lpwPrintBuffer, 0, MAX_STRING * sizeof(WCHAR)); // Clear print buffer.
  40. }
  41. va_end(argList);
  42. return hr;
  43. }
  44. //Code from: https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  45. VOID BeaconOutputStreamW() {
  46. STATSTG ssStreamData = { 0 };
  47. SIZE_T cbSize = 0;
  48. ULONG cbRead = 0;
  49. LARGE_INTEGER pos;
  50. LPWSTR lpwOutput = NULL;
  51. if (FAILED(g_lpStream->lpVtbl->Stat(g_lpStream, &ssStreamData, STATFLAG_NONAME))) {
  52. return;
  53. }
  54. cbSize = ssStreamData.cbSize.LowPart;
  55. lpwOutput = KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, cbSize + 1);
  56. if (lpwOutput != NULL) {
  57. pos.QuadPart = 0;
  58. if (FAILED(g_lpStream->lpVtbl->Seek(g_lpStream, pos, STREAM_SEEK_SET, NULL))) {
  59. goto CleanUp;
  60. }
  61. if (FAILED(g_lpStream->lpVtbl->Read(g_lpStream, lpwOutput, (ULONG)cbSize, &cbRead))) {
  62. goto CleanUp;
  63. }
  64. BeaconPrintf(CALLBACK_OUTPUT, "%ls", lpwOutput);
  65. }
  66. CleanUp:
  67. if (g_lpStream != NULL) {
  68. g_lpStream->lpVtbl->Release(g_lpStream);
  69. g_lpStream = NULL;
  70. }
  71. if (g_lpwPrintBuffer != NULL) {
  72. MSVCRT$free(g_lpwPrintBuffer);
  73. g_lpwPrintBuffer = NULL;
  74. }
  75. if (lpwOutput != NULL) {
  76. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, lpwOutput);
  77. }
  78. return;
  79. }
  80. BOOL GetHandles(int basePid, const BYTE flags, int targetPid) {
  81. NTSTATUS status;
  82. PSYSTEM_HANDLE_INFORMATION handleInfo;
  83. ULONG handleInfoSize = 0x10000;
  84. HANDLE processHandle;
  85. ULONG i;
  86. char procHostName[MAX_PATH];
  87. BOOL foundHandle = FALSE;
  88. if (flags == QUERY_PROC) BeaconPrintToStreamW(L"[+] PROCESS HANDLE RESULTS\n==========================================");
  89. else BeaconPrintToStreamW(L"[+] THREAD HANDLE RESULTS\n==========================================");
  90. NtQuerySystemInformation_t pNtQuerySystemInformation = (NtQuerySystemInformation_t) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQuerySystemInformation");
  91. NtDuplicateObject_t pNtDuplicateObject = (NtDuplicateObject_t) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtDuplicateObject");
  92. NtQueryObject_t pNtQueryObject = (NtQueryObject_t) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQueryObject");
  93. WCHAR Filter[100];
  94. switch(flags) {
  95. case QUERY_PROC: MSVCRT$swprintf_s(Filter, 50, L"%s", L"Process"); break;
  96. default: MSVCRT$swprintf_s(Filter, 50, L"%s", L"Thread"); break;
  97. }
  98. handleInfo = (PSYSTEM_HANDLE_INFORMATION) MSVCRT$malloc(handleInfoSize);
  99. while ((status = pNtQuerySystemInformation(SystemHandleInformation, handleInfo, handleInfoSize, NULL)) == STATUS_INFO_LENGTH_MISMATCH)
  100. handleInfo = (PSYSTEM_HANDLE_INFORMATION)MSVCRT$realloc(handleInfo, handleInfoSize *= 2);
  101. if (status != 0) {
  102. BeaconPrintf(CALLBACK_ERROR, "Failed to retrieve process information!\n");
  103. return 1;
  104. }
  105. for (i = 0 ; i < handleInfo->NumberOfHandles ; i++) {
  106. SYSTEM_HANDLE_TABLE_ENTRY_INFO objHandle = handleInfo->Handles[i];
  107. HANDLE dupHandle = NULL;
  108. POBJECT_TYPE_INFORMATION objectTypeInfo;
  109. PVOID objectNameInfo;
  110. UNICODE_STRING objectName;
  111. ULONG returnLength;
  112. if(objHandle.UniqueProcessId == 4) continue;
  113. if ((basePid != 0) && (objHandle.UniqueProcessId != basePid)) continue;
  114. if (objHandle.UniqueProcessId == KERNEL32$GetCurrentProcessId()) continue;
  115. if (!(processHandle = KERNEL32$OpenProcess(PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION, FALSE, objHandle.UniqueProcessId))) {
  116. continue;
  117. }
  118. KERNEL32$K32GetProcessImageFileNameA(processHandle, procHostName, MAX_PATH);
  119. if (!NT_SUCCESS(pNtDuplicateObject(processHandle, (void *) objHandle.HandleValue, KERNEL32$GetCurrentProcess(), &dupHandle, 0, 0, DUPLICATE_SAME_ACCESS))) {
  120. continue;
  121. }
  122. objectTypeInfo = (POBJECT_TYPE_INFORMATION) MSVCRT$malloc(0x1000);
  123. if (!NT_SUCCESS(pNtQueryObject(dupHandle, ObjectTypeInformation, objectTypeInfo, 0x1000, NULL))) {
  124. KERNEL32$CloseHandle(dupHandle);
  125. continue;
  126. }
  127. if (!SHLWAPI$StrStrIW(Filter, objectTypeInfo->Name.Buffer)) {
  128. MSVCRT$free(objectTypeInfo);
  129. KERNEL32$CloseHandle(dupHandle);
  130. continue;
  131. }
  132. objectNameInfo = MSVCRT$malloc(0x1000);
  133. objectName = *(PUNICODE_STRING) objectNameInfo;
  134. int procID = 0;
  135. if (flags == QUERY_PROC) procID = KERNEL32$GetProcessId(dupHandle);
  136. if (flags == QUERY_THREAD) procID = KERNEL32$GetProcessIdOfThread(dupHandle);
  137. char procNameTemp[MAX_PATH];
  138. if (procID != 0) {
  139. if (flags == QUERY_THREAD) {
  140. HANDLE pH = KERNEL32$OpenProcess(PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION, FALSE, procID);
  141. if (pH) KERNEL32$K32GetProcessImageFileNameA(pH, procNameTemp, MAX_PATH);
  142. else MSVCRT$sprintf_s(procNameTemp, MAX_PATH, "%s", "non existent?");
  143. KERNEL32$CloseHandle(pH);
  144. }
  145. else {
  146. KERNEL32$K32GetProcessImageFileNameA(dupHandle, procNameTemp, MAX_PATH);
  147. }
  148. }
  149. if (targetPid != 0 && targetPid != procID) {
  150. MSVCRT$free(objectTypeInfo);
  151. MSVCRT$free(objectNameInfo);
  152. KERNEL32$CloseHandle(dupHandle);
  153. continue;
  154. }
  155. if(procID != 0 && objHandle.UniqueProcessId != procID) {
  156. WCHAR WprocHostName[100];
  157. WCHAR WprocNameTemp[100];
  158. KERNEL32$MultiByteToWideChar(CP_ACP, 0, SHLWAPI$PathFindFileNameA(procHostName), -1, WprocHostName, 100);
  159. KERNEL32$MultiByteToWideChar(CP_ACP, 0, SHLWAPI$PathFindFileNameA(procNameTemp), -1, WprocNameTemp, 100);
  160. BeaconPrintToStreamW(L"\nHandle from:\t%s [%d]\nHandle to:\t%s [%d]\nHandle object:\t%#x\nAccess rights:\t%#x\n",
  161. WprocHostName,
  162. KERNEL32$GetProcessId(processHandle),
  163. WprocNameTemp,
  164. procID,
  165. objHandle.HandleValue,
  166. objHandle.GrantedAccess); //https://learn.microsoft.com/en-us/windows/win32/procthread/process-security-and-access-rights
  167. foundHandle = TRUE;
  168. }
  169. MSVCRT$free(objectTypeInfo);
  170. MSVCRT$free(objectNameInfo);
  171. KERNEL32$CloseHandle(dupHandle);
  172. }
  173. MSVCRT$free(handleInfo);
  174. KERNEL32$CloseHandle(processHandle);
  175. return foundHandle;
  176. }
  177. int go(char *args, int len) {
  178. int basePid = 0;
  179. int targetPid = 0;
  180. BYTE flags;
  181. CHAR *search;
  182. CHAR *query;
  183. BOOL res = NULL;
  184. datap parser;
  185. BeaconDataParse(&parser, args, len);
  186. search = BeaconDataExtract(&parser, NULL);
  187. query = BeaconDataExtract(&parser, NULL);
  188. if (MSVCRT$strcmp(query, "proc") == 0) flags = QUERY_PROC;
  189. else if (MSVCRT$strcmp(query, "thread") == 0) flags = QUERY_THREAD;
  190. else {
  191. BeaconPrintf(CALLBACK_ERROR, "Please specify either proc (PROCESS_HANDLE) or 2 (THREAD_HANDLE) as handle search options.\n");
  192. return 0;
  193. }
  194. if (MSVCRT$strcmp(search, "all") == 0) {
  195. BeaconPrintf(CALLBACK_OUTPUT, "[*] Start enumerating all processes with handles to all other processes\n");
  196. res = GetHandles(0, flags, 0);
  197. }
  198. else if (MSVCRT$strcmp(search, "h2p") == 0) {
  199. targetPid = BeaconDataInt(&parser);
  200. BeaconPrintf(CALLBACK_OUTPUT, "[*] Start enumerating all processes that have a handle to PID: [%d]\n", targetPid);
  201. res = GetHandles(0, flags, targetPid);
  202. }
  203. else if (MSVCRT$strcmp(search, "p2h") == 0) {
  204. basePid = BeaconDataInt(&parser);
  205. BeaconPrintf(CALLBACK_OUTPUT, "[*] Start enumerating handles from PID [%d] to all other processes\n", basePid);
  206. res = GetHandles(basePid, flags, 0);
  207. }
  208. else {
  209. BeaconPrintf(CALLBACK_ERROR, "Please specify one of the following process search options: ht | h2p | p2h\n");
  210. return 0;
  211. }
  212. if(!res) BeaconPrintf(CALLBACK_ERROR, "No handle found for this search query!\n");
  213. else {
  214. BeaconOutputStreamW();
  215. BeaconPrintf(CALLBACK_OUTPUT, "\n[+] DONE");
  216. }
  217. return 0;
  218. }