-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew-SecureFolder.ps1
376 lines (333 loc) · 20.7 KB
/
New-SecureFolder.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
function New-SecureFolder {
[cmdletbinding()]
Param(
[Parameter(Mandatory,ValueFromPipelineByPropertyName,ValueFromPipeline)]
[alias("FullName")]
[String[]]$Path,
[Parameter(ValueFromPipelineByPropertyName)]
[alias("BaseOU","OrganizationalUnit")]
[String]$OU = "OU=Shares,OU=Groups,DC=COMPANY,DC=com", # Modify this to the OU where you keep security groups
[Parameter(ValueFromPipelineByPropertyName)]
[alias("Manager")]
[String]$ManagedBy,
[Parameter(ValueFromPipelineByPropertyName)]
[alias("Domain")]
[String]$DomainName = $env:USERDOMAIN,
[Parameter(ValueFromPipelineByPropertyName)]
[String]$DescriptionPrefix = "\\server\Shares", # Modify this to your share base path
[Parameter(ValueFromPipelineByPropertyName)]
[String]$Notes,
[Parameter(HelpMessage="Do not apply ACL")]
[Switch]$NoAclAction,
[Parameter(HelpMessage="Do not try to create traversal ACL")]
[Switch]$NoTraversalACL,
[Parameter(HelpMessage="Verify and report, does not make any changes")]
[Switch]$VerifyOnly
)
BEGIN {
while (!($OUExists))
{
Write-Verbose "Testing OU $OU"
try {
Get-ADObject $OU -ErrorAction "SilentlyContinue" | Out-Null;
Write-Verbose "OU Exists. Continuing..."
$OUExists = $true
}
catch {
Write-Verbose "OU value is not an Active Directory object: $OU"
$OU = Read-Host "Please enter a valid OU DistinguishedName"
}
}
}
PROCESS {
foreach ($FullPath in $Path)
{
if ($FullPath.GetType().Name -eq "DirectoryInfo") { $FullPath = $FullPath.FullName }
$FullPath = $FullPath.TrimEnd("\")
if (!(Test-Path -Path $FullPath))
{
if ($VerifyOnly) { Write-Host "[-] The path does not exist: $FullPath" -ForegroundColor Red }
else {
try {
Write-Verbose "Path does not exist. Creating $FullPath"
New-Item -Path $FullPath -ItemType Directory
}
catch {
Write-Warning "Unable to create new folder."
break;
}
}
}
if ((Test-Path -Path $FullPath))
{
Write-Verbose "Path Exists ($FullPath). Continuing..."
$GroupNames = Get-SecureGroupNames -FullPath $FullPath
if ($ManagedBy)
{
while (!(Get-ADObject -filter {SamAccountName -eq $ManagedBy}))
{
Write-Verbose "ManagedBy value is not an Active Directory object: $ManagedBy"
$ManagedBy = Read-Host "Please enter a valid user or group name as the folder manager"
}
}
else { $ManagedBy = $GroupNames.Manager }
# Manager group
$GrpMgr = $null
try {
# Manager group
try {
$GrpMgr = Get-Adgroup $GroupNames.Manager -Properties Description,ManagedBy
Write-Verbose "AD Group exists ($($GroupNames.Manager)). Continuing..."
if ($VerifyOnly) { Write-Host "[+] Manager AD Group Exists: $($GroupNames.Manager)" -ForegroundColor Green }
else {
if ($null -eq $GrpMgr.Description) {
Write-Verbose "AD Group ($($GroupNames.Manager)) Description is empty. Applying..."
Set-ADGroup -Identity $GroupNames.Manager -Description "Manager of $($GroupNames.Manager))"
}
if ($null -eq $Grp.ManagedBy) {
Write-Verbose "AD Group ($($GroupNames.Manager)) manager is empty. Applying..."
Set-ADGroup -Identity $GroupNames.Manager -ManagedBy $ManagedBy
}
}
}
catch {
Write-Verbose "Creating group: $($GroupNames.Manager) managed by $ManagedBy"
if ($VerifyOnly) { Write-Host "[-] Manager AD Group does NOT exist: $($GroupNames.Manager)" -ForegroundColor Red }
else {
New-ADGroup -Path $OU -Name $GroupNames.Manager -Description "Manager of $($GroupNames.ReadWrite)" -GroupScope DomainLocal -GroupCategory Security
Set-ADGroup -Identity $GroupNames.Manager -ManagedBy $ManagedBy
if ($Notes) { Set-ADGroup -Identity $GroupNames.Manager -Replace @{info="$Notes"} }
}
} # End GrpMGR block
# Read/Write group
$GrpRW = $null
try {
$GrpRW = Get-ADGroup $GroupNames.ReadWrite -Properties Description,ManagedBy,Members
Write-Verbose "AD Group exists ($($GrpRW.SamAccountName)). Continuing..."
if ($VerifyOnly) { Write-Host "[+] Read/Write AD Group exists: $($GrpRW.SamAccountName)" -ForegroundColor Green }
else {
if ($null -eq $GrpRW.Description) {
Write-Verbose "AD Group ($($GrpRW.SamAccountName)) Description is empty. Applying..."
Set-ADGroup -Identity $GroupNames.ReadWrite -Description $GroupNames.Description
}
if ($null -eq $GrpRW.ManagedBy) {
Write-Verbose "AD Group ($($GrpRW.SamAccountName)) manager is empty. Applying..."
Set-ADGroup -Identity $GroupNames.ReadWrite -ManagedBy $ManagedBy
}
}
}
catch
{
Write-Verbose "Read/Write AD Group does NOT exist ($($GroupNames.ReadWrite)). Creating..."
if ($VerifyOnly) { Write-Host "[-] Read/Write AD Group does NOT exist: $($GroupNames.ReadWrite)" -ForegroundColor Red }
else {
Write-Verbose "Creating group: $($GroupNames.ReadWrite) managed by $ManagedBy"
try {
New-ADGroup -Path $OU -Name $GroupNames.ReadWrite -Description $GroupNames.Description -ManagedBy $ManagedBy -GroupScope DomainLocal -GroupCategory Security
if ($Notes) { Set-ADGroup -Identity $$GroupNames.ReadWrite -Replace @{info="$Notes"} }
$GrpRW = Get-ADGroup $GroupNames.ReadWrite -Properties Description,ManagedBy,Members
}
catch {
Write-Error "Failed to create AD Group: $($GroupNames.ReadWrite) $($Error[0])"
}
}
} # End GrpRW block
# Read Only group
$GrpRO = $null
try {
$GrpRO = Get-Adgroup $GroupNames.ReadOnly -Properties Description,ManagedBy,Members
Write-Verbose "AD Group exists ($($GrpRO.SamAccountName)). Continuing..."
if ($VerifyOnly) { Write-Host "[+] Read-Only AD Group exists: $($GroupNames.ReadOnly)" -ForegroundColor Green}
else {
if ($null -eq $GrpRO.Description) {
Write-Verbose "AD Group ($($GroupNames.ReadOnly)) Description is empty. Applying..."
Set-ADGroup -Identity $GroupNames.ReadOnly -Description $GroupNames.Description
}
if ($null -eq $GrpRO.ManagedBy) {
Write-Verbose "AD Group ($($GroupNames.ReadOnly)) manager is empty. Applying..."
Set-ADGroup -Identity $GroupNames.ReadOnly -ManagedBy $ManagedBy
}
}
}
catch
{
Write-Verbose "Read-Only AD Group does NOT exist ($($GroupNames.ReadOnly)). Creating..."
if ($VerifyOnly) { Write-Host "[-] Read-Only AD Group does NOT exist: $($GroupNames.ReadOnly)" -ForegroundColor Red }
else {
Write-Verbose "Creating group: $($GroupNames.ReadOnly) managed by $ManagedBy"
try {
New-ADGroup -Path $OU -Name $GroupNames.ReadOnly -Description $GroupNames.Description -ManagedBy $ManagedBy -GroupScope DomainLocal -GroupCategory Security
if ($Notes) { Set-ADGroup -Identity $GroupNames.ReadOnly -Replace @{info="$Notes"} }
$GrpRO = Get-Adgroup $GroupNames.ReadOnly -Properties Description,ManagedBy,Members
}
catch {
Write-Error "Failed to create AD Group: $($GroupNames.ReadOnly) $($Error[0])"
}
}
} # End GrpRO block
# Traversal Group
if (!($GroupNames.isRootFolder))
{
try {
$GrpTrv = Get-ADGroup $GroupNames.Traversal -Properties Description,ManagedBy,Members
Write-Verbose "Traversal AD Group exists ($($GrpTrv.SamAccountName)). Continuing..."
if ($VerifyOnly) { Write-Host "[+] Traversal AD Group exists: $($GrpTrv.SamAccountName)" -ForegroundColor Green }
else {
if (($null -eq $GrpTrv.Description) -or ($GrpTrv.Description -notcontains $GroupNames.ParentPath)) {
Write-Verbose "AD Group ($($GroupNames.Traversal)) Description is empty or inaccurate. Applying..."
Set-ADGroup -Identity $GroupNames.Traversal -Description "Traversal of $($GroupNames.ParentPath)"
}
}
}
catch
{
if ($VerifyOnly) { Write-Host "[-] Traversal AD Group does NOT exist: $($GroupNames.Traversal)" -ForegroundColor Red }
else {
Write-Verbose "Traversal group does not exist. Creating $($GroupNames.Traversal)"
try {
New-ADGroup -Path $OU -name $GroupNames.Traversal -Description "Traversal of $($GroupNames.ParentPath)" -GroupScope DomainLocal -GroupCategory Security
if ($Notes) { Set-ADGroup -Identity $GroupNames.Traversal -Replace @{info="$Notes"} }
$GrpTrv = Get-ADGroup $GroupNames.Traversal -Properties Description,ManagedBy,Members
}
catch {
Write-Error "Failed to create Traversal group: $($GroupNames.Traversal) $($Error[0])"
}
}
}
# Nest traversal group
try {
$GroupTRVMembers = $GrpTrv.Members
if (!($GroupTRVMembers.contains($GrpRW.DistinguishedName)))
{
Write-Verbose "$($GroupNames.Traversal) does not contain $($GrpRW.DistinguishedName)"
if ($VerifyOnly) { Write-Host "[-] Read/Write AD Group is NOT a member of the Traversal group: $($GroupNames.ReadWrite) -> $($GroupNames.Traversal)" -ForegroundColor Red }
else {
Write-Verbose "Adding group membership: $($GroupNames.ReadWrite) to $($GroupNames.Traversal)"
Add-ADGroupMember -identity $GroupNames.Traversal -members $GroupNames.ReadWrite
}
}
else {
if ($VerifyOnly) { Write-Host "[+] Read/Write AD Group is a member of the Traversal group: $($GroupNames.ReadWrite) -> $($GroupNames.Traversal)" -ForegroundColor Green }
}
if (!($GroupTRVMembers.contains($GrpRO.DistinguishedName)))
{
if ($VerifyOnly) { Write-Host "[-] Read-Only AD Group is NOT a member of the Traversal group: $($GroupNames.ReadOnly) -> $($GroupNames.Traversal)" -ForegroundColor Red }
else {
Write-Verbose "Adding group membership: $($GroupNames.ReadOnly) to $($GroupNames.Traversal)"
Add-ADGroupMember -identity $GroupNames.Traversal -members $GroupNames.ReadOnly
}
}
else {
if ($VerifyOnly) { Write-Host "[+] Read-Only AD Group is a member of the Traversal group: $($GroupNames.ReadOnly) -> $($GroupNames.Traversal)" -ForegroundColor Green }
}
}
catch {
if ($VerifyOnly) { Write-Host "[-] Could not retrive members of Traversal group." -ForegroundColor Red }
else {
Write-Error "Failed to retrieve members of Traversal group: $Error[0]"
}
}
} # End GrpTRV block
# Nest manager group
try { $GroupRWMembers = $GrpRW.Members }
catch { Write-Error "Could not collect $($GroupNames.ReadWrite) members."}
if (!($GroupRWMembers.contains($GrpMgr.DistinguishedName)))
{
if ($VerifyOnly) { Write-Host "[-] Manager AD Group is NOT a member of the Read/Write group: $($GroupNames.Manager) -> $($GroupNames.ReadWrite)" -ForegroundColor Red }
else {
Write-Verbose "Adding manager group membership: $($GroupNames.Manager) to $($GroupNames.ReadWrite)"
Add-ADGroupMember -identity $GroupNames.ReadWrite -members $GroupNames.Manager
}
}
else {
if ($VerifyOnly) { Write-Host "[+] Manager AD Group is a member of the Read/Write group: $($GroupNames.Manager) -> $($GroupNames.ReadWrite)" -ForegroundColor Green }
}
}
catch {
if ($VerifyOnly) { $false }
else {
Write-Error "Failed to create required AD Group(s). $($Error[0])"
break;
}
} # End AD Groups block
if (!($NoACLAction)) {
Write-Verbose "Gathering existing directory ACLs..."
$ParentPathACL = Get-ACL -Path $GroupNames.ParentPath
$FullPathACL = Get-ACL -Path $FullPath
if (!($GroupNames.isRootFolder)) {
$AccessRuleTrv = New-Object System.Security.AccessControl.FileSystemAccessRule("$DomainName\$($GroupNames.Traversal)","ReadAndExecute","None","None","Allow") # This folder only
}
$AccessRuleRW = New-Object System.Security.AccessControl.FileSystemAccessRule("$DomainName\$($GroupNames.ReadWrite)","Modify","ContainerInherit, ObjectInherit","None","Allow") # Subfolders and files
$AccessRuleRO = New-Object System.Security.AccessControl.FileSystemAccessRule("$DomainName\$($GroupNames.ReadOnly)","ReadAndExecute","ContainerInherit, ObjectInherit","None","Allow") # Subfolders and files
$IDReferences = @()
foreach ($Rule in $ParentPathACL.Access) { $IDReferences += $Rule.IdentityReference.value }
if (!($GroupNames.isRootFolder)) {
if (!($IDReferences.contains("$DomainName\$($GrpTrv.SamAccountName)")))
{
if ($VerifyOnly) { Write-Host "[-] Traversal AD Group is NOT applied to the parent path: $($GrpTrv.SamAccountName) -> $($GroupNames.ParentPath)" -ForegroundColor Red }
$attempts = 1; $ACLSuccess = $false
while (!($ACLSuccess) -and ($attempts -le 20)) {
try {
$ParentPathACL.AddAccessRule($AccessRuleTrv)
$ACLSuccess = $true
}
catch {
Write-Verbose "Failed to build traversal ACL ($attempts). Retrying..."
$attempts++
$ACLSuccess = $false
Start-Sleep -Seconds 2
}
}
Write-Verbose "Build ACL Success: $ACLSuccess - Attempts: $attempts"
if ($ACLSuccess) {
if ($VerifyOnly) { Write-Host "[+] The Traversal ACL was successfully built for $FullPath" -ForegroundColor Green }
else {
try {
Write-Verbose "Applying Traversal ACL to $($GroupNames.ParentPath)"
(Get-Item $GroupNames.ParentPath).SetAccessControl($ParentPathACL)
}
catch { Write-Error "Something went wrong while applying the Traversal ACL to $($GroupNames.ParentPath)" }
}
}
else {
if ($VerifyOnly) { Write-Host "[-] The Traversal ACL was NOT successfully built for $($GroupNames.ParentPath)" -ForegroundColor Red }
}
}
else {
if ($VerifyOnly) { Write-Host "[+] Traversal AD Group is applied to the parent path: $($GroupNames.Traversal) -> $($GroupNames.ParentPath)" -ForegroundColor Green }
Write-Verbose "Parent Path already contains traversal ACL. Continuing..."
}
}
Write-Verbose "Building ACL for $FullPath"
$attempts = 1; $ACLSuccess = $false
if ($VerifyOnly) { $attempts = 20 <# try once #> }
while (!($ACLSuccess) -and ($attempts -le 20)) {
try {
$FullPathACL.AddAccessRule($AccessRuleRW)
$FullPathACL.AddAccessRule($AccessRuleRO)
$ACLSuccess = $true
}
catch {
Write-Verbose "Failed to build ACL ($attempts). Retrying..."
$attempts++
$ACLSuccess = $false
Start-Sleep -Seconds 2
}
}
Write-Verbose "Build ACL Success: $($ACLSuccess) - Attempts: $attempts"
if ($ACLSuccess) {
if ($VerifyOnly) { Write-Host "[+] The ACL was successfully built for $FullPath" -ForegroundColor Green }
else {
Write-Verbose "Applying RW/RO ACL to $FullPath"
try { (Get-Item $FullPath).SetAccessControl($FullPathACL) }
catch { Write-Error "Something went wrong while applying the ACL to $FullPath" }
}
}
else {
if ($VerifyOnly) { Write-Host "[-] The ACL was NOT successfully built for $FullPath" -ForegroundColor Red }
}
} # End ACL Actions
}
} # END Foreach Path
} # END PROCESS
}