Skip to content
Caleb SmithCyberSecurityWithSmitti
Browse PowerShell example contentPowerShell example

Bounded file hash and signing inventory

Collect SHA-256 and Authenticode information for selected top-level executable/script files.

How to use it

.\Get-SocFileTrustInventory.ps1 -Folder 'C:\Lab' -MaxFiles 100 -MaxFileMB 64

Platform and permissions

Windows; file-read permission. Windows trust checks can consult configured certificate services.

What changes / what is read

Reads matching files; no file is executed, quarantined or uploaded by the script.

Scope and interpretation

No recursion; enumeration order and size/file caps limit coverage. Reads are not atomic. Valid signatures and known hashes require context and trusted reference provenance.

Practice task

Use harmless local files and explain each skipped/error row. Avoid uploading reports containing private paths.

Reference

Microsoft documentation. Review the documentation for your installed version and use only an authorised learning or investigation environment.

This is personal educational material. Review the complete code and your signing policy before use. PowerShell code is displayed and downloaded as text; WordPress does not run it. Collection caps limit rows/files, not every provider’s wall-clock execution time.

PowerShell · PowerShell 5.1+ syntax target; Windows collectors require the stated Windows modules. Full Windows acceptance testing remains required.Download .ps1
#requires -Version 5.1
[CmdletBinding()]
param([Parameter(Mandatory)][string]$Folder,[ValidateRange(1,1000)][int]$MaxFiles=100,[ValidateRange(1,1024)][int]$MaxFileMB=64)
if($env:OS -ne 'Windows_NT'){throw 'Authenticode inspection in this collector requires Windows.'}
$dir=Get-Item -LiteralPath $Folder -ErrorAction Stop
if(!$dir.PSIsContainer -or ($dir.Attributes -band [IO.FileAttributes]::ReparsePoint)){throw 'Choose a non-reparse-point directory.'}
$files=@(Get-ChildItem -LiteralPath $dir.FullName -File -Force -ErrorAction Stop | Where-Object Extension -in '.ps1','.exe','.dll','.sys' | Select-Object -First ($MaxFiles+1))
$rows=@(foreach($file in ($files | Select-Object -First $MaxFiles)){
    $hash=$null;$status=$null;$signer=$null;$errorText=$null
    if($file.Attributes -band [IO.FileAttributes]::ReparsePoint){$errorText='Skipped reparse point'}
    elseif($file.Length -gt ($MaxFileMB*1MB)){$errorText='Skipped: file size cap'}
    else {
        try {
            $hash=(Get-FileHash -LiteralPath $file.FullName -Algorithm SHA256 -ErrorAction Stop).Hash
            $sig=Get-AuthenticodeSignature -LiteralPath $file.FullName -ErrorAction Stop
            $status=[string]$sig.Status
            if($sig.SignerCertificate){$signer=$sig.SignerCertificate.Subject}
        } catch {$errorText=$_.Exception.Message}
    }
    [pscustomobject]@{Path=$file.FullName;Length=$file.Length;LastWriteUtc=$file.LastWriteTimeUtc;SHA256=$hash;SignatureStatus=$status;SignerSubject=$signer;Error=$errorText}
})
[pscustomobject]@{CollectedUtc=[datetime]::UtcNow;Files=$rows;ReachedFileCap=($files.Count -gt $MaxFiles);Note='Top-level files only, in enumeration order. Hash/signature reads are not atomic; a changing file can invalidate comparison. Trust is evaluated by this Windows host and its policy; certificate validation may consult configured network services. Valid signing does not prove benign intent, and unsigned does not prove malware.'}

Example code is displayed as text. Review it and test in your own lab before use.