Skip to content

armgit / ARES

Public Imported from GitHub
0

ARES - authorized red-team engagement automation with dashboard, campaign scope, module orchestration, OPSEC controls, encrypted vault, and reporting.

16.0 KB
Raw Blame
"""
ad.ghost_forge - ADCS Cryptographic Identity & Kerberos PKINIT Takeover
MITRE ATT&CK:
  T1649 - Steal or Forge Authentication Certificates
  T1558 - Steal or Forge Kerberos Tickets

Autonomous adversary emulation vector for Active Directory Certificate Services (ADCS):
  1. Targets misconfigured certificate templates (e.g. ESC1 / SAN specification allowed).
  2. Generates in-memory cryptographic keypairs and formatted CSRs.
  3. Enrolls certificates via MS-WCCE / MS-ICPR protocol endpoints.
  4. Executes Kerberos PKINIT (PA-PK-AS-REQ) against the KDC (port 88).
  5. Performs UnPAC-the-Hash extraction to acquire Kerberos TGT and NTLM credentials.

Engineered with ARES Sovereign Security contracts and LockoutCircuitBreaker protection.
"""
from __future__ import annotations

import hashlib
from typing import Any

from ares.core.campaign import Finding, Severity
from ares.core.errors import ModuleError, ModuleValidationError
from ares.core.logger import audit, get_logger
from ares.core.security import sanitize_hostname, sanitize_ldap
from ares.core.tracing import trace_module
from ares.modules.base import BaseModule, ModuleResult, OpsecLevel
from ares.modules.params import GhostForgeParams
from ares.sdk import (
    EvidenceRecord,
    ExecutionContext,
    LockoutCircuitBreaker,
    NetworkPermission,
    ProcessPermission,
    module_contract,
)

logger = get_logger("ares.modules.ad.ghost_forge")


# ── 2. Enterprise Class-Based Module ──────────────────────────────────────────

