Privilege Escalation
Privilege Escalation
Objective
This blog is all about security issues that could lead to a successful privilege
escalation attack on any Linux based systems. We will also discuss about
how an attacker can use the possible known techniques to successfully
elevate his privileges on a remote host and how we can protect our systems
from any such attack. At the end, there will be some examples to
demonstrate how we achieved privilege escalation on different Linux
systems under different conditions.
It is not a cheat sheet for enumeration using Linux commands, instead the
blog is particularly aimed at helping beginners understand the fundamentals
of Linux privilege escalation with examples.
Privilege escalation is all about proper enumeration. There are multiple ways
to perform the same tasks that I have shown in the examples.
Linux is inherited from UNIX, the concept of ownership and file permissions.
File permissions are one way the system protects itself from malicious
tampering. On a UNIX web server, every single file and folder stored on the
hard drive has a set of permissions associated with it, which says who is
allowed to do what with the file.
In the above two screenshots we can see that the file ‘docker-compose.yml’
only has read access by the owner which is ‘root’. Only the superuser can
read this file; when I attempted to read the file as a non-superuser, the
“permission denied” error occured.
1. Kernel Exploits
Assuming that we can run code as an unprivileged user, this is the generic
workflow of a kernel exploit.
1. A vulnerable kernel
2. A matching exploit
3. The ability to transfer the exploit onto the target
4. The ability to execute the exploit on the target
The easiest way to defend against kernel exploits is to keep the kernel
patched and updated. In the absence of patches, administrators can strongly
influence the ability to transfer and execute the exploit on the target. Given
these considerations, kernel exploit attacks are no longer viable if an
administrator can prevent the introduction and/or execution of the exploit
onto the Linux file system.
A race condition was found in the way the Linux kernel’s memory subsystem
handled the copy-on-write (COW) breakage of private read-only memory
mappings. An unprivileged local user could use this flaw to gain write access
to otherwise read-only memory mappings and thus increase their privileges
on the system. It was one of the most serious privilege escalation
vulnerability ever discovered and it affected almost all the major Linux
distros.
Though, it feels very tempting to just run an exploit and get root access, but
you should always keep this as your last option.
1. The remote host might crash as many of the root exploits publicly
available are not very stable.
2. You might get root and then crash the box.
3. The exploit might leave traces/logs that can get you caught.
You should always try the other techniques to get root which have been
discussed below before directly jumping to run a local root exploit.
Countermeasures
Exploiting any service which is running as root will give you Root!
The famous EternalBlue and SambaCry exploit, exploited smb service which
generally runs as root.
With just one exploit, an attacker can get remote code execution and Local
Privilege Escalation as well.
It was heavily used to spread ransomware across of the globe because of it’s
deadly combination.
You should always check if web servers, mail servers, database servers, etc.
are running as root. Many a times, web admins run these services as root
and forget about the security issues it might cause. There could be services
which run locally and are not exposed publicly which can also be exploited.
$ netstat -antup – It shows you all the ports which are open and are
listening. We can check for services which are running locally if they could
be exploited or not.
MySQL UDF Dynamic Library exploit lets you execute arbitrary commands
from the mysql shell. If mysql is running with root privileges, the commands
will be executed as root.
$ ps -aux | grep root – It shows us the services which are running as root.
> We can execute arbitrary commands using MySQL shell which will
be executed as root.
One of the biggest mistake web admins do, is to run a webserver with root
privilege. A command injection vulnerability on the web application can lead
an attacker to root shell. This is a classic example of why you should never
run any service as root unless really required.
Binary exploits of a root owned program are far less dangerous than a kernel
exploit because even if the service crashes, the host machine will not crash
and the services will probably auto restart.
Countermeasures
• Never run any service as root unless really required, especially web,
database and file servers.
SUID which stands for set user ID, is a Linux feature that allows users to
execute a file with the permissions of a specified user. For example, the
Linux ping command typically requires root permissions in order to open raw
network sockets. By marking the ping program as SUID with the owner as
root, ping executes with root privileges anytime a low privilege user
executes the program.
> -rwsr-xr-x– The ‘s’ character instead of ‘x’ indicates that the SUID bit is
set.
SUID is a feature that, when used properly, actually enhances Linux
security. The problem is that administrators may unknowingly introduce
dangerous SUID configurations when they install third party applications or
make logical configuration changes.
A large number of sysadmins don’t understand as where to set SUID bit and
where not. SUID bit should not be set especially on any file editor as an
attacker can overwrite any files present on the system.
> Nmap has SUID bit set. A lot of times administrators set the SUID bit to
nmap so that it can be used to scan the network efficiently as all the nmap
scanning techniques does not work if you don’t run it with root privilege.
> However, there is a functionality in nmap older versions where you can
run nmap in an interactive mode which allows you to escape to shell. If
nmap has SUID bit set, it will run with root privilege and we can get access
to ‘root’ shell through it’s interactive mode.
Countermeasures
• SUID bit should not be set to any program which lets you escape to
the shell.
• You should never set SUID bit on any file editor/compiler/interpreter
as an attacker can easily read/overwrite any files present on the
system.
If the attacker can’t directly get root access via any other techniques, he
might try to compromise any of the users who have SUDO access. Once he
has access to any of the SUDO users, he can basically execute any
commands with root privileges.
Administrators might just allow the users to run a few commands through
SUDO and not all of them but even with this configuration, they might
introduce vulnerabilities unknowingly which can lead to privilege escalation.
> Never give SUDO rights to any of the programming language compiler,
interpreter and editors.
> This technique can also be applied to vi, more, less, perl, ruby, gdb and
others.
Countermeasures
• Do not give sudo rights to any program which lets you escape to the
shell.
• Never give SUDO rights to vi, more, less, nmap, perl, ruby, python,
gdb and others.
Cron jobs generally run with root privileges. If we can successfully tamper
any script or binary which are defined in the cron jobs then we can execute
arbitrary code with root privilege.
$ ls -la /etc/cron.d – prints cron jobs which are already present in cron.d
$ ls -la rootme – After 5 minutes, the logrotate cronjob was run and cron-
logrotate.sh got execute with root privilege.
Let’s say Susan is an administrator and she adds ‘.’ in her path so that she
doesn’t have to write the 2 characters again.
This happens because Linux first searches for the program in the current
directory when ‘.’ is added in the PATH at the beginning and then searches
anywhere else.
> Another user ‘rashid’ knew that susan has added ‘.’ in her PATH because
she is lazy
> rashid tells susan that ‘ls’ command is not working in his directory
> rashid adds a code in his directory which will change the sudoers file and
make him administrator
> rashid stores that code in a file named as ‘ls’ and makes it executable
> susan has root privileges. She comes and executes ‘ls’ command in
rashid’s home directory
> Instead of the original ‘ls’ command, the malicious code gets executed
with root access
> Inside a file saved as ‘ls’, a code has been added which will print “Hello
world”
$ PATH=.:${PATH} – adds ‘.’ in the PATH variable
> Now, if a root user executes the code with root privilege, we can achieve
arbitrary code execution with root privilege.