Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 14

Log Data Analysis: Aggregation & String Functions

Recommended resources

Welcome to the final lesson of our module on manipulating data. So far, you have learned to summarize data with aggregate functions, group it with GROUP BY, filter those groups with HAVING, and make your queries readable with aliases. Now, we will bring all these skills together for a task that is central to the work of a security analyst: analyzing log data.

In this lesson, you will practice analyzing security log data by combining aggregate functions with a new set of tools: string functions. Raw log data is often messy and textual. String functions are your toolkit for dissecting, cleaning, and extracting meaningful information from this text. By the end of this lesson, you'll be able to write sophisticated queries to identify patterns, anomalies, and potential threats within large log datasets.

The Logic of Data Aggregation: A Quick Recap

Before we add string functions to the mix, let's briefly review the logical process of a query that uses aggregation. The GROUP BY clause is the engine that drives this analysis.

This diagram illustrates the pipeline for an aggregate query. First, the data is partitioned into groups based on the specified columns. Next, aggregate functions like `SUM` or `COUNT` are applied to each group. The rows within each group are then collapsed into a single summary row. Finally, you can use the `HAVING` clause to filter these aggregated results.

Your Toolkit for Text: An Introduction to String Functions

Log entries—whether from a web server, an EDR tool, or a cloud audit trail—are filled with text: IP addresses, request URIs, user-agent strings, file paths, and more. To analyze this data effectively, you need to be able to manipulate these strings.

The following video provides a great overview of the most common and useful string functions. As you watch, don't just focus on the syntax; think about how each function could be applied to a security context. For example, LOWER() can standardize inconsistent user agent strings before grouping, and SUBSTRING() can extract a session ID from a URL parameter.

String Functions in MySQL | Intermediate MySQL Series

This video from "Alex The Analyst" introduces several essential string functions in MySQL. The principles are applicable across most SQL dialects.

Please watch the following segments to familiarize yourself with these functions: UPPER and LOWER: For converting strings to a consistent case. TRIM, LTRIM, and RTRIM: For removing unwanted whitespace from the beginning or end of a string. LEFT, RIGHT, and SUBSTRING: For extracting parts of a string. SUBSTRING is particularly powerful for pulling specific data from a structured log entry. REPLACE: For substituting parts of a string with something else. LOCATE: For finding the position of a substring within a larger string, often used with SUBSTRING. CONCAT: For joining multiple strings together.


From Theory to Practice: Exploratory Data Analysis for SecOps

Now, let's apply these concepts to a realistic security scenario. Imagine you've been given a large JSON file containing enriched Endpoint Detection and Response (EDR) data. Your task is to perform an initial exploratory data analysis (EDA) to find suspicious patterns.

The following article is an excellent, hands-on guide that walks you through this exact process. It uses SQL to query security data, demonstrating how to combine aggregation, filtering, and string manipulation to gain insights.

Introductory SQL for SecOps: Exploratory Data Analysis ...

This blog post from Query.ai provides a superb, security-focused walkthrough of using SQL for data analysis. It perfectly demonstrates the techniques we're covering.

Please read the following sections carefully, paying close attention to the SQL queries and the rationale behind them: Start with the section titled "Aggregating and Filtering". This part shows how to: Find the "top talkers" (most frequent IPs) using COUNT, GROUP BY, and ORDER BY. Filter results for specific values using the WHERE clause (e.g., a specific IP or severity level). Combine WHERE with boolean operators like AND and OR for more complex conditions. Filter aggregated results using HAVING to find hosts that breach an alert threshold (e.g., HAVING alert_count > 3). Next, read "Timestamps and Pattern Matching". This section introduces a critical string-matching operator: Focus on the LIKE operator and the use of wildcards (%). This is one of the most common ways to search for patterns in text fields like file paths or user agents. Notice how it's used to find file paths containing ssh—a classic hunting technique.


Putting It All Together: Real-World Security Queries

The techniques in the article you just read are not just academic exercises; they are the basis for real-world security analytics. Let's look at a few more examples tailored to analyzing audit logs.

Example 1: Finding the Most Common Actions

Imagine you are analyzing Google Cloud audit logs. You want to know what the most frequent activities are in your environment over the last 30 days. This could help you establish a baseline of normal activity or spot an unusual spike in a particular action.

This query uses COUNT(*), GROUP BY, and ORDER BY to find the top 100 most frequent API calls.

SELECT
    proto_payload.audit_log.method_name AS api_method,
    proto_payload.audit_log.service_name AS service,
    resource.type AS resource_type,
    COUNT(*) AS action_count
FROM
    `Your_Log_View`
WHERE
    timestamp >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
    AND log_id = "cloudaudit.googleapis.com/data_access"
GROUP BY
    api_method,
    service,
    resource_type
ORDER BY
    action_count DESC
LIMIT 100;

(This query is adapted from the Google Cloud documentation on "SQL queries for security insights".)

Example 2: Hunting for Malicious User Agents

Web server logs contain the user-agent string for every request, which identifies the client browser or tool. Attackers often use automated tools like sqlmap or nmap that have distinctive user agents. You can hunt for these tools using the LIKE operator.

SELECT
    client_ip,
    user_agent,
    COUNT(*) AS request_count
FROM
    web_access_logs
WHERE
    user_agent LIKE '%sqlmap%' OR user_agent LIKE '%nmap%'
GROUP BY
    client_ip,
    user_agent
ORDER BY
    request_count DESC;

This query identifies IP addresses that are using known hacking tools and tells you how many requests each one has made. This is a direct, actionable piece of intelligence derived from combining string matching with aggregation.

Example 3: Detecting Anomaly in API Usage

Let's consider a more advanced example. What if you want to find users whose API activity today is significantly higher than their own personal average? This is a form of anomaly detection. While the query below uses some advanced window functions we haven't formally covered, you should be able to understand the core logic. It calculates a running average and standard deviation for each user's daily activity and flags days where the activity is more than three standard deviations above the average.

This demonstrates the ultimate goal of these techniques: moving from simple counting to sophisticated, statistics-based threat hunting.

SQL queries for security insights | Cloud Logging

This document from Google Cloud contains powerful, real-world examples of SQL for security analysis.

Take a look at the query under the heading "Identify high API usage by a principal". Notice how it combines aggregation (COUNT, AVG, STDDEV), date/string manipulation (EXTRACT), and pattern matching (LIKE) to perform anomaly detection. You are not expected to write this from scratch yet, but read the query and its explanation to see how these building blocks can be assembled into a powerful threat detection tool.


Conclusion

This lesson concludes our module on data manipulation and analysis. You have moved from simple data retrieval to complex analysis, equipping yourself with a powerful set of SQL skills directly applicable to cybersecurity.

Key Takeaways:

  • String functions (SUBSTRING, LOWER, CONCAT, etc.) are essential for parsing, cleaning, and extracting data from text-based log fields.
  • The LIKE operator with wildcards (%, _) is your primary tool for pattern matching in strings, perfect for hunting for indicators of compromise (IoCs) in log data.
  • By combining aggregate functions (COUNT, AVG), grouping (GROUP BY), filtering (WHERE, HAVING), and string manipulation, you can build powerful analytical queries to establish baselines, find anomalies, and hunt for specific threats.

We have focused on analyzing data within a single table. However, security data is often spread across multiple tables. For instance, a log file might give you a suspicious user ID, but to find that user's name, role, and department, you need to look in a separate users table. In our next module, we will tackle this exact challenge by learning how to use JOIN clauses to combine data from multiple tables, unlocking a much deeper level of analysis.