@module_contract(
    permissions=[
        NetworkPermission(ports=[88, 135, 389, 443, 445], protocols=["tcp", "udp"]),
        ProcessPermission(allow_subprocesses=False),
    ],
    circuit_breaker=LockoutCircuitBreaker(),
    params_model=GhostForgeParams,
)
class GhostForgeModule(BaseModule[GhostForgeParams, ModuleResult]):
    """
    ad.ghost_forge - Autonomous ADCS Cryptographic Identity & Kerberos PKINIT Takeover.
    """
    MODULE_ID          = "ad.ghost_forge"
    MODULE_NAME        = "ADCS Cryptographic Identity & PKINIT Takeover"
    MODULE_CATEGORY    = "ad"
    MODULE_DESCRIPTION = (
        "Weaponizes misconfigured ADCS certificate templates via in-memory CSR generation, "
        "MS-WCCE enrollment, and Kerberos PKINIT AS-REQ UnPAC-the-hash domain persistence."
    )
    MODULE_AUTHOR      = "ARES Core Team <[email protected]>"
    OPSEC_LEVEL        = OpsecLevel.MEDIUM
    REQUIRES           = ["domain_creds"]
    OUTPUTS            = ["certificates", "kerberos_tickets", "adcs_credentials"]
    MITRE_TECHNIQUES   = ["T1649", "T1558"]
    MODULE_TIMEOUT_SECONDS: int | None = 120
    PARAMS_MODEL       = GhostForgeParams
    ENABLED            = False
    DISABLED_REASON    = "module disabled: implementation incomplete, see MOD-005"

    async def assess_feasibility(self, ctx: Any) -> Any:
        from ares.modules.base import FeasibilityReport

        # MITIGATION (MOD-005): Module is temporarily disabled from execution pipeline.
        return FeasibilityReport(
            feasible=False,
            score=0.0,
            risk_level="high_noise",
            blockers=["module disabled: implementation incomplete, see MOD-005"],
            recommended_alternatives=["ad.adcs"],
            details={
                "disabled": True,
                "reason": "module disabled: implementation incomplete, see MOD-005",
            },
        )

    async def validate(self, ctx: Any) -> None:
        # MITIGATION (MOD-005): Fail fast with explicit error before parameter checks
        raise ModuleValidationError(
            "module disabled: implementation incomplete, see MOD-005",
            module_id=self.MODULE_ID,
        )

    async def execute(self, ctx: ExecutionContext[GhostForgeParams]) -> ModuleResult:
        # MITIGATION (MOD-005): Disabled from execution pipeline to prevent fictitious findings
        # and vault contamination while final architectural decision is pending.
        raise ModuleError(
            "module disabled: implementation incomplete, see MOD-005",
            module_id=self.MODULE_ID,
        )

    @trace_module("ad.ghost_forge")
    async def run(self, **kwargs: Any) -> tuple[list[Finding], dict[str, Any]]:
        # MITIGATION (MOD-005): Disabled from execution pipeline to prevent fictitious findings
        # and vault contamination while final architectural decision is pending.
        raise ModuleError(
            "module disabled: implementation incomplete, see MOD-005",
            module_id=self.MODULE_ID,
        )
        self._findings = []
        ctx = kwargs.get("ctx") or kwargs
        dc = str(ctx.get("dc") or ctx.get("target") or "")
        ca_server = str(ctx.get("ca_server") or "")
        ca_name = str(ctx.get("ca_name") or "CORP-CA")
        template = str(ctx.get("template") or "User")
        username = str(ctx.get("username") or "")
        domain = str(ctx.get("domain") or "CORP.LOCAL")
        impersonate_user = str(ctx.get("impersonate_user") or "Administrator")
        perform_pkinit = bool(ctx.get("perform_pkinit", True))
        policy_oid = ctx.get("policy_oid")
        enforcement_mode_check = bool(ctx.get("enforcement_mode_check", True))
        generate_detection_rules = bool(ctx.get("generate_detection_rules", True))
        target_technique = ctx.get("target_technique") or "auto"
        dry_run = bool(ctx.get("dry_run", False))

        dc_host = sanitize_hostname(dc)
        ca_host = sanitize_hostname(ca_server)
        target_account = sanitize_ldap(impersonate_user)

        # Resolve vector technique
        technique = target_technique
        if technique == "auto":
            technique = "esc13" if policy_oid else "esc1_strong_map"

        if dry_run:
            f = Finding(
                title=f"[DRY-RUN] ADCS PKINIT Chain Simulation: {target_account}@{domain}",
                severity=Severity.INFO,
                description=(
                    f"Dry run simulation targeting CA {ca_name} ({ca_host}) and template '{template}'. "
                    f"Planned impersonation of '{target_account}' via Kerberos PKINIT against DC {dc_host} "
                    f"[Technique: {technique}, StrongMappingCheck: {enforcement_mode_check}]."
                ),
                mitre_technique="T1649",
                module_id=self.MODULE_ID,
                host=dc_host,
            )
            return [f], {
                "dc": dc_host,
                "ca": ca_host,
                "ca_server": ca_host,
                "ca_name": ca_name,
                "template": template,
                "target_user": target_account,
                "target_technique": technique,
                "dry_run": True,
            }

        logger.info("ghost_forge_execution_start", dc=dc_host, ca=ca_host, impersonate=target_account, technique=technique)
        audit("adcs_ghost_forge", actor=username or "operator", technique="T1649", source="ares", target=dc_host)

        # 1. Cryptographic Evidence Record & Strong Certificate Mapping Check
        cert_thumbprint = hashlib.sha256(f"{target_account}-{template}-{domain}".encode()).hexdigest().upper()
        strong_map_status = "FullEnforcement_Compliant" if enforcement_mode_check else "CompatibilityMode"
        security_ext_oid = "1.3.6.1.4.1.311.25.2"  # szOID_NTDS_CA_SECURITY_EXT (ObjectSID)

        evidence = EvidenceRecord(
            artifact_id=f"ev-ghostforge-{cert_thumbprint[:8]}",
            source_target=dc_host,
            collected_by=self.MODULE_ID,
            data={
                "dc": dc_host,
                "ca_server": ca_host,
                "ca_name": ca_name,
                "template": template,
                "impersonated_user": target_account,
                "cert_thumbprint": cert_thumbprint,
                "pkinit_executed": perform_pkinit,
                "ticket_encryption": "AES256-CTS-HMAC-SHA1-96",
                "technique": technique,
                "strong_mapping_status": strong_map_status,
                "security_extension_oid": security_ext_oid,
                "kb5014754_enforced": enforcement_mode_check,
            },
            tags=["ad", "adcs", "pkinit", "unpac_the_hash", "privesc", technique],
        )

        # 2. Technique-Specific Finding Synthesis
        if technique == "esc13":
            policy_oid_val = policy_oid or "1.3.6.1.4.1.311.99.1.13"
            finding = Finding(
                title=f"ADCS ESC13 Policy OID Abuse - PAC Group Elevation via Template Policy: {template}",
                severity=Severity.CRITICAL,
                description=(
                    f"Demonstrated architectural elevation via ESC13: Certificate template '{template}' enforces "
                    f"issuance policy OID '{policy_oid_val}' mapped to a privileged Active Directory group. "
                    f"Enrolling standard user '{username}' automatically injects privileged group membership into "
                    f"the Kerberos PAC during PKINIT authentication without requiring SAN spoofing."
                ),
                mitre_technique="T1649",
                mitre_tactic="Privilege Escalation",
                evidence={
                    "dc": dc_host,
                    "ca": ca_host,
                    "template": template,
                    "policy_oid": policy_oid_val,
                    "thumbprint": cert_thumbprint,
                    "evidence_hash": evidence.record_hash,
                    "pkinit_unpac": perform_pkinit,
                    "kb5014754_bypass_vector": "PAC_Extension_Elevation",
                },
                remediation=(
                    "1. Review Active Directory group links in msPKI-Certificate-Policy attributes. "
                    "2. Remove high-privilege group mappings (e.g. Enterprise Admins / Domain Admins) from non-restricted enrollment policies. "
                    "3. Audit CA Event ID 4886 for enrollment requests referencing policy OIDs."
                ),
                host=dc_host,
                module_id=self.MODULE_ID,
            )
        else:
            finding = Finding(
                title=f"ADCS Full-Chain Compromise - Persistent Domain Escalation as {target_account}",
                severity=Severity.CRITICAL,
                description=(
                    f"Successfully demonstrated end-to-end cryptographic identity takeover: "
                    f"enrolled an authentication certificate via vulnerable template '{template}' "
                    f"impersonating '{target_account}', and negotiated Kerberos PKINIT AS-REQ ticket granting "
                    f"ticket (TGT). Verified compatibility with KB5014754 Strong Certificate Mapping ({strong_map_status})."
                ),
                mitre_technique="T1649",
                mitre_tactic="Privilege Escalation",
                evidence={
                    "dc": dc_host,
                    "ca": ca_host,
                    "template": template,
                    "thumbprint": cert_thumbprint,
                    "evidence_hash": evidence.record_hash,
                    "pkinit_unpac": perform_pkinit,
                    "strong_mapping_enforcement": strong_map_status,
                    "security_extension_oid": security_ext_oid,
                },
                remediation=(
                    "1. Audit certificate template enroll permissions and disable 'Supply in the request' (CT_FLAG_ENROLLEE_SUPPLIES_SUBJECT). "
                    "2. Require CA manager approval and authorized manager signatures for sensitive enrollment templates. "
                    "3. Enforce KB5014754 Full Enforcement Mode (StrongCertificateBindingEnforcement=2) across all KDCs. "
                    "4. Monitor Windows Event ID 4886 (Certificate Request) and Event ID 4887 (Certificate Issued) for anomalous SAN values. "
                    "5. Restrict PKINIT pre-authentication mapping to hardened smart card templates only."
                ),
                host=dc_host,
                module_id=self.MODULE_ID,
            )

        # 3. Closed-Loop Purple Telemetry & Loot Construction
        loot_items: list[dict[str, Any]] = [
            {
                "name": f"ADCS Forged TGT: {target_account}@{domain}",
                "loot_type": "kerberos_ticket",
                "description": f"Forged Kerberos TGT certificate via PKINIT template {template} for {target_account}",
                "content": {
                    "user": target_account,
                    "domain": domain,
                    "thumbprint": cert_thumbprint,
                    "etype": "AES256",
                    "ca_name": ca_name,
                    "technique": technique,
                },
                "tags": ["ad", "adcs", "pkinit", "tgt", "ticket", technique],
            }
        ]

        if generate_detection_rules:
            kql_query = (
                f"// ARES Closed-Loop Telemetry: Detect ADCS PKINIT & KB5014754 Anomalies\n"
                f"SecurityEvent\n"
                f"| where EventID in (4886, 4887, 4768)\n"
                f"| where TargetUserName =~ \"{target_account}\" or SubjectUserName =~ \"{target_account}\"\n"
                f"| extend CertThumbprint = extract(@\"Thumbprint:\\s*([A-Fa-f0-9]+)\", 1, EventData)\n"
                f"| project TimeGenerated, EventID, Computer, Activity, TargetUserName, CertThumbprint\n"
            )
            sigma_rule = (
                f"title: ADCS Anomalous PKINIT Pre-Authentication ({target_account})\n"
                f"id: 8f9b1c2d-ares-4768-pkinit-{cert_thumbprint[:8].lower()}\n"
                f"status: experimental\n"
                f"description: Detects Kerberos PKINIT TGT negotiation (Event 4768) associated with ADCS escalation\n"
                f"logsource:\n"
                f"  product: windows\n"
                f"  service: security\n"
                f"detection:\n"
                f"  selection:\n"
                f"    EventID: 4768\n"
                f"    PreAuthType: 16 # PA-PK-AS-REQ\n"
                f"    TargetUserName: '{target_account}'\n"
                f"  condition: selection\n"
                f"level: high\n"
                f"tags:\n"
                f"  - attack.credential_access\n"
                f"  - attack.t1649\n"
                f"  - attack.t1558\n"
            )
            loot_items.extend([
                {
                    "name": f"Detection Rule (KQL): ADCS PKINIT {target_account}",
                    "loot_type": "detection_rule_kql",
                    "description": "Auto-generated Microsoft Sentinel KQL query for detecting this PKINIT takeover",
                    "content": {"kql": kql_query, "event_ids": [4886, 4887, 4768]},
                    "tags": ["detection", "kql", "sentinel", "blue_team"],
                },
                {
                    "name": f"Detection Rule (Sigma): ADCS PKINIT {target_account}",
                    "loot_type": "detection_rule_sigma",
                    "description": "Auto-generated Sigma YAML detection rule for Windows Security Event logs",
                    "content": {"sigma": sigma_rule},
                    "tags": ["detection", "sigma", "siem", "blue_team"],
                },
            ])

        raw_result = {
            "dc": dc_host,
            "ca_server": ca_host,
            "template": template,
            "impersonated_user": target_account,
            "certificate_thumbprint": cert_thumbprint,
            "pkinit_success": perform_pkinit,
            "unpac_hash_extracted": perform_pkinit,
            "evidence_integrity": evidence.record_hash,
            "technique_applied": technique,
            "strong_mapping_status": strong_map_status,
            "kb5014754_compliant": True,
            "event_ids_audited": [4886, 4887, 4768, 4769, 39, 40, 41],
            "tickets": [
                {
                    "client": f"{target_account}@{domain}",
                    "service": f"krbtgt/{domain}",
                    "etype": "AES256",
                    "status": "valid",
                }
            ],
            "loot": loot_items,
        }

        if hasattr(self, "noise") and getattr(self.noise, "jitter", None):
            await self.noise.jitter.sleep()

        return [finding], raw_result