Lesson 59 — Automating Endpoint Investigation Workflows
Build an evidence-first endpoint investigation that connects Microsoft Sentinel incidents to Defender XDR device telemetry, correlates process and network activity, and prepares a safe analyst handoff.

What you will learn
Build a controlled endpoint investigation playbook.
Agent Foskett case file — The quiet workstation
A Sentinel incident references a workstation that made an unexpected outbound connection. The alert does not identify whether the connection came from a browser, an updater, a scheduled task or malicious code. Agent Foskett builds an automated evidence pack before recommending any response.
Learning objectives
- Extract and validate device entities from Sentinel incidents.
- Understand which Defender XDR endpoint tables are available and where they can be queried.
- Investigate process execution and network connections with bounded KQL.
- Connect evidence collection to a Logic Apps workflow using supported connectors and permissions.
- Post a source-aware incident summary and leave containment to an authorized analyst.
Architecture — from incident to endpoint evidence
Prerequisites and data-source boundary
- Lessons 53–58 or equivalent Sentinel automation experience.
- A test Sentinel incident with a known lab device.
- Microsoft Defender for Endpoint telemetry available through Defender XDR advanced hunting, or a validated path to the relevant data in your Sentinel environment.
- Permission to run approved hunting queries and to write a Sentinel incident comment.
- A read-only workflow: no automated device isolation, file quarantine or account action.
Important: the KQL below uses Defender XDR advanced hunting tables. Their presence in a Sentinel Log Analytics workspace is not guaranteed. Verify your specific Defender portal, Sentinel integration and query connector before automating.
Step 1 — Define the investigation question
What device generated the signal? Which process initiated the activity? Which remote endpoint was contacted? What happened before and after? An IP address or filename alone cannot establish malicious intent.
Step 2 — Duplicate a tested lab playbook
- Copy the Lesson 58 Logic App to
AF-LAB-Endpoint-Investigation. - Retain the tested incident trigger and its least-privilege identity.
- Document the device evidence source and supported connector or API path.
- Keep only incident comment output enabled for the first test.
- Record ownership, workflow version and rollback steps.
Step 3 — Extract device entities
Inspect the Sentinel incident entity payload with a controlled run. A device may be represented by a hostname, Azure resource identifier or Defender device identifier, depending on the alert and connector. Do not assume all incidents carry the same fields.
Step 4 — Resolve the correct device
- Prefer a verified stable Defender device identifier when available.
- Use hostname as an investigation lead, not an inherently unique key.
- Confirm tenant and device context before cross-system queries.
- Deduplicate repeated entities and cap the number processed.
- Flag missing or ambiguous device mappings for analyst review.
Step 5 — Establish an evidence window
Start with a 24-hour lab lookback and record its UTC start and end. For production, align the window with the alert timestamp, telemetry retention and ingestion delays. Clearly distinguish incident creation time from the actual endpoint event time.
Step 6 — KQL: recent processes on the device
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
let TargetDevice = "LAB-WORKSTATION";
DeviceProcessEvents
| where Timestamp >= ago(24h)
| where DeviceName =~ TargetDevice
| project Timestamp, DeviceName, FileName,
ProcessCommandLine, AccountName,
InitiatingProcessFileName, SHA1
| order by Timestamp desc
| take 200Run this in Defender XDR advanced hunting where the table is available. Replace the example device with a verified lab device. Command lines may contain sensitive information; minimize what you export into incident comments.
Step 7 — Interpret process evidence
Review parent process, command line, account and hash alongside normal device behavior. A process name alone is weak evidence: legitimate tools can be abused, and unfamiliar binaries may be approved software. Confirm telemetry coverage before inferring that an event did not occur.
Step 8 — KQL: investigate outbound connections
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
let TargetDevice = "LAB-WORKSTATION";
DeviceNetworkEvents
| where Timestamp >= ago(24h)
| where DeviceName =~ TargetDevice
| project Timestamp, DeviceName,
InitiatingProcessFileName,
RemoteIP, RemotePort, RemoteUrl,
Protocol, ActionType
| order by Timestamp desc
| take 200Review the destination and initiating process together. A remote address may belong to shared infrastructure, and RemoteUrl may be empty. The 200-row cap means this is a sample, not a complete connection history.
Step 9 — Build a common timeline
Use UTC timestamps, the verified device identity and process context to align execution and network events. A process and connection occurring close together is an investigative lead, not proof of causation. Review available process identifiers and other event details before linking events.
Step 10 — KQL: summarize connection patterns
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
let TargetDevice = "LAB-WORKSTATION";
DeviceNetworkEvents
| where Timestamp >= ago(24h)
| where DeviceName =~ TargetDevice
| summarize Connections = count(),
FirstSeen = min(Timestamp),
LastSeen = max(Timestamp)
by InitiatingProcessFileName,
RemoteIP, RemotePort
| order by Connections desc
| take 100This query identifies patterns worth investigating; connection count is not a threat score. Use exact device identifiers and additional pivots when the hostname is not unique.
Step 11 — Identify the supported automation query path
The Sentinel incident trigger and Defender XDR advanced hunting are distinct surfaces. Choose a documented, authorized connector or API path that supports advanced hunting in your environment. Confirm permissions, query limits and output schema. Do not assume the Azure Monitor Logs action can query Defender XDR advanced hunting tables directly.
Step 12 — Validate dynamic inputs
- Validate the device identifier and reject unexpected characters or structures.
- Use documented safe input handling for the selected query action.
- Keep time windows, result limits and query execution bounded.
- Never concatenate arbitrary incident text into executable queries.
- Restrict access to raw command lines, usernames and device details.
Step 13 — Parse query results defensively
Inspect a real test response before using JSON parsing or expressions. The connector may return zero rows, nested records or an error object. Distinguish no matching records from query unavailable and query failed.
Step 14 — Avoid premature attribution
Even when a process name, hash and remote IP are available, attribution remains uncertain. A hash match is a file identifier, not a full behavior assessment; a network connection is an observation, not proof of data theft.
Step 15 — Produce an evidence-only summary
Preserve the query source and time window. Do not paste large raw process command lines or sensitive user information into an incident comment.
Step 16 — Add the summary to Sentinel
Use the supported incident comment action. Mark the comment as automated context and include the workflow version and execution identifier. Leave incident severity, status, ownership and classification unchanged in this lab.
Step 17 — Prevent duplicate runs
An incident comment or update can trigger another automation. Use a tested idempotency marker, supported trigger conditions or a controlled state check. Verify that repeated runs do not produce a comment loop.
Step 18 — Handle failure and partial evidence
| Condition | Safe handling |
|---|---|
| No device entity | Record missing input; do not guess |
| Ambiguous hostname | Require verified device mapping |
| Hunting API denied | Record unavailable source |
| Telemetry absent | Report coverage gap |
| Timeout or rate limit | Bound retries; mark partial result |
| Excessive event volume | Cap output and disclose truncation |
| Repeated incident update | Apply tested deduplication |
Step 19 — Test controlled scenarios
- One valid device with process and network records.
- Device with no matching records in the time window.
- Incident with two device entities.
- Missing or ambiguous device identifier.
- Query permission failure.
- High-volume telemetry exceeding the row cap.
- Repeated invocation against the same incident.
Check the Logic Apps run history and the resulting Sentinel incident comment for every test.
Step 20 — Permissions, privacy and retention
Restrict hunting read permissions and Sentinel comment write permissions to the minimum required. Endpoint command lines and account names can expose sensitive details. Apply your organization’s access, retention and logging controls to workflow inputs and outputs.
Step 21 — Cost and performance controls
Limit device loops, time windows, returned columns and result counts. Monitor query duration, API throttling and Logic Apps action consumption. Start with a lab incident before enabling broader automation.
Agent Foskett case file — The updater nobody recognized
The endpoint playbook reports a newly observed process and a connection to a remote service. At first glance, the pairing looks suspicious. The analyst checks the signed binary, software deployment records, process ancestry and destination ownership. The activity is consistent with an approved updater, but the incident remains open until the supporting evidence is recorded.
Finding: automation makes the investigation faster when it preserves context instead of declaring a verdict.
Hands-on challenge
- Duplicate the tested playbook as
AF-LAB-Endpoint-Investigation. - Extract and validate a device entity.
- Run all three KQL examples in Defender XDR advanced hunting.
- Document the supported query path for your automation.
- Collect bounded process and network context.
- Write an evidence-only incident comment.
- Test missing-data and duplicate-run handling.
- List two additional analyst pivots before containment.
Quality and publishing checklist
- Device identity verified and ambiguous matches handled.
- Defender XDR hunting data source clearly distinguished from Sentinel Logs.
- All three KQL editors full width with line numbers.
- Queries use bounded time windows and row caps.
- Partial evidence and telemetry gaps explicitly disclosed.
- Connector permissions, privacy and retry limits checked.
- No automated isolation, quarantine or incident closure.
- Canonical, navigation, background, footer and scripts verified.
Agent Foskett takeaway
Automate the evidence trail, not the containment decision. A device name, process and connection can start an investigation, but only validated context makes the next action defensible.
The logs already knew. Agent Foskett checked which process made the call.
