Popular with:
Security Engineer
Application Security

The Importance of Security Logging in Protecting Your Web Apps

December 7, 2023
Written by
Abhay Bhargav

Developers don't always think like hackers.

Let me ask you this: Are your web applications truly secure, or are they just ticking time bombs waiting for an attack?

With breaches happening left and right and the security of web applications that can make or break a business, the importance of staying one step ahead of hackers cannot be overstated. The key to your applications' safety might surprise you - it's logging.

Logging plays an important role in securing your applications and the data that they handle, even though, for the most part, it goes unacknowledged. In this blog, we'll talk about why logging is a critical piece of the application security puzzle and how you can leverage it to make your applications more robust, reliable, and resilient.

Table of Contents:

Table of Contents:

1. Understanding what to log

  • AuthN (Authentication) Events
  • AuthZ (Authorization) Failures
  • Access to Sensitive Data
  • Validation Failures/Exceptions
  • Data access/Parser exceptions
  • Log Data Formatting and Retention

2. What should not be logged

3. The power of logging comes with great responsibility

Understanding what to log

In the world of cybersecurity, logging plays a pivotal role in keeping systems secure and monitored. However, deciding what to log is often a challenge. While the temptation to record every single event might be strong, indiscriminate logging can lead to a massive pile of data, making it difficult to extract meaningful insights.

To strike the right balance between granularity and efficiency, focus on logging critical events that provide actionable information. Key areas to prioritize include user authentication, data access, application errors, and network communication. By selectively logging these events, you can gather valuable insights without getting bogged down in irrelevant data.

Contextual logging adds another layer of value to log entries. By providing additional information surrounding each event, contextual logging makes logs more meaningful and easier to use, enhancing their effectiveness in identifying and mitigating potential threats. 

Contextual Logging: Enhancing Log Usability with Meaningful Information

Contextual logging is a method of logging that goes beyond simply recording timestamps, messages, and levels. It enriches log entries with additional information, providing valuable context that enhances the understanding and analysis of events within a system. By embedding contextual data, organizations can gain deeper insights into system behavior, identify potential issues more effectively, and facilitate troubleshooting with greater precision.

Methods for Contextual Logging

Several approaches can be employed to implement contextual logging:

  1. Structured Logging Libraries: Structured logging libraries, such as Logstash, Fluentd, and Graylog2, facilitate the logging of events in a structured format. This structured format simplifies parsing and analysis, enabling easier extraction of meaningful insights from log data.
  2. Log Management Tools: Log management tools, such as Graylog and Elasticsearch provide centralized platforms for collecting, storing, and analyzing logs from diverse sources. These tools often incorporate contextual logging capabilities, enabling the addition of relevant information to log entries.
  3. Logging Frameworks: Logging frameworks, such as Log4j and Logback provide structured logging capabilities within application code. These frameworks facilitate consistent and manageable logging practices, making it easier to incorporate contextual information into log entries.

Tools for Implementing Contextual Logging

A variety of tools can assist in implementing contextual logging practices, , the most popular of which is:

1. ELK Stack: The ELK Stack, which consists of Elasticsearch, Logstash, and Kibana, provides a powerful and versatile solution for implementing contextual logging.

  • Elasticsearch is a distributed search and analytics engine that excels at storing, indexing, and analyzing large volumes of log data. It handles complex data structures and performs real-time search and analysis.
  • Logstash is a data processing pipeline that serves as the intermediary between the data sources generating logs and Elasticsearch. It collects log data from various sources, applies transformations and filters to extract relevant information, and enriches the logs with contextual data. 
  • Kibana is a user interface and analytical dashboard that provides a centralized platform for visualizing and interacting with log data stored in Elasticsearch. It offers a variety of tools for data exploration, including visualizations, dashboards, and anomaly detection.

AuthN (Authentication) Events

Authentication events are the first line of defense when it comes to securing your web applications. Let's break down the significance of various AuthN events:

Logging successful logins

Logging successful login attempts is important for tracking user activity within your web apps. These logs can help you answer questions like: Who accessed the application? When did they log in? From which IP address did they log in? 

Multi-factor authentication events

Multi-factor authentication (MFA) adds an extra layer of security by requiring users to provide two or more forms of identification before granting access. Logging MFA events is crucial as it allows you to verify that the MFA process is functioning correctly.

