---
title: "2. Command injection operators"
date: 2024-12-05
draft: false
---

1. Attempt to append command through various injection methods. 
2. If command output changes from intended usual result, success. 

May not be true for advanced vulns. 
- may utilize various fuzzing methods / code reviews to identify potential cmd injection vulns. 
- iterate over payloads until success 

---
Module example: `Host Checker` functionality of a webapp. 
Enter an IP, returns ping results output. 
We can confidently guess that this functionality does the following on the backend: 
`ping $_USER_CONTROLLED_INPUT`

If input isn't sanitized we may be able to inject arbitrary commands. 

## Command injection operators 

To inject an additional command to the intended one, we may use any of the following operators:

| **Injection Operator** | **Injection Character** | **URL-Encoded Character** | **Executed Command**                       |
| ---------------------- | ----------------------- | ------------------------- | ------------------------------------------ |
| Semicolon              | `;`                     | `%3b`                     | Both                                       |
| New Line               | `\n`                    | `%0a`                     | Both                                       |
| Background             | `&`                     | `%26`                     | Both (second output generally shown first) |
| Pipe                   | `\|`                    | `%7c`                     | Both (only second output is shown)         |
| AND                    | `&&`                    | `%26%26`                  | Both (only if first succeeds)              |
| OR                     | `\|`                    | `%7c%7c`                  | Second (only if first fails)               |
| Sub-Shell              | ` `` `                  | `%60%60`                  | Both (Linux-only)                          |
| Sub-Shell              | `$()`                   | `%24%28%29`               | Both (Linux-only)                          |

We probably want `both` or `either` command logic. 
We would write out expected input (in the above example, an IP) followed by our injected command. 

There are a few unix-only operators that would work on linux and macos but not on Windows such as \`\` (two backticks) or with a subshell operator `$()`

All of these operators can be used for cmd inj regardless of webapp lang, framework, or backend server 
- php running on linux
- .net running on windows
- nodejs on macos 

The only exception may be a semicolon `;` which would not work if command is being executed with CMD (WOULD work with powershell) 


Such operators can be used for various injection types, like SQL injections, LDAP injections, XSS, SSRF, XML, etc. We have created a list of the most common operators that can be used for injections:

| **Injection Type**                      | **Operators**                                     |
| --------------------------------------- | ------------------------------------------------- |
| SQL Injection                           | `'` `,` `;` `--` `/* */`                          |
| Command Injection                       | `;` `&&`                                          |
| LDAP Injection                          | `*` `(` `)` `&` `\|`                              |
| XPath Injection                         | `'` `or` `and` `not` `substring` `concat` `count` |
| OS Command Injection                    | `;` `&` `\|`                                      |
| Code Injection                          | `'` `;` `--` `/* */` `$()` `${}` `#{}` `%{}` `^`  |
| Directory Traversal/File Path Traversal | `../` `..\\` `%00`                                |
| Object Injection                        | `;` `&` `\|`                                      |
| XQuery Injection                        | `'` `;` `--` `/* */`                              |
| Shellcode Injection                     | `\x` `\u` `%u` `%n`                               |
| Header Injection                        | `\n` `\r\n` `\t` `%0d` `%0a` `%09`                |

Keep in mind that this table is incomplete
---

In the example above with the hostchecking program, please note that IT IS VERY COMMON FOR DEVELOPERS TO ONLY IMPLEMENT FRONT-END INPUT VALIDATION. 
- VALIDATE this behavior if you receive an error but do not make any network requests. This implies the validation is done on the frontend. 
- Easily bypassed with burpsuite 

