Log Parsing
Scripts
Learn to write simple scripts that parse log files and extract indicators of compromise to defend networks and investigate incidents.
What Are Log Files?
Every system, application, and network device generates log files: timestamped records of events that happen during operation. They are the digital breadcrumbs that tell us what happened, when, and to whom.
Anatomy of a Log Line
Indicators of Compromise (IOCs)
An Indicator of Compromise (IOC) is any observable artifact in a log that suggests a system may have been breached or attacked. Parsing scripts search for these patterns automatically.
The Log Parsing Pipeline
Core Technique: String Splitting
The simplest parsing approach: split each line by a delimiter (space, comma, tab) to extract individual fields. This works well for structured logs with consistent formatting.
Pattern Matching with Regex
When log formats are complex or inconsistent, regular expressions (regex) let you define precise patterns to search for. Think of regex as a search template with wildcards.
Splitting vs Regex
| String Splitting | Regular Expressions | |
|---|---|---|
| Complexity | Simple, beginner-friendly | Steeper learning curve |
| Best For | Consistent delimiters (CSV, TSV) | Complex, variable formats |
| Flexibility | Limited: breaks if format changes | High: handles variations |
| Performance | Faster on simple data | Slightly slower, more powerful |
| Example Use | CSV logs, syslog | IP extraction, timestamp parsing |
How to Write a Parsing Script
Example 1: Count Requests Per IP
Goal: find which IP addresses are making the most requests. A high request count from a single IP may indicate automated scanning or a denial-of-service attempt.
Example 2: Detect Failed Logins
Goal: flag IPs with repeated 401 (Unauthorized) responses. More than 10 failures from one IP strongly suggests a brute-force attack.
Example 3: Extract IOCs with Regex
Goal: find all requests containing SQL injection patterns like UNION SELECT, OR 1=1, or single-quote characters in the URL path.
Combining Techniques
Key Data Structures
Choosing the right data structure determines how efficiently your script aggregates and searches through log data.
Common Mistakes
Parsing Techniques Summary
| Technique | When to Use | IOC Examples |
|---|---|---|
| split() | Clean, consistent delimiters | IP counts, status codes |
| regex findall() | Extract specific patterns | IP addresses, email addresses |
| regex search() | Detect presence of a pattern | SQLi strings, XSS payloads |
| Dictionary counting | Aggregate and threshold | Brute-force detection |
| Set membership | Compare against known-bad list | Threat intel IP matching |
Question 1 of 5
Which Python method is best suited for breaking a log line into individual fields when the format uses a consistent space delimiter?
Question 2 of 5
A security analyst finds that IP 10.0.5.22 has generated 847 requests in the last hour while all other IPs average 12. What type of IOC does this most likely indicate?
Question 3 of 5
In the following code, what does ip_counts.get(ip, 0) return if the IP has never been seen before?
Question 4 of 5
Why is it a common mistake to use readlines() to load an entire log file at once?
Question 5 of 5
A parsing script uses the regex pattern \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}. What does this pattern match?