Semantic YARA DSL - Construction-time validation
Project description
yara-dsl
Semantic YARA DSL - Construction-time validation for malware detection rules
A pure-Python domain-specific language for building guaranteed-valid YARA rules with semantic validation, cross-reference checking, and domain knowledge about PE/ELF files.
Overview
yara-dsl prevents impossible malware detection rules by validating at construction time rather than runtime. It provides type-safe string definitions (Text, Hex, Regex) with automatic validation and catches logic errors before compilation.
Key Features
- Construction-time validation - Errors caught before rule generation
- Cross-reference checking - Undefined string variables detected automatically
- Domain knowledge - PE/ELF impossibility rules blocked (e.g., >50 sections)
- Type-safe strings - Text, Hex, and Regex patterns with built-in validation
- Pure Python - Zero external dependencies, works everywhere
- Metadata validation - Required author and description fields
- Performance guards - Catches overly broad regex patterns and excessive string counts
Installation
pip install yara-dsl
Quick Start
from yara_dsl import Rule, Text, Hex, Regex
# Create a validated ransomware detection rule
rule = (Rule("LockBit_Detection")
.meta(
author="security-team@example.com",
description="Detects LockBit variants"
)
.strings(
Text("a", "LockBit", modifiers="nocase fullword"),
Hex("b", "52 41 4E 53 4F 4D 57 41 52 45"),
Regex("c", r"[a-f0-9]{32}@lockbit\.io")
)
.condition("all of them")
)
# Generates guaranteed-valid YARA
yara_source = rule.compile()
print(yara_source)
Validation Examples
yara-dsl catches errors that raw YARA would accept:
| Invalid Pattern | Issue |
|---|---|
Text("ab") |
Text too short (minimum 4 characters) |
$z in condition, undefined |
Undefined string reference |
Regex(".*") |
Performance risk pattern |
pe.sections > 50 |
Impossible PE characteristic |
| Less than 3 strings | Insufficient pattern matching |
| No metadata | Missing required author/description |
API Reference
Rule Class
Rule(name)- Create a new rule.meta(**kwargs)- Add metadata.strings(*string_objs)- Add string patterns (max 20).condition(expr)- Set detection condition.semantic_lint()- Validate before compiling.compile()- Generate YARA source code
String Types
Text(id, value, modifiers="")- Plain text strings (min 4 chars)Hex(id, value, modifiers="")- Hexadecimal patternsRegex(id, value, modifiers="")- Regular expressions (min 8 chars, no .*)
Example: Error Handling
from yara_dsl import Rule, Text, Severity
try:
rule = Rule("MyRule")
rule.strings(Text("a", "x")) # Too short!
except ValueError as e:
print(f"Error: {e}")
Use Cases
- SOC teams building detection rules for new malware variants
- Threat intelligence teams creating standardized rule repositories
- Security research requiring validated pattern matching
- Automated rule generation pipelines needing safety guarantees
License
MIT - See LICENSE
Built for security teams and threat intelligence researchers.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file yara_dsl-0.0.0.tar.gz.
File metadata
- Download URL: yara_dsl-0.0.0.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.13.7 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a00d6ae04d01fb8fe7ce7a89b4e4fdb31c899728b7496077454afa71d18e3fe8
|
|
| MD5 |
8d0f164b28da6731b46a73bc391e7cec
|
|
| BLAKE2b-256 |
3641e4a4a55827b13aee0025634600ee2aec3fe920bc096b2c4b0fd077865019
|
File details
Details for the file yara_dsl-0.0.0-py3-none-any.whl.
File metadata
- Download URL: yara_dsl-0.0.0-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.13.7 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
866b805174d7eff64afe29e60c0d165bf2af0f7460d0c592c0aee19315103efd
|
|
| MD5 |
d5e23c8fcdd19bb97abffc9132ee121c
|
|
| BLAKE2b-256 |
bd559a6ef1efac5269157d0d4bf9fec6ff7e4ad040bc641ba5e3f91bd0fea235
|