|
| 1 | +/* |
| 2 | + * This file is part of WebGoat, an Open Web Application Security Project utility. For details, please see http://www.owasp.org/ |
| 3 | + * |
| 4 | + * Copyright (c) 2002 - 2019 Bruce Mayhew |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or modify it under the terms of the |
| 7 | + * GNU General Public License as published by the Free Software Foundation; either version 2 of the |
| 8 | + * License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without |
| 11 | + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License along with this program; if |
| 15 | + * not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 16 | + * 02111-1307, USA. |
| 17 | + * |
| 18 | + * Getting Source ============== |
| 19 | + * |
| 20 | + * Source for this application is maintained at https://github.com/WebGoat/WebGoat, a repository for free software projects. |
| 21 | + */ |
| 22 | + |
| 23 | +package org.owasp.webgoat.lessons.sqlinjection.advanced; |
| 24 | + |
| 25 | +import java.sql.*; |
| 26 | +import java.sql.PreparedStatement; |
| 27 | +import lombok.extern.slf4j.Slf4j; |
| 28 | +import org.owasp.webgoat.container.LessonDataSource; |
| 29 | +import org.owasp.webgoat.container.assignments.AssignmentEndpoint; |
| 30 | +import org.owasp.webgoat.container.assignments.AssignmentHints; |
| 31 | +import org.owasp.webgoat.container.assignments.AttackResult; |
| 32 | +import org.springframework.util.StringUtils; |
| 33 | +import org.springframework.web.bind.annotation.PutMapping; |
| 34 | +import org.springframework.web.bind.annotation.RequestParam; |
| 35 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 36 | +import org.springframework.web.bind.annotation.RestController; |
| 37 | + |
| 38 | +/** |
| 39 | + * @author nbaars |
| 40 | + * @since 4/8/17. |
| 41 | + */ |
| 42 | +@RestController |
| 43 | +@AssignmentHints( |
| 44 | + value = {"SqlInjectionChallenge1", "SqlInjectionChallenge2", "SqlInjectionChallenge3"}) |
| 45 | +@Slf4j |
| 46 | +public class SqlInjectionChallenge extends AssignmentEndpoint { |
| 47 | + |
| 48 | + private final LessonDataSource dataSource; |
| 49 | + |
| 50 | + public SqlInjectionChallenge(LessonDataSource dataSource) { |
| 51 | + this.dataSource = dataSource; |
| 52 | + } |
| 53 | + |
| 54 | + @PutMapping("/SqlInjectionAdvanced/challenge") |
| 55 | + // assignment path is bounded to class so we use different http method :-) |
| 56 | + @ResponseBody |
| 57 | + public AttackResult registerNewUser( |
| 58 | + @RequestParam String username_reg, |
| 59 | + @RequestParam String email_reg, |
| 60 | + @RequestParam String password_reg) |
| 61 | + throws Exception { |
| 62 | + AttackResult attackResult = checkArguments(username_reg, email_reg, password_reg); |
| 63 | + |
| 64 | + if (attackResult == null) { |
| 65 | + |
| 66 | + try (Connection connection = dataSource.getConnection()) { |
| 67 | + String checkUserQuery = |
| 68 | + "select userid from sql_challenge_users where userid = ?"; |
| 69 | + PreparedStatement statement = connection.prepareStatement(checkUserQuery); |
| 70 | + statement.setString(1, username_reg); |
| 71 | + |
| 72 | + ResultSet resultSet = statement.execute(); |
| 73 | + if (resultSet.next()) { |
| 74 | + if (username_reg.contains("tom'")) { |
| 75 | + attackResult = success(this).feedback("user.exists").build(); |
| 76 | + } else { |
| 77 | + attackResult = failed(this).feedback("user.exists").feedbackArgs(username_reg).build(); |
| 78 | + } |
| 79 | + } else { |
| 80 | + PreparedStatement preparedStatement = |
| 81 | + connection.prepareStatement("INSERT INTO sql_challenge_users VALUES (?, ?, ?)"); |
| 82 | + preparedStatement.setString(1, username_reg); |
| 83 | + preparedStatement.setString(2, email_reg); |
| 84 | + preparedStatement.setString(3, password_reg); |
| 85 | + preparedStatement.execute(); |
| 86 | + attackResult = success(this).feedback("user.created").feedbackArgs(username_reg).build(); |
| 87 | + } |
| 88 | + } catch (SQLException e) { |
| 89 | + attackResult = failed(this).output("Something went wrong").build(); |
| 90 | + } |
| 91 | + } |
| 92 | + return attackResult; |
| 93 | + } |
| 94 | + |
| 95 | + private AttackResult checkArguments(String username_reg, String email_reg, String password_reg) { |
| 96 | + if (StringUtils.isEmpty(username_reg) |
| 97 | + || StringUtils.isEmpty(email_reg) |
| 98 | + || StringUtils.isEmpty(password_reg)) { |
| 99 | + return failed(this).feedback("input.invalid").build(); |
| 100 | + } |
| 101 | + if (username_reg.length() > 250 || email_reg.length() > 30 || password_reg.length() > 30) { |
| 102 | + return failed(this).feedback("input.invalid").build(); |
| 103 | + } |
| 104 | + return null; |
| 105 | + } |
| 106 | +} |
0 commit comments