3 - Reverse Shells in Windows
Topics
Introduction to the Windows Shells
Windows Permissions
Reverse Shells in Windows
SeImpersonatePrivilege Exploitation
On Cross Compilation
Windows Services
Weak Service Permissions
Unquoted Service Path
DLL Hijacking
Always Install Elevated
Files with Sensitive Data
Windows Hashes
Stored Credentials and the Windows Vault
Scheduled Task
Critical Registry Paths
Useful Tools
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
The attacker sets up a listener on their machine to wait for incoming connections.
The target machine executes a payload that connects back to the attacker’s listener.
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
The target machine executes a payload that binds a shell to a specific port.
The attacker connects to this port using a client (e.g.,
netcat
).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
Using
certutil
Certutil is a Windows tool that can download files:Using PowerShell PowerShell (iwr) can download files via HTTP:
Using FTP Windows supports FTP commands:
Using SMB Shares Files can be transferred using network shares:
Third-Party Tools
Netcat Set up a listener on the attacker machine:
Send the file from the target:
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
Download
ncat.exe
on the Target Machinencat.exe
(a lightweight implementation of Netcat) is required for creating the reverse shell.Set Up a Listener on the Attacker Machine The attacker machine must have a listener ready to receive the reverse connection.
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.
Download the Script on the Attacker Machine
Configure the Script Add the line to specify the attacker's IP and port:
Host the Script on an HTTP Server Use Python to serve the script for download:
Start listening mode with Netcat on attacker machine
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.
Generate the Payload Using Python Use the following Python script to create a Base64-encoded reverse shell payload:
Execute the Payload on the Target Machine Copy the generated Base64 string and execute it directly in PowerShell:
Disclaimer
❗ Never use tools and techniques on real IP addresses, hosts or networks without proper authorization!❗
Last updated