简体   繁体   English

X509Enrollment.CX509PrivateKey 在没有管理员权限的情况下创建

[英]X509Enrollment.CX509PrivateKey Create without Admin rights

I'm writing a Powershell script that creates self signed certificates which works fine as an Administrator but fails as Admin:我正在编写一个创建自签名证书的 Powershell 脚本,该证书作为管理员工作正常,但作为管理员失败:

The piece of code that fails when I call the Create method:调用 Create 方法时失败的代码段:

$PrivateKey = New-Object -ComObject X509Enrollment.CX509PrivateKey
$PrivateKey.ProviderName = "Microsoft RSA SChannel Cryptographic Provider"
$PrivateKey.KeySpec = 0x1
$PrivateKey.Length = 2048

# set security descriptor
$PrivateKey.SecurityDescriptor = "D:PAI(A;;0xd01f01ff;;;SY)(A;;0xd01f01ff;;;BA)(A;;0x80120089;;;NS)"

# key will be stored in local machine certificate store
$PrivateKey.MachineContext = 0x1

# export will be allowed
$PrivateKey.ExportPolicy = 0x1
$PrivateKey.Create()

It throws this error:它抛出这个错误:

Exception calling "Create" with "0" argument(s): "CertEnroll::CX509PrivateKey::Create: Access is denied. 0x80070005
(WIN32: 5)"
At C:\dev\LinoaSSC.ps1:106 char:1
+ $PrivateKey.Create()
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation

How should I troubleshoot or solve this error?我应该如何排除故障或解决此错误? Thanks guys!谢谢你们!

You need to be an administrator on the local machine to perform this and I believe most actions with certificates.您需要成为本地计算机的管理员才能执行此操作,我相信大多数操作都带有证书。 I'm not aware of any acceptable methods to work around this, but there are ways to elevate your permissions.我不知道有任何可接受的方法来解决这个问题,但有一些方法可以提升您的权限。

You can absolutely allow a non-admin to create machine key sets.您绝对可以允许非管理员创建机器密钥集。 You just need to allow the account you would like to create private keys to have write permissions to the following default directories.您只需要允许您要创建私钥的帐户对以下默认目录具有写入权限。

C:\ProgramData\Microsoft\Crypto\Keys
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\SystemCertificates
C:\ProgramData\Microsoft\Crypto\RSA

Then, in your code, provide a SecurityDescriptor on the CX509PrivateKey object before calling the Create method.然后,在您的代码中,在调用Create方法之前,在CX509PrivateKey对象上提供一个SecurityDescriptor The default SD is "D:P(A;;0xd01f01ff;;;CO)(A;;0xd01f01ff;;;SY)(A;;0xd01f01ff;;;BA)" , but simply add an entry for the account SID.默认 SD 是"D:P(A;;0xd01f01ff;;;CO)(A;;0xd01f01ff;;;SY)(A;;0xd01f01ff;;;BA)" ,但只需为帐户 SID 添加一个条目。

Curiously, the SecuityDescriptor does allow “CO”, which means CreatorOwner, but for CNG certificates, the private key gets created by lsass.exe running as SYSTEM, which is impersonating the account that requests the certificate.奇怪的是,SecuityDescriptor 确实允许“CO”,这意味着 CreatorOwner,但对于 CNG 证书,私钥由以 SYSTEM 身份运行的 lsass.exe 创建,它模拟请求证书的帐户。 By default, this doesn't allow the account that requested the certificate for some reason.默认情况下,这不允许出于某种原因请求证书的帐户。 It does appear that with CryptoAPI certs, the CreatorOwner SecurityDescriptor does allow the requesting account with the default SecurityDescriptor.看起来,对于 CryptoAPI 证书,CreatorOwner SecurityDescriptor 确实允许使用默认 SecurityDescriptor 的请求帐户。

Note: carefully consider the security implications for your environment.注意:仔细考虑对您的环境的安全影响。 This will allow the account to have access to the private keys in the machine store.这将允许帐户访问机器存储中的私钥。 In my scenario, principal of least privilege was a better trade off than granting the account full admin privileges.在我的场景中,与授予帐户完全管理员权限相比,最小特权主体是更好的权衡。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PowerShell JWT私钥x509证书 - PowerShell JWT PrivateKey x509Certificates PowerShell:New-SelfSignedCertificate:CertEnroll :: CX509Enrollment :: _ CreateRequest:指定了无效的标志。 0x80090009 - PowerShell: New-SelfSignedCertificate : CertEnroll::CX509Enrollment::_CreateRequest: Invalid flags specified. 0x80090009 .NET Framework x509Certificate2类,HasPrivateKey == true && PrivateKey == null? - .NET Framework x509Certificate2 Class, HasPrivateKey == true && PrivateKey == null? 如何在没有用户交互的情况下在Windows 10中创建和安装X.509自签名证书? - How to create and install X.509 self signed certificates in Windows 10 without user interaction? X509Store.Open() 抛出异常 - X509Store.Open() throwing an Exception 将x509证书重新分发到不同的商店 - Redistribute x509 certificates to different stores Powershell System.Security.Cryptography.X509Certificates.X509Certificate2 问题 - Powershell System.Security.Cryptography.X509Certificates.X509Certificate2 issues PowerShell - X509Certificates.X509Store 获取所有证书? - PowerShell - X509Certificates.X509Store get all certificates? Powershell 和 System.Security.Cryptography.X509Certificates.X509Certificate2 - Powershell and System.Security.Cryptography.X509Certificates.X509Certificate2 无需外部工具,自动将带链的 x509 证书从 Server 2008 R2 导出到 p7b 文件? - Automate export x509 certificate w/chain from Server 2008 R2 to a p7b file WITHOUT external tools?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM