T09 · Insecure Skill Coding Practices
- Location
scripts/analyze_logs.py:150- Finding
Arbitrary Command Execution Through Malicious Log Filenames
- Content
View full analysis
/dev/null") return stdout, stderr if code != 0 else "" else: # 目录,找最新的日志文件 stdout, stderr, code = run_cmd(f"ls -t {log_path}*.log 2>/dev/null | head -3") if not stdout: # 尝试 .out 文件 stdout, stderr, code = run_cmd(f"ls -t {log_path}*.out 2>/dev/null | head -3") if stdout: files = stdout.split('\n')[:3] all_logs = [] for f in files: content, _, _ = run_cmd(f"tail -{lines//3} '{f}' 2>/dev/null") all_logs.append(content) return '\n'.join(all_logs), "" return "", f"No log files found in {log_path}" ``` ### Technical Analysis The `run_cmd` function executes command strings with `shell=True`. In `get_log_files`, filenames returned by `ls` are treated as trusted data and inserted directly into another shell command: ```python run_cmd(f"tail -{lines//3} '{f}' 2>/dev/null") ``` Single quotes around `f` do not provide adequate protection. A filename containing a single quote can terminate the quoted filename and introduce shell metacharacters and additional commands. Becau ...[truncated 2293 chars]- Remediation
View remediation
