Repository navigation
Expand file tree
/
Copy pathGet-LdapCurrentUser.ps1
More file actions
164 lines (118 loc) · 4.48 KB
/
Copy pathGet-LdapCurrentUser.ps1
File metadata and controls
164 lines (118 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
function Get-LdapCurrentUser {
<#
.SYNOPSIS
Gets the current user who is authenticating to LDAP
Author: Lee Christensen (@tifkin_)
License: BSD 3-Clause
Required Dependencies: None
.DESCRIPTION
Gets the current user who is authenticating to LDAP. It does so by using the
LDAP_SERVER_WHO_AM_I_OID extended operation (MS-ADTS 3.1.1.3.4.2.4
LDAP_SERVER_WHO_AM_I_OID - https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/faf0b8c6-8c59-439f-ac62-dc4c078ed715).
.PARAMETER Server
LDAP server to connect to (likely a Domain Controller)
.PARAMETER AuthType
Protocol to use during authentication
.PARAMETER Certificate
Certificate (.pfx file) to use during authentication
.PARAMETER Certificate
Certificate file's password
.PARAMETER UseSSL
Whether to use SSL/TLS (LDAPS) when connecting
.PARAMETER Signing
Connect with Signing enabled
.PARAMETER Sealing
Connect with Sealing enabled
.PARAMETER VerifyServerCertificate
Verify that the server certificate is trusted
#>
[CmdletBinding(DefaultParameterSetName = 'Default')]
Param(
[Parameter(Mandatory=$false)]
[string]
$Server,
[Parameter(Mandatory=$false)]
[ValidateSet('Anonymous','Basic','Negotiate','Ntlm','Digest','Sicily','Dpa','Msn','External','Kerberos')]
$AuthType,
[Parameter(Mandatory=$false, ParameterSetName = 'CertAuth')]
[string]
$Certificate,
[Parameter(Mandatory=$false, ParameterSetName = 'CertAuth')]
[string]
$CertificatePassword,
[Parameter(Mandatory=$true, ParameterSetName = 'PasswordAuth')]
[ValidateNotNullOrEmpty()]
[string]
$Domain,
[Parameter(Mandatory=$true, ParameterSetName = 'PasswordAuth')]
[ValidateNotNullOrEmpty()]
[string]
$Username,
[Parameter(Mandatory=$true, ParameterSetName = 'PasswordAuth')]
[string]
$Password,
[Parameter(Mandatory=$false)]
[switch]
$UseSSL = $false,
[Parameter(Mandatory=$false)]
[switch]
$Signing = $false,
[Parameter(Mandatory=$false)]
[switch]
$Sealing = $false,
[Parameter(Mandatory=$false)]
[switch]
$VerifyServerCertificate
)
$c = $null
try {
$null = [System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.Protocols")
$null = [System.Reflection.Assembly]::LoadWithPartialName("System.Net")
$Ident = New-Object System.DirectoryServices.Protocols.LdapDirectoryIdentifier -ArgumentList @($Server)
$c = New-Object System.DirectoryServices.Protocols.LdapConnection $Ident
if($PSCmdlet.ParameterSetName -eq 'CertAuth') {
$Cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 @($Certificate, $CertificatePassword, 'Exportable')
$null = $c.ClientCertificates.Add($Cert)
} elseif($PSCmdlet.ParameterSetName -eq 'PasswordAuth') {
$cred = New-Object System.Net.NetworkCredential @($Username, $Password, $Domain)
$c.Credential = $cred
}
if($UseSSL) {
$c.SessionOptions.SecureSocketLayer = $true;
if(!$Server.EndsWith(":636")) {
Write-Warning "Using LDAPS, but the standard LDAPS port (636) was not specified in the -Server parameter"
}
}
if($Sealing) {
$c.SessionOptions.Sealing = $true
}
if($Signing) {
$c.SessionOptions.Signing = $true
}
if($AuthType) {
$c.AuthType = $AuthType
}
if(!$VerifyServerCertificate) {
$c.SessionOptions.VerifyServerCertificate = {
param($conn, [System.Security.Cryptography.X509Certificates.X509Certificate2]$cert)
Write-Verbose ($cert.ToString($true))
$true
}
}
# 1.3.6.1.4.1.4203.1.11.3 = OID for LDAP_SERVER_WHO_AM_I_OID (see MS-ADTS 3.1.1.3.4.2 LDAP Extended Operations)
$ExtRequest = New-Object System.DirectoryServices.Protocols.ExtendedRequest "1.3.6.1.4.1.4203.1.11.3"
$resp = $c.SendRequest($ExtRequest)
$str = [System.Text.Encoding]::ASCII.GetString($resp.ResponseValue)
if([string]::IsNullOrEmpty($str)) {
Write-Error "Authentication failed"
} else {
$str
}
} catch {
Write-Error $_
} finally {
if($c) {
$c.Dispose()
}
}
}