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:
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:
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:
Using proper encoding for special characters
Removing or replacing newline characters
Utilizing built-in logging framework security features
Implementing input validation
Code Samples
Vulnerable Code
Fixed Code
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?