psremote.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <wtsapi32.h>
  4. #include "beacon.h"
  5. #include "psremote.h"
  6. //https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  7. HRESULT BeaconPrintToStreamW(_In_z_ LPCWSTR lpwFormat, ...) {
  8. HRESULT hr = S_FALSE;
  9. va_list argList;
  10. DWORD dwWritten = 0;
  11. if (g_lpStream <= (LPSTREAM)1) {
  12. hr = OLE32$CreateStreamOnHGlobal(NULL, TRUE, &g_lpStream);
  13. if (FAILED(hr)) {
  14. return hr;
  15. }
  16. }
  17. if (g_lpwPrintBuffer <= (LPWSTR)1) {
  18. g_lpwPrintBuffer = (LPWSTR)MSVCRT$calloc(MAX_STRING, sizeof(WCHAR));
  19. if (g_lpwPrintBuffer == NULL) {
  20. hr = E_FAIL;
  21. goto CleanUp;
  22. }
  23. }
  24. va_start(argList, lpwFormat);
  25. if (!MSVCRT$_vsnwprintf_s(g_lpwPrintBuffer, MAX_STRING, MAX_STRING -1, lpwFormat, argList)) {
  26. hr = E_FAIL;
  27. goto CleanUp;
  28. }
  29. if (g_lpStream != NULL) {
  30. if (FAILED(hr = g_lpStream->lpVtbl->Write(g_lpStream, g_lpwPrintBuffer, (ULONG)MSVCRT$wcslen(g_lpwPrintBuffer) * sizeof(WCHAR), &dwWritten))) {
  31. goto CleanUp;
  32. }
  33. }
  34. hr = S_OK;
  35. CleanUp:
  36. if (g_lpwPrintBuffer != NULL) {
  37. MSVCRT$memset(g_lpwPrintBuffer, 0, MAX_STRING * sizeof(WCHAR));
  38. }
  39. va_end(argList);
  40. return hr;
  41. }
  42. //https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  43. VOID BeaconOutputStreamW() {
  44. STATSTG ssStreamData = { 0 };
  45. SIZE_T cbSize = 0;
  46. ULONG cbRead = 0;
  47. LARGE_INTEGER pos;
  48. LPWSTR lpwOutput = NULL;
  49. if (FAILED(g_lpStream->lpVtbl->Stat(g_lpStream, &ssStreamData, STATFLAG_NONAME))) {
  50. return;
  51. }
  52. cbSize = ssStreamData.cbSize.LowPart;
  53. lpwOutput = KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, cbSize + 1);
  54. if (lpwOutput != NULL) {
  55. pos.QuadPart = 0;
  56. if (FAILED(g_lpStream->lpVtbl->Seek(g_lpStream, pos, STREAM_SEEK_SET, NULL))) {
  57. goto CleanUp;
  58. }
  59. if (FAILED(g_lpStream->lpVtbl->Read(g_lpStream, lpwOutput, (ULONG)cbSize, &cbRead))) {
  60. goto CleanUp;
  61. }
  62. BeaconPrintf(CALLBACK_OUTPUT, "%ls", lpwOutput);
  63. }
  64. CleanUp:
  65. if (g_lpStream != NULL) {
  66. g_lpStream->lpVtbl->Release(g_lpStream);
  67. g_lpStream = NULL;
  68. }
  69. if (g_lpwPrintBuffer != NULL) {
  70. MSVCRT$free(g_lpwPrintBuffer);
  71. g_lpwPrintBuffer = NULL;
  72. }
  73. if (lpwOutput != NULL) {
  74. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, lpwOutput);
  75. }
  76. return;
  77. }
  78. BOOL ListProcesses(HANDLE handleTargetHost) {
  79. WTS_PROCESS_INFOA * proc_info;
  80. DWORD pi_count = 0;
  81. LPSTR procName;
  82. WCHAR WCprocName[256];
  83. BOOL RemoteProc = FALSE;
  84. if (!WTSAPI32$WTSEnumerateProcessesA(handleTargetHost, 0, 1, &proc_info, &pi_count)) {
  85. BeaconPrintf(CALLBACK_ERROR, "Failed to get a valid handle to the specified host!\n");
  86. return RemoteProc;
  87. }
  88. BeaconPrintToStreamW(L"\nProcess name\t\t\t\tPID\t\t\tSessionID\n");
  89. BeaconPrintToStreamW(L"===================================================================================\n");
  90. for (int i = 0 ; i < pi_count ; i++ ) {
  91. procName = proc_info[i].pProcessName;
  92. KERNEL32$MultiByteToWideChar(CP_ACP, 0, procName, -1, WCprocName, 256);
  93. BeaconPrintToStreamW(L"%-40s\t%d\t%23d\n",WCprocName ,proc_info[i].ProcessId ,proc_info[i].SessionId);
  94. RemoteProc = TRUE;
  95. }
  96. WTSAPI32$WTSCloseServer(handleTargetHost);
  97. return RemoteProc;
  98. }
  99. void go(char *args, int len) {
  100. CHAR *hostName;
  101. datap parser;
  102. DWORD argSize = NULL;
  103. HANDLE handleTargetHost = NULL;
  104. BOOL res = NULL;
  105. BeaconDataParse(&parser, args, len);
  106. hostName = BeaconDataExtract(&parser, &argSize);
  107. handleTargetHost = WTSAPI32$WTSOpenServerA(hostName);
  108. res = ListProcesses(handleTargetHost);
  109. if(!res) {
  110. BeaconPrintf(CALLBACK_ERROR, "[-] Couldn't list remote processes. Do you have enough privileges on the remote host?\n");
  111. return 0;
  112. }
  113. else {
  114. BeaconOutputStreamW();
  115. BeaconPrintf(CALLBACK_OUTPUT, "[+] DONE");
  116. }
  117. return 0;
  118. }