dellocalcert.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <windows.h>
  2. #include <wincrypt.h>
  3. #include <stdio.h>
  4. #include "dellocalcert.h"
  5. #include "beacon.h"
  6. #pragma comment(lib, "Crypt32.lib")
  7. #pragma comment(lib, "Advapi32.lib")
  8. BOOL deleteCertificateFromRootStore(const char *thumbprint, wchar_t *store) {
  9. BOOL result = FALSE;
  10. HCERTSTORE hStore = NULL;
  11. PCCERT_CONTEXT pCertContext = NULL;
  12. // Open Local Computer store
  13. hStore = CRYPT32$CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, (HCRYPTPROV)NULL, CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_STORE_OPEN_EXISTING_FLAG, store);
  14. if (!hStore) {
  15. BeaconPrintf(CALLBACK_ERROR, "Failed to open specified certificate store.\n");
  16. goto cleanup;
  17. }
  18. // Find the certificate with the matching thumbprint
  19. while (pCertContext = CRYPT32$CertEnumCertificatesInStore(hStore, pCertContext)) {
  20. BYTE certThumbprint[20];
  21. DWORD certThumbprintSize = sizeof(certThumbprint);
  22. CHAR certThumbprintStr[41];
  23. // Get the "Thumbprint" property
  24. if (CRYPT32$CertGetCertificateContextProperty(pCertContext, CERT_SHA1_HASH_PROP_ID, certThumbprint, &certThumbprintSize)) {
  25. for (DWORD i = 0; i < certThumbprintSize; ++i) {
  26. MSVCRT$sprintf(certThumbprintStr + (i * 2), "%02X", certThumbprint[i]);
  27. }
  28. certThumbprintStr[40] = '\0';
  29. // Check if the thumbprint matches
  30. if (MSVCRT$strcmp(certThumbprintStr, thumbprint) == 0) {
  31. break;
  32. }
  33. }
  34. }
  35. if (!pCertContext) {
  36. BeaconPrintf(CALLBACK_ERROR, "Certificate not found in the store based on the provided thumbprint.\n");
  37. goto cleanup;
  38. }
  39. // Delete the certificate from the store
  40. if (!CRYPT32$CertDeleteCertificateFromStore(pCertContext)) {
  41. DWORD dwError = KERNEL32$GetLastError();
  42. BeaconPrintf(CALLBACK_ERROR, "Failed to delete certificate from the store with error code: %x\n", dwError);
  43. goto cleanup;
  44. }
  45. result = TRUE;
  46. BeaconPrintf(CALLBACK_OUTPUT, "[+] Certificate deleted successfully from store!\n");
  47. cleanup:
  48. if (hStore) CRYPT32$CertCloseStore(hStore, 0);
  49. if (pCertContext) CRYPT32$CertFreeCertificateContext(pCertContext);
  50. return result;
  51. }
  52. int go(char *args, int len) {
  53. WCHAR *store = NULL; // Options: ROOT, MY, TRUST, CA, USERDS, AuthRoot, Disallowed
  54. CHAR *thumbprint = NULL; // must be all caps like 8D435430B9A409885ED90B3103F43EB85FCC0969
  55. datap parser;
  56. BeaconDataParse(&parser, args, len);
  57. store = BeaconDataExtract(&parser, NULL);
  58. thumbprint = BeaconDataExtract(&parser, NULL);
  59. if(store != NULL) {
  60. if(thumbprint != NULL) {
  61. deleteCertificateFromRootStore(thumbprint, store);
  62. }
  63. else BeaconPrintf(CALLBACK_ERROR,"Please specify a thumbprint.\n");
  64. }
  65. else BeaconPrintf(CALLBACK_ERROR,"Please specify a store name.\n");
  66. return 0;
  67. }