• Java代码审计sql注入基础


    Sql审计方法

    手动找的话,可以直接找到sqlmapper.xml文件或者直接搜索 select、update、delete、insert “String sql=”等关键词,定位SQL xml配置文件。

    如果 sql 语句中有出现 $ 进行参数拼接,则存在SQL注入风险。

    当找到某个变量关键词有 SQL 注入风险时,可以再根据调用链找到该存在注入风险的业务逻辑代码,查看参数来源是否安全、是否有配置SQL危险参数过滤的过滤器,最终确认是否存在SQL注入。以下给出可能造成sql注入攻击的关键字,审计时可根据实际情况进项查找。

    JDBC注入

    1、以前,人们通过这种方式从数据库获取数据

    public User getUserById(String id) throws SQLException {
        Connection connection = JDBCTOOLS.getConnection();
            String sql = "select id,username from user where id=" + id;
            Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);

        resultSet.next();
        int userId = resultSet.getInt(1);
        String username = resultSet.getString(2);
        User user = new User(userId, username);
        return user;
    }

    预编译处理后的代码(修复后代码)

    String sql = "select id,username from user where id= ?"

    但是预编译只能处理查询参数,很多场景下仅仅使用预编译是不够的。

    2、like情况

    在使用模糊查询的场景中 :

    String sql = "select * from user where username like '%?%'";

    这种写法是无法进行预编译的,程序会报错。

    3、order by

    需要按照时间、id等信息进行排序的时候,也是无法使用预编译的。

    order by与like一样,要分两步走才可避免情况
    需要按照时间、id等信息进行排序的时候,也是无法使用预编译的, sort=123

    String sort = req.getParameter("sort");

    String sql = "select * from user order by ?";
    PreparedStatement preparedStatement = conn

  • 相关阅读:
    C语言中自定义类型讲解
    IC模拟版图工程师高薪进阶之路,三年实现年薪30w+
    设置Ubuntu 20.04的静态IP地址(wifi模式下)
    PyQt5 QLineEdit自动补全
    DC电源模块的短期过载能力
    Excel VSTO开发10 -自定义任务面板
    Matplotlib轮廓图
    理解case when then else end 的使用,基础概念,建表语句,用例讲解
    Redis与Python交互
    ElasticSearch总结
  • 原文地址:https://blog.csdn.net/m0_55772907/article/details/127785622