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 본문, 헤더, 쿠키, 파일 업로드.
2
데이터 흐름 추적
입력을 코드 전체에서 추적하세요. 정제되나요?
3
싱크 식별
Where queries are executed: execute(), query()
4
신뢰 경계 확인
쿼리에 사용되는 저장된 데이터를 주의하세요.