深度分析Struts 2 任意文件上传漏洞 CVE-2023-50164
2024-1-23 09:35:33 Author: www.freebuf.com(查看原文) 阅读量:10 收藏

freeBuf

主站

分类

漏洞 工具 极客 Web安全 系统安全 网络安全 无线安全 设备/客户端安全 数据安全 安全管理 企业安全 工控安全

特色

头条 人物志 活动 视频 观点 招聘 报告 资讯 区块链安全 标准与合规 容器安全 公开课

官方公众号企业安全新浪微博

FreeBuf.COM网络安全行业门户,每日发布专业的安全资讯、技术剖析。

FreeBuf+小程序

FreeBuf+小程序

这是一个文件上传的逻辑漏洞,造成了容易文件上传可以getshell。

0x00漏洞信息

影响范围
Struts 2.0.0-2.3.37
Strust 2.5.0-2.5.32
Strust 6.0.0-6.3.0
这个漏洞需要使用struts2 来实现上传action,如果实现的上传方法没有加任何的Filter过滤或者WAF这些,那么就很可能可以被利用。

0x01 环境搭建

如果根据网上不完全的教程去搭建环境,坑很多。
其中一种:在官网下载了6.3.0的strstu2包,再其中的struts-STRUTS_6_3_0\apps\showcase\src\main\java\org\apache\struts2\showcase项目里面去修改,建议熟悉框架的师傅直接这样做,不熟悉的可以用下面的第二种方式。
ActionSupport

表示它是一个Struts2的Action。
该Action包含了三个属性:upload表示上传的文件,fileFileName表示上传的文件名,fileContentType表示上传的文件类型。
1.写对应的FileUploadAction,对应的Action代码
image
这里按照FileUploadAction来写一个Action

public class Upload extends ActionSupport {
    private File upload;
    private String uploadFileName;
    private String uploadContentType;

    // Custom upload logic
    public String execute() throws Exception {
        if (uploadFileName != null) {
            try {
                // Specify the directory where files will be uploaded
                String uploadDirectory = System.getProperty("user.dir") + "/uploads/";

                // Create the destination file
                File destFile = new File(uploadDirectory, uploadFileName);

                // Copy the uploaded file to the destination
                FileUtils.copyFile(upload, destFile);

                // Add message to reflect the exact upload path on the frontend
                addActionMessage("File uploaded successfully to " + destFile.getAbsolutePath());

                return SUCCESS;
            } catch (Exception e) {
                addActionError(e.getMessage());
                e.printStackTrace();
                return ERROR;
            }
        } else {
            return INPUT;
        }
     }

    // Getters and setters
    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

}

2.在/resource/struts-fileupload.xml里面添加上action对应到result的jsp界面代码
image

3.在webapp/WEB-INF/fileupload/DemoUpload.jsp里面写入访问action返回的前端和调用这些
image4.最后在web.xml上要注册写好,但是这里改动加上就会报错,不知道怎么改
image

一般s2框架需要有4个地方改动才能完成映射

新建一个项目,一步步配置,做好4步。
image

创建一个maven项目,然后在Modules里面新建一个web选择路径到我们创建的webapp目
image然后在resource这里创建一个struts的xml,这样目录就出来了,再去写上传Action的代码

实在不会的可以去下一


文章来源: https://www.freebuf.com/vuls/389426.html
如有侵权请联系:admin#unsafe.sh