findrwx.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. //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. if (g_lpwPrintBuffer <= (LPWSTR)1) {
  19. g_lpwPrintBuffer = (LPWSTR)MSVCRT$calloc(MAX_STRING, sizeof(WCHAR));
  20. if (g_lpwPrintBuffer == NULL) {
  21. hr = E_FAIL;
  22. goto CleanUp;
  23. }
  24. }
  25. va_start(argList, lpwFormat);
  26. if (!MSVCRT$_vsnwprintf_s(g_lpwPrintBuffer, MAX_STRING, MAX_STRING -1, lpwFormat, argList)) {
  27. hr = E_FAIL;
  28. goto CleanUp;
  29. }
  30. if (g_lpStream != NULL) {
  31. if (FAILED(hr = g_lpStream->lpVtbl->Write(g_lpStream, g_lpwPrintBuffer, (ULONG)MSVCRT$wcslen(g_lpwPrintBuffer) * sizeof(WCHAR), &dwWritten))) {
  32. goto CleanUp;
  33. }
  34. }
  35. hr = S_OK;
  36. CleanUp:
  37. if (g_lpwPrintBuffer != NULL) {
  38. MSVCRT$memset(g_lpwPrintBuffer, 0, MAX_STRING * sizeof(WCHAR)); // Clear print buffer.
  39. }
  40. va_end(argList);
  41. return hr;
  42. }
  43. //https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  44. VOID BeaconOutputStreamW() {
  45. STATSTG ssStreamData = { 0 };
  46. SIZE_T cbSize = 0;
  47. ULONG cbRead = 0;
  48. LARGE_INTEGER pos;
  49. LPWSTR lpwOutput = NULL;
  50. if (FAILED(g_lpStream->lpVtbl->Stat(g_lpStream, &ssStreamData, STATFLAG_NONAME))) {
  51. return;
  52. }
  53. cbSize = ssStreamData.cbSize.LowPart;
  54. lpwOutput = KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, cbSize + 1);
  55. if (lpwOutput != NULL) {
  56. pos.QuadPart = 0;
  57. if (FAILED(g_lpStream->lpVtbl->Seek(g_lpStream, pos, STREAM_SEEK_SET, NULL))) {
  58. goto CleanUp;
  59. }
  60. if (FAILED(g_lpStream->lpVtbl->Read(g_lpStream, lpwOutput, (ULONG)cbSize, &cbRead))) {
  61. goto CleanUp;
  62. }
  63. BeaconPrintf(CALLBACK_OUTPUT, "%ls", lpwOutput);
  64. }
  65. CleanUp:
  66. if (g_lpStream != NULL) {
  67. g_lpStream->lpVtbl->Release(g_lpStream);
  68. g_lpStream = NULL;
  69. }
  70. if (g_lpwPrintBuffer != NULL) {
  71. MSVCRT$free(g_lpwPrintBuffer);
  72. g_lpwPrintBuffer = NULL;
  73. }
  74. if (lpwOutput != NULL) {
  75. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, lpwOutput);
  76. }
  77. return;
  78. }
  79. BOOL FindRWX(HANDLE hProcess) {
  80. BOOL foundRWX = FALSE;
  81. LPVOID addr = 0;
  82. MEMORY_BASIC_INFORMATION mbi;
  83. mbi.BaseAddress = 0;
  84. mbi.AllocationBase = 0;
  85. mbi.AllocationProtect = 0;
  86. mbi.RegionSize = 0;
  87. mbi.State = 0;
  88. mbi.Protect = 0;
  89. mbi.Type = 0;
  90. BeaconPrintToStreamW(L"\nMemory address\t\t\tByte size\n");
  91. BeaconPrintToStreamW(L"================================================\n");
  92. while (KERNEL32$VirtualQueryEx(hProcess, addr, &mbi, sizeof(mbi))) {
  93. addr = (LPVOID)((DWORD_PTR) mbi.BaseAddress + mbi.RegionSize);
  94. if (mbi.Protect == PAGE_EXECUTE_READWRITE && mbi.State == MEM_COMMIT && mbi.Type == MEM_PRIVATE) {
  95. BeaconPrintToStreamW(L"%#-30llx\t%#7llu\n", mbi.BaseAddress, mbi.RegionSize);
  96. foundRWX = TRUE;
  97. }
  98. }
  99. return foundRWX;
  100. }
  101. void go(char *args, int len) {
  102. int pID = 0;
  103. datap parser;
  104. HANDLE hProcess = NULL;
  105. BOOL res = NULL;
  106. BeaconDataParse(&parser, args, len);
  107. pID = BeaconDataInt(&parser);
  108. hProcess = KERNEL32$OpenProcess(PROCESS_ALL_ACCESS, 0, pID);
  109. if (hProcess == NULL) {
  110. BeaconPrintf(CALLBACK_ERROR, "Error opening remote process or thread!\n");
  111. return -1;
  112. }
  113. res = FindRWX(hProcess);
  114. if(!res) {
  115. BeaconPrintf(CALLBACK_ERROR, "No READ, WRITE, EXECUTE memory region found in the specified process!");
  116. }
  117. else {
  118. BeaconOutputStreamW();
  119. BeaconPrintf(CALLBACK_OUTPUT, "\n[+] DONE");
  120. }
  121. KERNEL32$CloseHandle(hProcess);
  122. return 0;
  123. }