Skip to content

Instantly share code, notes, and snippets.

@mattifestation
Created March 16, 2017 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattifestation/d678967655491ef400189f928981a844 to your computer and use it in GitHub Desktop.
Save mattifestation/d678967655491ef400189f928981a844 to your computer and use it in GitHub Desktop.
Why are CAT SHA1 hashes different than SHA1 hashes for PE files?
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?
@ionescu007
Copy link

ionescu007 commented Mar 16, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment