|
| 1 | +""" |
| 2 | +Security alerts — list, triage, and update Microsoft 365 Defender |
| 3 | +alerts programmatically. |
| 4 | +
|
| 5 | +Security operations teams use this to: |
| 6 | + - List active alerts across the tenant |
| 7 | + - Filter by severity, status, or detection source |
| 8 | + - Update alert classification, determination, and status |
| 9 | + - Automate triage (e.g. auto-resolve low-severity known issues) |
| 10 | +
|
| 11 | +Requires delegated permission ``SecurityAlert.ReadWrite.All``. |
| 12 | +
|
| 13 | +https://learn.microsoft.com/en-us/graph/api/resources/alert |
| 14 | +""" |
| 15 | + |
| 16 | +from datetime import datetime, timedelta, timezone |
| 17 | + |
| 18 | +from office365.graph_client import GraphClient |
| 19 | +from tests import test_client_id, test_client_secret, test_tenant |
| 20 | + |
| 21 | + |
| 22 | +def main(): |
| 23 | + client = GraphClient(tenant=test_tenant).with_client_secret(test_client_id, test_client_secret) |
| 24 | + |
| 25 | + alerts = client.security.alerts_v2 |
| 26 | + |
| 27 | + # -- Step 1: list recent active alerts -- |
| 28 | + since = (datetime.now(timezone.utc) - timedelta(days=3)).isoformat() |
| 29 | + all_alerts = alerts.get().execute_query() |
| 30 | + print(f"Total alerts: {len(all_alerts)}\n") |
| 31 | + |
| 32 | + # Filter by status |
| 33 | + active = [a for a in all_alerts if a.properties.get("status") in ("new", "inProgress")] |
| 34 | + resolved = [a for a in all_alerts if a.properties.get("status") == "resolved"] |
| 35 | + |
| 36 | + print(f"Active: {len(active)}") |
| 37 | + print(f"Resolved: {len(resolved)}\n") |
| 38 | + |
| 39 | + if not active: |
| 40 | + print("No active alerts. Showing recent alerts:\n") |
| 41 | + |
| 42 | + for a in (active or all_alerts[:10]): |
| 43 | + alert_id = a.id or "?" |
| 44 | + title = a.properties.get("title", "(untitled)") |
| 45 | + severity = a.properties.get("severity", "?") |
| 46 | + status = a.properties.get("status", "?") |
| 47 | + created = a.properties.get("createdDateTime", "?") |
| 48 | + if isinstance(created, datetime): |
| 49 | + created = created.strftime("%m-%d %H:%M") |
| 50 | + |
| 51 | + detection = a.properties.get("detectionSource", a.properties.get("serviceSource", "?")) |
| 52 | + |
| 53 | + print(f" [{severity:7s}] [{status:10s}] {title}") |
| 54 | + print(f" id={alert_id[:20]} source={detection} created={created}") |
| 55 | + |
| 56 | + # Show tags if any |
| 57 | + tags = a.properties.get("tags", []) |
| 58 | + if tags: |
| 59 | + print(f" tags: {', '.join(tags)}") |
| 60 | + print() |
| 61 | + |
| 62 | + # -- Step 2: update an alert (triage) -- |
| 63 | + if active: |
| 64 | + target = active[0] |
| 65 | + alert_id = target.id |
| 66 | + title = target.properties.get("title", "(untitled)") |
| 67 | + print(f"Updating alert: {title}") |
| 68 | + |
| 69 | + # Available classification values: |
| 70 | + # unknown, falsePositive, truePositive, informationalExpectedActivity |
| 71 | + # |
| 72 | + # Available determination values: |
| 73 | + # unknown, apt, malware, phishing, securityPersonnel, securityTesting |
| 74 | + # unwantedSoftware, other, multiStagedAttack, compromisedAccount |
| 75 | + # maliciousUserActivity, notEnoughDataToValidate |
| 76 | + # |
| 77 | + # Available status values: |
| 78 | + # unknown, new, inProgress, resolved |
| 79 | + |
| 80 | + target.set_property("classification", "informationalExpectedActivity") |
| 81 | + target.set_property("determination", "securityTesting") |
| 82 | + target.set_property("status", "inProgress") |
| 83 | + target.set_property("comment", "Reviewed — security testing, investigating") |
| 84 | + target.update().execute_query() |
| 85 | + print(f" ✓ Updated: classification/status modified for {title}") |
| 86 | + |
| 87 | + # -- Step 3: create a custom alert (for security workflows) -- |
| 88 | + # Alerts can also be created, for example from a SOAR playbook: |
| 89 | + # |
| 90 | + # from office365.directory.security.alerts.alert import Alert |
| 91 | + # new_alert = Alert(client.context) |
| 92 | + # new_alert.set_property("title", "Custom alert from automation") |
| 93 | + # new_alert.set_property("severity", "medium") |
| 94 | + # new_alert.set_property("status", "new") |
| 95 | + # new_alert.set_property("category", "CustomAutomation") |
| 96 | + # client.security.alerts_v2.add(new_alert) |
| 97 | + # client.execute_query() |
| 98 | + # print("Custom alert created.") |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + main() |
0 commit comments