Module 10: Linux Privilege Escalation

Attacking the Users

Becoming a User

In Linux, users are identifed by UID and GID.

Inspecting the /etc/passwd file

offsec@linux01:~$ cat /etc/passwd | grep offsec
...
offsec:x:1000:1000:offsec,,,:/home/offsec:/bin/bash

Passwd file fields explained

  • Login Name: "offsec" - Indicates the username used for login.

  • Encrypted Password: "x" - This field typically contains the hashed version of the user's password. In this case, the value x means that the entire password hash is contained in the /etc/shadow file (more on that shortly).

  • UID: "1000" - Aside from the root user that has always a UID of 0, Linux starts counting regular user IDs from 1000. This value is also called real user ID.

  • GID: "1000" - Represents the user's specific Group ID.

  • Comment: "offsec,,," - This field generally contains a description about the user, often simply repeating username information.

  • Home Folder: "/home/offsec" - Describes the user's home directory prompted upon login.

  • Login Shell: "/bin/bash" - Indicates the initial directory from which the user is prompted to login.

Checking our user's sudo permissions

All privileged operations using sudo and sui are logged by default to /var/log/auth.log

Inspecting sudo related events

Unlike Ubuntu/Debian, Linux distributions such as CentOS and Fedora store authentication logs in /var/log/secure.

Blocked attempt to read /etc/shadow

Reviewing the log entry for the blocked attempt

aureport can be used to efficiently inspect very detailed logs generated by the audit daemon.

Enabling aureport detailed keylogging

Running aureport to fetch user's keylogs

Searching for the UID identified

A more accurate and controlled approach would be to inspect the auditd logs.

Inspecting audit TTY events

The data can be decoded via xxd.

Decoding the hex-encoded commands

Backdooring a User

User config files tend to reside in the home directory and shouldn't be editable by other users. Two specific configuration files are responsible for executing aliases and bash functions (.bashrc) and setting environmental variables (.profile).

Weak .bashrc permissions discovered

"Backdooring" (PoC) bob's .bashrc

Triggering the "backdoor" with a new login

Now from a defender's perspective, we can enable auditing rule to detect these. We can use auditctl to watch configuration files for any write and attribute change operations.

Configuring audit rules for privilege escalation detection

Verifying the audit rule

Audit rules configured through auditctl will not be persistent across reboots. To make them permanent, rules have to be added to the /etc/audit/rules.d/audit.rules file.

Inspecting the auditd rule report

To enhance analysis, the aureport tool supports the -i option that interprets user IDs and translates them into usernames.

The auid value is assigned every time a user logs in and is unchanged for the duration of that session.

Attacking the System

Abusing System Programs

Effective UID/GID was introduced which represents the actual value being checked when performing sensitive operations. Set-UID (SUID) allows programs to execute as a separate effective user.

Revealing the SUID flag on the passwd binary

In this case, with the Set-UID flag on the user owner permission set, it's stating that this binary should be run as the user owner. In this case, root.

Configuring root-monitoring audit rules

The two rules above log any activity of processes, either on x86 or x64 architectures, with effective UIDs equal to zero (root) that are also invoking the execve system call, which is ultimately responsible for executing programs throughout a shell.

Inspecting root shell activity

Extra Mile I

Extend the audit_key_search.py script to extract and print out the euid field. Once done, print an extra warning if the euid is zero and the auid is a standard user.

Weak Permissions

Linux File Permission Table for /etc/passwd

FORMAT
OWNER
GROUP
OTHER

symbolic

rw-

r--

r--

binary

110

100

100

octal

6

4

4

If you find scripts, cronjobs, etc. with write permissions to everyone then that's a no-no.

Audit rule to monitor all files in a folder

Analyzing the cronjob script modification from audit logs

The audit rules provided in this module should be taken as a baseline. Further rules should be customized and tailored depending on the environment being defended.

Monitoring /etc/shadow with audit watch rule

Analyzing the /etc/shadow audit logs

Extra Mile II

Run the audit_key_search.py file to search for password file dumping events. After inspecting the audit logs we might notice an auid value of 4294967295. This string corresponds to a specific numeric value in POSIX. What value does it represent?

Last updated