Skip to main content
  1. Posts/

Cache Poisoning Attacks on Dependency Management Systems like Maven

Sven Ruppert
Author
Sven Ruppert
20+ years of Java, specialised in Security, Vaadin and Developer Relations. When not coding, you’ll find me in the woods with an axe.
Table of Contents

Cache poisoning on Maven Caches is a specific attack that targets how Maven Caches manages packages and dependencies in a software development process. It’s essential to understand how Maven works before we look at the details of cache poisoning.

  1. Overview of Maven and its caches
  2. What is cache poisoning?
  3. Types of cache poisoning on Maven caches
    1. Man-in-the-Middle (MITM) Cache Poisoning
    2. Exploit repository vulnerabilities
    3. Dependency Confusion
      1. Basics of Dependency Confusion
      2. How Dependency Confusion Was Discovered
      3. Why does Dependency Confusion work so effectively?
    4. Typosquatting
      1. Basics of typosquatting
      2. Typical typosquatting techniques
      3. Dangers of typosquatting
      4. Maven typosquatting cases
  4. Process of a cache poisoning attack on Maven
  5. Security mechanisms to protect against cache poisoning
    1. Regular updates and patch management
    2. Using HTTPS
    3. Signature verification
    4. Improve repository security
    5. Dependency Scanning and Monitoring
    6. Version Pinning
    7. Private Maven-Repositories
    8. Implementation of code reviews and security checks
  6. Understanding CVEs in the context of Maven and cache poisoning
  7. Relevant CVEs related to Maven and repository management tools
    1. CVE-2020-13949: Remote Code Execution in Apache Maven
    2. CVE-2021-25329: Denial of Service in Maven Artifact Resolver
    3. CVE-2019-0221: Directory Traversal in Sonatype Nexus Repository Manager
    4. CVE-2022-26134: Arbitrary File Upload in JFrog Artifactory
    5. CVE-2021-44228: Log4Shell (Log4j)
  8. Analysis and impact of the mentioned CVEs
  9. Future challenges and continuous security improvements
  10. Conclusion

Overview of Maven and its caches
#

Apache Maven is a widely used build management tool in Java projects. It automates the dependency management, build process, and application deployment. However, some fundamental mechanisms make it necessary to consider the security of repositories and dependencies when using Maven.

Maven uses repositories to manage libraries and dependencies. There are two types of Maven repositories:

Local repository : A copy of all downloaded libraries and dependencies is saved on the local machine.

Remote Repositories : Maven can access various remote repositories, such as the central Maven repository or a company’s custom repositories.

After downloading them from a remote repository, Maven stores all dependencies in the local repository (cache). This allows dependencies that are needed multiple times to load more quickly because they do not have to be downloaded from a remote repository each time.

What is cache poisoning?
#

Cache poisoning is a class of attacks in which an attacker fills a system’s cache (in this case Maven caches) with manipulated or malicious content. This results in legitimate requests that should receive the original data instead of the data injected by an attacker. In Maven terms, cache poisoning refers to when an attacker injects malicious artefacts into a developer’s or build’s cache by exploiting a vulnerability in the Maven build process or repository servers.

The attack aims to deliver malicious dependencies that are then integrated into software projects. These poisoned dependencies could contain malicious code to steal sensitive data, take control of the system, or sabotage the project.

Types of cache poisoning on Maven caches
#

There are several scenarios in which cache poisoning attacks can be carried out on Maven repositories:

Man-in-the-Middle (MITM) Cache Poisoning
#

A man-in-the-middle attack allows an attacker to intercept and manipulate network traffic between the developer and the remote Maven repository. If communication is not encrypted, an attacker can inject crafted artefacts and introduce them into the local Maven cache. As a result, the developer believes that the dependencies come from a trusted repository, when in fact, they have been tampered with.

Such an attack could be successful if Maven communicates with repositories over unsecured HTTP connections. The central Maven repository (Maven Central) now exclusively uses HTTPS to prevent such attacks, but some private or legacy repositories use HTTP.

Exploit repository vulnerabilities
#

If an attacker gains access to the remote repository, they can upload arbitrary artefacts or replace existing versions. This happens, for example, if the repository is poorly secured or a vulnerability in the repository management tool (like Nexus or Artifactory) is exploited. In this case, the attacker can inject malware directly into the repository, causing developers worldwide to download the compromised artefact and store it in their Maven cache.

Dependency Confusion
#

A particularly dangerous attack vector that has received much attention in recent years is the so-called “dependency confusion” attack. This attack is because many modern software projects draw dependencies from internal and private repositories and public repositories such as Maven Central. The main goal of a Dependency Confusion attack is to inject malicious packages via publicly accessible repositories into a company or project that believes it is using internal or private dependencies.

