flawopen.com/command-injection/Rust
Learn how to fix Command Injection (CWE-78) in Rust. Side-by-side vulnerable vs secure code examples for std::process::Command with isolated argument slices.
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.
Web Application SecurityCWE-918.CWE-918Defense-in-DepthLa aplicación acepta nombres de host de diagnóstico, nombres de archivo o parámetros directamente desde una solicitud HTTP.
El backend construye la cadena de comando shell concatenando texto directamente en lugar de usar un vector de argumentos seguro.
El atacante inyecta metacaracteres de shell como ';', '&&', '|' o comillas invertidas (ej. '127.0.0.1; id') para escapar del comando esperado.
El shell del sistema operativo ejecuta la instrucción inyectada con todos los permisos del proceso del servidor web.
// Spawning shell explicitly with format! string
use std::process::Command;
fn run_backup(folder: &str) {
// Attacker input: "data; whoami"
let cmd = format!("tar -czf backup.tar.gz {}", folder);
Command::new("sh")
.arg("-c")
.arg(&cmd)
.status()
.expect("failed");
}
// Calling binary directly with discrete .arg() calls
use std::process::Command;
fn run_backup(folder: &str) {
// folder is passed as a literal argument to tar, not shell
Command::new("tar")
.arg("-czf")
.arg("backup.tar.gz")
.arg(folder)
.status()
.expect("failed");
}
arg() or .args() slices.