findrwx.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <strsafe.h>
  4. #include <winternl.h>
  5. #include "beacon.h"
  6. #include "findrwx.h"
  7. //Code from: https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  8. HRESULT BeaconPrintToStreamW(_In_z_ LPCWSTR lpwFormat, ...) {
  9. HRESULT hr = S_FALSE;
  10. va_list argList;
  11. DWORD dwWritten = 0;
  12. if (g_lpStream <= (LPSTREAM)1) {
  13. hr = OLE32$CreateStreamOnHGlobal(NULL, TRUE, &g_lpStream);
  14. if (FAILED(hr)) {
  15. return hr;
  16. }
  17. }
  18. // For BOF we need to avoid large stack buffers, so put print buffer on heap.
  19. if (g_lpwPrintBuffer <= (LPWSTR)1) { // Allocate once and free in BeaconOutputStreamW.
  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); // Free print buffer.
  73. g_lpwPrintBuffer = NULL;
  74. }
  75. if (lpwOutput != NULL) {
  76. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, lpwOutput);
  77. }
  78. return;
  79. }
  80. //find RWX memory region in found .NET process
  81. BOOL FindRWX(HANDLE hProcess) {
  82. BOOL foundRWX = FALSE;
  83. LPVOID addr = 0;
  84. MEMORY_BASIC_INFORMATION mbi;
  85. mbi.BaseAddress = 0;
  86. mbi.AllocationBase = 0;
  87. mbi.AllocationProtect = 0;
  88. mbi.RegionSize = 0;
  89. mbi.State = 0;
  90. mbi.Protect = 0;
  91. mbi.Type = 0;
  92. BeaconPrintToStreamW(L"\nMemory address\t\t\tByte size\n");
  93. BeaconPrintToStreamW(L"================================================\n");
  94. // query remote process memory information
  95. while (KERNEL32$VirtualQueryEx(hProcess, addr, &mbi, sizeof(mbi))) {
  96. addr = (LPVOID)((DWORD_PTR) mbi.BaseAddress + mbi.RegionSize);
  97. // look for RWX memory regions which are not backed by an image
  98. if (mbi.Protect == PAGE_EXECUTE_READWRITE && mbi.State == MEM_COMMIT && mbi.Type == MEM_PRIVATE) {
  99. BeaconPrintToStreamW(L"%#-30llx\t%#7llu\n", mbi.BaseAddress, mbi.RegionSize);
  100. foundRWX = TRUE;
  101. }
  102. }
  103. return foundRWX;
  104. }
  105. void go(char *args, int len) {
  106. int pID = 0;
  107. datap parser;
  108. HANDLE hProcess = NULL;
  109. BOOL res = NULL;
  110. BeaconDataParse(&parser, args, len);
  111. pID = BeaconDataInt(&parser);
  112. hProcess = KERNEL32$OpenProcess(PROCESS_ALL_ACCESS, 0, pID);
  113. if (hProcess == NULL) {
  114. BeaconPrintf(CALLBACK_ERROR, "Error opening remote process or thread!\n");
  115. return -1;
  116. }
  117. res = FindRWX(hProcess);
  118. if(!res) {
  119. BeaconPrintf(CALLBACK_ERROR, "No READ, WRITE, EXECUTE memory region found in the specified process!");
  120. }
  121. else {
  122. //print data to CS console
  123. BeaconOutputStreamW();
  124. BeaconPrintf(CALLBACK_OUTPUT, "\n[+] DONE");
  125. }
  126. KERNEL32$CloseHandle(hProcess);
  127. return 0;
  128. }