Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd">
<qhelp>
<overview>
<p>
Adding certificates to the system root certificate store (e.g.,
<code>Cert:\LocalMachine\Root</code>) weakens security for all applications running on
the same machine. Any certificate in the root store is trusted as a certificate authority,
which means a malicious or compromised certificate could be used to intercept encrypted
communications or impersonate trusted services for all users on the system.
</p>
</overview>
<recommendation>
<p>
Instead of importing certificates into the root store, use an application-specific or
user-specific certificate store such as <code>Cert:\CurrentUser\My</code> or
<code>Cert:\LocalMachine\My</code>. If root trust is genuinely required, ensure the
operation is restricted to controlled environments and the certificate is removed after use.
</p>
</recommendation>
<example>
<p>
The following example imports a certificate directly into the root store, weakening security
for all applications on the system:
</p>
<sample src="examples/DontInstallRootCertBad.ps1" />

<p>
The following example imports the certificate into a user-specific store, limiting the
scope of trust:
</p>
<sample src="examples/DontInstallRootCertGood.ps1" />
</example>
<references>
<li>Microsoft: <a href="https://learn.microsoft.com/en-us/powershell/module/pki/import-certificate">Import-Certificate</a>.</li>
<li>Microsoft: <a href="https://learn.microsoft.com/en-us/powershell/module/pki/import-pfxcertificate">Import-PfxCertificate</a>.</li>
<li>CWE-327: <a href="https://cwe.mitre.org/data/definitions/327.html">Use of a Broken or Risky Cryptographic Algorithm</a>.</li>
</references>
</qhelp>
32 changes: 32 additions & 0 deletions powershell/ql/src/queries/security/cwe-327/DontInstallRootCert.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @name Do not add certificates to the system root store
* @description Adding certificates to the system root certificate store weakens security for all
* applications running on the same machine by trusting potentially untrusted
* certificate authorities.
* @kind problem
* @problem.severity error
* @security-severity 7.5
* @precision high
* @id powershell/adding-cert-to-root-store
* @tags security
* external/cwe/cwe-327
*/

import powershell

/**
* A call to `Import-Certificate` or `Import-PfxCertificate` that targets the root certificate store.
*/
class ImportCertToRootStore extends CmdCall {
ImportCertToRootStore() {
this.getAName() = ["Import-Certificate", "Import-PfxCertificate"] and
exists(Expr loc |
loc = this.getNamedArgument("certstorelocation") and
loc.getValue().stringMatches("%Root%")
)
}
}

from ImportCertToRootStore call
select call,
"This call adds a certificate to the root certificate store, which weakens security for all applications on the machine."
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# BAD: Importing a certificate into the root store weakens security for all
# applications on the machine.
Import-Certificate -FilePath "C:\certs\my-cert.cer" -CertStoreLocation Cert:\LocalMachine\Root
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# GOOD: Importing a certificate into a user-specific store limits the scope of trust.
Import-Certificate -FilePath "C:\certs\my-cert.cer" -CertStoreLocation Cert:\CurrentUser\My
Loading