En examinant moi-même ce problème, je n'ai trouvé aucune solution de démarrage à la fin sur les scripts PowerShell autosignés. Alors, comment gérez-vous tout cela dans PowerShell lui-même pour utiliser la politique d'exécution AllSigned
et mieux sécuriser vos systèmes?La création de scripts PowerShell autosignés commence à se terminer
Répondre
Il s'agit d'une approche moderne des scripts PowerShell auto-signés. J'ai trouvé des versions plus anciennes de PowerShell ISE (seulement confirmé 2.0) encoder vos scripts dans Big Endian vs UTF-8 et causer des problèmes avec la signature. Avec cette méthode, vous ne devriez pas courir dedans puisque nous sommes sur v4 + ici.
Condition: PoSH 4.0+.
Cette fonction va: vérifier si un certificat Pfx existe et l'importer à LocalMachine\TrustedPublisher
; vérifier si un certificat lui a été transmis, exporter vers un certificat Pfx et l'importer; ou créez le certificat à LocalMachine\Personal
, exportez-le et importez-le. Je n'ai pas pu obtenir les autorisations pour travailler avec moi pour utiliser les magasins Cert:\CurrentUser
en dehors de \My
(Personnel).
$ErrorActionPreference = 'Stop'
Function New-SelfSignedCertificate
{
Param([Parameter(Mandatory=$True)]$PfxCertPath,$CertObj)
# Creates a SecureString object
$Cred = (Get-Credential).Password
If (Test-Path $PfxCertPath)
{
Try {
Import-PfxCertificate -FilePath $PfxCertPath -Password $Cred -CertStoreLocation Cert:\LocalMachine\TrustedPublisher
Write "$($PfxCertPath.FriendlyName) exists and is valid. Imported certificate to TrustedPublishers"
} Catch {
Write "Type mismatch or improper permission. Ensure your PFX cert is formed properly."
Write "[$($_.Exception.GetType().FullName)] $($_.Exception.Message)"
}
} ElseIf ($CertObj) {
Try {
Export-PfxCertificate -Cert $CertObj -FilePath $PfxCertPath -Password $Cred -Force
Import-PfxCertificate -FilePath $PfxCertPath -Password $Cred -CertStoreLocation Cert:\LocalMachine\TrustedPublisher
} Catch {
Write "[$($_.Exception.GetType().FullName)] $($_.Exception.Message)"
}
} Else {
Try {
$DNS = "$((GWMI Win32_ComputerSystem).DNSHostName).$((GWMI Win32_ComputerSystem).Domain)"
$CertObj = New-SelfSignedCertificate -CertStoreLocation Cert:\LocalMachine\My -DnsName $DNS -Type CodeSigningCert -FriendlyName 'Self-Sign'
Export-PfxCertificate -Cert $CertObj -FilePath $PfxCertPath -Password $Cred -Force
Import-PfxCertificate -FilePath $PfxCertPath -Password $Cred -CertStoreLocation Cert:\LocalMachine\TrustedPublisher
} Catch {
Write "[$($_.Exception.GetType().FullName)] $($_.Exception.Message)"
}
}
}
# Can be called like:
# Sign-Script -File C:\Script.ps1 -Certificate (GCI Cert:\LocalMachine\TrustedPublisher -CodeSigningCert)
#
# After the cert is imported to TrustedPublisher, you can use the
# exported pfx cert to sign on the machine instead of this method
Function Sign-Script
{
Param($File,$Cert)
If($Cert-is[String]){Try{$Cert=Get-PfxCertificate("$Cert")}Catch{}}
Set-AuthenticodeSignature -FilePath $File -Certificate $Cert -Force
}
Function Check-SignedScript
{
Param($File)
Get-AuthenticodeSignature -FilePath $File
}
Après tout est dit et fait, vous pouvez exécuter Set-ExecutionPolicy AllSigned
en tant qu'administrateur et utiliser ce script pour signer tous vos scripts. Check-SignedScript
vous dira si le signe est valide et vous pouvez dire si Sign-Script
a travaillé comme votre fichier aura # SIG # Begin signature block
à la fin. Toutes les modifications apportées à un script signé doivent être reconnues pour être exécutées.