Repository navigation
Closed
Labels
Resolution-FixedThe issue is fixed.
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
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 blockExpected 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.psm1Actual 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… psm1Error 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.0Visuals
Metadata
Metadata
Assignees
Labels
Resolution-FixedThe issue is fixed.

Activity
jborean93 commentedon Dec 12, 2022
One problem is that the
SourcePathOrExtensionextension should include the period, so.ps1or.psm1.The other problem is that the
-Contentparameter 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[System.Text.Encoding]::Unicode.GetString($bytes)[System.Text.Encoding]::Unicode.GetBytes($string)later onAs 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
aaand another file withaaa. The raw bytes for these are0x61 0x61and0x61 0x61 0x61respectively. When those bytes are read as Unicode it is read as the string慡and慡�respectively. Finally converting that back to bytes results in0x61 0x61and0x61 0x61 0xFD 0xFF. We can see theaacase was able to roundtrip back normally butaaawas not.The
-FilePathparameter 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.
jborean93 commentedon Dec 13, 2022
I've opened #18774 which fixes the problem for me.
s-nix commentedon Dec 13, 2022
Thanks Jordan Borean (@jborean93)! It appears that the following works as a workaround as well:
Not Working
Working