베타 Shoulder는 베타 버전입니다 — 결과가 가끔 잘못될 수 있습니다. 여러분의 피드백이 다음에 무엇을 고칠지 결정합니다. 피드백 공유
💻

Command Injection

Review child_process usage
🛡️ 3 개의 규칙이 이를 탐지합니다

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

+14 -6 go
  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)
  }
  
Command Injection via child_process CRITICAL

Use execFile/spawn with array arguments instead of exec with string commands

+5 -5 javascript
- 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');
    });
  });
  
OS Command Injection CRITICAL

Use subprocess.run with list arguments and shell=False

+1 -1 python
  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 .
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

신뢰 경계 확인

쿼리에 사용되는 저장된 데이터를 주의하세요.

🔍

코드베이스를 스캔하세요: Command Injection

Shoulder CLI는 전체 코드베이스에서 취약한 패턴을 찾아냅니다.