enumrwx.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <strsafe.h>
  4. #include <winternl.h>
  5. #include "beacon.h"
  6. #include "enumrwx.h"
  7. //START TrustedSec BOF print code: https://github.com/trustedsec/CS-Situational-Awareness-BOF/blob/master/src/common/base.c
  8. #ifndef bufsize
  9. #define bufsize 8192
  10. #endif
  11. char *output = 0;
  12. WORD currentoutsize = 0;
  13. HANDLE trash = NULL;
  14. int bofstart();
  15. void internal_printf(const char* format, ...);
  16. void printoutput(BOOL done);
  17. int bofstart() {
  18. output = (char*)MSVCRT$calloc(bufsize, 1);
  19. currentoutsize = 0;
  20. return 1;
  21. }
  22. void internal_printf(const char* format, ...){
  23. int buffersize = 0;
  24. int transfersize = 0;
  25. char * curloc = NULL;
  26. char* intBuffer = NULL;
  27. va_list args;
  28. va_start(args, format);
  29. buffersize = MSVCRT$vsnprintf(NULL, 0, format, args);
  30. va_end(args);
  31. if (buffersize == -1) return;
  32. char* transferBuffer = (char*)KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, bufsize);
  33. intBuffer = (char*)KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, buffersize);
  34. va_start(args, format);
  35. MSVCRT$vsnprintf(intBuffer, buffersize, format, args);
  36. va_end(args);
  37. if(buffersize + currentoutsize < bufsize)
  38. {
  39. MSVCRT$memcpy(output+currentoutsize, intBuffer, buffersize);
  40. currentoutsize += buffersize;
  41. } else {
  42. curloc = intBuffer;
  43. while(buffersize > 0)
  44. {
  45. transfersize = bufsize - currentoutsize;
  46. if(buffersize < transfersize)
  47. {
  48. transfersize = buffersize;
  49. }
  50. MSVCRT$memcpy(output+currentoutsize, curloc, transfersize);
  51. currentoutsize += transfersize;
  52. if(currentoutsize == bufsize)
  53. {
  54. printoutput(FALSE);
  55. }
  56. MSVCRT$memset(transferBuffer, 0, transfersize);
  57. curloc += transfersize;
  58. buffersize -= transfersize;
  59. }
  60. }
  61. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, intBuffer);
  62. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, transferBuffer);
  63. }
  64. void printoutput(BOOL done) {
  65. char * msg = NULL;
  66. BeaconOutput(CALLBACK_OUTPUT, output, currentoutsize);
  67. currentoutsize = 0;
  68. MSVCRT$memset(output, 0, bufsize);
  69. if(done) {MSVCRT$free(output); output=NULL;}
  70. }
  71. //END TrustedSec BOF print code.
  72. BOOL FindRWX(HANDLE hProcess) {
  73. BOOL foundRWX = FALSE;
  74. LPVOID addr = 0;
  75. MEMORY_BASIC_INFORMATION mbi;
  76. mbi.BaseAddress = 0;
  77. mbi.AllocationBase = 0;
  78. mbi.AllocationProtect = 0;
  79. mbi.RegionSize = 0;
  80. mbi.State = 0;
  81. mbi.Protect = 0;
  82. mbi.Type = 0;
  83. internal_printf("\nMemory address\t\t\tByte size\n");
  84. internal_printf("================================================\n");
  85. while (KERNEL32$VirtualQueryEx(hProcess, addr, &mbi, sizeof(mbi))) {
  86. addr = (LPVOID)((DWORD_PTR) mbi.BaseAddress + mbi.RegionSize);
  87. if (mbi.Protect == PAGE_EXECUTE_READWRITE && mbi.State == MEM_COMMIT && mbi.Type == MEM_PRIVATE) {
  88. internal_printf("%#-30llx\t%#7llu\n", mbi.BaseAddress, mbi.RegionSize);
  89. foundRWX = TRUE;
  90. }
  91. }
  92. return foundRWX;
  93. }
  94. void go(char *args, int len) {
  95. int pID = 0;
  96. datap parser;
  97. HANDLE hProcess = NULL;
  98. BOOL res = NULL;
  99. BeaconDataParse(&parser, args, len);
  100. pID = BeaconDataInt(&parser);
  101. if(!bofstart()) return;
  102. hProcess = KERNEL32$OpenProcess(PROCESS_ALL_ACCESS, 0, pID);
  103. if (hProcess == NULL) {
  104. BeaconPrintf(CALLBACK_ERROR, "Error opening remote process or thread!\n");
  105. return -1;
  106. }
  107. res = FindRWX(hProcess);
  108. if(!res) {
  109. BeaconPrintf(CALLBACK_ERROR, "No READ, WRITE, EXECUTE memory region found in the specified process!");
  110. }
  111. else {
  112. printoutput(TRUE);
  113. }
  114. KERNEL32$CloseHandle(hProcess);
  115. return 0;
  116. }