Skip to main content

SQL injection - Code

Description

Dynamic SQL statements are generated without the required data validation and without using parameterized statements or stored procedures.

Impact

Inject SQL statements with the possibility of obtain information about the database, as well as extracting information from it.

Recommendation

Perform database queries by means of parameterized statements or stored procedures.

Threat

Authenticated attacker from the Internet.

Expected Remediation Time

⌚ 30 minutes.

Score

Default score using CVSS 3.1. It may change depending on the context of the vulnerability.

Base

  • Attack vector: N
  • Attack complexity: L
  • Privileges required: L
  • User interaction: N
  • Scope: U
  • Confidentiality: N
  • Integrity: L
  • Availability: N

Temporal

  • Exploit code madurity: P
  • Remediation level: X
  • Report confidence: X

Result

  • Vector string: CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:N/I:L/A:N/E:P/RL:X/RC:X
  • Score:
    • Base: 4.3
    • Temporal: 4.1
  • Severity:
    • Base: Medium
    • Temporal: Medium

Code Examples

Compliant code

The SQL queries are used by parameterized queries using sanitized user data

static void getQuery(param) {
param = java.net.URLDecoder.decode(param, ""UTF-8"");
//Clean User parameters
cleanParam = getCleanValue(param)
//Do Query with a standard sentence
String sql = ""INSERT INTO users (username, password) VALUES ('foo','""+ cleanParam + ""')"";

try {
java.sql.Statement statement = org.owasp.benchmark.helpers.DatabaseHelper.getSqlStatement();
org.owasp.benchmark.helpers.DatabaseHelper.outputUpdateComplete(sql, response);
} catch (java.sql.SQLException e) {
...
);
}

Non compliant code

There are unvalidated SQL queries that use input data

static void getQuery(sql) {
try {
java.sql.Statement statement = org.owasp.benchmark.helpers.DatabaseHelper.getSqlStatement();

// Updating db model without filtering user inpu
org.owasp.benchmark.helpers.DatabaseHelper.outputUpdateComplete(sql, response);
} catch (java.sql.SQLException e) {
...
);
}

Requirements