Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 66 additions & 21 deletions powershell/internal/services/ad/Connect-MtAdTarget.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@
[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
)

Expand Down Expand Up @@ -144,6 +150,9 @@
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
Expand Down Expand Up @@ -300,7 +309,9 @@
[Parameter(Mandatory)]
[string]$RequestedTlsMode,

[switch]$SkipCertificateCheck
[switch]$SkipCertificateCheck,

[switch]$ForceUnencrypted
)

$lastException = $null
Expand All @@ -309,25 +320,33 @@

$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'"
Expand All @@ -338,6 +357,11 @@
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
Expand Down Expand Up @@ -479,6 +503,27 @@
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.'
}
Expand All @@ -498,7 +543,7 @@
}

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 `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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')
Expand All @@ -23,7 +23,7 @@
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')
Expand All @@ -37,7 +37,7 @@
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')
Expand All @@ -53,7 +53,7 @@
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')
Expand Down
Original file line number Diff line number Diff line change
@@ -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)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,27 @@
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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand All @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<redacted>@'
$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
}
Loading
Loading