|
| 1 | +import panther_event_type_helpers as event_type |
| 2 | +import pycountry |
| 3 | + |
| 4 | +# Configuration Required: |
| 5 | +# Configure the below list of rogue states according to your needs/experience |
| 6 | +# Refer to the link below to find the alpha-2 code corresponding to your country |
| 7 | +# https://www.iban.com/country-codes |
| 8 | +ROGUE_STATES = {"CN", "IR", "RU"} |
| 9 | + |
| 10 | + |
| 11 | +def rule(event): |
| 12 | + # Only evaluate successful logins |
| 13 | + if event.udm("event_type") != event_type.SUCCESSFUL_LOGIN: |
| 14 | + return False |
| 15 | + |
| 16 | + # Ignore events with no IP data |
| 17 | + if not event.udm("source_ip"): |
| 18 | + return False |
| 19 | + |
| 20 | + # Get contry of request origin and compare to identified rogue state list |
| 21 | + return bool(is_rogue_state(get_country(event).alpha_2)) |
| 22 | + |
| 23 | + |
| 24 | +def title(event): |
| 25 | + log_type = event.get("p_log_type") |
| 26 | + country = get_country(event) |
| 27 | + account_name = get_account_name(event) |
| 28 | + return f"{log_type}: Sign-In for account {account_name} from Rogue State '{country.name}'" |
| 29 | + |
| 30 | + |
| 31 | +def alert_context(event): |
| 32 | + return { |
| 33 | + "source_ip": event.udm("source_ip"), |
| 34 | + "country": get_country(event).name, |
| 35 | + "account_name": get_account_name(event), |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | +def get_country(event) -> str: |
| 40 | + """Returns the country code from an event's IPinfo data.""" |
| 41 | + location_data = event.deep_get("p_enrichment", "ipinfo_location", event.udm_path("source_ip")) |
| 42 | + if not location_data: |
| 43 | + return "" # Ignore event if we have no enrichment to analyze |
| 44 | + return pycountry.countries.get(alpha_2=location_data.get("country").upper()) |
| 45 | + |
| 46 | + |
| 47 | +def get_account_name(event) -> str: |
| 48 | + """Returns the account name.""" |
| 49 | + if account_name := event.deep_get("p_udm", "user", "email"): |
| 50 | + return account_name |
| 51 | + if account_name := event.deep_get("p_udm", "user", "name"): |
| 52 | + return account_name |
| 53 | + if account_name := event.udm("actor_user"): |
| 54 | + return account_name |
| 55 | + return "UNKNWON ACCOUNT" |
| 56 | + |
| 57 | + |
| 58 | +def is_rogue_state(country_code: str) -> bool: |
| 59 | + """Returns whether the country code provided belongs to an identified rogue state.""" |
| 60 | + # This function makes it easy for us to use unit test mocks to ensure altering the ROGUE_STATES |
| 61 | + # dict doesn't break our test suite. |
| 62 | + return country_code in ROGUE_STATES |
0 commit comments