Query Details

Matching Domain Redirectors From Urlclickevents Table With Netapi External Threat Intel Source

Query

let NetAPIDomains = externaldata (Domain:string) ["https://netapi.com/api2/netapi-compromised-domains.txt"] with (format="txt")
| where isnotempty(Domain) and Domain !startswith "#"
| distinct Domain;
UrlClickEvents
| where ActionType == "ClickAllowed" // Click has been allowed by SafeLinks
| extend UrlChain = todynamic(UrlChain)
| mv-expand UrlChain
| extend UrlString = tostring(UrlChain)
| extend Host = tolower(extract(@"^(?:[A-Za-z][A-Za-z0-9+.-]*://)?(?:[^@/?#]*@)?([^/:?#\s]+)", 1, UrlString))
| where isnotempty(Host)
| extend Labels = split(Host, ".")
| mv-expand Start = range(0, array_length(Labels) - 2) to typeof(int) // the host and each parent domain
| extend Domain = strcat_array(array_slice(Labels, Start, -1), ".")
| join kind=inner NetAPIDomains on Domain
| project-away Domain1
| summarize arg_min(Start, *) by ReportId, UrlString // one row per URL, the longest listed match

About this query

Explanation

This query is designed to identify potentially malicious domain redirectors by analyzing user click events from emails, Teams messages, and Office 365 apps. It works by examining the URLs users click on, specifically looking at the sequence of redirects (UrlChain) that occur. The query extracts the hostnames from these URLs and checks them against a list of compromised domains provided by NetAPI, an external threat intelligence source.

Here's a simplified breakdown of the query:

  1. Load Compromised Domains: It retrieves a list of compromised domains from NetAPI, filtering out any empty entries or comments.

  2. Filter Click Events: It looks at URL click events where the action type is "ClickAllowed," meaning the click was permitted by SafeLinks.

  3. Extract and Expand URLs: The query processes each URL in the UrlChain to extract the hostnames, converting them to lowercase for consistency.

  4. Domain Matching: It splits the hostnames into parts to check not only the exact host but also any parent domains against the NetAPI list.

  5. Join and Filter: It matches these domains with the NetAPI list to find any that are compromised.

  6. Summarize Results: Finally, it summarizes the results to show the longest matching domain for each URL, providing a lead for further investigation.

This query helps security teams identify potential threats by flagging domains that might be involved in phishing or other malicious activities, allowing them to take appropriate action.