π‘οΈ Automated Security Assessment & KQL Query Generation for Microsoft Defender
| Feature | Description |
|---|---|
| π€ GitHub Copilot Agent | Custom agent for intelligent KQL query generation in VS Code & GitHub web |
| π Advanced Hunting Automation | Runs customizable security checks via Microsoft Graph API |
| π Real-Time Console Logs | Easy-to-read PowerShell output with color-coded results |
| π Styled HTML Report | Professional assessment report for sharing |
| π Secure Authentication | Uses Azure Service Principal with Microsoft Graph API |
| π§° Plug-and-Play | Just configure secrets, add queries, and run |
Generates comprehensive security assessment reports using Advanced Hunting queries across all Microsoft Defender products:
- Microsoft Defender for Endpoint
- Microsoft Defender for Cloud Apps
- Microsoft Defender for Identity
- Microsoft Defender for Office 365
- Microsoft Defender XDR
Add this to your workflow (.github/workflows/defender-scout.yml):
name: Defender Scout Report
on:
workflow_dispatch:
schedule:
- cron: '0 6 * * 1' # Weekly on Monday at 6 AM UTC
jobs:
report:
runs-on: ubuntu-latest
steps:
- name: π‘οΈ Run Microsoft Defender Scout
uses: samikroy/ms-defender-scout@v1
with:
azure_client_id: ${{ secrets.AZURE_CLIENT_ID }}
azure_client_secret: ${{ secrets.AZURE_CLIENT_SECRET }}
azure_tenant_id: ${{ secrets.AZURE_TENANT_ID }}
# report_path: 'my-custom-report.html' # optional, default: ms-defender-scout-report.html| Input | Required | Default | Description |
|---|---|---|---|
azure_client_id |
β | β | Service Principal Application (client) ID |
azure_client_secret |
β | β | Service Principal client secret |
azure_tenant_id |
β | β | Azure AD Tenant ID |
report_path |
β | ms-defender-scout-report.html |
Output path for the HTML report |
| Output | Description |
|---|---|
report_path |
Path to the generated HTML report |
The report is also automatically uploaded as a build artifact (retained 90 days).
.
βββ .github/
β βββ agents/
β β βββ Defender Scout KQL agent.md # π€ GitHub Copilot Agent
β βββ workflows/
β βββ generate-report.yml # GitHub Actions workflow
βββ scripts/
β βββ generate-ms-defender-scout-report.ps1 # Main report generator
β βββ kql-queries.txt # Advanced Hunting queries library
βββ ms-defender-scout-report.html # Sample generated report
βββ KQL-EXAMPLES.md # 20+ example KQL queries
βββ LICENSE # MIT License
βββ README.md # You're reading it
The Defender Scout KQL Agent is a specialized GitHub Copilot Agent that works in both VS Code and GitHub web interface!
In VS Code:
- Open GitHub Copilot Chat:
Ctrl+Shift+I(Windows/Linux) orCmd+Shift+I(Mac) - Start chatting with the agent:
@workspace Generate a KQL query to find devices with critical vulnerabilities
@workspace Create a threat hunting query for PowerShell downloads
@workspace Validate this query: DeviceInfo | where OSPlatform == "Windows"
@workspace Optimize my query for better performance
@workspace Show me devices that haven't updated in 30 days
On GitHub Web:
- Comment on issues or pull requests
- Use the same
@workspacecommands to get KQL queries generated instantly!
| Capability | Description |
|---|---|
| Generate KQL Queries | Natural language β Production-ready queries |
| Validate Syntax | Check queries for errors and issues |
| Optimize Performance | Improve query speed and efficiency |
| Explain Queries | Get plain English explanations |
| Threat Hunting | Security-focused query generation |
| Best Practices | Built-in Microsoft Defender security patterns |
The agent comes with 8 quick-start prompts:
- Generate Device Query
- Threat Hunting
- Vulnerability Check
- Alert Analysis
- Email Security
- Query Optimization
- Explain Query
- Identity Attacks
Find devices with outdated software:
DeviceInfo
| where Timestamp > ago(1d)
| where OSPlatform == "Windows"
| where OSBuild < "19045" // Windows 10 22H2
| project DeviceName, OSVersion, OSBuild, LastSeen
| order by LastSeen descDetect PowerShell download attempts:
DeviceProcessEvents
| where Timestamp > ago(24h)
| where FileName =~ "powershell.exe"
| where ProcessCommandLine has_any ("DownloadString", "WebClient", "IEX")
| project Timestamp, DeviceName, AccountName, ProcessCommandLine
| order by Timestamp desc
| take 100Define KQL Queries β Authenticate via Graph API β Run Advanced Hunting β Generate HTML Report
The report generator:
- Authenticates using Service Principal credentials
- Reads KQL queries from
scripts/kql-queries.txt - Executes each query against Microsoft Graph Advanced Hunting API
- Formats results into a styled HTML report
- Displays summary in console with color-coded status
Service Principal Setup:
The solution requires a Service Principal with SecurityEvents.Read.All or ThreatHunting.Read.All permissions in Microsoft Graph API.
-
Register an Azure AD application:
az ad app create --display-name "MS Defender Scout"
-
Create a service principal:
az ad sp create --id <app-id>
-
Assign Microsoft Graph API permissions:
- Go to Azure Portal β Azure Active Directory β App registrations
- Select your app β API permissions β Add permission
- Choose Microsoft Graph β Application permissions
- Add:
SecurityEvents.Read.AllorThreatHunting.Read.All - Grant admin consent
-
Create a client secret:
- Go to Certificates & secrets β New client secret
- Save the secret value securely
Option 1: GitHub Actions (Recommended)
-
Clone this repository
git clone https://github.com/samikroy/ms-defender-scout.git cd ms-defender-scout -
Configure Repository Secrets
Go to:
Settings β Secrets and variables β Actions β New repository secretAdd these secrets:
AZURE_CLIENT_ID- Your Service Principal Application (client) IDAZURE_CLIENT_SECRET- Your Service Principal client secretAZURE_TENANT_ID- Your Azure AD Tenant ID
-
Review the Workflow Schedule
Check:
.github/workflows/generate-report.ymlDefault schedule: Runs weekly on Monday at 6 AM UTC
Modify the cron expression to change frequency:
schedule: - cron: '0 6 * * 1' # Every Monday at 6 AM UTC
-
Run the Workflow
- Go to Actions tab
- Select "Generate MS Defender Scout Report"
- Click "Run workflow"
- Download the HTML report from artifacts
Option 2: Run Locally
-
Set environment variables:
$env:AZURE_CLIENT_ID = "your-client-id" $env:AZURE_CLIENT_SECRET = "your-client-secret" $env:AZURE_TENANT_ID = "your-tenant-id"
-
Run the script:
.\scripts\generate-ms-defender-scout-report.ps1
-
View the report:
.\ms-defender-scout-report.html
Edit scripts/kql-queries.txt to add your own Advanced Hunting queries.
Format: Query Title >> KQL Query
Example:
Device Inventory >> DeviceInfo | summarize Count=count() by DeviceName, OSPlatform | sort by Count desc
Advanced Hunting supports these tables:
- Device tables: DeviceInfo, DeviceNetworkInfo, DeviceProcessEvents, DeviceNetworkEvents, DeviceFileEvents, DeviceRegistryEvents, DeviceLogonEvents, DeviceImageLoadEvents, DeviceEvents
- Email tables: EmailEvents, EmailAttachmentInfo, EmailUrlInfo, EmailPostDeliveryEvents
- Identity tables: IdentityLogonEvents, IdentityQueryEvents, IdentityDirectoryEvents
- Alert tables: AlertInfo, AlertEvidence
- Cloud App tables: CloudAppEvents
- Vulnerability tables: DeviceTvmSoftwareVulnerabilities, DeviceTvmSecureConfigurationAssessment
See Advanced Hunting schema documentation for complete details.
- GitHub Copilot Agent - VS Code agent configuration for KQL query generation
- KQL Examples - 20+ ready-to-use queries for common security scenarios
- KQL Quick Reference - Microsoft official docs
- Advanced Hunting Best Practices - Optimization guide
- Microsoft Defender Schema - Complete table reference
- Never commit secrets to the repository
- Use GitHub Secrets or Azure Key Vault for credentials
- Rotate Service Principal secrets regularly
- Apply principle of least privilege for API permissions
- Review and audit query results regularly
| Issue | Solution |
|---|---|
| Authentication fails | Verify Service Principal credentials and API permissions |
| Query returns no data | Check if your tenant has data in the queried tables |
| Permission denied | Ensure admin consent is granted for Graph API permissions |
| Workflow fails | Check GitHub Actions logs for detailed error messages |
| Agent not responding | Ensure GitHub Copilot extension is installed and enabled in VS Code |
Have ideas for improvements? Contributions are welcome!
- Issues: Report bugs or request features
- Pull Requests: Submit improvements or new query templates
- Questions: Open a GitHub issue for support
MIT License - Β© 2026 Samik Roy
See LICENSE for details.
β Star this repo if you find it useful!