Summary
A command injection vulnerability in pymetasploit3's console.run_module_with_output() allows attackers to inject newline characters into module options such as RHOSTS, breaking the intended command structure.
When untrusted values are passed into these options, the Metasploit console can be tricked into executing additional, unintended commands, which may result in arbitrary command execution and manipulation of Metasploit sessions.
Affected context
This vulnerability affects applications and automation tooling that use pymetasploit3 (through version 1.0.6) to interact programmatically with a Metasploit console.
It is relevant where:
- User-influenced or external data is mapped into Metasploit module options (e.g.,
RHOSTS,LHOST, or other string fields). - Those option values are incorporated into commands sent to the Metasploit console without robust escaping or validation.
- The Metasploit console interprets newline characters as separators between distinct commands.
In such setups, misuse of the pymetasploit3 API or insufficient sanitization of module parameters can expose the command interface to injection.
Exploit preconditions
- Attack surface: The pymetasploit3-driven integration point (e.g., web UI, API, orchestration tool, or script) that accepts input and passes it into
console.run_module_with_output(). - Required access: Ability to supply or influence module options used by pymetasploit3 (for example, specifying
RHOSTSor other fields in a UI or API). - Trigger point: Invocation of
console.run_module_with_output()with a crafted value containing newline characters (\nor equivalent) that gets embedded in the command string. - Why exploitation is possible: The function constructs commands for the Metasploit console in a way that does not neutralize newline characters or other control delimiters. As a result, injected newlines terminate the intended command and start additional arbitrary commands that the Metasploit console executes.
Why this happens
The core issue is improper neutralization of special elements within commands sent to an interpreter, aligning with CWE-77: Improper Neutralization of Special Elements used in a Command ('Command Injection').
pymetasploit3's console.run_module_with_output() builds command strings that are sent to the Metasploit console, but it fails to sanitize or encode newline characters and related control characters in module option values.
Because the Metasploit console treats newlines as command separators, an attacker-controlled option value containing newlines can break out of the intended command structure and inject additional console commands.
Conceptually, this represents the general anti-pattern of concatenating untrusted data into command-like strings without strict normalization, encoding, or whitelisting, particularly when interacting with interactive shells or interpreters.
Potential impact
- Execution of unintended Metasploit console commands under the privileges of the Metasploit process.
- Manipulation of Metasploit sessions (e.g., starting, stopping, or interacting with sessions in unauthorized ways).
- Modification of Metasploit configuration, workspaces, or database content (hosts, services, loot, etc.).
- Launching of additional exploit modules or payloads against arbitrary targets defined by the attacker.
- Potential pivot to underlying operating system commands if the Metasploit console is configured or extended to execute shell commands.
- Disruption of security testing workflows, leading to unreliable results or unintentional impact on systems in scope.
General mitigation guidance
Mitigation typically focuses on treating all module option values as untrusted data and enforcing strict encoding and validation before they are passed into Metasploit commands.
Non-version-specific defensive patterns include:
- Input normalization and encoding: Normalize and encode newline and other control characters in option values before embedding them into console commands. Disallow or transparently escape line breaks that could split commands.
- Strict validation and whitelisting: Apply input validation rules for module options, especially fields like
RHOSTS, ensuring they conform to expected formats (e.g., IP addresses, CIDR ranges, hostnames) and reject characters outside an approved set. - Separation of data and control: Where possible, use APIs or abstractions that treat parameters and commands separately rather than constructing raw command strings; prefer structured parameter passing over string concatenation.
- Least-privilege execution: Run the Metasploit console and any automation around it with the minimum necessary privileges, limiting the blast radius of a successful injection.
- Trust boundary tightening: Avoid passing direct user input into pymetasploit3; instead, introduce intermediate validation layers or templates that strictly constrain how and where user-supplied data appears in Metasploit commands.
- Secure defaults in wrappers and integrations: For any higher-level tools built on pymetasploit3, adopt secure defaults that strip or reject control characters in user-supplied module options.
How teams detect issues like this
Detection efforts can focus both on code-level patterns and runtime behavior:
- Static analysis and code review:
- Flag any use of
console.run_module_with_output()or similar APIs where arguments are built from concatenated strings that include user-influenced data. - Look for missing validation on module options, particularly lack of checks against newline characters or other control delimiters.
- Enforce linting or custom static rules that disallow unescaped
\nor\rin values destined for interpreter-like interfaces. - Security-focused code review:
- Examine integrations that bridge web/API inputs with Metasploit through pymetasploit3, checking that each field is constrained to a safe character set and expected formats.
- Check for generic "pass-through" endpoints that forward request parameters directly to module options.
- Runtime monitoring and logging:
- Log all commands sent to the Metasploit console through pymetasploit3 and monitor for suspicious patterns such as unexpected line breaks or multi-line command submissions originating from a single input.
- Use anomaly detection or rule-based alerting for sequences that indicate command chaining or unusual module invocations.
- Automated review tools:
- Integrate SAST or custom security linters into CI workflows to detect unsafe command construction patterns involving pymetasploit3.
- Apply policy-as-code tools to enforce guardrails on how external input can be used when interacting with command interpreters like Metasploit.

