finddotnet.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. if (g_lpwPrintBuffer <= (LPWSTR)1) {
  24. g_lpwPrintBuffer = (LPWSTR)MSVCRT$calloc(MAX_STRING, sizeof(WCHAR));
  25. if (g_lpwPrintBuffer == NULL) {
  26. hr = E_FAIL;
  27. goto CleanUp;
  28. }
  29. }
  30. va_start(argList, lpwFormat);
  31. if (!MSVCRT$_vsnwprintf_s(g_lpwPrintBuffer, MAX_STRING, MAX_STRING -1, lpwFormat, argList)) {
  32. hr = E_FAIL;
  33. goto CleanUp;
  34. }
  35. if (g_lpStream != NULL) {
  36. if (FAILED(hr = g_lpStream->lpVtbl->Write(g_lpStream, g_lpwPrintBuffer, (ULONG)MSVCRT$wcslen(g_lpwPrintBuffer) * sizeof(WCHAR), &dwWritten))) {
  37. goto CleanUp;
  38. }
  39. }
  40. hr = S_OK;
  41. CleanUp:
  42. if (g_lpwPrintBuffer != NULL) {
  43. MSVCRT$memset(g_lpwPrintBuffer, 0, MAX_STRING * sizeof(WCHAR)); // Clear print buffer.
  44. }
  45. va_end(argList);
  46. return hr;
  47. }
  48. //Code from: https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  49. VOID BeaconOutputStreamW() {
  50. STATSTG ssStreamData = { 0 };
  51. SIZE_T cbSize = 0;
  52. ULONG cbRead = 0;
  53. LARGE_INTEGER pos;
  54. LPWSTR lpwOutput = NULL;
  55. if (FAILED(g_lpStream->lpVtbl->Stat(g_lpStream, &ssStreamData, STATFLAG_NONAME))) {
  56. return;
  57. }
  58. cbSize = ssStreamData.cbSize.LowPart;
  59. lpwOutput = KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, cbSize + 1);
  60. if (lpwOutput != NULL) {
  61. pos.QuadPart = 0;
  62. if (FAILED(g_lpStream->lpVtbl->Seek(g_lpStream, pos, STREAM_SEEK_SET, NULL))) {
  63. goto CleanUp;
  64. }
  65. if (FAILED(g_lpStream->lpVtbl->Read(g_lpStream, lpwOutput, (ULONG)cbSize, &cbRead))) {
  66. goto CleanUp;
  67. }
  68. BeaconPrintf(CALLBACK_OUTPUT, "%ls", lpwOutput);
  69. }
  70. CleanUp:
  71. if (g_lpStream != NULL) {
  72. g_lpStream->lpVtbl->Release(g_lpStream);
  73. g_lpStream = NULL;
  74. }
  75. if (g_lpwPrintBuffer != NULL) {
  76. MSVCRT$free(g_lpwPrintBuffer);
  77. g_lpwPrintBuffer = NULL;
  78. }
  79. if (lpwOutput != NULL) {
  80. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, lpwOutput);
  81. }
  82. return;
  83. }
  84. BOOL FindDotNet() {
  85. int p = 0;
  86. int pid = 0;
  87. char psPath[MAX_PATH];
  88. HANDLE currentProc = NULL;
  89. UNICODE_STRING sectionName = { 0 };
  90. WCHAR ProcNumber[30];
  91. OBJECT_ATTRIBUTES objectAttributes;
  92. BOOL dotNetFound = FALSE;
  93. LPCSTR procName;
  94. WCHAR WCprocName[256];
  95. NtGetNextProcess_t pNtGetNextProcess = (NtGetNextProcess_t) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtGetNextProcess");
  96. NtOpenSection_t pNtOpenSection = (NtOpenSection_t) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtOpenSection");
  97. if (pNtGetNextProcess == NULL || pNtOpenSection == NULL) {
  98. BeaconPrintf(CALLBACK_ERROR, "Error resolving native API calls!\n");
  99. return -1;
  100. }
  101. WCHAR objPath[] = L"\\BaseNamedObjects\\Cor_Private_IPCBlock_v4_";
  102. sectionName.Buffer = (PWSTR)KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, 500);
  103. BeaconPrintToStreamW(L"\nProcess name\t\t\t\t\t\tPID\n");
  104. BeaconPrintToStreamW(L"=====================================================================\n");
  105. while (!pNtGetNextProcess(currentProc, MAXIMUM_ALLOWED, 0, 0, &currentProc)) {
  106. pid = KERNEL32$GetProcessId(currentProc);
  107. if (pid == 0) continue;
  108. USER32$wsprintfW(ProcNumber, L"%d", pid);
  109. MSVCRT$memset(sectionName.Buffer, 0, 500);
  110. MSVCRT$memcpy(sectionName.Buffer, objPath, MSVCRT$wcslen(objPath) * 2); // add section name "prefix"
  111. KERNEL32$lstrcatW(sectionName.Buffer, ProcNumber);
  112. sectionName.Length = MSVCRT$wcslen(sectionName.Buffer) * 2; // finally, adjust the string size
  113. sectionName.MaximumLength = sectionName.Length + 1;
  114. InitializeObjectAttributes(&objectAttributes, &sectionName, OBJ_CASE_INSENSITIVE, NULL, NULL);
  115. HANDLE sectionHandle = NULL;
  116. NTSTATUS status = pNtOpenSection(&sectionHandle, SECTION_QUERY, &objectAttributes);
  117. if (NT_SUCCESS(status)) {
  118. KERNEL32$CloseHandle(sectionHandle);
  119. KERNEL32$K32GetProcessImageFileNameA(currentProc, psPath, MAX_PATH);
  120. procName = SHLWAPI$PathFindFileNameA(psPath);
  121. KERNEL32$MultiByteToWideChar(CP_ACP, 0, procName, -1, WCprocName, 256);
  122. BeaconPrintToStreamW(L"%-60s\t%d\n", WCprocName, pid);
  123. dotNetFound = TRUE;
  124. }
  125. }
  126. return dotNetFound;
  127. }
  128. int go(void) {
  129. BOOL res = NULL;
  130. res = FindDotNet();
  131. if(!res) {
  132. BeaconPrintf(CALLBACK_ERROR, "No .NET process found!");
  133. }
  134. else {
  135. BeaconOutputStreamW();
  136. }
  137. return 0;
  138. }