Hacktricks-skills reverse-shell-defense
Security education skill for understanding reverse shell techniques to improve defensive security posture. Use this skill when users ask about reverse shells, shell connections, or need to understand how attackers establish remote access for authorized security testing, incident response, or defensive hardening. This skill focuses on detection, prevention, and authorized testing only.
git clone https://github.com/abelrguezr/hacktricks-skills
skills/generic-hacking/reverse-shells/linux/SKILL.MDReverse Shell Defense & Detection
⚠️ AUTHORIZED USE ONLY: This skill is for security professionals conducting authorized penetration testing, incident response, or defensive security work. Never use these techniques on systems you don't own or have explicit written permission to test.
Overview
Reverse shells are a common technique in security assessments where a compromised system initiates a connection back to an attacker-controlled listener. Understanding these techniques is essential for:
- Detection: Identifying malicious activity in logs and network traffic
- Prevention: Hardening systems against shell-based attacks
- Incident Response: Recognizing and responding to active compromises
- Authorized Testing: Conducting legitimate penetration tests
Common Shell Types
Bash/Shell-Based Shells
Bash provides built-in TCP capabilities via
/dev/tcp/ that can establish reverse connections:
Detection indicators:
- Process spawning with
in command line/dev/tcp/ - Unusual file descriptor redirections (
,>&
)0>&1 - Commands containing IP addresses and port numbers
Prevention:
- Monitor for
usage in process monitoring/dev/tcp/ - Restrict bash capabilities in containers
- Use AppArmor/SELinux to limit network access
Netcat-Based Shells
Netcat (
nc) is frequently used for reverse shells due to its simplicity:
Detection indicators:
ornc -e
flags in process listingsnc -c- Netcat connecting to external IPs on unusual ports
- FIFO pipes (
) combined with netcatmkfifo
Prevention:
- Remove or restrict netcat access
- Monitor for FIFO creation in
/tmp - Network segmentation to limit egress
Python-Based Shells
Python reverse shells use socket and subprocess modules:
Detection indicators:
- Python processes with
,socket
,pty
importssubprocess - Environment variables like
,RHOSTRPORT
calls in process memoryos.dup2()
Prevention:
- Restrict Python execution in sensitive environments
- Monitor for socket creation in Python processes
- Use application whitelisting
Other Languages
Reverse shells can be crafted in many languages:
- Perl: Uses Socket module
- Ruby: Uses TCPSocket
- PHP: Uses fsockopen or proc_open
- Java: Uses Runtime.exec() with bash
- Node.js: Uses net and child_process modules
- Go: Uses net and os/exec packages
Detection Strategies
Process Monitoring
Watch for these patterns in process listings:
# Check for suspicious shell processes ps aux | grep -E 'nc|netcat|bash.*tcp|python.*socket' # Look for unusual parent-child relationships pstree -p | grep -E 'bash|sh|python' # Monitor for /dev/tcp usage lsof -i | grep ESTABLISHED
Network Monitoring
Egress filtering indicators:
- Outbound connections to unknown IPs
- Connections on non-standard ports (4444, 5555, 1337, etc.)
- Long-lived TCP connections from shell processes
Tools:
for packet capturetcpdump
/netstat
for connection monitoringss- IDS/IPS systems for pattern detection
Log Analysis
Key log sources:
- authentication attempts/var/log/auth.log
- system events/var/log/syslog- Application logs - unusual command execution
- Network logs - connection attempts
Search patterns:
- Commands containing IP addresses
- Shell spawning with redirections
- Unusual user activity
Prevention Techniques
System Hardening
-
Principle of Least Privilege
- Run services with minimal permissions
- Use dedicated service accounts
- Restrict sudo access
-
Network Controls
- Implement egress filtering
- Use firewalls to limit outbound connections
- Segment networks to contain breaches
-
Application Security
- Validate and sanitize all inputs
- Use parameterized queries
- Implement proper authentication
Container Security
# Docker security best practices security_opt: - no-new-privileges:true - seccomp:unconfined read_only: true cap_drop: - ALL cap_add: - NET_BIND_SERVICE
Monitoring & Alerting
Set up alerts for:
- New outbound connections to unknown destinations
- Shell processes spawned by web services
- Unusual command execution patterns
- File creation in
with execute permissions/tmp
Incident Response
When You Detect a Reverse Shell
-
Contain
- Isolate the affected system from the network
- Preserve evidence (memory dump, logs)
- Document the timeline
-
Analyze
- Identify the entry point
- Determine what data was accessed
- Check for persistence mechanisms
-
Eradicate
- Remove malicious files and processes
- Close unauthorized access points
- Patch the vulnerability
-
Recover
- Restore from clean backups
- Implement additional security controls
- Monitor for re-infection
Forensic Collection
# Capture network connections netstat -tulpn > /evidence/connections.txt # List all processes with full details ps auxww > /evidence/processes.txt # Capture open files lsof > /evidence/open_files.txt # Memory dump (if available) # dd if=/dev/mem of=/evidence/memory.dump bs=1M
Authorized Testing
Setting Up a Test Environment
Requirements:
- Isolated network (no production systems)
- Written authorization
- Clear scope and rules of engagement
- Proper documentation
Safe Testing Practices:
- Use dedicated test systems
- Document all activities
- Have an exit strategy
- Report findings responsibly
Common Test Scenarios
-
Web Application Testing
- Test for RCE vulnerabilities
- Verify WAF effectiveness
- Check input validation
-
Network Testing
- Test firewall rules
- Verify egress filtering
- Check IDS/IPS detection
-
Endpoint Testing
- Test EDR detection
- Verify application whitelisting
- Check process monitoring
References
- OWASP Testing Guide
- SANS Incident Response
- MITRE ATT&CK - Command and Control
- NIST Cybersecurity Framework
Legal & Ethical Considerations
Always ensure:
- Written authorization before any testing
- Clear scope and boundaries
- Compliance with applicable laws
- Responsible disclosure of findings
- Protection of sensitive data
When in doubt:
- Consult with legal counsel
- Get explicit written permission
- Document all activities
- Report findings through proper channels