if
(!(
[Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]
::GetCurrent()).IsInRole(
[Security.Principal.WindowsBuiltInRole]
'Administrator'
)) {
Start-Process
-FilePath
PowerShell.exe
-Verb
RunAs
-ArgumentList
(
"-NoExit"
,(
"cd {0} ;"
-f
$PSScriptRoot
),(
"`"$($MyInvocation.MyCommand.Path)`" $($MyInvocation.UnboundArguments)"
))
Exit
}
Set-Location
-path
"$(Get-Location)"
$signtool
=
"D:\signtool.exe"
$cert_km
=
"D:\cert\localhost-km.crt"
$cert_pk
=
"D:\cert\localhost-pk.pfx"
$pk_root
=
"D:\cert\localhost-root-ca.crt"
New-CIPolicy
-FilePath
SiPolicy.xml
-Level
RootCertificate
-ScanPath
"C:\Windows\System32\"
Add-SignerRule
-FilePath
.\SiPolicy.xml
-CertificatePath
$cert_km
-Kernel
-Update
-Supplemental
Set-RuleOption
-FilePath
.\SiPolicy.xml
-Option
0
-Delete
Set-RuleOption
-FilePath
.\SiPolicy.xml
-Option
2
-Delete
Set-RuleOption
-FilePath
.\SiPolicy.xml
-Option
12
-Delete
Set-RuleOption
-FilePath
.\SiPolicy.xml
-Option
3
Set-RuleOption
-FilePath
.\SiPolicy.xml
-Option
5
Set-RuleOption
-FilePath
.\SiPolicy.xml
-Option
6
Set-RuleOption
-FilePath
.\SiPolicy.xml
-Option
9
Set-RuleOption
-FilePath
.\SiPolicy.xml
-Option
10
Set-RuleOption
-FilePath
.\SiPolicy.xml
-Option
16
Set-RuleOption
-FilePath
.\SiPolicy.xml
-Option
17
Set-CIPolicyVersion
-FilePath
.\SiPolicy.xml
-Version
10.0.0.0
$xmlPath
=
"$(Get-Location)"
+
"\SiPolicy.xml"
$xmlPath_Modified
=
"$(Get-Location)"
+
"\SiPolicy_Modified.xml"
$xml
=
New-Object
System.Xml.XmlDocument
$xml
.Load(
"$xmlPath"
)
$nsManager
=
New-Object
System.Xml.XmlNamespaceManager(
$xml
.NameTable)
$nsManager
.AddNamespace(
"sipolicy"
,
"urn:schemas-microsoft-com:sipolicy"
)
$signers
=
$xml
.SelectSingleNode(
"//sipolicy:Signers"
,
$nsManager
)
$newSigner1_0
=
$xml
.CreateElement(
"Signer"
,
$xml
.DocumentElement.NamespaceURI)
$newSigner1_0
.SetAttribute(
"ID"
,
"ID_SIGNER_KNOWNROOT_1_0"
)
$newSigner1_0
.SetAttribute(
"Name"
,
"Unknown Root"
)
$certRoot1_0
=
$xml
.CreateElement(
"CertRoot"
,
$xml
.DocumentElement.NamespaceURI)
$certRoot1_0
.SetAttribute(
"Type"
,
"Wellknown"
)
$certRoot1_0
.SetAttribute(
"Value"
,
"01"
)
$newSigner1_0
.AppendChild(
$certRoot1_0
)
$signers
.AppendChild(
$newSigner1_0
)
$newSigner2_0
=
$xml
.CreateElement(
"Signer"
,
$xml
.DocumentElement.NamespaceURI)
$newSigner2_0
.SetAttribute(
"ID"
,
"ID_SIGNER_KNOWNROOT_2_0"
)
$newSigner2_0
.SetAttribute(
"Name"
,
"Self-Signed Root"
)
$certRoot2_0
=
$xml
.CreateElement(
"CertRoot"
,
$xml
.DocumentElement.NamespaceURI)
$certRoot2_0
.SetAttribute(
"Type"
,
"Wellknown"
)
$certRoot2_0
.SetAttribute(
"Value"
,
"02"
)
$newSigner2_0
.AppendChild(
$certRoot2_0
)
$signers
.AppendChild(
$newSigner2_0
)
$newSigner14_0
=
$xml
.CreateElement(
"Signer"
,
$xml
.DocumentElement.NamespaceURI)
$newSigner14_0
.SetAttribute(
"ID"
,
"ID_SIGNER_KNOWNROOT_14_0"
)
$newSigner14_0
.SetAttribute(
"Name"
,
"Microsoft Trusted Root Store"
)
$certRoot14_0
=
$xml
.CreateElement(
"CertRoot"
,
$xml
.DocumentElement.NamespaceURI)
$certRoot14_0
.SetAttribute(
"Type"
,
"Wellknown"
)
$certRoot14_0
.SetAttribute(
"Value"
,
"14"
)
$newSigner14_0
.AppendChild(
$certRoot14_0
)
$signers
.AppendChild(
$newSigner14_0
)
$driversScenario
=
$xml
.SelectSingleNode(
"//sipolicy:SigningScenario[@ID='ID_SIGNINGSCENARIO_DRIVERS_1']"
,
$nsManager
)
$allowedsigners_DRIVERS
=
$driversScenario
.SelectSingleNode(
"./sipolicy:ProductSigners/sipolicy:AllowedSigners"
,
$nsManager
)
$allowedsigner1_0
=
$xml
.CreateElement(
"AllowedSigner"
,
$xml
.DocumentElement.NamespaceURI)
$allowedsigner1_0
.SetAttribute(
"SignerId"
,
"ID_SIGNER_KNOWNROOT_1_0"
)
$allowedsigners_DRIVERS
.AppendChild(
$allowedsigner1_0
)
$allowedsigner2_0
=
$xml
.CreateElement(
"AllowedSigner"
,
$xml
.DocumentElement.NamespaceURI)
$allowedsigner2_0
.SetAttribute(
"SignerId"
,
"ID_SIGNER_KNOWNROOT_2_0"
)
$allowedsigners_DRIVERS
.AppendChild(
$allowedsigner2_0
)
$allowedsigner14_0
=
$xml
.CreateElement(
"AllowedSigner"
,
$xml
.DocumentElement.NamespaceURI)
$allowedsigner14_0
.SetAttribute(
"SignerId"
,
"ID_SIGNER_KNOWNROOT_14_0"
)
$allowedsigners_DRIVERS
.AppendChild(
$allowedsigner14_0
)
$xml
.Save(
$xmlPath_Modified
)
ConvertFrom-CIPolicy
-XmlFilePath
.\SiPolicy_Modified.xml
-BinaryFilePath
.\SiPolicy.bin
&
$signtool
sign /v /p7 . /p7co 1.3.6.1.4.1.311.79.1 /fd sha256 /ac
$pk_root
/f
$cert_pk
SiPolicy.bin
Move-Item
-Force
-Path
.\SiPolicy.bin.p7
-Destination
.\SiPolicy.p7b
mountvol X: /s
Copy-Item
-Force
-Path
.\SiPolicy.p7b
-Destination
X:\EFI\Microsoft\Boot\
Write-Host
"(EFI分区)SiPolicy.p7b签名状态:"
certutil.exe
-asn
X:\EFI\Microsoft\Boot\SiPolicy.p7b
mountvol X: /d
Read-Host
-Prompt
"内核代码证书规则SiPolicy.p7b构建已完成,按任意键删除临时文件并退出本程序。"
Remove-Item
-Path
.\SiPolicy.p7b, .\SiPolicy.bin, .\SiPolicy.xml, .\SiPolicy_Modified.xml
$host
.SetShouldExit(0)