A Python SDK and CLI tool for managing Azure Sentinel resources. This is a Python port of the original PowerShell AzSentinel module.
PyAZSentinel provides a comprehensive Python interface for managing Azure Sentinel resources including:
- Alert Rules: Create, read, update, delete, and manage alert rules
- Hunting Rules: Manage hunting rules for threat detection
- Incidents: View and update security incidents
- Data Connectors: Manage data source connections
- Alert Rule Actions: Configure automated responses
- Import/Export: Support for JSON and YAML configuration files
pip install pyazsentinelgit clone https://github.com/misterapol/PyAZSentinel.git
cd PyAZSentinel
pip install -e .pip install -e ".[dev]"PyAZSentinel uses Azure Identity for authentication. You can authenticate using:
-
Azure CLI (recommended for development):
az login
-
Service Principal (recommended for automation):
from pyazsentinel import AzureSentinelClient client = AzureSentinelClient( subscription_id="your-subscription-id", workspace_name="your-workspace-name", tenant_id="your-tenant-id", client_id="your-client-id", client_secret="your-client-secret" )
-
Managed Identity (for Azure resources):
from pyazsentinel import AzureSentinelClient client = AzureSentinelClient( subscription_id="your-subscription-id", workspace_name="your-workspace-name" )
from pyazsentinel import AzureSentinelClient
# Initialize client
client = AzureSentinelClient(
subscription_id="your-subscription-id",
workspace_name="your-workspace-name"
)
# List all alert rules
alert_rules = client.alert_rules.list()
# Get specific alert rule
rule = client.alert_rules.get("rule-name")
# Create new alert rule from JSON
with open("alert_rule.json", "r") as f:
rule_config = json.load(f)
new_rule = client.alert_rules.create(rule_config)
# List incidents
incidents = client.incidents.list()
# Update incident
client.incidents.update(
incident_id="incident-id",
status="Closed",
classification="BenignPositive"
)The CLI provides command-line access to all functionality:
# List alert rules
pyazsentinel alert-rules list --workspace-name "my-workspace"
# Get specific alert rule
pyazsentinel alert-rules get --workspace-name "my-workspace" --rule-name "my-rule"
# Create alert rule from JSON file
pyazsentinel alert-rules create --workspace-name "my-workspace" --file "rule.json"
# Import multiple alert rules
pyazsentinel alert-rules import --workspace-name "my-workspace" --file "rules.json"
# List hunting rules
pyazsentinel hunting-rules list --workspace-name "my-workspace"
# List incidents
pyazsentinel incidents list --workspace-name "my-workspace"
# Export all configurations
pyazsentinel export --workspace-name "my-workspace" --output-dir "./backup"PyAZSentinel supports the same JSON and YAML formats as the original PowerShell module:
{
"Scheduled": [
{
"displayName": "Suspicious Process Execution",
"description": "Detects suspicious process execution patterns",
"severity": "Medium",
"enabled": true,
"query": "SecurityEvent | where EventID == 4688",
"queryFrequency": "PT1H",
"queryPeriod": "PT1H",
"triggerOperator": "GreaterThan",
"triggerThreshold": 0
}
],
"Fusion": [],
"MLBehaviorAnalytics": [],
"MicrosoftSecurityIncidentCreation": []
}Scheduled:
- displayName: "Suspicious Process Execution"
description: "Detects suspicious process execution patterns"
severity: "Medium"
enabled: true
query: "SecurityEvent | where EventID == 4688"
queryFrequency: "PT1H"
queryPeriod: "PT1H"
triggerOperator: "GreaterThan"
triggerThreshold: 0- ✅ Create, read, update, delete alert rules
- ✅ Support for all rule types (Scheduled, Fusion, ML Behavior Analytics, etc.)
- ✅ Import/export functionality
- ✅ Enable/disable rules
- ✅ Rule validation
- ✅ Create, read, update, delete hunting rules
- ✅ Import/export functionality
- ✅ Query validation
- ✅ List and filter incidents
- ✅ Update incident status and classification
- ✅ Add comments to incidents
- ✅ Incident analytics
- ✅ List available data connectors
- ✅ Configure data source connections
- ✅ Import connector configurations
- ✅ JSON format support
- ✅ YAML format support
- ✅ Bulk operations
- ✅ Configuration validation
-
Clone the repository:
git clone https://github.com/misterapol/PyAZSentinel.git cd PyAZSentinel -
Create virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install development dependencies:
pip install -e ".[dev]" -
Install pre-commit hooks:
pre-commit install
# Run all tests
pytest
# Run with coverage
pytest --cov=pyazsentinel
# Run specific test file
pytest tests/test_alert_rules.py# Format code
black pyazsentinel tests
# Sort imports
isort pyazsentinel tests
# Type checking
mypy pyazsentinelIf you're migrating from the PowerShell AzSentinel module, here's the mapping:
| PowerShell Function | Python SDK | CLI Command |
|---|---|---|
Get-AzSentinelAlertRule |
client.alert_rules.get() |
pyazsentinel alert-rules get |
New-AzSentinelAlertRule |
client.alert_rules.create() |
pyazsentinel alert-rules create |
Import-AzSentinelAlertRule |
client.alert_rules.import_from_file() |
pyazsentinel alert-rules import |
Get-AzSentinelIncident |
client.incidents.get() |
pyazsentinel incidents get |
Update-AzSentinelIncident |
client.incidents.update() |
pyazsentinel incidents update |
Export-AzSentinel |
client.export_all() |
pyazsentinel export |
This project is licensed under the MIT License - see the LICENSE file for details.
- Original PowerShell module by Wortell
- Azure Sentinel team for the underlying APIs
- Python community for excellent libraries and tools