Repository navigation
Expand file tree
/
Copy pathTest-FileSignature.ps1
More file actions
351 lines (302 loc) · 15.1 KB
/
Copy pathTest-FileSignature.ps1
File metadata and controls
351 lines (302 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#Requires -Version 5.1
<#
.SYNOPSIS
Prueft eine heruntergeladene Datei anhand ihrer Signatur (GPG/OpenPGP und Authenticode).
.DESCRIPTION
Allgemeines Pruefwerkzeug fuer Downloads, die zusammen mit einer abgetrennten
Signaturdatei (.sig, .asc, .gpg) veroeffentlicht werden - z. B. Notepad++,
7-Zip-Mirror, Linux-ISOs, Git-Releases.
Es werden bis zu drei unabhaengige Pruefungen durchgefuehrt:
1. GPG Abgetrennte OpenPGP-Signatur gegen die Datei (gpg --verify).
Der oeffentliche Schluessel kommt aus -PublicKeyFile, dem
vorhandenen Schluesselbund oder - nur mit -Fetch - von einem
Keyserver.
2. Authenticode Eingebettete Windows-Signatur (exe, dll, msi, ps1 ...).
Eine unsignierte Datei ist kein Fehler, sondern wird gemeldet.
3. SHA-256 Nur wenn -ExpectedSha256 angegeben ist.
WICHTIG zur Vertrauensfrage: Eine "gute" GPG-Signatur beweist nur, dass die
Datei zu DIESEM Schluessel passt - nicht, dass der Schluessel dem Hersteller
gehoert. Ein per -Fetch geholter Schluessel ist deshalb unbelegt. Mit
-ExpectedFingerprint (aus einer zweiten, offiziellen Quelle) wird das
erzwungen; ohne diesen Parameter lautet das Ergebnis hoechstens WARN.
Alle GPG-Arbeiten laufen in einem temporaeren Schluesselbund, der danach
geloescht wird. Der Benutzer-Schluesselbund (~/.gnupg) wird nicht veraendert.
Es wird nichts installiert oder ausgefuehrt; die gepruefte Datei wird nur
gelesen.
.PARAMETER Path
Zu pruefende Datei.
.PARAMETER SignaturePath
Abgetrennte Signaturdatei. Ohne Angabe wird nach <Path>.sig, .asc, .gpg
gesucht; gibt es keine, laufen nur Authenticode und ggf. SHA-256.
.PARAMETER PublicKeyFile
Datei mit dem oeffentlichen Schluessel (ASCII-armored oder binaer).
.PARAMETER ExpectedFingerprint
Erwarteter Fingerabdruck des Signaturschluessels (40 Hex-Zeichen, Leerzeichen
erlaubt). Weicht der echte ab, ist das Ergebnis FAIL.
.PARAMETER Fetch
Erlaubt, den fehlenden oeffentlichen Schluessel per Fingerabdruck von einem
Keyserver zu holen (Netzzugriff, nur der Fingerabdruck wird uebertragen).
.PARAMETER KeyServer
Basis-URL eines HKP-ueber-HTTPS-Keyservers. Standard: https://keyserver.ubuntu.com
.PARAMETER ExpectedSha256
Erwarteter SHA-256-Hash der Datei (optional).
.PARAMETER GpgPath
Pfad zu gpg.exe. Ohne Angabe: PATH, Gpg4win, Git for Windows.
.EXAMPLE
.\Test-FileSignature.ps1 -Path .\npp.8.9.9.Installer.x64.exe -SignaturePath .\npp.8.9.9.Installer.x64.exe.sig -Fetch
Prueft GPG (Schluessel vom Keyserver) und Authenticode. Ergebnis WARN, weil der
Schluessel nicht gegen eine zweite Quelle abgeglichen wurde.
.EXAMPLE
.\Test-FileSignature.ps1 .\app.exe .\app.exe.sig -Fetch -ExpectedFingerprint '14BC E436 2749 B2B5 1F8C 7122 6C42 9F1D 8D84 F46E'
Wie oben, aber der Schluessel muss den angegebenen Fingerabdruck haben: Ergebnis OK.
.EXAMPLE
.\Test-FileSignature.ps1 .\debian.iso .\debian.iso.sign -PublicKeyFile .\release-key.asc
Schluessel liegt lokal vor, kein Netzzugriff.
.OUTPUTS
PSCustomObject mit Ergebnis (OK, WARN, FAIL) und den Einzelbefunden.
Rueckgabewerte sind auch als Prozess-Exitcode nutzbar: 0 OK, 1 WARN, 2 FAIL
(nur beim Aufruf als Skript mit -ExitCode).
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, Position = 0)]
[string]$Path,
[Parameter(Position = 1)]
[string]$SignaturePath,
[string]$PublicKeyFile,
[string]$ExpectedFingerprint,
[switch]$Fetch,
[string]$KeyServer = 'https://keyserver.ubuntu.com',
[string]$ExpectedSha256,
[string]$GpgPath,
[switch]$ExitCode
)
Set-StrictMode -Version 2.0
$ErrorActionPreference = 'Stop'
function Find-Gpg {
param([string]$Explicit)
if ($Explicit) {
if (Test-Path -LiteralPath $Explicit) { return (Resolve-Path -LiteralPath $Explicit).Path }
throw "gpg nicht gefunden: $Explicit"
}
$cmd = Get-Command gpg.exe -ErrorAction SilentlyContinue
if ($cmd) { return $cmd.Source }
$candidates = @(
"${env:ProgramFiles(x86)}\GnuPG\bin\gpg.exe",
"$env:ProgramFiles\GnuPG\bin\gpg.exe",
"$env:ProgramFiles\Git\usr\bin\gpg.exe",
"${env:ProgramFiles(x86)}\Git\usr\bin\gpg.exe"
)
foreach ($c in $candidates) { if ($c -and (Test-Path -LiteralPath $c)) { return $c } }
return $null
}
# gpg aus Git for Windows (MSYS) versteht "C:/x" nicht als absoluten Pfad und
# haengt das Arbeitsverzeichnis davor; dort ist "/c/x" noetig. Gpg4win (nativ)
# versteht "C:/x" und kein "/c/x".
$script:GpgIsMsys = $false
function ConvertTo-GpgPath {
param([string]$P)
$p2 = $P -replace '\\', '/'
if ($script:GpgIsMsys -and $p2 -match '^([A-Za-z]):/(.*)$') {
return '/' + $Matches[1].ToLowerInvariant() + '/' + $Matches[2]
}
return $p2
}
function Invoke-Gpg {
param([string]$Gpg, [string[]]$Arguments)
$old = $ErrorActionPreference
$ErrorActionPreference = 'Continue' # stderr von nativen Programmen ist kein Abbruchgrund
try { $out = & $Gpg @Arguments 2>&1 } finally { $ErrorActionPreference = $old }
return @($out | ForEach-Object { "$_" })
}
function Format-Fingerprint {
param([string]$F)
if (-not $F) { return '' }
$f = ($F -replace '\s', '').ToUpperInvariant()
return (($f -split '(.{4})' | Where-Object { $_ }) -join ' ')
}
# --- Eingaben pruefen --------------------------------------------------------
if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { throw "Datei nicht gefunden: $Path" }
$file = (Resolve-Path -LiteralPath $Path).Path
if (-not $SignaturePath) {
foreach ($ext in '.sig', '.asc', '.gpg') {
if (Test-Path -LiteralPath ($file + $ext)) { $SignaturePath = $file + $ext; break }
}
}
if ($SignaturePath -and -not (Test-Path -LiteralPath $SignaturePath -PathType Leaf)) {
throw "Signaturdatei nicht gefunden: $SignaturePath"
}
if ($SignaturePath) { $SignaturePath = (Resolve-Path -LiteralPath $SignaturePath).Path }
$expFpr = if ($ExpectedFingerprint) { ($ExpectedFingerprint -replace '\s', '').ToUpperInvariant() } else { $null }
if ($expFpr -and $expFpr -notmatch '^[0-9A-F]{40}$') {
throw 'ExpectedFingerprint muss 40 Hex-Zeichen haben (v4-Fingerabdruck).'
}
$findings = New-Object System.Collections.Generic.List[string]
$level = 0 # 0 OK, 1 WARN, 2 FAIL
function Set-Level { param([int]$L) if ($L -gt $script:level) { $script:level = $L } }
# --- Hash --------------------------------------------------------------------
$sha = (Get-FileHash -LiteralPath $file -Algorithm SHA256).Hash
if ($ExpectedSha256) {
$exp = ($ExpectedSha256 -replace '\s', '').ToUpperInvariant()
if ($exp -eq $sha) { $shaState = 'Uebereinstimmung' }
else { $shaState = 'ABWEICHUNG'; Set-Level 2; $findings.Add("SHA-256 weicht ab (erwartet $exp).") }
}
else { $shaState = 'nicht angefordert' }
# --- Authenticode ------------------------------------------------------------
$authState = 'nicht verfuegbar'; $authSigner = ''; $authThumb = ''
if (Get-Command Get-AuthenticodeSignature -ErrorAction SilentlyContinue) {
$a = Get-AuthenticodeSignature -LiteralPath $file
$authState = "$($a.Status)"
if ($a.SignerCertificate) {
$authSigner = $a.SignerCertificate.Subject
$authThumb = $a.SignerCertificate.Thumbprint
}
switch ($authState) {
'Valid' { }
'NotSigned' { } # bei .zip/.iso usw. normal; Bewertung ueber GPG
default { Set-Level 2; $findings.Add("Authenticode: $authState - $($a.StatusMessage)") }
}
}
# --- GPG ---------------------------------------------------------------------
$gpgState = 'keine Signaturdatei'; $gpgFpr = ''; $gpgSigner = ''; $gpgCreated = ''; $keySource = ''
if ($SignaturePath) {
$gpg = Find-Gpg -Explicit $GpgPath
if (-not $gpg) {
$gpgState = 'gpg fehlt'
Set-Level 2
$findings.Add('gpg.exe nicht gefunden (Gpg4win oder Git for Windows installieren, oder -GpgPath angeben).')
}
else {
$script:GpgIsMsys = ($gpg -match '[\\/]usr[\\/]bin[\\/]gpg\.exe$')
$home_ = Join-Path ([IO.Path]::GetTempPath()) ('fsig_' + [guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $home_ -Force | Out-Null
$homeArg = ConvertTo-GpgPath $home_
$base = @('--homedir', $homeArg, '--batch', '--no-tty', '--no-autostart')
try {
# Fingerabdruck des Signierers aus dem Signaturpaket lesen
$pk = Invoke-Gpg $gpg ($base + @('--list-packets', (ConvertTo-GpgPath $SignaturePath)))
$issuer = $null
foreach ($l in $pk) {
if ($l -match 'issuer fpr v4\s+([0-9A-Fa-f]{40})') { $issuer = $Matches[1].ToUpperInvariant(); break }
}
if (-not $issuer) {
foreach ($l in $pk) { if ($l -match 'keyid\s+([0-9A-Fa-f]{16})') { $issuer = $Matches[1].ToUpperInvariant(); break } }
}
# Schluessel beschaffen
$keyFile = $null
if ($PublicKeyFile) {
if (-not (Test-Path -LiteralPath $PublicKeyFile)) { throw "Schluesseldatei nicht gefunden: $PublicKeyFile" }
$keyFile = (Resolve-Path -LiteralPath $PublicKeyFile).Path
$keySource = 'Datei'
}
elseif ($Fetch) {
if (-not $issuer) { throw 'Signatur nennt keinen Schluessel - Abruf nicht moeglich.' }
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
$keyFile = Join-Path $home_ 'key.asc'
$url = ($KeyServer.TrimEnd('/')) + '/pks/lookup?op=get&search=0x' + $issuer
Invoke-WebRequest -Uri $url -UseBasicParsing -OutFile $keyFile -TimeoutSec 30
$keySource = "Keyserver ($KeyServer)"
}
elseif ($env:GNUPGHOME -or (Test-Path (Join-Path $HOME '.gnupg'))) {
# vorhandenen Benutzer-Schluesselbund nur lesend verwenden
$keySource = 'Benutzer-Schluesselbund'
}
if ($keyFile) {
[void](Invoke-Gpg $gpg ($base + @('--import', (ConvertTo-GpgPath $keyFile))))
$verBase = $base
}
elseif ($keySource -eq 'Benutzer-Schluesselbund') {
$verBase = @('--batch', '--no-tty', '--no-autostart')
}
else { $verBase = $base }
$v = Invoke-Gpg $gpg ($verBase + @('--status-fd', '1', '--verify',
(ConvertTo-GpgPath $SignaturePath), (ConvertTo-GpgPath $file)))
$status = @($v | Where-Object { $_ -match '^\[GNUPG:\]' })
$good = $status | Where-Object { $_ -match '\[GNUPG:\] GOODSIG\s+(\S+)\s+(.*)$' } | Select-Object -First 1
$valid = $status | Where-Object { $_ -match '\[GNUPG:\] VALIDSIG\s+([0-9A-Fa-f]{40})\s+(\S+)' } | Select-Object -First 1
if ($good -and $good -match 'GOODSIG\s+(\S+)\s+(.*)$') { $gpgSigner = $Matches[2] }
if ($valid -and $valid -match 'VALIDSIG\s+([0-9A-Fa-f]{40})\s+(\S+)') {
$gpgFpr = $Matches[1].ToUpperInvariant()
$gpgCreated = $Matches[2]
}
if ($status -match '\[GNUPG:\] BADSIG') { $gpgState = 'UNGUELTIG (Datei veraendert oder falsche Signatur)'; Set-Level 2 }
elseif ($status -match '\[GNUPG:\] NO_PUBKEY') { $gpgState = 'Schluessel fehlt'; Set-Level 2
$findings.Add("Oeffentlicher Schluessel nicht vorhanden (Signierer $issuer). -PublicKeyFile oder -Fetch verwenden.") }
elseif ($status -match '\[GNUPG:\] REVKEYSIG') { $gpgState = 'Schluessel WIDERRUFEN'; Set-Level 2 }
elseif ($status -match '\[GNUPG:\] ERRSIG') { $gpgState = 'Pruefung fehlgeschlagen'; Set-Level 2 }
elseif ($good -and $valid) {
$gpgState = 'Gueltig'
if ($status -match '\[GNUPG:\] EXPKEYSIG') {
$gpgState = 'Gueltig, Schluessel abgelaufen'; Set-Level 1
$findings.Add('Signaturschluessel ist abgelaufen.')
}
elseif ($status -match '\[GNUPG:\] EXPSIG') {
$gpgState = 'Gueltig, Signatur abgelaufen'; Set-Level 1
}
if ($expFpr) {
if ($gpgFpr -ne $expFpr) {
# Fingerabdruck des Hauptschluessels kann abweichen (Unterschluessel)
$primary = $null
if ($valid -match 'VALIDSIG(?:\s+\S+){8}\s+([0-9A-Fa-f]{40})') { $primary = $Matches[1].ToUpperInvariant() }
if ($primary -and $primary -eq $expFpr) { }
else { $gpgState = 'Gueltig, aber FALSCHER Schluessel'; Set-Level 2
$findings.Add("Fingerabdruck $gpgFpr weicht vom erwarteten $expFpr ab.") }
}
}
elseif ($keySource -ne 'Datei') {
Set-Level 1
$findings.Add('Schluessel-Echtheit nicht belegt: Fingerabdruck mit offizieller Quelle abgleichen oder -ExpectedFingerprint angeben.')
}
else {
Set-Level 1
$findings.Add('Kein -ExpectedFingerprint angegeben: Herkunft der Schluesseldatei selbst pruefen.')
}
}
else {
$gpgState = 'unklar'; Set-Level 2
$findings.Add('gpg lieferte kein eindeutiges Ergebnis: ' + (($v | Select-Object -First 3) -join ' | '))
}
}
finally {
# gpg-agent/keyboxd halten das Verzeichnis offen
try {
if (Test-Path (Join-Path (Split-Path $gpg) 'gpgconf.exe')) {
[void](Invoke-Gpg (Join-Path (Split-Path $gpg) 'gpgconf.exe') @('--homedir', $homeArg, '--kill', 'all'))
}
} catch { }
Start-Sleep -Milliseconds 300
Remove-Item -LiteralPath $home_ -Recurse -Force -ErrorAction SilentlyContinue
}
}
}
elseif ($authState -in 'NotSigned', 'nicht verfuegbar' -and -not $ExpectedSha256) {
Set-Level 1
$findings.Add('Weder Signaturdatei noch Authenticode noch Hash vorhanden - nichts belegt.')
}
# --- Ergebnis ----------------------------------------------------------------
$names = @('OK', 'WARN', 'FAIL')
$result = [pscustomobject][ordered]@{
Ergebnis = $names[$level]
Datei = $file
Groesse = (Get-Item -LiteralPath $file).Length
SHA256 = $sha
SHA256Pruefung = $shaState
GpgStatus = $gpgState
GpgFingerprint = (Format-Fingerprint $gpgFpr)
GpgSignierer = $gpgSigner
GpgErstellt = $gpgCreated
GpgSchluesselQuelle = $keySource
AuthenticodeStatus = $authState
AuthenticodeSigner = $authSigner
AuthenticodeThumbprint = $authThumb
Hinweise = @($findings)
}
$color = @('Green', 'Yellow', 'Red')[$level]
Write-Host ("Ergebnis: {0} - {1}" -f $names[$level], (Split-Path $file -Leaf)) -ForegroundColor $color
Write-Host (" SHA-256 : {0} [{1}]" -f $sha, $shaState)
Write-Host (" GPG : {0}" -f $gpgState)
if ($gpgFpr) { Write-Host (" {0} {1} (Schluessel: {2})" -f (Format-Fingerprint $gpgFpr), $gpgSigner, $keySource) }
Write-Host (" Authenticode: {0} {1}" -f $authState, $authSigner)
foreach ($f in $findings) { Write-Host " ! $f" -ForegroundColor $color }
if ($ExitCode) { exit $level }
$result