Credential Access (TA0006)(external, opens in a new tab or window)
text code block:FROM logs-network_traffic.flow-* | WHERE network.type == "ipv4" AND network.transport == "tcp" AND agent.id IS NOT NULL AND network.community_id IS NOT NULL AND event.start IS NOT NULL AND event.end IS NOT NULL AND source.ip != destination.ip AND source.mac != destination.mac // Orient each Ethernet leg by IP so reverse-direction reports describe the same path. | EVAL Esql.peer_a = LEAST(source.ip, destination.ip), Esql.peer_b = GREATEST(source.ip, destination.ip), Esql.vlan = MV_CONCAT(TO_STRING(network_traffic.flow.vlan), "/") | EVAL mac_a = CASE(source.ip == Esql.peer_a, source.mac, destination.mac), mac_b = CASE(source.ip == Esql.peer_a, destination.mac, source.mac), Esql.leg_start = event.start | EVAL Esql.leg_key = CONCAT(mac_a, "|", mac_b) // Collapse periodic reports for each flow instance without merging a restored direct path into the earlier instance. | STATS Esql.leg_end = MAX(event.end) BY agent.id, Esql.vlan, network.community_id, Esql.peer_a, Esql.peer_b, mac_a, mac_b, Esql.leg_key, Esql.leg_start // Attach the three-leg topology to every retained instance so each direct instance can be evaluated independently. | INLINE STATS Esql.legs = COUNT_DISTINCT(Esql.leg_key), macs_a = VALUES(mac_a), macs_b = VALUES(mac_b) BY agent.id, Esql.vlan, network.community_id, Esql.peer_a, Esql.peer_b | WHERE Esql.legs == 3 AND MV_COUNT(macs_a) == 2 AND MV_COUNT(macs_b) == 2 | EVAL relay_macs = MV_INTERSECTION(macs_a, macs_b) | WHERE MV_COUNT(relay_macs) == 1 | EVAL Esql.relay_mac = MV_FIRST(relay_macs) | EVAL is_direct = mac_a != Esql.relay_mac AND mac_b != Esql.relay_mac // Preserve the start and side of every relay-leg instance for candidate pairing. | EVAL relay_candidate = CASE( NOT is_direct, CONCAT(TO_STRING(Esql.leg_start), "|", CASE(mac_a == Esql.relay_mac, "a", "b")), null ) | INLINE STATS relay_candidates = VALUES(relay_candidate) WHERE relay_candidate IS NOT NULL BY agent.id, Esql.vlan, network.community_id, Esql.peer_a, Esql.peer_b | WHERE is_direct AND MV_COUNT(relay_candidates) >= 2 | EVAL candidate_1 = relay_candidates, candidate_2 = relay_candidates, Esql.original_mac_a = mac_a, Esql.original_mac_b = mac_b, Esql.direct_start = Esql.leg_start, Esql.direct_end = Esql.leg_end | MV_EXPAND candidate_1 | MV_EXPAND candidate_2 | EVAL relay_start_1 = TO_DATETIME(MV_FIRST(SPLIT(candidate_1, "|"))), relay_start_2 = TO_DATETIME(MV_FIRST(SPLIT(candidate_2, "|"))), relay_side_1 = MV_LAST(SPLIT(candidate_1, "|")), relay_side_2 = MV_LAST(SPLIT(candidate_2, "|")) | WHERE relay_side_1 != relay_side_2 | EVAL Esql.relay_start = LEAST(relay_start_1, relay_start_2), last_leg_start = GREATEST(relay_start_1, relay_start_2) | WHERE Esql.relay_mac != Esql.original_mac_a AND Esql.relay_mac != Esql.original_mac_b AND Esql.relay_start >= Esql.direct_start + 5 seconds AND Esql.relay_start <= Esql.direct_end + 30 seconds AND last_leg_start <= Esql.relay_start + 2 seconds | SORT Esql.relay_start DESC, Esql.direct_start DESC | LIMIT 1 BY agent.id, Esql.vlan, network.community_id, Esql.peer_a, Esql.peer_b | KEEP agent.id, Esql.vlan, network.community_id, Esql.peer_a, Esql.peer_b, Esql.original_mac_a, Esql.original_mac_b, Esql.relay_mac, Esql.direct_start, Esql.direct_end, Esql.relay_start
Install detection rules in Elastic Security
Detect Potential On-Path Relay in a TCP Flow in the Elastic Security detection engine by installing this rule into your Elastic Stack.
To setup this rule, check out the installation guide for Prebuilt Security Detection Rules(external, opens in a new tab or window).