MyBatis Plus<=3.5.6 存在SQL注入漏洞(CVE-2024-35548)

漏洞描述

MyBatis Plus 是 MyBatis 的增强工具,用于简化数据库开发,提高开发效率。

受影响版本中,由于 UpdateWrapper 类未对用户可控的参数进行过滤导致存在SQL注入漏洞,攻击者可基于布尔盲注窃取数据库敏感信息。

影响范围

mybatis-plus@(-∞, 3.5.6]

com.baomidou:mybatis-plus-core@(-∞, 3.5.6]

以上信息来自:OSCS社区

前置知识

Mybatis-Plus(mbp) 提供 Wrapper 极大的减少大家的写SQL 的时间,Wrapper 类封装了大量的逻辑方法 eq , gt , lt等,使得操作数据库的代码进一步简洁。

在代码中有更新数据的需求,mbp 提供了 UpdateWrapper 类简化更新的操作。

eq 方法用于指定查询条件

代码示例:

@Slf4j
@RestController
public class TestDemo {
    @Autowired
    private UserMapper usermapper;
    @GetMapping("/test")
    public String updateAgeByName() {
        User user = new User();
        user.setAge(18);

        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("name", "Yu9");

        usermapper.update(user,updateWrapper);
        return "Update success!";
    }
}

漏洞分析

执行后可以看到最终执行的sql语句

UPDATE user SET age=? WHERE (name = ?)

可以看到使用eq 方法用于指定查询条件,字段值做了预处理、字段名没有

若该值用户可控,是否可产生sql注入漏洞呢?

编写demo,让该参数前端可控

经过测试,确实是可行的,会产生sql注入漏洞

漏洞复现

payload:

1=1) and updatexml(1,concat(0x7e,database()),0) and (1

日志中可以看到经过构造的参数会拼接到sql语句中

参考

https://www.oscs1024.com/hd/MPS-mg7u-bw9p

https://github.com/baomidou/mybatis-plus/issues/6167