# Command Injection

#### What is Command Injection?

* **Command Injection** is a type of security vulnerability where an attacker can execute arbitrary commands on a host operating system via a vulnerable application.
* It typically occurs in web applications that improperly process user input to execute system-level commands.

#### How Does Command Injection Occur?

* **Vulnerable Input Processing:** When user input is not properly validated or sanitized, and directly used in a system command.
* **Commonly Affected Languages/Technologies:** PHP, Java, Python, Shell scripts, SQL databases with system command functions.

#### Examples of Command Injection

* A web form that takes a user input (like a username) and uses it in a system command without proper validation.
* URL parameters or HTTP headers that are concatenated directly into system commands.

#### Basic Example

* Unsafe code in PHP: `shell_exec('ping ' . $_GET['ip']);`
* In this example, if an attacker inputs `8.8.8.8; rm -rf /`, it could lead to the execution of a harmful command (`rm -rf /`).

#### Impact

* **Data Breach:** Unauthorized access to or theft of data.
* **System Compromise:** Gain control over the host system.
* **Denial of Service:** Disrupt services by executing resource-intensive commands.

#### Prevention and Mitigation

* **Input Validation:** Strictly validate user inputs for expected formats.
* **Use Safe APIs:** Use language-specific safe APIs that abstract command execution.
* **Escaping Inputs:** Properly escape special characters in user inputs.
* **Least Privilege Principle:** Run applications with the least privileges necessary.
* **Regular Security Auditing:** Regularly audit code and applications for vulnerabilities.

#### Testing for Command Injection

* **Manual Testing:** Attempt to input system commands through the application.
* **Automated Tools:** Use security scanning tools like OWASP ZAP, Burp Suite.
* **Code Review:** Regularly review code for potential vulnerabilities.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://itrp19-notes.gitbook.io/notes/reference/hacking/web-app-pentesting/command-injection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
