某租车系统Java代码审计之后台注入漏洞分析
字数 980 2025-08-15 21:31:15
租车系统Java代码审计:后台SQL注入漏洞分析
漏洞概述
本教学文档分析了一个基于Java开发的租车系统中存在的SQL注入漏洞。该漏洞位于后台管理系统的"删除外聘员工"功能模块中,由于未对用户输入进行过滤和参数化处理,导致攻击者可以构造恶意SQL语句执行任意数据库操作。
系统简介
- 系统类型:O2O租车服务平台
- 功能模块:车辆库管理、门店管理、员工管理、司机管理、订单管理、活动管理、评价管理、财务管理等
- 部署方式:基于Tomcat的Java Web应用,使用MySQL数据库
漏洞位置
漏洞存在于以下文件:
WebRoot/WEB-INF/lib/car-weishang-1.0.jar!/com/weishang/my/admin/DeleteAunt.class
WebRoot/WEB-INF/lib/car-weishang-1.0.jar!/com/weishang/my/service/ShopService.class
漏洞分析
1. 请求处理流程
- 入口点:
DeleteAunt.class中的doGet方法 - 参数获取:通过
request.getParameterValues("aunt_id")获取用户输入的参数 - 参数处理:将获取的数组参数拼接为逗号分隔的字符串
2. 漏洞代码分析
DeleteAunt.class关键代码
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
// ...省略部分代码...
String[] auntIds = request.getParameterValues("aunt_id");
String ids = "";
for (int i = 0; i < auntIds.length; ++i) {
ids = ids + auntIds[i] + ",";
}
ids = ids.substring(0, ids.length() - 1);
String flag = ss.deleteAunt(ids);
// ...省略部分代码...
}
ShopService.class关键代码
public String deleteAunt(String ids) {
String sql = "delete from aunt where aunt_id in (" + ids + ")";
int flag = this.jdbc.executeUpdate(sql);
this.jdbc.close();
return flag > 0 ? "ok" : "bad";
}
3. 漏洞成因
- 直接拼接SQL:将用户输入的
aunt_id参数直接拼接到SQL语句中 - 缺乏过滤:未对用户输入进行任何过滤或转义处理
- 未使用预编译:没有使用PreparedStatement进行参数化查询
漏洞利用
1. 利用条件
- 攻击者需要拥有后台访问权限(默认账号:adimin 默认密码:zft3285497)
- 能够访问删除外聘员工的功能接口
2. 利用方式
攻击者可以构造恶意的aunt_id参数,例如:
aunt_id=1) OR 1=1 --
这将导致执行以下SQL语句:
delete from aunt where aunt_id in (1) OR 1=1 -- )
从而删除整个aunt表中的数据。
修复建议
1. 参数化查询
使用PreparedStatement进行参数化查询:
public String deleteAunt(String ids) {
try {
String sql = "delete from aunt where aunt_id in (?)";
PreparedStatement pstmt = this.jdbc.getConnection().prepareStatement(sql);
pstmt.setString(1, ids);
int flag = pstmt.executeUpdate();
pstmt.close();
this.jdbc.close();
return flag > 0 ? "ok" : "bad";
} catch (SQLException e) {
e.printStackTrace();
return "bad";
}
}
2. 输入验证
对输入的aunt_id进行严格验证,确保只包含数字和逗号:
if (!ids.matches("^[0-9,]+$")) {
return "bad";
}
3. 最小权限原则
数据库连接应使用最小权限账户,避免使用高权限账户连接数据库。
4. 全局过滤
实现全局的SQL注入过滤器,对所有用户输入进行过滤。
总结
该漏洞是典型的SQL注入漏洞,由于开发人员未对用户输入进行有效过滤和参数化处理导致。修复时应优先考虑使用参数化查询(PreparedStatement),其次是对用户输入进行严格验证。这类漏洞的危害性较高,可能导致数据泄露、篡改或删除,应引起足够重视。