Improper Neutralization of Special Elements used in an OS Command
User input is passed unsanitized to system shell commands, allowing attackers to execute arbitrary commands on the server.
普及度
Common
Found in many applications
影響度
Critical
Full server compromise
予防
Avoid shell
Use execFile, not exec
2 予防
2 予防
この脆弱性の修正方法
3 件の Shoulder 検出ルールに基づく OS Command Injection の予防策。
Command Injection via os/exec
CRITICAL
Use exec.Command with explicit arguments, never shell invocation
package main import ( "net/http" "os/exec" - ) - - func handler(w http.ResponseWriter, r *http.Request) { - cmd := r.URL.Query().Get("cmd") - // Vulnerable: shell invocation with user input - output, _ := exec.Command("sh", "-c", cmd).Output() + "regexp" + ) + + var safePattern = regexp.MustCompile(`^[a-zA-Z0-9._-]+$`) + + func handler(w http.ResponseWriter, r *http.Request) { + filename := r.URL.Query().Get("file") + // Validate input + if !safePattern.MatchString(filename) { + http.Error(w, "Invalid filename", http.StatusBadRequest) + return + } + // Safe: explicit command with validated argument + output, _ := exec.Command("cat", filename).Output() w.Write(output) }
JavaScript
すべて表示 JavaScript 詳細 →
Command Injection via child_process
CRITICAL
Use execFile/spawn with array arguments instead of exec with string commands
- const { exec } = require('child_process'); - - app.get('/convert', (req, res) => { - const filename = req.query.file; - exec(`convert ${filename} output.png`, (err, stdout) => { + const { execFile } = require('child_process'); + + app.get('/convert', (req, res) => { + const filename = req.query.file; + execFile('convert', [filename, 'output.png'], (err, stdout) => { res.send('Converted'); }); });
Python
すべて表示 Python 詳細 →
OS Command Injection
CRITICAL
Use subprocess.run with list arguments and shell=False
import subprocess from flask import request @app.route('/convert') def convert(): filename = request.args.get('file') - subprocess.run(f'convert {filename} output.png', shell=True) + subprocess.run(['convert', filename, 'output.png'], check=True) return 'Done'
3 検出
3 検出
コードの脆弱性を見つける
Shoulderを使用してコードのCommand Injectionパターンをスキャンしましょう。 3 ルール.
# Scan with Shoulder CLI npx @shoulderdev/cli trust --cwe=78 # Or scan entire project npx @shoulderdev/cli trust .
検出ルール (3)
🟨
Javascript
1 rules
🔷
Typescript
1 rules
4 警告サイン
4 警告サイン
コードレビューで注目すべき点
これらのパターンはCommand Injectionの潜在的な脆弱性を示しています。コードレビューとセキュリティ監査中に探してください。
user input flowing to os/exec command execution, enabling OS command injection
go-command-injection
user input flowing to shell command execution functions
javascript-command-injection
untrusted user input flowing into operating system command execution
functions without proper saniti
python-command-injection
5 コード監査
5 コード監査
手動レビューパターン
コードを手動でレビューする際は、これらの危険なパターンを探してください。
注意すべき危険信号
query = + 文字列連結execute(f"... or execute("..." +raw_query, rawQuery, executeRaw${ or #{ SQL 文字列の内部
6 専門家による分析
6 専門家による分析
セキュリティ専門家の思考法
セキュリティのプロがこの脆弱性をレビューする際に使う思考モデル。
1
エントリポイントをマッピング
URL パラメータ、POST ボディ、ヘッダー、Cookie、ファイルアップロード。
2
データフローを追跡
入力をコード内で追跡しましょう。サニタイズされていますか?
3
シンクを特定
Where queries are executed: execute(), query()
4
信頼境界を確認
クエリで使用される保存データに注意。