Weak File/Folder Permissions

MITRE ATT&CK™ Hijack Execution Flow - Technique T1574

Theory

It is very often in Windows environments to discover services that run with SYSTEM privileges. If you have write permissions over the folder or binary used by the service you can use it to escalate you privileges.

Practice

Service Binary Hijacking

Given sufficient permissions over a service's binary, swapping it with our own binary enables us to gain code execution as the user configured to run this service.

To identify weaknesses in service binary permissions, we can take the following steps: retrieve a complete list of all service binary files, retrieve their permissions, identify specific ones for our controlled user.

We can perform such enumeration by using one of the following methods:

CMD

for /f "tokens=2 delims='='" %a in ('wmic service list full^|find /i "pathname"^|find /i /v "system32"') do @echo %a >> %temp%\perm.txt
for /f eol^=^"^ delims^=^" %a in (%temp%\perm.txt) do cmd.exe /c icacls "%a" 2>nul | findstr "(M) (F) :\"

PowerShell

Get-WmiObject Win32_Service | ForEach-Object { $serviceName = $_.Name; $path = $_.PathName; $startName = $_.StartName; if ($path -ne $null -and $path -ne "") { $formattedPath = if ($path -match '.*\.exe') { if ($path -match '^"(.+?\.exe)') { $matches[1] } else { $path -replace '^(.*\.exe).*', '$1' } } else { $path }; $acl = try { Get-Acl -Path $formattedPath -ErrorAction Stop } catch { $null }; if ($acl -ne $null) { $relevantACE = $acl | Select-Object -ExpandProperty Access | Where-Object { $_.FileSystemRights -match 'Write|FullControl|Modify' }; if ($relevantACE) { [PSCustomObject]@{ ServiceName = $serviceName; FormattedPath = $formattedPath; StartName = $startName; ACL = $relevantACE | Select-Object -Property IdentityReference, FileSystemRights | Format-List | Out-String } } } } } | Sort-Object -Property FormattedPath -Unique | Format-List

PowerUp

. .\PowerUp.ps1
Get-ModifiableServiceFile

winPEAS

winPEASx64.exe servicesinfo

Service DLL Hijacking

DLL hijacking can be applied in many other cases, but this section focuses solely on services. For a more comprehensive approach, please refer to this page.

In case you have write permissions over the service binary folder, we can write our DLL in and then hijack the DLL search order. Here is the default DLL search order in windows (in safe mode which is the default):

  1. The executable directory.

  2. The system directory.

  3. The 16-bit system directory.

  4. The Windows directory.

  5. The current directory.

  6. The directories that are listed in the PATH environment variable.

We can enumerate permissive service folders by using the following PowerShell command

Get-WmiObject Win32_Service | ForEach-Object { $s = $_.Name; $p = $_.PathName; $start = $_.StartName; if ($p -ne $null -and $p -ne "") { $f = if ($p -match '.*\.exe') { if ($p -match '^"(.+\\)') { $matches[1] } else { $p -replace '^(.*\\).*', '$1' } } else { $p }; $a = try { (Get-Acl -Path $f -ErrorAction Stop).Access | Where-Object { $_.FileSystemRights -match 'Write|FullControl|Modify' } } catch { $null }; if ($a) { [PSCustomObject]@{ ServiceName = $s; StartName = $start; ExecutableFolder = $f; FolderACL = $a | Select-Object IdentityReference, FileSystemRights | Format-List | Out-String } } } } | Sort-Object -Property ExecutableFolder -Unique | Format-List

If we find a writable service folder, we first want to exfiltrate its service binary to a local windows machine. On this controlled computer, download Process Monitor (procmon) to monitor for missing or hijackable DLLs.

In procomon, specify this three filters (edit the service name with yours):

We may find some CreateFile actions with a NAME NOT FOUND result for a dll inside of the writable service binary folder. If so we can use this DLL name for DLL Hijacking !

Resources

Last updated