Digitally Sign Files (PowerShell Example) - TheGetch/Penetration-Testing-Methodology GitHub Wiki

Digitally Sign Files (PowerShell Example)

From:


Creating a self-signed Certificate

Open Windows PowerShell and run the following One-Liner to create a signing certificate.

New-SelfSignedCertificate -DnsName [email protected] -CertStoreLocation Cert:\CurrentUser\My\ -Type Codesigning

You can find your certificate in your certificate store. Run certmgr.msc.

C:\> certmgr.msc

Import the Certifcate in Trusted Root Certification Autorities and Trusted Publisher

Now the certificate must be exported and then imported into the Trusted Root Certification Authorities and Trusted Publishers.

8ab02b88ef77cc3546efeee221572d2e.png

Double click on the certificate and select Details and Copy to file … 7da14c9eb1d42a665ffae8244e61bf1c.png

Do not export the private key. No need for. 4240f7f1c67a23c0acf1fa744440ce76.png

Select CER Format. a17b4ff14cc80167800b1d0a245218d5.png

Save the file wherever you want. Now import the certificate to the Trusted Root Authorities and Trusted Publishers. c79078e168a1d1c4504bce07cd58afd3.png

Sign a file

Next, we use Set-Authenticodesignature to sign our file. In this example, it is a. ps1 file, thus a PowerShell script.

Set-AuthenticodeSignature -FilePath C:\Temp\script1.ps1 -Certificate (Get-ChildItem -Path Cert:\CurrentUser\My\ -CodeSigningCert)

0cb6fed0f5db13b2943b1a837a962d06.png

Don’t worry about the Status Unknown Error. The next time you do it valid comes up. Crazy Stuff. Ok, we don’t care about this now.

df6e6cb7c6cd61d70e212b8460172018.png

Nice. Finally, see what happened. Open Windows Explorer, right click on your file, select properties and click on Digital Signatures.

2abe4c022a4bb94a20940d9c50a6d563.png

Testing your script

For testing your script, make sure the execution policy allows the running of PS1 scripts.

Get-ExecutionPolicy

Remotesigned, AllSigned and Unrestricted are your friends … If the policy is set to restricted then set it – for this testing environment – to AllSigned.

Set-ExecutionPolicy AllSigned

e51fab04194a66b4edb32ee902ff341b.png


#powershell

⚠️ **GitHub.com Fallback** ⚠️