12. Linux Priv Escalation V2
12. Linux Priv Escalation V2
@mmar
Hassan 1
Linux Privilege Escalation
This Lecture will focus on privilege escalation techniques in Linux and *nix
systems. You will learn:
How basic user privileges work in Linux
Multiple methods for escalating your user’s privileges
Why and how these methods work
Tools you can use to identify potential escalation paths
Hassan 2
Pre-Requisites
A basic understanding of Linux systems would be desirable, though the lecture will
cover some aspects
This course assumes that you have a fully working low- privileged shell on a Linux
system, and will not cover obtaining one (this is a post-exploitation lecture)
The “user” account password is: password321 The “root” account password is: password123
https://tryhackme.com/r/room/linprivesc
Hassan 3
General Concepts
Our ultimate goal with privilege escalation in Linux is to gain a shell running as the
root user
Privilege escalation can be simple (e.g. a kernel exploit) or require a lot of
reconnaissance on the compromised system
In a lot of cases, privilege escalation may not simply rely on a single
misconfiguration, but may require you to think, and combine multiple
misconfigurations
Hassan 4
General Concepts
Hassan 5
Understanding
Permissions in
Linux
6
Users, Groups, and Files & Directories
Hassan 7
Users
User accounts are configured in the /etc/passwd file. User password hashes are
stored in the /etc/shadow file. Users are identified by an integer user ID (UID)
The “root” user account is a special type of account in Linux. It has a UID of 0, and
the system grants this user access to every file
Hassan 8
Groups
Hassan 9
Files & Directories
Hassan 10
File Permissions
Hassan 11
Directory Permissions
Hassan 12
Special Permissions
Hassan 13
Viewing Permissions
$ ls -l /bin/date
-rwxr-xr-x 1 root root 60416 Apr 28 2010 /bin/date
Hassan 14
Viewing Permissions
Hassan 15
Real, Effective, & Saved UID/GID
Hassan 16
Real, Effective, & Saved UID/GID
A user’s effective ID is normally equal to their real ID, however when executing a
process as another user, the effective ID is set to that user’s real ID
The effective ID is used in most access control decisions to verify a user, and
commands such as whoami use the effective ID
Finally, the saved ID is used to ensure that SUID processes can temporarily switch
a user’s effective ID back to their real ID and back again without losing track of the
original effective ID
Hassan 17
Real, Effective, & Saved UID/GID
Print real, effective, saved, and file system user / group IDs of the current process
(i.e. our shell):
Hassan 18
Spawning
Root Shells
19
Spawning Root Shells
Hassan 20
“rootbash” SUID
One of the easiest ways to spawn a root shell is to create a copy of the /bin/bash
executable file
Make sure it is owned by the root user, and has the SUID bit set
A root shell can be spawned by simply executing the rootbash file with the -p
command line option.
The benefit of this method is it is persistent (once you run the exploit, rootbash can
be used multiple times)
Hassan 21
Custom Executable
There may be instances where some root process executes another process
which you can control. In these cases, the following C code, once compiled, will
spawn a Bash shell running as root:
int main() {
setuid(0);
system("/bin/bash -p");
}
Compile using:
$ gcc -o <name> <filename.c>
Hassan 22
msfvenom
This reverse shell can be caught using Netcat or Metasploit’s own multi/handler
Hassan 23
Native Reverse Shells
There are multiple ways to spawn reverse shells natively on many Linux
distributions
A good tool for suggesting these is: https://github.com/mthbernardes/rsg
All can be caught using a simple Netcat listener
Hassan 24
Privilege
Escalation
Tools
25
Why use tools?
Tools allow us to automate the reconnaissance that can identify potential privilege
escalations
While it is always important to understand what tools are doing, they are
invaluable in a time-limited setting
The most widely used tools are Linux Smart Enumeration and LinEnum
Hassan 26
Linux Smart Enumeration
Hassan 27
LinEnum
Hassan 28
Other Tools
There are also many other useful tools which you can experiment with
https://github.com/linted/linuxprivchecker
https://github.com/AlessandroZ/BeRoot
http://pentestmonkey.net/tools/audit/unix-privesc-check
https://github.com/peass-ng/PEASS-ng/tree/master/linPEAS
Hassan 29
Kernel
Exploits
30
What is a Kernel?
Hassan 31
Finding Kernel Exploits
Hassan 32
Finding Kernel Exploits
Note that none of the exploits match the distribution of Linux (Debian)
Hassan 33
Finding Kernel Exploits
3. We can try and adjust our search to be less specific with the kernel version, but
more specific with the distribution:
# searchsploit linux kernel 2.6 priv esc debian
4. Again, we get a few exploits that we can’t use for various reasons.
Hassan 34
Finding Kernel Exploits
4. Again, we get a few exploits that we can’t use for various reasons.
5. Install Linux Exploit Suggester 2 https://github.com/jondonas/linux-exploit-
suggester-2 tool against the original kernel version:
Hassan 35
Finding Kernel Exploits
6. There are a number of Dirty COW exploits, all of which use different methods to
obtain a root shell. The following version seems to work best on the practice VM:
7. Download and compile it using the instructions in the file:
Hassan 36
Finding Kernel Exploits
9. Once the exploit is complete, simply execute the /usr/bin/passwd binary to get a
root shell:
$ /usr/bin/passwd
root@debian:/home/user# id
uid=0(root) gid=1000(user) groups=0(root) ...
it replaces the SUID file /usr/bin/passwd with one that spawns a shell (a backup of /usr/bin/passwd is made at /tmp/bak).
Hassan 37
Finding Kernel Exploits
10. Remember to restore the original /usr/bin/passwd file and exit the root shell
before continuing
mv /tmp/bak /usr/bin/passwd
exit
Hassan 38
Passwords
& Keys
39
Passwords
While it might seem like a long shot, weak password storage and password re-use
can be easy ways to escalate privileges.
While the root user’s account password is hashed and stored securely in
/etc/shadow, other passwords, such as those for services may be stored in
plaintext in config files
If the root user re-used their password for a service, that password may be found
and used to switch to the root user
Hassan 40
History Files
History files record commands issued by users while they are using certain
programs
If a user types a password as part of a command, this password may get stored in
a history file
It is always a good idea to try switching to the root user with a discovered
password
Hassan 41
History Files
1. View the contents of hidden files in the user’s home directory with filenames
ending in “history”:
$ cat ~/.*history | less
ls -al
cat .bash_history
ls -al
mysql -h somehost.local -uroot -ppassword123
It appears that the user connected to a MySQL server as the root user using the
password “password123”.
Hassan 42
History Files
2. Use the su command to switch to the root user account, using the password
found in the history file:
$ su root
Password:
root@debian:/home/user# id
uid=0(root) gid=0(root) groups=0(root)
Hassan 43
Config Files
Many services and programs use configuration (config) files to store settings
If a service needs to authenticate to something, it might store the credentials in a
config file
If these config files are accessible, and the passwords they store are reused by
privileged users, we may be able to use it to log in as that user
Hassan 44
Config Files
Hassan 45
Config Files
4. Use the su command to switch to the root user account, using the password
found in the auth.txt file:
$ su root
Password:
root@debian:/home/user# id
uid=0(root) gid=0(root) groups=0(root)
Hassan 46
SSH Keys
SSH keys can be used instead of passwords to authenticate users using SSH
SSH keys come in pairs: one private key, and one public key. The private key
should always be kept secret
If a user has stored their private key insecurely, anyone who can read the key may
be able to log into their account using it.
Hassan 47
SSH Keys
1. A hidden directory (.ssh) exists in the system root directory. View the contents
of this directory:
$ ls -l /.ssh
total 4
-rw-r--r-- 1 root root 1679 Aug 19 06:56 root_key
$ cat /.ssh/root_key
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA3IIf6Wczcdm38MZ9+QADSYq9FfKfwj0mJaUteyJHWHZ3/GNm
...
Hassan 48
SSH Keys
3. Copy the root_key file to your local machine and correct its permissions so
SSH will accept it:
$ chmod 600 root_key
4. Use the key to connect to the SSH server as the root user:
$ ssh -i root_key [email protected] -oHostKeyAlgorithms=+ssh-rsa
...
root@debian:~# id
uid=0(root) gid=0(root) groups=0(root)
host *
# nano ~/.ssh/config, add the lines at the beginning PubkeyAcceptedKeyTypes=+ssh-rsa
HostKeyAlgorithms=+ssh-rsa
Hassan 49
Weak File
Permissions
50
Weak File Permissions
Hassan 51
Useful Commands
Hassan 52
Useful Commands
Hassan 53
/etc/shadow
The /etc/shadow file contains user password hashes, and by default is not
readable by any user except for root
If we are able to read the contents of the /etc/shadow file, we might be able to
crack the root user’s password hash
If we are able to modify the /etc/shadow file, we can replace the root user’s
password hash with one we know
Hassan 54
Readable /etc/shadow
Hassan 55
Readable /etc/shadow
Hassan 56
Writable /etc/shadow
Hassan 57
Writable /etc/shadow
Edit the /etc/shadow and replace the root user’s password hash with the one we
generated
Hassan 58
Writable /etc/shadow
Hassan 59
/etc/passwd
Hassan 60
/etc/passwd
The “x” in the second field instructs Linux to look for the password hash in the
/etc/shadow file
In some versions of Linux, it is possible to simply delete the “x”, which Linux
interprets as the user having no password
root::0:0:root:/root:/bin/bash
Hassan 61
Writable /etc/passwd
Hassan 62
Writable /etc/passwd
Edit the /etc/passwd file and enter the hash in the second field of the root user
row:
Hassan 63
Writable /etc/passwd
Hassan 64
Backups
Hassan 65
Backups
Hassan 66
Backups
Further inspection of this file seems to indicate that this is an SSH private key. The
name and owner of the file suggest this key belongs to the root user
$ head -n 1 /.ssh/root_key
-----BEGIN RSA PRIVATE KEY-----
Hassan 67
Backups
Before we try to use this key, let’s confirm that root logins are even allowed via
SSH
Copy the key over to your local machine, and give it correct permissions
(otherwise SSH will refuse to use it)
# chmod 600 root_key
Hassan 68
Backups
Hassan 69
Sudo
70
What is sudo?
sudo is a program which lets users run other programs with the security privileges
of other users. By default, that other user will be root
A user generally needs to enter their password to use sudo, and they must be
permitted access via rule(s) in the /etc/sudoers file
Rules can be used to limit users to certain programs and forgo the password entry
requirement
Hassan 71
Useful Commands
Hassan 72
Known Password
By far the most obvious privilege escalation with sudo is to use sudo as it was
intended!
If your low privileged user account can use sudo unrestricted (i.e. you can run any
programs) and you know the user’s password, privilege escalation is easy, by
using the “switch user” (su) command to spawn a root shell
$ sudo su
Hassan 73
Other Methods
If for some reason the su program is not allowed, there are many other ways to
escalate privileges:
$ sudo -s
$ sudo -i
$ sudo /bin/bash
$ sudo passwd
Even if there are no “obvious” methods for escalating privileges, we may be able
to use a shell escape sequence
Hassan 74
Shell Escape Sequences
Hassan 75
Privilege Escalation (Generic)
Hassan 76
Privilege Escalation (Generic)
For each program in the list, see if there is a shell escape sequence on GTFOBins
(https://gtfobins.github.io/)
If an escape sequence exists, run the program via sudo and perform the sequence
to spawn a root shell
Hassan 77
Privilege Escalation (Generic)
For each program in the list, see if there is a shell escape sequence on GTFOBins
(https://gtfobins.github.io/)
If an escape sequence exists, run the program via sudo and perform the sequence
to spawn a root shell
Hassan 78
Privilege Escalation (Generic)
Hassan 79
Privilege Escalation (Generic)
Hassan 80
Abusing Intended Functionality
Hassan 81
Privilege Escalation
Hassan 82
Privilege Escalation
Hassan 83
Privilege Escalation
Use the su command to switch to the root user, entering the password we cracked
when prompted:
$ su
Password:
root@debian:/# id
uid=0(root) gid=0(root) groups=0(root)
Hassan 84
Privilege
Escalation
Strategy
85
Enumeration
Hassan 86
Strategy
Spend some time and read over the results of your enumeration
If Linux Smart Enumeration level 0 or 1 finds something interesting, make a note
of it
Avoid rabbit holes by creating a checklist of things you need for the privilege
escalation method to work
Hassan 87
Strategy
Have a quick look around for files in your user’s home directory and other common
locations (e.g. /var/backup, /var/logs)
If your user has a history file, read it, it may have important information like
commands or even passwords.
Hassan 88
Strategy
Try things that don’t have many steps first, e.g. Sudo, Cron Jobs, SUID files.
Have a good look at root processes, enumerate their versions and search for
exploits.
Check for internal ports that you might be able to forward to your attacking
machine.
Hassan 89
Strategy
If you still don’t have root, re-read your full enumeration dumps and highlight
anything that seems odd
This might be a process or file name you aren’t familiar with, an “unusual”
filesystem configured (on Linux, anything that isn’t ext, swap, or tmpfs), or even a
username
At this stage you can also start to think about Kernel Exploits
Hassan 90
Don’t Panic
Hassan 91
Thanks
Hassan 92