How to Spawn an Interactive Shell Behind the Firewall - Payatu (2024)

As penetration testers/security researchers you often come across firewalls configured with egress/ingress filtering that makes it difficult to spawn a shell. In this blog, we will discuss and demonstrate the concept of “Forward Shells”, a method that can be used to evade firewall rules and spawn an interactive shell.

Table of Contents

What is a Shell?

To achieve control of a compromised system, an attacker usually aims to exploit a vulnerability that would award them with Remote Code Execution (RCE). The next logical step is to spawn a shell, which is basically an interactive TTY. With such access, the attacker may increase their privileges and gain influential control over the system, which may hamper the organization.

What does “Spawning a Shell” mean?

“Spawning a shell” simply means spawning an interactive TTY, by exploiting the RCE vulnerability, or gaining access to a protocol like SSH. Speaking specifically in terms of abusing RCE vulnerabilities, shells are traditionally spawned using two methods:

Spawning Reverse Shells

A Reverse Shell is an RCE exploit where the attacker gets the victim machine to connect and provides them access to an interactive TTY. This usually involves the attacker sending a command to the victim, which, when executed, will get the victim to connect to a specific port on the attacker’s machine and provide them access to a TTY.

How to Spawn an Interactive Shell Behind the Firewall - Payatu (1)

Here’s an example with netcat. On the right side, there’s Attacker’s kali machine, listening on port 9001. The Victim’s Ubuntu machine on the left connects to this port and gives the attacker access to the bash shell.

How to Spawn an Interactive Shell Behind the Firewall - Payatu (2)

Spawning Bind Shells

A Bind Shell is an RCE exploit where the attacker connects to the victim machine to get access to an interactive TTY. This usually involves the attacker sending a command to the victim, which, when executed, will open a port on the victim’s server. The attacker is greeted with an interactive TTY when they connect to the newly opened port.

How to Spawn an Interactive Shell Behind the Firewall - Payatu (3)

Here’s an example explained with netcat. On the left side is the Victim’s Ubuntu machine, listening on port 9001. The Attacker’s kali machine on the right connects to this port and gains access to the bash shell.

How to Spawn an Interactive Shell Behind the Firewall - Payatu (4)

What is a Forward Shell?

Sometimes spawning a shell via traditional methods is not easy. There can be egress/ingress firewall rules or iptables rules that can block an attacker’s attempt to spawn a shell. In such cases, an attacker may use a concept called a “Forward Shell.” Forward Shell is the brainchild of IppSec; you can find the script on his GitHub repository. Let us understand some other terminology before diving into how a forward shell works.

Pipes and Named Pipes

A pipe is a form of redirection, that sends the output of one command, program, or process to another command, program, or process. A named pipe, also known as a FIFO, is a file like a pipe but with a name. Commands, programs, and processes can use this name to read and write to and from the pipe. Named pipes can be created by using the mkfifo command.

TTY and PTY

A TTY (teletype) is a terminal. It’s a user interface that uses text to manage input and output. A PTY (pseudo teletype) is a software implementation that appears to other processes as a TTY, but it’s a program that communicates directly with a TTY, acting as a mediator between the terminal and the program.

How does Spawning an Interactive Forward Shell work?

The Forward Shell creates a named pipe and writes commands to it. The commands are then read from the pipe and executed, and the output is written to a file. A background thread then reads the output file, prints the output, and then clears the file. This does not require any persistent network connection, meaning we can spawn an interactive PTY circumventing a firewall that has egress/ingress filtering to block reverse/bind shells.

How to Spawn an Interactive Shell Behind the Firewall - Payatu (5)

Understanding the Script

The script, as it sits on Ippsec’s GitHub repository, is hard coded for exploiting shellshock vulnerabilities. Here’s how the entire thing works:

  1. The constructor __init__ defines the command string, which when run will set up named pipes on the server. The command string looks like this:

mkfifo {self.stdin}; tail -f {self.stdin} | /bin/sh 2>&1 > {self.stdout}”.

  1. self.stdin is the named pipe, created with mkfifo
  1. tail –f self.stdin | /bin/sh continuously reads the end of the named pipe and passes its contents to the sh shell to run
  1. 2>&1> self.stdout writes the output and erros produced by /bin/sh self.stdout, which is the output file.
  1. RunRawCmd function writes commands to self.stdin pipe, which is then run, and the output redirected to self.stdout
  1. WriteCmd function converts user supplied commands to base64 to avoid ambiguous characters and then passes the final string to RunRawCmd().
  1. ReadThread runs as a daemon which reads the command output from the self.stdout file, every 1.3s. This interval can be easily changed.
  1. The UpgradeShell function upgrades your web shell to a fully interactive PTY, using either python3, python, or script command, whichever is available.