Basics of Dependency Confusion
#

Many companies and projects maintain internal Maven repositories where they store their own libraries and dependencies that are not publicly accessible. These internal libraries can implement specific functionalities or make adaptations to public libraries. Developers often define the name and version of dependencies in the Maven configuration (pom.xml) without realising that Maven prioritises dependencies, favouring public repositories like Maven Central over internal ones unless explicitly configured otherwise.

A dependency confusion attack exploits exactly this priority order. The attacker publishes a package with the same name as an internal library to the public Maven repository, often with a higher version number than the one used internally. When Maven then looks for that dependency, it usually prefers the publicly available package rather than the private internal version. This downloads the malicious package and stores it in the developer’s Maven cache, from where it will be used in future builds.

How Dependency Confusion Was Discovered
#

A security researcher named Alex Birsan popularised this attack in 2021 when he demonstrated how easy it was to poison dependencies in projects at major tech companies. By releasing packages with the same names as internal libraries of large companies such as Apple, Microsoft, and Tesla, he successfully launched dependency confusion attacks against these companies.

Birsan did not use malicious content in his attacks but harmless code to prove that the system was vulnerable. He was able to show that in many cases, the companies’ build systems had downloaded and used the malicious (in his case harmless) package instead of the real internal library. This disclosure led to massive awareness in the security community about the risks of Dependency Confusion.

Why does Dependency Confusion work so effectively?
#

The success of a Dependency Confusion attack lies in the default configuration of many build systems and the way Maven resolves dependencies. There are several reasons why this attack vector is so effective:

  • - Automatic prioritisation of public repositories
  • - Trust the version number
  • - Missing signature verification
  • - Reliance on external code

Typosquatting
#

Typosquatting is an attack technique that exploits user oversight by targeting common typos that can occur while typing package names in software development, such as in Maven. Attackers release packages with similar or slightly misspelt names that closely resemble legitimate libraries. When developers accidentally enter the wrong package name in their dependency definitions or automated tools resolve these packages, they download the malicious package. Typosquatting is one of the most well-known attack methods for manipulating package managers, such as Maven, npm, PyPI, and others that host publicly available libraries.

Basics of typosquatting
#

Typosquatting is based on the idea that users often make typos when entering commands or package names. Attackers exploit this by creating packages or artifacts with names that are very similar to well-known and widely used libraries but differ in small details. These details may include minor variations such as missing letters, additional characters, or alternative spellings.

Typical typosquatting techniques
#

Misspelled package names :

One of the most straightforward techniques is to change or add a letter in the name of a well-known library. An example would be the package **com.google.common** , which is often used. An attacker could use a package named **com.gooogle.common** (with an extra “o”) that is easily overlooked.

Different spellings :

Attackers can also use alternative spellings of well-known libraries or names. For example, an attacker could use a package named **com.apache.loggin** to publish the popular **com.apache.logging** looks similar, but due to the missing letter combination “g " and “n " at “logging " is easily overlooked.

Use of prefixes or suffixes :

Another option is to add prefixes or suffixes that increase the similarity to legitimate packages. For example, an attacker could use the package **com.google.common-utils** or **com.google. commonx to publish** the same as the legitimate package**com.google.common** resembles.

Similarity in naming :

