PowerShell Hash Control

In enterprise and other sensitive environments, ensuring the security and integrity of downloaded software is of paramount importance. Therefore, meticulously checking the hash value of downloaded software becomes an essential practice. By verifying the hash value, which acts as a unique fingerprint of the original file, organizations can confirm that the downloaded software has not been altered, corrupted, or tampered with during transit or distribution. This proactive measure helps mitigate the risk of potential security breaches, malware infections, or unintended modifications that could compromise the entire system’s stability and confidentiality. Employing hash value checks provides an additional layer of trust and confidence, enabling enterprises to maintain a secure and reliable software environment.

PowerShell Code for Easy Check

You could technically use a One-Line-Code to check the integrity. But sometimes you maybe don’t remember the Code. So here is a little PowerShell Code for that.

Add-Type -AssemblyName System.Windows.Forms

function Get-FileHashValue {
    param (
        [string]$filePath,
        [string]$algorithm = "SHA256"
    )

    $hasher = Get-FileHash -Path $filePath -Algorithm $algorithm
    return $hasher.Hash
}

# Function to create the GUI window
function Show-GUI {
    $form = New-Object Windows.Forms.Form
    $form.Text = "File Hash Checker"
    $form.Width = 500
    $form.Height = 250
    $form.StartPosition = "CenterScreen"
    $form.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")

    $label1 = New-Object Windows.Forms.Label
    $label1.Text = "Select a file:"
    $label1.Location = New-Object Drawing.Point(30, 30)
    $label1.AutoSize = $true

    $fileDialog = New-Object Windows.Forms.OpenFileDialog
    $fileDialog.Filter = "All Files (*.*)|*.*"

    $button1 = New-Object Windows.Forms.Button
    $button1.Text = "Browse"
    $button1.Location = New-Object Drawing.Point(380, 25)
    $button1.Add_Click({
        $fileDialog.ShowDialog()
    })

    $label2 = New-Object Windows.Forms.Label
    $label2.Text = "Select Hash method:"
    $label2.Location = New-Object Drawing.Point(30, 70)
    $label2.AutoSize = $true

    $combo1 = New-Object Windows.Forms.ComboBox
    $combo1.Items.Add("SHA256")
    $combo1.Items.Add("SHA1")
    $combo1.Items.Add("MD5")
    $combo1.Location = New-Object Drawing.Point(220, 65)

    $label3 = New-Object Windows.Forms.Label
    $label3.Text = "Enter the expected hash:"
    $label3.Location = New-Object Drawing.Point(30, 110)
    $label3.AutoSize = $true

    $textbox1 = New-Object Windows.Forms.TextBox
    $textbox1.Location = New-Object Drawing.Point(220, 105)

    $button2 = New-Object Windows.Forms.Button
    $button2.Text = "Check Hash"
    $button2.Location = New-Object Drawing.Point(200, 150)
    $button2.Add_Click({
        if ([System.IO.File]::Exists($fileDialog.FileName)) {
            $filePath = $fileDialog.FileName
            $algorithm = $combo1.SelectedItem.ToString()
            $expectedHash = $textbox1.Text

            try {
                $fileHash = Get-FileHashValue -filePath $filePath -algorithm $algorithm

                if ($fileHash -eq $expectedHash) {
                    [System.Windows.Forms.MessageBox]::Show("Hashsum is correct. The file is unchanged.", "File Hash Checker", "OK", [System.Windows.Forms.MessageBoxIcon]::Information)
                } else {
                    [System.Windows.Forms.MessageBox]::Show("Hashsum does not match. The file may be corrupted or modified.", "File Hash Checker", "OK", [System.Windows.Forms.MessageBoxIcon]::Warning)
                }
            } catch {
                [System.Windows.Forms.MessageBox]::Show("Error calculating hash: $_", "File Hash Checker", "OK", [System.Windows.Forms.MessageBoxIcon]::Error)
            }
        } else {
            [System.Windows.Forms.MessageBox]::Show("Please select a valid file.", "File Hash Checker", "OK", [System.Windows.Forms.MessageBoxIcon]::Exclamation)
        }
    })

    # Add controls to the form
    $form.Controls.Add($label1)
    $form.Controls.Add($button1)
    $form.Controls.Add($label2)
    $form.Controls.Add($combo1)
    $form.Controls.Add($label3)
    $form.Controls.Add($textbox1)
    $form.Controls.Add($button2)

    $form.ShowDialog()
}

Show-GUI

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert