示例 - 存在SQL注入漏洞的代码示例(JSON传参方式):

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%@ page import="java.sql.Connection" %>
    3. <%@ page import="java.sql.DriverManager" %>
    4. <%@ page import="java.sql.ResultSet" %>
    5. <%@ page import="java.util.HashMap" %>
    6. <%@ page import="java.util.Map" %>
    7. <%@ page import="com.alibaba.fastjson.JSON" %>
    8. <%@ page import="org.apache.commons.io.IOUtils" %>
    9. <%@ page import="com.alibaba.fastjson.JSONObject" %>
    10. <%
    11. // MYSQL sys_user示例表,测试时请先创建对应的数据库和表
    12. //
    13. // CREATE TABLE `sys_user` (
    14. // `id` int(9) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户ID',
    15. // `username` varchar(16) NOT NULL COMMENT '用户名',
    16. // `password` varchar(32) NOT NULL COMMENT '用户密码',
    17. // `user_avatar` varchar(255) DEFAULT NULL COMMENT '用户头像',
    18. // `register_time` datetime DEFAULT NULL COMMENT '注册时间',
    19. // PRIMARY KEY (`id`),
    20. // UNIQUE KEY `idx_sys_user_username` (`username`) USING BTREE
    21. // ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT='系统用户表'
    22. //
    23. // INSERT INTO `sys_user` VALUES ('1', 'admin', '123456', '/res/images/avatar/default.png', '2020-05-05 17:21:27'), ('2', 'test', '123456', '/res/images/avatar/default.png', '2020-05-06 18:27:10'), ('3', 'root', '123456', '/res/images/avatar/default.png', '2020-05-06 18:28:27'), ('4', 'user', '123456', '/res/images/avatar/default.png', '2020-05-06 18:31:34'), ('5', 'rasp', '123456', '/res/images/avatar/default.png', '2020-05-06 18:32:08');
    24. %>
    25. <%
    26. String contentType = request.getContentType();
    27. // 只接受JSON请求
    28. if (contentType != null && contentType.toLowerCase().contains("application/json")) {
    29. String content = IOUtils.toString(request.getInputStream());
    30. JSONObject json = JSON.parseObject(content);
    31. String username = json.getString("username");
    32. String password = json.getString("password");
    33. // 处理用户登陆逻辑
    34. if (username != null && password != null) {
    35. ResultSet rs = null;
    36. Connection connection = null;
    37. Map<String, Object> userInfo = new HashMap<String, Object>();
    38. try {
    39. Class.forName("com.mysql.jdbc.Driver");
    40. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/javaweb-bbs", "root", "root");
    41. String sql = "select * from sys_user where username = '" + username + "' and password = '" + password + "'";
    42. System.out.println(sql);
    43. rs = connection.prepareStatement(sql).executeQuery();
    44. while (rs.next()) {
    45. userInfo.put("id", rs.getString("id"));
    46. userInfo.put("username", rs.getString("username"));
    47. userInfo.put("password", rs.getString("password"));
    48. userInfo.put("user_avatar", rs.getString("user_avatar"));
    49. userInfo.put("register_time", rs.getDate("register_time"));
    50. }
    51. // 检查是否登陆成功
    52. if (userInfo.size() > 0) {
    53. // 设置用户登陆信息
    54. out.println(JSON.toJSONString(userInfo));
    55. } else {
    56. out.println("<script>alert('登陆失败,账号或密码错误!');history.back(-1)</script>");
    57. }
    58. } catch (Exception e) {
    59. e.printStackTrace();
    60. out.println("<script>alert('登陆失败,服务器异常!');history.back(-1)</script>");
    61. } finally {
    62. // 关闭数据库连接
    63. if (rs != null)
    64. rs.close();
    65. if (connection != null)
    66. connection.close();
    67. }
    68. }
    69. }
    70. %>

    如果应用系统本身通过JSON格式传参,传统的WAF可能无法识别,如果后端将参数进行SQL语句的拼接,则将会导致SQL注入漏洞。攻击者通过篡改JSON中对应参数的数据,达到SQL注入攻击的目的,如下图:
    4. 3. SQL注入 - JSON传参测试 - 图1
    Back