Skip to content

Instantly share code, notes, and snippets.

@jborean93
Created May 21, 2024 04:53
Show Gist options
  • Save jborean93/0cfd53e287aab39377b5126419054604 to your computer and use it in GitHub Desktop.
Save jborean93/0cfd53e287aab39377b5126419054604 to your computer and use it in GitHub Desktop.
Test code to testing WinVerifyTrust with a catalog file.
ctypes_struct WINTRUST_DATA {
[int]$cbStruct
[IntPtr]$pPolicyCallbackData
[IntPtr]$pSIPClientData
[int]$dwUIChoice
[int]$fdwRevocationChecks
[int]$dwUnionChoice
[IntPtr]$pCatalog
[int]$dwStateAction
[IntPtr]$hWVTStateData
[IntPtr]$pwszURLReference
[int]$dwProvFlags
[int]$dwUIContext
[IntPtr]$pSignatureSettings
}
ctypes_struct WINTRUST_CATALOG_INFO {
[int]$cbStruct
[int]$dwCatalogVersion
[MarshalAs('LPWStr')]
[string]$pcwszCatalogFilePath
[MarshalAs('LPWStr')]
[string]$pcwszMemberTag
[IntPtr]$pcwszMemberFilePath
[IntPtr]$hMemberFile
[IntPtr]$pbCalculatedFileHash
[int]$cbCalculatedFileHash
[IntPtr]$pcCatalogContext
[IntPtr]$hCatAdmin
}
$DRIVER_ACTION_VERIFY = [Guid]::new('f750e6c3-38ee-11d1-85e5-00c04fc295ee')
$WINTRUST_ACTION_GENERIC_VERIFY_V2 = [Guid]::new('00AAC56B-CD44-11d0-8CC2-00C04FC295EE')
$fileHash = [Convert]::FromHexString('B24D16D1E0B673B610A8C641E8AAB74F9E357E86')
$catPath = 'C:\Windows\system32\CatRoot\{F750E6C3-38EE-11D1-85E5-00C04FC295EE}\PowerShell-AuthenticodeTest-8472624b-d66e-43ef-bfe4-e81ce9095fd4.cat'
$filePath = '\\?\C:\Users\vagrant-domain\AppData\Local\Temp\script-with-cat.ps1'
$wintrust = New-CtypesLib Wintrust.dll
$hashBuffer = $catInfoBuffer = [IntPtr]::Zero
try {
$hashBuffer = [System.Runtime.InteropServices.Marshal]::AllocHGlobal($fileHash.Length)
[System.Runtime.InteropServices.Marshal]::Copy($fileHash, 0, $hashBuffer, $fileHash.Length)
$catInfo = [WINTRUST_CATALOG_INFO]@{
cbStruct = [System.Runtime.InteropServices.Marshal]::SizeOf[WINTRUST_CATALOG_INFO]()
dwCatalogVersion = 0
pcwszCatalogFilePath = $catPath
pcwszMemberTag = $filePath
pcwszMemberFilePath = [IntPtr]::Zero
hMemberFile = [System.IntPtr]::Zero
pbCalculatedFileHash = $hashBuffer
cbCalculatedFileHash = $fileHash.Length
pcCatalogContext = [System.IntPtr]::Zero
hCatAdmin = [System.IntPtr]::Zero
}
$catInfoBuffer = [System.Runtime.InteropServices.Marshal]::AllocHGlobal($catInfo.cbStruct)
[System.Runtime.InteropServices.Marshal]::StructureToPtr[WINTRUST_CATALOG_INFO]($catInfo, $catInfoBuffer, $false)
$trustData = [WINTRUST_DATA]@{
cbStruct = [System.Runtime.InteropServices.Marshal]::SizeOf[WINTRUST_DATA]()
dwUIChoice = 2 # WTD_UI_NONE
dwUnionChoice = 2 # WTD_CHOICE_CATALOG
pCatalog = $catInfoBuffer
dwStateAction = 1 # WTD_STATEACTION_VERIFY
dwProvFlags = 0x00001080 # WTD_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT, WTD_CACHE_ONLY_URL_RETRIEVAL
}
$wintrust.WinVerifyTrust($null, [ref]$DRIVER_ACTION_VERIFY, [ref]$trustData)
$wintrust.WinVerifyTrust($null, [ref]$WINTRUST_ACTION_GENERIC_VERIFY_V2, [ref]$trustData)
}
finally {
if ($hashBuffer -ne [IntPtr]::Zero) {
[System.Runtime.InteropServices.Marshal]::FreeHGlobal($hashBuffer)
}
if ($catInfoBuffer -ne [IntPtr]::Zero) {
[System.Runtime.InteropServices.Marshal]::FreeHGlobal($catInfoBuffer)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment