-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldapDumpPwsh.ps1
189 lines (149 loc) · 5.48 KB
/
ldapDumpPwsh.ps1
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<#
.SYNOPSIS
LDAP Enumeration Script using paged (chunked) queries to avoid OOM.
.DESCRIPTION
This script connects to a specified LDAP path and enumerates objects
(users, groups, computers, etc.) with “chunks” to avoid overload of mem.
.PARAMETER LDAPPath
LDAP path, include DN. Ex:
LDAP://<ip>/DC=example,DC=com
.PARAMETER Username
Username "DOMAIN\foo" ou "foo@domain".
.PARAMETER Password
Password.
.PARAMETER AuthType
Type of auth .NET, Ex:
[System.DirectoryServices.AuthenticationTypes]::Secure
[System.DirectoryServices.AuthenticationTypes]::SecureSocketsLayer
.NOTES
Requirements.. Windows PowerShell and .NET Framework with System.DirectoryServices
#>
Param(
[string]$LDAPPath = "LDAP://<ip>/DC=<example>,DC=<com>",
[string]$Username = "", # DOMAIN\JOHNDOE
[string]$Password = "",
[System.DirectoryServices.AuthenticationTypes]$AuthType = [System.DirectoryServices.AuthenticationTypes]::Secure,
[string]$OutputPath = ".\"
)
Write-Host "[*] Connecting to: $LDAPPath"
Write-Host "[*] Using Credentials: $Username"
Write-Host "[*] Authentication Type: $AuthType"
try {
$DirectoryEntry = New-Object System.DirectoryServices.DirectoryEntry($LDAPPath, $Username, $Password, $AuthType)
$DirectoryEntry.RefreshCache()
Write-Host "[+] Successfully connected to LDAP!"
} catch {
Write-Host "[-] Failed to connect to LDAP server: $($_.Exception.Message)" -ForegroundColor Red
return
}
function Export-LDAPSearchChunked {
param (
[string]$Filter,
[string[]]$Properties,
[scriptblock]$Transform,
[string]$CsvFile,
[int]$PageSize = 500
)
$FullPath = Join-Path $OutputPath $CsvFile
if (Test-Path $FullPath) {
Remove-Item $FullPath -Force
}
$Searcher = New-Object System.DirectoryServices.DirectorySearcher($DirectoryEntry)
$Searcher.Filter = $Filter
$Searcher.PageSize = $PageSize
$Searcher.PropertiesToLoad.AddRange($Properties)
Write-Host "[*] Querying LDAP with filter: $Filter - PageSize=$PageSize"
$Results = $null
try {
$Results = $Searcher.FindAll()
} catch {
Write-Host "[-] Error in LDAP query: $($_.Exception.Message)" -ForegroundColor Red
return
}
if (-not $Results) {
Write-Host "[-] No results found for '$Filter'."
return
}
$ChunkSize = 500
$ChunkArray = @()
$Count = 0
foreach ($Result in $Results) {
$Count++
$Entry = $Result.GetDirectoryEntry()
$Obj = & $Transform $Entry
if ($Obj -ne $null) {
$ChunkArray += $Obj
}
if ($ChunkArray.Count -ge $ChunkSize) {
if (Test-Path $FullPath) {
$ChunkArray | Export-Csv -Path $FullPath -NoTypeInformation -Append -Encoding UTF8
} else {
$ChunkArray | Export-Csv -Path $FullPath -NoTypeInformation -Encoding UTF8
}
$ChunkArray = @()
}
}
if ($ChunkArray.Count -gt 0) {
if (Test-Path $FullPath) {
$ChunkArray | Export-Csv -Path $FullPath -NoTypeInformation -Append -Encoding UTF8
} else {
$ChunkArray | Export-Csv -Path $FullPath -NoTypeInformation -Encoding UTF8
}
$ChunkArray = @()
}
Write-Host "[+] Total of $Count results exported to $FullPath"
# Free MEM :P
$Results.Dispose()
}
Export-LDAPSearchChunked `
-Filter "(objectClass=group)" `
-Properties @("cn","description","member") `
-CsvFile "domain_groups.csv" `
-Transform {
param($Entry)
[PSCustomObject]@{
"GroupName" = $Entry.Properties["cn"][0]
"Description" = $Entry.Properties["description"][0]
"Members" = ($Entry.Properties["member"] -join "; ")
}
}
Export-LDAPSearchChunked `
-Filter "(&(objectClass=user)(!(objectClass=computer)))" `
-Properties @("cn","mail","lastLogonTimestamp","userAccountControl") `
-CsvFile "domain_users.csv" `
-Transform {
param($Entry)
[PSCustomObject]@{
"UserName" = $Entry.Properties["cn"][0]
"Email" = $Entry.Properties["mail"][0]
"LastLogon" = $Entry.Properties["lastLogonTimestamp"][0]
"AccountDisabled" = ($Entry.Properties["userAccountControl"][0] -band 2 -ne 0)
"PasswordNeverExpires" = ($Entry.Properties["userAccountControl"][0] -band 65536 -ne 0)
}
}
Export-LDAPSearchChunked `
-Filter "(objectClass=computer)" `
-Properties @("cn","operatingSystem","lastLogonTimestamp") `
-CsvFile "domain_computers.csv" `
-Transform {
param($Entry)
[PSCustomObject]@{
"ComputerName" = $Entry.Properties["cn"][0]
"OperatingSystem"= $Entry.Properties["operatingSystem"][0]
"LastLogon" = $Entry.Properties["lastLogonTimestamp"][0]
}
}
Export-LDAPSearchChunked `
-Filter "(objectClass=domainDNS)" `
-Properties @("minPwdLength","maxPwdAge","lockoutThreshold") `
-CsvFile "domain_policy.csv" `
-Transform {
param($Entry)
[PSCustomObject]@{
"MinPasswordLength" = $Entry.Properties["minPwdLength"][0]
"MaxPasswordAge" = $Entry.Properties["maxPwdAge"][0]
"LockoutThreshold" = $Entry.Properties["lockoutThreshold"][0]
}
}
Write-Host ""
Write-Host "[+] LDAP Enumeration Completed with paged queries!" -ForegroundColor Green