I modified the script a little bit, so that it now:

  1. Reads and setups the request from a file
  1. Finds the “INJECT” keyword in the script and replaces it with user provided/script generated commands.

This allows us to use the forward-shell against both web shells, and RCE vulnerabilities. You can find my version of the script over here.

Using the Upgraded Script in a Real Scenario

In order to test the script and look at it from the victim’s point of view, we set up a ubuntu VM and deployed a simple PHP web shell on it. For the sake of demonstration, let’s assume that a firewall exists and blocks all bind/reverse shells on both TCP and UDP ports. Let us use the forward shell to spawn an interactive PTY:

How to Spawn an Interactive Shell Behind the Firewall - Payatu (6)

That timeout error occurs because the server does not respond after setting the named pipes. To avoid waiting and receiving a timeout anyway, we set the timeout to 0.1s to get an instant shell. Increasing the timeout for requests can resolve this error. This shell is a fully interactive PTY and allows us to do everything a regular shell would do. You can change directories, switch users, and even clear the screen.

How to Spawn an Interactive Shell Behind the Firewall - Payatu (7)

Payatu is a Research Focused, CERT-In impaneled Cybersecurity Consulting company specializing in security assessments of IoT product ecosystems, Web applications & Networks with a proven track record of securing applications and infrastructure for customers across 20+ countries.

How to Spawn an Interactive Shell Behind the Firewall - Payatu (2024)
Top Articles
High Red Blood Cell Count: Symptoms, Meaning, Causes
List of Popular Large Cap Stocks in India
Mickey Moniak Walk Up Song
Kostner Wingback Bed
Netronline Taxes
55Th And Kedzie Elite Staffing
Shoe Game Lit Svg
Uti Hvacr
The Potter Enterprise from Coudersport, Pennsylvania
Alpha Kenny Buddy - Songs, Events and Music Stats | Viberate.com
Magic Mike's Last Dance Showtimes Near Marcus Cedar Creek Cinema
Healing Guide Dragonflight 10.2.7 Wow Warring Dueling Guide
735 Reeds Avenue 737 & 739 Reeds Ave., Red Bluff, CA 96080 - MLS# 20240686 | CENTURY 21
6813472639
Webcentral Cuny
Transfer and Pay with Wells Fargo Online®
Csi Tv Series Wiki
Inter-Tech IM-2 Expander/SAMA IM01 Pro
Bank Of America Financial Center Irvington Photos
Craigslist Maui Garage Sale
Happy Life 365, Kelly Weekers | 9789021569444 | Boeken | bol
Kingdom Tattoo Ithaca Mi
Weldmotor Vehicle.com
Bento - A link in bio, but rich and beautiful.
Ticket To Paradise Showtimes Near Cinemark Mall Del Norte
Netspend Ssi Deposit Dates For 2022 November
Times Narcos Lied To You About What Really Happened - Grunge
Lbrands Login Aces
Jackass Golf Cart Gif
Sacramento Craigslist Cars And Trucks - By Owner
Pay Stub Portal
Motor Mounts
Publix Daily Soup Menu
Strange World Showtimes Near Regal Edwards West Covina
Breckie Hill Fapello
Nsu Occupational Therapy Prerequisites
About Us | SEIL
Gwu Apps
Elisabeth Shue breaks silence about her top-secret 'Cobra Kai' appearance
Kornerstone Funeral Tulia
Samantha Lyne Wikipedia
Craigslist Minneapolis Com
Vérificateur De Billet Loto-Québec
Arch Aplin Iii Felony
A rough Sunday for some of the NFL's best teams in 2023 led to the three biggest upsets: Analysis
Hello – Cornerstone Chapel
Bf273-11K-Cl
Mlb Hitting Streak Record Holder Crossword Clue
Ciara Rose Scalia-Hirschman
Read Love in Orbit - Chapter 2 - Page 974 | MangaBuddy
Zalog Forum
Gainswave Review Forum
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 6296

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.