Skip to content

Commit

Permalink
Filter out system directories when searching for modifiable apps
Browse files Browse the repository at this point in the history
  • Loading branch information
itm4n committed Sep 3, 2023
1 parent 13405c5 commit 3fc7475
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Improved result header.
- Improved BIOS mode + Secure Boot check.
- The modifiable application check now displays a warning when a system folder is detected, rather than searching it recursively.

## 2023-08-20

Expand Down
36 changes: 18 additions & 18 deletions PrivescCheck.ps1

Large diffs are not rendered by default.

64 changes: 47 additions & 17 deletions src/302_Application.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,52 @@ function Invoke-ModifiableProgramsCheck {

[CmdletBinding()] Param()

$Items = Get-InstalledPrograms -Filtered

foreach ($Item in $Items) {

$SearchPath = New-Object -TypeName System.Collections.ArrayList
[void]$SearchPath.Add([String]$(Join-Path -Path $Item.FullName -ChildPath "\*")) # Do this to avoid the use of -Depth which is PSH5+
[void]$SearchPath.Add([String]$(Join-Path -Path $Item.FullName -ChildPath "\*\*")) # Do this to avoid the use of -Depth which is PSH5+

$ChildItems = Get-ChildItem -Path $SearchPath -ErrorAction SilentlyContinue -ErrorVariable GetChildItemError

if (-not $GetChildItemError) {
BEGIN {
$SystemPaths = @()

function Test-IsSystemFolder {
param(
[string] $Path
)

# Initialize system path list
if ($SystemPaths.Count -eq 0) {
[string[]] $SystemPaths += $env:windir
[string[]] $SystemPaths += Join-Path -Path "$($env:windir)" -ChildPath "System"
[string[]] $SystemPaths += Join-Path -Path "$($env:windir)" -ChildPath "System32"
[string[]] $SystemPaths += Join-Path -Path "$($env:windir)" -ChildPath "Syswow64"
[string[]] $SystemPaths += Join-Path -Path "$($env:windir)" -ChildPath "Sysnative"
[string[]] $SystemPaths += $env:ProgramFiles
[string[]] $SystemPaths += ${env:ProgramFiles(x86)}
[string[]] $SystemPaths += $env:ProgramData
}

$ChildItems | ForEach-Object {
$SystemPaths -contains $Path.TrimEnd('\\')
}
}

PROCESS {
$Items = Get-InstalledPrograms -Filtered

foreach ($Item in $Items) {

# Ensure the path is not a known system folder, in which case it does not make
# sense to check it. This also prevents the script from spending a considerable
# amount of time and resources searching those paths recursively.
if (Test-IsSystemFolder -Path $Item.FullName) {
Write-Warning "System path detected, ignoring: $($Item.FullName)"
continue
}

# Build the search path list. The following trick is used to search recursively
# without using the 'Depth' option, which is only available in PSv5+. This
# allows us to maintain compatibility with PSv2.
$SearchPath = New-Object -TypeName System.Collections.ArrayList
[void]$SearchPath.Add([String]$(Join-Path -Path $Item.FullName -ChildPath "\*"))
[void]$SearchPath.Add([String]$(Join-Path -Path $Item.FullName -ChildPath "\*\*"))

Get-ChildItem -Path $SearchPath -ErrorAction SilentlyContinue | ForEach-Object {

if ($_ -is [System.IO.DirectoryInfo]) {
$ModifiablePaths = $_ | Get-ModifiablePath -LiteralPaths
}
Expand All @@ -71,11 +103,9 @@ function Invoke-ModifiableProgramsCheck {
}
}

if ($ModifiablePaths) {
foreach ($Path in $ModifiablePaths) {
if ($Path.ModifiablePath -eq $_.FullName) {
$Path
}
foreach ($Path in $ModifiablePaths) {
if ($Path.ModifiablePath -eq $_.FullName) {
$Path
}
}
}
Expand Down

0 comments on commit 3fc7475

Please sign in to comment.