From 3f7a6111e72c29e42546edc8a2eaedb8fc527f4f Mon Sep 17 00:00:00 2001 From: Snozz-Al Date: Fri, 20 Nov 2026 16:37:59 +0000 Subject: [PATCH 1/3] feat(ad): add explicit LDAP protection opt-outs --- .../services/ad/Connect-MtAdTarget.ps1 | 87 +++++-- .../ad/Get-MtAdSupportedAuthMatrix.ps1 | 8 +- .../Get-MtAdLdapProtectionCacheSuffix.ps1 | 14 ++ .../protocol/New-MtAdProtocolConnection.ps1 | 17 ++ .../ad/protocol/New-MtLdapConnection.ps1 | 57 ++++- .../Write-MtLdapProtectionWarning.ps1 | 33 +++ powershell/public/connect/Connect-Maester.ps1 | 51 +++- .../public/services/ad/Get-MtADDacls.ps1 | 12 +- .../services/ad/Get-MtADDomainState.ps1 | 32 ++- .../public/services/ad/Get-MtADGpoState.ps1 | 12 +- .../ActiveDirectoryLdapProtection.Tests.ps1 | 175 ++++++++++++++ .../ActiveDirectoryProtocol.Tests.ps1 | 219 ++++++++++++++++++ website/docs/advanced/active-directory.md | 8 + .../ad-connection-troubleshooting.md | 42 +++- 14 files changed, 714 insertions(+), 53 deletions(-) create mode 100644 powershell/internal/services/ad/protocol/Get-MtAdLdapProtectionCacheSuffix.ps1 create mode 100644 powershell/internal/services/ad/protocol/Write-MtLdapProtectionWarning.ps1 create mode 100644 powershell/tests/functions/ActiveDirectoryLdapProtection.Tests.ps1 diff --git a/powershell/internal/services/ad/Connect-MtAdTarget.ps1 b/powershell/internal/services/ad/Connect-MtAdTarget.ps1 index eaa8bff7f6..d52e2cbaa4 100644 --- a/powershell/internal/services/ad/Connect-MtAdTarget.ps1 +++ b/powershell/internal/services/ad/Connect-MtAdTarget.ps1 @@ -12,11 +12,17 @@ function Connect-MtAdTarget { [ValidateSet('Negotiate', 'Kerberos', 'Ntlm', 'Basic')] [string]$AuthMode = 'Negotiate', - [ValidateSet('Auto', 'Ldaps', 'StartTls')] + [ValidateSet('Auto', 'Ldaps', 'StartTls', 'None')] [string]$TlsMode = 'Auto', [switch]$SkipCertificateCheck, + [switch]$ForceUnencrypted, + + [switch]$ForceUnsigned, + + [switch]$AllowPlaintextPassword, + [switch]$PassThru ) @@ -144,6 +150,9 @@ function Connect-MtAdTarget { TlsMode = $SelectedTlsMode AuthenticationMode = $AuthenticationMode SkipCertificateCheck = $SkipCertificateCheck.IsPresent + ForceUnencrypted = $TlsMode -eq 'None' -and $ForceUnencrypted.IsPresent + ForceUnsigned = $TlsMode -eq 'None' -and $ForceUnsigned.IsPresent + AllowPlaintextPassword = $TlsMode -eq 'None' -and $AllowPlaintextPassword.IsPresent Capabilities = [ordered]@{ SupportsPaging = $true SupportsRangeRetrieval = $true @@ -300,7 +309,9 @@ function Connect-MtAdTarget { [Parameter(Mandatory)] [string]$RequestedTlsMode, - [switch]$SkipCertificateCheck + [switch]$SkipCertificateCheck, + + [switch]$ForceUnencrypted ) $lastException = $null @@ -309,25 +320,33 @@ function Connect-MtAdTarget { $isWindowsRuntime = $adPrerequisites.PlatformProfile -like 'Windows*' - # Wrap in @() so a single attempt stays an array; otherwise .Count returns the hashtable key count - $tlsAttempts = @(switch ($RequestedTlsMode) { - 'Ldaps' { @(@{ Name = 'Ldaps'; Port = 636; UseStartTls = $false }) } - 'StartTls' { @(@{ Name = 'StartTls'; Port = 389; UseStartTls = $true }) } - default { - if ($isWindowsRuntime) { - Write-Verbose 'Windows runtime detected. TLS fallback order: LDAPS (636), StartTLS (389).' - @( - @{ Name = 'Ldaps'; Port = 636; UseStartTls = $false } - @{ Name = 'StartTls'; Port = 389; UseStartTls = $true } - ) - } else { - Write-Verbose 'Non-Windows runtime detected. Skipping StartTLS in Auto mode due to known upstream .NET bug. Only LDAPS (636) will be attempted.' - @( - @{ Name = 'Ldaps'; Port = 636; UseStartTls = $false } - ) + # Handle explicit unencrypted mode + if ($RequestedTlsMode -eq 'None') { + if (-not $ForceUnencrypted.IsPresent) { + throw "TLS mode 'None' requires -ForceUnencrypted. This disables TLS and is intended for isolated test environments only." + } + $tlsAttempts = @(@{ Name = 'None'; Port = 389; UseStartTls = $false }) + } else { + # Wrap in @() so a single attempt stays an array; otherwise .Count returns the hashtable key count + $tlsAttempts = @(switch ($RequestedTlsMode) { + 'Ldaps' { @(@{ Name = 'Ldaps'; Port = 636; UseStartTls = $false }) } + 'StartTls' { @(@{ Name = 'StartTls'; Port = 389; UseStartTls = $true }) } + default { + if ($isWindowsRuntime) { + Write-Verbose 'Windows runtime detected. TLS fallback order: LDAPS (636), StartTLS (389).' + @( + @{ Name = 'Ldaps'; Port = 636; UseStartTls = $false } + @{ Name = 'StartTls'; Port = 389; UseStartTls = $true } + ) + } else { + Write-Verbose 'Non-Windows runtime detected. Skipping StartTLS in Auto mode due to known upstream .NET bug. Only LDAPS (636) will be attempted.' + @( + @{ Name = 'Ldaps'; Port = 636; UseStartTls = $false } + ) + } } - } - }) + }) + } foreach ($tlsAttempt in $tlsAttempts) { Write-Verbose "Attempting TLS mode '$($tlsAttempt.Name)' to '$Server' on port $($tlsAttempt.Port) with AuthType '$AuthenticationMode'" @@ -338,6 +357,11 @@ function Connect-MtAdTarget { Port = $tlsAttempt.Port AuthType = $AuthenticationMode } + if ($tlsAttempt.Name -eq 'None') { + $connectionParameters['ForceUnencrypted'] = $true + $connectionParameters['ForceUnsigned'] = $ForceUnsigned.IsPresent + $connectionParameters['AllowPlaintextPassword'] = $AllowPlaintextPassword.IsPresent + } if ($tlsAttempt.UseStartTls) { $connectionParameters['UseStartTls'] = $true @@ -479,6 +503,27 @@ function Connect-MtAdTarget { throw "TLS mode '$TlsMode' is not supported on this platform profile." } + if ($TlsMode -eq 'None' -and -not $ForceUnencrypted.IsPresent) { + throw "TLS mode 'None' requires -ForceUnencrypted. This disables TLS and is intended for isolated test environments only." + } + + if ($ForceUnencrypted.IsPresent -and $TlsMode -ne 'None') { + Write-Verbose '-ForceUnencrypted has no effect unless -TlsMode None is selected. TLS remains enabled.' + } + + if ($ForceUnsigned.IsPresent -and ($TlsMode -ne 'None' -or -not $ForceUnencrypted.IsPresent -or $authenticationMode -eq 'Basic')) { + throw '-ForceUnsigned requires -TlsMode None, -ForceUnencrypted, and Negotiate, Kerberos, or Ntlm authentication.' + } + if ($AllowPlaintextPassword.IsPresent -and ($TlsMode -ne 'None' -or -not $ForceUnencrypted.IsPresent -or $authenticationMode -ne 'Basic')) { + throw '-AllowPlaintextPassword requires -TlsMode None, -ForceUnencrypted, and Basic authentication.' + } + if ($authenticationMode -eq 'Basic' -and $TlsMode -eq 'None' -and -not $AllowPlaintextPassword.IsPresent) { + throw 'Basic authentication without TLS requires the additional -AllowPlaintextPassword acknowledgement together with -ForceUnencrypted.' + } + if ($AllowPlaintextPassword.IsPresent -and [string]::IsNullOrWhiteSpace($ActiveDirectoryServer)) { + throw '-AllowPlaintextPassword requires an explicit -ActiveDirectoryServer. DNS-based or ambient target discovery is not permitted for plaintext password binds.' + } + if ($authenticationMode -eq 'Basic' -and $null -eq $ActiveDirectoryCredential) { throw 'Basic authentication requires -ActiveDirectoryCredential.' } @@ -498,7 +543,7 @@ function Connect-MtAdTarget { } Write-Verbose "Resolved target server: '$targetServer'. Attempting AD connection with AuthMode '$authenticationMode' and TlsMode '$TlsMode'." - $resolvedConnection = Connect-MtAdResolvedTarget -Server $targetServer -Credential $ActiveDirectoryCredential -AuthenticationMode $authenticationMode -RequestedTlsMode $TlsMode -SkipCertificateCheck:$SkipCertificateCheck + $resolvedConnection = Connect-MtAdResolvedTarget -Server $targetServer -Credential $ActiveDirectoryCredential -AuthenticationMode $authenticationMode -RequestedTlsMode $TlsMode -SkipCertificateCheck:$SkipCertificateCheck -ForceUnencrypted:$ForceUnencrypted $adConnectionState = Get-MtAdSessionState -Connected $true ` -ResolvedForest $resolvedConnection.Metadata.ResolvedForest ` -ResolvedDomain $resolvedConnection.Metadata.ResolvedDomain ` diff --git a/powershell/internal/services/ad/Get-MtAdSupportedAuthMatrix.ps1 b/powershell/internal/services/ad/Get-MtAdSupportedAuthMatrix.ps1 index 7d9279c2cd..f5cd6cc8a3 100644 --- a/powershell/internal/services/ad/Get-MtAdSupportedAuthMatrix.ps1 +++ b/powershell/internal/services/ad/Get-MtAdSupportedAuthMatrix.ps1 @@ -9,7 +9,7 @@ function Get-MtAdSupportedAuthMatrix { PSEdition = 'Desktop' PowerShellVersionFloor = [version]'5.1' SupportedAuthModes = @('Negotiate', 'Kerberos', 'Ntlm', 'Basic') - TlsModes = @('Ldaps', 'StartTls') + TlsModes = @('Ldaps', 'StartTls', 'None') TlsRequiredAuthModes = @('Basic') ExplicitCredentialAuthModes = @('Negotiate', 'Kerberos', 'Ntlm', 'Basic') IntegratedAuthModes = @('Negotiate', 'Kerberos', 'Ntlm') @@ -23,7 +23,7 @@ function Get-MtAdSupportedAuthMatrix { PSEdition = 'Core' PowerShellVersionFloor = [version]'7.0' SupportedAuthModes = @('Negotiate', 'Kerberos', 'Ntlm', 'Basic') - TlsModes = @('Ldaps', 'StartTls') + TlsModes = @('Ldaps', 'StartTls', 'None') TlsRequiredAuthModes = @('Basic') ExplicitCredentialAuthModes = @('Negotiate', 'Kerberos', 'Ntlm', 'Basic') IntegratedAuthModes = @('Negotiate', 'Kerberos', 'Ntlm') @@ -37,7 +37,7 @@ function Get-MtAdSupportedAuthMatrix { PSEdition = 'Core' PowerShellVersionFloor = [version]'7.2' SupportedAuthModes = @('Negotiate', 'Kerberos', 'Basic') - TlsModes = @('Ldaps', 'StartTls') + TlsModes = @('Ldaps', 'StartTls', 'None') TlsRequiredAuthModes = @('Basic') ExplicitCredentialAuthModes = @('Negotiate', 'Kerberos', 'Basic') IntegratedAuthModes = @('Negotiate', 'Kerberos') @@ -53,7 +53,7 @@ function Get-MtAdSupportedAuthMatrix { PSEdition = 'Core' PowerShellVersionFloor = [version]'7.2' SupportedAuthModes = @('Negotiate', 'Kerberos', 'Basic') - TlsModes = @('Ldaps', 'StartTls') + TlsModes = @('Ldaps', 'StartTls', 'None') TlsRequiredAuthModes = @('Basic') ExplicitCredentialAuthModes = @('Negotiate', 'Kerberos', 'Basic') IntegratedAuthModes = @('Negotiate', 'Kerberos') diff --git a/powershell/internal/services/ad/protocol/Get-MtAdLdapProtectionCacheSuffix.ps1 b/powershell/internal/services/ad/protocol/Get-MtAdLdapProtectionCacheSuffix.ps1 new file mode 100644 index 0000000000..e959d13afc --- /dev/null +++ b/powershell/internal/services/ad/protocol/Get-MtAdLdapProtectionCacheSuffix.ps1 @@ -0,0 +1,14 @@ +function Get-MtAdLdapProtectionCacheSuffix { + [CmdletBinding()] + [OutputType([string])] + param( + [Parameter(Mandatory)] + [object]$ConnectionState + ) + + if ($ConnectionState.TlsMode -ne 'None') { + return '' + } + + return ":ldap-none:$($ConnectionState.AuthenticationMode):unsigned=$([bool]$ConnectionState.ForceUnsigned):plaintext-password=$([bool]$ConnectionState.AllowPlaintextPassword)" +} diff --git a/powershell/internal/services/ad/protocol/New-MtAdProtocolConnection.ps1 b/powershell/internal/services/ad/protocol/New-MtAdProtocolConnection.ps1 index 5bf668b9b2..c378a2ee6a 100644 --- a/powershell/internal/services/ad/protocol/New-MtAdProtocolConnection.ps1 +++ b/powershell/internal/services/ad/protocol/New-MtAdProtocolConnection.ps1 @@ -31,10 +31,27 @@ function New-MtAdProtocolConnection { Server = $ProtocolEvidence.ResolvedServer AuthType = $ProtocolEvidence.AuthenticationMode } + if ($ProtocolEvidence.TlsMode -eq 'None') { + if (-not [bool]$ProtocolEvidence.ForceUnencrypted) { + throw 'Cached LDAP evidence for TLS mode None requires the explicit ForceUnencrypted opt-in.' + } + $ldapConnectionParameters['ForceUnencrypted'] = [bool]$ProtocolEvidence.ForceUnencrypted + $ldapConnectionParameters['ForceUnsigned'] = [bool]$ProtocolEvidence.ForceUnsigned + $ldapConnectionParameters['AllowPlaintextPassword'] = [bool]$ProtocolEvidence.AllowPlaintextPassword + if ($ldapConnectionParameters['AllowPlaintextPassword']) { + if ([string]::IsNullOrWhiteSpace($ProtocolEvidence.RequestedServer)) { + throw 'Plaintext password reuse requires an explicitly requested server in the cached LDAP evidence.' + } + # Do not redirect a plaintext password to a name supplied by an unauthenticated RootDSE. + $ldapConnectionParameters['Server'] = $ProtocolEvidence.RequestedServer + } + } if ($ProtocolEvidence.TlsMode -eq 'StartTls') { $ldapConnectionParameters['Port'] = 389 $ldapConnectionParameters['UseStartTls'] = $true + } elseif ($ProtocolEvidence.TlsMode -eq 'None') { + $ldapConnectionParameters['Port'] = 389 } else { $ldapConnectionParameters['Port'] = 636 } diff --git a/powershell/internal/services/ad/protocol/New-MtLdapConnection.ps1 b/powershell/internal/services/ad/protocol/New-MtLdapConnection.ps1 index 1d17809a17..e0036c035d 100644 --- a/powershell/internal/services/ad/protocol/New-MtLdapConnection.ps1 +++ b/powershell/internal/services/ad/protocol/New-MtLdapConnection.ps1 @@ -17,7 +17,13 @@ [timespan] $Timeout = [timespan]::FromSeconds(30), - [switch] $SkipCertificateCheck + [switch] $SkipCertificateCheck, + + [switch] $ForceUnencrypted, + + [switch] $ForceUnsigned, + + [switch] $AllowPlaintextPassword ) $effectivePort = if ($UseStartTls.IsPresent -and -not $PSBoundParameters.ContainsKey('Port')) { @@ -27,9 +33,25 @@ $Port } - if ($AuthType -eq 'Basic' -and -not $UseStartTls.IsPresent -and $effectivePort -ne 636) { + $usesTls = $UseStartTls.IsPresent -or $effectivePort -eq 636 + if ($AuthType -eq 'Basic' -and -not $usesTls -and -not ($ForceUnencrypted.IsPresent -and $AllowPlaintextPassword.IsPresent)) { throw 'Basic authentication requires LDAPS on port 636 or StartTLS.' } + if (-not $usesTls -and -not $ForceUnencrypted.IsPresent) { + throw 'LDAP without TLS requires -ForceUnencrypted.' + } + if ($ForceUnsigned.IsPresent -and ($usesTls -or -not $ForceUnencrypted.IsPresent -or $AuthType -eq 'Basic')) { + throw '-ForceUnsigned requires LDAP without TLS, -ForceUnencrypted, and Negotiate, Kerberos, or Ntlm authentication.' + } + if ($AllowPlaintextPassword.IsPresent -and ($usesTls -or -not $ForceUnencrypted.IsPresent -or $AuthType -ne 'Basic')) { + throw '-AllowPlaintextPassword requires Basic authentication without TLS and -ForceUnencrypted.' + } + if ($AllowPlaintextPassword.IsPresent -and $null -eq $Credential) { + throw '-AllowPlaintextPassword requires an explicit credential.' + } + if (-not $usesTls) { + Write-MtLdapProtectionWarning -Server $Server -Port $effectivePort -AuthType $AuthType -ForceUnsigned:$ForceUnsigned + } $identifier = $null $connection = $null @@ -84,6 +106,25 @@ } } + # Explicit opt-outs must be honored, not silently left to provider defaults. + # Basic/simple bind has no SASL layer; do not write SASL options on it or TLS. + if (-not $usesTls -and $AuthType -ne 'Basic') { + try { + $connection.SessionOptions.Sealing = $false + if ($connection.SessionOptions.Sealing) { + throw 'The provider did not disable SASL sealing.' + } + if ($ForceUnsigned.IsPresent) { + $connection.SessionOptions.Signing = $false + if ($connection.SessionOptions.Signing) { + throw 'The provider did not disable SASL signing.' + } + } + } catch { + throw "The LDAP platform/provider cannot honor the requested SASL protection opt-outs. No bind was attempted. Use TLS or a supported client platform. $($_.Exception.Message)" + } + } + if ($PSBoundParameters.ContainsKey('Credential')) { $networkCredential = $Credential.GetNetworkCredential() @@ -113,6 +154,18 @@ Write-Verbose "Attempting LDAP bind to '$Server' on port $effectivePort with AuthType '$AuthType'" $connection.Bind() + if (-not $usesTls -and $AuthType -ne 'Basic') { + try { + if ($connection.SessionOptions.Sealing) { + throw 'SASL sealing was re-enabled by the client platform or policy.' + } + if ($ForceUnsigned.IsPresent -and $connection.SessionOptions.Signing) { + throw 'SASL signing was re-enabled by the client platform or policy.' + } + } catch { + throw "The LDAP platform/provider did not honor the requested protection opt-outs. The bind completed, but Maester did not issue a directory query after the bind. Maester does not change host or DC policy. $($_.Exception.Message)" + } + } Write-Verbose "LDAP bind to '$Server' on port $effectivePort succeeded" return $connection } diff --git a/powershell/internal/services/ad/protocol/Write-MtLdapProtectionWarning.ps1 b/powershell/internal/services/ad/protocol/Write-MtLdapProtectionWarning.ps1 new file mode 100644 index 0000000000..106a0e5b9f --- /dev/null +++ b/powershell/internal/services/ad/protocol/Write-MtLdapProtectionWarning.ps1 @@ -0,0 +1,33 @@ +function Write-MtLdapProtectionWarning { + [CmdletBinding()] + param( + [Parameter(Mandatory)] + [string]$Server, + + [int]$Port, + + [Parameter(Mandatory)] + [string]$AuthType, + + [switch]$ForceUnsigned + ) + + $safeServer = $Server -replace '(?i)(ldap(?:s)?://)?[^/\s:@]+:[^@\s/]+@', '$1@' + $warning = "INSECURE LDAP to '$safeServer' on port ${Port}: TLS and SASL sealing are explicitly disabled. LDAP queries and results have no confidentiality protection and may be read on the network. " + if ($AuthType -eq 'Basic') { + $warning += 'Basic/simple bind sends the username and password in plaintext to a server that this connection does not authenticate. LDAP messages are unsigned, have no integrity protection, and may be altered. ' + } else { + $warning += switch ($AuthType) { + 'Kerberos' { 'Kerberos exchanges tickets and authenticators, not the plaintext password. ' } + 'Ntlm' { 'NTLM exchanges challenge-response tokens, not the plaintext password; these tokens can be relayed or attacked by offline password cracking. ' } + default { 'Negotiate may select Kerberos or NTLM; it exchanges authentication tokens, not the plaintext password. NTLM tokens can be relayed or attacked by offline password cracking. ' } + } + if ($ForceUnsigned.IsPresent) { + $warning += 'SASL signing is explicitly disabled: LDAP messages have no integrity protection and may be altered or relayed. ' + } else { + $warning += 'SASL signing is left to platform policy and is not required by Maester. Signing, if negotiated, protects message integrity, not confidentiality. ' + } + } + $warning += 'Use only in isolated test environments. These settings affect LDAP, not SMB/SYSVOL or WinRM. Every new bind repeats this warning and, for Basic, sends the password again.' + Write-Warning $warning +} diff --git a/powershell/public/connect/Connect-Maester.ps1 b/powershell/public/connect/Connect-Maester.ps1 index 723fa64c30..d7e6e54cfa 100644 --- a/powershell/public/connect/Connect-Maester.ps1 +++ b/powershell/public/connect/Connect-Maester.ps1 @@ -36,6 +36,22 @@ Validates connectivity to the current Active Directory domain. Active Directory must be explicitly selected and is not included in -Service All. +.EXAMPLE + Connect-Maester -Service ActiveDirectory -ActiveDirectoryServer 'dc01.contoso.com' -ActiveDirectoryTlsMode None -ActiveDirectoryForceUnencrypted + + Disables LDAP TLS and SASL sealing for an isolated test lab only, exposing directory queries and results. Both options are required; Auto never falls back to LDAP without TLS. Kerberos/NTLM exchange authentication tokens, not the plaintext password. Signing is left to platform policy and, if negotiated, protects integrity, not confidentiality. Unsupported protection opt-outs fail before a directory query. + +.EXAMPLE + Connect-Maester -Service ActiveDirectory -ActiveDirectoryServer 'dc01.contoso.com' -ActiveDirectoryTlsMode None -ActiveDirectoryForceUnencrypted -ActiveDirectoryForceUnsigned + + Additionally disables SASL signing for an isolated test lab. Queries and results have neither confidentiality nor message integrity protection. The connection fails if client or DC policy prevents the requested opt-outs. Maester does not change host or DC policy. These settings affect LDAP, not SMB/SYSVOL or WinRM. + +.EXAMPLE + $credential = Get-Credential + Connect-Maester -Service ActiveDirectory -ActiveDirectoryServer 'dc01.contoso.com' -ActiveDirectoryCredential $credential -ActiveDirectoryAuthMode Basic -ActiveDirectoryTlsMode None -ActiveDirectoryForceUnencrypted -ActiveDirectoryAllowPlaintextPassword + + Explicitly permits a Basic/simple bind that transmits the username and password in plaintext to a server this connection does not authenticate. Queries and results are also plaintext and unsigned. Use only in an isolated test lab. An explicit server is required, but does not authenticate that server. Every reconnect sends the password again and repeats the warning. Do not use ForceUnsigned with Basic; simple binds have no SASL signing. + .EXAMPLE Connect-Maester -Service Azure,Graph @@ -202,11 +218,20 @@ [ValidateSet('Negotiate', 'Kerberos', 'Ntlm', 'Basic')] [string]$ActiveDirectoryAuthMode = 'Negotiate', - # The TLS mode used for the Active Directory LDAP connection. Auto tries LDAPS before StartTLS. + # The TLS mode used for Active Directory LDAP. Auto tries LDAPS before StartTLS and never falls back to plaintext. None requires -ActiveDirectoryForceUnencrypted and disables TLS and SASL sealing for isolated test environments only. [Alias('TlsMode')] - [ValidateSet('Auto', 'Ldaps', 'StartTls')] + [ValidateSet('Auto', 'Ldaps', 'StartTls', 'None')] [string]$ActiveDirectoryTlsMode = 'Auto', + # Acknowledge disabling both LDAP TLS and SASL sealing with -ActiveDirectoryTlsMode None. Queries and results lose confidentiality; SASL signing remains platform-dependent unless -ActiveDirectoryForceUnsigned is also given. Unsupported opt-outs fail closed. Intended for isolated test environments only. + [switch]$ActiveDirectoryForceUnencrypted, + + # Explicitly disable SASL signing (message integrity). Requires TLS mode None and ForceUnencrypted, and is not valid with Basic. Fails if the provider or policy re-enables signing; Maester never changes host policy. Intended for isolated test environments only. + [switch]$ActiveDirectoryForceUnsigned, + + # Additionally acknowledge sending the actual Basic/simple-bind username and password in plaintext, with unsigned queries and results. Requires TLS mode None, ForceUnencrypted, Basic authentication, an explicit server and a credential. Repeated binds transmit the password again. Intended for isolated test environments only. + [switch]$ActiveDirectoryAllowPlaintextPassword, + # Skip certificate validation for the Active Directory LDAP connection. Intended for test environments only. [switch]$ActiveDirectorySkipCertificateCheck ) @@ -666,6 +691,16 @@ } } + if ($ActiveDirectoryForceUnencrypted.IsPresent) { + $connectAdParameters['ForceUnencrypted'] = $true + } + if ($ActiveDirectoryForceUnsigned.IsPresent) { + $connectAdParameters['ForceUnsigned'] = $true + } + if ($ActiveDirectoryAllowPlaintextPassword.IsPresent) { + $connectAdParameters['AllowPlaintextPassword'] = $true + } + if ($ActiveDirectorySkipCertificateCheck.IsPresent) { $connectAdParameters['SkipCertificateCheck'] = $true } @@ -677,7 +712,17 @@ $__MtSession.ADConnection.ResolvedForest, $__MtSession.ADConnection.AuthenticationMode, $__MtSession.ADConnection.TlsMode) - Add-ConnectionResult -Name 'Active Directory' -Status 'Connected' -Details ("{0} ({1})" -f $__MtSession.ADConnection.ResolvedDomain, $__MtSession.ADConnection.ResolvedServer) + $adDetails = "{0} ({1})" -f $__MtSession.ADConnection.ResolvedDomain, $__MtSession.ADConnection.ResolvedServer + if ($__MtSession.ADConnection.TlsMode -eq 'None') { + $adDetails += ' - TLS/sealing disabled (test lab only)' + if ($__MtSession.ADConnection.ForceUnsigned -or $__MtSession.ADConnection.AuthenticationMode -eq 'Basic') { + $adDetails += ', unsigned' + } + if ($__MtSession.ADConnection.AllowPlaintextPassword) { + $adDetails += ', plaintext password' + } + } + Add-ConnectionResult -Name 'Active Directory' -Status 'Connected' -Details $adDetails } catch { Write-Verbose "Failed to connect to Active Directory: $($_.Exception.Message)" Add-ConnectionResult -Name 'Active Directory' -Status 'Failed' -Details (Get-ErrorSummary $_) diff --git a/powershell/public/services/ad/Get-MtADDacls.ps1 b/powershell/public/services/ad/Get-MtADDacls.ps1 index 96ada4b20a..d1eee0d590 100644 --- a/powershell/public/services/ad/Get-MtADDacls.ps1 +++ b/powershell/public/services/ad/Get-MtADDacls.ps1 @@ -59,6 +59,7 @@ } elseif ($__MtSession.ADConnection.ResolvedDomain) { ":$($__MtSession.ADConnection.ResolvedDomain)" } else { '' } + $targetSuffix += Get-MtAdLdapProtectionCacheSuffix -ConnectionState $__MtSession.ADConnection $cacheKey = "Dacls$targetSuffix" if ($Refresh -or -not $__MtSession.ADCache.ContainsKey($cacheKey)) { @@ -67,10 +68,13 @@ $protocolConnection = $null try { $protocolTargetParameters = @{ - AuthMode = $__MtSession.ADConnection.RequestedAuthMode - TlsMode = $__MtSession.ADConnection.RequestedTlsMode - SkipCertificateCheck = [bool]$__MtSession.ADConnection.SkipCertificateCheck - PassThru = $true + AuthMode = $__MtSession.ADConnection.RequestedAuthMode + TlsMode = $__MtSession.ADConnection.RequestedTlsMode + SkipCertificateCheck = [bool]$__MtSession.ADConnection.SkipCertificateCheck + ForceUnencrypted = [bool]$__MtSession.ADConnection.ForceUnencrypted + ForceUnsigned = [bool]$__MtSession.ADConnection.ForceUnsigned + AllowPlaintextPassword = [bool]$__MtSession.ADConnection.AllowPlaintextPassword + PassThru = $true } if ($__MtSession.ADConnection.RequestedServer) { $protocolTargetParameters['ActiveDirectoryServer'] = $__MtSession.ADConnection.RequestedServer diff --git a/powershell/public/services/ad/Get-MtADDomainState.ps1 b/powershell/public/services/ad/Get-MtADDomainState.ps1 index e31cf1e903..a258e809f3 100644 --- a/powershell/public/services/ad/Get-MtADDomainState.ps1 +++ b/powershell/public/services/ad/Get-MtADDomainState.ps1 @@ -595,6 +595,7 @@ function Get-MtADDomainState { } elseif ($__MtSession.ADConnection.ResolvedDomain) { ":$($__MtSession.ADConnection.ResolvedDomain)" } else { '' } + $targetSuffix += Get-MtAdLdapProtectionCacheSuffix -ConnectionState $__MtSession.ADConnection $metadataCacheKey = "DomainState:Metadata$targetSuffix$computerSuffix" # Initialize domain state with safe defaults for all properties @@ -700,10 +701,13 @@ function Get-MtADDomainState { } $protocolTargetParameters = @{ - AuthMode = $__MtSession.ADConnection.RequestedAuthMode - TlsMode = $effectiveTlsMode - SkipCertificateCheck = [bool]$__MtSession.ADConnection.SkipCertificateCheck - PassThru = $true + AuthMode = $__MtSession.ADConnection.RequestedAuthMode + TlsMode = $effectiveTlsMode + SkipCertificateCheck = [bool]$__MtSession.ADConnection.SkipCertificateCheck + ForceUnencrypted = [bool]$__MtSession.ADConnection.ForceUnencrypted + ForceUnsigned = [bool]$__MtSession.ADConnection.ForceUnsigned + AllowPlaintextPassword = [bool]$__MtSession.ADConnection.AllowPlaintextPassword + PassThru = $true } if ($ComputerName) { $protocolTargetParameters['ActiveDirectoryServer'] = $ComputerName @@ -725,14 +729,18 @@ function Get-MtADDomainState { $protocolConnection = New-MtAdProtocolConnection -ProtocolEvidence $protocolConnectionState $protocolRootDse = Get-MtLdapRootDse -Connection $protocolConnection $protocolEvidence = [PSCustomObject]@{ - ResolvedServer = $protocolConnectionState.ResolvedServer - ResolvedDomain = $protocolConnectionState.ResolvedDomain - ResolvedForest = $protocolConnectionState.ResolvedForest - AuthenticationMode = $protocolConnectionState.AuthenticationMode - TlsMode = $protocolConnectionState.TlsMode - SkipCertificateCheck = [bool]$protocolConnectionState.SkipCertificateCheck - DefaultNamingContext = $protocolRootDse.DefaultNamingContext - VerifiedAt = Get-Date + RequestedServer = $protocolConnectionState.RequestedServer + ResolvedServer = $protocolConnectionState.ResolvedServer + ResolvedDomain = $protocolConnectionState.ResolvedDomain + ResolvedForest = $protocolConnectionState.ResolvedForest + AuthenticationMode = $protocolConnectionState.AuthenticationMode + TlsMode = $protocolConnectionState.TlsMode + SkipCertificateCheck = [bool]$protocolConnectionState.SkipCertificateCheck + ForceUnencrypted = [bool]$protocolConnectionState.ForceUnencrypted + ForceUnsigned = [bool]$protocolConnectionState.ForceUnsigned + AllowPlaintextPassword = [bool]$protocolConnectionState.AllowPlaintextPassword + DefaultNamingContext = $protocolRootDse.DefaultNamingContext + VerifiedAt = Get-Date } $collectionTime = Get-Date diff --git a/powershell/public/services/ad/Get-MtADGpoState.ps1 b/powershell/public/services/ad/Get-MtADGpoState.ps1 index 4a8b236ba1..3612ab6dca 100644 --- a/powershell/public/services/ad/Get-MtADGpoState.ps1 +++ b/powershell/public/services/ad/Get-MtADGpoState.ps1 @@ -46,6 +46,7 @@ function Get-MtADGpoState { $resolvedDomain = [string]$__MtSession.ADConnection.ResolvedDomain $cacheKey = if ($resolvedDomain) { "GpoState:$resolvedDomain" } else { 'GpoState' } + $cacheKey += Get-MtAdLdapProtectionCacheSuffix -ConnectionState $__MtSession.ADConnection if ($Refresh -or -not $__MtSession.ADCache.ContainsKey($cacheKey)) { Write-Verbose 'Collecting AD GPO State data from Active Directory' @@ -53,10 +54,13 @@ function Get-MtADGpoState { $protocolConnection = $null try { $protocolTargetParameters = @{ - AuthMode = $__MtSession.ADConnection.RequestedAuthMode - TlsMode = $__MtSession.ADConnection.RequestedTlsMode - SkipCertificateCheck = [bool]$__MtSession.ADConnection.SkipCertificateCheck - PassThru = $true + AuthMode = $__MtSession.ADConnection.RequestedAuthMode + TlsMode = $__MtSession.ADConnection.RequestedTlsMode + SkipCertificateCheck = [bool]$__MtSession.ADConnection.SkipCertificateCheck + ForceUnencrypted = [bool]$__MtSession.ADConnection.ForceUnencrypted + ForceUnsigned = [bool]$__MtSession.ADConnection.ForceUnsigned + AllowPlaintextPassword = [bool]$__MtSession.ADConnection.AllowPlaintextPassword + PassThru = $true } if ($__MtSession.ADConnection.RequestedServer) { $protocolTargetParameters['ActiveDirectoryServer'] = $__MtSession.ADConnection.RequestedServer diff --git a/powershell/tests/functions/ActiveDirectoryLdapProtection.Tests.ps1 b/powershell/tests/functions/ActiveDirectoryLdapProtection.Tests.ps1 new file mode 100644 index 0000000000..9f394f0470 --- /dev/null +++ b/powershell/tests/functions/ActiveDirectoryLdapProtection.Tests.ps1 @@ -0,0 +1,175 @@ +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'Fake credentials for isolated unit tests.')] +param() + +BeforeAll { + Import-Module "$PSScriptRoot/../../Maester.psd1" -Force +} + +Describe 'Explicit LDAP protection opt-outs' { + BeforeEach { + InModuleScope Maester { + $script:protectionClient = [PSCustomObject]@{ + SessionOptions = [PSCustomObject]@{ ProtocolVersion = 0; ReferralChasing = 0; SecureSocketLayer = $false; Signing = $true; Sealing = $true } + AuthType = $null + Timeout = $null + Credential = $null + BindCount = 0 + DisposeCount = 0 + SigningAtBind = $null + SealingAtBind = $null + } + $script:protectionClient | Add-Member ScriptMethod Bind { + $this.BindCount++ + $this.SigningAtBind = $this.SessionOptions.Signing + $this.SealingAtBind = $this.SessionOptions.Sealing + } + $script:protectionClient | Add-Member ScriptMethod Dispose { $this.DisposeCount++ } + Mock New-Object { $script:protectionClient } -ParameterFilter { $TypeName -eq 'System.DirectoryServices.Protocols.LdapConnection' } + Mock Write-Warning { } + } + } + + It 'Disables sealing before bind but leaves signing untouched unless requested' { + InModuleScope Maester { + $null = New-MtLdapConnection -Server localhost -Port 389 -ForceUnencrypted + $script:protectionClient.BindCount | Should -Be 1 + $script:protectionClient.SealingAtBind | Should -BeFalse + $script:protectionClient.SigningAtBind | Should -BeTrue + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -like '*queries and results*' -and $Message -like '*Signing, if negotiated, protects message integrity, not confidentiality*' } + } + } + + It 'Disables signing and sealing before bind with both explicit opt-ins' { + InModuleScope Maester { + $null = New-MtLdapConnection -Server localhost -Port 389 -ForceUnencrypted -ForceUnsigned + $script:protectionClient.SigningAtBind | Should -BeFalse + $script:protectionClient.SealingAtBind | Should -BeFalse + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -like '*SASL signing is explicitly disabled*' -and $Message -like '*not the plaintext password*' -and $Message -like '*not SMB/SYSVOL or WinRM*' } + } + } + + It 'Requires a lower-layer opt-in for LDAP without TLS' { + InModuleScope Maester { + { New-MtLdapConnection -Server localhost -Port 389 } | Should -Throw '*requires -ForceUnencrypted*' + $script:protectionClient.BindCount | Should -Be 0 + } + } + + It 'Requires the additional password acknowledgement for Basic without TLS' { + InModuleScope Maester { + { New-MtLdapConnection -Server localhost -Port 389 -AuthType Basic -ForceUnencrypted } | Should -Throw '*Basic authentication requires LDAPS*' + $script:protectionClient.BindCount | Should -Be 0 + } + } + + It 'Allows acknowledged plaintext Basic without writing SASL options' { + InModuleScope Maester { + $credential = [PSCredential]::new('fixture', (ConvertTo-SecureString 'fake-password' -AsPlainText -Force)) + $null = New-MtLdapConnection -Server localhost -Port 389 -AuthType Basic -Credential $credential -ForceUnencrypted -AllowPlaintextPassword + $script:protectionClient.BindCount | Should -Be 1 + $script:protectionClient.SigningAtBind | Should -BeTrue + $script:protectionClient.SealingAtBind | Should -BeTrue + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -like '*username and password in plaintext*' -and $Message -like '*unsigned, have no integrity protection*' -and $Message -like '*localhost*' -and $Message -notlike '*fake-password*' } + } + } + + It 'Does not write SASL options or warn on the encrypted default' { + InModuleScope Maester { + $null = New-MtLdapConnection -Server localhost -ForceUnencrypted + $script:protectionClient.SigningAtBind | Should -BeTrue + $script:protectionClient.SealingAtBind | Should -BeTrue + $script:protectionClient.SessionOptions.SecureSocketLayer | Should -BeTrue + Should -Invoke Write-Warning -Times 0 -Exactly + } + } + + It 'Rejects unsigned or plaintext-password opt-ins on an encrypted transport' { + InModuleScope Maester { + { New-MtLdapConnection -Server localhost -ForceUnencrypted -ForceUnsigned } | Should -Throw '*ForceUnsigned requires LDAP without TLS*' + { New-MtLdapConnection -Server localhost -AuthType Basic -ForceUnencrypted -AllowPlaintextPassword } | Should -Throw '*AllowPlaintextPassword requires Basic authentication without TLS*' + $script:protectionClient.BindCount | Should -Be 0 + } + } + + It 'Rejects the redundant unsigned option for Basic' { + InModuleScope Maester { + { New-MtLdapConnection -Server localhost -Port 389 -AuthType Basic -ForceUnencrypted -AllowPlaintextPassword -ForceUnsigned } | Should -Throw '*ForceUnsigned requires*' + $script:protectionClient.BindCount | Should -Be 0 + } + } + + It 'Fails before bind when the provider cannot disable sealing' { + InModuleScope Maester { + $script:protectionClient.SessionOptions | Add-Member ScriptProperty Sealing { $true } { throw 'Unsupported LDAP option' } -Force + { New-MtLdapConnection -Server localhost -Port 389 -ForceUnencrypted } | Should -Throw '*cannot honor*No bind was attempted*' + $script:protectionClient.BindCount | Should -Be 0 + $script:protectionClient.DisposeCount | Should -Be 1 + } + } + + It 'Fails and disposes after bind if policy re-enables signing' { + InModuleScope Maester { + $script:protectionClient | Add-Member ScriptMethod Bind { $this.BindCount++; $this.SessionOptions.Signing = $true } -Force + { New-MtLdapConnection -Server localhost -Port 389 -ForceUnencrypted -ForceUnsigned } | Should -Throw '*SASL signing was re-enabled*' + $script:protectionClient.BindCount | Should -Be 1 + $script:protectionClient.DisposeCount | Should -Be 1 + } + } + + It 'Fails and disposes after bind if policy re-enables sealing' { + InModuleScope Maester { + $script:protectionClient | Add-Member ScriptMethod Bind { $this.BindCount++; $this.SessionOptions.Sealing = $true } -Force + { New-MtLdapConnection -Server localhost -Port 389 -ForceUnencrypted } | Should -Throw '*SASL sealing was re-enabled*' + $script:protectionClient.DisposeCount | Should -Be 1 + } + } + + It 'Rejects cached None evidence without an explicit opt-in' { + InModuleScope Maester { + { New-MtAdProtocolConnection -ProtocolEvidence ([PSCustomObject]@{ TlsMode = 'None'; ResolvedServer = 'localhost'; AuthenticationMode = 'Negotiate' }) } | Should -Throw '*requires the explicit ForceUnencrypted opt-in*' + $script:protectionClient.BindCount | Should -Be 0 + } + } + + It 'Reconstructs and applies every unsigned token-based opt-in' { + InModuleScope Maester { + $connection = New-MtAdProtocolConnection -ProtocolEvidence ([PSCustomObject]@{ TlsMode = 'None'; ResolvedServer = 'localhost'; AuthenticationMode = 'Negotiate'; ForceUnencrypted = $true; ForceUnsigned = $true }) + $script:protectionClient.SigningAtBind | Should -BeFalse + $script:protectionClient.SealingAtBind | Should -BeFalse + $connection.Dispose() + } + } + + It 'Pins plaintext-password reuse to the requested server, not the RootDSE name' { + InModuleScope Maester { + Mock New-MtLdapConnection { $script:protectionClient } + $connection = New-MtAdProtocolConnection -ProtocolEvidence ([PSCustomObject]@{ TlsMode = 'None'; RequestedServer = 'chosen.example'; ResolvedServer = 'untrusted.example'; AuthenticationMode = 'Basic'; ForceUnencrypted = $true; AllowPlaintextPassword = $true }) + Should -Invoke New-MtLdapConnection -Times 1 -Exactly -ParameterFilter { $Server -eq 'chosen.example' -and $ForceUnencrypted -and $AllowPlaintextPassword } + $connection.Dispose() + } + } + + It 'Rejects plaintext-password reuse without a requested server' { + InModuleScope Maester { + { New-MtAdProtocolConnection -ProtocolEvidence ([PSCustomObject]@{ TlsMode = 'None'; ResolvedServer = 'untrusted.example'; AuthenticationMode = 'Basic'; ForceUnencrypted = $true; AllowPlaintextPassword = $true }) } | Should -Throw '*requires an explicitly requested server*' + } + } + + It 'Partitions None caches by protection choices without changing encrypted cache keys' { + InModuleScope Maester { + $signed = [PSCustomObject]@{ TlsMode = 'None'; AuthenticationMode = 'Negotiate'; ForceUnsigned = $false; AllowPlaintextPassword = $false } + $unsigned = [PSCustomObject]@{ TlsMode = 'None'; AuthenticationMode = 'Negotiate'; ForceUnsigned = $true; AllowPlaintextPassword = $false } + $basic = [PSCustomObject]@{ TlsMode = 'None'; AuthenticationMode = 'Basic'; ForceUnsigned = $false; AllowPlaintextPassword = $true } + (Get-MtAdLdapProtectionCacheSuffix $signed) | Should -Not -Be (Get-MtAdLdapProtectionCacheSuffix $unsigned) + (Get-MtAdLdapProtectionCacheSuffix $basic) | Should -Not -Be (Get-MtAdLdapProtectionCacheSuffix $signed) + Get-MtAdLdapProtectionCacheSuffix ([PSCustomObject]@{ TlsMode = 'Ldaps' }) | Should -Be '' + } + } + + It 'Redacts userinfo if a server argument contains credentials' { + InModuleScope Maester { + Write-MtLdapProtectionWarning -Server 'ldap://fixture:fake-password@localhost' -Port 389 -AuthType Basic + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -like '*@localhost*' -and $Message -notlike '*fake-password*' } + } + } +} diff --git a/powershell/tests/functions/ActiveDirectoryProtocol.Tests.ps1 b/powershell/tests/functions/ActiveDirectoryProtocol.Tests.ps1 index 4cdb32a7bb..fcb06caffd 100644 --- a/powershell/tests/functions/ActiveDirectoryProtocol.Tests.ps1 +++ b/powershell/tests/functions/ActiveDirectoryProtocol.Tests.ps1 @@ -294,6 +294,225 @@ Describe 'Active Directory Protocol Contracts' -Skip:(-not $script:HasDirectoryS } } + Describe 'Unencrypted LDAP mode (TlsMode None)' { + BeforeEach { + InModuleScope Maester { + $__MtSession.ADConnection = $null + $__MtSession.ADCredential = $null + $__MtSession.ADCache = @{} + } + Mock Test-MtAdProtocolPrerequisites -ModuleName Maester { + [PSCustomObject]@{ + IsReady = $true + AuthModes = @('Negotiate', 'Kerberos', 'Ntlm', 'Basic') + TlsModes = @('Ldaps', 'StartTls', 'None') + PlatformProfile = 'WindowsPS7' + MissingPrerequisites = @() + } + } + Mock New-MtLdapConnection -ModuleName Maester { + $id = New-Object System.DirectoryServices.Protocols.LdapDirectoryIdentifier @('localhost', 389, $false, $false) + New-Object System.DirectoryServices.Protocols.LdapConnection @($id) + } + Mock Get-MtLdapRootDse -ModuleName Maester { + [PSCustomObject]@{ + DefaultNamingContext = 'DC=contoso,DC=com' + RootDomainNamingContext = 'DC=contoso,DC=com' + ConfigurationNamingContext = 'CN=Configuration,DC=contoso,DC=com' + SchemaNamingContext = 'CN=Schema,CN=Configuration,DC=contoso,DC=com' + DnsHostName = 'dc01.contoso.com' + } + } + Mock Invoke-MtLdapSearch -ModuleName Maester { + [PSCustomObject]@{ dnsRoot = 'contoso.com'; nCName = 'DC=contoso,DC=com'; trustParent = $null } + } + Mock Write-Warning -ModuleName Maester { } + } + + AfterEach { + InModuleScope Maester { + $__MtSession.ADConnection = $null + $__MtSession.ADCredential = $null + $__MtSession.ADCache = @{} + } + } + + It 'Connect-MtAdTarget with -TlsMode None without -ForceUnencrypted should throw' { + InModuleScope Maester { + { Connect-MtAdTarget -ActiveDirectoryServer 'dc01.contoso.com' -TlsMode 'None' -AuthMode 'Negotiate' } | + Should -Throw -ExpectedMessage "*TLS mode 'None' requires -ForceUnencrypted.*" + Should -Invoke New-MtLdapConnection -Times 0 -Exactly + } + } + + It 'Connect-MtAdTarget with -TlsMode None and -ForceUnencrypted should succeed' { + InModuleScope Maester { + Connect-MtAdTarget -ActiveDirectoryServer 'dc01.contoso.com' -TlsMode 'None' -AuthMode 'Negotiate' -ForceUnencrypted + $__MtSession.ADConnection.TlsMode | Should -Be 'None' + $__MtSession.ADConnection.ForceUnencrypted | Should -BeTrue + Should -Invoke New-MtLdapConnection -Times 1 -Exactly -ParameterFilter { $Port -eq 389 -and -not $UseStartTls } + } + } + + It 'Connect-MtAdTarget with -TlsMode None and -AuthMode Basic should throw' { + InModuleScope Maester { + $cred = [PSCredential]::new('CONTOSO\user', (ConvertTo-SecureString 'pass' -AsPlainText -Force)) + { Connect-MtAdTarget -ActiveDirectoryServer 'dc01.contoso.com' -TlsMode 'None' -AuthMode 'Basic' -ForceUnencrypted -ActiveDirectoryCredential $cred } | + Should -Throw -ExpectedMessage '*Basic authentication without TLS requires the additional -AllowPlaintextPassword acknowledgement*' + Should -Invoke New-MtLdapConnection -Times 0 -Exactly + } + } + + It 'New-MtAdProtocolConnection with TlsMode None uses port 389 without StartTLS' { + InModuleScope Maester { + $evidence = [PSCustomObject]@{ + ResolvedServer = 'dc01.contoso.com' + AuthenticationMode = 'Negotiate' + TlsMode = 'None' + ForceUnencrypted = $true + SkipCertificateCheck = $false + } + + $connection = New-MtAdProtocolConnection -ProtocolEvidence $evidence + $connection.Dispose() + Should -Invoke New-MtLdapConnection -Times 1 -Exactly -ParameterFilter { $Port -eq 389 -and -not $UseStartTls } + } + } + + It 'Preserves the opt-in for domain-state re-probes and reads' { + InModuleScope Maester { + Mock Get-MtLdapDomain { [PSCustomObject]@{ Name = 'contoso.com' } } + Connect-MtAdTarget -ActiveDirectoryServer 'dc01.contoso.com' -TlsMode None -ForceUnencrypted + $state = Get-MtADDomainState -Categories Domain -Refresh -ErrorAction Stop + $state.Domain.Name | Should -Be 'contoso.com' + $state.ProtocolEvidence.TlsMode | Should -Be 'None' + $__MtSession.ADConnection.ForceUnencrypted | Should -BeTrue + Should -Invoke New-MtLdapConnection -Times 3 -Exactly -ParameterFilter { $Port -eq 389 -and -not $UseStartTls } + } + } + + It 'Preserves the opt-in for Group Policy re-probes and reads' { + InModuleScope Maester { + Mock Get-MtLdapGpo { @() } + Mock Get-MtLdapGpoLink { @() } + Connect-MtAdTarget -ActiveDirectoryServer 'dc01.contoso.com' -TlsMode None -ForceUnencrypted + Get-MtADGpoState -Refresh -ErrorAction Stop | Should -Not -BeNullOrEmpty + Should -Invoke New-MtLdapConnection -Times 3 -Exactly -ParameterFilter { $Port -eq 389 -and -not $UseStartTls } + } + } + + It 'Preserves the opt-in for DACL re-probes and reads' -Skip:(-not $IsWindows) { + InModuleScope Maester { + Mock Get-MtLdapDacl { [PSCustomObject]@{ DistinguishedName = 'DC=contoso,DC=com' } } + Connect-MtAdTarget -ActiveDirectoryServer 'dc01.contoso.com' -TlsMode None -ForceUnencrypted + Get-MtADDacls -Refresh -ErrorAction Stop | Should -Not -BeNullOrEmpty + Should -Invoke New-MtLdapConnection -Times 3 -Exactly -ParameterFilter { $Port -eq 389 -and -not $UseStartTls } + } + } + + It 'Forwards the public opt-in and TLS mode' { + Connect-Maester -Service ActiveDirectory -ActiveDirectoryServer 'dc01.contoso.com' -ActiveDirectoryTlsMode None -ActiveDirectoryForceUnencrypted + InModuleScope Maester { + $__MtSession.ADConnection.Connected | Should -BeTrue + $__MtSession.ADConnection.ForceUnencrypted | Should -BeTrue + $__MtSession.ADConnection.TlsMode | Should -Be 'None' + } + } + + It 'Keeps the public default encrypted without an opt-in' { + Connect-Maester -Service ActiveDirectory -ActiveDirectoryServer 'dc01.contoso.com' + InModuleScope Maester { + $__MtSession.ADConnection.ForceUnencrypted | Should -BeFalse + $__MtSession.ADConnection.TlsMode | Should -Be 'Ldaps' + } + Should -Invoke Write-Warning -ModuleName Maester -Times 0 -Exactly + } + + It 'Preserves opt-ins through the public connection and reads' -ForEach @( + @{ Protection = 'unsigned'; Collector = 'domain' } + @{ Protection = 'unsigned'; Collector = 'gpo' } + @{ Protection = 'plaintext-password'; Collector = 'domain' } + @{ Protection = 'plaintext-password'; Collector = 'gpo' } + ) { + InModuleScope Maester -Parameters @{ Protection = $Protection; Collector = $Collector } { + param($Protection, $Collector) + Mock Get-MtLdapDomain { [PSCustomObject]@{ Name = 'contoso.com' } } + Mock Get-MtLdapGpo { @() } + Mock Get-MtLdapGpoLink { @() } + $parameters = @{ Service = 'ActiveDirectory'; ActiveDirectoryServer = 'dc01.contoso.com'; ActiveDirectoryTlsMode = 'None'; ActiveDirectoryForceUnencrypted = $true } + if ($Protection -eq 'unsigned') { + $parameters.ActiveDirectoryForceUnsigned = $true + } else { + $parameters.ActiveDirectoryAuthMode = 'Basic' + $parameters.ActiveDirectoryAllowPlaintextPassword = $true + $parameters.ActiveDirectoryCredential = [PSCredential]::new('fixture', (ConvertTo-SecureString 'fake' -AsPlainText -Force)) + } + Connect-Maester @parameters + if ($Collector -eq 'domain') { + $state = Get-MtADDomainState -Categories Domain -Refresh -ErrorAction Stop + $state.ProtocolEvidence.ForceUnsigned | Should -Be ($Protection -eq 'unsigned') + $state.ProtocolEvidence.AllowPlaintextPassword | Should -Be ($Protection -eq 'plaintext-password') + } else { + Get-MtADGpoState -Refresh -ErrorAction Stop | Should -Not -BeNullOrEmpty + } + Should -Invoke New-MtLdapConnection -Times 3 -Exactly -ParameterFilter { + $Port -eq 389 -and $ForceUnencrypted -and + ([bool]$ForceUnsigned -eq ($Protection -eq 'unsigned')) -and + ([bool]$AllowPlaintextPassword -eq ($Protection -eq 'plaintext-password')) + } + } + } + + It 'Preserves opt-ins through DACL reads' -Skip:(-not $IsWindows) -ForEach @( + @{ Protection = 'unsigned' } + @{ Protection = 'plaintext-password' } + ) { + InModuleScope Maester -Parameters @{ Protection = $Protection } { + param($Protection) + Mock Get-MtLdapDacl { [PSCustomObject]@{ DistinguishedName = 'DC=contoso,DC=com' } } + $parameters = @{ ActiveDirectoryServer = 'dc01.contoso.com'; TlsMode = 'None'; ForceUnencrypted = $true } + if ($Protection -eq 'unsigned') { + $parameters.ForceUnsigned = $true + } else { + $parameters.AuthMode = 'Basic' + $parameters.AllowPlaintextPassword = $true + $parameters.ActiveDirectoryCredential = [PSCredential]::new('fixture', (ConvertTo-SecureString 'fake' -AsPlainText -Force)) + } + Connect-MtAdTarget @parameters + Get-MtADDacls -Refresh -ErrorAction Stop | Should -Not -BeNullOrEmpty + Should -Invoke New-MtLdapConnection -Times 3 -Exactly -ParameterFilter { + $Port -eq 389 -and $ForceUnencrypted -and + ([bool]$ForceUnsigned -eq ($Protection -eq 'unsigned')) -and + ([bool]$AllowPlaintextPassword -eq ($Protection -eq 'plaintext-password')) + } + } + } + + It 'Rejects incompatible opt-ins before creating a connection' { + InModuleScope Maester { + { Connect-MtAdTarget -ActiveDirectoryServer 'dc01.contoso.com' -ForceUnsigned } | Should -Throw '*ForceUnsigned requires*' + { Connect-MtAdTarget -ActiveDirectoryServer 'dc01.contoso.com' -TlsMode None -ForceUnencrypted -AllowPlaintextPassword } | Should -Throw '*AllowPlaintextPassword requires*' + { Connect-MtAdTarget -ActiveDirectoryDomain 'contoso.com' -TlsMode None -ForceUnencrypted -AuthMode Basic -AllowPlaintextPassword } | Should -Throw '*requires an explicit -ActiveDirectoryServer*' + Should -Invoke New-MtLdapConnection -Times 0 -Exactly + } + } + + It ' never falls back to LDAP without TLS, even with the force switch' -ForEach @( + @{ Mode = 'Auto'; Attempts = 2 } + @{ Mode = 'Ldaps'; Attempts = 1 } + @{ Mode = 'StartTls'; Attempts = 1 } + ) { + InModuleScope Maester -Parameters @{ Mode = $Mode; Attempts = $Attempts } { + param($Mode, $Attempts) + Mock New-MtLdapConnection { throw 'TLS failed' } + { Connect-MtAdTarget -ActiveDirectoryServer 'dc01.contoso.com' -TlsMode $Mode -ForceUnencrypted } | Should -Throw + Should -Invoke New-MtLdapConnection -Times $Attempts -Exactly + Should -Invoke New-MtLdapConnection -Times 0 -Exactly -ParameterFilter { $Port -eq 389 -and -not $UseStartTls } + Should -Invoke Write-Warning -Times 0 -Exactly + } + } + } + Describe 'Basic-over-LDAPS success path' { BeforeEach { $script:bindCalled = $false diff --git a/website/docs/advanced/active-directory.md b/website/docs/advanced/active-directory.md index 234ff2d34e..78f3bec6c1 100644 --- a/website/docs/advanced/active-directory.md +++ b/website/docs/advanced/active-directory.md @@ -5,6 +5,14 @@ sidebar: order: 1 --- +## LDAP Connection Protection + +Active Directory LDAP connections use TLS by default. `Auto`, `Ldaps`, and `StartTls` retain their existing behavior. The explicit `None` mode and unencrypted-bind switches are lab-only; see [AD connection troubleshooting](/docs/connect-maester/ad-connection-troubleshooting/) for parameters and examples. + +TLS provides connection confidentiality and integrity and authenticates the server certificate. SASL sealing provides confidentiality; SASL signing provides integrity, not encryption. Disabling TLS and SASL sealing exposes LDAP queries and results to observation, while disabling signing also permits in-transit tampering. Basic/simple bind without TLS sends the password on the wire and does not authenticate the server; an explicitly named server is not proof of server identity. Negotiate, Kerberos, and NTLM exchange authentication tokens, not clear-text passwords. NTLM challenge-response material can be relayed or subjected to offline password cracking. + +These LDAP settings do not disable SMB/SYSVOL or WSMan protection. A domain controller's stronger-authentication or signing policy may reject an unencrypted bind; Maester does not change client or domain-controller policy to work around that rejection. + # Active Directory Permission Model - Version: 3.0 diff --git a/website/docs/connect-maester/ad-connection-troubleshooting.md b/website/docs/connect-maester/ad-connection-troubleshooting.md index e6fb9ca26e..17e97ed93e 100644 --- a/website/docs/connect-maester/ad-connection-troubleshooting.md +++ b/website/docs/connect-maester/ad-connection-troubleshooting.md @@ -1,6 +1,6 @@ --- title: Active Directory Connection Troubleshooting -description: "Fix Active Directory connection errors in Maester: TLS modes (LDAPS and StartTLS), domain controller certificate problems, the Linux and macOS StartTLS limit, and verbose diagnostics." +description: "Troubleshoot Active Directory connections, TLS modes, unencrypted LDAP options, certificate problems, and verbose diagnostics." sidebar: label: AD Connection Troubleshooting order: 3 @@ -8,15 +8,51 @@ sidebar: ## Overview - Maester uses System.DirectoryServices.Protocols.LdapConnection for cross-platform AD connectivity. -- TLS is required for all AD connections. +- TLS is used by default for AD connections. Explicit options can disable LDAP TLS for isolated troubleshooting, with significant security risks. ## Understanding TLS Modes -- The -ActiveDirectoryTlsMode parameter supports three values: Auto, Ldaps, StartTls. +- The -ActiveDirectoryTlsMode parameter supports four values: Auto, Ldaps, StartTls, None. - Auto (default): Tries LDAPS (port 636) first, then StartTLS (port 389) on Windows. On Linux/macOS, only tries LDAPS. - Ldaps: Uses LDAPS on port 636 only. - StartTls: Uses StartTLS on port 389 only. +- None: Requires the explicit lab-only opt-ins described below; disables TLS and SASL sealing. - Auto mode adapts to the platform due to a known .NET limitation. +## Unencrypted LDAP (Lab Use Only) + +`None` is an explicit opt-out from TLS, not a troubleshooting default. `Auto`, `Ldaps`, and `StartTls` are unchanged; Maester does not write SASL signing or sealing options on TLS connections. + +| Protection | What it provides | What happens when disabled | +|---|---|---| +| TLS | Confidentiality and integrity for the LDAP connection, plus server certificate authentication | LDAP traffic is not protected by TLS. With Basic authentication, the password is sent on the wire and the server is not authenticated. | +| SASL sealing | Confidentiality for SASL-protected LDAP traffic | Directory queries and results can be read by an observer. | +| SASL signing | Integrity, but not encryption | Directory queries and results can be tampered with in transit. | + +The following switches require `-ActiveDirectoryTlsMode None` and `-ActiveDirectoryForceUnencrypted` together: + +- `-ActiveDirectoryForceUnencrypted` explicitly disables LDAP TLS and SASL sealing. +- `-ActiveDirectoryForceUnsigned` additionally disables SASL signing of LDAP messages for Negotiate, Kerberos, or NTLM authentication. These mechanisms exchange authentication tokens, not clear-text passwords. NTLM challenge-response material can be relayed or subjected to offline password cracking; disabling signing also removes LDAP message integrity protection. This option is separate from `-ActiveDirectoryAllowPlaintextPassword` and cannot be combined with Basic authentication. +- `-ActiveDirectoryAllowPlaintextPassword` is a separate acknowledgement for Basic/simple bind, which sends the password without TLS. It requires `-ActiveDirectoryServer` to be explicitly set, as well as `None` and `ForceUnencrypted`. Basic does not use SASL signing or sealing options. Naming a server explicitly does not authenticate that server when TLS is off. + +These settings are strongly discouraged and intended only for controlled lab testing. Domain controllers can reject unencrypted binds, including with `strongerAuthRequired` or signing policy. Maester fails closed if the client provider cannot apply the requested signing/sealing settings or if policy re-enables signing that was explicitly disabled. Standard Linux OpenLDAP with .NET 8 or .NET 10 does not support the required signing/sealing setters, so token-based unencrypted mode fails clearly there. On Windows, `LDAPClientIntegrity=1` can cause Negotiate signing to be re-enabled; `ForceUnsigned` therefore fails until the client policy permits unsigned LDAP. Maester does not change host or domain-controller policy automatically. + +Maester emits a warning naming the target before each risky bind. Reused state, reprobes, or reconstructed connections create fresh binds, so warnings recur and Basic sends the password again. These LDAP options do not disable protection for SMB/SYSVOL or WSMan. + +Token-based authentication with signing and sealing explicitly disabled: + +```powershell +Connect-Maester -Service ActiveDirectory -ActiveDirectoryServer 'dc01.example.com' -ActiveDirectoryTlsMode None -ActiveDirectoryForceUnencrypted -ActiveDirectoryForceUnsigned +``` + +Basic/simple bind with its separate plaintext-password acknowledgement: + +```powershell +$credential = Get-Credential +Connect-Maester -Service ActiveDirectory -ActiveDirectoryServer 'dc01.example.com' -ActiveDirectoryAuthMode Basic -ActiveDirectoryTlsMode None -ActiveDirectoryForceUnencrypted -ActiveDirectoryAllowPlaintextPassword -ActiveDirectoryCredential $credential +``` + +Do not use these examples against production systems or untrusted networks. + ## Certificate Issues on the Domain Controller - Common symptoms: connection fails with certificate, trust, validation, expired, or chain errors. - Troubleshooting steps: From a14c6de0c827a7c0606586d54ac5b858a40748aa Mon Sep 17 00:00:00 2001 From: Michael <431932+soulemike@users.noreply.github.com> Date: Sat, 21 Nov 2026 05:47:32 -0700 Subject: [PATCH 2/3] Update Connect-Maester.ps1 --- powershell/public/connect/Connect-Maester.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/powershell/public/connect/Connect-Maester.ps1 b/powershell/public/connect/Connect-Maester.ps1 index c003760a76..bceeb80d0a 100644 --- a/powershell/public/connect/Connect-Maester.ps1 +++ b/powershell/public/connect/Connect-Maester.ps1 @@ -51,6 +51,8 @@ Connect-Maester -Service ActiveDirectory -ActiveDirectoryServer 'dc01.contoso.com' -ActiveDirectoryCredential $credential -ActiveDirectoryAuthMode Basic -ActiveDirectoryTlsMode None -ActiveDirectoryForceUnencrypted -ActiveDirectoryAllowPlaintextPassword Explicitly permits a Basic/simple bind that transmits the username and password in plaintext to a server this connection does not authenticate. Queries and results are also plaintext and unsigned. Use only in an isolated test lab. An explicit server is required, but does not authenticate that server. Every reconnect sends the password again and repeats the warning. Do not use ForceUnsigned with Basic; simple binds have no SASL signing. + +.EXAMPLE Connect-Maester -Service Graph,AzureDevOps -AzureDevOpsOrganization 'contoso' Connects to Microsoft Graph and the Azure DevOps organization 'contoso' for the Azure DevOps (AZDO) tests. The Azure DevOps connection needs the community ADOPS module (Install-Module ADOPS -Scope CurrentUser), which Maester does not install. When an Az context exists (for example after Connect-Maester -Service Azure), its access token is passed to Connect-ADOPS; otherwise Connect-ADOPS signs in interactively. The organization can also be set with AzureDevOpsOrganization in maester-config.json. From 93813895679d45e745b8f974c54175dd6cf0ca2b Mon Sep 17 00:00:00 2001 From: Snozz-Al Date: Sat, 21 Nov 2026 13:01:06 +0000 Subject: [PATCH 3/3] fix(ad): address PR analyzer findings --- .../ad/protocol/Get-MtAdLdapProtectionCacheSuffix.ps1 | 2 +- .../services/ad/protocol/Write-MtLdapProtectionWarning.ps1 | 2 +- powershell/public/connect/Connect-Maester.ps1 | 2 +- .../functions/ActiveDirectoryLdapProtection.Tests.ps1 | 2 +- .../tests/functions/ActiveDirectoryProtocol.Tests.ps1 | 7 ++++++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/powershell/internal/services/ad/protocol/Get-MtAdLdapProtectionCacheSuffix.ps1 b/powershell/internal/services/ad/protocol/Get-MtAdLdapProtectionCacheSuffix.ps1 index e959d13afc..09ec27d5d6 100644 --- a/powershell/internal/services/ad/protocol/Get-MtAdLdapProtectionCacheSuffix.ps1 +++ b/powershell/internal/services/ad/protocol/Get-MtAdLdapProtectionCacheSuffix.ps1 @@ -1,4 +1,4 @@ -function Get-MtAdLdapProtectionCacheSuffix { +function Get-MtAdLdapProtectionCacheSuffix { [CmdletBinding()] [OutputType([string])] param( diff --git a/powershell/internal/services/ad/protocol/Write-MtLdapProtectionWarning.ps1 b/powershell/internal/services/ad/protocol/Write-MtLdapProtectionWarning.ps1 index 106a0e5b9f..46a1ea2651 100644 --- a/powershell/internal/services/ad/protocol/Write-MtLdapProtectionWarning.ps1 +++ b/powershell/internal/services/ad/protocol/Write-MtLdapProtectionWarning.ps1 @@ -1,4 +1,4 @@ -function Write-MtLdapProtectionWarning { +function Write-MtLdapProtectionWarning { [CmdletBinding()] param( [Parameter(Mandatory)] diff --git a/powershell/public/connect/Connect-Maester.ps1 b/powershell/public/connect/Connect-Maester.ps1 index bceeb80d0a..db258ea5f5 100644 --- a/powershell/public/connect/Connect-Maester.ps1 +++ b/powershell/public/connect/Connect-Maester.ps1 @@ -51,7 +51,7 @@ Connect-Maester -Service ActiveDirectory -ActiveDirectoryServer 'dc01.contoso.com' -ActiveDirectoryCredential $credential -ActiveDirectoryAuthMode Basic -ActiveDirectoryTlsMode None -ActiveDirectoryForceUnencrypted -ActiveDirectoryAllowPlaintextPassword Explicitly permits a Basic/simple bind that transmits the username and password in plaintext to a server this connection does not authenticate. Queries and results are also plaintext and unsigned. Use only in an isolated test lab. An explicit server is required, but does not authenticate that server. Every reconnect sends the password again and repeats the warning. Do not use ForceUnsigned with Basic; simple binds have no SASL signing. - + .EXAMPLE Connect-Maester -Service Graph,AzureDevOps -AzureDevOpsOrganization 'contoso' diff --git a/powershell/tests/functions/ActiveDirectoryLdapProtection.Tests.ps1 b/powershell/tests/functions/ActiveDirectoryLdapProtection.Tests.ps1 index 9f394f0470..08ecdd53ea 100644 --- a/powershell/tests/functions/ActiveDirectoryLdapProtection.Tests.ps1 +++ b/powershell/tests/functions/ActiveDirectoryLdapProtection.Tests.ps1 @@ -1,4 +1,4 @@ -[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'Fake credentials for isolated unit tests.')] +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'Fake credentials for isolated unit tests.')] param() BeforeAll { diff --git a/powershell/tests/functions/ActiveDirectoryProtocol.Tests.ps1 b/powershell/tests/functions/ActiveDirectoryProtocol.Tests.ps1 index fa42b26a30..d230b58215 100644 --- a/powershell/tests/functions/ActiveDirectoryProtocol.Tests.ps1 +++ b/powershell/tests/functions/ActiveDirectoryProtocol.Tests.ps1 @@ -505,7 +505,12 @@ Describe 'Active Directory Protocol Contracts' -Skip:(-not $script:HasDirectoryS InModuleScope Maester -Parameters @{ Mode = $Mode; Attempts = $Attempts } { param($Mode, $Attempts) Mock New-MtLdapConnection { throw 'TLS failed' } - { Connect-MtAdTarget -ActiveDirectoryServer 'dc01.contoso.com' -TlsMode $Mode -ForceUnencrypted } | Should -Throw + $connectParameters = @{ + ActiveDirectoryServer = 'dc01.contoso.com' + TlsMode = $Mode + ForceUnencrypted = $true + } + { Connect-MtAdTarget @connectParameters } | Should -Throw Should -Invoke New-MtLdapConnection -Times $Attempts -Exactly Should -Invoke New-MtLdapConnection -Times 0 -Exactly -ParameterFilter { $Port -eq 389 -and -not $UseStartTls } Should -Invoke Write-Warning -Times 0 -Exactly