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.
オフィスのアシスタントに「report.pdf」という書類の印刷を頼む場面を想像してください。攻撃者が「report.pdf; whoami」というファイル名を渡すと、アシスタントはそのメモ全体をそのまま端末の窓口に提出してしまい、書類の印刷に加えて管理者バッジの読み取りまで実行してしまいます。
Web Application SecurityCWE-918.CWE-918CWE-918):Standard Common Weakness Enumeration classification for command-injection-rust.Defense-in-DepthアプリケーションがHTTPリクエストから診断用ホスト名やファイル名などの入力を直接受け取ります。
バックエンドが引数リストを使わず、生文字列の結合によってシェルコマンドを構築します。
攻撃者が ';' や '&&'、'|'、バッククォートなどのメタ文字(例: '127.0.0.1; id')を挿入して構文を脱出します。
OSシェルが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 を追加してください。