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

Get-AuthenticodeSignature: Add embedded cert opt #23821

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jborean93
Copy link
Collaborator

@jborean93 jborean93 commented May 20, 2024

PR Summary

Added the ability to retrieve an embedded authenticode signture of a file that ignores any certificates inside a .cat file. This is done through a new switch parameter -EmbeddedSignature on the Get-AuthenticodeSignature cmdlet.

This is a WIP until I find some time to add some tests. Edit: see #23821 (comment) as to why I cannot add tests

PR Context

Fixes: #23820

PR Checklist

@jborean93 jborean93 changed the title Get-AuthenticodeSignature: Add embedded cert opt [WIP] Get-AuthenticodeSignature: Add embedded cert opt May 20, 2024
{
Signature signature = null;

if (fileContent == null)
if (!embeddedSignatureOnly && fileContent == null)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer simplify logic:

Suggested change
if (!embeddedSignatureOnly && fileContent == null)
if (embeddedSignatureOnly)
{
// WinVerifyTrust APIs
return GetSignatureFromWinVerifyTrust(fileName, fileContent);
}
if (fileContent == null)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would mean calling GetSignatureFromWinVerifyTrust in multiple locations. While this approach is indeed more streamlined, I believe that using a negated boolean check is still quite comprehensible.

@jborean93 jborean93 changed the title [WIP] Get-AuthenticodeSignature: Add embedded cert opt Get-AuthenticodeSignature: Add embedded cert opt May 21, 2024
@jborean93
Copy link
Collaborator Author

I've tried adding tests but I cannot get Windows to see the newly installed .cat file generated in the test as valid. I followed this guide on adding the .cat file to the Windows caroot store and that was able to add the file successfully. I was able to verify that both the embedded signature of my test file and the .cat signature were valid and trusted according to Windows Explorer. Unfortunately it seems like the call to WinVerifyTrust that the catalog extension method calls fails with CERT_E_UNTRUSTEDROOT. Adding a trace for this call I can see it is called with the following data:

- Function: Wintrust.dll!WinVerifyTrust
  Time: 2024-05-21T13:18:30.6104744+10:00
  ThreadId: 14220
  Arguments:
    Hwdn: 0xFFFFFFFFFFFFFFFF - HWND
    ActionId:
      Raw: 0xAB341EF5C0 - PGUID
      Value: f750e6c3-38ee-11d1-85e5-00c04fc295ee
      ID: DRIVER_ACTION_VERIFY
    Data:
      Raw: 0xAB341EF0C0 - PWINTRUST_DATA
      CBStruct: 88
      PolicyCallbackData: 0xAB341EF170 - Pointer
      SIPClientData: 0x00000000 - Pointer
      UIChoice: 0x00000002 - WTD_UI_NONE
      RevocationChecks: 0x00000000 - WTD_REVOKE_NONE
      UnionChoice: 0x00000002 - WTD_CHOICE_CATALOG
      UnionData:
        Raw: 0xAB341EF120 - PWINTRUST_CATALOG_INFO
        CBStruct: 72
        CatalogVersion: 0
        CatalogFilePath:
          Raw: 0xAB341EF5D4 - Pointer
          Value: C:\Windows\system32\CatRoot\{F750E6C3-38EE-11D1-85E5-00C04FC295EE}\PowerShell-AuthenticodeTest-8472624b-d66e-43ef-bfe4-e81ce9095fd4.cat
        MemberTag:
          Raw: 0xAB341EFA70 - Pointer
          Value: '\\?\C:\Users\vagrant-domain\AppData\Local\Temp\script-with-cat.ps1'
        MemberFilePath:
          Raw: 0x00000000 - Pointer
          Value: null
        MemberFile: 0x00000000 - HANDLE
        CalculatedFileHash:
          Size: 20
          Raw: 0x296B7D41730 - Pointer
          Value: B24D16D1E0B673B610A8C641E8AAB74F9E357E86
        CatalogContext: 0x00000000 - PCCTL_CONTEXT
        CatAdmin: 0x00000000 - HCATADMIN
      StateAction: 0x00000001 - WTD_STATEACTION_VERIFY
      WVTStateData: 0x00000000 - HANDLE
      URLReference:
        Raw: 0x00000000 - Pointer
        Value: null
      ProvFlags: 0x00001080 - WTD_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT, WTD_CACHE_ONLY_URL_RETRIEVAL
      UIContext: 0x00000000 - WTD_UICONTEXT_EXECUTE
      SignatureSettings: 0x00000000 - PWINTRUST_SIGNATURE_SETTINGS
- Function: Wintrust.dll!WinVerifyTrust
  Time: 2024-05-21T13:18:30.6241246+10:00
  ThreadId: 14220
  Result: -2146762487
  Info:
    ErrorCode: CERT_E_UNTRUSTEDROOT

The cat filepath, member path, and hash are all correct, it just seems like I'm missing something where it's considering the root is untrusted. My guess is that only WHQL or EV signed files are trusted in the context of WINTRUST_CATALOG_INFO but that is just a guess.

Unfortunately unless someone knows more info as to why this fails and whether there is a workaround I'm not sure there is a good way to test this new functionality. Relying on builtin files will probably be brittle as they are updated/removed in the future so I don't think that's a good idea to add as a test here. I probably shouldn't spend much more time on this but happy to have another look if anyone has any recommendations.

@jborean93 jborean93 marked this pull request as ready for review May 21, 2024 04:48
Added the ability to retrieve an embedded authenticode signture of a
file that ignores any certificates inside a .cat file. This is done
through a new switch parameter -EmbeddedOnly on the
Get-AuthenticodeSignature cmdlet.
@jborean93 jborean93 closed this May 24, 2024
@jborean93 jborean93 reopened this May 24, 2024
Copy link
Contributor

microsoft-github-policy-service bot commented May 24, 2024

📣 Hey @jborean93, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label May 31, 2024
Copy link
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Review - Needed The PR is being reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Get-AuthenticodeSignature needs to use embedded signatures
2 participants