Skip to content

About

Python-based SOC threat detection lab using Sigma-style rules and log analysis

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

# Threat Detection Lab

A small Python log-analysis tool that scans a log file with Sigma-style YAML detection rules and saves every match to a JSON alert report.

I built this project to learn how detection rules work: how a rule is structured, how it's matched against log data, and how the matches are turned into alerts an analyst can review.

---

## How It Works


logs/sample.log  ->  rules/\*.yml  ->  src/detector.py  ->  reports/threat\_report.json

   (log data)       (detections)      (matching engine)        (alert report)

1. src/rule\_loader.py loads every .yml rule in the rules/ folder.

2. src/detector.py reads logs/sample.log line by line and checks each line against each rule's message|contains keywords.

3. Each matching line becomes an alert with the rule title, severity, and the original log line as evidence.

4. All alerts are written to reports/threat\_report.json along with the scan time and total alert count.

Adding a new detection only requires adding a new YAML file; no code changes are needed.

---

## Detection Rules

The rules follow the structure of [Sigma](https://github.com/SigmaHQ/sigma), the open standard for writing SIEM detection rules: title, ID, status, description, log source, detection logic, and severity level.

| Rule | File | Matches log lines containing | Severity |

|---|---|---|---|

| Multiple Failed Login Attempts Detected | rules/failed\_login\_detection.yml | Failed login | Medium |

| Malware Detection Alert | rules/malware\_detection.yml | Malware | High |

Example rule:

title: Multiple Failed Login Attempts Detected

id: 001

status: experimental

description: Detects repeated failed login attempts that may indicate brute force activity.

logsource:

  product: windows

  service: security

detection:

  selection:

    message|contains:

      - "Failed login"

  condition: selection

level: medium

---

## Example Run

The sample log contains normal logins for alice and bob, three failed logins for admin, one failed login for root, and a malware signature alert.


> python src/detector.py

=== Threat Detection Lab ===



Analysis Complete

Alerts Found: 5

Report: reports/threat\_report.json

Excerpt from reports/threat\_report.json:

{

    "scan\_time": "2026-08-02 14:33:45.202121",

    "log\_file": "logs\\\\sample.log",

    "detections": \[

        {

            "rule": "Multiple Failed Login Attempts Detected",

            "severity": "medium",

            "log": "2026-08-02 09:17:10 WARNING Failed login for user admin"

        },

        {

            "rule": "Malware Detection Alert",

            "severity": "high",

            "log": "2026-08-02 09:18:42 ERROR Malware signature detected"

        }

    ],

    "total\_alerts": 5

}

From an analyst's point of view, the report shows that admin was targeted three times in about 20 seconds, which is worth investigating as possible password guessing, and that the malware alert should be escalated first because it has the highest severity.

---

## Skills Demonstrated

- **Detection rule writing:** creating Sigma-style rules with log sources, detection logic and severity levels

- **Log analysis:** reading application and authentication logs and spotting suspicious events

- **Alert generation:** turning rule matches into structured alerts with evidence

- **Python scripting:** file parsing, YAML loading and JSON report output

- **Modular design:** keeping detection logic in rule files, separate from the code

---

## Tools and Technologies

- Python 3

- PyYAML

- YAML (Sigma-style rule format)

- JSON (report output)

- Git and GitHub

---

## Getting Started

Requires Python 3. Run the commands from the project's root folder, because the scripts use relative paths.

git clone https://github.com/Eyaisu1463/threat-detection-lab.git

cd threat-detection-lab



python -m venv .venv

.\\.venv\\Scripts\\Activate.ps1      # Mac/Linux: source .venv/bin/activate



pip install -r requirements.txt

python src/detector.py

---

## Project Structure


threat-detection-lab/

├── logs/

│   └── sample.log               # Sample log data

├── reports/

│   ├── threat\_report.json       # Generated alert report

│   └── threat\_report.txt        # Summary of failed logins by user

├── rules/

│   ├── failed\_login\_detection.yml

│   └── malware\_detection.yml

├── src/

│   ├── detector.py              # Detection engine

│   └── rule\_loader.py           # Loads YAML rules

├── requirements.txt

└── README.md

---

## Limitations

This is a learning project, so the matching engine is intentionally simple:

- It supports only the message|contains keyword match. Other Sigma features such as condition logic, logsource filtering and other field modifiers are not processed.

- Matching is case-sensitive.

- The failed login rule alerts on every failed login. It doesn't count attempts per user or use a time window, so a single failed login also creates an alert.

- The sample log is small and hand-written, not exported from a real system.

## Next Steps

- Add a threshold so the failed login rule only fires after several failures for the same user within a time window

- Map each rule to a MITRE ATT&CK technique

- Make matching case-insensitive and support more Sigma modifiers

- Test the rules against real Windows or Linux logs

---

## Related Project

[Windows SOC Lab](https://github.com/Eyaisu1463/windows-soc-lab) builds on this project with Windows Security Event IDs, MITRE ATT&CK mapping, and written incident investigations.

About

Python-based SOC threat detection lab using Sigma-style rules and log analysis

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages