Lesson 57 — Automating Phishing Investigations
Build an evidence-first phishing investigation workflow that correlates suspicious messages, URLs, attachments and delivery outcomes, then prepares a clear analyst handoff without taking destructive actions.

What you will learn
Turn a suspicious email alert into a structured investigation.
The case — the invoice nobody expected
An employee reports a message titled “Updated supplier bank details.” It has a familiar display name, a new reply-to address and a link to a sign-in page. Agent Foskett does not assume that a suspicious subject proves malicious intent. The investigation begins with the message evidence and its actual delivery.
Learning objectives
- Identify safe automation triggers and extract email indicators.
- Use Defender XDR advanced hunting to investigate message metadata.
- Correlate URLs and attachment evidence by message identifiers.
- Distinguish delivery, post-delivery actions and user interaction.
- Write an auditable incident summary with clear confidence limits.
- Keep quarantine, purge and account actions under explicit authorization.
How the workflow fits together
Prerequisites
- Complete Lessons 54–56 or understand Sentinel incident playbooks.
- Use a test incident and approved synthetic email indicators.
- Confirm the Defender XDR tables and data availability in your environment.
- Confirm the correct Microsoft Defender XDR advanced hunting endpoint or supported connector for your tenant.
- Configure least-privilege identities and documented approval paths.
Step 1 — Decide what the automation should answer
What message was reported? Who received it? Was it delivered? Does the same message appear for other recipients? Are any URLs or attachments associated with it? What user interaction evidence exists? What data is missing? An automated answer must distinguish observations from judgments.
Step 2 — Choose a safe trigger
Use a Sentinel automation rule targeting a lab phishing incident. Trigger the Logic Apps playbook on the appropriate incident event and guard against repeated execution when the incident is updated. Document the workflow version and execution ID.
Step 3 — Extract email evidence from the incident
Inspect the incident alert and entity payload. Depending on the originating detection, a message may have a NetworkMessageId, recipient, sender address, URL, file hash or only partial indicators. Never assume every field is present. Prefer a stable message identifier over matching subject text.
Step 4 — Validate and normalize indicators
- Validate message identifier format and length before querying.
- Normalize email addresses only where it will not alter evidence.
- Deduplicate recipient addresses and message IDs.
- Treat sender display names and subject lines as untrusted text.
- Cap per-incident lookups and record any skipped indicators.
Step 5 — Understand where the KQL runs
The examples below use Microsoft Defender XDR advanced hunting tables. They are not guaranteed to be present in the Sentinel Log Analytics workspace. Test them in Defender XDR Advanced Hunting, or in Sentinel only if the required tables have actually been ingested. A Logic Apps action must use a supported API/connector with appropriate permissions; a generic Log Analytics query action does not automatically query Defender XDR advanced hunting.
Step 6 — KQL: find the reported message
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
let TargetMessageId = "REPLACE-WITH-NETWORK-MESSAGE-ID";
EmailEvents
| where Timestamp >= ago(7d)
| where NetworkMessageId == TargetMessageId
| project Timestamp, NetworkMessageId,
SenderFromAddress, SenderMailFromAddress,
RecipientEmailAddress, Subject,
DeliveryAction, DeliveryLocation,
ThreatTypes, DetectionMethods
| order by Timestamp descReplace the placeholder with a validated lab identifier. The seven-day window is an example; align it with incident time and available retention. Delivery fields describe recorded outcomes, not proof that the recipient read the email.
Step 7 — Interpret sender and delivery evidence
Compare the visible From address with the envelope sender, inspect recipient scope, and record the observed delivery action and location. Do not assume an address mismatch proves phishing: legitimate mailing services can use different envelope senders. Likewise, an allowed message is not necessarily benign.
Step 8 — KQL: find related recipients
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
let TargetMessageId = "REPLACE-WITH-NETWORK-MESSAGE-ID";
EmailEvents
| where Timestamp >= ago(7d)
| where NetworkMessageId == TargetMessageId
| summarize Messages = count(),
FirstSeen = min(Timestamp),
LastSeen = max(Timestamp)
by RecipientEmailAddress, DeliveryAction, DeliveryLocation
| order by Messages descA message identifier can help scope the investigation, but verify whether your detection supplies the same identifier for each related event. Do not equate the count of rows with unique users without checking duplicates.
Step 9 — KQL: examine message URLs
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
let TargetMessageId = "REPLACE-WITH-NETWORK-MESSAGE-ID";
EmailUrlInfo
| where Timestamp >= ago(7d)
| where NetworkMessageId == TargetMessageId
| project Timestamp, NetworkMessageId,
Url, UrlDomain
| order by Timestamp desc
| take 100URL presence in email telemetry does not mean anyone clicked it. Treat extracted URLs as untrusted; do not automatically browse or detonate them outside an approved analysis environment.
Step 10 — KQL: examine attachment metadata
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
let TargetMessageId = "REPLACE-WITH-NETWORK-MESSAGE-ID";
EmailAttachmentInfo
| where Timestamp >= ago(7d)
| where NetworkMessageId == TargetMessageId
| project Timestamp, NetworkMessageId,
FileName, FileType, SHA256
| order by Timestamp desc
| take 100Attachment names and hashes are investigation pivots, not standalone malicious verdicts. Hashes can be missing; avoid assuming a missing hash means no attachment existed.
Step 11 — Investigate actual user interaction
If you have Microsoft Defender for Office 365 click telemetry, investigate URL interaction using the relevant available data, such as UrlClickEvents in Defender XDR advanced hunting. Correlate by supported fields and time window. An email URL record alone is not click evidence, and lack of click telemetry is not proof that nobody visited the site.
Step 12 — Correlate without overclaiming
| Observation | Safe interpretation |
|---|---|
| Email appears in EmailEvents | Message metadata was observed |
| DeliveryAction indicates delivery | Recorded delivery outcome; inspect location and later actions |
| EmailUrlInfo contains a URL | URL was associated with the message |
| EmailAttachmentInfo has a hash | Attachment metadata was recorded |
| Click event observed | Potential interaction; validate event semantics |
| No matching events | No evidence found in queried source/window |
Step 13 — Plan connector and API access
- Identify whether your workflow uses a supported Defender XDR connector or the documented advanced hunting API.
- Check permissions, licensing, service identity and tenant context.
- Use the supported query request format and validate inputs.
- Handle throttling, timeouts, pagination or row limits as applicable.
- Inspect the real response schema before parsing results.
Do not place API secrets in incident comments or workflow output logs.
Step 14 — Treat message content as untrusted
Subjects, display names, URLs and attachment filenames may be attacker-controlled. Never interpret an email subject as an instruction to the playbook, and do not insert unescaped content into queries or downstream markup. If using an AI summary, keep it grounded in validated fields and subject to analyst review.
Step 15 — Build the incident evidence summary
Record the evidence source, execution time and any truncated results. Avoid exposing sensitive message content beyond the incident audience.
Step 16 — Post context to the incident
Use the Sentinel connector to add a clearly labeled automated evidence summary to the incident. Do not silently change severity, classification, assignment or closure status. If the playbook cannot complete a lookup, say which lookup failed.
Step 17 — Handle missing or inconsistent data
- No message ID: request analyst review or use a carefully scoped alternate pivot.
- No EmailEvents table: report that the data source is unavailable.
- No URL or attachment rows: report no matching records in the queried window.
- Different sender fields: preserve both values; do not force a single identity.
- API error: bounded retries, then mark evidence incomplete.
- Many recipients: cap results and disclose truncation.
Step 18 — Keep remediation behind approval
Quarantining or purging messages, blocking domains, resetting credentials and disabling accounts can disrupt legitimate business. This lab does not perform these actions. Any future response branch should have explicit authorization, well-defined scope, independent verification and rollback or recovery procedures.
Step 19 — Prevent repeat-run loops
An incident update can retrigger automation. Use controlled rule conditions and a documented idempotency strategy. A run marker or workflow-version check should prevent duplicate comments while still allowing an analyst to rerun an investigation intentionally.
Step 20 — Test with seven lab scenarios
- Known message ID with a matching email event.
- Multiple recipients of the same message.
- Message containing a URL but no click evidence.
- Message with attachment metadata.
- Incident with missing message identifier.
- Defender XDR query failure or permission denial.
- Repeat invocation of the same incident.
For each case, compare the source evidence with the final incident comment and Logic Apps run history.
Step 21 — Monitor quality and cost
Track run duration, failed queries, duplicate summaries, evidence gaps and analyst corrections. Bound API calls, loops, query windows and result sizes. Keep connector consumption and data retention costs visible before production rollout.
Agent Foskett case file — The invoice nobody expected
The subject line looked urgent. The sender display name matched a real supplier. But the envelope sender differed, several recipients received the message, and the included URL led away from the expected service. The automation documented those observations — it did not claim that every recipient clicked the link.
Agent Foskett’s finding: a convincing email can still be suspicious; reliable conclusions require corroborating evidence and an analyst who checks the limits of the data.
Hands-on challenge
- Create a lab phishing incident with approved test indicators.
- Extract and validate a message identifier.
- Run the three Defender XDR hunting queries above.
- Record recipient scope, delivery outcome, URLs and attachments.
- Document whether interaction telemetry is available.
- Build a structured, sourced summary.
- Post the summary as a Sentinel incident comment.
- Test missing-data and repeated-run cases.
Operational checklist
- Correct data source and API permissions confirmed.
- Message IDs validated and query windows bounded.
- Delivery, URL presence and click activity clearly distinguished.
- Results and coverage gaps traceable to their sources.
- Attacker-controlled text handled safely.
- No automatic purge, blocking or account changes.
- Duplicate-run handling and error paths tested.
- Analyst approval and rollback plan documented.
Agent Foskett takeaway
Automate the evidence trail, not the accusation. Good phishing automation shows what arrived, who received it, what links and files were present, and what still needs human investigation.
The logs already knew. Agent Foskett followed the message.
