Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

27 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Detection as Code - Sigma to Wazuh

A comprehensive Detection as Code (DaC) repository that converts vendor-agnostic Sigma detection rules into Wazuh-compatible XML format with automated CI/CD pipelines.

Status: Production Ready | License: NopelCyber | Wazuh Version: 4.7.0+


🎯 Overview

This repository implements a complete Detection as Code workflow:

Sigma Rules (YAML) β†’ Validate β†’ Convert to Wazuh XML β†’ Deploy to Wazuh Manager

Key Features

  • βœ… Vendor-Agnostic Detection Rules - Write once in Sigma format, deploy anywhere
  • βœ… Automated Conversion - Python-based converter transforms Sigma β†’ Wazuh XML
  • βœ… Continuous Integration - GitHub Actions validates, converts, and deploys automatically
  • βœ… REST API Deployment - JWT token-based secure deployment to Wazuh Manager
  • βœ… Rule Versioning - Complete git history of all detection rules
  • βœ… Easy Maintenance - Never manually edit converted rules

πŸ“ Project Structure

detection-as-code/
β”œβ”€β”€ .github/
β”‚   └── workflows/              # GitHub Actions CI/CD pipelines
β”‚       β”œβ”€β”€ validate-rules.yml
β”‚       β”œβ”€β”€ convert-rules.yml
β”‚       └── deploy-rules.yml
β”œβ”€β”€ sigma-rules/                # Source Sigma detection rules (vendor-agnostic)
β”‚   β”œβ”€β”€ process_creation/
β”‚   β”œβ”€β”€ file_access/
β”‚   β”œβ”€β”€ registry_event/
β”‚   └── network_connection/
β”œβ”€β”€ converted-rules/
β”‚   └── wazuh/
β”‚       β”œβ”€β”€ rules/              # Auto-generated Wazuh XML rules (DO NOT EDIT)
β”‚       └── decoders/
β”œβ”€β”€ converters/
β”‚   └── sigma-to-wazuh.py       # Python converter script
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ validate.sh             # Validate Sigma rules syntax
β”‚   β”œβ”€β”€ convert-all.sh          # Convert Sigma to Wazuh XML
β”‚   └── deploy.sh               # Deploy rules to Wazuh Manager
β”œβ”€β”€ tests/
β”‚   └── test_rule_coverage.py   # Unit tests for rule conversion
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ README.md
β”‚   β”œβ”€β”€ contributing.md
β”‚   └── rule-writing-guide.md
β”œβ”€β”€ requirements.txt            # Python dependencies
β”œβ”€β”€ docker-compose.yml          # Local Wazuh testing environment
└── .gitignore

πŸš€ Quick Start

Prerequisites

  • Python 3.7+
  • Git
  • Bash shell
  • Curl (for API calls)
  • Wazuh Manager instance (4.7.0+)

1. Clone the Repository

git clone https://github.com/yourusername/detection-as-code.git
cd detection-as-code

2. Install Dependencies

pip install -r requirements.txt

3. Set Wazuh Credentials

Set environment variables for deployment:

export WAZUH_MANAGER_HOST="your-wazuh-url.com"
export WAZUH_JWT_TOKEN="your-jwt-token-here"

Or create a .env file:

cat > .env << 'EOF'
WAZUH_MANAGER_HOST=your-wazuh-url.com
WAZUH_JWT_TOKEN=your-jwt-token-here
EOF

source .env

4. Run the Complete Workflow

# Validate all Sigma rules
bash scripts/validate.sh

# Convert Sigma rules to Wazuh XML
bash scripts/convert-all.sh

# Deploy to Wazuh Manager
bash scripts/deploy.sh

πŸ“‹ Detailed Setup Instructions

Getting Your Wazuh JWT Token

# Authenticate to Wazuh API
curl -k -X POST "https://your-wazuh-url.com:55000/security/user/authenticate" \
  --header "Content-Type: application/json" \
  -d '{"username":"admin","password":"your_password"}'

# Response includes "token" field - copy and use it

Adding Your First Sigma Rule

Create a new file sigma-rules/process_creation/my_rule.yml:

title: Suspicious PowerShell Execution
id: 12345678-1234-1234-1234-123456789012
status: test
description: Detects suspicious PowerShell command execution
logsource:
    product: windows
    category: process_creation
detection:
    selection:
        CommandLine|contains:
            - 'powershell.exe'
            - 'Invoke-WebRequest'
        Image|endswith:
            - 'cmd.exe'
            - 'powershell.exe'
    condition: selection
falsepositives:
    - System administrators
    - Automated scripts
level: high
tags:
    - attack.execution
    - attack.t1059

Running the Conversion Pipeline

# 1. Validate YAML syntax and schema
bash scripts/validate.sh

# Output should show:
# πŸ” Validating Sigma rules...
# βœ… All validations passed!

# 2. Convert Sigma to Wazuh XML
bash scripts/convert-all.sh

