Linux Privilege Escalation
Introduction to Linux Privilege Escalation
The root account on Linux systems provides full administrative level access to the operating system. During an assessment, you may gain a low-privileged shell on a Linux host and need to perform privilege escalation to the root account. Fully compromising the host would allow us to capture traffic and access sensitive files, which may be used to further access within the environment. Additionally, if the Linux machine is domain joined, we can gain the NTLM hash and begin enumerating and attacking Active Directory.
Enumeration
Enumeration is the key to privilege escalation. Several helper scripts (such as LinEnum) exist to assist with enumeration. Still, it is also important to understand what pieces of information to look for and to be able to perform your enumeration manually. When you gain initial shell access to the host, it is important to check several key details.
OS Version
: Knowing the distribution (Ubuntu, Debian, FreeBSD, Fedora, SUSE, Red Hat, CentOS, etc.) will give you an idea of the types of tools that may be available. This would also identify the operating system version, for which there may be public exploits available.
Kernel Version
: As with the OS version, there may be public exploits that target a vulnerability in a specific kernel version. Kernel exploits can cause system instability or even a complete crash. Be careful running these against any production system, and make sure you fully understand the exploit and possible ramifications before running one.
Running Services
: Knowing what services are running on the host is important, especially those running as root. A misconfigured or vulnerable service running as root can be an easy win for privilege escalation. Flaws have been discovered in many common services such as Nagios, Exim, Samba, ProFTPd, etc. Public exploit PoCs exist for many of them, such as CVE-2016-9566, a local privilege escalation flaw in Nagios Core < 4.2.4.
List Current Processes (running as root)
Installed Packages and Versions
: Like running services, it is important to check for any out-of-date or vulnerable packages that may be easily leveraged for privilege escalation. An example is Screen, which is a common terminal multiplexer (similar to tmux). It allows you to start a session and open many windows or virtual terminals instead of opening multiple terminal sessions. Screen version 4.05.00 suffers from a privilege escalation vulnerability that can be easily leveraged to escalate privileges.
Logged in Users
: Knowing which other users are logged into the system and what they are doing can give greater into possible local lateral movement and privilege escalation paths.
List Current Processes
User Home Directories
: Are other user's home directories accessible? User home folders may also contain SSH keys that can be used to access other systems or scripts and configuration files containing credentials. It is not uncommon to find files containing credentials that can be leveraged to access other systems or even gain entry into the Active Directory environment.
Home Directory Contents
We can check individual user directories and check to see if files such as the .bash_history
file are readable and contain any interesting commands, look for configuration files, and check to see if we can obtain copies of a user's SSH keys
User's Home Directory Contents
If you find an SSH key for your current user, this could be used to open an SSH session on the host (if SSH is exposed externally) and gain a stable and fully interactive session. SSH keys could be leveraged to access other systems within the network as well. At the minimum, check the ARP cache to see what other hosts are being accessed and cross-reference these against any useable SSH private keys.
SSH Directory Contents
It is also important to check a user's bash history, as they may be passing passwords as an argument on the command line, working with git repositories, setting up cron jobs, and more. Reviewing what the user has been doing can give you considerable insight into the type of server you land on and give a hint as to privilege escalation paths.
Bash History
Sudo Privileges
: Can the user run any commands either as another user or as root? If you do not have credentials for the user, it may not be possible to leverage sudo permissions. However, often sudoer entries include NOPASSWD
, meaning that the user can run the specified command without being prompted for a password. Not all commands, even we can run as root, will lead to privilege escalation. It is not uncommon to gain access as a user with full sudo privileges, meaning they can run any command as root. Issuing a simple sudo su
command will immediately give you a root session.
Sudo - List User's Privileges
Configuration Files
: Configuration files can hold a wealth of information. It is worth searching through all files that end in extensions such as .conf
and .config
, for usernames, passwords, and other secrets.
Readable Shadow File
: If the shadow file is readable, you will be able to gather password hashes for all users who have a password set. While this does not guarantee further access, these hashes can be subjected to an offline brute-force attack to recover the cleartext password.
Password Hashes in /etc/passwd
: Occasionally, you will see password hashes directly in the /etc/passwd file. This file is readable by all users, and as with hashes in the shadow
file, these can be subjected to an offline password cracking attack. This configuration, while not common, can sometimes be seen on embedded devices and routers.
Passwd
Cron Jobs
: Cron jobs on Linux systems are similar to Windows scheduled tasks. They are often set up to perform maintenance and backup tasks. In conjunction with other misconfigurations such as relative paths or weak permissions, they can leverage to escalate privileges when the scheduled cron job runs.
Cron Jobs
Unmounted File Systems and Additional Drives
: If you discover and can mount an additional drive or unmounted file system, you may find sensitive files, passwords, or backups that can be leveraged to escalate privileges.
File Systems & Additional Drives
SETUID and SETGID Permissions
: Binaries are set with these permissions to allow a user to run a command as root, without having to grant root-level access to the user. Many binaries contain functionality that can be exploited to get a root shell.
Writeable Directories
: It is important to discover which directories are writeable if you need to download tools to the system. You may discover a writeable directory where a cron job places files, which provides an idea of how often the cron job runs and could be used to elevate privileges if the script that the cron job runs is also writeable.
Find Writable Directories
Writeable Files
: Are any scripts or configuration files world-writable? While altering configuration files can be extremely destructive, there may be instances where a minor modification can open up further access. Also, any scripts that are run as root using cron jobs can be modified slightly to append a command.
Find Writable Files
Special Permissions
The Set User ID upon Execution
(setuid
) permission can allow a user to execute a program or script with the permissions of another user, typically with elevated privileges. The setuid
bit appears as an s
.
It may be possible to reverse engineer the program with the SETUID bit set, identify a vulnerability, and exploit this to escalate our privileges. Many programs have additional features that can be leveraged to execute commands and, if the setuid
bit is set on them, these can be used for our purpose.
The Set-Group-ID (setgid) permission is another special permission that allows us to run binaries as if we were part of the group that created them. These files can be enumerated using the following command: find / -uid 0 -perm -6000 -type f 2>/dev/null
. These files can be leveraged in the same manner as setuid
binaries to escalate privileges.
This resource has more information about the setuid
and setgid
bits, including how to set the bits.
GTFOBins
The GTFOBins project is a curated list of binaries and scripts that can be used by an attacker to bypass security restrictions. Each page details the program's features that can be used to break out of restricted shells, escalate privileges, spawn reverse shell connections, and transfer files. For example, apt-get
can be used to break out of restricted environments and spawn a shell by adding a Pre-Invoke command:
It is worth familiarizing ourselves with as many GTFOBins as possible to quickly identify misconfigurations when we land on a system that we must escalate our privileges to move further.
Sudo Rights Abuse
When the sudo
command is issued, the system will check if the user issuing the command has the appropriate rights, as configured in /etc/sudoers
. When landing on a system, we should always check to see if the current user has any sudo privileges by typing sudo -l
. Sometimes we will need to know the user's password to list their sudo
rights, but any rights entries with the NOPASSWD
option can be seen without entering a password.
It is easy to misconfigure this. For example, a user may be granted root-level permissions without requiring a password. Or the permitted command line might be specified too loosely, allowing us to run a program in an unintended way, resulting in privilege escalation. For example, if the sudoers file is edited to grant a user the right to run a command such as tcpdump
per the following entry in the sudoers file: (ALL) NOPASSWD: /usr/sbin/tcpdump
an attacker could leverage this to take advantage of a the postrotate-command option.
By specifying the -z
flag, an attacker could use tcpdump
to execute a shell script, gain a reverse shell as the root user or run other privileged commands. For example, an attacker could create the shell script .test
containing a reverse shell and execute it as follows:
Let's try this out. First, make a file to execute with the postrotate-command
, adding a simple reverse shell one-liner.
Next, start a netcat
listener on our attacking box and run tcpdump
as root with the postrotate-command
. If all goes to plan, we will receive a root reverse shell connection.
We receive a root shell almost instantly.
AppArmor in more recent distributions has predefined the commands used with the postrotate-command
, effectively preventing command execution. Two best practices that should always be considered when provisioning sudo
rights:
1.
Always specify the absolute path to any binaries listed in the sudoers
file entry. Otherwise, an attacker may be able to leverage PATH abuse (which we will see in the next section) to create a malicious binary that will be executed when the command runs (i.e., if the sudoers
entry specifies cat
instead of /bin/cat
this could likely be abused).
2.
Grant sudo
rights sparingly and based on the principle of least privilege. Does the user need full sudo
rights? Can they still perform their job with one or two entries in the sudoers
file? Limiting the privileged command that a user can run will greatly reduce the likelihood of successful privilege escalation.
Privileged Groups
LXC / LXD
LXD is similar to Docker and is Ubuntu's container manager. Upon installation, all users are added to the LXD group. Membership of this group can be used to escalate privileges by creating an LXD container, making it privileged, and then accessing the host file system at /mnt/root
. Let's confirm group membership and use these rights to escalate to root.
Unzip the Alpine image.
Start the LXD initialization process. Choose the defaults for each prompt. Consult this post for more information on each step.
Import the local image.
Start a privileged container with the security.privileged
set to true
to run the container without a UID mapping, making the root user in the container the same as the root user on the host.
Mount the host file system.
Finally, spawn a shell inside the container instance. We can now browse the mounted host file system as root. For example, to access the contents of the root directory on the host type cd /mnt/root/root
. From here we can read sensitive files such as /etc/shadow
and obtain password hashes or gain access to SSH keys in order to connect to the host system as root, and more.
Docker
Placing a user in the docker group is essentially equivalent to root level access to the file system without requiring a password. Members of the docker group can spawn new docker containers. One example would be running the command docker run -v /root:/mnt -it ubuntu
. This command creates a new Docker instance with the /root directory on the host file system mounted as a volume. Once the container is started we are able to browse the mounted directory and retrieve or add SSH keys for the root user. This could be done for other directories such as /etc
which could be used to retrieve the contents of the /etc/shadow
file for offline password cracking or adding a privileged user.
Disk
Users within the disk group have full access to any devices contained within /dev
, such as /dev/sda1
, which is typically the main device used by the operating system. An attacker with these privileges can use debugfs
to access the entire file system with root level privileges. As with the Docker group example, this could be leveraged to retrieve SSH keys, credentials or to add a user.
ADM
Members of the adm group are able to read all logs stored in /var/log
. This does not directly grant root access, but could be leveraged to gather sensitive data stored in log files or enumerate user actions and running cron jobs.
Capabilities
Linux capabilities are a security feature in the Linux operating system that allows specific privileges to be granted to processes, allowing them to perform specific actions that would otherwise be restricted. This allows for more fine-grained control over which processes have access to certain privileges, making it more secure than the traditional Unix model of granting privileges to users and groups.
However, like any security feature, Linux capabilities are not invulnerable and can be exploited by attackers. One common vulnerability is using capabilities to grant privileges to processes that are not adequately sandboxed or isolated from other processes, allowing us to escalate their privileges and gain access to sensitive information or perform unauthorized actions.
Another potential vulnerability is the misuse or overuse of capabilities, which can result in processes having more privileges than they need. This can create unnecessary security risks, as we could exploit these privileges to gain access to sensitive information or perform unauthorized actions.
Setting capabilities involves using the appropriate tools and commands to assign specific capabilities to executables or programs. In Ubuntu, for example, we can use the setcap
command to set capabilities for specific executables. This command allows us to specify the capability we want to set and the value we want to assign.
Set Capability
When capabilities are set for a binary, it means that the binary will be able to perform specific actions that it would not be able to perform without the capabilities. For example, if the cap_net_bind_service
capability is set for a binary, the binary will be able to bind to network ports, which is a privilege usually restricted.
Some capabilities, such as cap_sys_admin
, which allows an executable to perform actions with administrative privileges, can be dangerous if they are not used properly. For example, we could exploit them to escalate their privileges, gain access to sensitive information, or perform unauthorized actions. Therefore, it is crucial to set these types of capabilities for properly sandboxed and isolated executables and avoid granting them unnecessarily.
Capability
Description
cap_sys_admin
Allows to perform actions with administrative privileges, such as modifying system files or changing system settings.
cap_sys_chroot
Allows to change the root directory for the current process, allowing it to access files and directories that would otherwise be inaccessible.
cap_sys_ptrace
Allows to attach to and debug other processes, potentially allowing it to gain access to sensitive information or modify the behavior of other processes.
cap_sys_nice
Allows to raise or lower the priority of processes, potentially allowing it to gain access to resources that would otherwise be restricted.
cap_sys_time
Allows to modify the system clock, potentially allowing it to manipulate timestamps or cause other processes to behave in unexpected ways.
cap_sys_resource
Allows to modify system resource limits, such as the maximum number of open file descriptors or the maximum amount of memory that can be allocated.
cap_sys_module
Allows to load and unload kernel modules, potentially allowing it to modify the operating system's behavior or gain access to sensitive information.
cap_net_bind_service
Allows to bind to network ports, potentially allowing it to gain access to sensitive information or perform unauthorized actions.
When using the setcap
command to set capabilities for an executable in Linux, we need to specify the capability we want to set and the value we want to assign. The values we use will depend on the specific capability we are setting and the privileges we want to grant to the executable.
Here are some examples of values that we can use with the setcap
command, along with a brief description of what they do:
Capability Values
Description
=
This value sets the specified capability for the executable, but does not grant any privileges. This can be useful if we want to clear a previously set capability for the executable.
+ep
This value grants the effective and permitted privileges for the specified capability to the executable. This allows the executable to perform the actions that the capability allows but does not allow it to perform any actions that are not allowed by the capability.
+ei
This value grants sufficient and inheritable privileges for the specified capability to the executable. This allows the executable to perform the actions that the capability allows and child processes spawned by the executable to inherit the capability and perform the same actions.
+p
This value grants the permitted privileges for the specified capability to the executable. This allows the executable to perform the actions that the capability allows but does not allow it to perform any actions that are not allowed by the capability. This can be useful if we want to grant the capability to the executable but prevent it from inheriting the capability or allowing child processes to inherit it.
Several Linux capabilities can be used to escalate a user's privileges to root
, including:
Capability
Description
cap_setuid
Allows a process to set its effective user ID, which can be used to gain the privileges of another user, including the root
user.
cap_setgid
Allows to set its effective group ID, which can be used to gain the privileges of another group, including the root
group.
cap_sys_admin
This capability provides a broad range of administrative privileges, including the ability to perform many actions reserved for the root
user, such as modifying system settings and mounting and unmounting file systems.
cap_dac_override
Allows bypassing of file read, write, and execute permission checks.
Enumerating Capabilities
It is important to note that these capabilities should be used with caution and only granted to trusted processes, as they can be misused to gain unauthorized access to the system. To enumerate all existing capabilities for all existing binary executables on a Linux system, we can use the following command:
Exploitation
If we gained access to the system with a low-privilege account, then discovered the cap_dac_override
capability:
For example, the /usr/bin/vim.basic
binary is run without special privileges, such as with sudo
.
However, because the binary has the cap_dac_override
capability set, it can escalate the privileges of the user who runs it. This would allow the penetration tester to gain the cap_dac_override
capability and perform tasks that require this capability.
Let us take a look at the /etc/passwd
file where the user root
is specified:
We can use the cap_dac_override
capability of the /usr/bin/vim
binary to modify a system file:
We also can make these changes in a non-interactive mode:
Now, we can see that the x
in that line is gone, which means that we can use the command su
to log in as root without being asked for the password.
Last updated