-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppResign.ps1
150 lines (122 loc) · 5.04 KB
/
AppResign.ps1
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
param (
[Parameter(Mandatory = $true)][System.IO.FileInfo]$Package,
[Parameter(Mandatory = $false)][System.IO.FileInfo]$Certificate,
[Parameter(Mandatory = $false)][string]$CertificatePassword,
[Parameter(Mandatory = $false)][System.IO.FileInfo]$IntermediateCertificate
)
function GetWinSdkDir {
$dir = Get-Item -Path "${env:ProgramFiles(x86)}\Windows Kits\10\bin"
$dir = Get-ChildItem "$dir\10.*" | Sort-Object -Descending
$dir = $dir[0]
$dir = Get-Item -Path "$dir\x64"
return $dir
}
function CreateEmptyDir {
param ([Parameter(Mandatory = $true)][System.IO.DirectoryInfo]$Path)
if (Test-Path $Path ) {
Remove-Item -Path $Path -Recurse -Force
}
return New-Item -Path $Path -ItemType Directory
}
function RemoveMetadata {
param ([Parameter(Mandatory = $true)][System.IO.DirectoryInfo]$Path)
Remove-Item -Path "$Path\AppxMetadata" -Recurse -Force
Remove-Item -Path "$Path\AppxBlockMap.xml"
Remove-Item -Path "$Path\AppxSignature.p7x"
}
function GetSigningCertificate {
$cert = $null
if ($Certificate) {
if ($CertificatePassword) {
$cert = Get-PfxData -FilePath $Certificate.FullName -Password (ConvertTo-SecureString -String $CertificatePassword -Force -AsPlainText)
}
else {
$cert = Get-PfxData -FilePath $Certificate.FullName
}
$cert = $cert.EndEntityCertificates[0]
}
else {
$codeSigningSubject = "CN=AppPinning Code Sign"
$cert = Get-ChildItem Cert:\CurrentUser\My | Where-Object { $_.Subject -eq $codeSigningSubject }
if ($cert.count -eq 0) {
$cert = New-SelfSignedCertificate -Type CodeSigningCert -Subject $codeSigningSubject -KeyExportPolicy Exportable -CertStoreLocation Cert:\CurrentUser\My\ -NotAfter (Get-Date).AddYears(100)
}
else {
$cert = $cert[0]
}
}
return $cert
}
function SignPackage {
param ([Parameter(Mandatory = $true)][System.IO.FileInfo]$Path)
$signArgs = @("sign", "/fd", "SHA256")
$signArgs += @("/tr", "http://timestamp.sectigo.com", "/td", "SHA256")
if ($IntermediateCertificate) {
$signArgs += @("/ac", "$IntermediateCertificate")
}
if ($Certificate) {
if ($CertificatePassword) {
$signArgs += @("/f", "$Certificate", "/p", "$CertificatePassword")
}
else {
$signArgs += @("/f", "$Certificate")
}
}
else {
$signArgs += $signArgs += @("/a")
}
$signArgs += @("$Path")
&$signToolBin.FullName $signArgs
}
function ResignAppx {
param ([Parameter(Mandatory = $true)][System.IO.FileInfo]$InputPackage,
[Parameter(Mandatory = $true)][System.IO.FileInfo]$OutputPackage,
[Parameter(Mandatory = $true)][System.IO.DirectoryInfo]$WorkDir
)
$dir = CreateEmptyDir "$WorkDir\appx"
&$makeAppxBin.FullName unpack /p $InputPackage.FullName /d $dir.FullName
RemoveMetadata $dir
$manifestFile = Get-Item -Path "$dir\AppxManifest.xml"
$packagePublisherElement = Select-Xml -path $manifestFile.FullName -XPath "//*[local-name()='Identity']/@Publisher"
$packagePublisherElement.Node.Value = $signCert.Subject
$packagePublisherElement.Node.OwnerDocument.Save($manifestFile.FullName)
&$makeAppxBin.FullName pack /d $dir.FullName /p $OutputPackage.FullName
SignPackage $OutputPackage.FullName
Remove-Item -Path $dir.FullName -Recurse -Force
}
function ResignAppxBundle {
param ([Parameter(Mandatory = $true)][System.IO.FileInfo]$InputPackage,
[Parameter(Mandatory = $true)][System.IO.FileInfo]$OutputPackage,
[Parameter(Mandatory = $true)][System.IO.DirectoryInfo]$WorkDir
)
$originalPkgsDir = CreateEmptyDir "$WorkDir\appxbundle_original"
$modifiedPkgsDir = CreateEmptyDir "$WorkDir\appxbundle_resigned"
&$makeAppxBin.FullName unbundle /p $InputPackage.FullName /d $originalPkgsDir.FullName
RemoveMetadata $originalPkgsDir.FullName
foreach ($i in (Get-ChildItem -Path "$originalPkgsDir\*.appx")) {
ResignAppx $i ([System.IO.FileInfo]("$modifiedPkgsDir\$($i.Name)")) $WorkDir
}
&$makeAppxBin.FullName bundle /o /d $modifiedPkgsDir.FullName /p $OutputPackage.FullName
SignPackage $OutputPackage.FullName
Remove-Item -Path $originalPkgsDir.FullName -Recurse -Force
Remove-Item -Path $modifiedPkgsDir.FullName -Recurse -Force
}
if (-not($Package.Exists)) {
throw "$($Package.FullName) not found"
}
$winSDKDir = GetWinSdkDir
$makeAppxBin = Get-Item -Path "$winSDKDir\makeappx.exe"
$signToolBin = Get-Item -Path "$winSDKDir\signtool.exe"
$signCert = GetSigningCertificate
$tempDir = CreateEmptyDir "$(Split-Path -Parent $PSCommandPath)\_appxresigner_workdir"
$outPackage = [System.IO.FileInfo]($Package.FullName -replace "\.([^.]+)", "_resigned.`$1")
if ($Package.Extension -eq ".appx") {
ResignAppx $Package $outPackage $tempDir
}
elseif ($Package.Extension -eq ".appxbundle") {
ResignAppxBundle $Package $outPackage $tempDir
}
else {
throw "$($Package.FullName) unsupported"
}
Remove-Item -Path $tempDir.FullName -Recurse -Force