Private/ASP00/Invoke-ADCSSetup.ps1

<#
.SYNOPSIS
Configures AD CS roles and vulnerable certificate templates for the AS2Go lab.
 
.DESCRIPTION
Inventories enterprise certification authorities and Web Enrollment services using a preferred root-domain DC.
Offers installation of missing CA and Web Enrollment roles or creation of AS2Go certificate templates.
The template workflow offers an optional internal-name suffix, reports existing AS2Go templates, and invokes
New-ESCxCertficateTemplate to create and publish a new ESC1 and ESC4 template pair. The menu remains available
until the user skips it.
 
.PARAMETER CTPreFix
Template name prefix passed to New-ESCxCertficateTemplate. The menu can replace it with a custom prefix.
 
.EXAMPLE
Invoke-ADCSSetup -CTPreFix "AS2Go"
Opens the AD CS setup menu with AS2Go as the initial certificate-template prefix.
 
.INPUTS
None. This function does not declare pipeline input.
 
.OUTPUTS
Console messages and output from invoked commands; no dedicated result object is defined.
 
.NOTES
Private AS2Go function. Run from an initialized AS2Go module session.
Requires initialized AS2Go controller mappings for template creation and publication.
 
.COMPONENT
Category: Main Function
 
.FUNCTIONALITY
Reviews certification authorities and enrollment services, then offers installation of missing AD CS roles.
Creates and publishes a new ESC1 and ESC4 certificate template pair on a selected Enterprise CA.
#>

function Invoke-ADCSSetup {

    ################################################################################
    ##### #####
    ##### Parent Function to setup ADCS and enroll malicious CA Templates #####
    ##### #####
    ################################################################################

    Param([string] $CTPreFix)

    $Info = Measure-FunctionRuntime -Begin
    $CurrentFunction = $Info.FunctionName
    
    If (-not $SkipClearHost) { Clear-Host }
    Write-AS2GoOutput -Type Header -Message "Setup Active Directory Certificate Services (ADCS) for AS2Go"
        
    $Functionality = (Get-Help $CurrentFunction).Functionality
    Write-AS2GoOutput -Type H1 -Message ($Functionality + "`n")
        
    $bestDC = Get-BestDomainController -Domain (Get-ADForest).RootDomain
    Get-ADCSEnterpriseCA -Server $bestDC
    Get-ADCSEnrollmentServices -Server $bestDC

    do {

        If ($Script:ADCSEnrollmentServiceMissing -eq $true) {
            $default = 0
        }
        else {
            $default = 1
        }


        $Options = @(
            [pscustomobject] @{ Label = "Install AD&CS"; Help = "Install one Certification Authority & Web Enrollment services."; Value = "ADCS" },
            [pscustomobject] @{ Label = "Add (and publish) Certificate &Templates"; Help = "Create and publish vulnerable certificate templates, e.g. ESC1"; Value = "Templates" },
            [pscustomobject] @{ Label = "&Skip"; Help = "Skip configuring ADCS for AS2Go."; Value = $Script:Skip }
        )

        $title = "AS2Go - Configure ADCS for AS2Go"
        $message = "Select an option to configure ADCS for AS2Go."
        $answer = Show-DecisionPrompt -Message $message  -Options $Options -Default $default -Title $title

        If ($answer -eq 'ADCS') {
            Add-ADCSForAS2Go
            Write-AS2GoOutput -Type Success "ADCS setup completed successfully."

            Write-AS2GoOutput -Type Warning -Message "Manual follow-up steps (not automated):" -NoExtraLines
            Write-AS2GoOutput -Type Info -Message "Configure the following Group Policy setting:" -NoExtraLines
            Write-AS2GoOutput -Type Bullet -Message "Enable 'Certificate Services Client - Auto-Enrollment'"
            Write-AS2GoOutput -Type Bullet -Message " Path: Computer Configuration > Policies > Windows Settings > Security Settings > Public Key Policies > Certificate Services Client - Auto-Enrollment"
            Write-AS2GoOutput -Type Bullet -Message " Configuration Model: Enabled"
            Write-AS2GoOutput -Type Bullet -Message " Select: Renew expired certificates, update pending certificates, and remove revoked certificates"
            Write-AS2GoOutput -Type Bullet -Message " Select: Update certificates that use certificate templates"
        }
        elseif ($answer -eq 'Templates') {

            $TargetServer = $Script:BestDCs[(Get-ADForest).RootDomain]


            $Title = "Certificate Templates - Define Suffix for the new Template Pair"

            $Options = @(
                [pscustomobject] @{ Label = "&Use timestamp suffix"; Help = "Name the new templates with a shared timestamp suffix." ; Value = "Previous" },
                [pscustomobject] @{ Label = "&Define new suffix"; Help = "Define a dedicated suffix for the internal template names."; Value = "New" }
            )
            $Decision = Show-DecisionPrompt -Message $message  -Options $Options -Default 0 -Title $Title

            If ($Decision -eq "New") {
                do {
                    $result = Read-Host "Enter new template name suffix"
                    if ([string]::IsNullOrWhiteSpace($result)) {
                        Write-AS2GoOutput -Type Warning -Message "Input is required. Empty, whitespace-only or null values are not allowed."
                    }
                } until (-not [string]::IsNullOrWhiteSpace($result))

                $CTSuffix = $result.Trim()
                Write-AS2GoOutput -Type TextMaker -Message "New template name suffix:" -Tm $CTSuffix
            }
            else {
                $CTSuffix = $null
                Write-AS2GoOutput -Type Info -message "Keep timestamp suffix for the new Certificate Templates!"
            }


            Get-ADCSCertificateTemplates -CTPrefix $CTPreFix -Server $TargetServer

            If ($Script:ADCSASGoCTemplatesMissing) {
                Write-AS2GoOutput -Type Info -Message "No AS2Go certificate templates were found; a new ESC1 and ESC4 pair will be created."
            }
            else {
                Write-AS2GoOutput -Type Info -Message "Existing AS2Go templates stay unchanged; another ESC1 and ESC4 pair will be created."
            }

            Write-AS2GoOutput -Type TextMaker -Message "Installation will be performed on server" -TM $TargetServer

            If ($EnableLogging) {
                $TemplatePairCreated = New-ESCxCertficateTemplate -CTPrefix $CTPreFix -Suffix $CTSuffix -Server $TargetServer -verbose
            }
            else {
                $TemplatePairCreated = New-ESCxCertficateTemplate -CTPrefix $CTPreFix -Suffix $CTSuffix -Server $TargetServer
            }

            Get-ADCSCertificateTemplates -CTPrefix $CTPreFix -Server $TargetServer

            If ($TemplatePairCreated) {
                Write-AS2GoOutput -Type Success "New ESC1 and ESC4 templates with prefix $CTPreFix PUBLISHED successfully."
            }
            else {
                Write-AS2GoOutput -Type Error -Message "Creating or publishing the new ESC1 and ESC4 templates failed. Review the messages above."
            }
        }
        else {
            Write-log -Message " >> Skipping ADCS setup for AS2Go."
            $repeat = $Script:No
        }

    } Until ($repeat -eq $Script:No)

    Measure-FunctionRuntime -End -Details $Info
}