无日志访问shell文件

学习JAVA内存shell时,考古发现了n1nty大佬早在17年的一系列文章,其中讲到了tomcat在日志功能在默认配置下,避开各Container组件中的日志记录器中的log(方法,实现访问shell文件无日志产生的操作

继承Container的StandardEngine、StandardHost、StandardContext、StandardWrapper均实现了logAccess方法

1
2
3
4
5
6
7
8
9
10
11
12
public void logAccess(Request request, Response response, long time, boolean useDefault) {
boolean logged = false;
if (this.getAccessLog() != null) {
this.getAccessLog().log(request, response, time);
logged = true;
}

if (this.getParent() != null) {
this.getParent().logAccess(request, response, time, useDefault && !logged);
}

}

通过上述代码可知,日志记录操作是会以Wrapper->Context->Host->Engine层层往上的方式记录,然后存放到/logs目录下

看一下log(的代码org.apache.catalina.valves.AbstractAccessLogValve

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void log(Request request, Response response, long time) {
if (this.getState().isAvailable() && this.getEnabled() && this.logElements != null && (this.condition == null || null == request.getRequest().getAttribute(this.condition)) && (this.conditionIf == null || null != request.getRequest().getAttribute(this.conditionIf))) {
long start = request.getCoyoteRequest().getStartTime();
Date date = getDate(start + time);
CharArrayWriter result = (CharArrayWriter)this.charArrayWriters.pop();
if (result == null) {
result = new CharArrayWriter(128);
}

for(int i = 0; i < this.logElements.length; ++i) {
this.logElements[i].addElement(result, date, request, response, time);
}

this.log(result);
if (result.size() <= this.maxLogMessageBufferSize) {
result.reset();
this.charArrayWriters.push(result);
}

}
}

如果设置其condition或者conditionIf为一个特定的值,然后在之后访问shell的过程中,传入一个以该值为参数名的参数,上述代码的if判断为false,即可使该Container层不记录日志,利用时层层遍历修改即可

Java Instrumentation

java 内存shell大多都是以tomcat讲起的,而在实际项目上又遇到了很多不同的web容器(weblogic、spring、websphere),注入内存shell时就需要搭环境、找代码、改代码,就很烦

还好此次考古还挖掘到了rebeyond大佬的一篇文章,其中利用到了Java Instrumentation,大概就是一种可以在jvm中修改任意类的应用程序

任意类!!!那我们随便找一个Servlet或者Filter啥的跟容器无关的类(当然也需要能够获取到我们的传参),注入我们的恶意代码不就行了,要做的可能就只是找个类名以及写写shell代码

作者贴心的给出了完整的利用代码可以在tomcat的框架类中注入内存shell(带参访问任意url皆可),也可根据自己的需求来改,贴下自己改的

https://github.com/x1a0t/InjectShell

Reference

https://mp.weixin.qq.com/s/x4pxmeqC1DvRi9AdxZ-0Lw
https://mp.weixin.qq.com/s/1ZiLD396088TxiW_dUOFsQ
https://mp.weixin.qq.com/s/7b3Fyu_K6ZRgKlp6RkdYoA
https://www.cnblogs.com/rebeyond/p/9686213.html