psremote.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <wtsapi32.h>
  4. #include "beacon.h"
  5. #include "psremote.h"
  6. //Code from: 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. // For BOF we need to avoid large stack buffers, so put print buffer on heap.
  18. if (g_lpwPrintBuffer <= (LPWSTR)1) { // Allocate once and free in BeaconOutputStreamW.
  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. //Code from: 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. //BeaconPrintf(CALLBACK_OUTPUT, "DONE"); //DEBUG
  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); // Free print buffer.
  73. g_lpwPrintBuffer = NULL;
  74. }
  75. if (lpwOutput != NULL) {
  76. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, lpwOutput);
  77. }
  78. return;
  79. }
  80. int ListProcesses(HANDLE handleTargetHost) {
  81. WTS_PROCESS_INFOA * proc_info;
  82. DWORD pi_count = 0;
  83. LPSTR procName;
  84. WCHAR WCprocName[256];
  85. if (!WTSAPI32$WTSEnumerateProcessesA(handleTargetHost, 0, 1, &proc_info, &pi_count)) {
  86. BeaconPrintf(CALLBACK_ERROR, "Failed to get a valid handle to the specified host!\n");
  87. return -1;
  88. }
  89. BeaconPrintToStreamW(L"\nProcess name\t\t\t\tPID\t\t\tSessionID\n");
  90. BeaconPrintToStreamW(L"===================================================================================\n");
  91. for (int i = 0 ; i < pi_count ; i++ ) {
  92. procName = proc_info[i].pProcessName;
  93. KERNEL32$MultiByteToWideChar(CP_ACP, 0, procName, -1, WCprocName, 256);
  94. BeaconPrintToStreamW(L"%-40s\t%d\t%23d\n",WCprocName ,proc_info[i].ProcessId ,proc_info[i].SessionId);
  95. }
  96. WTSAPI32$WTSCloseServer(handleTargetHost);
  97. return 0;
  98. }
  99. void go(char *args, int len) {
  100. CHAR *hostName;
  101. datap parser;
  102. DWORD argSize = NULL;
  103. HANDLE handleTargetHost = NULL;
  104. int res;
  105. BeaconDataParse(&parser, args, len);
  106. hostName = BeaconDataExtract(&parser, &argSize);
  107. handleTargetHost = WTSAPI32$WTSOpenServerA(hostName);
  108. res = ListProcesses(handleTargetHost);
  109. //print data to CS console
  110. BeaconOutputStreamW();
  111. return 0;
  112. }