Query Details
// Detecting Port Scanning on Internet-Facing Devices // Are you aware of when your endpoints or servers are exposed to the internet? Whether intentional or accidental, it’s crucial to have the capability to detect port scans or attacks and take necessary defensive actions. I’ve developed a custom DefenderXDR detection KQL that provides this visibility for your fleet of MDE devices. // When this custom detection is triggered, you should verify if the device firewall is enabled, check for any critical or high vulnerabilities that could be exploited, and identify the hostile country performing the scan (e.g., Russia - something might be off!). This is an enhanced version of the KQL function InboundExternalNetworkEvents(X), where X is the DeviceID. let InternetFacingDevice= DeviceInfo | where Timestamp > ago(1h) | where IsInternetFacing | summarize arg_max(Timestamp, *) by DeviceId | project DeviceId; DeviceNetworkEvents | where DeviceId has_any(InternetFacingDevice) | where ActionType == @"InboundConnectionAttempt" | where not(ipv4_is_private(RemoteIP)) | extend IPLocation = geo_info_from_ip_address(RemoteIP) | summarize Connections=count() by RemoteIP, tostring(IPLocation.country) | sort by Connections desc | where Connections >= 3 // MITRE ATT&CK // T1049: System Network Connections Discovery
This KQL query is designed to detect port scanning activities on devices that are exposed to the internet. Here's a simplified breakdown of what the query does:
Identify Internet-Facing Devices:
DeviceInfo table for devices that have been marked as "internet-facing" within the last hour.DeviceIds.Monitor Network Events:
DeviceNetworkEvents table for any inbound connection attempts to these identified internet-facing devices.Analyze and Summarize Connections:
Additional Context:
Overall, this query helps in identifying suspicious network activities that could indicate a port scan on devices exposed to the internet, allowing for timely defensive actions.

Steven Lim
Released: October 16, 2024
Tables
Keywords
Operators