diff --git a/src/main/java/org/owasp/webgoat/lessons/sqlinjection/introduction/SqlInjectionLesson8.java b/src/main/java/org/owasp/webgoat/lessons/sqlinjection/introduction/SqlInjectionLesson8.java index fb417e8e3f5..cc20c9294fa 100644 --- a/src/main/java/org/owasp/webgoat/lessons/sqlinjection/introduction/SqlInjectionLesson8.java +++ b/src/main/java/org/owasp/webgoat/lessons/sqlinjection/introduction/SqlInjectionLesson8.java @@ -1,3 +1,4 @@ + /* * SPDX-FileCopyrightText: Copyright © 2016 WebGoat authors * SPDX-License-Identifier: GPL-2.0-or-later @@ -46,20 +47,16 @@ public AttackResult completed(@RequestParam String name, @RequestParam String au protected AttackResult injectableQueryConfidentiality(String name, String auth_tan) { StringBuilder output = new StringBuilder(); - String query = - "SELECT * FROM employees WHERE last_name = '" - + name - + "' AND auth_tan = '" - + auth_tan - + "'"; try (Connection connection = dataSource.getConnection()) { try { - Statement statement = - connection.createStatement( - ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); - log(connection, query); - ResultSet results = statement.executeQuery(query); + String query = "SELECT * FROM employees WHERE last_name = ? AND auth_tan = ?"; + PreparedStatement statement = connection.prepareStatement(query, + ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); + statement.setString(1, name); + statement.setString(2, auth_tan); + log(connection, query, name, auth_tan); + ResultSet results = statement.executeQuery(); if (results.getStatement() != null) { if (results.first()) { @@ -128,18 +125,18 @@ public static String generateTable(ResultSet results) throws SQLException { return (table.toString()); } - public static void log(Connection connection, String action) { - action = action.replace('\'', '"'); + public static void log(Connection connection, String query, String name, String auth_tan) { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = sdf.format(cal.getTime()); - String logQuery = - "INSERT INTO access_log (time, action) VALUES ('" + time + "', '" + action + "')"; + String logQuery = "INSERT INTO access_log (time, action) VALUES (?, ?)"; try { - Statement statement = connection.createStatement(TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE); - statement.executeUpdate(logQuery); + PreparedStatement statement = connection.prepareStatement(logQuery); + statement.setString(1, time); + statement.setString(2, query + " with params: " + name + ", " + auth_tan); + statement.executeUpdate(); } catch (SQLException e) { System.err.println(e.getMessage()); }