Created
March 16, 2017 02:34
-
-
Save mattifestation/d678967655491ef400189f928981a844 to your computer and use it in GitHub Desktop.
Why are CAT SHA1 hashes different than SHA1 hashes for PE files?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Install-Module -Name PSScriptAnalyzer -RequiredVersion '1.11.0' -Force | |
$ModuleInfo = Get-Module -ListAvailable -Name PSScriptAnalyzer | ? { $_.Version -eq '1.11.0' } | |
$ModuleDir = Split-Path -Parent $ModuleInfo.Path | |
# C:\Program Files\WindowsPowerShell\Modules\PSScriptAnalyzer\1.11.0 for me | |
$NewtonsoftPath = "$ModuleDir\Newtonsoft.Json.dll" | |
$ManifestPath = "$ModuleDir\PSScriptAnalyzer.psd1" | |
# Requires Win 10 Enterprise to use the ConfigCI cmdlets | |
$ModuleFileInfo = Get-SystemDriver -UserPEs -NoShadowCopy -ScanPath $ModuleDir -PathToCatroot $ModuleDir | |
$NewtonsoftPath, $ManifestPath | % { | |
$CurrentFile = $_ | |
$HashInfo = Get-FileHash -Algorithm SHA1 -Path $CurrentFile | |
$FileInfo = $ModuleFileInfo | ? { ($_.FriendlyName -eq $CurrentFile) -and ($_.UserMode -eq $True) } | |
# Get-FileHash uses the System.Security.Cryptography.SHA1CryptoServiceProvider class to calculate the hash | |
# Get-SystemDriver uses CryptCATAdminAcquireContext2 w/ "SHA1" and CryptCATAdminCalcHashFromFileHandle2 to calculate the hash | |
[PSCustomObject] @{ | |
GetFileHashHash = $HashInfo.Hash | |
GetSystemDriverHash = $FileInfo.Hash | |
Equal = $HashInfo.Hash -eq $FileInfo.Hash | |
FilePath = $CurrentFile | |
} | |
} | |
# Why do the hashes match for PSScriptAnalyzer.psd1 but not Newtonsoft.Json.dll?!? | |
# The hashes appear to differ when the file is a PE (EXE, DLL, etc.). Why is that? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This fact was used about 10-13 years ago (back when Authenticode signatures were all the hotness) to change binaries with overlaid executable data as part of the "Security Directory" which was not subject to the signing check. Certain tools such as WinZip self-extractors for example were then vulnerable to extracting the malicious code or executing the malicious overlaid data.