OWASP Top 10 Security Lab

A03

Injection Attacks

Master the art of exploiting injection vulnerabilities

What are Injection Attacks?

Injection flaws, such as SQL, NoSQL, OS, and LDAP injection, occur when untrusted data is sent to an interpreter as part of a command or query. The attacker's hostile data can trick the interpreter into executing unintended commands or accessing data without proper authorization.

🎯 Common Injection Types

  • SQL Injection: Manipulating database queries through user input
  • Command Injection: Executing arbitrary operating system commands
  • LDAP Injection: Exploiting LDAP queries for authentication bypass
  • XPath Injection: Manipulating XML path language queries
  • NoSQL Injection: Exploiting NoSQL database queries
  • CRLF Injection: Header manipulation and response splitting

⚠️ Attack Impact

  • Complete database compromise and data exfiltration
  • Authentication bypass and privilege escalation
  • Remote code execution on the server
  • System takeover and lateral movement
  • Data corruption and service denial
  • Compliance violations and legal liability

🔍 Common Attack Vectors

# SQL Injection Examples ' OR '1'='1' -- # Authentication bypass '; DROP TABLE users; -- # Data destruction ' UNION SELECT password FROM admin_users -- # Data extraction # Command Injection Examples ; cat /etc/passwd # File disclosure | whoami # Command chaining && rm -rf / # Destructive commands # LDAP Injection Examples *)(cn=*))(|(cn=* # LDAP filter bypass admin)(&(password=* # Authentication bypass

🛡️ Prevention Strategies

  • Parameterized Queries: Use prepared statements with parameter binding
  • Input Validation: Whitelist allowed characters and patterns
  • Escape Output: Properly encode special characters
  • Least Privilege: Limit database and system permissions
  • WAF Implementation: Deploy Web Application Firewalls
  • Regular Testing: Automated security scanning and penetration testing
// Vulnerable Code (PHP) $query = "SELECT * FROM users WHERE username = '" . $_POST['username'] . "'"; $result = mysql_query($query); // Secure Code (PHP with PDO) $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?"); $stmt->execute([$_POST['username']]); $result = $stmt->fetchAll(); // Vulnerable Code (Python) os.system("ping " + user_input) // Secure Code (Python) subprocess.run(["ping", user_input], shell=False)