3 - Reverse Shells in Windows

Topics

  1. Introduction to the Windows Shells

  2. Windows Permissions

  3. Reverse Shells in Windows

  4. SeImpersonatePrivilege Exploitation

  5. On Cross Compilation

  6. Windows Services

  7. Weak Service Permissions

  8. Unquoted Service Path

  9. DLL Hijacking

  10. Always Install Elevated

  11. Files with Sensitive Data

  12. Windows Hashes

  13. Stored Credentials and the Windows Vault

  14. Scheduled Task

  15. Critical Registry Paths

  16. Useful Tools

  17. AMSI Bypass

What is a Shell?

A shell is an interface that allows users to execute commands on an operating system. In the context of security and exploitation, a shell refers to a command-line interface provided to an attacker, enabling them to execute commands on a compromised machine.


Bind and Reverse Shells

Reverse Shells

A reverse shell involves the target machine initiating a connection back to the attacker's system. The attacker then uses this connection to execute commands on the target machine.

How It Works

  1. The attacker sets up a listener on their machine to wait for incoming connections.

  2. The target machine executes a payload that connects back to the attacker’s listener.

  3. Once the connection is established, the attacker gains shell access to the target system.

Example on Windows

Using PowerShell to create a reverse shell:

Advantages of Reverse Shells

  • Firewall Evasion: Since the target initiates the connection, it often bypasses restrictive inbound firewall rules.

  • Ease of Use: Simplifies access when the attacker has a dynamic or NATed IP.


Bind Shells

A bind shell involves the target machine listening for an incoming connection from the attacker. The attacker then connects to the target machine to gain shell access.

How It Works

  1. The target machine executes a payload that binds a shell to a specific port.

  2. The attacker connects to this port using a client (e.g., netcat).

  3. Once connected, the attacker can execute commands on the target machine.

Example on Windows

Using PowerShell to create a bind shell:

Drawbacks of Bind Shells

  • Firewall Restrictions: Most firewalls block inbound connections to unexpected ports.

  • Network Exposure: Requires the attacker to know the target's IP and accessible port.

Why Reverse Shells Are Preferred

  • Firewall Evasion: Since reverse shells initiate outbound connections, they are more likely to bypass firewalls.

  • Dynamic Networks: The attacker can operate from behind NAT or a dynamic IP without needing direct access to the target's network.

  • Stealth: Outbound connections are less suspicious and harder to trace compared to open inbound ports.


File Transfer in Windows

File transfer is often necessary for exploitation, privilege escalation, or data exfiltration. Several methods can be used to transfer files on Windows systems.

if windows defender is active on the machine, the usage of certutil for the downloading of file can trigger an antivirus warning, disabling the download of the file, or utilize techniques for evading it.

Native Methods

  1. Using certutil Certutil is a Windows tool that can download files:

  2. Using PowerShell PowerShell (iwr) can download files via HTTP:

  3. Using FTP Windows supports FTP commands:

  4. Using SMB Shares Files can be transferred using network shares:

Third-Party Tools

  1. Netcat Set up a listener on the attacker machine:

    Send the file from the target:

  2. Python Simple HTTP Server On the attacker machine:

    On the target machine:

Spawning a Reverse Shell

cmd.exe

The cmd.exe reverse shell relies on utilities like ncat.exe to establish a connection. The process involves downloading the necessary binary, setting up a listener on the attacker machine, and initiating the reverse connection from the victim machine.

Steps

  1. Download ncat.exe on the Target Machine ncat.exe (a lightweight implementation of Netcat) is required for creating the reverse shell.

  2. Set Up a Listener on the Attacker Machine The attacker machine must have a listener ready to receive the reverse connection.

  3. Initiate the Reverse Shell from the Target Machine On the victim machine, execute the following command to connect back to the attacker and spawn a shell:


Powershell

Using Invoke-PowerShellTcp.ps1 Script

Invoke-PowerShellTcp.ps1 is a PowerShell script designed for reverse shells.

  1. Download the Script on the Attacker Machine

  2. Configure the Script Add the line to specify the attacker's IP and port:

  3. Host the Script on an HTTP Server Use Python to serve the script for download:

  4. Start listening mode with Netcat on attacker machine

  1. Execute the Script from the Target Machine

  • From CMD:

  • From PowerShell:


Using a Base64-Encoded Payload

An alternative method involves encoding a reverse shell script in Base64 and executing it directly.

  1. Generate the Payload Using Python Use the following Python script to create a Base64-encoded reverse shell payload:

  2. Execute the Payload on the Target Machine Copy the generated Base64 string and execute it directly in PowerShell:

Last updated