Interactive Playground
Experiment with the vulnerable code and security rule below. Edit the code to see how the rule detects different vulnerability patterns.
pathfinder scan --ruleset python/PYTHON-LANG-SEC-014 --project .About This Rule
Understanding the vulnerability and how it is detected
A reverse shell is a type of backdoor in which the compromised host initiates an outbound network connection to an attacker-controlled server and then redirects its stdin, stdout, and stderr over that connection, giving the attacker an interactive shell session on the host.
Common Python reverse shell patterns include creating a socket connection to a remote IP and port, then either passing the socket file descriptor to subprocess (subprocess.call(["/bin/sh", "-i"]), or duplicating the file descriptor over stdin/stdout/stderr using os.dup2(), or executing the socket through exec()/eval() with __builtins__ access.
Finding this pattern in production code is a critical security incident indicator. It may represent active compromise of the codebase, a supply chain attack through a dependency, a malicious insider contribution, or a legitimate security testing tool that was accidentally included in production code.
Security Implications
Potential attack scenarios if this vulnerability is exploited
Complete Host Compromise
A reverse shell gives the attacker an interactive command line on the host with the privileges of the Python process. All data, credentials, certificates, and capabilities of the process are immediately accessible to the attacker.
Persistent Access
Unlike a one-shot command injection, a reverse shell provides an ongoing interactive session. The attacker can install additional backdoors, lateral movement tools, and persistence mechanisms during the session.
Firewall Bypass via Outbound Connection
Reverse shells initiate outbound connections from the compromised host to the attacker. Most network perimeters allow outbound connections, so this technique bypasses inbound firewall rules that would block a traditional bind shell.
Supply Chain Compromise Indicator
Reverse shell code found in a third-party dependency or unexpected location in the codebase may indicate a supply chain attack. This requires forensic analysis to determine when and how the code was introduced and which systems may have executed it.
How to Fix
Recommended remediation steps
- 1Immediately quarantine and investigate any codebase containing a reverse shell pattern; treat it as an active security incident.
- 2Audit git history and dependency lockfiles to determine when the reverse shell code was introduced and trace the source.
- 3Scan all deployed instances for active outbound connections to unexpected IP addresses and ports.
- 4Implement code review policies that flag any use of socket combined with subprocess or os.dup2() for review by the security team.
- 5Use software composition analysis (SCA) tools to scan third-party dependencies for malicious code patterns before installation.
Detection Scope
How Code Pathfinder analyzes your code for this vulnerability
This rule detects patterns combining socket module calls (socket.socket, socket.connect) with process execution (subprocess.call, os.dup2, exec) that match known Python reverse shell templates. The pattern is highly specific to malicious code and has very few legitimate uses in production application code.
Compliance & Standards
Industry frameworks and regulations that require detection of this vulnerability
References
External resources and documentation
Similar Rules
Explore related security rules for Python
Dangerous os.system() or os.popen() Call
os.system() and os.popen() execute shell commands via /bin/sh, enabling command injection when arguments contain untrusted input.
Dangerous subprocess Usage
subprocess calls detected. Ensure command arguments are not user-controlled to prevent OS command injection.
Socket Bound to All Interfaces (0.0.0.0)
Binding a socket to 0.0.0.0 exposes the service on all network interfaces, including public-facing ones. Bind to specific interfaces in production.
Frequently Asked Questions
Common questions about Python Reverse Shell Pattern Detected
New feature
Get these findings posted directly on your GitHub pull requests
The Python Reverse Shell Pattern Detected rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.