-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindWriteAccess.ps1
More file actions
452 lines (362 loc) · 15.6 KB
/
findWriteAccess.ps1
File metadata and controls
452 lines (362 loc) · 15.6 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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<#
.SYNOPSIS
PSAccessFinder - find folders where you can write into
.DESCRIPTION
This Powershell script searches recursive for folders where the current user has Write, Modify or FullControl permissions. Its meant to find insufficient permissions that you can use for DLL Hijacking
.PARAMETER startfolder
The path to the start the permission check
.PARAMETER inputCSV
The path to the csv file, this file should contain a column with the header "Path" and "Process Name", like the procmon export produce it.
.PARAMETER envCheck
enumerate all machine environment variables and check write access to the executable pathes
.PARAMETER services
enumerate all services and check write access to the executable pathes
.PARAMETER serviceFilter
0 = no services are filtered (default)
1 = windows services get filterd
2 = 1 + quoted pathes in servics get filterd
.PARAMETER noRecurse
Only relevant for searches from current or start folder, if set, searching in subfolders will be skipped
.PARAMETER noSkip
Doesn´t skip time consuming folders (defined in $global:skippFolders) AND doesn´t break if permissions are found (it keeps searching in subfolders), use carefuly because this can take ages!
A good approach why you want to set this, is when you want to search for writeable subfolders of a specific application location, defined with -starfolder
.PARAMETER checkParents
Check also the parents folders, make sense when a startfolder which is not on root level is defined or in combination with the service parameter
.PARAMETER verbose
0 = print a table of found pathes after finishing (default)
1 = 0 + print instantly: found folders, if inputCSV is set, the cleaned csv table, if service is set, the services
2 = 0 + 1 + folders without permissions
.PARAMETER formatList
If set, the output will be printed as list instead of a table
.EXAMPLE
C:\PS>.\findWriteAccess.ps1
check permissions in sub directories, starting at the current directory
only output pahtes with permission (no verbose)
.EXAMPLE
C:\PS>.\findWriteAccess.ps1 -startfolder "C:\Users\Admin" -verbose 1
check permissions in sub directories, starting at the defined start folder
verbose 1 will instantly print matching folders
.EXAMPLE
C:\PS>.\findWriteAccess.ps1 -inputCSV .\Logfile.CSV -verbose 2
use a csv containing a coloumn "Path" and "Process Name" to check, usually you will take a export from procmon.exe
verbose 2 will instantly print matching and also folders with no permissions
.EXAMPLE
C:\PS>.\findWriteAccess.ps1 -services -serviceFilter 2 -verbose 1 -checkParents
show all unqoted non windows services and check permissons also of the parent folders
.LINK
https://github.com/secure-77/PSAccessFinder
#>
# inputs
param (
[String]$startfolder = "",
[String]$inputCSV,
[switch]$services,
[switch]$envCheck,
[int]$serviceFilter = 0,
[switch]$noRecurse,
[switch]$noSkip,
[switch]$checkParents,
[int]$verbose = 0,
[switch]$formatList
)
if ($startfolder -eq "" -AND $inputCSV -eq "" -AND !$services -AND !$envCheck) {
Write-Output "no start folder and no csv defined, using current folder`n"
$startfolder = Get-Location
}
$global:skippFolders = @("C:\Windows\servicing\LCU", "C:\Windows\WinSxS")
$language = GET-WinSystemLocale | Select-Object Name
$global:username = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$global:userShema = "BUILTIN\Users"
$global:domainUserShema = $env:USERDomain + "\Domain Users"
$global:authShema = "NT AUTHORITY\authenticated users"
$global:everyone = "everyone"
$global:verboseLevel = $verbose
# always disalbe recurse if parents check is one
if ($checkParents) {
$global:noRecurseOn = $true
}
else {
$global:noRecurseOn = $noRecurse
}
$folders = @()
$global:Output = @()
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
# Language
if ($language -match "de-DE") {
$global:userShema = "VORDEFINIERT\Benutzer"
$global:authShema = "NT-AUTORITÄT\Authentifizierte Benutzer"
$global:domainUserShema = $env:USERDomain + "\Domänen-Benutzer"
$global:everyone = "Jeder"
}
# (New-Object System.DirectoryServices.DirectorySearcher("(&(objectCategory=User)(samAccountName=$($env:username)))")).FindOne().GetDirectoryEntry().memberOf
## add local groups of current user
$global:checkGroups = @()
$global:allGroups = @(Get-LocalGroup).SID
foreach ( $groupSID in $allGroups )
{
$isMember = Get-LocalGroupMember -SID $groupSID | Where-Object { $_.Name -eq $global:username }
if ($isMember) {
$groupName = $groupSID.Translate([System.Security.Principal.NTAccount]).Value
$global:checkGroups += @($groupName)
}
}
$global:checkGroups += @($global:everyone,$global:authShema,$global:domainUserShema,$global:userShema,$global:username)
$global:checkGroups += @([Security.Principal.WindowsIdentity]::GetCurrent().Groups | foreach-object {$_.Translate([Security.Principal.NTAccount]).value})
$global:checkGroups = $global:checkGroups | Sort-Object -Unique
if ($verboseLevel -gt 0) {
Write-Output "Checking the following Security Profiles `n"
$global:checkGroups
}
# Get subfolders of directory
function Get-SubFolders {
param (
$startfolder
)
Write-Output "check folder: $startfolder"
Return Get-ChildItem $startfolder -ErrorAction SilentlyContinue | where-object { $_.PSIsContainer -eq "TRUE" }
}
function Get-Folder {
param (
$startfolder
)
Return Get-Item $startfolder -ErrorAction SilentlyContinue | where-object { $_.PSIsContainer -eq "TRUE" }
}
# Check the ACLs for all subfolders
function Invoke-CheckACLs {
param (
$targetfolder,
$recursive = $true
)
foreach ($folder in $targetfolder) {
$FullPath = $folder.FullName
try {
$acl = get-acl $FullPath -ErrorAction SilentlyContinue
}
Catch {
continue;
}
$Access = $acl.Access
$check = $false
foreach ($AccessObject in $Access) {
$User = $AccessObject.IdentityReference.value
$Rights = $AccessObject.FileSystemRights
$Control = $AccessObject.AccessControlType
if ($Control -eq "Allow") {
foreach ( $group in $global:checkGroups )
{
if ($User -eq $group ) {
if ($Rights -match "FullControl" -or $Rights -match "Write" -or $Rights -match "Modify" -or $Rights -match "CreateFiles") {
# Found access
if ($verboseLevel -gt 0) {
Write-Output "Path found: $FullPath"
}
# only skip subfolders if switch is true
if (!$noSkip) {
$check = $true
}
$Line = New-Object PSObject
if (!$folder.ProcessName -eq "") {
$Line | Add-Member -membertype NoteProperty -name "Process" -Value $folder.ProcessName
}
# check if origPath is set
if ($folder.OrigPath) {
$dllFullPath = $folder.OrigPath + "\" + $folder.dllName
# check if origPath is different to the acl one
if ($folder.OrigPath -ne $FullPath) {
if ($verboseLevel -gt 0) {
Write-Output "Orig Path: $FullPath"
}
$Line | Add-Member -membertype NoteProperty -name "AcessTo" -Value $FullPath
}
else {
$Line | Add-Member -membertype NoteProperty -name "AcessTo" -Value "FullPath"
}
}
else {
$dllFullPath = $FullPath + "\" + $folder.dllName
}
$Line | Add-Member -membertype NoteProperty -name "FullPath" -Value $dllFullPath
$Line | Add-Member -membertype NoteProperty -name "Acess" -Value $User
$Line | Add-Member -membertype NoteProperty -name "Rights" -Value $Rights
$global:Output += $Line
}
}
}
}
}
if ($check -eq $false) {
if ($verboseLevel -gt 1) {
Write-Output "no permissions in $FullPath"
}
# no access, check subfolders
if ( $recursive -eq $true) {
# only process non time consuming folders
if (!$global:skippFolders.contains($FullPath) -OR $noSkip) {
if ($verboseLevel -gt 1) {
Write-Output "recursive on, checking subfolders"
}
$subfolders = Get-SubFolders -startfolder $FullPath
Invoke-CheckACLs -targetfolder $subfolders
}
# skip these and print info
else {
if ($verboseLevel -gt 0) {
Write-Output "skipping subfolders check for $FullPath"
}
}
}
# check parent folders
elseif ($checkParents) {
$parent = Split-Path $FullPath -Parent
if ($parent) {
$parentFolder = Get-Folder($parent)
if ($verboseLevel -gt 1) {
Write-Output "check parent $parentFolder"
}
Invoke-CheckACLs -targetfolder $parentFolder -recursive $false
}
}
}
}
}
# Entry Point after start (check for service)
if ($services -or $showNonWServices) {
$Win32_Service = Get-CimInstance Win32_Service -Property Name, DisplayName, PathName, StartName | Select-Object Name, DisplayName, PathName, StartName
if (!$Win32_Service) {
"No CIM Access to Services, check manuel via registry!"
}
$serviceList = New-Object System.Collections.ArrayList
foreach ($service in $Win32_Service) {
Try {
# filter windows services
if ($serviceFilter -gt 0) {
$path = $service.Pathname.tostring().replace('"','')
$cri = ([System.Diagnostics.FileVersionInfo]::GetVersionInfo($path)).legalcopyright
if ($cri -like "*Microsoft*") {
continue
}
}
# filter quoted pathes
if ($serviceFilter -gt 1) {
if ($service.PathName -like '"*') {
continue
}
}
$cleanPath = $service.PathName -replace '"', ""
$cleanPath = $cleanPath -replace '.exe.*', ".exe"
if ($cleanPath -ne "") {
$Line = New-Object PSObject
$Line | Add-Member -membertype NoteProperty -name "Process Name" -Value $service.Name
$Line | Add-Member -membertype NoteProperty -name "User" -Value $service.StartName
$Line | Add-Member -membertype NoteProperty -name "Orig Path" -Value $service.PathName
$Line | Add-Member -membertype NoteProperty -name "Path" -Value $cleanPath
$serviceList += $Line
}
}
catch {}
}
if ($verbose -gt 0) {
if ($formatList) {
Write-Output $serviceList | Format-List
} else {
Write-Output $serviceList
}
}
}
# Followup Point after start (check for service or csv)
if (!$inputCSV -eq "" -or $services) {
if ($services) {
Write-Output "using service list...`n"
$csvFolders = $serviceList
}
else {
Write-Output "try to parse csv and remove duplicates...`n"
$csvFolders = Import-Csv $inputCSV | Sort-Object Path -Unique
}
if ($verboseLevel -gt 0 -and !$service) {
Write-Output "finished, using the following list:"
Write-Output $csvFolders | Select-Object "Process Name", "Path" | Format-Table -AutoSize
}
$folderCount = $csvFolders.Count
Write-Output "`nstarting search for write access in $folderCount locations... `n"
foreach ($dll in $csvFolders) {
#extract path of file and add FullName Member
$dllPath = Split-Path -Path $dll.Path
$dllOrigPath = $dllPath
# check if path does exist, if not step up
$pathExist = $false
DO {
if ($dllPath -ne "") {
if (Get-Item $dllPath -ErrorAction SilentlyContinue ) {
$pathExist = $true
}
else {
if ($verboseLevel -gt 1) {
Write-Output "Path doesn't exist, checking parent: $dllPath"
}
$dllPath = Split-Path $dllPath -Parent
}
}
else {
$pathExist = $true
}
} While ($pathExist -eq $false)
$dllName = Split-Path -Leaf $dll.Path
$dllPath | Add-Member -NotePropertyName origPath -NotePropertyValue $dllOrigPath
$dllPath | Add-Member -NotePropertyName FullName -NotePropertyValue $dllPath
$dllPath | Add-Member -NotePropertyName ProcessName -NotePropertyValue $dll."Process Name"
$dllPath | Add-Member -NotePropertyName dllName -NotePropertyValue $dllName
Invoke-CheckACLs -targetfolder $dllPath -recursive $false
}
}
# Entry Point after start (check for system env pathes)
elseif($envCheck) {
$pathCollection = [Environment]::GetEnvironmentVariables("Machine").Path.Split(';')
Write-Output "`nstarting search for write access in:`n $pathCollection`n"
foreach ($folder in $pathCollection) {
$folders = Get-SubFolders -startfolder $folder
if ($checkParents) {
Write-Output "parents search is on`n"
$folders = Get-Folder($startfolder)
}
if ($verboseLevel -gt 0) {
Write-Output "recursive search is off"
}
Invoke-CheckACLs -targetfolder $folders -recursive $false
}
}
# Entry Point after start (check startfolder)
else {
Write-Output "`nstarting search for write access in $startfolder`n"
$folders = Get-SubFolders -startfolder $startfolder
if ($noRecurseOn) {
if ($verboseLevel -gt 0) {
Write-Output "recursive search is off"
}
if ($checkParents) {
Write-Output "parents search is on`n"
$folders = Get-Folder($startfolder)
}
Invoke-CheckACLs -targetfolder $folders -recursive $false
}
else {
Invoke-CheckACLs -targetfolder $folders
}
}
# Output
if ($Output.Count -gt 0) {
Write-Output "`nfound some folders, happy hunting :)"
if ($formatList) {
Write-Output $Output | Format-List
}
else {
Write-Output $Output | Format-Table -AutoSize
}
}
else {
Write-Output "`nfound no folders with permissions :("
}
[int]$elapsedSecods = $stopwatch.Elapsed.Seconds
[int]$elapsedMinutes = $stopwatch.Elapsed.Minutes
Write-Output "Search took $elapsedMinutes minutes and $elapsedSecods seconds"
$stopwatch.Stop()