Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crypto::cng on Windows 10 x64 1909, 2004, 20H2, 21H2 #362

Merged
merged 7 commits into from
Jul 26, 2022
Merged

Fix crypto::cng on Windows 10 x64 1909, 2004, 20H2, 21H2 #362

merged 7 commits into from
Jul 26, 2022

Conversation

hubert3
Copy link
Contributor

@hubert3 hubert3 commented Jul 21, 2021

Fix crypto::cng on Windows 10 x64 version 1909

Was failing with error:

ERROR kull_m_patch_genericProcessOrServiceFromBuild ; kull_m_patch (0x00000000))

I found that the version of KeyIso service (ncryptprov.dll) on my Win10 x64 1909 needs the patch sequence defined in PTRN_W10_1809_SPCryptExportKey and the existing definition for KULL_M_WIN_BUILD_10_1909 did not work.

Also added a correct patch definition for build KULL_M_WIN_BUILD_10_2004.

crypto::cng now succeeds on both Win10 x64 1909 and 2004 for me. Non-exportable CNG private key export was successfully tested on 1909 with command crypto::certificates /systemstore:local_machine /export

Tested with Win10 Pro x64 Version 1909 Build 18363.1556 (ncryptprov.dll 10.0.18362.1411)
Also works on Win10 Pro x64 Version 2004 Build 19041.804 (ncryptprov.dll 10.0.19041.662)

…nericProcessOrServiceFromBuild ; kull_m_patch (0x00000000))

Non-exportable private key export succeeds with crypto::certificates /systemstore:local_machine /export
Tested with:  Win10 Pro x64 Version 1909 Build 18363.1556 (ncryptprov.dll 10.0.18362.1411)
Also works on Win10 Pro x64 Version 2004 Build 19041.804  (ncryptprov.dll 10.0.19041.662)
@hubert3 hubert3 changed the title Fix crypto::cng on Windows 10 x64 version 1909 (ERROR kull_m_patch_ge… Fix crypto::cng on Windows 10 x64 1909 Jul 21, 2021
@bezik46
Copy link

bezik46 commented Feb 22, 2022

Anybody has version working for Microsoft Windows [Version 10.0.19044.1526] ?

@hubert3 hubert3 changed the title Fix crypto::cng on Windows 10 x64 1909 Fix crypto::cng on Windows 10 x64 1909, 2004, 20H2, 21H2 Jul 26, 2022
@hubert3
Copy link
Contributor Author

hubert3 commented Jul 26, 2022

This pull request should make crypto::cng work on all the Windows 10 x64 versions in the table below

ncryptprov.dll version number was higher on my 20H2 test system than on 21H2 🤷‍♂️

@scerazy @juxeii grateful if you could test, contact me if you need a compiled mimikatz exe

Version Codename Marketing Name Build
1909 19H2 November 2019 Update 18363
2004 20H1 May 2020 Update 19041
20H2 20H2 October 2020 Update 19042
21H2 21H2 November 2021 Update 19044

@gentilkiwi gentilkiwi merged commit f602468 into gentilkiwi:master Jul 26, 2022
@bezik46
Copy link

bezik46 commented Jul 26, 2022

Compiled exe would be appreciated

Thanks

@gentilkiwi
Copy link
Owner

As it was merged, mimikatz has automatic build: https://github.com/gentilkiwi/mimikatz#continuous-integration

You can find binaries in artifacts: https://ci.appveyor.com/project/gentilkiwi/mimikatz

Thank you to @hubert3 for the work :)

@sikhness
Copy link

sikhness commented Sep 8, 2022

Even with the latest binary, this seems to not work on my installation of Windows 10 20H2.

When I run:

crypto::capi
crypto::cng

crypto::certificates /export

I can see that it generates the pfx, however it does not actually contain the private key in it (I know because when I import it, it doesn't exist in the certificate, and also when I use openssl to extract it, it is blank).

Also, when I run:

crypto::capi
crypto::cng

crypto::keys /export

I get a Private export : ERROR kuhl_m_crypto_exportKeyToFile ; NCryptExportKey(PKCS8_PRIVATEKEY -- init): 0xc0000005 error.

I am using the latest build from AppVeyor. How can I get this to work?

@hubert3
Copy link
Contributor Author

hubert3 commented Oct 17, 2022

@sikhness what was the output of running crypto::cng & crypto::capi? Did you run privilege::debug first? It should look like this:

mimikatz # privilege::debug
Privilege '20' OK

mimikatz # crypto::cng
"KeyIso" service patched

mimikatz # crypto::capi
Local CryptoAPI RSA CSP patched
Local CryptoAPI DSS CSP patched

If you are still having an issue, please reply with output of 'version' and 'crypto::certificates' for the item you're trying to export (to confirm your Windows build, type of item and keystore it's using)

@sikhness
Copy link

Hi @hubert3,
Yes I did run privilege::debug first, I have the exact same output as you for the commands you listed:

mimikatz # privilege::debug
Privilege '20' OK

mimikatz # crypto::cng
"KeyIso" service patched

mimikatz # crypto::capi
Local CryptoAPI RSA CSP patched
Local CryptoAPI DSS CSP patched

Here is the output for the version command and crypto::certificates commands as requested (I've hidden any possible identifying information):

mimikatz # version

mimikatz 2.2.0 (arch x64)
Windows NT 10.0 build 19042 (arch x64)
msvc 150030729 207
mimikatz # crypto::certificates
 * System Store  : 'CURRENT_USER' (0x00010000)
 * Store         : 'My'

 1. <<Hidden>>
    Subject  : <<Hidden>>
    Issuer   : <<Hidden>>
    Serial   : <<Hidden>>
    Algorithm: 1.2.840.113549.1.1.1 (RSA)
    Validity : <<Hidden>>
    UPN      : <<Hidden>>
    Hash SHA1: <<Hidden>>
        Key Container  : <<Hidden>>
        Provider       : Microsoft Software Key Storage Provider
        Provider type  : cng (0)
        Type           : CNG Key (0xffffffff)
        |Provider name : Microsoft Software Key Storage Provider
        |Implementation: NCRYPT_IMPL_SOFTWARE_FLAG ;
        Key Container  : <<Hidden>>
        Unique name    : <<Hidden>>
        Algorithm      : RSA
        Key size       : 2048 (0x00000800)
        Export policy  : 00000000 ( )
        Exportable key : NO
        LSA isolation  : NO

Thanks for your help!

@hubert3
Copy link
Contributor Author

hubert3 commented Nov 8, 2022

@sikhness so the item you want to export is definitely using the CNG provider, so crypto::cng is the correct command (crypto::capi is not needed)

crypto::cng patching succeeds, so exporting the private key should work

What exactly is the output when you run:

crypto::certificates /systemstore:current_user /store:my /export

On the lines that say Public export and Private export for the specific item?

Mimikatz will try to dump everything in that key store so make sure you're looking at the output for the specific item you are interested in (there could be errors from other irrelevant items)

If it says Private export : OK - 'blah.pfx' for the cert/key you want then it should have worked - Feel free to contact me by twitter DM if it's not working

@deliciouslytyped
Copy link

deliciouslytyped commented Dec 18, 2022

xref #413 because I found this but not that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants