finddotnet.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <psapi.h>
  4. #include <shlwapi.h>
  5. #include <strsafe.h>
  6. #include <winternl.h>
  7. #include "beacon.h"
  8. #include "finddotnet.h"
  9. #pragma comment(lib, "ntdll.lib")
  10. #pragma comment(lib, "User32.lib")
  11. #pragma comment(lib, "Shlwapi.lib")
  12. //Code from: https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  13. HRESULT BeaconPrintToStreamW(_In_z_ LPCWSTR lpwFormat, ...) {
  14. HRESULT hr = S_FALSE;
  15. va_list argList;
  16. DWORD dwWritten = 0;
  17. if (g_lpStream <= (LPSTREAM)1) {
  18. hr = OLE32$CreateStreamOnHGlobal(NULL, TRUE, &g_lpStream);
  19. if (FAILED(hr)) {
  20. return hr;
  21. }
  22. }
  23. // For BOF we need to avoid large stack buffers, so put print buffer on heap.
  24. if (g_lpwPrintBuffer <= (LPWSTR)1) { // Allocate once and free in BeaconOutputStreamW.
  25. g_lpwPrintBuffer = (LPWSTR)MSVCRT$calloc(MAX_STRING, sizeof(WCHAR));
  26. if (g_lpwPrintBuffer == NULL) {
  27. hr = E_FAIL;
  28. goto CleanUp;
  29. }
  30. }
  31. va_start(argList, lpwFormat);
  32. if (!MSVCRT$_vsnwprintf_s(g_lpwPrintBuffer, MAX_STRING, MAX_STRING -1, lpwFormat, argList)) {
  33. hr = E_FAIL;
  34. goto CleanUp;
  35. }
  36. if (g_lpStream != NULL) {
  37. if (FAILED(hr = g_lpStream->lpVtbl->Write(g_lpStream, g_lpwPrintBuffer, (ULONG)MSVCRT$wcslen(g_lpwPrintBuffer) * sizeof(WCHAR), &dwWritten))) {
  38. goto CleanUp;
  39. }
  40. }
  41. hr = S_OK;
  42. CleanUp:
  43. if (g_lpwPrintBuffer != NULL) {
  44. MSVCRT$memset(g_lpwPrintBuffer, 0, MAX_STRING * sizeof(WCHAR)); // Clear print buffer.
  45. }
  46. va_end(argList);
  47. return hr;
  48. }
  49. //Code from: https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  50. VOID BeaconOutputStreamW() {
  51. STATSTG ssStreamData = { 0 };
  52. SIZE_T cbSize = 0;
  53. ULONG cbRead = 0;
  54. LARGE_INTEGER pos;
  55. LPWSTR lpwOutput = NULL;
  56. if (FAILED(g_lpStream->lpVtbl->Stat(g_lpStream, &ssStreamData, STATFLAG_NONAME))) {
  57. return;
  58. }
  59. cbSize = ssStreamData.cbSize.LowPart;
  60. lpwOutput = KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, cbSize + 1);
  61. if (lpwOutput != NULL) {
  62. pos.QuadPart = 0;
  63. if (FAILED(g_lpStream->lpVtbl->Seek(g_lpStream, pos, STREAM_SEEK_SET, NULL))) {
  64. goto CleanUp;
  65. }
  66. if (FAILED(g_lpStream->lpVtbl->Read(g_lpStream, lpwOutput, (ULONG)cbSize, &cbRead))) {
  67. goto CleanUp;
  68. }
  69. BeaconPrintf(CALLBACK_OUTPUT, "%ls", lpwOutput);
  70. }
  71. CleanUp:
  72. if (g_lpStream != NULL) {
  73. g_lpStream->lpVtbl->Release(g_lpStream);
  74. g_lpStream = NULL;
  75. }
  76. if (g_lpwPrintBuffer != NULL) {
  77. MSVCRT$free(g_lpwPrintBuffer); // Free print buffer.
  78. g_lpwPrintBuffer = NULL;
  79. }
  80. if (lpwOutput != NULL) {
  81. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, lpwOutput);
  82. }
  83. return;
  84. }
  85. BOOL FindDotNet() {
  86. int p = 0;
  87. int pid = 0;
  88. char psPath[MAX_PATH];
  89. HANDLE currentProc = NULL;
  90. UNICODE_STRING sectionName = { 0 };
  91. WCHAR ProcNumber[30];
  92. OBJECT_ATTRIBUTES objectAttributes;
  93. BOOL dotNetFound = FALSE;
  94. LPCSTR procName;
  95. WCHAR WCprocName[256];
  96. // resolve function addresses
  97. NtGetNextProcess_t pNtGetNextProcess = (NtGetNextProcess_t) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtGetNextProcess");
  98. NtOpenSection_t pNtOpenSection = (NtOpenSection_t) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtOpenSection");
  99. if (pNtGetNextProcess == NULL || pNtOpenSection == NULL) {
  100. BeaconPrintf(CALLBACK_ERROR, "Error resolving native API calls!\n");
  101. return -1;
  102. }
  103. // Most .NET processes have a handle open to a section named \BaseNamedObjects\Cor_Private_IPCBlock(_v4)_<ProcessId>.
  104. WCHAR objPath[] = L"\\BaseNamedObjects\\Cor_Private_IPCBlock_v4_";
  105. sectionName.Buffer = (PWSTR)KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, 500);
  106. BeaconPrintToStreamW(L"\nProcess name\t\t\t\t\t\tPID\n");
  107. BeaconPrintToStreamW(L"=====================================================================\n");
  108. // loop through all processes
  109. while (!pNtGetNextProcess(currentProc, MAXIMUM_ALLOWED, 0, 0, &currentProc)) {
  110. pid = KERNEL32$GetProcessId(currentProc);
  111. if (pid == 0) continue;
  112. // convert INT to WCHAR
  113. USER32$wsprintfW(ProcNumber, L"%d", pid);
  114. // and fill out UNICODE_STRING structure
  115. MSVCRT$memset(sectionName.Buffer, 0, 500);
  116. MSVCRT$memcpy(sectionName.Buffer, objPath, MSVCRT$wcslen(objPath) * 2); // add section name "prefix"
  117. KERNEL32$lstrcatW(sectionName.Buffer, ProcNumber);
  118. sectionName.Length = MSVCRT$wcslen(sectionName.Buffer) * 2; // finally, adjust the string size
  119. sectionName.MaximumLength = sectionName.Length + 1;
  120. // try to open the section - if exists, .NET process is found
  121. InitializeObjectAttributes(&objectAttributes, &sectionName, OBJ_CASE_INSENSITIVE, NULL, NULL);
  122. HANDLE sectionHandle = NULL;
  123. NTSTATUS status = pNtOpenSection(&sectionHandle, SECTION_QUERY, &objectAttributes);
  124. if (NT_SUCCESS(status)) {
  125. KERNEL32$CloseHandle(sectionHandle);
  126. KERNEL32$K32GetProcessImageFileNameA(currentProc, psPath, MAX_PATH);
  127. procName = SHLWAPI$PathFindFileNameA(psPath);
  128. KERNEL32$MultiByteToWideChar(CP_ACP, 0, procName, -1, WCprocName, 256);
  129. BeaconPrintToStreamW(L"%-60s\t%d\n", WCprocName, pid);
  130. dotNetFound = TRUE;
  131. }
  132. }
  133. return dotNetFound;
  134. }
  135. int go(void) {
  136. BOOL res = NULL;
  137. res = FindDotNet();
  138. if(!res) {
  139. BeaconPrintf(CALLBACK_ERROR, "No .NET process found!");
  140. }
  141. else {
  142. //print data to CS console
  143. BeaconOutputStreamW();
  144. }
  145. return 0;
  146. }