gpt4 book ai didi

java - 试图在 Java Web 应用程序中实现 SQL 漏洞。 Java + MySQL

转载 作者:行者123 更新时间:2023-11-29 02:10:13 26 4
gpt4 key购买 nike

这有点奇怪。对于我的大学期末项目,我正在尝试开发一个易受攻击的 Web 应用程序作为一种教育工具。我想要实现的漏洞之一是 SQL 漏洞,用户可以通过网站上的“产品搜索”页面执行 SQL 注入(inject)。

问题是输入似乎在某个地方被自动清理,这意味着我无法执行注入(inject)攻击。我只做了一个单引号(')的测试记录,当单引号进入搜索时返回。如果输入没有经过清理,它会返回一个错误,对吧?我在想这可能是我正在使用的软件的一项功能,我需要禁用或使用旧版本,或者我不小心以这种方式设置了它,以至于发生了这种情况。如果有人知道为什么会发生这种情况,将不胜感激任何帮助! :)

我在 MySQL Community Server 8.0.13 中设置了一个数据库,并使用 JSP 制作了一个简单的应用程序。我在下面包含了源代码。

“产品搜索”页面:

<%@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>

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();
}

}
}

最佳答案

应该放置以接受 SQL 注入(inject)的 doPost 方法片段:

....
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 可防止 SQL 注入(inject),因此请改用 Statement。

关于java - 试图在 Java Web 应用程序中实现 SQL 漏洞。 Java + MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54690299/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com