Account lockout events

Account lockout events are when a user account is temporarily disabled due to several unsuccessful login attempts. These events are important to log  as they indicate potential security incidents. 

AuthZ (Authorization) Failures

Authorization events are equally necessary for securing web applications. These events are the permission and access rights granted to users and the attempts to manipulate or bypass these permissions. Let's explore the importance of various AuthZ events:

Logging unauthorized access attempts

Unauthorized access attempts are when a user tries to access resources or make transactions they are not permitted to. It's a top concern in web application security. Logging these events is important to identify potential intrusions or insider threats. It helps in pinpointing who attempted to access restricted areas and when the attempts were made.

Recording access to restricted resources

Beyond just logging unauthorized access attempts, it's essential to log successful unauthorized accesses as well. These are when a user gains unintended access to a resource because of a misconfiguration or a flaw in the authorization system. 

Role and privilege changes

Changes in user roles and privileges are another important aspect of AuthZ events. Any alterations to user permissions, whether intentional or not, should be diligently logged, including granting or revoking access rights, elevating privileges, or changing user roles.

Access to Sensitive Data

Access to sensitive data pertains to protecting information that, if exposed or mishandled, could lead to serious consequences for both users and organizations. Here are the details:

Defining sensitive data in the context of your application

Establishing a clear definition of what can be considered “sensitive data” can be tricky as it can vary from application to application. It includes customer records, financial data, personal information, intellectual property, or any other data that, if disclosed, could result in harm, privacy violations, or legal liabilities. You can adjust your logging strategy to focus on these high-risk areas by precisely defining what falls into this category.

Logging access to sensitive data

Once you've decided what qualifies as sensitive data, it's essential to log access to these resources rigorously. This includes:

  1. Customer records

Logging access to customer records is important for tracking who views or changes this information to help identify unauthorized access or breaches of confidentiality.

  1. Financial data

Financial data, like transactions and account balances, are highly sensitive. Logging access to financial records guarantees that transparency is prioritized and fraudulent activities or irregularities are mitigated.

  1. Personal information

Personal information, such as names, addresses, social security numbers, or medical records, requires strict access control and thorough logging to protect user privacy and adhere to data protection regulations.

  1. Intellectual property

If your application deals with intellectual property, such as code, patents, or trade secrets, it's important to log any access to these valuable assets to prevent theft or misuse.

Masking or obfuscating sensitive data in logs

Yes, logging access to sensitive data is important, but protecting the data itself within the logs is also of equal importance. This can be achieved through data masking or obfuscation, where sensitive information is replaced with placeholders or hashes in the logs. Masking prevents unauthorized users from gathering insights into the actual data while still allowing administrators to track access and events related to sensitive data. By implementing this practice, you strike a balance between security and compliance to ensure that your logs remain a valuable tool for monitoring while minimizing the risk of data exposure.

ta access/Parser exceptions

Any exceptions or errors in the process of data access and parsing are important operations in web applications because of their significant security implications. Let's explore the importance of logging database query failures, capturing data access errors, handling parser exceptions, and monitoring changes to data schemas or structures:

Logging database query failures

Database query failures can be because of a variety of issues, like syntax errors, connectivity problems, or unauthorized access attempts. Logging these failures is essential so that you can maintain the integrity and security of your application's data. Tracking database query failures helps to quickly identify potential SQL injection attempts or issues with your application's interaction with the database.

Capturing data access errors

Data access errors include issues related to reading, writing, or manipulating data. These errors can reveal security vulnerabilities or misconfigurations that may expose sensitive information. Logging data access errors helps you identify when data access operations fail, enabling you to pinpoint potential issues or security threats.

Handling parser exceptions

Parser exceptions happen when processing user input or external data, such as XML, JSON, or other data formats. Incorrectly handling these exceptions can lead to security vulnerabilities, such as denial of service (DoS) attacks or malicious data injection. Logging parser exceptions is important to understand where and why data processing errors occur. 

Monitoring changes to data schemas or structures

Changes to data schemas or structures can impact the security and functionality of your web application. Logging changes like that is essential to make sure that unauthorized modifications are promptly detected. 

Log Data Formatting and Retention

The way you structure and store your logs directly affects your ability to monitor and investigate security incidents. Let's delve into the key details:

