-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDump-GroupPolicies.ps1
309 lines (251 loc) · 10.8 KB
/
Dump-GroupPolicies.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
function Dump-GroupPolicies {
<#
.SYNOPSIS
Dump all the GPOs from Active Directory.
.DESCRIPTION
This cmdlet allows a normal user, without any special permissions, to
dump all the Group Policies from Active Directory.
.PARAMETER DomainFile
File containing the list of domains.
.PARAMETER ResultFile
File that will be written with the GPOs.
.PARAMETER QueryDates
Adds the created and changed dates to each user found.
.LINK
https://www.serializing.me/tags/active-directory/
.EXAMPLE
Dump-GroupPolicies -DomainFile .\Domains.xml -ResultFile .\GroupPolicies.xml
.NOTE
Function: Dump-GroupPolicies
Author: Duarte Silva (@serializingme)
License: GPLv3
Required Dependencies: None
Optional Dependencies: None
Version: 1.0.5
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $True)]
[String]$DomainFile,
[Parameter(Mandatory = $True)]
[String]$ResultFile,
[Parameter(Mandatory = $False)]
[Switch]$QueryDates
)
function Date-ToString {
param(
[DateTime]$Date,
[Bool]$InUTC = $False
)
[String]$Format = 'yyyy-MM-ddTHH:mm:ss.fffffffZ'
if ($InUTC) {
return $Date.ToString($Format)
}
else {
return $Date.ToUniversaltime().ToString($Format)
}
}
function Is-ValidProperty {
param(
[DirectoryServices.ResultPropertyValueCollection]$Property
)
return ($Property -ne $Null) -and ($Property.Count -eq 1) -and
(-not [String]::IsNullOrEmpty($Property.Item(0)))
}
function Process-Policy {
param(
[Xml.XmlWriter]$ResultFileWriter,
[Collections.Hashtable]$Policy
)
$ResultFileWriter.WriteStartElement('GroupPolicy')
$ResultFileWriter.WriteAttributeString('GUID', $Policy.GUID)
if ($Policy.Name -ne $Null) {
$ResultFileWriter.WriteAttributeString('Name', $Policy.Name)
}
if ($Policy.Path -ne $Null) {
$ResultFileWriter.WriteAttributeString('Path', $Policy.Path)
}
if ($Policy.Version -ne $Null) {
$ResultFileWriter.WriteAttributeString('Version', $Policy.Version)
}
if ($Policy.Created -ne $Null) {
$ResultFileWriter.WriteAttributeString('Created',
(Date-ToString -Date $Policy.Created -InUTC $True))
}
if ($Policy.Changed -ne $Null) {
$ResultFileWriter.WriteAttributeString('Changed',
(Date-ToString -Date $Policy.Changed -InUTC $True))
}
$ResultFileWriter.WriteEndElement()
}
function Process-Domain {
param(
[Xml.XmlWriter]$ResultFileWriter,
[String]$DomainName,
[String]$DomainDNS,
[Bool]$QueryDates
)
Write-Verbose ('Obtaining group policies for the {0} domain' -f $DomainName)
[DirectoryServices.DirectoryEntry]$DomainRoot = $Null
[DirectoryServices.DirectorySearcher]$PolicySearch = $Null
try {
[DirectoryServices.SortOption]$Sort = New-Object DirectoryServices.SortOption('name',
[DirectoryServices.SortDirection]::Ascending);
$DomainRoot = New-Object DirectoryServices.DirectoryEntry @(
'LDAP://{0}' -f $DomainName )
# Search for all group policies that have a name (usually a GUID).
$PolicySearch = New-Object DirectoryServices.DirectorySearcher @(
$DomainRoot, '(&(objectclass=grouppolicycontainer)(name=*))' )
$PolicySearch.PageSize = 500
$PolicySearch.Sort = $Sort
$PolicySearch.PropertiesToLoad.Add('displayname') | Out-Null
$PolicySearch.PropertiesToLoad.Add('name') | Out-Null
$PolicySearch.PropertiesToLoad.Add('gpcfilesyspath') | Out-Null
$PolicySearch.PropertiesToLoad.Add('versionnumber') | Out-Null
if ($QueryDates -eq $True) {
$PolicySearch.PropertiesToLoad.Add('whencreated') | Out-Null
$PolicySearch.PropertiesToLoad.Add('whenchanged') | Out-Null
}
[Collections.Hashtable]$Policy = @{
'Name' = $Null;
'GUID' = $Null;
'Path' = $Null;
'Version' = $Null;
'Created' = $Null;
'Changed' = $Null;
}
[Collections.ArrayList]$Policies = New-Object Collections.ArrayList
$PolicySearch.FindAll() | ForEach-Object {
$Policy.GUID = $_.Properties.name.Item(0).ToUpper()
if (Is-ValidProperty -Property $_.Properties.displayname) {
$Policy.Name = $_.Properties.displayname.Item(0).ToUpper()
}
if (Is-ValidProperty -Property $_.Properties.gpcfilesyspath) {
$Policy.Path = $_.Properties.gpcfilesyspath.Item(0)
}
if (Is-ValidProperty -Property $_.Properties.versionnumber) {
$Policy.Version = $_.Properties.versionnumber.Item(0)
}
if (Is-ValidProperty -Property $_.Properties.whencreated) {
$Policy.Created = $_.Properties.whencreated.Item(0)
}
if (Is-ValidProperty -Property $_.Properties.whenchanged) {
$Policy.Changed = $_.Properties.whenchanged.Item(0)
}
Process-Policy -ResultFileWriter $ResultFileWriter -Policy $Policy
# Add the processed policy GUID so that the current applied
# policies can be compared with the ones in the destination
# folder.
$Policies.Add($Policy.GUID) | Out-Null
# Make sure the hastable properties are null since it is being
# reused.
$Policy.Name = $Null
$Policy.GUID = $Null
$Policy.Path = $Null
$Policy.Version = $Null
$Policy.Created = $Null
$Policy.Changed = $Null
}
Write-Verbose ('Processed {0} policies for domain {1}' -f $Policies.Count,
$DomainName)
}
catch {
Write-Warning ('Failed to find group policies for {0} ({1})' -f $DomainName, $DomainDNS)
}
finally {
if ($PolicySearch -ne $Null) {
$PolicySearch.Dispose()
}
if ($DomainRoot -ne $Null) {
try {
$DomainRoot.Dispose()
}
catch {
Write-Warning 'Failed to dispose the domain root, probably because the server is not operational'
}
}
}
}
[IO.FileStream]$DomainFileStream = $Null
[Xml.XmlReader]$DomainFileReader = $Null
[IO.FileStream]$ResultFileStream = $Null
[Xml.XmlWriter]$ResultFileWriter = $Null
try {
[IO.FileInfo]$DomainFileInfo = New-Object IO.FileInfo @( $DomainFile )
if ($DomainFileInfo.Exists -eq $False) {
Write-Error 'The file to read the domains from does not exist'
}
[IO.FileInfo]$ResultFileInfo = New-Object IO.FileInfo @( $ResultFile )
if ($ResultFileInfo.Exists -eq $True) {
Write-Warning 'The file to save the scan results to exists and it will be overwritten'
}
# Instantiate the XML stream and reader.
$DomainFileStream = New-Object IO.FileStream @( $DomainFileInfo.FullName,
[IO.FileMode]::Open, [IO.FileAccess]::Read )
[Xml.XmlReaderSettings]$DomainXmlSettings = New-Object Xml.XmlReaderSettings
$DomainXmlSettings.IgnoreProcessingInstructions = $True
$DomainXmlSettings.IgnoreComments = $True
$DomainXmlSettings.ProhibitDtd = $True
$DomainFileReader = [Xml.XmlReader]::Create($DomainFileStream, $DomainXmlSettings)
# Instantiate the XML stream and writer.
$ResultFileStream = New-Object IO.FileStream @( $ResultFileInfo.FullName,
[IO.FileMode]::Create, [IO.FileAccess]::Write )
[Xml.XmlWriterSettings]$ResultXmlSettings = New-Object Xml.XmlWriterSettings
$ResultXmlSettings.Indent = $True
$ResultFileWriter = [Xml.XmlWriter]::Create($ResultFileStream, $ResultXmlSettings)
$ResultFileWriter.WriteStartElement('Domains')
$ResultFileWriter.WriteStartElement('Start')
$ResultFileWriter.WriteAttributeString('Time', (Date-ToString -Date (Get-Date)))
$ResultFileWriter.WriteEndElement()
[Collections.HashTable]$Domain = @{
'Name' = $Null;
'DNS' = $Null;
}
while ($DomainFileReader.Read()) {
if ($DomainFileReader.IsStartElement()) {
if (($DomainFileReader.Name -ne 'Domain') -and
($DomainFileReader.Name -ne 'Trusted')) {
continue
}
while ($DomainFileReader.MoveToNextAttribute()) {
if ($DomainFileReader.Name -eq 'Name') {
$Domain.Name = $DomainFileReader.Value
}
elseif ($DomainFileReader.Name -eq 'DNS') {
$Domain.DNS = $DomainFileReader.Value
}
}
$DomainFileReader.MoveToElement() | Out-Null
$ResultFileWriter.WriteStartElement('Domain')
$ResultFileWriter.WriteAttributeString('Name', $Domain.Name)
$ResultFileWriter.WriteAttributeString('DNS', $Domain.DNS)
Process-Domain -ResultFileWriter $ResultFileWriter -DomainName $Domain.Name `
-DomainDNS $Domain.DNS -QueryDates $QueryDates
$ResultFileWriter.WriteEndElement()
}
elseif ($DomainFileReader.Name -eq 'Domain') {
# Since the hashtable holding the domain information is being
# reused, make sure the entries are cleared for the next domain.
$Domain.Name = $Null
$Domain.DNS = $Null
}
}
$ResultFileWriter.WriteStartElement('End')
$ResultFileWriter.WriteAttributeString('Time', (Date-ToString -Date (Get-Date)))
$ResultFileWriter.WriteEndElement()
}
finally {
if ($ResultFileWriter -ne $Null) {
$ResultFileWriter.Close()
}
if ($ResultFileStream -ne $Null) {
$ResultFileStream.Close()
}
if ($DomainFileReader -ne $Null) {
$DomainFileReader.Close()
}
if ($DomainFileStream -ne $Null) {
$DomainFileStream.Close()
}
}
}