2

This is a bit of an odd one. For my uni final project I'm trying to develop a vulnerable web application as an educational tool. One of the vulnerabilities I want to implement is an SQL vulnerability where the user could perform an SQL injection through a 'product search' page on the site.

The problem is that somewhere along the way the inputs seem to be getting sanitised automatically which means I am unable to perform an injection attack. I made a test record of just a single quote (') and this is returned when a single quote in put into the search. If the input was not sanitised it would return an error, right? I'm thinking this could be a feature of the software I'm using that I'll need to disable or use an older version, or I've accidentally set it up in such a way that this is happening. If anyone knows why this might be happening, any help would be massively appreciated! :)

I have a database set up in MySQL Community Server 8.0.13 and a simple application made using JSPs. I have included source code below.

The 'Product Search' page:

<%@page import="java.sql.Connection"%>
<%@page import="databaseManagement.DBConnection"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Search Our Products</title>
</head>
<body>
    <h1>Search for a product</h1>
    <form method="post" action="ProductSearch">

        Search: <input type="text" name="Search"> <br> 
        <input type="submit" value="Go"> 

        <%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%><br>

                <table align="left" border="1">
                        <tr>
                            <th>ID</th>
                            <th>Name</th>
                            <th>Description</th>
                            <th>Price</th>
                        </tr>
                        <c:forEach var="product" items="${r1}">


                            <tr bgcolor="">

                                <td>${product.id}</td>
                                <td>${product.name}</td>
                                <td>${product.description}</td>
                                <td>${product.price}</td>
                            </tr>
                        </c:forEach>
                    </table>
    </form>
</body>
</html>

The java servlet:

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import databaseManagement.DBConnection;
import databaseManagement.Product;

@WebServlet("/ProductSearch")
public class ProductSearch extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public ProductSearch() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub

        String searchTerm = request.getParameter("Search");
        searchTerm = "%" + searchTerm + "%";
        ArrayList<Product> ab = new ArrayList();

        try {

            String sql1 = "select * from products where name like ?;";
            DBConnection db = new DBConnection();
            Connection con = db.getConnection();

            PreparedStatement ps = con.prepareStatement(sql1);

            ps.setString(1, searchTerm);

            ResultSet rs = ps.executeQuery();
            while (rs.next()) {

                Product b = new Product();
                b.setId(rs.getInt("id"));
                b.setName(rs.getString("name"));
                b.setDescription(rs.getString("description"));
                b.setPrice(rs.getString("price"));
                ab.add(b);
            }

            request.setAttribute("r1", ab);
            request.getRequestDispatcher("productSearch.jsp").forward(request, response);

        }

        catch (Exception s2) {
            s2.printStackTrace();
        }

    }
}
2
  • 1
    The PreparedStatements setString Method does the job you dont want it to do. Simply concat searchTerm to your SQL-String Commented Feb 14, 2019 at 12:24
  • 1
    As you are using prepared statements with a placeholder (?) and ps.setString(...), then you are safe from someone putting a single quote into this parameter. If you want to demonstrate an SQL vulnerability, build the query string yourself, for example String sql = "select * from products where name like '" + searchTerm + "'"; Commented Feb 14, 2019 at 12:25

2 Answers 2

1

Fragment of doPost method that should be placed to accept SQL injection:

....
try {

        String sql1 = "select * from products where name like '"+searchTerm+"';";
        DBConnection db = new DBConnection();
        Connection con = db.getConnection();

        Statement ps = con.createStatement();

        ResultSet rs = ps.executeQuery();
        while (rs.next()) {

....

PreparedStatement prevents SQL Injection, so use Statement instead.

Sign up to request clarification or add additional context in comments.

Comments

1

It's PreparedStatement who's sanitising your inputs. Instead of setting your parameters, simply concat them to the sql.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.