Impossible Travel
erin signs in to mail from the office in Hanoi at 08:55 and from an address in Frankfurt at 09:40. Nobody flies 8,700 km in 45 minutes. Either someone else has her password (or her session token), or one of the two locations is wrong. Impossible travel is the rule that asks that question for every pair of consecutive sign-ins per user. It is one of the most widely deployed identity detections because it needs only sign-in logs and a geolocation lookup, and one of the noisiest when deployed carelessly. This lesson builds it, then removes its classic false positives.
The mechanism it detects
Stolen credentials and stolen session cookies (from phishing kits that proxy the real login page, from infostealer malware on a home PC, from password reuse) are used from wherever the attacker sits, while the real user keeps working where they are. The identity provider sees the same account from two places in a short time. The rule turns that into a number: the speed the user would have had to travel.
Definitions
- Great-circle distance between two points on a sphere of radius R = 6,371 km, by the haversine formula, with latitudes φ and longitudes λ in radians: d = 2R · asin( √( sin²(Δφ/2) + cos φ₁ · cos φ₂ · sin²(Δλ/2) ) ).
- Required speed = d / Δt between two consecutive sign-ins of one user.
- Threshold: faster than a commercial airliner (about 900 km/h) is impossible; many products also flag "unlikely" speeds or new countries.
The rule
import math
import pandas as pd
GEO = { # synthetic geolocation table: (city, lat, lon, kind); real ones come from a GeoIP database
"10.10.0.15": ("Hanoi office", 21.03, 105.85, "office"),
"192.0.2.10": ("Hanoi VPN", 21.03, 105.85, "corporate VPN"),
"198.51.100.23": ("Singapore", 1.35, 103.82, "residential"),
"203.0.113.9": ("Frankfurt", 50.11, 8.68, "hosting"),
"198.51.100.200": ("Tokyo", 35.68, 139.69, "corporate web proxy"),
"203.0.113.120": ("Da Nang", 16.05, 108.20, "mobile"),
}
def km(a, b):
(_, la1, lo1, _), (_, la2, lo2, _) = GEO[a], GEO[b]
p1, p2, dp, dl = map(math.radians, (la1, la2, la2 - la1, lo2 - lo1))
h = math.sin(dp / 2) ** 2 + math.cos(p1) * math.cos(p2) * math.sin(dl / 2) ** 2
return 2 * 6371 * math.asin(math.sqrt(h))
signins = pd.DataFrame([
("erin", "08:55", "10.10.0.15"), ("erin", "09:40", "203.0.113.9"), # stolen password
("bob", "08:30", "10.10.0.15"), ("bob", "08:50", "198.51.100.200"), # same office, via the cloud proxy
("carol", "07:10", "198.51.100.23"), ("carol", "13:30", "10.10.0.15"), # flew home from Singapore
("dave", "09:00", "10.10.0.15"), ("dave", "09:30", "203.0.113.120"), # phone on a mobile network
("judy", "10:00", "192.0.2.10"), ("judy", "10:05", "10.10.0.15"), # VPN, then office
], columns=["user", "time", "ip"])
signins["time"] = pd.to_datetime("2026-01-15 " + signins.time)
signins = signins.sort_values(["user", "time"])
signins["prev_ip"] = signins.groupby("user").ip.shift()
signins["prev_time"] = signins.groupby("user").time.shift()
pairs = signins.dropna(subset=["prev_ip"]).copy()
pairs["km"] = [km(a, b) for a, b in zip(pairs.prev_ip, pairs.ip)]
pairs["hours"] = (pairs.time - pairs.prev_time).dt.total_seconds() / 3600
pairs["kmh"] = pairs.km / pairs.hours
for r in pairs.itertuples():
flag = "IMPOSSIBLE" if r.kmh > 900 else ""
print(f"{r.user:6} {GEO[r.prev_ip][0]:>13} -> {GEO[r.ip][0]:<13} {r.km:6.0f} km in {r.hours:4.2f} h = {r.kmh:6.0f} km/h {flag}".rstrip())bob Hanoi office -> Tokyo 3665 km in 0.33 h = 10995 km/h IMPOSSIBLE
carol Singapore -> Hanoi office 2199 km in 6.33 h = 347 km/h
dave Hanoi office -> Da Nang 607 km in 0.50 h = 1213 km/h IMPOSSIBLE
erin Hanoi office -> Frankfurt 8721 km in 0.75 h = 11627 km/h IMPOSSIBLE
judy Hanoi VPN -> Hanoi office 0 km in 0.08 h = 0 km/h
Five users, five pairs, three flagged as impossible, and only one of them is an attack. erin's 8,721 km in 45 minutes (about 11,600 km/h) is the stolen password. bob never left the office: his browser went out through the company's cloud web proxy, whose address geolocates to Tokyo. dave's phone switched to a mobile network whose addresses the geolocation database places in Da Nang; 607 km in 30 minutes is 1,213 km/h, just over the threshold. carol really travelled (347 km/h is a flight with time at airports), and judy's VPN and office addresses are both in Hanoi. A precision of one in three on five users becomes hundreds of false alarms a day in a large company, which is why the naive version is quickly switched off.
Removing the false positives
The naive rule's false positives have known causes, and each has a targeted fix:
import pandas as pd
pairs = pd.DataFrame([ # the flagged pairs from above, with what the identity provider also logged
("erin", "Hanoi office", "Frankfurt", "office", "hosting", False, "password", 8721),
("bob", "Hanoi office", "Tokyo", "office", "corporate web proxy", True, "password+MFA", 3665),
("dave", "Hanoi office", "Da Nang", "office", "mobile", True, "password+MFA", 607),
], columns=["user", "from", "to", "from_kind", "to_kind", "known_device", "auth", "km"])
TRUSTED_EGRESS = {"corporate web proxy", "corporate VPN"} # our own egress points: their geolocation is not the user's
def verdict(r):
if r.to_kind in TRUSTED_EGRESS or r.from_kind in TRUSTED_EGRESS:
return "suppress: corporate egress point"
if r.to_kind == "mobile" and r.km < 1500:
return "suppress: mobile carrier geolocation is coarse"
score = 50 + (25 if not r.known_device else 0) + (15 if r.to_kind == "hosting" else 0) + (10 if "MFA" not in r.auth else 0)
return f"ALERT score {score}: {'new device, ' if not r.known_device else ''}{r.to_kind} network, {r.auth}"
for r in pairs.itertuples():
print(f"{r.user:5} {r.to:10} {verdict(r)}")erin Frankfurt ALERT score 100: new device, hosting network, password
bob Tokyo suppress: corporate egress point
dave Da Nang suppress: mobile carrier geolocation is coarse
The enriched rule keeps erin's case and explains why it is serious: a device never seen before, a hosting-provider network (attackers rent servers and VPS more often than they fly), and no MFA on the sign-in. bob and dave disappear for documented reasons. This pattern, detect broadly, then score with context, recurs throughout the course (Enrichment).
How it is detected in production
Identity platforms ship this detection (Microsoft Entra ID Protection's "atypical travel" detection, and its "impossible travel" detection, which comes from Microsoft Defender for Cloud Apps; similar ones in other identity providers and in SIEM content packs). Building your own is still useful when sign-ins come from several systems (VPN, mail, SaaS) that no single product sees together. In SIEM terms it is a self-join per user on consecutive sign-ins with a geolocation lookup; in Sentinel KQL that is SigninLogs | sort by UserPrincipalName, TimeGenerated | serialize | extend prev = prev(Location)…, in SPL streamstats current=f last(lat) last(lon) last(_time) by user.
How the weakness is fixed
Impossible travel detects stolen credentials after they are used. The fixes reduce what a stolen password or token is worth:
- Phishing-resistant MFA (FIDO2 security keys or passkeys) for all users: a password alone no longer works, and a proxying phishing page cannot replay the WebAuthn response for a different origin.
- Conditional access: require a managed, compliant device for sensitive apps, block sign-ins from hosting providers or unexpected countries, re-authenticate for risky sign-ins.
- Token protection: short session lifetimes, binding tokens to the device where supported, and revoking sessions when risk is detected.
- Response playbook: on a true positive, revoke all sessions, reset the password, check MFA registrations for a newly added factor (attackers add their own), and search for mailbox rules created by the attacker.
Pitfalls
- Treating GeoIP as ground truth. Databases are wrong for mobile carriers, satellite links, new allocations and anything behind a proxy. Keep the geolocation's accuracy radius and the network type.
- Comparing only the last two sign-ins. An attacker who signs in between two genuine sign-ins creates two impossible pairs; dedupe them into one alert.
- Ignoring the "possible" travel. A sign-in from a new country at a plausible speed is not impossible, but combined with a new device and no MFA it is still worth an alert.
Practice
1. Compute the speed for a sign-in from Hanoi at 08:00 and from Singapore (2,200 km away) at 11:30. Is it impossible?
2,200 km / 3.5 h ≈ 630 km/h: below 900 km/h, so possible by plane. A pure speed rule stays quiet; a "new country plus new device" rule might still fire.
2. Why should the corporate VPN's address be a trusted egress point, and what new risk does that create?
All VPN users appear at the gateway's location, so their real location is unknown and speeds are meaningless. The risk: an attacker who gets onto the VPN (with stolen VPN credentials) is invisible to the rule, so the VPN's own sign-ins need their own detection (new source address, impossible travel on the VPN's client addresses).
3. erin's two sign-ins were 45 minutes apart. If the attacker had waited 12 hours, what would the rule say, and what would still catch it?
8,721 km in 12.75 h ≈ 684 km/h: possible, no alert. The context signals still apply: new device, hosting network, no MFA, a country erin has never used. A risk score built on those catches it without relying on speed.
Test yourself
What does impossible travel compute for each pair of consecutive sign-ins of a user?
- The number of failed passwords
- The speed needed to travel between the two locations in the time between them
- The user's working hours
- The password's entropy
Why was bob's sign-in through the Tokyo proxy a false positive?
- bob was in Tokyo
- The corporate web proxy's address geolocates to Tokyo, not to the user
- The office is in Tokyo
- bob used a hosting provider
Which fix makes a phished password useless on its own?
- Longer passwords
- Phishing-resistant MFA such as FIDO2 or passkeys
- A stricter impossible-travel threshold
- Changing the GeoIP vendor
In the enriched rule, which three signals raised erin's score?
- Known device, office network, MFA
- New device, hosting-provider network, no MFA
- Mobile network and VPN
- Only the distance