Skip to content

Get-AuthenticodeSignature Displaying Inconsistent Behavior Between -FilePath and -Content #18773

Closed

Description

@s-nix

Prerequisites

Steps to reproduce

I am currently experiencing an issue with Get-AuthenticodeSignature where when checking the signature with a direct file path the signature is valid, however when converting the same file into a byte array and attempting to run Get-AuthenticodeSignature against it, it reports no valid signature.

Get-AuthenticodeSignature -FilePath "~\Repos\Strapper\Strapper\Strapper.psm1"
<#
SignerCertificate                         Status               StatusMessage        Path
-----------------                         ------               -------------        ----
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF  Valid                Signature verified.  Strapper.psm1
#>


Get-AuthenticodeSignature -Content (Get-Content -Path "~\Repos\Strapper\Strapper\Strapper.psm1" -AsByteStream) -SourcePathOrExtension psm1
<#
SignerCertificate                         Status               StatusMessage        Path
-----------------                         ------               -------------        ----
                                          NotSigned            The file psm1 is no… psm1
#>

I assumed that this could have been a file extension issue, and it does appear to be replicated with even simple files, but changing the extension doesn't appear to fix the issue:

Get-AuthenticodeSignature -FilePath "~\Downloads\Get-Host.psm1"
<#
SignerCertificate                         Status               StatusMessage        Path
-----------------                         ------               -------------        ----
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF  Valid                Signature verified.  Get-Host.psm1
#>

Get-AuthenticodeSignature -Content (Get-Content "~\Downloads\Get-Host.psm1" -AsByteStream) -SourcePathOrExtension psm1
<#
SignerCertificate                         Status               StatusMessage        Path
-----------------                         ------               -------------        ----
                                          NotSigned            The file psm1 is no… psm1
#>

Get-AuthenticodeSignature -FilePath "~\Downloads\Get-Host.ps1"
<#
SignerCertificate                         Status               StatusMessage        Path
-----------------                         ------               -------------        ----
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF  Valid                Signature verified.  Get-Host.ps1
#>

Get-AuthenticodeSignature -Content (Get-Content "~\Downloads\Get-Host.ps1" -AsByteStream) -SourcePathOrExtension ps1
<#
SignerCertificate                         Status               StatusMessage        Path
-----------------                         ------               -------------        ----
                                          NotSigned            The file ps1 is not… ps1
#>

Get-Host.psm1

Get-Host
# SIG # Begin signature block
# MIInbwYJKoZIhvcN...
# SIG # End signature block

Expected behavior

PS> Get-AuthenticodeSignature -Content (Get-Content -Path "~\Repos\Strapper\Strapper\Strapper.psm1" -AsByteStream) -SourcePathOrExtension psm1
SignerCertificate                         Status               StatusMessage        Path
-----------------                         ------               -------------        ----
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF  Valid                Signature verified.  Strapper.psm1

Actual behavior

PS> Get-AuthenticodeSignature -Content (Get-Content -Path "~\Repos\Strapper\Strapper\Strapper.psm1" -AsByteStream) -SourcePathOrExtension psm1
SignerCertificate                         Status               StatusMessage        Path
-----------------                         ------               -------------        ----
                                          NotSigned            The file psm1 is no… psm1

Error details

No response

Environment data

PS> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.3.0
PSEdition                      Core
GitCommitId                    7.3.0
OS                             Microsoft Windows 10.0.22621
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

image

Activity

  1. added
    Needs-Triage
    on Dec 12, 2022
  2. jborean93 commented on Dec 12, 2022

    @jborean93
    Collaborator

    One problem is that the SourcePathOrExtension extension should include the period, so .ps1 or .psm1.

    The other problem is that the -Content parameter will only reliably work if the file was Unicode/UTF-16 encoded. If it is not then whether it works is dependent on if the raw bytes can do a roundtrip to UTF-16 and back to bytes. I'm unsure why but internally the cmdlet will do this with -Content

    • Take the input bytes and do [System.Text.Encoding]::Unicode.GetString($bytes)
    • Take that string and convert back to bytes with [System.Text.Encoding]::Unicode.GetBytes($string) later on

    As mentioned above this is fine if the raw bytes are UTF-16 encoded already but if it isn't then it depends on whether this roundtrip encoding will break things. Say for example we have an ASCII (or extended ASCII equivalent) encoded file with just aa and another file with aaa. The raw bytes for these are 0x61 0x61 and 0x61 0x61 0x61 respectively. When those bytes are read as Unicode it is read as the string 慡 and 慡� respectively. Finally converting that back to bytes results in 0x61 0x61 and 0x61 0x61 0xFD 0xFF. We can see the aa case was able to roundtrip back normally but aaa was not.

    The -FilePath parameter is not affected by this as the path is given directly to the Win32 API to validate and it just reads the bytes directly.

    TLDR: The fix for this is to not try and convert the raw bytes to a string internally. A workaround is to ensure the file is UTF-16 encoded. I might submit a PR shortly for this. Another workaround is to add 1 extra character somewhere, this won't work if you are using UTF-8 with a BOM though.

  3. ghost added
    In-PR
    on Dec 13, 2022
  4. jborean93 commented on Dec 13, 2022

    @jborean93
    Collaborator

    I've opened #18774 which fixes the problem for me.

  5. s-nix commented on Dec 13, 2022

    @s-nix
    Author

    Thanks Jordan Borean (@jborean93)! It appears that the following works as a workaround as well:

    Not Working

    # ~\Repos\Strapper\Strapper\Strapper.psm1 is UTF-8 encoded (no BOM)
    Get-AuthenticodeSignature `
        -Content ((Get-Content -path "~\Repos\Strapper\Strapper\Strapper.psm1" -AsByteStream)) `
        -SourcePathOrExtension .psm1
    <#
    SignerCertificate                         Status               StatusMessage        Path
    -----------------                         ------               -------------        ----
                                              NotSigned            The file psm1 is no… psm1
    #>

    Working

    # ~\Repos\Strapper\Strapper\Strapper.psm1 is UTF-8 encoded (no BOM)
    Get-AuthenticodeSignature `
        -Content ([System.Text.Encoding]::Unicode.GetBytes((Get-Content -Path "~\Repos\Strapper\Strapper\Strapper.psm1" -Raw))) `
        -SourcePathOrExtension .psm1
    <#
    SignerCertificate                         Status               StatusMessage        Path
    -----------------                         ------               -------------        ----
    FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF  Valid                Signature verified.  Strapper.psm1
    #>
  6. ghost removed
    In-PR
    on Apr 19, 2023
  7. ghost added
    In-PR
    on Apr 20, 2023
  8. ghost removed
    In-PR
    on Apr 25, 2023
  9. ghost added
    Resolution-Fixed
    on Apr 25, 2023
  10. ghost removed
    Needs-Triage
    on Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-Fixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions