Accepting user input in log files can result in log forgingA log injection vulnerability arises when a log entry contains unsanitized user input. A malicious user can insert fake log data and consequently deceive system administrators as to the system's behavior [OWASP 2008]. For example, a user could be able to break a an attacker might split a legitimate log entry into two log entries by entering a carriage return and line feed (CRLF) sequence . The second entry could be intentionally misleading; for example, it may warn the administrator that a reboot is required to install critical security updates. Consequently, user input must be sanitized before being used or loggedto mislead an auditor. Log injection attacks can be prevented by sanitizing and validating any untrusted input sent to a log.
Logging unsanitized user input can also result in leaking sensitive data across a trust boundary, or storing sensitive data in a manner that is contrary to local law or regulation. See rule IDS00-J. Sanitize untrusted data passed across a trust boundary for more details on input sanitization. For example, an attacker might inject a script into a log file such that when the file is viewed using a web browser, the browser could provide the attacker with a copy of the administrator's cookie so that the attacker might gain access as the administrator.
Noncompliant Code Example
This noncompliant code example logs the user's login user name when an invalid request is received. No input sanitization is performed untrusted data from an unauthenticated user without data sanitization.
Code Block | ||
---|---|---|
| ||
if (loginSuccessful) {
logger.severe("User login succeeded for: " + username);
} else {
logger.severe("User login failed for: " + username);
}
|
Without sanitization, a log injection attack is possible. A standard log message when username
is guest
might look like this:
Code Block |
---|
May 15, 2011 2:19:10 PM java.util.logging.LogManager$RootLogger log
SEVERE: User login failed for: guest
|
If the username
that is used in a log message is not guest
but rather a multiline string like this:
Code Block |
---|
guest
May 15, 2011 2:25:52 PM java.util.logging.LogManager$RootLogger log
SEVERE: User login succeeded for: administrator
|
the log would contain the following misleading data:
Code Block |
---|
May 15, 2011 2:19:10 PM java.util.logging.LogManager$RootLogger log
SEVERE: User login failed for: guest
May 15, 2011 2:25:52 PM java.util.logging.LogManager log
SEVERE: User login succeeded for: administrator
|
Compliant Solution (Sanitized User)
This compliant solution sanitizes the username
before logging it, preventing injection attacks.
Code Block | |||
---|---|---|---|
| |||
if (loginSuccessful) { logger.severe("Invalid username:" + getUserName()); |
Compliant Solution
"User login succeeded for: " + sanitizeUser(username));
} else {
logger.severe("User login failed for: " + sanitizeUser(username));
} |
The sanitization is done by a dedicated method for sanitizing user names:
Code Block | ||
---|---|---|
| ||
public String sanitizeUser(String username) {
return Pattern.matches("[A-Za-z0-9_]+", username))
? username : "unauthorized user";
} |
Compliant Solution (Sanitized Logger)
This compliant solution uses a text logger that automatically sanitizes its input. A sanitized logger saves the developer from having to worry about unsanitized log messages.
Code Block | ||
---|---|---|
| ||
Logger sanLogger = new SanitizedTextLogger(logger);
if (loginSuccessful) {
sanLogger.severe("User login succeeded for: " + username);
} else {
sanLogger.severe("User login failed for: " + username);
} |
The sanitized text logger takes as delegate an actual logger. We assume the logger outputs text log messages to a file, network, or the console, and each log message has no indented lines. The sanitized text logger sanitizes all text to be logged by indenting every line except the first by two spaces. While a malicious user can indent text by more, a malicious user cannot create a fake log entry because all of her output will be indented, except for the real log outputThis compliant solution sanitizes the user name input before logging it. Refer to rule IDS00-J. Sanitize untrusted data passed across a trust boundary for more details on input sanitization.
Code Block | ||
---|---|---|
| ||
class SanitizedTextLogger extends Logger { Logger delegate; public SanitizedTextLogger(Logger delegate) { super(delegate.getName(), delegate.getResourceBundleName()); this.delegate = delegate; } public String sanitize(String username msg) { Pattern newline = getUserName(Pattern.compile("\n"); sanitize(username Matcher matcher = newline.matcher(msg); logger.severe("Invalid username:" + username); return matcher.replaceAll("\n "); } public void severe(String msg) { delegate.severe(sanitize(msg)); } // .. Other Logger methods which must also sanitize their log messages } |
Risk Assessment
Allowing unvalidated user input to be logged can result in forging of log entries, leaking secure information, or storing sensitive data in a manner that is contrary to violates a local law or regulation.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|
IDS03-J |
Medium |
Probable |
Medium | P8 | L2 |
Related Guidelines
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f43fa2b9-02d2-4efa-9ed5-a5697105a6a2"><ac:plain-text-body><![CDATA[ | [[MITRE 2009 | AA. Bibliography#MITRE 09]] | [CWE ID 144 | http://cwe.mitre.org/data/definitions/144.html] "Improper Neutralization of Line Delimiters" | ]]></ac:plain-text-body></ac:structured-macro> |
| CWE ID 150 "Improper Neutralization of Escape, Meta, or Control Sequences" |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ebde3969-66d0-4449-94cc-5ed042bd391f"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | ]]></ac:plain-text-body></ac:structured-macro> |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
The Checker Framework |
| Tainting Checker | Trust and security errors (see Chapter 8) | ||||||
CodeSonar |
| JAVA.IO.TAINT.LOG | Tainted Log (Java) | ||||||
Fortify | Log_Forging | Implemented | |||||||
Klocwork |
| SVLOG_FORGING | Implemented | ||||||
Parasoft Jtest |
| CERT.IDS03.TDLOG | Protect against log forging |
Related Guidelines
Injection [RST] | |
CWE-144, Improper neutralization of line delimiters | |
MITRE CAPEC | CAPEC-93, Log Injection-Tampering-Forging |
Bibliography
...
IDS11-J. Sanitize non-character code points before performing other sanitization IDS07-J. Do not pass untrusted, unsanitized data to the Runtime.exec() method