findlib.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <tlhelp32.h>
  4. #include <psapi.h>
  5. #include <shlwapi.h>
  6. #include "findlib.h"
  7. #include "beacon.h"
  8. #pragma comment(lib, "ntdll.lib")
  9. #pragma comment(lib, "Shlwapi.lib")
  10. //START TrustedSec BOF print code: https://github.com/trustedsec/CS-Situational-Awareness-BOF/blob/master/src/common/base.c
  11. #ifndef bufsize
  12. #define bufsize 8192
  13. #endif
  14. char *output = 0;
  15. WORD currentoutsize = 0;
  16. HANDLE trash = NULL;
  17. int bofstart();
  18. void internal_printf(const char* format, ...);
  19. void printoutput(BOOL done);
  20. int bofstart() {
  21. output = (char*)MSVCRT$calloc(bufsize, 1);
  22. currentoutsize = 0;
  23. return 1;
  24. }
  25. void internal_printf(const char* format, ...){
  26. int buffersize = 0;
  27. int transfersize = 0;
  28. char * curloc = NULL;
  29. char* intBuffer = NULL;
  30. va_list args;
  31. va_start(args, format);
  32. buffersize = MSVCRT$vsnprintf(NULL, 0, format, args);
  33. va_end(args);
  34. if (buffersize == -1) return;
  35. char* transferBuffer = (char*)KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, bufsize);
  36. intBuffer = (char*)KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, buffersize);
  37. va_start(args, format);
  38. MSVCRT$vsnprintf(intBuffer, buffersize, format, args);
  39. va_end(args);
  40. if(buffersize + currentoutsize < bufsize)
  41. {
  42. MSVCRT$memcpy(output+currentoutsize, intBuffer, buffersize);
  43. currentoutsize += buffersize;
  44. } else {
  45. curloc = intBuffer;
  46. while(buffersize > 0)
  47. {
  48. transfersize = bufsize - currentoutsize;
  49. if(buffersize < transfersize)
  50. {
  51. transfersize = buffersize;
  52. }
  53. MSVCRT$memcpy(output+currentoutsize, curloc, transfersize);
  54. currentoutsize += transfersize;
  55. if(currentoutsize == bufsize)
  56. {
  57. printoutput(FALSE);
  58. }
  59. MSVCRT$memset(transferBuffer, 0, transfersize);
  60. curloc += transfersize;
  61. buffersize -= transfersize;
  62. }
  63. }
  64. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, intBuffer);
  65. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, transferBuffer);
  66. }
  67. void printoutput(BOOL done) {
  68. char * msg = NULL;
  69. BeaconOutput(CALLBACK_OUTPUT, output, currentoutsize);
  70. currentoutsize = 0;
  71. MSVCRT$memset(output, 0, bufsize);
  72. if(done) {MSVCRT$free(output); output=NULL;}
  73. }
  74. //END TrustedSec BOF print code.
  75. BOOL ListModules(int pid, char *targetModName) {
  76. HANDLE hProcess;
  77. MEMORY_BASIC_INFORMATION mbi;
  78. char * base = NULL;
  79. BOOL foundModule = FALSE;
  80. hProcess = KERNEL32$OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
  81. if (hProcess == NULL) return foundModule;
  82. while (KERNEL32$VirtualQueryEx(hProcess, base, &mbi, sizeof(mbi)) == sizeof(MEMORY_BASIC_INFORMATION)) {
  83. char fqModPath[MAX_PATH];
  84. char modName[MAX_PATH];
  85. if(targetModName != NULL) {
  86. // only focus on the base address regions
  87. if ((mbi.AllocationBase == mbi.BaseAddress) && (mbi.AllocationBase != NULL)) {
  88. if (KERNEL32$K32GetModuleBaseNameA(hProcess, (HMODULE) mbi.AllocationBase, (LPSTR) modName, sizeof(modName) / sizeof(TCHAR))) {
  89. if(MSVCRT$strcmp(targetModName, modName) == 0) {
  90. KERNEL32$K32GetModuleFileNameExA(hProcess, (HMODULE) mbi.AllocationBase, (LPSTR) fqModPath, sizeof(fqModPath) / sizeof(TCHAR));
  91. internal_printf("\nModulePath:\t%s\nModuleAddr:\t%#llx\n", fqModPath, mbi.AllocationBase);
  92. foundModule = TRUE;
  93. }
  94. }
  95. }
  96. // check the next region
  97. base += mbi.RegionSize;
  98. }
  99. else {
  100. // only focus on the base address regions
  101. if ((mbi.AllocationBase == mbi.BaseAddress) && (mbi.AllocationBase != NULL)) {
  102. if (KERNEL32$K32GetModuleFileNameExA(hProcess, (HMODULE) mbi.AllocationBase, (LPSTR) fqModPath, sizeof(fqModPath) / sizeof(TCHAR))) {
  103. internal_printf("ModulePath [%#llx]: %s\n", mbi.AllocationBase, fqModPath);
  104. foundModule = TRUE;
  105. }
  106. }
  107. // check the next region
  108. base += mbi.RegionSize;
  109. }
  110. }
  111. KERNEL32$CloseHandle(hProcess);
  112. return foundModule;
  113. }
  114. BOOL FindProcess(char *targetModName) {
  115. int procID = 0;
  116. HANDLE currentProc = NULL;
  117. char procPath[MAX_PATH];
  118. char procName[MAX_PATH];
  119. BOOL foundProc = FALSE;
  120. BOOL res = FALSE;
  121. // resolve function address
  122. NtGetNextProcess_t pNtGetNextProcess = (NtGetNextProcess_t) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtGetNextProcess");
  123. // loop through all processes
  124. while (!pNtGetNextProcess(currentProc, MAXIMUM_ALLOWED, 0, 0, &currentProc)) {
  125. procID = KERNEL32$GetProcessId(currentProc);
  126. if(procID == 4) continue;
  127. if (procID == KERNEL32$GetCurrentProcessId()) continue;
  128. if (procID != 0) foundProc = ListModules(procID, targetModName);
  129. if(foundProc) {
  130. KERNEL32$K32GetProcessImageFileNameA(currentProc, procPath, MAX_PATH);
  131. MSVCRT$strncpy(procName, SHLWAPI$PathFindFileNameA(procPath), MAX_PATH);
  132. internal_printf("ProcName:\t%s\nProcID:\t\t%d\nProcPath:\tC:\%s\n", procName, procID, procPath);
  133. res = TRUE;
  134. }
  135. }
  136. return res;
  137. }
  138. int go(char *args, int len) {
  139. int pid = 0;
  140. BOOL res = NULL;
  141. CHAR *option;
  142. CHAR *targetModName;
  143. datap parser;
  144. BeaconDataParse(&parser, args, len);
  145. option = BeaconDataExtract(&parser, NULL);
  146. if(!bofstart()) return;
  147. if (MSVCRT$strcmp(option, "list") == 0) {
  148. pid = BeaconDataInt(&parser);
  149. BeaconPrintf(CALLBACK_OUTPUT, "[*] Start enumerating loaded modules for PID: %d\n\n", pid);
  150. internal_printf("[+] FOUND MODULES:\n==============================================================\n");
  151. res = ListModules(pid, NULL);
  152. }
  153. else if (MSVCRT$strcmp(option, "search") == 0) {
  154. targetModName = BeaconDataExtract(&parser, NULL);
  155. BeaconPrintf(CALLBACK_OUTPUT, "[*] Start enumerating processes that loaded module: %s\n[!] Can take some time..\n\n", targetModName);
  156. internal_printf("[+] FOUND PROCESSES:\n==============================================================\n");
  157. res = FindProcess(targetModName);
  158. }
  159. else {
  160. BeaconPrintf(CALLBACK_ERROR, "This enumeration option isn't supported. Please specify one of the following enumeration options: search | list\n");
  161. return 0;
  162. }
  163. if(!res) BeaconPrintf(CALLBACK_ERROR, "No modules found for this search query!\n\n");
  164. else {
  165. printoutput(TRUE);
  166. }
  167. return 0;
  168. }