Linux Capabilities
Last updated
Last updated
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
RootedCON is the most relevant cybersecurity event in Spain and one of the most important in Europe. With the mission of promoting technical knowledge, this congress is a boiling meeting point for technology and cybersecurity professionals in every discipline.\
Linux capabilities divide root privileges into smaller, distinct units, allowing processes to have a subset of privileges. This minimizes the risks by not granting full root privileges unnecessarily.
Normal users have limited permissions, affecting tasks like opening a network socket which requires root access.
Inherited (CapInh):
Purpose: Determines the capabilities passed down from the parent process.
Functionality: When a new process is created, it inherits the capabilities from its parent in this set. Useful for maintaining certain privileges across process spawns.
Restrictions: A process cannot gain capabilities that its parent did not possess.
Effective (CapEff):
Purpose: Represents the actual capabilities a process is utilizing at any moment.
Functionality: It's the set of capabilities checked by the kernel to grant permission for various operations. For files, this set can be a flag indicating if the file's permitted capabilities are to be considered effective.
Significance: The effective set is crucial for immediate privilege checks, acting as the active set of capabilities a process can use.
Permitted (CapPrm):
Purpose: Defines the maximum set of capabilities a process can possess.
Functionality: A process can elevate a capability from the permitted set to its effective set, giving it the ability to use that capability. It can also drop capabilities from its permitted set.
Boundary: It acts as an upper limit for the capabilities a process can have, ensuring a process doesn't exceed its predefined privilege scope.
Bounding (CapBnd):
Purpose: Puts a ceiling on the capabilities a process can ever acquire during its lifecycle.
Functionality: Even if a process has a certain capability in its inheritable or permitted set, it cannot acquire that capability unless it's also in the bounding set.
Use-case: This set is particularly useful for restricting a process's privilege escalation potential, adding an extra layer of security.
Ambient (CapAmb):
Purpose: Allows certain capabilities to be maintained across an execve
system call, which typically would result in a full reset of the process's capabilities.
Functionality: Ensures that non-SUID programs that don't have associated file capabilities can retain certain privileges.
Restrictions: Capabilities in this set are subject to the constraints of the inheritable and permitted sets, ensuring they don't exceed the process's allowed privileges.
For further information check:
To see the capabilities for a particular process, use the status file in the /proc directory. As it provides more details, let’s limit it only to the information related to Linux capabilities. Note that for all running processes capability information is maintained per thread, for binaries in the file system it’s stored in extended attributes.
You can find the capabilities defined in /usr/include/linux/capability.h
You can find the capabilities of the current process in cat /proc/self/status
or doing capsh --print
and of other users in /proc/<pid>/status
This command should return 5 lines on most systems.
CapInh = Inherited capabilities
CapPrm = Permitted capabilities
CapEff = Effective capabilities
CapBnd = Bounding set
CapAmb = Ambient capabilities set
These hexadecimal numbers don’t make sense. Using the capsh utility we can decode them into the capabilities name.
Lets check now the capabilities used by ping
:
Although that works, there is another and easier way. To see the capabilities of a running process, simply use the getpcaps tool followed by its process ID (PID). You can also provide a list of process IDs.
Lets check here the capabilities of tcpdump
after having giving the binary enough capabilities (cap_net_admin
and cap_net_raw
) to sniff the network (tcpdump is running in process 9562):
As you can see the given capabilities corresponds with the results of the 2 ways of getting the capabilities of a binary. The getpcaps tool uses the capget() system call to query the available capabilities for a particular thread. This system call only needs to provide the PID to obtain more information.
Binaries can have capabilities that can be used while executing. For example, it's very common to find ping
binary with cap_net_raw
capability:
You can search binaries with capabilities using:
If we drop the CAP_NET_RAW capabilities for ping, then the ping utility should no longer work.
Besides the output of capsh itself, the tcpdump command itself should also raise an error.
/bin/bash: /usr/sbin/tcpdump: Operation not permitted
The error clearly shows that the ping command is not allowed to open an ICMP socket. Now we know for sure that this works as expected.
You can remove capabilities of a binary with
Apparently it's possible to assign capabilities also to users. This probably means that every process executed by the user will be able to use the users capabilities.
Base on on this, this and this a few files new to be configured to give a user certain capabilities but the one assigning the capabilities to each user will be /etc/security/capability.conf
.
File example:
Compiling the following program it's possible to spawn a bash shell inside an environment that provides capabilities.
Inside the bash executed by the compiled ambient binary it's possible to observe the new capabilities (a regular user won't have any capability in the "current" section).
You can only add capabilities that are present in both the permitted and the inheritable sets.
The capability-aware binaries won't use the new capabilities given by the environment, however the capability dumb binaries will use them as they won't reject them. This makes capability-dumb binaries vulnerable inside a special environment that grant capabilities to binaries.
By default a service running as root will have assigned all the capabilities, and in some occasions this may be dangerous. Therefore, a service configuration file allows to specify the capabilities you want it to have, and the user that should execute the service to avoid running a service with unnecessary privileges:
By default Docker assigns a few capabilities to the containers. It's very easy to check which capabilities are these by running:
RootedCON is the most relevant cybersecurity event in Spain and one of the most important in Europe. With the mission of promoting technical knowledge, this congress is a boiling meeting point for technology and cybersecurity professionals in every discipline.
Capabilities are useful when you want to restrict your own processes after performing privileged operations (e.g. after setting up chroot and binding to a socket). However, they can be exploited by passing them malicious commands or arguments which are then run as root.
You can force capabilities upon programs using setcap
, and query these using getcap
:
The +ep
means you’re adding the capability (“-” would remove it) as Effective and Permitted.
To identify programs in a system or folder with capabilities:
In the following example the binary /usr/bin/python2.6
is found vulnerable to privesc:
Capabilities needed by tcpdump
to allow any user to sniff packets:
From the docs: Note that one can assign empty capability sets to a program file, and thus it is possible to create a set-user-ID-root program that changes the effective and saved set-user-ID of the process that executes the program to 0, but confers no capabilities to that process. Or, simply put, if you have a binary that:
is not owned by root
has no SUID
/SGID
bits set
has empty capabilities set (e.g.: getcap myelf
returns myelf =ep
)
then that binary will run as root.
CAP_SYS_ADMIN
is a highly potent Linux capability, often equated to a near-root level due to its extensive administrative privileges, such as mounting devices or manipulating kernel features. While indispensable for containers simulating entire systems, CAP_SYS_ADMIN
poses significant security challenges, especially in containerized environments, due to its potential for privilege escalation and system compromise. Therefore, its usage warrants stringent security assessments and cautious management, with a strong preference for dropping this capability in application-specific containers to adhere to the principle of least privilege and minimize the attack surface.
Example with binary
Using python you can mount a modified passwd file on top of the real passwd file:
And finally mount the modified passwd
file on /etc/passwd
:
And you will be able to su
as root using password "password".
Example with environment (Docker breakout)
You can check the enabled capabilities inside the docker container using:
Inside the previous output you can see that the SYS_ADMIN capability is enabled.
Mount
This allows the docker container to mount the host disk and access it freely:
Full access
In the previous method we managed to access the docker host disk. In case you find that the host is running an ssh server, you could create a user inside the docker host disk and access it via SSH:
This means that you can escape the container by injecting a shellcode inside some process running inside the host. To access processes running inside the host the container needs to be run at least with --pid=host
.
CAP_SYS_PTRACE
grants the ability to use debugging and system call tracing functionalities provided by ptrace(2)
and cross-memory attach calls like process_vm_readv(2)
and process_vm_writev(2)
. Although powerful for diagnostic and monitoring purposes, if CAP_SYS_PTRACE
is enabled without restrictive measures like a seccomp filter on ptrace(2)
, it can significantly undermine system security. Specifically, it can be exploited to circumvent other security restrictions, notably those imposed by seccomp, as demonstrated by proofs of concept (PoC) like this one.
Example with binary (python)
Example with binary (gdb)
gdb
with ptrace
capability:
Create a shellcode with msfvenom to inject in memory via gdb
Debug a root process with gdb ad copy-paste the previously generated gdb lines:
Example with environment (Docker breakout) - Another gdb Abuse
If GDB is installed (or you can install it with apk add gdb
or apt install gdb
for example) you can debug a process from the host and make it call the system
function. (This technique also requires the capability SYS_ADMIN
).
You won’t be able to see the output of the command executed but it will be executed by that process (so get a rev shell).
If you get the error "No symbol "system" in current context." check the previous example loading a shellcode in a program via gdb.
Example with environment (Docker breakout) - Shellcode Injection
You can check the enabled capabilities inside the docker container using:
List processes running in the host ps -eaf
Get the architecture uname -m
Find a shellcode for the architecture (https://www.exploit-db.com/exploits/41128)
Find a program to inject the shellcode into a process memory (https://github.com/0x00pf/0x00sec_code/blob/master/mem_inject/infect.c)
Modify the shellcode inside the program and compile it gcc inject.c -o inject
Inject it and grab your shell: ./inject 299; nc 172.17.0.1 5600
CAP_SYS_MODULE
empowers a process to load and unload kernel modules (init_module(2)
, finit_module(2)
and delete_module(2)
system calls), offering direct access to the kernel's core operations. This capability presents critical security risks, as it enables privilege escalation and total system compromise by allowing modifications to the kernel, thereby bypassing all Linux security mechanisms, including Linux Security Modules and container isolation. This means that you can insert/remove kernel modules in/from the kernel of the host machine.
Example with binary
In the following example the binary python
has this capability.
By default, modprobe
command checks for dependency list and map files in the directory /lib/modules/$(uname -r)
.
In order to abuse this, lets create a fake lib/modules folder:
Then compile the kernel module you can find 2 examples below and copy it to this folder:
Finally, execute the needed python code to load this kernel module:
Example 2 with binary
In the following example the binary kmod
has this capability.
Which means that it's possible to use the command insmod
to insert a kernel module. Follow the example below to get a reverse shell abusing this privilege.
Example with environment (Docker breakout)
You can check the enabled capabilities inside the docker container using:
Inside the previous output you can see that the SYS_MODULE capability is enabled.
Create the kernel module that is going to execute a reverse shell and the Makefile to compile it:
The blank char before each make word in the Makefile must be a tab, not spaces!
Execute make
to compile it.
Finally, start nc
inside a shell and load the module from another one and you will capture the shell in the nc process:
The code of this technique was copied from the laboratory of "Abusing SYS_MODULE Capability" from https://www.pentesteracademy.com/
Another example of this technique can be found in https://www.cyberark.com/resources/threat-research-blog/how-i-hacked-play-with-docker-and-remotely-ran-code-on-the-host
CAP_DAC_READ_SEARCH enables a process to bypass permissions for reading files and for reading and executing directories. Its primary use is for file searching or reading purposes. However, it also allows a process to use the open_by_handle_at(2)
function, which can access any file, including those outside the process's mount namespace. The handle used in open_by_handle_at(2)
is supposed to be a non-transparent identifier obtained through name_to_handle_at(2)
, but it can include sensitive information like inode numbers that are vulnerable to tampering. The potential for exploitation of this capability, particularly in the context of Docker containers, was demonstrated by Sebastian Krahmer with the shocker exploit, as analyzed here. This means that you can bypass can bypass file read permission checks and directory read/execute permission checks.
Example with binary
The binary will be able to read any file. So, if a file like tar has this capability it will be able to read the shadow file:
Example with binary2
In this case lets suppose that python
binary has this capability. In order to list root files you could do:
And in order to read a file you could do:
Example in Environment (Docker breakout)
You can check the enabled capabilities inside the docker container using:
Inside the previous output you can see that the DAC_READ_SEARCH capability is enabled. As a result, the container can debug processes.
You can learn how the following exploiting works in https://medium.com/@fun_cuddles/docker-breakout-exploit-analysis-a274fff0e6b3 but in resume CAP_DAC_READ_SEARCH not only allows us to traverse the file system without permission checks, but also explicitly removes any checks to open_by_handle_at(2) and could allow our process to sensitive files opened by other processes.
The original exploit that abuse this permissions to read files from the host can be found here: http://stealth.openwall.net/xSports/shocker.c, the following is a modified version that allows you to indicate the file you want to read as first argument and dump it in a file.
The exploit needs to find a pointer to something mounted on the host. The original exploit used the file /.dockerinit and this modified version uses /etc/hostname. If the exploit isn't working maybe you need to set a different file. To find a file that is mounted in the host just execute mount command:
The code of this technique was copied from the laboratory of "Abusing DAC_READ_SEARCH Capability" from https://www.pentesteracademy.com/
RootedCON is the most relevant cybersecurity event in Spain and one of the most important in Europe. With the mission of promoting technical knowledge, this congress is a boiling meeting point for technology and cybersecurity professionals in every discipline.
This mean that you can bypass write permission checks on any file, so you can write any file.
There are a lot of files you can overwrite to escalate privileges, you can get ideas from here.
Example with binary
In this example vim has this capability, so you can modify any file like passwd, sudoers or shadow:
Example with binary 2
In this example python
binary will have this capability. You could use python to override any file:
Example with environment + CAP_DAC_READ_SEARCH (Docker breakout)
You can check the enabled capabilities inside the docker container using:
First of all read the previous section that abuses DAC_READ_SEARCH capability to read arbitrary files of the host and compile the exploit. Then, compile the following version of the shocker exploit that will allow you to write arbitrary files inside the hosts filesystem:
In order to scape the docker container you could download the files /etc/shadow
and /etc/passwd
from the host, add to them a new user, and use shocker_write
to overwrite them. Then, access via ssh.
The code of this technique was copied from the laboratory of "Abusing DAC_OVERRIDE Capability" from https://www.pentesteracademy.com
This means that it's possible to change the ownership of any file.
Example with binary
Lets suppose the python
binary has this capability, you can change the owner of the shadow file, change root password, and escalate privileges:
Or with the ruby
binary having this capability:
This means that it's possible to change the permission of any file.
Example with binary
If python has this capability you can modify the permissions of the shadow file, change root password, and escalate privileges:
This means that it's possible to set the effective user id of the created process.
Example with binary
If python has this capability, you can very easily abuse it to escalate privileges to root:
Another way:
This means that it's possible to set the effective group id of the created process.
There are a lot of files you can overwrite to escalate privileges, you can get ideas from here.
Example with binary
In this case you should look for interesting files that a group can read because you can impersonate any group:
Once you have find a file you can abuse (via reading or writing) to escalate privileges you can get a shell impersonating the interesting group with:
In this case the group shadow was impersonated so you can read the file /etc/shadow
:
If docker is installed you could impersonate the docker group and abuse it to communicate with the docker socket and escalate privileges.
This means that it's possible to set capabilities on files and processes
Example with binary
If python has this capability, you can very easily abuse it to escalate privileges to root:
Note that if you set a new capability to the binary with CAP_SETFCAP, you will lose this cap.
Once you have SETUID capability you can go to its section to see how to escalate privileges.
Example with environment (Docker breakout)
By default the capability CAP_SETFCAP is given to the proccess inside the container in Docker. You can check that doing something like:
This capability allow to give any other capability to binaries, so we could think about escaping from the container abusing any of the other capability breakouts mentioned in this page. However, if you try to give for example the capabilities CAP_SYS_ADMIN and CAP_SYS_PTRACE to the gdb binary, you will find that you can give them, but the binary won’t be able to execute after this:
From the docs: Permitted: This is a limiting superset for the effective capabilities that the thread may assume. It is also a limiting superset for the capabilities that may be added to the inheri‐table set by a thread that does not have the CAP_SETPCAP capability in its effective set. It looks like the Permitted capabilities limit the ones that can be used. However, Docker also grants the CAP_SETPCAP by default, so you might be able to set new capabilities inside the inheritables ones. However, in the documentation of this cap: CAP_SETPCAP : […] add any capability from the calling thread’s bounding set to its inheritable set. It looks like we can only add to the inheritable set capabilities from the bounding set. Which means that we cannot put new capabilities like CAP_SYS_ADMIN or CAP_SYS_PTRACE in the inherit set to escalate privileges.
CAP_SYS_RAWIO provides a number of sensitive operations including access to /dev/mem
, /dev/kmem
or /proc/kcore
, modify mmap_min_addr
, access ioperm(2)
and iopl(2)
system calls, and various disk commands. The FIBMAP ioctl(2)
is also enabled via this capability, which has caused issues in the past. As per the man page, this also allows the holder to descriptively perform a range of device-specific operations on other devices
.
This can be useful for privilege escalation and Docker breakout.
This means that it's possible to kill any process.
Example with binary
Lets suppose the python
binary has this capability. If you could also modify some service or socket configuration (or any configuration file related to a service) file, you could backdoor it, and then kill the process related to that service and wait for the new configuration file to be executed with your backdoor.
Privesc with kill
If you have kill capabilities and there is a node program running as root (or as a different user)you could probably send it the signal SIGUSR1 and make it open the node debugger to where you can connect.
RootedCON is the most relevant cybersecurity event in Spain and one of the most important in Europe. With the mission of promoting technical knowledge, this congress is a boiling meeting point for technology and cybersecurity professionals in every discipline.
This means that it's possible to listen in any port (even in privileged ones). You cannot escalate privileges directly with this capability.
Example with binary
If python
has this capability it will be able to listen on any port and even connect from it to any other port (some services require connections from specific privileges ports)
CAP_NET_RAW capability permits processes to create RAW and PACKET sockets, enabling them to generate and send arbitrary network packets. This can lead to security risks in containerized environments, such as packet spoofing, traffic injection, and bypassing network access controls. Malicious actors could exploit this to interfere with container routing or compromise host network security, especially without adequate firewall protections. Additionally, CAP_NET_RAW is crucial for privileged containers to support operations like ping via RAW ICMP requests.
This means that it's possible to sniff traffic. You cannot escalate privileges directly with this capability.
Example with binary
If the binary tcpdump
has this capability you will be able to use it to capture network information.
Note that if the environment is giving this capability you could also use tcpdump
to sniff traffic.
Example with binary 2
The following example is python2
code that can be useful to intercept traffic of the "lo" (localhost) interface. The code is from the lab "The Basics: CAP-NET_BIND + NET_RAW" from https://attackdefense.pentesteracademy.com/
CAP_NET_ADMIN capability grants the holder the power to alter network configurations, including firewall settings, routing tables, socket permissions, and network interface settings within the exposed network namespaces. It also enables turning on promiscuous mode on network interfaces, allowing for packet sniffing across namespaces.
Example with binary
Lets suppose that the python binary has these capabilities.
This means that it's possible modify inode attributes. You cannot escalate privileges directly with this capability.
Example with binary
If you find that a file is immutable and python has this capability, you can remove the immutable attribute and make the file modifiable:
Note that usually this immutable attribute is set and remove using:
CAP_SYS_CHROOT enables the execution of the chroot(2)
system call, which can potentially allow for the escape from chroot(2)
environments through known vulnerabilities:
CAP_SYS_BOOT not only allows the execution of the reboot(2)
system call for system restarts, including specific commands like LINUX_REBOOT_CMD_RESTART2
tailored for certain hardware platforms, but it also enables the use of kexec_load(2)
and, from Linux 3.17 onwards, kexec_file_load(2)
for loading new or signed crash kernels respectively.
CAP_SYSLOG was separated from the broader CAP_SYS_ADMIN in Linux 2.6.37, specifically granting the ability to use the syslog(2)
call. This capability enables the viewing of kernel addresses via /proc
and similar interfaces when the kptr_restrict
setting is at 1, which controls the exposure of kernel addresses. Since Linux 2.6.39, the default for kptr_restrict
is 0, meaning kernel addresses are exposed, though many distributions set this to 1 (hide addresses except from uid 0) or 2 (always hide addresses) for security reasons.
Additionally, CAP_SYSLOG allows accessing dmesg
output when dmesg_restrict
is set to 1. Despite these changes, CAP_SYS_ADMIN retains the ability to perform syslog
operations due to historical precedents.
CAP_MKNOD extends the functionality of the mknod
system call beyond creating regular files, FIFOs (named pipes), or UNIX domain sockets. It specifically allows for the creation of special files, which include:
S_IFCHR: Character special files, which are devices like terminals.
S_IFBLK: Block special files, which are devices like disks.
This capability is essential for processes that require the ability to create device files, facilitating direct hardware interaction through character or block devices.
It is a default docker capability (https://github.com/moby/moby/blob/master/oci/caps/defaults.go#L6-L19).
This capability permits to do privilege escalations (through full disk read) on the host, under these conditions:
Have initial access to the host (Unprivileged).
Have initial access to the container (Privileged (EUID 0), and effective CAP_MKNOD
).
Host and container should share the same user namespace.
Steps to Create and Access a Block Device in a Container:
On the Host as a Standard User:
Determine your current user ID with id
, e.g., uid=1000(standarduser)
.
Identify the target device, for example, /dev/sdb
.
Inside the Container as root
:
Back on the Host:
This approach allows the standard user to access and potentially read data from /dev/sdb
through the container, exploiting shared user namespaces and permissions set on the device.
CAP_SETPCAP enables a process to alter the capability sets of another process, allowing for the addition or removal of capabilities from the effective, inheritable, and permitted sets. However, a process can only modify capabilities that it possesses in its own permitted set, ensuring it cannot elevate another process's privileges beyond its own. Recent kernel updates have tightened these rules, restricting CAP_SETPCAP
to only diminish the capabilities within its own or its descendants' permitted sets, aiming to mitigate security risks. Usage requires having CAP_SETPCAP
in the effective set and the target capabilities in the permitted set, utilizing capset()
for modifications. This summarizes the core function and limitations of CAP_SETPCAP
, highlighting its role in privilege management and security enhancement.
CAP_SETPCAP
is a Linux capability that allows a process to modify the capability sets of another process. It grants the ability to add or remove capabilities from the effective, inheritable, and permitted capability sets of other processes. However, there are certain restrictions on how this capability can be used.
A process with CAP_SETPCAP
can only grant or remove capabilities that are in its own permitted capability set. In other words, a process cannot grant a capability to another process if it does not have that capability itself. This restriction prevents a process from elevating the privileges of another process beyond its own level of privilege.
Moreover, in recent kernel versions, the CAP_SETPCAP
capability has been further restricted. It no longer allows a process to arbitrarily modify the capability sets of other processes. Instead, it only allows a process to lower the capabilities in its own permitted capability set or the permitted capability set of its descendants. This change was introduced to reduce potential security risks associated with the capability.
To use CAP_SETPCAP
effectively, you need to have the capability in your effective capability set and the target capabilities in your permitted capability set. You can then use the capset()
system call to modify the capability sets of other processes.
In summary, CAP_SETPCAP
allows a process to modify the capability sets of other processes, but it cannot grant capabilities that it doesn't have itself. Additionally, due to security concerns, its functionality has been limited in recent kernel versions to only allow reducing capabilities in its own permitted capability set or the permitted capability sets of its descendants.
Most of these examples were taken from some labs of https://attackdefense.pentesteracademy.com/, so if you want to practice this privesc techniques I recommend these labs.
Other references:
RootedCON is the most relevant cybersecurity event in Spain and one of the most important in Europe. With the mission of promoting technical knowledge, this congress is a boiling meeting point for technology and cybersecurity professionals in every discipline.
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)