-
Notifications
You must be signed in to change notification settings - Fork 4k
Update signature script for strong name and authenticode #4462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
<# | ||
Check (and recurse) current directory .\CheckSignature.ps1 | ||
Find and check directory of Azure PowerShell modules .\CheckSignature.ps1 -AzurePowerShell | ||
Check directory of MSI-installed modules .\CheckSignature.ps1 -MsiInstall | ||
Check directory of gallery-installed modules .\CheckSignature.ps1 -GalleryInstall | ||
Check files in provided path .\CheckSignature.ps1 -CustomPath $Path | ||
#> | ||
[CmdletBinding(DefaultParameterSetName="CurrentDirectory")] | ||
Param | ||
( | ||
[Parameter(ParameterSetName="AzurePowerShell", Mandatory=$true)] | ||
[switch]$AzurePowerShell, | ||
[Parameter(ParameterSetName="MsiInstall", Mandatory=$true)] | ||
[switch]$MsiInstall, | ||
[Parameter(ParameterSetName="GalleryInstall", Mandatory=$true)] | ||
[switch]$GalleryInstall, | ||
[Parameter(ParameterSetName="CustomPath", Mandatory=$true)] | ||
[string]$CustomPath | ||
) | ||
|
||
function Check-StrongName { | ||
[CmdletBinding()] | ||
param([Parameter(ValueFromPipeline=$true)][string]$path) | ||
$output = & "sn.exe" -vf $path | ||
$length = $output.Length - 1 | ||
if (-not $output[$length].Contains("is valid")) { | ||
Write-Output "$path has an invalid strong name." | ||
} | ||
} | ||
|
||
function Check-AuthenticodeSignature { | ||
[CmdletBinding()] | ||
param([Parameter(ValueFromPipeline=$true)][string]$path) | ||
$output = Get-AuthenticodeSignature $path | ||
if (-not ($output.Status -like "Valid")) { | ||
Write-Output "$path has an invalid authenticode signature. Status is $($output.Status)" | ||
} | ||
} | ||
|
||
function Check-All { | ||
[CmdletBinding()] | ||
param([Parameter()][string]$path) | ||
|
||
if (!(Get-Command "sn.exe" -ErrorAction SilentlyContinue)) | ||
{ | ||
Write-Error "Unable to find sn.exe; please ensure that the Windows SDK is installed and found in the PATH environment variable." | ||
return | ||
} | ||
|
||
$invalidList = @() | ||
|
||
$files = Get-ChildItem $path\* -Include *.dll -Recurse | Where-Object { $_.FullName -like "*Azure*" } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the future, it would be useful to factor out this file test iterator into its own function, so that you're calling it twice here, rather than duplicating the code. |
||
Write-Host "Checking the strong name signature of $($files.Count) files (.dll)" -ForegroundColor Yellow | ||
|
||
$invalidStrongNameList = @() | ||
|
||
for ($idx = 0; $idx -lt $files.Length; $idx++) { | ||
$percent = (100 * $idx) / $files.Length | ||
Write-Progress -Activity "Validating strong name signature of $($files[$idx])" -Status "$percent% Complete" -PercentComplete $percent | ||
$invalidStrongNameList += Check-StrongName -path $files[$idx] | ||
} | ||
|
||
if ($invalidStrongNameList.Length -gt 0) { | ||
$invalidList += $invalidStrongNameList | ||
Write-Host "Found $($invalidStrongNameList.Count) files with an invalid strong name signature." -ForegroundColor Red | ||
} | ||
else { | ||
Write-Host "All files (.dll) have a strong name signature." -ForegroundColor Green | ||
} | ||
|
||
# ------------------------------------- | ||
|
||
$files = Get-ChildItem $path\* -Include *.dll, *.ps1, *.psm1 -Recurse | Where-Object { $_.FullName -like "*Azure*" } | ||
$files = $files | Where-Object { ($_.FullName -notlike "*Newtonsoft.Json*") -and ` | ||
($_.FullName -notlike "*AutoMapper*") -and ` | ||
($_.FullName -notlike "*Security.Cryptography*") -and ` | ||
($_.FullName -notlike "*BouncyCastle.Crypto*")} | ||
Write-Host "Checking the authenticode signature of $($files.Count) files (.dll, .ps1, .psm1)" -ForegroundColor Yellow | ||
|
||
$invalidAuthenticodeList = @() | ||
|
||
for ($idx = 0; $idx -lt $files.Length; $idx++) { | ||
$percent = (100 * $idx) / $files.Length | ||
Write-Progress -Activity "Validating authenticode signature of $($files[$idx])" -Status "$percent% Complete" -PercentComplete $percent | ||
$invalidAuthenticodeList += Check-AuthenticodeSignature -path $files[$idx] | ||
} | ||
|
||
if ($invalidAuthenticodeList.Length -gt 0) { | ||
$invalidList += $invalidAuthenticodeList | ||
Write-Host "Found $($invalidAuthenticodeList.Count) files with an invalid authenticode signature." -ForegroundColor Red | ||
} | ||
else { | ||
Write-Host "All files (.dll, .ps1, .psd1) have a valid authenticode signature." -ForegroundColor Green | ||
} | ||
|
||
if ($invalidList.Length -gt 0) { | ||
Write-Output($invalidList) | ||
throw "Strong name signature check and/or authenticode signature check failed. Please see the above errors." | ||
} | ||
} | ||
|
||
$path = ".\" | ||
|
||
if ($PSCmdlet.ParameterSetName -eq "AzurePowerShell") | ||
{ | ||
$ProfileModule = Get-Module -Name AzureRM.Profile -ListAvailable | ||
if (!($ProfileModule)) | ||
{ | ||
Write-Error "Unable to find the AzureRM.Profile module. Please ensure that Azure PowerShell has been installed and the appropriate path is found in the PSModulePath environment variable." | ||
return | ||
} | ||
|
||
if ($ProfileModule.Count -gt 1) | ||
{ | ||
Write-Error "Mulitple versions of Azure PowerShell were found. Please use the -MsiInstall and -GalleryInstall switches to select the installed version you want to check." | ||
return | ||
} | ||
|
||
$ModulePath = $ProfileModule.Path | ||
if ($ModulePath -like "*Microsoft SDKs\Azure\PowerShell*") | ||
{ | ||
$EndIdx = $ModulePath.IndexOf("PowerShell\ResourceManager", [System.StringComparison]::OrdinalIgnoreCase) + "PowerShell".Length | ||
} | ||
elseif ($ModulePath -like "*Modules\AzureRM.Profile*") | ||
{ | ||
$EndIdx = $ModulePath.IndexOf("Modules\AzureRM.Profile", [System.StringComparison]::OrdinalIgnoreCase) + "Modules".Length | ||
} | ||
|
||
$path = $ModulePath.Substring(0, $EndIdx) | ||
Write-Host "Found AzureRM module - checking all (Azure) files in $path" -ForegroundColor Yellow | ||
} | ||
elseif ($PSCmdlet.ParameterSetName -eq "MsiInstall") | ||
{ | ||
$ProfileModule = Get-Module -Name AzureRM.Profile -ListAvailable | ||
if (!($ProfileModule)) | ||
{ | ||
Write-Error "Unable to find the AzureRM.Profile module. Please ensure that Azure PowerShell has been installed and the appropriate path is found in the PSModulePath environment variable." | ||
return | ||
} | ||
|
||
$SearchString = "Microsoft SDKs\Azure\PowerShell" | ||
|
||
$ModulePath = $ProfileModule.Path | ||
|
||
if ($ProfileModule.Count -gt 1) | ||
{ | ||
$ModulePath = $ProfileModule | Where-Object { $_.Path -like "*$($SearchString)*" } | ||
if (!($ModulePath)) | ||
{ | ||
Write-Error "Unable to find path of MSI-installed modules from multiple locations found in PSModulePath." | ||
return | ||
} | ||
} | ||
else | ||
{ | ||
if ($ModulePath -notlike "*$($SearchString)*") | ||
{ | ||
Write-Error "Modules installed on the current machine were not from MSI. Consider using the -GalleryInstall switch." | ||
return | ||
} | ||
} | ||
|
||
$EndIdx = $ModulePath.IndexOf("PowerShell\ResourceManager", [System.StringComparison]::OrdinalIgnoreCase) + "PowerShell".Length | ||
$path = $ModulePath.Substring(0, $EndIdx) | ||
Write-Host "Installed Azure PowerShell from MSI - checking all (Azure) files in $path" -ForegroundColor Yellow | ||
} | ||
elseif ($PSCmdlet.ParameterSetName -eq "GalleryInstall") | ||
{ | ||
$ProfileModule = Get-Module -Name AzureRM.Profile -ListAvailable | ||
if (!($ProfileModule)) | ||
{ | ||
Write-Error "Unable to find the AzureRM.Profile module. Please ensure that Azure PowerShell has been installed and the appropriate path is found in the PSModulePath environment variable." | ||
return | ||
} | ||
|
||
$SearchString = "WindowsPowerShell\Modules" | ||
|
||
$ModulePath = $ProfileModule.Path | ||
|
||
if ($ProfileModule.Count -gt 1) | ||
{ | ||
$ModulePath = $ProfileModule | Where-Object { $_.Path -like "*$($SearchString)*" } | ||
if (!($ModulePath)) | ||
{ | ||
Write-Error "Unable to find path of gallery-installed modules from multiple locations found in PSModulePath." | ||
return | ||
} | ||
} | ||
else | ||
{ | ||
if ($ModulePath -notlike "*$($SearchString)*") | ||
{ | ||
Write-Error "Modules installed on the current machine were not from the gallery. Consider using the -MsiInstall switch." | ||
return | ||
} | ||
} | ||
|
||
$EndIdx = $ModulePath.IndexOf("Modules\AzureRM.Profile", [System.StringComparison]::OrdinalIgnoreCase) + "Modules".Length | ||
$path = $ModulePath.Substring(0, $EndIdx) | ||
Write-Host "Installed Azure PowerShell from the gallery - checking all (Azure) files in $path" -ForegroundColor Yellow | ||
} | ||
elseif ($PSCmdlet.ParameterSetName -eq "CustomPath") | ||
{ | ||
$path = $CustomPath | ||
Write-Host "Custom path provided - checking all (Azure) files in $path" -ForegroundColor Yellow | ||
} | ||
else | ||
{ | ||
Write-Host "No switch parameter set - checking all files in current directory" -ForegroundColor Yellow | ||
} | ||
|
||
Check-All $path |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should check for the existence of sn.exe on the path and fail fast if it is not found at the beginning of the Check-All script.