🐧Linux Capabilities Labs

🎯 Objective

Learn how to manage Linux capabilities using native tools, to understand how they provide fine-grained control over root privileges.


🧰 Prerequisites

  • Linux system (Ubuntu/Debian recommended)

  • Tools: libcap2-bin, capsh, getcap, setcap, ping, bash

  • Root or sudo access

Install required tools:

sudo apt update
sudo apt install libcap2-bin iputils-ping -y

πŸ”Ή Lab 1: View Capabilities on a Binary

Check where pingis installed first with which ping.

getcap /bin/ping

βœ… Expected Output:

βœ… cap_net_raw=ep Breakdown

cap_net_raw: This is a Linux capability that allows a process to:

  • Open raw sockets

  • Send and receive ICMP (ping), which is normally a privileged operation

  • e = Effective: The capability is active when the binary is run

  • p = Permitted: The capability is allowed for the binary

So, when you run /bin/ping, it has permission to use raw sockets without needing to be setuid root.


πŸ”Ή Lab 2: Remove Capability from a Binary

❌ This should fail with Operation not permitted message


πŸ”Ή Lab 3: Restore the Capability

βœ… This should work again


πŸ”Ή Lab 4: Drop Capabilities from a Shell (Using capsh)

First try if ping works with standard capabilities set:

Now try again but with dropping the cap_net_raw capability:

❌ Expected: Ping fails due to missing capability.


πŸ”Ή Lab 5: Run a Root Process with Minimal Capabilities

Step 1: Write a simple C program

Open up an editor with nano showuid.c and copy the following content into the editor:

Write the file using Ctrl-W and exit the editor using Ctrl-X.

Step 2: Compile and assign capability

βœ… showuid has only the cap_net_raw capability.


πŸ”Ή Lab 6: List of All Available Capabilities

Capability
Description

CAP_CHOWN

Make arbitrary changes to file UIDs and GIDs

CAP_NET_ADMIN

Perform network operations like modify routing tables

CAP_NET_BIND_SERVICE

Bind a socket to Internet domain privileged ports (port numbers less than 1024)

CAP_NET_RAW

use RAW and PACKET sockets

CAP_SETUID

Make arbitrary manipulations of process UIDs

CAP_SYS_ADMIN

Perform system admin operations like mount, swapon, sethostname or perform privileged syslog

CAP_SYS_BOOT

Use reboot

CAP_SYS_CHROOT

Use chroot

CAP_SYS_TIME

Set system clock

CAP_SYSLOG

Perform privileged syslog operations

🧠 Useful capabilities:

  • CAP_NET_RAW

  • CAP_NET_BIND_SERVICE

  • CAP_SYS_ADMIN (powerful, use cautiously)

If you want to query capabilities for a process, use this command

Execute the binary ./showuid from previous lab.

Then open a new shell and check for the process id:

With the corresponding process id check for effective capabilities of the process:

βœ… Expected output

You should mainly use this for root processes. Processes for non-root users usually do not have any capability set.


βœ… Wrap-Up

  • βœ… Used getcap, setcap, and capsh to inspect and modify capabilities

  • βœ… Demonstrated capability-based privilege separation

  • βœ… Reinforced the principle of least privilege


Last updated