-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathsign_artifact.ps1
More file actions
56 lines (48 loc) · 1.91 KB
/
sign_artifact.ps1
File metadata and controls
56 lines (48 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# ---------------------------------------------------------------------------
# sign_artifact.ps1 - Creates a certificate using the provided parameters
# and signs the target binary.
# Created certificate is protected by the user that runs the script.
# This project makes use of ATT&CK®
# ATT&CK Terms of Use - https://attack.mitre.org/resources/terms-of-use/
# Usage: powershell -file sign_artifact.ps1 -Target ARTIFACT_TO_SIGN -CertSubject CERT_SUBJECT -CertExportPath EXPORT_PATH -CertDnsName DNS_NAME
# ---------------------------------------------------------------------------
param(
[Parameter(Mandatory=$true)][String]$Target,
[Parameter(Mandatory=$true)][String]$CertSubject,
[Parameter(Mandatory=$true)][String]$CertExportPath,
[Parameter(Mandatory=$true)][String]$CertDnsName
)
Write-Output "[DEBUG] Signing artifact $Target";
$params = @{
Subject = "$CertSubject"
Type = 'CodeSigning'
CertStoreLocation = 'Cert:\CurrentUser\My'
HashAlgorithm = 'sha256'
DnsName = "$CertDnsName"
NotBefore = (Get-Date).AddMonths(-6)
NotAfter = (Get-Date).AddMonths(18)
};
$cert = New-SelfSignedCertificate @params;
if ($cert) {
# Sign artifact
Write-Output $cert;
Set-AuthenticodeSignature -FilePath "$Target" -Certificate $cert;
if ((Get-AuthenticodeSignature "$Target").SignerCertificate) {
Write-Output "[DEBUG] Signed $Target";
} else {
Write-Output "[ERROR] Failed to sign $Target";
exit 7;
}
# Export certificate
Write-Output "[DEBUG] Exporting certificate";
Export-PfxCertificate -Cert $cert -FilePath "$CertExportPath" -ProtectTo "$(whoami)";
if (Test-Path "$CertExportPath") {
Write-Output "[DEBUG] Exported certificate to $CertExportPath";
} else {
Write-Output "[ERROR] Failed to export certificate.";
exit 8;
}
} else {
Write-Output "[ERROR] Failed to create certificate.";
exit 6;
}