Attackers can also take advantage of naming conventions in the open-source community by publishing packages containing common terms or abbreviations often used in combination with other libraries. An example would be releasing a package like **common-lang3-utils** , which is linked to the popular Apache Commons library `commons-lang3 ’ remembers.

Dangers of typosquatting
#

The threat of typosquatting is grave because it is difficult to detect. Developers often rely on their build tools like Maven to reliably download and integrate packages into their projects. If an incorrect package name is entered, you may not immediately realise that you have included a malicious dependency. Typosquatting is a form of social engineering because it exploits people’s susceptibility to errors.

A successful typosquatting attack can lead to severe consequences:

  • Data loss
  • Malware injection
  • Loss of trust

Maven typosquatting cases
#

There have also been incidents of typosquatting in the Maven community. In one case, a package named **commons-loggin** was published, corresponding to the legitimate Apache Commons logging package **commons-logging** . Developers who entered the package name incorrectly downloaded and integrated the malicious package into their projects, creating potential security risks.

Typosquatting is a sophisticated and difficult-to-detect attack method that targets human error. Attackers take advantage of the widespread use of package managers such as Maven, npm, and PyPI by publishing slightly misspelt or similar-sounding packages that contain malicious code. Developers and organisations must be aware of this threat and take appropriate protective measures to ensure that only legitimate and trustworthy packages are included in their projects.

Process of a cache poisoning attack on Maven
#

A typical sequence of a cache poisoning attack on Maven could look like this:

Identification of a target repository : The attacker is looking for a Maven repository used by developers but may have vulnerabilities. This can happen, for example, through outdated versions of publicly available repository management tools.

Handling Artifacts : The attacker manipulates the artefact, e.g. a JAR file, by adding malicious code. This can range from simple backdoors to complex Trojans.

Provision of the poisoned artefact : The manipulated artefact is either uploaded to the public repository (e.g., in the form of a typosquatting package) or injected directly into a compromised target repository.

Download by developer : The developer uses Maven to update or reload the dependencies for his project. Maven downloads the poisoned artefact, which is stored in the local cache.

Compromising the project : Maven will use the poisoned artefact from the cache in future builds. This artefact can then execute malicious code in the application’s context, resulting in system compromise.

Security mechanisms to protect against cache poisoning
#

Various measures should be implemented on both the developer and repository provider sides to protect against cache poisoning attacks.

Regular updates and patch management
#

Make sure Maven, its plugins, and all repository management tools are always up to date. Security updates should be applied immediately to address known vulnerabilities.

Using HTTPS
#

The use of encrypted connections (HTTPS) between Maven and the repository is crucial to ensure that no man-in-the-middle attacks can be performed on the transferring artifacts. Maven Central enforces HTTPS connections, but private repositories should also adhere to this standard.

Signature verification
#

Another protective measure is the use of cryptographic signatures for artefacts. Maven supports the use of PGP signatures to ensure the integrity of artefacts. Developers should ensure that the signatures of downloaded artefacts are verified to ensure that they have not been tampered with.

Improve repository security
#

Repository providers should ensure that their repositories are well protected by implementing robust authentication mechanisms, regular patches and updates to repository management tools such as Nexus or Artifactory.

Dependency Scanning and Monitoring
#

Tools like OWASP Dependency Check or Snyk can scan known dependency vulnerabilities. These tools can help identify malicious or stale dependencies and prevent them from entering the Maven cache.

Version Pinning
#

“Version pinning” means setting specific versions of dependencies in the pom.xml file instead of using dynamic version ranges ([1.0,)). This helps prevent unexpected updates and ensures that only explicitly defined versions of the artefacts are used.

Private Maven-Repositories
#

One approach to maintaining control over dependencies is maintaining a private Maven repository within the organisation. This ensures that only checked artifacts end up in the internal cache, reducing the risk of introducing malicious dependencies into the build process.

Implementation of code reviews and security checks
#

Conduct regular code reviews to ensure only trusted dependencies are used in projects. Automated security checks can provide additional security.

Understanding CVEs in the context of Maven and cache poisoning
#

CVE (Common Vulnerabilities and Exposures) is a standardised system for identifying and cataloguing security vulnerabilities in software. Each CVE number refers to a specific vulnerability discovered and documented by security experts.

There are no specific CVEs that exclusively target cache poisoning in the context of Maven and cache poisoning. Instead, various vulnerabilities in Maven itself, its plugins, or the underlying repository management tools (such as Sonatype Nexus or JFrog Artifactory) can be indirectly exploited for cache poisoning attacks. These vulnerabilities could allow attackers to manipulate dependencies, compromise the integrity of downloads, or bypass Maven’s security mechanisms.

Relevant CVEs related to Maven and repository management tools#

Although no CVEs are explicitly classified as cache poisoning, there are several vulnerabilities in Maven and related tools that could potentially be exploited for cache poisoning attacks:

CVE-2020-13949: Remote Code Execution in Apache Maven
#

  • Description : This vulnerability affected Maven Surefire plugin versions prior to 2.22.2, which could enable remote code execution (RCE). An attacker could use specially crafted POM files to execute malicious code on the build system.
  • Relevance to cache poisoning : By running RCE, an attacker could inject crafted dependencies into the local Maven cache, which could compromise future builds.
  • Reference : CVE-2020-13949

CVE-2021-25329: Denial of Service in Maven Artifact Resolver
#

  • Description : This vulnerability affected the Apache Maven Artifact Resolver component, which is responsible for resolving and downloading artefacts. A specially crafted POM file could lead to a denial of service (DoS).
  • Relevance to cache poisoning : A DoS attack could impact the availability of Maven repositories, forcing developers to use alternative (possibly insecure) repositories, increasing the risk of cache poisoning.
  • Reference : CVE-2021-25329

CVE-2019-0221: Directory Traversal in Sonatype Nexus Repository Manager
#

  • Description : This vulnerability allows attackers to access files within the Nexus Repository Manager through a directory traversal attack.
  • Relevance to cache poisoning : By accessing critical files or configurations, attackers could compromise the repository manager and insert malicious artefacts, which are then downloaded by developers and stored in the local Maven cache.
  • Reference : CVE-2019-0221

CVE-2022-26134: Arbitrary File Upload in JFrog Artifactory
#

  • Description : This vulnerability allowed attackers to upload arbitrary files to a JFrog Artifactory server, which could result in a complete compromise of the server.
  • Relevance to cache poisoning : By uploading arbitrary files, attackers could inject malicious Maven artefacts into the repository, which developers then download and cache.
  • Reference : CVE-2022-26134

CVE-2021-44228: Log4Shell (Log4j)
#

  • Description : This widespread vulnerability affected the Log4j library and allowed remote code execution by exploiting JNDI injections.
  • Relevance to cache poisoning : Many Maven projects use Log4j as a dependency. A manipulated version of this dependency could enable RCE through cache poisoning.
  • Reference : CVE-2021-44228

Analysis and impact of the mentioned CVEs
#

The above CVEs illustrate how vulnerabilities in Maven and related tools can potentially be exploited for cache poisoning and other types of attacks:

  • Remote Code Execution (RCE) : Vulnerabilities such as CVE-2020-13949 and CVE-2021-44228 allow attackers to execute malicious code on the build system. This can be used to inject manipulated dependencies into the local cache.
  • Denial of Service (DoS) : CVE-2021-25329 demonstrates how attackers can impact the availability of Maven repositories. This may result in developers being forced to resort to alternative sources that may be unsafe.
  • Directory Traversal and Arbitrary File Upload : CVE-2019-0221 and CVE-2022-26134 demonstrate how attackers can compromise repository management tools to upload malicious artefacts that developers then unknowingly use in their projects.

Future challenges and continuous security improvements
#

As the use of open-source libraries in modern software development continues to increase, the threat of attacks such as cache poisoning also increases. Automating the build process and relying on external repositories creates an expanded attack surface that attackers seek to exploit.

It is becoming increasingly important for companies and developers to cultivate a security culture prioritising the safe handling of dependencies and artefacts. This requires not only the implementation of technical protection measures but also the training of developers to raise awareness of the risks of cache poisoning and other dependency attacks.

Conclusion
#

Cache poisoning attacks on Maven caches are a severe risk, especially at a time when open-source components play an essential role in software development. The attacks exploit vulnerabilities in how dependencies are resolved, downloaded and cached.

Developers and companies must implement best security practices to prevent such attacks. This includes using HTTPS, signing and verifying artefacts, securing repositories, regular vulnerability scanning, and controlling the dependencies introduced into their projects.

Awareness of the risks and implementing appropriate security measures are key to preventing cache poisoning attacks and ensuring the integrity of software development processes.

Happy Coding

Sven

Related

BLD - a lightweight Java Build Tool

What is a dependency management tool? # A dependency management tool is a software system or utility that automates the process of identifying, retrieving, updating, and maintaining the external libraries or packages (referred to as dependencies) required by a software project. It ensures that all necessary dependencies are included and managed in a standardised way, which helps prevent version conflicts, missing libraries, and manual errors during software development.

CWE-1123: Excessive Use of Self-Modifying Code for Java Developers

Self-modifying code refers to a type of code that alters its own instructions while it is executing. While this practice can offer certain advantages, such as optimisation and adaptability, it is generally discouraged due to the significant risks and challenges it introduces. For Java developers, using self-modifying code is particularly problematic because it undermines the codebase’s predictability, readability, and maintainability, and Java as a language does not natively support self-modification of its code.

Securing Apache Maven: Understanding Cache-Related Risks

What is a Package Manager - Bird-Eye View # A package manager is a tool or system in software development designed to simplify the process of installing, updating, configuring, and removing software packages on a computer system. It automates managing dependencies and resolving conflicts between different software components, making it easier for developers to work with various libraries, frameworks, and tools within their projects.

CWE-778: Lack of control over error reporting in Java

Learn how inadequate control over error reporting leads to security vulnerabilities and how to prevent them in Java applications. # Safely handling error reports is a central aspect of software development, especially in safety-critical applications. CWE-778 describes a vulnerability caused by inadequate control over error reports. This post will analyse the risks associated with CWE-778 and show how developers can implement safe error-handling practices to avoid such vulnerabilities in Java programs.