Match indicators precisely
Use an exact normalized lookup and avoid substring false positives.
What counts as a match?
An indicator list is useful only when its matching rules are clear. The text capture.example.test can occur inside an unrelated hostname. A substring test would also match notcapture.example.test and capture.example.test.safe.test. Neither is the same domain.
For this lesson, normalize supplied domain names by stripping surrounding whitespace, lowercasing letters, and removing trailing dots. Then use exact set membership. Do not add subdomain matching unless a detection policy explicitly asks for it. These inputs are domain fields, not whole URLs; parsing a URL into a hostname is the next lesson.
Separate lookup values from observations
A set removes duplicate indicators and provides a direct membership test. Keep observations as a list so repeated observations remain traceable to their own event IDs. Return one result per matching observation in input order, even when the indicator list repeats a value.
An indicator match does not tell you whether a user clicked a link or whether a device was compromised. Keep the source ID so an analyst can inspect surrounding evidence. Changes to a feed can also alter yesterday's result; production investigations should retain the list version they used.
Try the exact domain, an uppercase spelling, a trailing dot, a subdomain, and a misleading suffix. The distinction is visible in a handful of cases and prevents a surprisingly common false-positive pattern.
known = {"capture.example.test"}
print("capture.example.test" in known)
print("capture.example.test.safe.test" in known)
Put it into practice.
Define match_domains(observations, indicators). Each observation has id and domain; indicators is a list of domain strings. Normalize using strip().lower().rstrip("."). Return {"id": original_id, "domain": normalized_domain} for exact matches, preserving observation order. Duplicate indicators must not duplicate findings. Do not match subdomains or substrings.
See an explained solution
def match_domains(observations, indicators):
known = {value.strip().lower().rstrip(".") for value in indicators}
matches = []
for row in observations:
domain = row["domain"].strip().lower().rstrip(".")
if domain in known:
matches.append({"id": row["id"], "domain": domain})
return matches
Set membership compares whole normalized values. It avoids accidental suffix and subdomain matches while retaining each source observation.