Malspec

Famous hacks, and how they were fixed.

Famous bugsIncident report

Log4Shell: the logging bug that let a chat message run code

A forgotten feature in the Log4j library let attackers run code by getting a line of text logged; four releases in five weeks closed it.

Illustration: a log book whose lines include a red hook pulling a package from a distant red server, and a green switch on the cover cutting the red cable.

In December 2021 the world learned that Log4j, a logging library inside a huge number of Java programs, would fetch and run code from the internet if it was asked to write down the wrong line of text. Anything that logged what users typed, from a chat message to a web form, could be told to do it. The Apache Log4j team shipped a fix within days of the flaw going public, then three more releases in three weeks as researchers found gaps, while companies everywhere searched for copies of Log4j they did not know they had.

When: December 2021 Hit: Countless Java applications, services and devices that used Log4j 2 Way in: Log4j's lookup feature, which could load and run code named in a logged message Found by: Chen Zhaojun of the Alibaba Cloud security team Fixed by: Log4j 2.15.0, then 2.16.0, 2.17.0 and 2.17.1 (CVE-2021-44228) Time to fix: 12 days from report to first fix; five weeks to the final patch

What happened

Nearly every program writes logs: notes about what it is doing, who logged in, what went wrong. In the Java world, a free library called Log4j, maintained by volunteers at the Apache Software Foundation, did this job for an enormous number of applications, from enterprise software and cloud services to games and network devices.

On 24 November 2021 a security researcher privately reported to the Log4j team that the library had a serious flaw. On 9 December, before most users had been warned, details and a working attack appeared publicly online. Within hours attackers were scanning the internet for vulnerable systems.

The flaw was easy to trigger and the library was everywhere, often buried several layers deep inside other software. The director of the US Cybersecurity and Infrastructure Security Agency, Jen Easterly, called it one of the most serious vulnerabilities she had seen in her career. Security teams spent the holidays hunting for Log4j in their systems.

How it worked

Log4j had a convenience feature called lookups. A log message could contain a short placeholder that Log4j would replace with a value, such as an environment setting, when it wrote the line.

One kind of lookup used JNDI, a Java system for finding resources on a network. It could be told to contact a server and load an object from it. Log4j processed lookups not only in the program's own messages but also inside text that came from users.

So if an application logged anything an outsider controlled, such as a username, a search term or a browser's identifying string, an attacker could put a lookup in that text. When Log4j wrote the log line, it would contact the attacker's server, download code and run it inside the application. Logging a message became running a stranger's program.

The deeper problem was that the feature was rarely needed and few people knew it existed. It had been part of Log4j since 2013.

How it was discovered

Chen Zhaojun of Alibaba Cloud's security team found the flaw and reported it privately to the Apache Log4j project on 24 November 2021 so that a fix could be prepared.

The public found out another way. On 9 December a proof of concept was posted online, and players noticed that the popular game Minecraft, which uses Log4j, could be attacked by typing a crafted message into its chat. The news spread from there.

Afterwards, companies that watch internet traffic went back through their records to see when attacks had started. Cloudflare reported finding attempts as early as 1 December, more than a week before the public disclosure, which meant some attackers had learned of the flaw before most defenders did. From 9 December, the attacks were easy to see: crafted lookups turned up in web requests, usernames and form fields across the internet, and network scanners began flagging them within days.

The timeline

2021-11-24 · Alibaba Cloud reports the flaw to Apache

The Log4j maintainers start work on a fix in private.

2021-12-09 · Details become public and scanning begins

A working attack appears online and attackers race to find vulnerable servers.

2021-12-10 · Log4j 2.15.0 and CVE-2021-44228 are announced

The first fix turns off lookups in messages by default and restricts JNDI to local resources.

2021-12-13 · Log4j 2.16.0 removes message lookups entirely

Researchers find the first fix can be bypassed in some setups (CVE-2021-45046). The second release disables JNDI by default and removes message lookups.

2021-12-28 · Log4j 2.17.1 closes the last related gap

After a denial-of-service flaw fixed in 2.17.0 and a final configuration issue, 2.17.1 becomes the version to install.

How it was fixed

The library fix came in steps, each closing a gap found in the one before. The end state was simple: Log4j no longer looks up anything in log messages, and its JNDI feature is off unless someone deliberately turns it on. For systems that could not be updated at once, the maintainers published ways to remove the dangerous class from the library file.

The harder fix was finding every copy. Log4j was often inside other products, sometimes bundled inside a library that was inside another library. Vendors published lists of affected products and patches over weeks. In the US, CISA issued an emergency directive ordering federal agencies to find and fix affected systems, and kept a public list of vulnerable software.

Because the flaw was so easy to use and so widespread, security teams also had to assume that some systems had been attacked before they were patched and check them for signs of intrusion.

What changed after

In July 2022 the US Cyber Safety Review Board, in its first report, concluded that Log4Shell was an "endemic vulnerability" that would remain in systems for years, because so many copies would never be updated.

The episode pushed organisations to keep a software bill of materials, a list of every component inside their software, so that the next "where do we use this?" question could be answered in minutes instead of weeks. It also renewed attention on funding the volunteers who maintain widely used open-source code, through efforts such as the OpenSSF.

Lessons that still apply

  • Know what's inside your software. Teams with a component inventory found Log4j quickly. Others searched for weeks.
  • Treat logged text as untrusted. Anything a user can type can reach your logs, so logging must never interpret it.
  • Turn off features you don't use. Lookups were rarely needed, but they were on by default.
  • Patch, then look for intrusions. When attackers move first, fixing the hole does not remove anyone already inside.

Sources

The fix → Four releases in five weeks, and a hunt through every program for a library nobody knew they had.

Share this