Agent Foskett Academy • KQL Academy • Module 12 • Lesson 144 • Endpoint Investigation

Lesson 144 — The File Appeared Seconds Before It Executed

The process tree from Lesson 143 gave us another clue. A file appears on disk only seconds before it is executed.

That timing matters. In this lesson we use DeviceFileEvents to identify when the file was created, which process created it, where it was written and what hash identifies it. We then pivot back into DeviceProcessEvents to test whether the same file actually executed and whether it appears elsewhere in the estate.

The file did not just exist. It arrived, then executed. That sequence is the investigation.
Agent Foskett KQL Academy file creation and execution investigation
Your case file

A DLL is written to a temporary path on WS-FIN-042. Seconds later the same hash appears in process execution telemetry.

✓ Find newly created files
✓ Identify the creating process
✓ Correlate hash with execution
✓ Pivot the hash across devices

Case briefing

CASE FILE Device: WS-FIN-042 01:22:16 — cmd.exe active ↓ 01:22:18 — payload.dll created ↓ 01:22:19 — payload.dll executed / loaded by the attack chain ↓ THE QUESTION Where did the file come from, which process created it, and does it appear anywhere else?

Investigation objective

Use DeviceFileEvents and DeviceProcessEvents to correlate file creation with subsequent execution, preserve the file hash and originating process, and determine whether the file belongs to a larger endpoint incident.

Investigator's rule

Creation is not execution. A file event proves that Defender observed file-system activity. A process event proves execution-related telemetry. Correlate the two rather than assuming one proves the other.

Stage 1 — find files created around the suspicious process

Start with the device and narrow time window from the process-tree investigation. Microsoft documents DeviceFileEvents as the advanced hunting table for file creation, modification and other file-system events.

01-find-new-files-around-process.kql
123456789101112131415
let TargetDevice = "WS-FIN-042";
let TargetTime = datetime(2026-08-18 01:22:19);
DeviceFileEvents
| where DeviceName =~ TargetDevice
| where Timestamp between (TargetTime - 5m .. TargetTime + 5m)
| where ActionType == "FileCreated"
| project Timestamp,
          DeviceName,
          FileName,
          FolderPath,
          SHA1,
          InitiatingProcessFileName,
          InitiatingProcessCommandLine,
          InitiatingProcessId
| order by Timestamp asc

Keep the creator

The file itself is only half the story. InitiatingProcessFileName, its command line and process ID can identify what wrote the file to disk.

Keep the hash

A SHA-1 hash gives the investigator a stronger pivot than filename alone. Filenames can be changed or reused; the hash helps relate the same file content across events and devices when available.

Stage 2 — follow the file's own history

Once the suspicious filename is known, look at every observed file event for that file on the device. This can show whether it was created, modified, renamed or otherwise touched before execution.

02-follow-file-history.kql
1234567891011121314
let TargetDevice = "WS-FIN-042";
let TargetFile = "payload.dll";
DeviceFileEvents
| where DeviceName =~ TargetDevice
| where FileName =~ TargetFile
| project Timestamp,
          ActionType,
          FileName,
          FolderPath,
          SHA1,
          InitiatingProcessFileName,
          InitiatingProcessCommandLine,
          InitiatingProcessId
| order by Timestamp asc

Path changes meaning

A file appearing under a user's temporary directory, downloads area or another writable path can deserve different scrutiny from the same filename under a normal application directory. Path is context, not a verdict.

ActionType matters

Do not treat every DeviceFileEvents row as a creation. Preserve ActionType so the report accurately describes what Defender observed.

Stage 3 — correlate the file hash with process execution

Now pivot the SHA-1 into DeviceProcessEvents. If the same hash appears as a process image on the same device shortly afterwards, the evidence becomes much stronger.

03-correlate-file-hash-with-execution.kql
123456789101112131415
let TargetDevice = "WS-FIN-042";
let TargetSHA1 = "0123456789abcdef0123456789abcdef01234567";
DeviceProcessEvents
| where DeviceName =~ TargetDevice
| where SHA1 == TargetSHA1
| project Timestamp,
          FileName,
          FolderPath,
          ProcessCommandLine,
          AccountName,
          ProcessId,
          InitiatingProcessFileName,
          InitiatingProcessCommandLine,
          InitiatingProcessId
| order by Timestamp asc

Why hash correlation helps

The filename may differ between telemetry sources or be changed after arrival. Matching file content by hash provides a more resilient way to connect creation and execution.

Still keep time and device

Hash equality connects the file content. Timestamp and device context tell you whether the events belong to the same incident sequence.

Stage 4 — pivot the hash across the estate

The file hash is now an investigative entity. Ask whether the same content has been observed on other devices during the hunting window.