Choosing an appropriate log format

Selecting the right log format is the foundation of effective log management. Common log formats include JSON, CSV, plain text, and more. The choice of format should align with your application's needs and your capacity for log analysis. For example, JSON is more compatible to use for structured data and makes it easier to parse and extract information. On the other hand, plain text logs might be simpler but can be difficult to work with for complex data analysis.

Setting log retention policies for different types of events

Log retention policies determine how long you keep log data before it's archived or deleted. Different types of events might have distinct retention needs. For instance, while you may want to keep security-related logs for an extended period to investigate past incidents, other less critical logs can have shorter retention periods. 

Techniques for log compression and archiving

As log volumes become heavier, efficient log storage becomes more important. Employing techniques such as log compression and archiving can help you maintain long-term retention without excessive storage costs. Compression reduces the size of log files and optimizes the efficiency of your storage. Archiving involves moving older logs to separate storage, like cold storage or tape, to free up space on active storage systems. Both techniques can significantly reduce the storage footprint and costs while still allowing you to retain historical log data for auditing and analysis.

What should not be logged

Logging sensitive data can lead to severe security and compliance issues, making it important to identify and exclude such information from your logs. Let's explore:

Identifying information that should be excluded from logs

  1. Personally Identifiable Information (PII) - The most important data to exclude from logs is personally identifiable information (PII). This includes names, addresses, social security numbers, and any data that can be used to identify an individual.

  1. Passwords and Authentication Tokens - Passwords and authentication tokens are the keys to accessing user accounts. Logging these can have catastrophic consequences if the logs are accessed by unauthorized parties. Always avoid storing or logging sensitive authentication credentials.

  1. Credit card numbers and financial data - Credit card numbers, bank account details, and other financial information are prime targets for cybercriminals. Logging this information poses a significant risk, as it can lead to payment card industry (PCI) compliance violations and financial fraud.

  1. Health information (HIPAA) - Health information, like medical records and patient data, is protected by the Health Insurance Portability and Accountability Act (HIPAA). It's extremely prohibited to log this information because of severe legal consequences.

  1. Sensitive keys and secrets - Keys, secrets, and tokens used for encryption, authentication, or access control should never be logged. Exposing these in logs can lead to unauthorized access, data breaches, and system compromises.

Implementing data redaction or exclusion techniques

To prevent sensitive information from appearing in logs, implement data redaction or exclusion techniques. These techniques involve replacing sensitive data with placeholders, hashes, or other obfuscated representations in the log entries. Here are some strategies for data redaction:

  • Use Regular Expressions (Regex): Create regular expressions that can identify and redact or exclude sensitive patterns, such as credit card numbers or social security numbers.
  • Tokenization: Replace sensitive data with tokens or randomly generated strings. Tokenization preserves the format of the data without exposing the actual information.
  • Hashing: Hash sensitive data, such as passwords or PII, before logging it. Hashing ensures that the original data cannot be reconstructed from the log entries.
  • Masking: Mask sensitive data by hiding portions of it, such as displaying only the last four digits of a credit card number.

With techniques like this, you can balance logging the information necessary for monitoring and security and protecting sensitive data to ensure that your logs remain a valuable tool for analysis without risking data breaches or non-compliance with data protection regulations.

The power of logging comes with great responsibility

It's not just about collecting data; it's about how you structure, store, and manage it. Choosing the right log format, establishing retention policies, and employing techniques like compression and archiving are key to optimizing log management, reducing storage costs, and ensuring long-term accessibility.

AppSecEngineer can help you and your team streamline the complex process of web application security. Library of world-class resources, hands-on labs that focus on training your team on real-world scenarios, and courses to speed up processes without sacrificing security—these are just some of our proven training methods that helped 90% of our AppSecEngineer for Business clients who've seen improved results in as little as 3 months.

Bleeding-edge. Hands-on. Affordable. That's AppSecEngineer.

Source for article
Abhay Bhargav

Abhay Bhargav

Abhay is a speaker and trainer at major industry events including DEF CON, BlackHat, OWASP AppSecUSA. He loves golf (don't get him started).

Abhay Bhargav


Contact Support


1603 Capitol Avenue,
Suite 413A #2898,
Cheyenne, Wyoming 82001,
United States

Copyright AppSecEngineer © 2023