-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheck_Signatures.ps1
More file actions
183 lines (158 loc) · 5.96 KB
/
Check_Signatures.ps1
File metadata and controls
183 lines (158 loc) · 5.96 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
# check_signature.ps1
# This script checks the signature of given files or directories containing executables
# Author: @ma0f97
# Version: 1.0
# Arguments:
# -file: Specify a specific file or directory path to check (e.g. "C:\Windows\System32\notepad.exe" or "C:\Windows\System32")
# -hideTrusted: Hide files signed by trusted signers defined in the script (e.g. Microsoft)
# -verboseMode: Print verbose output
param(
[Parameter(Mandatory=$true)]
[string]$file,
[Parameter(Mandatory=$false)]
[switch]$hideTrusted,
[Parameter(Mandatory=$false)]
[switch]$verboseMode
)
# Define trusted signers within this array
$trustedSigners = @(
"Microsoft Corporation",
"Microsoft Windows",
"Elasticsearch",
"Logitech",
"Magnitude Software",
"Simba Technologies Inc.",
"Mozilla Corporation",
"NVIDIA Corporation",
"Google Inc.",
"VMware, Inc.",
"Malwarebytes Inc.",
"Stiftelsen Syncthing",
"Discord Inc.",
"Valve Corp.",
"Wireguard LLC",
"Parsec Cloud",
"OpenVPN Technologies, Inc.",
"Malwarebytes Corporation" # Add more trusted signers as needed
)
# Initialize counters for each signature status
$script:trustedCount = 0
$script:untrustedCount = 0
$script:unsignedCount = 0
$script:failedSignatureCount = 0
# Lists to store unique signers, unsigned files, and failed signatures
$script:uniqueSigners = @{}
$script:unsignedFiles = @()
$script:failedSignatureFiles = @()
function CheckSignature {
param(
[string]$binaryPath,
[switch]$hide,
[switch]$verboseSwitch
)
if ($verboseSwitch) {
Write-Output "Checking signature for: $binaryPath"
}
$signature = Get-AuthenticodeSignature -FilePath $binaryPath
$signedByFull = $signature.SignerCertificate.Subject
$signedBy = $signedByFull -replace '.*O="?([^,]+).*','$1'
$fullPath = Resolve-Path $binaryPath
$filename = Split-Path $binaryPath -Leaf
if ($verboseSwitch) {
Write-Output "Full Signature Subject: $signedByFull"
}
if ($signature.Status -eq 'Valid') {
$isTrusted = $false
foreach ($signer in $trustedSigners) {
if ($signedBy -like "*$signer*") {
$isTrusted = $true
break
}
}
if ($isTrusted -and $hide) {
$script:trustedCount++
return
} elseif ($isTrusted) {
# Add to unique signers list
if ($isTrusted) {
$script:uniqueSigners[$signedBy] = "Trusted"
} else {
$script:uniqueSigners[$signedBy] = "Untrusted"
}
$script:trustedCount++
Write-Output "Executable: $filename"
Write-Host "Signed Status: Signed by" -NoNewline; Write-Host " $signedBy " -NoNewline -ForegroundColor Blue; Write-Output "- Verified"
} else {
# Add to unique signers list
if ($isTrusted) {
$script:uniqueSigners[$signedBy] = "Trusted"
} else {
$script:uniqueSigners[$signedBy] = "Untrusted"
}
$script:untrustedCount++
Write-Output "Executable: $filename"
Write-Host "Signed Status: Signed by" -NoNewline; Write-Host " $signedBy " -NoNewline -ForegroundColor Green; Write-Output "- Verified"
}
} elseif ($signedBy -eq "") {
# Add to unsigned files list
$script:unsignedFiles += $fullPath
$script:unsignedCount++
Write-Output "Executable: $filename"
Write-Host "Signed Status: " -NoNewline; Write-Host "UNSIGNED" -ForegroundColor DarkYellow
} else {
# Add to failed signature files list
$script:failedSignatureFiles += $fullPath
$script:failedSignatureCount++
Write-Output "Executable: $filename"
if ($trustedSigners -contains $signedBy) {
Write-Host "ALERT: POSSIBLE FAKE SIGNATURE BY A TRUSTED ORGANIZATION!" -ForegroundColor Red
}
Write-Host "Signed Status: Signed by" -NoNewline; Write-Host " $signedBy " -NoNewline -ForegroundColor Red; Write-Output "- SIGNATURE FAILED"
}
Write-Output ""
Write-Output ""
}
# Truncate any unwanted arguments from the path
if ($file -match '(.+?)"') {
$file = $matches[1]
}
Write-Output $file
if (Test-Path $file -PathType Leaf) {
# Single file provided
CheckSignature -binaryPath $file -hide:$hideTrusted -verboseSwitch:$verboseMode
} elseif (Test-Path $file -PathType Container) {
Write-Output "Checking signatures for all executables in $file and subdirectories (this may take a while)..."
# Directory provided
$executables = Get-ChildItem -Path $file -Recurse -Include *.exe,*.dll | Where-Object { -not $_.PSIsContainer }
if ($verboseMode) {
Write-Output "Found $($executables.Count) executables in $file"
}
foreach ($executable in $executables) {
CheckSignature -binaryPath $executable.FullName -hide:$hideTrusted -verboseSwitch:$verboseMode
}
} else {
Write-Output "The provided path is neither a valid file nor a directory."
exit
}
# Display summary at the end
Write-Output "SUMMARY:"
Write-Host "Trusted Signatures: $trustedCount" -ForegroundColor Blue
Write-Host "Signed but Untrusted: $untrustedCount" -ForegroundColor Green
Write-Host "Unsigned: $unsignedCount" -ForegroundColor DarkYellow
Write-Host "Failed Signatures: $failedSignatureCount" -ForegroundColor Red
Write-Output "----------------------------------------"
Write-Output "Unique Signers:"
foreach ($signer in $script:uniqueSigners.Keys) {
$color = if ($script:uniqueSigners[$signer] -eq "Trusted") { "Blue" } else { "Green" }
Write-Host " $signer" -ForegroundColor $color
}
Write-Output "----------------------------------------"
Write-Output "Unsigned Files:"
$script:unsignedFiles | ForEach-Object {
Write-Output " $_"
}
Write-Output "----------------------------------------"
Write-Output "Files with Failed Signatures:"
$script:failedSignatureFiles | ForEach-Object {
Write-Output " $_"
}