Enumeration
In PowerShell
Caution: This command works via RDP in an interactive session, but NOT in a non-privileged bind/winrm shell.
All services:
PS> Get-CimInstance -ClassName win32_service | Select Name,State,PathName
All running services:
PS> Get-CimInstance -ClassName win32_service | Select Name,State,PathName | Where-Object {$_.State -like 'Running'}
PS> Get-CimInstance -ClassName win32_service | Select Name,State,PathName | Where-Object {$_.State -like 'Running'} | Where-Object { -not ($_.PathName -like '*indows*') }
All autostart programs (not services, but it fits here anyway…):
Get-CimInstance Win32_StartupCommand | Select-Object Name, command, Location, User | Format-List
Check if a service will start automatically on boot time:
PS> Get-CimInstance -ClassName win32_service | Select Name, StartMode | Where-Object {$_.Name -like 'mysq*'} // This is not grep! Add * here!
In Windows command line
schtasks /query /fo LIST /v
net start
tasklist /v
tasklist /SVC // Shows all running processes only
accesschk.exe /accepteula -ucqv $servicename // Show which users have permissions for a serviceaccesschk64.exe /accepteula -wvucq $currentUserName $serviceexe// Shows the permissions you have
The following lists all service names and the binaries for them. After this, check interesting files with cacls / icacls.
sc query state= all | findstr "SERVICE_NAME:" >> Servicenames.txt
FOR /F "tokens=2 delims= " %i in (Servicenames.txt) DO @echo %i >> services.txt
FOR /F %i in (services.txt) DO @sc qc %i | findstr "BINARY_PATH_NAME" >> path.txt
Show all path of executeable files which are used by Windows services
sc query state= all | findstr "SERVICE_NAME:" >> a & FOR /F "tokens=2 delims= " %i in (a) DO @echo %i >> b & FOR /F %i in (b) DO @(@echo %i & @echo --------- & @sc qc %i | findstr "BINARY_PATH_NAME" & @echo.) & del a 2>nul & del b 2>nul
With escalation scripts
- Execute
winPEASany.exe quiet servicesinfoand see if there are interesting services. - Use PowerUp with AllChecks to see also services and their executables. Note if there is a service with CanRestart: True.
Handling services with sc
sc query state= all // shows all services
sc qc $service // shows one service
sc start $service
sc stop $service
sc config $service start= demand // only needed to manually activate a service which is disabled. Remember to disable it afterwards again.
Handling services with wmic
Show processes:
wmic /node:$victim /user:$user /password:$password process list brief
Kill processes by PID or name:
wmic /node:$victim /user:$user /password:$password process where processid="$pid" delete wmic /node:$victim /user:$user /password:$password process where name="$name" delete
Note: Check file permissions for each service file — if we can write to one, we can change the file or inject own code with shelter!
Exploitation
If you need to restart a service:
PS> Restart-Service BetaService
Binary hijacking
There is a binary writeable you can execute? See binary hijacking.
DLL injection + hijacking
Does the target run a (privileged) software you can install on an own VM? Try to investigate with Procmon, if it loads DLLs you could overwrite. See the DLL injection article.
Unquoted Service Paths
Check if a (privileged) service is running in a directory with spaces in which we could write a binary. See the Unquoted Service Path article.
Leave a Reply
You must be logged in to post a comment.