Fixing Log Forging

Everything you need to know in order to fix your code

Tools recognizing this:

Opengrep Fortify Checkmarx SonarQube Snyk Semgrep CodeQL

What is Log Forging

Log Forging (also known as Log Injection) is a security vulnerability that occurs when an application writes untrusted user input to log files without proper validation or encoding.

An attacker can exploit this vulnerability by injecting malicious content into logs, which can lead to:

  • Insertion of fake log entries

  • Manipulation of log data

  • Cross-site scripting (XSS) if logs are displayed in web interfaces

  • Log file corruption

  • Potential system compromise through log analysis tools

One Simple Example

Consider this common logging scenario:

logger.info("User login attempt for username: " + username);

Assuming the value of username comes from user's input, an attacker could provide this input as the username:

admin%0D%0ALogin successful

%0D%0A is the new-line character, resulting in the following log entry:

User login attempt for username: admin 
Login successful

This creates a false log entry suggesting a successful login, potentially misleading security analysts or automated log monitoring systems.

Real-world Occurrences of Log Forging

Log4Shell Vulnerability in Apache Log4j (CVE-2021-44228)

In December 2021, a critical vulnerability known as Log4Shell was discovered in Apache Log4j, a widely used Java-based logging framework. This vulnerability allowed attackers to perform log injection by crafting malicious input that, when logged by the application, could lead to remote code execution.

The vulnerability had a widespread impact, affecting numerous applications and services that utilized Log4j for logging. Exploitation of this vulnerability allowed attackers to execute arbitrary code on affected systems, leading to potential data breaches and system compromises.

Reference: Log4Shell - Wikipedia

Spring Security Log Injection (CVE-2021-22060)

A log forging vulnerability in Spring Security allowed an attacker to inject malicious characters into authentication logs, potentially altering log records or bypassing security checks.

Impact: Attackers could manipulate logs to make it appear as if a different user performed specific actions, covering their tracks during exploitation.

Fixing Log Forging

The most effective way to prevent Log Forging is to properly encode or sanitize all user-supplied data before writing it to log files. This can be achieved by:

  1. Using proper encoding for special characters

  2. Removing or replacing newline characters

  3. Utilizing built-in logging framework security features

  4. Implementing input validation

Code Samples

Vulnerable Code

String userInput = request.getParameter("username");
logger.info("User input: " + userInput);

Fixed Code

String userInput = request.getParameter("username");
String sanitized = userInput.replace('\n', '_').replace('\r', '_');
logger.info("User input: {}", sanitized);

Fix Explanation

The vulnerable code directly writes user input to logs. The fix sanitizes the input by replacing newline characters. Uses proper logging framework placeholder to prevent string concatenation. Ensures log entries cannot be split across multiple lines.

Last updated

Was this helpful?