top of page

Code Signing PowerShell Scripts

Updated: Aug 18

In this article, I'll describe the process of Code Signing PowerShell scripts from a Microsoft CA. I'll not cover how Code Signing adds security, simply put Code Signing doesn't provide or was intended to provide a robust security layer.


However, Code Signing does provide both Authenticity and Integrity:

  • The Authenticity that the script was written or reviewed by a trusted entity and then signed.

  • Integrity ensures that once signed the script hasn't been modified, useful when deploying scripts or executing scripts by a scheduled task with a service account.


Bypassing Code Signing requirements is simple, open ISE, paste in the code and F8, instant bypass. However, my development 'Enterprise' system is not standard, ISE won't work as Constrained Language Mode prevents all but core functionality from loading, meaning no API's, .Net, Com and most modules.


As a note, even with the script, code signed, ISE is next to useless with Constrained Language Mode enforced. Scripts require both signing and authorising in Applocker\WDAC and will only execute from native PowerShell.


Back to it.....


This is a typical message when executing a PowerShell script with the system requiring Code Signing. To successfully execute the script, the script must be signed with a digital signature from either a CA or Self Signed certificate.


I'm not going to Self Sign, it's filth and I've access to a Microsoft Certificate Authority (CA) as part of the Enterprise.

ree

Login to the CA, launch 'Manage' and locate the 'Code Signing' template, then 'Duplicate Template'.

ree

Complete the new template with the following settings:


General:

Name the new certificate template with something meaningful and up the validity to 3 years or to the maximum the corporate policy allows.

ree

Compatibility:

Update the Compatibility Settings and Certificate Recipient to 'Windows Server 2016' and 'Windows 10/Windows Server 2016' respectively.

ree

Request Handling:

Check the 'Allow private key to be exported'.

ree

Cryptographic:

Set 'Minimum key size' to either 1024, 2048, 4096, 8192 or 16,384


Select 'Requests must use one of the following providers:' and check 'Microsoft Enhanced RSA and AES Cryptographic Provider' (description)

ree

Security:

Ideally, enrolment is controlled via an AD Group with both READ and Enrol permissions. Do not under any circumstances allow WRITE or FULL.

ree

Save the new template and then issue by right-clicking on 'Certificate Template' > New and 'Certificate Template to Issue'.

ree

From a client and logged on with the account that is a member of the 'CRT_PowerShellCodeSigning' group, launch MMC and add the Certificate snap-in for the Current User.


Browse to Personal > Certificates and right-click in the empty space to the right, then click on 'All Tasks' > 'Request New Certificate.

ree

Select the 'Toyo Code Signing' template and then click on 'Properties' to add in some additional information.

ree

Add a Friendly Name and Description.

ree

Enrol the template.

ree

Now, right-click on the new 'Code Signing' certificate > All Tasks > Export.

ree

Select 'Yes, export the private key'.

ree

Ensure the 2 PKCS options are selected.

ree

Check the 'Group or username (recommended)' and on the Encryption drop-down select 'AES256-SHA256'.

ree

Complete the wizard by exporting the .pfx file

ree

The final step is to sign a script with the .pfx file using PowerShell.


Set-AuthenticodeSignature -FilePath "C:\Downloads\SecureReport9.4.ps1" -cert "C:\Downloads\CodeSigning.pfx"


Open the newly signed script and at the bottom of the script is the digital signature.

ree

Launch PowerShell.exe and run the script.


For those with Applocker\WDAC then the script requires adding to the allow list by file hash. Now I'll be able to execute my own Pentest script on my allegedly secure system and locate any missing settings.....


As always thanks for your support.

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page