2017-09-20 17 views
6

자체 서명 된 인증서를 사용하여 .ps1에 서명하려고합니다. 사용 사례는 개인용 개발 스테이션에서 직접 작성하는 스크립트 용이므로 사용하지 마십시오. 실제 CA를 사용하거나 지불해야합니다. ). 그러나 내가 읽은 인증서 생성 및 디지털 서명에 관한 많은 가이드가 있더라도 문제없이 작동 할 수는 없습니다.자체 서명 된 인증서 (및 makecert.exe 제외)를 사용하여 PowerShell 스크립트에 서명

는 여기에 지금까지 수행 한 작업은 다음과 같습니다

# Create a certificate to use as trusted root of the signing chain 
$root = New-SelfSignedCertificate ` 
    -Subject "CN=PowerShell Trusted Authority" ` 
    -FriendlyName "PowerShell Trusted Authority" ` 
    -KeyUsageProperty Sign ` 
    -KeyUsage CertSign, CRLSign, DigitalSignature ` 
    -CertStoreLocation Cert:\LocalMachine\My\ ` 
    -NotAfter (Get-Date).AddYears(10) 

# Create a certificate to use for signing powershell scripts 
New-SelfSignedCertificate ` 
    -Signer $root ` 
    -Subject "CN=PowerShell Code Signing" ` 
    -KeyAlgorithm RSA ` 
    -KeyLength 2048 ` 
    -Type CodeSigningCert ` 
    -CertStoreLocation Cert:\LocalMachine\My\ 

# Move the root cert into Trusted Root CAs 
Move-Item "Cert:\LocalMachine\My\$($root.Thumbprint)" Cert:\LocalMachine\Root 

관리 PowerShell을 인스턴스에서 수행 위의 모든합니다. 이 작업이 끝나면 예상되는 위치에서 관리 콘솔의 두 인증서를 모두 볼 수 있으며 서명 인증서의 인증서 경로는 유효한 것으로 확인됩니다.

나는 다음 일반 PS 프롬프트를 열고 스크립트를 로그인을 시도 : 당신이 볼 수 있듯이

# Obtain a reference to the signing certificate 
PS> $cert = Get-ChildItem Cert:\LocalMachine\My\ -CodeSigningCert 

# Attempt at signing 
PS> Set-AuthenticodeSignature .\Microsoft.PowerShell_profile.ps1 $cert 


    Directory: C:\Users\tomas\Documents\WindowsPowerShell 


SignerCertificate   Status    Path 
-----------------   ------    ---- 
          UnknownError   Microsoft.PowerShell_profile.ps1 

은 실제 서명이 실패합니다. PowerShell 파일을 보면 스크립트에 서명이 추가되지 않은 것을 볼 수 있습니다.

관리자 프롬프트에서 서명하는 경우 조금 더 많은 것으로 보입니다. 서명 블록이 스크립트에 추가되고 서명 인증서의 지문이 Set-AuthenticodeSignature의 출력에 인쇄되지만 상태는 여전히 UnknownError이고 AllSigned 정책의 실행은 여전히 ​​허용되지 않습니다.

# Output some info about the certificate: 
PS> $cert | Format-List 

Subject  : CN=PowerShell Code Signing 
Issuer  : CN=PowerShell Trusted Authority 
Thumbprint : <omitted> 
FriendlyName : 
NotBefore : 9/20/2017 10:48:59 PM 
NotAfter  : 9/20/2018 11:08:59 PM 
Extensions : {System.Security.Cryptography.Oid, System.Security.Cryptography.Oid, 
       System.Security.Cryptography.Oid, System.Security.Cryptography.Oid} 

나는하지만 항상 같은 상태 메시지 (UnknownError)와, 특히 코드 서명에 대한 인증서를 생성하기 위해, New-SelfSignedCertificate 주술의 변종의 다수를 시도했습니다.

내 궁극적 인 목표는 Set-ExecutionPolicy AllSigned 일 수 있고 내가 직접 만든 스크립트를 실행할 수있게하는 것입니다. 그 일을하기 위해이 과정에서 내가 누락 된 것은 무엇입니까?

+0

흥미 롭습니다. 오류에 대한 자세한 내용은 무엇입니까? –

+0

@Bill_Stewart : 불행히도, 아니. 이 실패 후에'$ Error' 변수는 비어 있습니다. 왜 실패했는지를 보여주는 다른 출력은 어디에도 없습니다. 모든 문제 해결 제안은 매우 환영합니다 :) –

+0

해당 출력의 'Status' 속성은 어떤 유형입니까? –

답변

1

이렇게 생각하면 인증서 체인 트러스트가 필요하지 않으므로 첫 번째 인증서가 필요하지 않습니다. 두 번째 인증서를 사용하여 신뢰할 수있는 루트 폴더로 옮기면 작동합니다. '루트'가 자체 서명되어 다른 인증서에 서명 할 수 없으므로 첫 번째 인증서를 사용한 다음 다른 인증서를 만드는 것이 실패한 것으로 보입니다.

자체 서명 인증서 방법

# Create a certificate to use for signing powershell scripts 
$selfsigncert = New-SelfSignedCertificate ` 
       -Subject "CN=PowerShell Code Signing" ` 
       -KeyAlgorithm RSA ` 
       -KeyLength 2048 ` 
       -Type CodeSigningCert ` 
       -CertStoreLocation Cert:\LocalMachine\My\ 

# Move the root cert into Trusted Root CAs 
Move-Item "Cert:\LocalMachine\My\$($selfsigncert.Thumbprint)" Cert:\LocalMachine\Root 

# Obtain a reference to the code signing cert in Trusted Root 
$selfsignrootcert = "Cert:\LocalMachine\Root\$($selfsigncert.Thumbprint)" 

# Sign script 
Set-AuthenticodeSignature C:\powershell.ps1 $selfsignrootcert 

당신이 엔터프라이즈 루트 CA에 액세스 할 수있는 경우, 당신은 당신이 당신의 질문에 사용한 방법을 사용할 수 있습니다.

기업 루트 CA 방법 (당신은 당신의 질문에이 같이 동일한 방법으로) - 당신이 당신의 루트 CA 인증서 지문을 팀을

# Get Enterprise Root CA thumbprint 
$rootcert = get-childitem Cert:\LocalMachine\Root\XXXXXXXXXXXX 


# Generate certificate 
$fromrootcert = New-SelfSignedCertificate ` 
       -Signer $rootcert ` 
       -Subject "CN=PowerShell Code Signing" ` 
       -KeyAlgorithm RSA ` 
       -KeyLength 2048 ` 
       -Type CodeSigningCert ` 
       -CertStoreLocation Cert:\LocalMachine\My\ 

# Sign script 
Set-AuthenticodeSignature C:\powershell.ps1 $fromrootcert 

감사를 알아야합니다.

+0

안녕하세요, Tim입니다. 이전 버전 인 것을 알고 있지만 방금 지침을 따르려고했지만 오류가 발생했습니다. '오류 : "지정된 경로의 형식이 지원되지 않습니다."'인증서를 다시 작성하고 지시 사항을 다시 수행했지만 지금 당황했습니다. 어떤 아이디어? –

+0

안녕하세요, 자체 서명 된 또는 엔터프라이즈 루트 CA 방법을 사용하고 있습니까? 어떤 cmdlet에서 오류가 발생합니까? 고마워, 팀. –

+0

@RossLyons는 내가 태그를 지정하지 않았기 때문에 응답을 받았는지 확실하지 않습니다. 죄송합니다. –