# Output should show:
# πŸ”„ Converting Sigma rules to Wazuh format...
# βœ“ Converted: Suspicious PowerShell Execution
#   β†’ ./converted-rules/wazuh/rules/suspicious_powershell_execution.xml
# βœ… Conversion complete!

# 3. Deploy to Wazuh Manager
bash scripts/deploy.sh

# Output should show:
# πŸš€ Deploying rules to Wazuh Manager via API...
# Deploying rules to: https://your-wazuh-url.com:55000
# 
# πŸ“€ Uploading: suspicious_powershell_execution.xml (XXX bytes)
# Response Code: HTTP 200
# βœ“ Successfully deployed to: https://your-wazuh-url.com:55000/rules/files/...
# 
# πŸ“Š Deployment Summary:
#   βœ“ Deployed: 1
#   βœ— Failed: 0
# βœ… All rules deployed successfully!

πŸ“Š Sigma Rule Format

Rule Structure

title: Rule Title (Required)
id: unique-uuid-here (Required)
status: test|production|deprecated (Required)
description: Detailed description (Required)
logsource:
    product: windows|linux|macos|web
    category: process_creation|file_access|registry_event|network_connection
detection:
    selection:
        field|modifier: value
        field2: [value1, value2]
    condition: selection
falsepositives:
    - Common false positive scenario 1
    - Common false positive scenario 2
level: informational|low|medium|high|critical
tags:
    - attack.discovery
    - attack.t1087

Field Modifiers

CommandLine|contains: 'powershell'      # Contains string
Image|endswith: '.exe'                  # Ends with
CommandLine|startswith: 'cmd'           # Starts with
CommandLine|all: 
    - 'net.exe'
    - 'user'                            # Contains all strings

Rule Examples

Example 1: Process Creation Detection

title: Suspicious cmd.exe Execution with Encoded Command
id: 3a1234f5-a678-4321-b123-c4d5e6f7g8h9
status: production
description: Detects cmd.exe executed with encoded PowerShell commands
logsource:
    product: windows
    category: process_creation
detection:
    selection:
        Image|endswith: 
            - 'cmd.exe'
        CommandLine|contains:
            - 'powershell.exe -enc'
            - 'powershell.exe -e'
            - '/c powershell'
    condition: selection
level: high
falsepositives:
    - Legitimate admin scripts
tags:
    - attack.execution
    - attack.t1086

Example 2: File Access Detection

title: Suspicious File Access to SAM Database
id: 5b2345g6-b789-5432-c234-d5e6f7g8h9i0
status: production
description: Detects unauthorized access to Windows SAM registry file
logsource:
    product: windows
    category: file_access
detection:
    selection:
        TargetFilename|contains: 'SAM'
        Action: 'CreateKey'
    condition: selection
level: critical
falsepositives:
    - Authorized backup tools
tags:
    - attack.credential_access
    - attack.t1003

πŸ”§ Understanding the Conversion

How Sigma Rules Convert to Wazuh

Input Sigma Rule:

title: Clear Logs Process Execution
level: high
detection:
    selection:
        CommandLine|contains: 'wevtutil cl'
        Image|endswith: 'cmd.exe'
    condition: selection

Output Wazuh XML:

<group name="sigma">
  <rule id="600123" level="7">
    <decoded_as>json</decoded_as>
    <match>wevtutil cl</match>
    <match>cmd.exe</match>
    <description>Clear Logs Process Execution (from Sigma conversion)</description>
    <options>no_full_log</options>
  </rule>
</group>

Conversion Mapping

Sigma Level Wazuh Level
informational 2
low 3
medium 5
high 7
critical 15

Rule ID Generation

  • Rule IDs start at 600000+ to avoid conflicts with built-in Wazuh rules
  • IDs are generated from Sigma rule title hash (consistent across conversions)
  • Format: 600000 + abs(hash(title)) % 10000

πŸ€– Automated Deployment (GitHub Actions)