04-pivot-file-hash-across-devices.kql
123456789101112
let TargetSHA1 = "0123456789abcdef0123456789abcdef01234567";
DeviceFileEvents
| where Timestamp > ago(7d)
| where SHA1 == TargetSHA1
| summarize FirstSeen=min(Timestamp),
            LastSeen=max(Timestamp),
            Events=count(),
            Devices=make_set(DeviceName, 50),
            Paths=make_set(FolderPath, 50),
            Creators=make_set(InitiatingProcessFileName, 25)
          by SHA1, FileName
| order by FirstSeen asc

One device or several?

If the same hash appears across multiple endpoints, the incident may be broader than the original workstation. Compare creation time, path and initiating process before assuming every occurrence has the same cause.

Prevalence is context

A widely seen hash can belong to legitimate software. A very rare hash can still be benign. The value comes from combining prevalence with path, signer, source process, execution and surrounding activity.

Stage 5 — build the file evidence timeline

Finish by laying the hash-related file events out chronologically across devices. This gives you the first-seen time and shows how the file moved or appeared through the environment.

05-build-file-evidence-timeline.kql
12345678910111213
let TargetSHA1 = "0123456789abcdef0123456789abcdef01234567";
DeviceFileEvents
| where Timestamp > ago(7d)
| where SHA1 == TargetSHA1
| project Timestamp,
          DeviceName,
          ActionType,
          FileName,
          FolderPath,
          InitiatingProcessFileName,
          InitiatingProcessCommandLine,
          InitiatingProcessId
| order by Timestamp asc

Creation can answer “how”

If PowerShell, a browser, archive tool or another suspicious process created the file, the creation event can connect the file to the earlier execution chain.

Execution answers “what happened next”

Process telemetry can show whether the file became active code. Together, file and process evidence can establish an arrival-to-execution sequence much more clearly than either table alone.

Agent Foskett's file timeline

01:22:11 powershell.exe ↓ 01:22:16 cmd.exe ↓ 01:22:18 DeviceFileEvents payload.dll CREATED C:\Users\j.smith\AppData\Local\Temp\payload.dll SHA1 captured ↓ 01:22:19 DeviceProcessEvents matching file/hash appears in execution telemetry ↓ HASH PIVOT Check other devices ↓ NEXT QUESTION What did the process connect to?
The file event explained how it arrived. The process event explained what happened next.

Your evidence board

EvidenceWhat it supportsWeight
FileCreated eventDefender observed the file being created on the device.Strong for creation
Initiating process is part of the suspicious chainConnects file arrival to earlier execution activity.Strong
SHA-1 capturedProvides a stable pivot for the same file content.Strong
Matching hash appears in process telemetry seconds laterSupports a creation-to-execution relationship.Strong
Same hash found on another deviceMay indicate wider distribution or legitimate common software.Context requiring validation
Suspicious filename aloneDoes not establish maliciousness.Insufficient alone

Write the finding like an investigator

Example: Microsoft Defender XDR file telemetry recorded payload.dll being created on WS-FIN-042 shortly after the suspicious PowerShell and command-shell activity identified in the process tree. The file event preserved the file path, SHA-1 and initiating-process context. Process telemetry was then queried using the same SHA-1 to determine whether the file content appeared in execution-related activity on the device. The close timing and shared hash support a relationship between file arrival and subsequent execution; however, the file's maliciousness should be assessed using the complete process, network, prevalence and file-reputation context.

Lesson 144 key takeaways

  • DeviceFileEvents records file creation, modification and other file-system activity.
  • Preserve ActionType so you describe the observed file event accurately.
  • The initiating process can explain where a file came from.
  • Filename and path provide context but are not proof of maliciousness.
  • SHA-1 is a valuable pivot for relating the same file content across telemetry.
  • Creation does not prove execution.
  • Use DeviceProcessEvents to test whether the file content appears in process execution telemetry.
  • Time + device + hash can turn separate rows into one arrival-to-execution sequence.
  • Pivot suspicious hashes across the estate to determine scope.
  • Prevalence is investigative context, not a verdict.

Module 12 — the attack chain continues

Lesson 143 reconstructed the suspicious process tree. Lesson 144 now connects that execution chain to a file that appeared immediately before it became active. Next we follow the process into network telemetry and ask where it communicated.

Next: Lesson 145 — The Process Connected to an External IP.

Continue your KQL investigation training

Module 12 follows endpoint evidence from suspicious execution through process relationships, files, network activity, persistence and spread.

Related Agent Foskett Investigations

Continue with endpoint investigations where file creation, process execution and timing reveal how suspicious code arrived and became active.

🔎 KQL Academy — Module 12: Advanced Endpoint Investigation

Following the attack chain from the first suspicious process to a defensible endpoint compromise assessment.

Correlate file creation and process execution with KQL

Lesson 144 of the Agent Foskett KQL Academy uses Microsoft Defender XDR DeviceFileEvents and DeviceProcessEvents to investigate a file that appears on disk immediately before execution.

Investigate file hashes and endpoint execution in Microsoft Defender XDR

Learn how to preserve file paths, SHA-1 hashes and initiating-process evidence, correlate file-system activity with process execution and pivot suspicious file content across devices to determine incident scope.