A05—Injection
>Control Description
>Prevention & Mitigation Strategies
- 1.The preferred option is to use a safe API, which avoids using the interpreter entirely, provides a parameterized interface, or migrates to Object Relational Mapping Tools (ORMs).
- 2.Use positive server-side input validation. This is not a complete defense as many applications require special characters, such as text areas or APIs for mobile applications.
- 3.For any residual dynamic queries, escape special characters using the specific escape syntax for that interpreter.
>Attack Scenarios
An application uses untrusted data in the construction of the following vulnerable SQL call: ``` String query = "SELECT * FROM accounts WHERE custID='" + request.getParameter("id") + "'"; ``` An attacker modifies the 'id' parameter value in their browser to send: `' OR '1'='1`. For example: ``` http://example.com/app/accountView?id=' OR '1'='1 ``` This changes the meaning of the query to return all records from the accounts table. More dangerous attacks could modify or delete data or even invoke stored procedures.
An application's blind trust in frameworks may result in queries that are still vulnerable. For example, Hibernate Query Language (HQL): ``` Query HQLQuery = session.createQuery("FROM accounts WHERE custID='" + request.getParameter("id") + "'"); ``` An attacker supplies: `' OR custID IS NOT NULL OR custID='`. This bypasses the filter and returns all accounts. While HQL has fewer dangerous functions than raw SQL, it still allows unauthorized data access when user input is concatenated into queries.
An application passes user input directly to an OS command: ``` String cmd = "nslookup " + request.getParameter("domain"); Runtime.getRuntime().exec(cmd); ``` An attacker supplies `example.com; cat /etc/passwd` to execute arbitrary commands on the server.
>Related CWEs
>References
- •OWASP Proactive Controls: Secure Database Access
- •OWASP ASVS: V5 Input Validation and Encoding
- •OWASP Testing Guide: SQL Injection,
- •Command Injection
- •ORM Injection
- •OWASP Cheat Sheet: Injection Prevention
- •OWASP Cheat Sheet: SQL Injection Prevention
- •OWASP Cheat Sheet: Injection Prevention in Java
- •OWASP Cheat Sheet: Query Parameterization
- •OWASP Automated Threats to Web Applications – OAT-014
- •PortSwigger: Server-side template injection
- •Awesome Fuzzing: a list of fuzzing resources
Ask AI
Configure your API key to use AI features.