|
| 1 | +# CWE-117: Improper Output Neutralization for Logs |
| 2 | + |
| 3 | +Ensure all untrusted data is properly neutralized or sanitized before writing to application logs. Log injection occurs when untrusted data is written to application logs without proper neutralization, allowing attackers to forge log entries or inject malicious content. Attackers can inject fake log records or hide real ones by inserting newline sequences (`\r` or `\n`), misleading auditors and incident-response teams. This vulnerability can also enable injection of XSS attacks when logs are viewed in vulnerable web applications. |
| 4 | + |
| 5 | +Attackers can exploit this weakness (see [*Attacks on Logs*](https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html#attacks-on-logs) [[OWASP 2025]](https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html#attacks-on-logs)) by submitting strings containing CRLF sequences that create fake log entries. |
| 6 | + |
| 7 | +Attackers can exploit this weakness by submitting strings containing Carriage Return Line Feed (CRLF) sequences that create fake log entries. For instance, an attacker authenticating with a crafted username can make failed login attempts appear successful in audit logs, potentially framing innocent users or hiding malicious activity. |
| 8 | + |
| 9 | +This vulnerability is classified as **CWE-117: Improper Output Neutralization for Logs** [[CWE-117](https://cwe.mitre.org/data/definitions/117.html)]. It occurs when CRLF sequences are not properly neutralized in log output, which is a specific instance of the broader **CWE-93: Improper Neutralization of CRLF Sequences** [[CWE-93](https://cwe.mitre.org/data/definitions/93.html)] weakness. Attackers exploit this using the **CAPEC-93: Log Injection-Tampering-Forging** [[CAPEC-93](https://capec.mitre.org/data/definitions/93.html)] attack pattern. |
| 10 | + |
| 11 | +The OWASP Top 10 [[OWASP A09:2021](https://owasp.org/Top10/A09_2021-Security_Logging_and_Monitoring_Failures/)] lists “Security Logging and Monitoring Failures” as a critical security risk. |
| 12 | + |
| 13 | +## Noncompliant Code Example |
| 14 | + |
| 15 | +This example demonstrates how raw user input written to logs enables injection attacks: |
| 16 | + |
| 17 | +*[noncompliant01.py](noncompliant01.py):* |
| 18 | + |
| 19 | +```python |
| 20 | +""" Non-compliant Code Example """ |
| 21 | +import logging |
| 22 | + |
| 23 | +def log_authentication_failed(user): |
| 24 | + """Simplified audit logging missing RFC 5424 details""" |
| 25 | + logging.warning("User login failed for: '%s'", user) |
| 26 | + |
| 27 | +##################### |
| 28 | +# attempting to exploit above code example |
| 29 | +##################### |
| 30 | +log_authentication_failed("guest'\nWARNING:root:User login failed for: 'administrator") |
| 31 | +``` |
| 32 | + |
| 33 | +**Output of `noncompliant01.py`:** |
| 34 | + |
| 35 | +```bash |
| 36 | +WARNING:root:User login failed for: 'guest' |
| 37 | +WARNING:root:User login failed for: 'administrator' |
| 38 | +``` |
| 39 | + |
| 40 | +The attacker's input creates what appears to be a legitimate log entry showing a login failure for user `administrator`. |
| 41 | + |
| 42 | +## Compliant Solution |
| 43 | + |
| 44 | +As per the OWASP Logging Cheat Sheet [[OWASP 2025]](https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html) section on ["Event collection"](https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html#event-collection), applications should sanitize event data to prevent log injection and encode data correctly for the logged format. |
| 45 | + |
| 46 | +The `compliant01.py` solution uses a strict allow-list for usernames and returns early on any mismatch, so `CR/LF` or other disallowed characters never reach the logger; for rejected attempts it logs a safe one-line summary with `%r` (escaped newlines), preventing forged secondary log lines. In short: validate upfront and neutralize what you do record. |
| 47 | + |
| 48 | +*[compliant01.py](compliant01.py):* |
| 49 | + |
| 50 | +```python |
| 51 | +""" Compliant Code Example """ |
| 52 | + |
| 53 | +import logging |
| 54 | +import re |
| 55 | + |
| 56 | +# Allow only ASCII letters, digits, underscore, dot, and hyphen; max 64 chars |
| 57 | +_ALLOWED_USER = re.compile(r"^[A-Za-z0-9._-]{1,64}$") |
| 58 | + |
| 59 | + |
| 60 | +def is_allowed_username(user: str) -> bool: |
| 61 | + """Return True if username matches the strict allow-list.""" |
| 62 | + return bool(_ALLOWED_USER.fullmatch(user)) |
| 63 | + |
| 64 | + |
| 65 | +def log_authentication_failed(user): |
| 66 | + """Simplified audit logging (example)""" |
| 67 | + if not is_allowed_username(user): |
| 68 | + # Safe summary: %r escapes CR/LF so the log remains one line |
| 69 | + logging.warning("Rejected login attempt: invalid username=%r", user) |
| 70 | + return |
| 71 | + logging.warning("User login failed for: '%s'", user) |
| 72 | + |
| 73 | + |
| 74 | +##################### |
| 75 | +# attempting to exploit above code example |
| 76 | +##################### |
| 77 | +log_authentication_failed("guest'\nWARNING:root:User login failed for: 'administrator") |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +# TODO: Production — keep sink-side neutralization (escape CR/LF) even with validation, |
| 82 | +# prefer structured JSON logs. |
| 83 | +``` |
| 84 | + |
| 85 | +The following output shows that a warning is logged, and the input is sanitized before logging. |
| 86 | + |
| 87 | +**Example compliant01.py output:** |
| 88 | + |
| 89 | +```bash |
| 90 | +WARNING:root:Rejected login attempt: invalid username="guest'\nWARNING:root:User login failed for: 'administrator" |
| 91 | +``` |
| 92 | + |
| 93 | +## Automated Detection |
| 94 | + |
| 95 | +<table> |
| 96 | + <thead> |
| 97 | + <tr> |
| 98 | + <th>Tool</th> |
| 99 | + <th>Version</th> |
| 100 | + <th>Check</th> |
| 101 | + <th>Description</th> |
| 102 | + </tr> |
| 103 | + </thead> |
| 104 | + <tbody> |
| 105 | + <tr> |
| 106 | + <td>Bandit</td> |
| 107 | + <td>1.7.5</td> |
| 108 | + <td>Not Available</td> |
| 109 | + <td></td> |
| 110 | + </tr> |
| 111 | + <tr> |
| 112 | + <td>CodeQL</td> |
| 113 | + <td>Latest</td> |
| 114 | + <td><a href="https://codeql.github.com/codeql-query-help/python/py-log-injection/">py-log-injection</a></td> |
| 115 | + <td></td> |
| 116 | + </tr> |
| 117 | + <tr> |
| 118 | + <td>Veracode</td> |
| 119 | + <td>Latest</td> |
| 120 | + <td>CWE 117</td> |
| 121 | + <td><a href="https://community.veracode.com/s/article/How-to-Fix-CWE-117-Improper-Output-Neutralization-for-Logs">How to Fix CWE-117</a></td> |
| 122 | + </tr> |
| 123 | + </tbody> |
| 124 | +</table> |
| 125 | + |
| 126 | +## Related Guidelines |
| 127 | + |
| 128 | +<table> |
| 129 | + <tr> |
| 130 | + <td><a href="http://cwe.mitre.org/">MITRE CWE</a></td> |
| 131 | + <td>Pillar: <a href="https://cwe.mitre.org/data/definitions/707.html"> CWE-707: Improper Neutralization</a></td> |
| 132 | + </tr> |
| 133 | + <tr> |
| 134 | + <td><a href="http://cwe.mitre.org/">MITRE CWE</a></td> |
| 135 | + <td>Base: <a href="https://cwe.mitre.org/data/definitions/117.html">CWE-117: Improper Output Neutralization for Log </a></td> |
| 136 | + </tr> |
| 137 | + <tr> |
| 138 | + <td><a href="http://cwe.mitre.org/">MITRE CWE</a></td> |
| 139 | + <td>Base: <a href="https://cwe.mitre.org/data/definitions/93.html">CWE-93: Improper Neutralization of CRLF Sequences ('CRLF Injection') </a></td> |
| 140 | + </tr> |
| 141 | + <tr> |
| 142 | + <td><a href="http://cwe.mitre.org/">MITRE CWE</a></td> |
| 143 | + <td>Variant: <a href="https://cwe.mitre.org/data/definitions/113.html">CWE-113: Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Request/Response Splitting') </a></td> |
| 144 | + </tr> |
| 145 | + <tr> |
| 146 | + <td><a href="http://capec.mitre.org/">MITRE CAPEC</a></td> |
| 147 | + <td>Detailed: <a href="https://capec.mitre.org/data/definitions/93.html">CAPEC-93: Log Injection-Tampering-Forging </a></td> |
| 148 | + </tr> |
| 149 | + <tr> |
| 150 | + <td><a href="https://owasp.org/Top10/">OWASP Top 10</a></td> |
| 151 | + <td><a href="https://owasp.org/Top10/A09_2021-Security_Logging_and_Monitoring_Failures/">A09:2021 – Security Logging and Monitoring Failures </a></td> |
| 152 | + </tr> |
| 153 | + <tr> |
| 154 | + <td><a href="https://owasp.org/">OWASP ASVS 4.0</a></td> |
| 155 | + <td><a href="https://owasp.org/www-project-application-security-verification-standard/">OWASP Application Security Verification Standard (ASVS) </a>. See "V16 Security Logging and Error Handling". |
| 156 | +</td> |
| 157 | + <tr> |
| 158 | + <td><a href="https://cheatsheetseries.owasp.org/index.html">OWASP Cheat Sheet Series</a></td> |
| 159 | + <td><a href="https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html">OWASP Logging Cheat Sheet</a></td> |
| 160 | + </tr> |
| 161 | + </tr> |
| 162 | + <tr> |
| 163 | + <td>ISO/IEC TR 24772:2013</td> |
| 164 | + <td><a href="https://www.iso.org/standard/61457.html">ISO/IEC TR 24772:2013 Information technology — Programming languages — Guidance to avoiding vulnerabilities in programming languages through language selection and use </a></td> |
| 165 | + </tr> |
| 166 | + <tr> |
| 167 | + <td>NIST SP 800-92</td> |
| 168 | + <td><a href="https://csrc.nist.gov/pubs/sp/800/92/final">NIST SP 800-92 Guide to Computer Security Log Management </a></td> |
| 169 | + </tr> |
| 170 | +</table> |
| 171 | + |
| 172 | +## Bibliography |
| 173 | + |
| 174 | +<table> |
| 175 | + <tr> |
| 176 | + <td>[CWE-117]</a></td> |
| 177 | + <td>CWE-117: Improper Output Neutralization for Log [online]. Available from <a href="https://cwe.mitre.org/data/definitions/117.html">https://cwe.mitre.org/data/definitions/117.html</a>, [Accessed 24 September 2025]</td> |
| 178 | + </tr> |
| 179 | + <tr> |
| 180 | + <td>[CWE-93]</a></td> |
| 181 | + <td>CWE-93: Improper Neutralization of CRLF Sequences ('CRLF Injection') [online]. Available from: <a href="https://cwe.mitre.org/data/definitions/93.html">https://cwe.mitre.org/data/definitions/93.html</a>, [Accessed 24 September 2025]</td> |
| 182 | + </tr> |
| 183 | + <tr> |
| 184 | + <td>[CWE-113]</a></td> |
| 185 | + <td>CWE-113: Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Request/Response Splitting')<a href="https://cwe.mitre.org/data/definitions/113.html">https://cwe.mitre.org/data/definitions/113.html</a>, [Accessed 24 September 2025]</td> |
| 186 | + </tr> |
| 187 | + <tr> |
| 188 | + <td>[CAPEC-93]</td> |
| 189 | + <td>CAPEC-93: Log Injection-Tampering-Forging [online]. Available from: <a href="https://capec.mitre.org/data/definitions/93.html">https://capec.mitre.org/data/definitions/93.html</a>, [Accessed 24 September 2025]</td> |
| 190 | + </tr> |
| 191 | + <tr> |
| 192 | + <td>[OWASP A09:2021]</td> |
| 193 | + <td>A09:2021 – Security Logging and Monitoring Failures [online]. Available from:<a href="https://owasp.org/Top10/A09_2021-Security_Logging_and_Monitoring_Failures/">https://owasp.org/Top10/A09_2021-Security_Logging_and_Monitoring_Failures/</a>, [Accessed 24 September 2025]</td> |
| 194 | + </tr> |
| 195 | + <tr> |
| 196 | + <td>[OWASP 2025]</td> |
| 197 | + <td>OWASP Cheat Sheet Series: Logging Cheat Sheet [online]. Available from: <a href="https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html">https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html</a> (see “Event collection” and “Attacks on Logs”). [Accessed 24 September 2025]</td> |
| 198 | + </tr> |
| 199 | +</table> |
0 commit comments