Setting Up GitHub Actions

  1. Create empty GitHub repository (without README)
  2. Push code to repository:
    git remote add origin https://github.com/yourusername/detection-as-code.git
    git branch -M main
    git push -u origin main
  3. Add GitHub Secrets:
    • Go to Settings β†’ Secrets and variables β†’ Actions
    • Click "New repository secret"
    • Add WAZUH_MANAGER_HOST (your Wazuh URL without https://)
    • Add WAZUH_JWT_TOKEN (your JWT token)

GitHub Actions Workflows

Validation Workflow (on every push):

Push β†’ Validate Sigma YAML syntax β†’ Check schema compliance

Conversion Workflow (on every push):

Push β†’ Validate β†’ Convert Sigma to Wazuh XML β†’ Generate artifacts

Deployment Workflow (on push to main only):

Push to main β†’ Validate β†’ Convert β†’ Deploy via Wazuh API β†’ Verify

Monitoring Workflows

View workflow runs:

  1. Go to GitHub repo β†’ Actions tab
  2. Click workflow name to see details
  3. Each job shows logs and status

πŸ§ͺ Testing Rules Locally

Using Docker

Run complete Wazuh stack locally:

# Start Wazuh Manager and Indexer
docker-compose up -d

# Wait for services to start (2-3 minutes)
sleep 180

# Check status
docker-compose ps

# Access Wazuh Dashboard
# URL: https://localhost
# Default credentials: admin/SecurePassword123

Validate Rule Syntax

# Check YAML syntax
python3 -c "import yaml; yaml.safe_load(open('sigma-rules/process_creation/my_rule.yml'))"

# Run full validation
bash scripts/validate.sh

View Converted Rules

# List all converted rules
ls -la converted-rules/wazuh/rules/

# View specific rule
cat converted-rules/wazuh/rules/my_rule.xml

# Count total rules
ls -1 converted-rules/wazuh/rules/ | wc -l

πŸ“ Best Practices

Writing Sigma Rules

  1. Be Specific - Use multiple conditions to reduce false positives
  2. Document Everything - Include clear descriptions and false positive scenarios
  3. Test Thoroughly - Validate against real logs before production deployment
  4. Version Control - Always commit with meaningful messages
  5. Tag Properly - Use MITRE ATT&CK tags for better organization
  6. Use Meaningful Titles - Rule titles should describe the detection clearly

Rule Management

  1. Never manually edit converted XML - Always modify the Sigma YAML source
  2. Use status field - Mark rules as test/production/deprecated
  3. Track false positives - Document common false positive scenarios
  4. Review before merge - Use pull requests for all changes
  5. Keep rules organized - Use proper directory structure by category

Deployment Strategy

  1. Test in non-prod first - Deploy to test environment before production
  2. Monitor after deployment - Check Wazuh logs for rule triggers
  3. Maintain backups - Keep version history in git
  4. Document changes - Use commit messages to track changes
  5. Regular audits - Review rule effectiveness quarterly

πŸ› Troubleshooting

Common Issues

Issue: "WAZUH_MANAGER_HOST not set"

# Solution: Set environment variable
export WAZUH_MANAGER_HOST="your-wazuh-url.com"
export WAZUH_JWT_TOKEN="your-token"

Issue: "XML syntax error" from Wazuh API

# Solution: Verify converted XML is valid
cat converted-rules/wazuh/rules/your_rule.xml
# Should start with <group> tag, NOT <?xml declaration>

Issue: "Invalid JWT Token"

# Solution: Regenerate token (tokens expire after 15 minutes)
curl -k -X POST "https://your-wazuh-url.com:55000/security/user/authenticate" \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"your_password"}'

Issue: "Connection refused"

# Solution: Verify Wazuh Manager is running and accessible
curl -k https://your-wazuh-url.com:55000/security/user/authenticate

Issue: GitHub Actions won't deploy

# Solution: Check that secrets are properly set
# Go to Settings β†’ Secrets and variables β†’ Actions
# Verify WAZUH_MANAGER_HOST and WAZUH_JWT_TOKEN are present

πŸ“š Additional Resources

Sigma Rule Documentation

Wazuh Documentation

Detection Engineering Resources


🀝 Contributing

How to Contribute

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/new-detection-rule)
  3. Add your Sigma rule to appropriate directory
  4. Test locally (bash scripts/validate.sh && bash scripts/convert-all.sh)
  5. Commit with clear message (git commit -m "Add: Detection for suspicious process X")
  6. Push to branch (git push origin feature/new-detection-rule)
  7. Create Pull Request with description of detection rule

PR Requirements

  • βœ… YAML syntax is valid
  • βœ… Rule is tested against real logs
  • βœ… Description is clear and detailed
  • βœ… False positives are documented
  • βœ… Commit message is descriptive

πŸ‘₯ Team

  • Project Lead: Asish Verma
  • Contributors: Security Engineering
  • Maintainer: NopelCyber

πŸ“ž Support

For issues, questions, or contributions:


πŸ”’ Security

  • Never commit credentials - Always use environment variables or GitHub Secrets
  • JWT tokens are sensitive - Rotate tokens regularly
  • Validate all inputs - Ensure Sigma rules are from trusted sources
  • Monitor deployments - Review Wazuh logs after each deployment

πŸ“Š Statistics

Metric Value
Total Sigma Rules N/A
Converted to Wazuh N/A
CI/CD Automation βœ… Active
Last Update TBD
Deployment Method REST API + JWT
Wazuh Version 4.7.0+

Last Updated: 2025-01-15 | Version: 1.0.0 | Status: Production Ready


Quick Command Reference

# Setup
pip install -r requirements.txt
source .env

# Development
bash scripts/validate.sh          # Validate rules
bash scripts/convert-all.sh       # Convert to Wazuh
bash scripts/deploy.sh            # Deploy to Wazuh

# Testing
docker-compose up -d              # Start local Wazuh
docker-compose down               # Stop local Wazuh

# Git
git add .
git commit -m "Add: rule description"
git push origin main

Happy Detection Engineering! πŸš€πŸ”’

About

Detection as Code: Sigma rules converted to Wazuh

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages