enumlocalcert.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include <windows.h>
  2. #include <wincrypt.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "enumlocalcert.h"
  7. #include "beacon.h"
  8. #pragma comment(lib, "Crypt32.lib")
  9. #pragma comment(lib, "Advapi32.lib")
  10. //https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  11. HRESULT BeaconPrintToStreamW(_In_z_ LPCWSTR lpwFormat, ...) {
  12. HRESULT hr = S_FALSE;
  13. va_list argList;
  14. DWORD dwWritten = 0;
  15. if (g_lpStream <= (LPSTREAM)1) {
  16. hr = OLE32$CreateStreamOnHGlobal(NULL, TRUE, &g_lpStream);
  17. if (FAILED(hr)) {
  18. return hr;
  19. }
  20. }
  21. if (g_lpwPrintBuffer <= (LPWSTR)1) {
  22. g_lpwPrintBuffer = (LPWSTR)MSVCRT$calloc(MAX_STRING, sizeof(WCHAR));
  23. if (g_lpwPrintBuffer == NULL) {
  24. hr = E_FAIL;
  25. goto CleanUp;
  26. }
  27. }
  28. va_start(argList, lpwFormat);
  29. if (!MSVCRT$_vsnwprintf_s(g_lpwPrintBuffer, MAX_STRING, MAX_STRING -1, lpwFormat, argList)) {
  30. hr = E_FAIL;
  31. goto CleanUp;
  32. }
  33. if (g_lpStream != NULL) {
  34. if (FAILED(hr = g_lpStream->lpVtbl->Write(g_lpStream, g_lpwPrintBuffer, (ULONG)MSVCRT$wcslen(g_lpwPrintBuffer) * sizeof(WCHAR), &dwWritten))) {
  35. goto CleanUp;
  36. }
  37. }
  38. hr = S_OK;
  39. CleanUp:
  40. if (g_lpwPrintBuffer != NULL) {
  41. MSVCRT$memset(g_lpwPrintBuffer, 0, MAX_STRING * sizeof(WCHAR));
  42. }
  43. va_end(argList);
  44. return hr;
  45. }
  46. //https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  47. VOID BeaconOutputStreamW() {
  48. STATSTG ssStreamData = { 0 };
  49. SIZE_T cbSize = 0;
  50. ULONG cbRead = 0;
  51. LARGE_INTEGER pos;
  52. LPWSTR lpwOutput = NULL;
  53. if (FAILED(g_lpStream->lpVtbl->Stat(g_lpStream, &ssStreamData, STATFLAG_NONAME))) {
  54. return;
  55. }
  56. cbSize = ssStreamData.cbSize.LowPart;
  57. lpwOutput = KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, cbSize + 1);
  58. if (lpwOutput != NULL) {
  59. pos.QuadPart = 0;
  60. if (FAILED(g_lpStream->lpVtbl->Seek(g_lpStream, pos, STREAM_SEEK_SET, NULL))) {
  61. goto CleanUp;
  62. }
  63. if (FAILED(g_lpStream->lpVtbl->Read(g_lpStream, lpwOutput, (ULONG)cbSize, &cbRead))) {
  64. goto CleanUp;
  65. }
  66. BeaconPrintf(CALLBACK_OUTPUT, "%ls", lpwOutput);
  67. }
  68. CleanUp:
  69. if (g_lpStream != NULL) {
  70. g_lpStream->lpVtbl->Release(g_lpStream);
  71. g_lpStream = NULL;
  72. }
  73. if (g_lpwPrintBuffer != NULL) {
  74. MSVCRT$free(g_lpwPrintBuffer);
  75. g_lpwPrintBuffer = NULL;
  76. }
  77. if (lpwOutput != NULL) {
  78. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, lpwOutput);
  79. }
  80. return;
  81. }
  82. void replace_wchar(LPWSTR str, wchar_t old_char, wchar_t new_char) {
  83. for (size_t i = 0; str[i] != L'\0'; i++) {
  84. if (str[i] == old_char) {
  85. str[i] = new_char;
  86. }
  87. }
  88. }
  89. BOOL printCertProperties(PCCERT_CONTEXT pCertContext) {
  90. LPWSTR pszName = NULL;
  91. DWORD dwSize;
  92. BYTE thumbprint[20];
  93. DWORD thumbprintSize = sizeof(thumbprint);
  94. WCHAR thumbprintStr[41];
  95. // Get the "Issued By" property
  96. if (!CRYPT32$CertGetNameStringW(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, NULL, 0)) return FALSE;
  97. dwSize = CRYPT32$CertGetNameStringW(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, NULL, 0);
  98. pszName = (LPWSTR)KERNEL32$LocalAlloc(LPTR, dwSize * sizeof(wchar_t));
  99. if (!pszName) return FALSE;
  100. if (!CRYPT32$CertGetNameStringW(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, pszName, dwSize)) return FALSE;
  101. BeaconPrintToStreamW(L"\nIssued By: %s\n", pszName);
  102. KERNEL32$LocalFree(pszName);
  103. // Get the "Thumbprint" property
  104. if (CRYPT32$CertGetCertificateContextProperty(pCertContext, CERT_SHA1_HASH_PROP_ID, thumbprint, &thumbprintSize)) {
  105. for (DWORD i = 0; i < thumbprintSize; ++i) {
  106. MSVCRT$_snwprintf_s(thumbprintStr + (i * 2), 3, 2, L"%02X", thumbprint[i]);
  107. }
  108. thumbprintStr[40] = L'\0';
  109. BeaconPrintToStreamW(L"Thumbprint: %s\n", thumbprintStr);
  110. }
  111. else {
  112. BeaconPrintToStreamW(L"Failed to get thumbprint.\n");
  113. }
  114. // Get the "Friendly Name" property
  115. dwSize = 0;
  116. dwSize = CRYPT32$CertGetNameStringW(pCertContext, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, NULL, 0);
  117. if (dwSize == 1) {
  118. BeaconPrintToStreamW(L"Friendly Name: none\n");
  119. }
  120. else
  121. {
  122. pszName = (LPWSTR)KERNEL32$LocalAlloc(LPTR, dwSize * sizeof(wchar_t));
  123. if (!pszName) return FALSE;
  124. if (!CRYPT32$CertGetNameStringW(pCertContext, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, pszName, dwSize)) return FALSE;
  125. replace_wchar(pszName, L'\x2013', L'-');
  126. BeaconPrintToStreamW(L"Friendly Name: %ls\n", pszName);
  127. KERNEL32$LocalFree(pszName);
  128. }
  129. // Get the "Expiration Date" property
  130. SYSTEMTIME stExpirationDate;
  131. KERNEL32$FileTimeToSystemTime(&pCertContext->pCertInfo->NotAfter, &stExpirationDate);
  132. WCHAR szExpirationDate[256];
  133. KERNEL32$GetDateFormatW(LOCALE_USER_DEFAULT, 0, &stExpirationDate, L"yyyy-MM-dd", szExpirationDate, sizeof(szExpirationDate) / sizeof(WCHAR));
  134. BeaconPrintToStreamW(L"Expiration Date: %s\n", szExpirationDate);
  135. // Get the "Intended Purposes" property
  136. PCERT_ENHKEY_USAGE pUsage = NULL;
  137. DWORD dwUsageSize = 0;
  138. if (!CRYPT32$CertGetEnhancedKeyUsage(pCertContext, 0, NULL, &dwUsageSize)) return FALSE;
  139. pUsage = (PCERT_ENHKEY_USAGE)KERNEL32$LocalAlloc(LPTR, dwUsageSize);
  140. if (!pUsage) return FALSE;
  141. if (!CRYPT32$CertGetEnhancedKeyUsage(pCertContext, 0, pUsage, &dwUsageSize)) return FALSE;
  142. BeaconPrintToStreamW(L"Intended Purposes:\n");
  143. for (DWORD i = 0; i < pUsage->cUsageIdentifier; ++i)
  144. {
  145. LPCSTR pszOID = pUsage->rgpszUsageIdentifier[i];
  146. PCCRYPT_OID_INFO pInfo = CRYPT32$CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY, (void*)pszOID, 0);
  147. if (pInfo)
  148. {
  149. BeaconPrintToStreamW(L" - %s (%S)\n", pInfo->pwszName, pszOID);
  150. }
  151. else
  152. {
  153. BeaconPrintToStreamW(L" - Unknown OID: %S\n", pszOID);
  154. }
  155. }
  156. KERNEL32$LocalFree(pUsage);
  157. BeaconPrintToStreamW(L"\n");
  158. return TRUE;
  159. }
  160. int go(char *args, int len) {
  161. BOOL res = NULL;
  162. WCHAR *store; // Options: ROOT, MY, TRUST, CA, USERDS, AuthRoot, Disallowed
  163. HCERTSTORE hStore = NULL;
  164. datap parser;
  165. BeaconDataParse(&parser, args, len);
  166. store = BeaconDataExtract(&parser, NULL);
  167. // Open Local Computer store
  168. hStore = CRYPT32$CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, (HCRYPTPROV)NULL, CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_STORE_OPEN_EXISTING_FLAG, store);
  169. if (!hStore) {
  170. BeaconPrintf(CALLBACK_ERROR, "Failed to open specified certificate store\n");
  171. return 1;
  172. }
  173. PCCERT_CONTEXT pCertContext = NULL;
  174. while (pCertContext = CRYPT32$CertEnumCertificatesInStore(hStore, pCertContext)) {
  175. res = printCertProperties(pCertContext);
  176. }
  177. if(!res) {
  178. BeaconPrintf(CALLBACK_ERROR, "Failed to list certificates in specified store.\n");
  179. return 0;
  180. }
  181. else {
  182. BeaconOutputStreamW();
  183. BeaconPrintf(CALLBACK_OUTPUT, "[+] DONE");
  184. }
  185. if (pCertContext) CRYPT32$CertFreeCertificateContext(pCertContext);
  186. if (hStore) CRYPT32$CertCloseStore(hStore, 0);
  187. return 0;
  188. }