
Threat-hunting notebook that detects DLL search-order hijacking in Sysmon logs with Python and pandas, with matching KQL queries for Microsoft Defender XDR and Sentinel.
A threat-hunting notebook that detects DLL search-order hijacking in Windows Sysmon logs using Python and pandas, with each detection also written in KQL for Microsoft Defender XDR and Microsoft Sentinel. Built as hands-on practice for the Microsoft SC-200 (Security Operations Analyst) certification.
T1574.001/atomic_red_team/windows-sysmon.logPython, pandas, matplotlib, seaborn, ElementTree (built-in XML parser), Google Colab, and KQL for Microsoft Defender XDR advanced hunting.
What: Loaded pandas, NumPy, matplotlib and seaborn, and turned off warning messages. Why: pandas is the main tool for working with the log as a table; the others support analysis and charts.
What: Downloaded the Sysmon log from the Splunk attack_data repository and saved it locally, then checked the file was the real log and not a Git LFS pointer. Why: The repository stores large files in Git LFS, which can return a small placeholder file instead of the data. Checking first avoids analysing the wrong file.
What: Wrapped all events in a single root tag, parsed the file with Python's built-in ElementTree parser, and turned each event into one row. The event ID, time and computer came from the System section, and every <Data Name="..."> field in EventData became its own column. The result was a table of 79 events and 19 columns.
Why: The log holds one XML event per line with no root element, so it isn't valid XML until wrapped. Windows events also use an XML namespace, which had to be handled or the searches would silently find nothing. Flattening the nested XML into columns makes it possible to filter and group the data.
What: Converted the event time to a datetime and sorted chronologically, counted event types, listed the processes, users and hosts involved, measured the time span, and grouped DLL loads by their top-level folder. Why: Scoping comes first in any investigation. It showed the incident was small: one process, one user, one host, about 0.3 seconds. The folder breakdown established a baseline of normal locations and exposed 2 loads from a user profile folder among 77 from Windows system folders.
What: Wrote a regular expression matching folders a normal user can write to (AppData, Temp, Downloads, Desktop, Users\Public, ProgramData) and filtered for images loaded from them. It found updater.exe and amsi.dll, both in AppData\Roaming. Added the KQL equivalent for DeviceImageLoadEvents.
Why: Genuine Windows DLLs live in protected folders. Side-loading needs the attacker to place a DLL somewhere they can write. Both files were signed by Microsoft, which showed that location, not signature, was the useful signal.
What: Split each path into DLL name and folder, grouped by process and DLL name, and counted how many distinct folders each DLL came from. amsi.dll was loaded by the same process from both System32 and AppData\Roaming in the same millisecond, with identical hashes. Added the KQL equivalent using summarize, dcount and make_set.
Why: A program normally loads a given DLL from one place, so a second location is the fingerprint of search-order hijacking and far more specific than Detection A. The matching hashes showed the planted file was a genuine Microsoft copy, consistent with a test proving the technique without a payload.
What: First compared each file name with its built-in OriginalFileName across all rows, which gave 22 hits, 21 of them harmless naming quirks. Then narrowed the check to the row where the process loads its own executable, which revealed that updater.exe was originally PowerShell.EXE. Added the KQL equivalent for DeviceProcessEvents, limited to commonly abused built-in Windows tools.
Why: Renaming a file doesn't change the original name compiled into it, so a mismatch exposes disguised programs. Attackers rename tools like PowerShell to evade rules that watch for them by name. Narrowing the check turned a noisy rule into a single true positive.
What: Tested a naive "unsigned DLL" rule, which produced 14 alerts. All 14 turned out to be .NET native images (*.ni.dll), which are compiled locally and therefore unsigned. Tuned the rule by excluding only that specific folder, reducing the alerts to 0. Added the KQL equivalent, joining DeviceFileCertificateInfo for signature data.
Why: To show why "unsigned" alone is a weak detection: it was noisy and still missed the real attack, which used genuinely signed files. It also demonstrates good tuning practice: excluding the narrowest known-good item rather than a whole folder like C:\Windows, which would create a blind spot.
What: Listed the Sysmon events a full investigation needs and checked which were present: only Event 7 was. Wrote KQL pivot queries for Defender XDR covering who launched the program, who dropped the files, and whether it connected anywhere afterwards. Why: Event 7 proves the side-load happened but not how the files arrived or what happened next. Identifying missing evidence and knowing where to find it in Defender is a core analyst skill.
What: Combined the three detections into one table showing which detections fired on each file, ranked by the number of agreeing signals, then wrote the findings, MITRE mapping, confidence assessment and recommended response.
Why: Several independent signals agreeing produce a reliable alert. The two attacker files each triggered two detections, while the genuine System32 amsi.dll triggered only one and was explained as legitimate.
A renamed copy of Windows PowerShell (updater.exe) was placed in C:\Users\Administrator\AppData\Roaming next to a copy of amsi.dll. Because Windows searches the program's own folder first, the process loaded the AppData amsi.dll instead of the genuine one in System32.
| MITRE ATT&CK technique | Evidence |
|---|---|
| T1574.001 – Hijack Execution Flow: DLL | amsi.dll loaded from AppData next to the program |
| T1036.003 – Masquerading: Rename System Utilities | updater.exe has OriginalFileName PowerShell.EXE |
| T1562.001 – Impair Defenses (potential) | Targeting AMSI, which antivirus uses to scan PowerShell |
Confidence: high that DLL hijacking occurred (three independent detections agree); low that a malicious payload ran, since the planted DLL matches the genuine Microsoft file's hash.