Injection Vulnerabilities Overview
Injection Vulnerabilities Overview
Injection vulnerabilities are among the most common and dangerous security flaws. They occur when an application sends untrusted data to an interpreter without proper validation or sanitization. This allows attackers to inject malicious code that can manipulate the interpreter into executing unintended commands or accessing data without proper authorization.Common types of injection vulnerabilities include SQL injection, NoSQL injection, OS command injection, LDAP injection, and template injection. These vulnerabilities can lead to data theft, data loss, data corruption, denial of service, or complete host takeover.
SQL Injection
SQL Injection
- Use parameterized queries or prepared statements
- Apply input validation and sanitization
- Use stored procedures
- Implement least privilege principles for database accounts
- Use an ORM (Object-Relational Mapping) framework that handles parameter sanitization
NoSQL Injection
NoSQL Injection
- Validate and sanitize all user inputs
- Use type checking
- Avoid using operators directly from user input
- Implement proper authentication and authorization
- Use MongoDB’s aggregation framework instead of direct queries when possible
OS Command Injection
OS Command Injection
- Avoid calling OS commands directly when possible
- Use safer library alternatives instead of shell commands
- If OS commands must be used, validate and sanitize all inputs
- Use allowlists for permitted characters or commands
- Implement proper privilege separation
- Use subprocess modules with arrays instead of string commands
LDAP Injection
LDAP Injection
- Validate and sanitize all user inputs
- Use LDAP-specific encoding functions to escape special characters
- Implement proper authentication and authorization
- Use bind variables where possible
- Apply the principle of least privilege for LDAP accounts
Template Injection
Template Injection
- Never allow users to modify or submit templates
- Use a template engine that automatically escapes output
- Apply context-specific escaping
- Sandbox template execution environments
- Minimize the use of dangerous template features (like eval or execute)
XML Injection
XML Injection
- Use XML libraries to create XML documents instead of string concatenation
- Apply proper XML encoding for special characters
- Validate XML against a strict schema
- Implement XML parser hardening
- Disable external entity processing
Code Injection
Code Injection
eval() in various programming languages. This vulnerability can lead to complete system compromise.To prevent Code Injection:- Avoid using dynamic code execution functions like
eval() - If dynamic code execution is necessary, implement strict input validation
- Use safer alternatives like expression evaluators or template engines
- Implement proper sandboxing and isolation
- Apply the principle of least privilege
HTTP Header Injection
HTTP Header Injection
- Validate and sanitize all user inputs used in HTTP headers
- Remove or encode CR and LF characters (\r, \n)
- Use framework functions that handle header encoding
- Implement proper output encoding
- Use allowlists for redirect URLs
Path Traversal Injection
Path Traversal Injection
- Validate and sanitize file paths
- Use allowlists for permitted files or extensions
- Convert relative paths to absolute paths and validate them
- Implement proper access controls
- Use file system permissions as an additional layer of defense
Email Header Injection
Email Header Injection
- Validate and sanitize all user inputs used in email headers
- Remove or encode CR and LF characters (\r, \n)
- Use email libraries or frameworks that handle header encoding
- Implement proper output encoding
- Use allowlists for email addresses
Format String Injection
Format String Injection
- Always use format specifiers (%s, %d, etc.) in format strings
- Never pass user input directly as a format string
- Validate and sanitize all user inputs
- Use safer alternatives to format string functions when possible
- Implement proper compiler protections
Log Injection
Log Injection
- Sanitize all user inputs before logging
- Remove or encode CR and LF characters (\r, \n)
- Use logging frameworks that handle encoding
- Implement proper output encoding
- Use structured logging formats like JSON
Server-Side Includes (SSI) Injection
Server-Side Includes (SSI) Injection
- Validate and sanitize all user inputs used in SSI directives
- Disable unnecessary SSI directives
- Use allowlists for permitted SSI directives
- Implement proper access controls
- Consider using alternative technologies to SSI
CRLF Injection
CRLF Injection
- Remove or encode CR and LF characters (\r, \n) in user inputs
- Use framework functions that handle header encoding
- Implement proper output encoding
- Validate and sanitize all user inputs
- Use allowlists for permitted characters
XPath Injection
XPath Injection
- Use parameterized XPath queries
- Validate and sanitize all user inputs
- Implement proper authentication and authorization
- Use XPath 2.0 or later, which supports parameterized queries
- Apply the principle of least privilege
JSON Injection
JSON Injection
- Use JSON serialization functions instead of string concatenation
- Validate and sanitize all user inputs
- Implement proper output encoding
- Use Content-Type headers with charset
- Consider using JSON schema validation
HTML Injection
HTML Injection
- Use proper HTML encoding for all user inputs
- Implement Content Security Policy (CSP)
- Use template engines that automatically escape output
- Validate and sanitize all user inputs
- Use allowlists for permitted HTML tags and attributes
CSS Injection
CSS Injection
- Validate and sanitize all user inputs used in CSS
- Use allowlists for permitted CSS properties and values
- Implement Content Security Policy (CSP)
- Use separate CSS files instead of inline styles
- Consider using CSS sanitization libraries
Shell Metacharacter Injection
Shell Metacharacter Injection
- Avoid calling shell commands directly
- Use safer alternatives like subprocess modules with arrays
- Validate and sanitize all user inputs
- Use allowlists for permitted characters
- Implement proper privilege separation
OGNL Injection
OGNL Injection
- Validate and sanitize all user inputs used in OGNL expressions
- Use allowlists for permitted expressions
- Implement proper access controls
- Keep frameworks up-to-date
- Consider using alternatives to OGNL