From 47762e735c097ef91aba2ae07a0ac1a748a3e321 Mon Sep 17 00:00:00 2001 From: Juan Manuel Tejada <58552435+juanmill4@users.noreply.github.com> Date: Mon, 23 Nov 2026 03:02:33 +0000 Subject: [PATCH] Add DarkEye TI provider (HaveIBeenRansom and Breach House) One provider, msticpy/context/tiproviders/darkeye.py, looks up domains, hostnames and email addresses in the two DarkEye products that have an API. Each product has its own optional key (HIBRAuthKey, BreachHouseAuthKey); a product without a key is not queried, and at least one key is required. Both products are combined into one result in which every record carries its product and category (ransomware, breach, leak, stealer, lead). Only listing metadata is kept. The tests in tests/context/test_darkeye.py run offline. --- .../msticpy.context.tiproviders.darkeye.rst | 7 + .../api/msticpy.context.tiproviders.rst | 1 + docs/source/data_acquisition/TIProviders.rst | 10 +- docs/source/getting_started/msticpyconfig.rst | 11 + msticpy/context/lookup.py | 10 +- msticpy/context/tiproviders/__init__.py | 1 + msticpy/context/tiproviders/darkeye.py | 651 +++++++++++++ msticpy/resources/mpconfig_defaults.yaml | 8 + tests/context/test_darkeye.py | 873 ++++++++++++++++++ tests/msticpyconfig-test.yaml | 6 + 10 files changed, 1576 insertions(+), 2 deletions(-) create mode 100644 docs/source/api/msticpy.context.tiproviders.darkeye.rst create mode 100644 msticpy/context/tiproviders/darkeye.py create mode 100644 tests/context/test_darkeye.py diff --git a/docs/source/api/msticpy.context.tiproviders.darkeye.rst b/docs/source/api/msticpy.context.tiproviders.darkeye.rst new file mode 100644 index 00000000..a2166158 --- /dev/null +++ b/docs/source/api/msticpy.context.tiproviders.darkeye.rst @@ -0,0 +1,7 @@ +msticpy.context.tiproviders.darkeye module +========================================== + +.. automodule:: msticpy.context.tiproviders.darkeye + :members: + :show-inheritance: + :undoc-members: diff --git a/docs/source/api/msticpy.context.tiproviders.rst b/docs/source/api/msticpy.context.tiproviders.rst index 34d285bd..b6c34716 100644 --- a/docs/source/api/msticpy.context.tiproviders.rst +++ b/docs/source/api/msticpy.context.tiproviders.rst @@ -18,6 +18,7 @@ Submodules msticpy.context.tiproviders.binaryedge msticpy.context.tiproviders.crowdsec msticpy.context.tiproviders.cyberint + msticpy.context.tiproviders.darkeye msticpy.context.tiproviders.greynoise msticpy.context.tiproviders.ibm_xforce msticpy.context.tiproviders.intsights diff --git a/docs/source/data_acquisition/TIProviders.rst b/docs/source/data_acquisition/TIProviders.rst index 1a6184c3..4971887e 100644 --- a/docs/source/data_acquisition/TIProviders.rst +++ b/docs/source/data_acquisition/TIProviders.rst @@ -33,6 +33,7 @@ Features - **CrowdSec** - **AbuseIPDB** - **ScanMalware** + - **DarkEye** (HaveIBeenRansom and Breach House) - Other pseudo-TI providers are also included: @@ -286,6 +287,12 @@ fictitious - the format of the keys may differ from what is shown ScanMalware: Primary: True Provider: "ScanMalware" + DarkEye: + Args: + HIBRAuthKey: 1234567890 + BreachHouseAuthKey: 1234567890 + Primary: True + Provider: "DarkEye" You need to tell `TILookup` to refresh its configuration. @@ -307,7 +314,8 @@ of providers loaded. 'AzSTI - Microsoft Sentinel TI provider class. (primary)', 'CrowdSec - CrowdSec CTI Smoke Lookup. (primary)', 'AbuseIPDB - AbuseIPDB Lookup. (primary)', - 'ScanMalware - ScanMalware Lookup. (primary)'] + 'ScanMalware - ScanMalware Lookup. (primary)', + 'DarkEye - DarkEye Lookup (HaveIBeenRansom and Breach House). (primary)'] .. warning:: Depending on the type of account that you have with a provider, they will typically impose a limit diff --git a/docs/source/getting_started/msticpyconfig.rst b/docs/source/getting_started/msticpyconfig.rst index 45340bc9..339e9cff 100644 --- a/docs/source/getting_started/msticpyconfig.rst +++ b/docs/source/getting_started/msticpyconfig.rst @@ -118,6 +118,7 @@ Currently supported provider names are: - CrowdSec - AbuseIPDB - ScanMalware +- DarkEye .. code:: yaml @@ -152,6 +153,16 @@ Currently supported provider names are: ScanMalware: Primary: True Provider: "ScanMalware" + DarkEye: + Args: + HIBRAuthKey: 1234567890 + BreachHouseAuthKey: 1234567890 + Primary: True + Provider: "DarkEye" + +.. note:: DarkEye covers two products: HaveIBeenRansom (``HIBRAuthKey``) + and Breach House (``BreachHouseAuthKey``). Each key is optional but at + least one is required. Only the products that have a key are queried. .. note:: You store values in the ``Args`` section as simple strings, as names of environment variables containing the value, or diff --git a/msticpy/context/lookup.py b/msticpy/context/lookup.py index 21a2e8bc..4d303057 100644 --- a/msticpy/context/lookup.py +++ b/msticpy/context/lookup.py @@ -54,7 +54,15 @@ logger: logging.Logger = logging.getLogger(__name__) -_HTTP_PROVIDER_LEGAL_KWARGS: list[str] = ["timeout", "ApiID", "AuthKey", "Instance"] +_HTTP_PROVIDER_LEGAL_KWARGS: list[str] = [ + "timeout", + "ApiID", + "AuthKey", + "Instance", + # DarkEye has one optional key for each of its two products + "HIBRAuthKey", + "BreachHouseAuthKey", +] class ProgressCounter: diff --git a/msticpy/context/tiproviders/__init__.py b/msticpy/context/tiproviders/__init__.py index ea5370f2..b124f977 100644 --- a/msticpy/context/tiproviders/__init__.py +++ b/msticpy/context/tiproviders/__init__.py @@ -31,4 +31,5 @@ "BinaryEdge": ("binaryedge", "BinaryEdge"), "Cyberint": ("cyberint", "Cyberint"), "ScanMalware": ("scanmalware", "ScanMalware"), + "DarkEye": ("darkeye", "DarkEye"), } diff --git a/msticpy/context/tiproviders/darkeye.py b/msticpy/context/tiproviders/darkeye.py new file mode 100644 index 00000000..9b6b4e16 --- /dev/null +++ b/msticpy/context/tiproviders/darkeye.py @@ -0,0 +1,651 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +""" +DarkEye Provider. + +Looks up domains, hostnames and email addresses in DarkEye (https://darkeye.io). +DarkEye has two products with a paid API, and this one provider covers both: + +- HaveIBeenRansom (https://haveibeenransom.com) indexes ransomware leaks and + data breaches. +- Breach House (https://breach.house) lists ransomware victims, data breaches, + leads and stealer logs. + +Each product has its own optional key (``HIBRAuthKey`` and +``BreachHouseAuthKey``). A product without a key is not queried and its host is +never contacted. At least one key is required. A lookup returns one combined +result in which every record states its product and its category +(``ransomware``, ``breach``, ``leak``, ``stealer`` or ``lead``). + +Only listing metadata is read (id, title, website, group, country and date). +Nothing else in a response is kept, so no password or credential field is ever +returned, and HaveIBeenRansom's stealer-content endpoint is not used. + +Input can be a single IoC observable or a pandas DataFrame containing +multiple observables. + +""" + +from __future__ import annotations + +import json +import logging +import re +import unicodedata +from typing import Any, ClassVar +from urllib.parse import quote + +import httpx +from pydantic import BaseModel, Field +from typing_extensions import Self + +from ..._version import VERSION +from ...common.exceptions import MsticpyConfigError +from ...common.pkg_config import get_http_timeout +from ..http_provider import APILookupParams +from ..lookup_result import LookupStatus +from .ti_http_provider import HttpTIProvider +from .ti_provider_base import ResultSeverity + +__version__ = VERSION +__author__ = "Juan Manuel Tejada" + +logger: logging.Logger = logging.getLogger(__name__) + +# Product names, as they appear in every record. +HIBR: str = "HaveIBeenRansom" +BREACH_HOUSE: str = "Breach House" + +_HIBR_HOST: str = "https://haveibeenransom.com" +_HIBR_METADATA_URL: str = f"{_HIBR_HOST}/api/metadata" +_BREACH_HOUSE_SEARCH_URL: str = "https://breach.house/api/v1/search" + +# HaveIBeenRansom `leak_type` values. Anything else is reported as a plain leak. +_HIBR_CATEGORIES: dict[str, str] = { + "ransomware_leak": "ransomware", + "traditional_breach": "breach", +} +_UNKNOWN_HIBR_CATEGORY: str = "leak" + +# Breach House sections of the cross-source search, and the category of each. +_BREACH_HOUSE_CATEGORIES: dict[str, str] = { + "ransomware": "ransomware", + "breaches": "breach", + "leads": "lead", + "stealers": "stealer", +} +# Breach House text fields that may mention an email address. They are only used +# to check an exact match and are never copied into a record. +_BREACH_HOUSE_MATCH_FIELDS: tuple[str, ...] = ( + "post_title", + "name", + "website", + "description", + "meta_description", +) + +# Bounds on one lookup. HaveIBeenRansom is paged and every page is one request. +_MAX_PAGES: int = 5 +_BREACH_HOUSE_LIMIT: int = 25 # results requested per Breach House section +_MAX_RECORDS: int = 100 # records kept per product +_MAX_TEXT: int = 256 +_MAX_MATCH_TEXT: int = 4000 +_MAX_EMAIL: int = 320 +_MAX_HOSTNAME: int = 253 + +_HOSTNAME_LABEL = re.compile(r"[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?") + +_USER_AGENT: str = f"msticpy-darkeye/{VERSION}" + +_NOT_QUERIED: str = "Not queried: no API key configured." + + +class DarkEyeRecord(BaseModel): + """Listing metadata of one DarkEye record. Nothing else is kept.""" + + product: str + category: str + id: str = "" + title: str = "" + website: str = "" + group: str = "" + country: str = "" + discovered: str = "" + + +class _ProductResult(BaseModel): + """Outcome of the lookup in one product.""" + + product: str + error: str = "" + truncated: bool = False + records: list[DarkEyeRecord] = Field(default_factory=list) + reference: str = "" + + +class _ProductError(Exception): + """A product lookup failed. The message is safe to show.""" + + +def _clean_key(name: str, value: str | None) -> str | None: + """Return the key, or None if it is not set. Raise if it cannot be a key.""" + if not value or not str(value).strip(): + return None + key: str = str(value).strip() + if any(ord(char) < 33 or ord(char) > 126 for char in key): + # The key itself is never put in a message. + err_msg: str = ( + f"{name} contains whitespace or characters that are not printable ASCII." + ) + raise MsticpyConfigError(err_msg) + return key + + +def _text(value: Any, limit: int = _MAX_TEXT) -> str: + """Return single-line printable text, or "" for anything but text and numbers.""" + if isinstance(value, bool) or not isinstance(value, str | int): + return "" + normalized: str = unicodedata.normalize("NFKC", str(value)) + printable: str = "".join( + " " if char.isspace() else char + for char in normalized + if char.isspace() or unicodedata.category(char)[0] != "C" + ) + return " ".join(printable.split())[:limit] + + +def _hostname_target(value: str) -> str | None: + """Return a plain lower-case ASCII hostname, or None. Wildcards are refused.""" + host: str = value.strip().lower().rstrip(".") + try: + host = host.encode("idna").decode("ascii") + except UnicodeError: + return None + labels: list[str] = host.split(".") + if ( + len(host) > _MAX_HOSTNAME + or len(labels) < 2 + or labels[-1].isdigit() + or not all(_HOSTNAME_LABEL.fullmatch(label) for label in labels) + ): + return None + return host + + +def _email_target(value: str) -> str | None: + """Return the email address, or None. Wildcards, spaces and controls are refused.""" + email: str = value.strip() + if ( + not email + or len(email) > _MAX_EMAIL + or email.count("@") != 1 + or any(char in "*?" for char in email) + or any(char.isspace() or unicodedata.category(char)[0] == "C" for char in email) + ): + return None + local, _, domain = email.partition("@") + if not local or _hostname_target(domain) is None: + return None + return email + + +def _normalize_target(value: str, ioc_type: str) -> str | None: + """Return the value to send to DarkEye for an observable, or None if not valid.""" + if ioc_type == "email": + return _email_target(value) + return _hostname_target(value) + + +def _mentions_email(texts: list[str], email: str) -> bool: + """Return True if the exact address appears in one of the texts.""" + # Boundary match: "bob@example.com" must not match "bob@example.com.au" or + # "xbob@example.com". + pattern: str = r"(? bool: + """ + Return True if a Breach House record is about the host, not just near it. + + The search is free text, so a record whose website is another domain only + mentions the host. With a website it must be the host or one of its + subdomains. Without one the title is free text and the host must stand alone + as a hostname: not inside notexample.com or example.com.other.com, but still + as mail.example.com. + """ + website: str = record.website.lower().rstrip(".") + if website: + return website == host or website.endswith(f".{host}") + title: str = record.title.lower() + pattern: str = r"(? bool: + """Return True if a Breach House item is about the observable.""" + if ioc_type == "email": + texts: list[str] = [ + _text(item.get(field), _MAX_MATCH_TEXT) for field in _BREACH_HOUSE_MATCH_FIELDS + ] + return _mentions_email(texts, target) + return _mentions_host(record, target) + + +def _make_record(product: str, category: str, item: dict[str, Any]) -> DarkEyeRecord: + """Return the listing metadata of an API item. Nothing else is read.""" + return DarkEyeRecord( + product=product, + category=category, + id=_text(item.get("id_source") or item.get("victim_id") or item.get("id")), + title=_text(item.get("post_title") or item.get("name")), + website=_text(item.get("website")), + group=_text(item.get("group_name")), + country=_text(item.get("country")), + discovered=_text(item.get("discovered"))[:10], + ) + + +def _record_identity(record: DarkEyeRecord) -> str: + """Return a key that identifies a record within its product and category.""" + return f"{record.category}|{record.id or json.dumps(dict(record), sort_keys=True)}" + + +class DarkEye(HttpTIProvider): + """DarkEye Lookup (HaveIBeenRansom and Breach House).""" + + # `_QUERIES` declares the supported observable types and the HaveIBeenRansom + # path of each. Breach House has one search endpoint for every type, see + # `_BREACH_HOUSE_SEARCH_URL`. There are no query subtypes: both products are + # always combined into one result. + _BASE_URL: ClassVar[str] = _HIBR_HOST + _QUERIES: ClassVar[dict[str, APILookupParams]] = { + "dns": APILookupParams(path="/api/metadata/domain/{observable}"), + "hostname": APILookupParams(path="/api/metadata/domain/{observable}"), + "email": APILookupParams(path="/api/metadata/email/{observable}"), + } + + def __init__( + self: Self, + *, + timeout: int | None = None, + HIBRAuthKey: str | None = None, # noqa: N803 + BreachHouseAuthKey: str | None = None, # noqa: N803 + ) -> None: + """ + Initialize the provider. + + Parameters + ---------- + timeout : int, optional + Timeout of each HTTP request, by default None + HIBRAuthKey : str, optional + HaveIBeenRansom API key. Without it HaveIBeenRansom is not queried. + BreachHouseAuthKey : str, optional + Breach House API token. Without it Breach House is not queried. + + Raises + ------ + MsticpyConfigError + If neither key is set, or a key contains characters that cannot be + sent in an HTTP header. + + """ + super().__init__(timeout=timeout) + self._hibr_key: str | None = _clean_key("HIBRAuthKey", HIBRAuthKey) + self._breach_house_key: str | None = _clean_key( + "BreachHouseAuthKey", BreachHouseAuthKey + ) + if not (self._hibr_key or self._breach_house_key): + err_msg: str = ( + "DarkEye needs at least one API key: set HIBRAuthKey " + "(HaveIBeenRansom) and/or BreachHouseAuthKey (Breach House)." + ) + raise MsticpyConfigError(err_msg) + + def _run_ti_lookup_query( + self: Self, + result: dict[str, Any], + *, + query_type: str | None = None, + timeout: int | None = None, + ) -> dict[str, Any]: + """ + Look up an observable in every product that has a key. + + The base implementation issues one request to one host. DarkEye has two + hosts, each with its own key, and the answer is one combined result, so + the lookup is overridden. Each response is reduced to listing metadata as it + is read, and nothing else from it is kept. + + Parameters + ---------- + result : dict + Dict result with resolved type and pre-processed item. + Status is non-zero on failure. + query_type : str, optional + Not supported. DarkEye has no query subtypes. + timeout : int, optional + Timeout to use for lookups + + Returns + ------- + dict[str, Any] + The lookup result. + + Raises + ------ + LookupError + If a query subtype is requested. + + """ + if result["Status"] != LookupStatus.OK.value or result["SafeIoc"] is None: + # Not a supported type, or the pre-processing rejected the value + return result + ioc_type: str = result["IocType"] + if query_type: + err_msg: str = f"Provider does not support this type {ioc_type}-{query_type}." + raise LookupError(err_msg) + + target: str | None = _normalize_target(result["SafeIoc"], ioc_type) + if target is None: + result["Status"] = LookupStatus.BAD_FORMAT.value + result["Details"] = f"Observable is not a valid {ioc_type} for DarkEye." + return result + + products: list[_ProductResult] = [] + if self._hibr_key: + product = _ProductResult(product=HIBR) + self._query_hibr(product, ioc_type, target, timeout) + products.append(product) + if self._breach_house_key: + product = _ProductResult(product=BREACH_HOUSE) + self._query_breach_house(product, ioc_type, target, timeout) + products.append(product) + self._fill_result(result, products) + return result + + def _get_json( + self: Self, + url: str, + key: str, + *, + params: dict[str, Any] | None = None, + timeout: int | None = None, + ) -> dict[str, Any]: + """Return the JSON object of a GET request, or raise _ProductError.""" + try: + response: httpx.Response = self._httpx_client.get( + url=url, + params=params, + # The key goes in this request's header only and the URL is always + # on the key's own host. Redirects are refused so that a redirect + # cannot carry the header to another host. + headers={ + "Authorization": f"Bearer {key}", + "Accept": "application/json", + "User-Agent": _USER_AGENT, + }, + follow_redirects=False, + timeout=get_http_timeout(timeout=timeout), + ) + except httpx.TimeoutException: + raise _ProductError("request timed out") from None + except httpx.HTTPError as err: + # The exception text can carry request details: report only its type. + err_msg: str = f"request failed ({type(err).__name__})" + raise _ProductError(err_msg) from None + + status: int = response.status_code + if status in (httpx.codes.UNAUTHORIZED, httpx.codes.FORBIDDEN): + err_msg = f"HTTP {status} (check the API key and plan)" + raise _ProductError(err_msg) + if status == httpx.codes.TOO_MANY_REQUESTS: + err_msg = f"HTTP {status} (query limit reached)" + raise _ProductError(err_msg) + if status != httpx.codes.OK: + err_msg = f"HTTP {status}" + raise _ProductError(err_msg) + try: + data: Any = response.json() + except ValueError: + raise _ProductError("invalid JSON response") from None + if not isinstance(data, dict) or data.get("success") is False or "error" in data: + raise _ProductError("unexpected response") + return data + + def _query_hibr( + self: Self, + product: _ProductResult, + ioc_type: str, + target: str, + timeout: int | None, + ) -> None: + """Read HaveIBeenRansom metadata (ransomware leaks and data breaches).""" + base_url: str = (self._BASE_URL + self._QUERIES[ioc_type].path).format( + observable=quote(target, safe="") + ) + product.reference = base_url + try: + self._read_hibr_pages(product, base_url, timeout) + except _ProductError as err: + product.error = str(err) + logger.info("DarkEye %s lookup failed: %s", HIBR, product.error) + + def _read_hibr_pages( + self: Self, + product: _ProductResult, + base_url: str, + timeout: int | None, + ) -> None: + """Read the pages of a HaveIBeenRansom lookup into the product result.""" + seen: set[str] = set() + for page in range(1, _MAX_PAGES + 1): + data = self._get_json( + base_url if page == 1 else f"{base_url}/{page}", + str(self._hibr_key), + timeout=timeout, + ) + items: Any = data["results"] if "results" in data else data.get("sources") + pagination: Any = data.get("pagination") + has_next: Any = data.get("has_next") + if has_next is None and isinstance(pagination, dict): + has_next = pagination.get("has_next") + if ( + not isinstance(items, list) + or not isinstance(has_next, bool) + or any(not isinstance(item, dict) for item in items) + ): + raise _ProductError("unexpected response") + for item in items: + category: str = _HIBR_CATEGORIES.get( + _text(item.get("leak_type")), _UNKNOWN_HIBR_CATEGORY + ) + record = _make_record(HIBR, category, item) + if not (record.id or record.title or record.website): + raise _ProductError("unexpected response") + if _record_identity(record) in seen: + continue + seen.add(_record_identity(record)) + if len(product.records) >= _MAX_RECORDS: + product.truncated = True + return + product.records.append(record) + if not has_next: + return + product.truncated = True + + def _query_breach_house( + self: Self, + product: _ProductResult, + ioc_type: str, + target: str, + timeout: int | None, + ) -> None: + """Search Breach House (ransomware, breaches, leads and stealers).""" + product.reference = f"{_BREACH_HOUSE_SEARCH_URL}?q={quote(target, safe='')}" + try: + data = self._get_json( + _BREACH_HOUSE_SEARCH_URL, + str(self._breach_house_key), + params={"q": target, "limit": _BREACH_HOUSE_LIMIT}, + timeout=timeout, + ) + self._read_breach_house_sections(product, data, ioc_type, target) + except _ProductError as err: + product.error = str(err) + logger.info("DarkEye %s lookup failed: %s", BREACH_HOUSE, product.error) + + @staticmethod + def _read_breach_house_sections( + product: _ProductResult, + data: dict[str, Any], + ioc_type: str, + target: str, + ) -> None: + """ + Read the sections of a Breach House search into the product result. + + The search is free text over the victim and listing fields, so a record + is kept only when it is about the observable: the exact address for an + email, the host or one of its subdomains for a hostname. + """ + if not any(source in data for source in _BREACH_HOUSE_CATEGORIES): + raise _ProductError("unexpected response") + seen: set[str] = set() + for source, category in _BREACH_HOUSE_CATEGORIES.items(): + section: Any = data.get(source) + if section is None: + continue + total: Any = None + if isinstance(section, dict): + total, section = section.get("total"), section.get("results") + if not isinstance(section, list) or any(not isinstance(i, dict) for i in section): + raise _ProductError("unexpected response") + if isinstance(total, int) and not isinstance(total, bool): + product.truncated |= total > len(section) + else: + product.truncated |= len(section) >= _BREACH_HOUSE_LIMIT + for item in section: + record = _make_record(BREACH_HOUSE, category, item) + if not _is_about(item, record, ioc_type, target): + continue + if _record_identity(record) in seen: + continue + seen.add(_record_identity(record)) + if len(product.records) >= _MAX_RECORDS: + product.truncated = True + return + product.records.append(record) + + @staticmethod + def _severity(records: list[DarkEyeRecord]) -> ResultSeverity: + """ + Return the severity of a set of records. + + `ResultSeverity` has no value for "listed as a victim". A listing means the + organisation or address appears in leaked or listed data, which is not the + same as the observable being hostile, so the result never goes above + `warning` and is never `high`. A record means something was found, so any + record other than a lead is `warning`. Leads are `information`, and so is + no record at all: no record only means nothing was found, not that the + observable is clean. + """ + if any(record.category != "lead" for record in records): + return ResultSeverity.warning + return ResultSeverity.information + + def _fill_result( + self: Self, + result: dict[str, Any], + products: list[_ProductResult], + ) -> None: + """Set status, severity, details and raw result from the product results.""" + records: list[DarkEyeRecord] = [rec for prod in products for rec in prod.records] + errors: list[str] = [ + f"{prod.product}: {prod.error}" for prod in products if prod.error + ] + result["Reference"] = ", ".join(prod.reference for prod in products) + + if len(errors) == len(products) and not records: + # Nothing was answered: an empty result would read as "not found". + result["Status"] = LookupStatus.QUERY_FAILED.value + result["Severity"] = ResultSeverity.unknown.name + result["Result"] = False + result["Details"] = "; ".join(errors) + result["RawResult"] = {"Errors": errors} + return + + categories: dict[str, int] = {} + for record in records: + categories[record.category] = categories.get(record.category, 0) + 1 + record_dicts: list[dict[str, str]] = [dict(record) for record in records] + product_info: dict[str, dict[str, Any]] = {} + for prod in products: + info: dict[str, Any] = { + "Status": "error" if prod.error else "ok", + "Records": len(prod.records), + "Truncated": prod.truncated, + } + if prod.error: + info["Error"] = prod.error + product_info[prod.product] = info + not_queried: list[str] = [ + name + for name, key in ((HIBR, self._hibr_key), (BREACH_HOUSE, self._breach_house_key)) + if not key + ] + for name in not_queried: + product_info[name] = {"Status": _NOT_QUERIED} + + details: dict[str, Any] = { + "Count": len(records), + "Products": product_info, + "Categories": categories, + "Truncated": any(prod.truncated for prod in products), + "Records": record_dicts, + } + if errors: + details["Errors"] = errors + result["Status"] = LookupStatus.OK.value + result["Result"] = bool(records) + result["Severity"] = self._severity(records).name + result["Details"] = details + # Not the raw API response: that can hold fields this provider never reads. + result["RawResult"] = {"Records": record_dicts, "Products": product_info} + + def parse_results(self: Self, response: dict) -> tuple[bool, ResultSeverity, Any]: + """ + Return the details of the response. + + Not used by this provider: `_run_ti_lookup_query` reduces each response + to listing metadata itself and builds the result from both products. + This reports the same details from a result that has already been built. + + Parameters + ---------- + response : Dict + The returned data response + + Returns + ------- + Tuple[bool, ResultSeverity, Any] + bool = positive or negative hit + ResultSeverity = enumeration of severity + Object with match details + + """ + raw: Any = response.get("RawResult") + if response.get("Status") != LookupStatus.OK.value or not isinstance(raw, dict): + return False, ResultSeverity.information, "Not found." + records: list[DarkEyeRecord] = [ + DarkEyeRecord(**record) for record in raw.get("Records", []) + ] + return ( + bool(records), + self._severity(records), + {"Count": len(records), "Products": raw.get("Products", {})}, + ) diff --git a/msticpy/resources/mpconfig_defaults.yaml b/msticpy/resources/mpconfig_defaults.yaml index 75f0bfbb..b6a76882 100644 --- a/msticpy/resources/mpconfig_defaults.yaml +++ b/msticpy/resources/mpconfig_defaults.yaml @@ -104,6 +104,14 @@ TIProviders: ScanMalware: Primary: bool(default=True) Provider: "ScanMalware" + DarkEye: + Args: + # Each key is optional, but at least one is needed. A product without a key + # is not queried. + HIBRAuthKey: *cred_key_opt + BreachHouseAuthKey: *cred_key_opt + Primary: bool(default=False) + Provider: "DarkEye" RiskIQ: Primary: bool(default=True) Provider: "RiskIQ" diff --git a/tests/context/test_darkeye.py b/tests/context/test_darkeye.py new file mode 100644 index 00000000..6dc27b00 --- /dev/null +++ b/tests/context/test_darkeye.py @@ -0,0 +1,873 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +"""DarkEye TI provider tests. + +No network access: every HTTP request is answered by respx. The provider is +tested through its real httpx client, so the headers, URLs and redirect handling +that are asserted here are what would be sent. +""" + +from __future__ import annotations + +import json +import logging +import warnings +from pathlib import Path +from typing import Any + +import httpx +import pytest +import pytest_check as check +import respx + +from msticpy.common.exceptions import MsticpyConfigError +from msticpy.config.mp_config_control import MpConfigControls, get_mpconfig_definitions +from msticpy.context.lookup_result import LookupStatus +from msticpy.context.tilookup import TILookup +from msticpy.context.tiproviders import darkeye +from msticpy.context.tiproviders.darkeye import DarkEye, DarkEyeRecord +from msticpy.context.tiproviders.result_severity import ResultSeverity + +from ..unit_test_lib import custom_mp_config, get_test_data_path + +__author__ = "Juan Manuel Tejada" + +# pylint: disable=protected-access, redefined-outer-name + +# Mock secrets. Two different values so that a key sent to the wrong host shows up. +_HIBR_KEY = "[PLACEHOLDER-HIBR]" +_BH_KEY = "[PLACEHOLDER-BH]" + +_HIBR_DOMAIN = "https://haveibeenransom.com/api/metadata/domain/example.com" +_HIBR_EMAIL = "https://haveibeenransom.com/api/metadata/email/bob%40example.com" +_BH_SEARCH = "https://breach.house/api/v1/search" + +# Fields that exist in real responses but that the provider must never return. +_SECRET_FIELDS: dict[str, str] = { + "password": "SECRET-PASSWORD", + "credentials": "SECRET-CREDENTIALS", + "Identities Found": "SECRET-IDENTITIES", + "description": "SECRET-DESCRIPTION", + "meta_description": "SECRET-META", + "post_url": "http://SECRET-LEAK-SITE.onion/post", + "download_link": "http://SECRET-DOWNLOAD.onion/file", + "hibr": {"domain": "SECRET-HIBR-BLOCK"}, +} + + +@pytest.fixture +def api(): + """Return a respx router that answers every request itself.""" + with respx.mock(assert_all_called=False) as router: + yield router + + +def _hibr_item( + item_id: Any = "1", leak_type: str = "traditional_breach", **kwargs +) -> dict[str, Any]: + """Return a HaveIBeenRansom metadata item.""" + item = { + "id_source": item_id, + "leak_type": leak_type, + "post_title": f"Title {item_id}", + "website": "example.com", + "group_name": "group", + "country": "US", + "discovered": "2024-05-06 01:02:03", + } + item.update(kwargs) + return item + + +def _hibr_page( + items: list[dict[str, Any]], *, has_next: bool = False, documented: bool = False +) -> dict[str, Any]: + """Return a HaveIBeenRansom page, in the shape the API sends or the documented one.""" + if documented: + return {"results": items, "pagination": {"has_next": has_next}, "success": True} + return {"sources": items, "has_next": has_next} + + +def _bh_item(item_id: Any = "v1", **kwargs) -> dict[str, Any]: + """Return a Breach House item for example.com.""" + item = { + "victim_id": item_id, + "post_title": f"Victim {item_id}", + "website": "example.com", + "group_name": "group", + "country": "IT", + "discovered": "2023-01-15 10:22:00.000000", + } + item.update(kwargs) + return item + + +def _bh_body(**sections: list[dict[str, Any]]) -> dict[str, Any]: + """Return a Breach House search body with a total for each section.""" + return {name: {"results": items, "total": len(items)} for name, items in sections.items()} + + +def _lookup(provider: DarkEye, ioc: str, ioc_type: str, **kwargs) -> dict[str, Any]: + """Run a lookup and return the single result row as a dict.""" + return provider.lookup_ioc(ioc=ioc, ioc_type=ioc_type, **kwargs).to_dict(orient="records")[ + 0 + ] + + +@pytest.fixture +def both() -> DarkEye: + """Return a provider with both keys.""" + return DarkEye(HIBRAuthKey=_HIBR_KEY, BreachHouseAuthKey=_BH_KEY) + + +def _hosts(api: respx.MockRouter) -> dict[str, list[httpx.Request]]: + """Return the requests that were made, by host.""" + by_host: dict[str, list[httpx.Request]] = {} + for call in api.calls: + by_host.setdefault(call.request.url.host, []).append(call.request) + return by_host + + +# --- keys ------------------------------------------------------------------ + + +@pytest.mark.parametrize( + "keys", [{}, {"HIBRAuthKey": ""}, {"HIBRAuthKey": " ", "BreachHouseAuthKey": ""}] +) +def test_no_key_cannot_run(keys): + """With no key the provider is refused at load, before anything can be sent.""" + with pytest.raises(MsticpyConfigError) as err: + DarkEye(**keys) + check.is_in("HIBRAuthKey", str(err.value)) + check.is_in("BreachHouseAuthKey", str(err.value)) + + +@pytest.mark.parametrize("bad_key", ["has space", "tab\tkey", "line\nbreak", "ünïcode"]) +def test_unusable_key_is_refused_without_echoing_it(bad_key): + """A key that is not printable ASCII is refused and never repeated.""" + with pytest.raises(MsticpyConfigError) as err: + DarkEye(HIBRAuthKey=bad_key) + check.is_not_in(bad_key, str(err.value)) + check.is_in("HIBRAuthKey", str(err.value)) + + +def test_keys_are_stripped(): + """Surrounding whitespace of a key is removed.""" + provider = DarkEye(BreachHouseAuthKey=f" {_BH_KEY}\n") + check.equal(provider._breach_house_key, _BH_KEY) + check.is_none(provider._hibr_key) + + +# --- both products --------------------------------------------------------- + + +def test_both_keys_one_combined_result(api, both): + """Both products are queried and come back as one result with product and category.""" + api.get(_HIBR_DOMAIN).respond( + json=_hibr_page( + [_hibr_item("1", "ransomware_leak"), _hibr_item("2", "traditional_breach")] + ) + ) + api.get(_BH_SEARCH).respond( + json=_bh_body( + ransomware=[_bh_item("r1")], + breaches=[_bh_item("b1")], + leads=[_bh_item("l1")], + stealers=[_bh_item("s1")], + ) + ) + row = _lookup(both, "example.com", "dns") + + check.equal(row["Provider"], "DarkEye") + check.equal(row["Status"], LookupStatus.OK.value) + check.is_true(row["Result"]) + check.equal(row["Severity"], ResultSeverity.warning.name) + details = row["Details"] + check.equal(details["Count"], 6) + pairs = [(rec["product"], rec["category"]) for rec in details["Records"]] + check.equal( + pairs, + [ + ("HaveIBeenRansom", "ransomware"), + ("HaveIBeenRansom", "breach"), + ("Breach House", "ransomware"), + ("Breach House", "breach"), + ("Breach House", "lead"), + ("Breach House", "stealer"), + ], + ) + check.equal(details["Categories"], {"ransomware": 2, "breach": 2, "lead": 1, "stealer": 1}) + check.equal(details["Products"]["HaveIBeenRansom"]["Records"], 2) + check.equal(details["Products"]["Breach House"]["Records"], 4) + check.is_false(details["Truncated"]) + check.is_not_in("Errors", details) + check.is_in("haveibeenransom.com", row["Reference"]) + check.is_in("breach.house", row["Reference"]) + first = details["Records"][0] + check.equal( + first, + { + "product": "HaveIBeenRansom", + "category": "ransomware", + "id": "1", + "title": "Title 1", + "website": "example.com", + "group": "group", + "country": "US", + "discovered": "2024-05-06", + }, + ) + + +def test_each_key_goes_only_to_its_own_host(api, both): + """The HaveIBeenRansom key is only sent to its host and the Breach House key to its own.""" + api.get(_HIBR_DOMAIN).respond(json=_hibr_page([_hibr_item()])) + api.get(_BH_SEARCH).respond(json=_bh_body(ransomware=[_bh_item()])) + _lookup(both, "example.com", "dns") + + by_host = _hosts(api) + check.equal(set(by_host), {"haveibeenransom.com", "breach.house"}) + for request in by_host["haveibeenransom.com"]: + check.equal(request.headers["Authorization"], f"Bearer {_HIBR_KEY}") + check.is_not_in(_BH_KEY, str(request.url)) + for request in by_host["breach.house"]: + check.equal(request.headers["Authorization"], f"Bearer {_BH_KEY}") + check.is_not_in(_HIBR_KEY, str(request.url)) + # Keys travel in a header only, never in a URL + for call in api.calls: + check.is_not_in("PLACEHOLDER", str(call.request.url)) + bh_request = by_host["breach.house"][0] + check.equal(bh_request.url.params["q"], "example.com") + check.equal(bh_request.url.params["limit"], "25") + check.equal(bh_request.headers["User-Agent"].split("/")[0], "msticpy-darkeye") + + +def test_only_hibr_key_queries_only_hibr(api): + """Without a Breach House key that host is not contacted.""" + api.get(_HIBR_DOMAIN).respond(json=_hibr_page([_hibr_item("1", "ransomware_leak")])) + bh_route = api.get(_BH_SEARCH).respond(json=_bh_body(ransomware=[_bh_item()])) + row = _lookup(DarkEye(HIBRAuthKey=_HIBR_KEY), "example.com", "dns") + + check.equal(bh_route.call_count, 0) + check.equal(set(_hosts(api)), {"haveibeenransom.com"}) + check.equal(row["Details"]["Count"], 1) + check.equal(row["Details"]["Products"]["Breach House"], {"Status": darkeye._NOT_QUERIED}) + check.equal(row["Details"]["Products"]["HaveIBeenRansom"]["Status"], "ok") + check.is_false(row["Reference"].count("breach.house")) + + +def test_only_breach_house_key_queries_only_breach_house(api): + """Without a HaveIBeenRansom key that host is not contacted.""" + hibr_route = api.get(_HIBR_DOMAIN).respond(json=_hibr_page([_hibr_item()])) + api.get(_BH_SEARCH).respond(json=_bh_body(stealers=[_bh_item("s1")])) + row = _lookup(DarkEye(BreachHouseAuthKey=_BH_KEY), "example.com", "dns") + + check.equal(hibr_route.call_count, 0) + check.equal(set(_hosts(api)), {"breach.house"}) + check.equal(row["Details"]["Records"][0]["product"], "Breach House") + check.equal(row["Details"]["Records"][0]["category"], "stealer") + check.equal( + row["Details"]["Products"]["HaveIBeenRansom"], {"Status": darkeye._NOT_QUERIED} + ) + + +# --- failures -------------------------------------------------------------- + + +_FAILURES = [ + pytest.param( + {"status_code": 401, "json": {"error": "invalid_token"}}, "HTTP 401", id="401" + ), + pytest.param({"status_code": 403}, "HTTP 403", id="403"), + pytest.param({"status_code": 429}, "query limit", id="429"), + pytest.param({"status_code": 500}, "HTTP 500", id="500"), + pytest.param({"status_code": 200, "text": ""}, "invalid JSON", id="not json"), + pytest.param({"status_code": 200, "json": ["a list"]}, "unexpected response", id="list"), + pytest.param( + {"status_code": 200, "json": {"success": False}}, + "unexpected response", + id="success false", + ), + pytest.param( + {"status_code": 200, "json": {"error": "x"}}, "unexpected response", id="error key" + ), + pytest.param({"status_code": 200, "json": {}}, "unexpected response", id="no sections"), +] + + +@pytest.mark.parametrize(("failure", "message"), _FAILURES) +def test_hibr_fails_breach_house_records_are_returned(api, both, failure, message): + """If HaveIBeenRansom fails the Breach House records are returned and the failure shown.""" + api.get(_HIBR_DOMAIN).respond(**failure) + api.get(_BH_SEARCH).respond(json=_bh_body(ransomware=[_bh_item("r1")])) + row = _lookup(both, "example.com", "dns") + + check.equal(row["Status"], LookupStatus.OK.value) + check.is_true(row["Result"]) + details = row["Details"] + check.equal([r["product"] for r in details["Records"]], ["Breach House"]) + check.equal(details["Products"]["HaveIBeenRansom"]["Status"], "error") + check.equal(details["Products"]["Breach House"]["Status"], "ok") + check.equal(len(details["Errors"]), 1) + check.is_true(details["Errors"][0].startswith("HaveIBeenRansom: ")) + check.is_in(message, details["Errors"][0]) + + +@pytest.mark.parametrize(("failure", "message"), _FAILURES) +def test_breach_house_fails_hibr_records_are_returned(api, both, failure, message): + """If Breach House fails the HaveIBeenRansom records are returned and the failure shown.""" + api.get(_HIBR_DOMAIN).respond(json=_hibr_page([_hibr_item("1", "ransomware_leak")])) + api.get(_BH_SEARCH).respond(**failure) + row = _lookup(both, "example.com", "dns") + + check.equal(row["Status"], LookupStatus.OK.value) + check.is_true(row["Result"]) + details = row["Details"] + check.equal([r["product"] for r in details["Records"]], ["HaveIBeenRansom"]) + check.equal(details["Products"]["Breach House"]["Status"], "error") + check.is_true(details["Errors"][0].startswith("Breach House: ")) + check.is_in(message, details["Errors"][0]) + + +def test_transport_errors_do_not_echo_the_key(api, both, caplog): + """Exception text may carry request details: only its type is reported, and never logged.""" + caplog.set_level(logging.DEBUG) + api.get(_HIBR_DOMAIN).mock(side_effect=httpx.ConnectError(f"boom Bearer {_HIBR_KEY}")) + api.get(_BH_SEARCH).mock(side_effect=httpx.ReadTimeout(f"slow Bearer {_BH_KEY}")) + row = _lookup(both, "example.com", "dns") + + check.equal(row["Status"], LookupStatus.QUERY_FAILED.value) + check.is_false(row["Result"]) + check.equal(row["Severity"], ResultSeverity.unknown.name) + check.is_in("HaveIBeenRansom: request failed (ConnectError)", row["Details"]) + check.is_in("Breach House: request timed out", row["Details"]) + for text in (str(row), caplog.text): + check.is_not_in(_HIBR_KEY, text) + check.is_not_in(_BH_KEY, text) + + +def test_all_products_failing_is_a_failure_not_an_empty_result(api, both): + """When every product fails the result is a failure, which is not 'not found'.""" + api.get(_HIBR_DOMAIN).respond(status_code=401) + api.get(_BH_SEARCH).respond(status_code=429) + row = _lookup(both, "example.com", "dns") + + check.equal(row["Status"], LookupStatus.QUERY_FAILED.value) + check.is_false(row["Result"]) + check.equal(row["Severity"], ResultSeverity.unknown.name) + check.is_in("HaveIBeenRansom: HTTP 401", row["Details"]) + check.is_in("Breach House: HTTP 429", row["Details"]) + + +def test_single_configured_product_failing_is_a_failure(api): + """With one key, a failure of that product is a failure of the lookup.""" + api.get(_HIBR_DOMAIN).respond(status_code=403) + row = _lookup(DarkEye(HIBRAuthKey=_HIBR_KEY), "example.com", "dns") + + check.equal(row["Status"], LookupStatus.QUERY_FAILED.value) + check.is_false(row["Result"]) + + +def test_hibr_second_page_failing_keeps_first_page_records(api, both): + """Records read before a failing page are kept and the failure is reported.""" + api.get(_HIBR_DOMAIN).respond(json=_hibr_page([_hibr_item("1")], has_next=True)) + api.get(f"{_HIBR_DOMAIN}/2").respond(status_code=500) + api.get(_BH_SEARCH).respond(json=_bh_body()) + row = _lookup(both, "example.com", "dns") + + check.equal([r["id"] for r in row["Details"]["Records"]], ["1"]) + check.is_in("HTTP 500", row["Details"]["Errors"][0]) + + +def test_redirect_is_not_followed_and_key_stays_on_its_host(api, both): + """A redirect must not carry the Authorization header to another host.""" + other = api.get("https://attacker.example/steal").respond(json={}) + api.get(_HIBR_DOMAIN).respond( + status_code=302, headers={"Location": "https://attacker.example/steal"} + ) + api.get(_BH_SEARCH).respond(json=_bh_body(ransomware=[_bh_item()])) + row = _lookup(both, "example.com", "dns") + + check.equal(other.call_count, 0) + check.is_not_in("attacker.example", set(_hosts(api))) + check.is_in("HTTP 302", row["Details"]["Errors"][0]) + check.equal(row["Details"]["Count"], 1) + + +# --- empty results --------------------------------------------------------- + + +def test_empty_results_are_a_negative_result(api, both): + """Both products answer with nothing: Result False, Status OK, information.""" + api.get(_HIBR_DOMAIN).respond(json=_hibr_page([])) + api.get(_BH_SEARCH).respond( + json=_bh_body(ransomware=[], breaches=[], leads=[], stealers=[]) + ) + row = _lookup(both, "example.com", "dns") + + check.equal(row["Status"], LookupStatus.OK.value) + check.is_false(row["Result"]) + check.equal(row["Severity"], ResultSeverity.information.name) + check.equal(row["Details"]["Count"], 0) + check.equal(row["Details"]["Records"], []) + check.equal(row["Details"]["Products"]["HaveIBeenRansom"]["Status"], "ok") + check.equal(row["Details"]["Products"]["Breach House"]["Status"], "ok") + + +def test_breach_house_records_for_other_domains_are_dropped(api): + """Free-text hits that are about another domain are not results.""" + api.get(_BH_SEARCH).respond( + json=_bh_body(ransomware=[_bh_item("x", website="notexample.com")]) + ) + row = _lookup(DarkEye(BreachHouseAuthKey=_BH_KEY), "example.com", "dns") + + check.is_false(row["Result"]) + check.equal(row["Status"], LookupStatus.OK.value) + check.equal(row["Details"]["Count"], 0) + + +# --- hostname matching of Breach House ------------------------------------- + + +def test_breach_house_hostname_boundary(api): + """Breach House is free text: only the host, its subdomains or a standalone title hit.""" + items = [ + _bh_item("a", website="example.com"), + _bh_item("b", website="mail.example.com"), + _bh_item("c", website="notexample.com"), + _bh_item("d", website="example.com.evil.net"), + _bh_item("e", website="", post_title="Leak of example.com data"), + _bh_item("f", website="", post_title="notexample.com only"), + _bh_item("g", website="", post_title="see example.com.other.org"), + _bh_item("h", website="", post_title="mail.example.com leak"), + _bh_item("i", website="", post_title=""), + _bh_item("j", website="WWW.EXAMPLE.COM.", post_title="Upper"), + ] + api.get(_BH_SEARCH).respond(json=_bh_body(ransomware=items)) + row = _lookup(DarkEye(BreachHouseAuthKey=_BH_KEY), "example.com", "dns") + + check.equal([r["id"] for r in row["Details"]["Records"]], ["a", "b", "e", "h", "j"]) + + +def test_hostname_is_normalised(api): + """A hostname is lower-cased before it is sent, and a trailing dot is dropped.""" + check.equal(darkeye._hostname_target(" Example.COM. "), "example.com") + check.equal(darkeye._hostname_target("bücher.example"), "xn--bcher-kva.example") + check.is_none(darkeye._hostname_target("a" * 64 + ".com")) + check.is_none(darkeye._hostname_target("1.2.3.4")) + api.get(_BH_SEARCH).respond(json=_bh_body(ransomware=[_bh_item("a")])) + row = _lookup(DarkEye(BreachHouseAuthKey=_BH_KEY), "EXAMPLE.com", "hostname") + + check.equal(api.calls.last.request.url.params["q"], "example.com") + check.equal(row["Details"]["Count"], 1) + + +@pytest.mark.parametrize("ioc", ["exa*mple.com", "example.c?m", "-bad-.example.com", "a..com"]) +def test_wildcards_and_bad_hostnames_send_nothing(api, both, ioc): + """Both APIs treat * and ? as wildcards: such values are refused before any request.""" + row = _lookup(both, ioc, "dns") + + check.equal(row["Status"], LookupStatus.BAD_FORMAT.value) + check.is_false(row["Result"]) + check.equal(len(api.calls), 0) + + +# --- email ----------------------------------------------------------------- + + +def test_email_is_looked_up_with_exact_match_filter(api, both): + """HaveIBeenRansom is queried by email and Breach House hits must name the exact address.""" + api.get(_HIBR_EMAIL).respond( + json=_hibr_page([_hibr_item("7", "ransomware_leak", website="")]) + ) + api.get(_BH_SEARCH).respond( + json=_bh_body( + ransomware=[ + _bh_item("a", website="", post_title="bob@example.com in a combo list"), + _bh_item("b", website="", post_title="xbob@example.com"), + _bh_item("c", website="", post_title="bob@example.com.au"), + _bh_item("d", website="", post_title="BOB@EXAMPLE.COM"), + _bh_item("e", website="", post_title="alice@example.com"), + _bh_item("f", website="", post_title="bob@example.com-old"), + _bh_item("g", website="", post_title="", description="mail bob@example.com."), + ], + stealers=[_bh_item("s", website="", post_title="", name="bob@example.com")], + ) + ) + row = _lookup(both, "bob@example.com", "email") + + check.equal(api.calls[0].request.url.raw_path, b"/api/metadata/email/bob%40example.com") + bh_request = [c.request for c in api.calls if c.request.url.host == "breach.house"][0] + check.equal(bh_request.url.params["q"], "bob@example.com") + records = row["Details"]["Records"] + check.equal( + [(r["product"], r["id"]) for r in records], + [ + ("HaveIBeenRansom", "7"), + ("Breach House", "a"), + ("Breach House", "d"), + ("Breach House", "g"), + ("Breach House", "s"), + ], + ) + # The description is only read to match; it is never copied into a record + check.is_not_in("mail bob", json.dumps(records)) + + +@pytest.mark.parametrize("ioc", ["*@example.com", "bob?@example.com", "bob@*.example.com"]) +def test_email_wildcards_send_nothing(api, both, ioc): + """An email with a wildcard is refused before any request.""" + row = _lookup(both, ioc, "email") + + check.equal(row["Status"], LookupStatus.BAD_FORMAT.value) + check.equal(len(api.calls), 0) + + +# --- unsupported ----------------------------------------------------------- + + +@pytest.mark.parametrize( + ("ioc", "ioc_type"), + [ + ("185.92.220.35", "ipv4"), + ("https://example.com/a", "url"), + ("7657fcb7d772448a6d8504e4b20168b7", "md5_hash"), + ], +) +def test_unsupported_types_send_nothing(api, both, ioc, ioc_type): + """Only dns, hostname and email are supported.""" + row = _lookup(both, ioc, ioc_type) + + check.equal(row["Status"], LookupStatus.NOT_SUPPORTED.value) + check.equal(len(api.calls), 0) + check.equal(sorted(both.supported_types), ["dns", "email", "hostname"]) + + +def test_query_subtypes_are_not_supported(api, both): + """DarkEye always combines both products: there are no query subtypes.""" + row = _lookup(both, "example.com", "dns", query_type="quick") + + check.is_false(row["Result"]) + check.is_in("does not support", str(row["Details"])) + check.equal(len(api.calls), 0) + + +# --- data rules ------------------------------------------------------------ + + +def test_sensitive_fields_are_never_returned(api, both): + """Credential, description, leak-site and download fields are never read into a result.""" + api.get(_HIBR_DOMAIN).respond( + json=_hibr_page([_hibr_item("1", "ransomware_leak", **_SECRET_FIELDS)]) + ) + api.get(_BH_SEARCH).respond( + json=_bh_body( + ransomware=[_bh_item("r1", **_SECRET_FIELDS)], + stealers=[_bh_item("s1", **_SECRET_FIELDS)], + ) + ) + frame = both.lookup_ioc(ioc="example.com", ioc_type="dns") + row = frame.to_dict(orient="records")[0] + + everything = json.dumps(row, default=str) + frame.to_json() + for field, value in _SECRET_FIELDS.items(): + check.is_not_in(field, everything) + check.is_not_in(str(value), everything) + if isinstance(value, dict): + check.is_not_in("SECRET-HIBR-BLOCK", everything) + for record in row["Details"]["Records"]: + check.equal(set(record), set(dict(DarkEyeRecord(product="p", category="c")))) + check.equal(set(row["RawResult"]), {"Records", "Products"}) + + +def test_only_listing_endpoints_are_requested(api, both): + """No stealer-content endpoint is used: only the metadata and search endpoints.""" + api.get(_HIBR_DOMAIN).respond(json=_hibr_page([_hibr_item()])) + api.get(_BH_SEARCH).respond(json=_bh_body(stealers=[_bh_item("s1")])) + _lookup(both, "example.com", "dns") + api.get(_HIBR_EMAIL).respond(json=_hibr_page([_hibr_item()])) + _lookup(DarkEye(HIBRAuthKey=_HIBR_KEY), "bob@example.com", "email") + + for call in api.calls: + url = call.request.url + if url.host == "haveibeenransom.com": + check.is_true( + url.path.startswith(("/api/metadata/domain/", "/api/metadata/email/")), + url.path, + ) + else: + check.equal((url.host, url.path), ("breach.house", "/api/v1/search")) + check.is_not_in("stealer", url.path) + check.equal(call.request.method, "GET") + + +def test_text_is_cleaned_and_bounded(api): + """Control characters are dropped, lookalike characters normalised, length capped.""" + api.get(_BH_SEARCH).respond( + json=_bh_body( + ransomware=[ + _bh_item( + "a", + post_title="Example‮\x00 Corp\n" + "x" * 500, + group_name="ᴀbc", + country=None, + discovered="2024-05-06 01:02:03", + ) + ] + ) + ) + row = _lookup(DarkEye(BreachHouseAuthKey=_BH_KEY), "example.com", "dns") + record = row["Details"]["Records"][0] + + check.is_true(record["title"].startswith("Example Corp x")) + check.is_true(len(record["title"]) <= 256) + check.is_not_in("\x00", record["title"]) + check.is_not_in("‮", record["title"]) + check.equal(record["country"], "") + check.equal(record["discovered"], "2024-05-06") + + +# --- severity -------------------------------------------------------------- + + +@pytest.mark.parametrize( + ("sections", "expected"), + [ + ({"ransomware": []}, ResultSeverity.information), + ({"leads": [_bh_item("l")]}, ResultSeverity.information), + ({"ransomware": [_bh_item("r")]}, ResultSeverity.warning), + ({"breaches": [_bh_item("b")]}, ResultSeverity.warning), + ({"stealers": [_bh_item("s")]}, ResultSeverity.warning), + ({"leads": [_bh_item("l")], "stealers": [_bh_item("s")]}, ResultSeverity.warning), + ], + ids=["none", "lead only", "ransomware", "breach", "stealer", "lead and stealer"], +) +def test_severity_mapping(api, sections, expected): + """No record or leads only is information, any other record is warning, never high.""" + api.get(_BH_SEARCH).respond(json=_bh_body(**sections)) + row = _lookup(DarkEye(BreachHouseAuthKey=_BH_KEY), "example.com", "dns") + + check.equal(row["Severity"], expected.name) + check.not_equal(row["Severity"], ResultSeverity.high.name) + check.equal(row["Result"], any(sections.values())) + + +def test_hibr_unknown_leak_type_is_a_leak_and_a_warning(api): + """An unknown HaveIBeenRansom leak_type is reported as a plain leak.""" + api.get(_HIBR_DOMAIN).respond(json=_hibr_page([_hibr_item("1", "something_new")])) + row = _lookup(DarkEye(HIBRAuthKey=_HIBR_KEY), "example.com", "dns") + + check.equal(row["Details"]["Records"][0]["category"], "leak") + check.equal(row["Severity"], ResultSeverity.warning.name) + + +# --- paging and limits ----------------------------------------------------- + + +def test_hibr_pagination_dedup_and_documented_shape(api): + """Pages are followed while has_next is true, repeated ids are dropped.""" + api.get(_HIBR_DOMAIN).respond( + json=_hibr_page([_hibr_item("1"), _hibr_item("2", "ransomware_leak")], has_next=True) + ) + api.get(f"{_HIBR_DOMAIN}/2").respond( + json=_hibr_page([_hibr_item("2", "ransomware_leak"), _hibr_item("3")], has_next=True) + ) + # an empty page can still have a next page + api.get(f"{_HIBR_DOMAIN}/3").respond(json=_hibr_page([], has_next=True)) + api.get(f"{_HIBR_DOMAIN}/4").respond( + json=_hibr_page([_hibr_item("4")], has_next=False, documented=True) + ) + row = _lookup(DarkEye(HIBRAuthKey=_HIBR_KEY), "example.com", "dns") + + check.equal([r["id"] for r in row["Details"]["Records"]], ["1", "2", "3", "4"]) + check.equal(len(api.calls), 4) + check.is_false(row["Details"]["Truncated"]) + + +def test_hibr_max_pages_marks_truncated(api): + """Reading stops at the page cap and the result says it may be incomplete.""" + for page in range(1, 8): + url = _HIBR_DOMAIN if page == 1 else f"{_HIBR_DOMAIN}/{page}" + api.get(url).respond(json=_hibr_page([_hibr_item(str(page))], has_next=True)) + row = _lookup(DarkEye(HIBRAuthKey=_HIBR_KEY), "example.com", "dns") + + check.equal(len(api.calls), darkeye._MAX_PAGES) + check.equal(row["Details"]["Count"], darkeye._MAX_PAGES) + check.is_true(row["Details"]["Truncated"]) + check.is_true(row["Details"]["Products"]["HaveIBeenRansom"]["Truncated"]) + + +def test_hibr_record_cap_stops_paging(api): + """No more than the record cap is kept per product, and no more pages are read.""" + items = [_hibr_item(str(i)) for i in range(darkeye._MAX_RECORDS + 5)] + api.get(_HIBR_DOMAIN).respond(json=_hibr_page(items, has_next=True)) + row = _lookup(DarkEye(HIBRAuthKey=_HIBR_KEY), "example.com", "dns") + + check.equal(row["Details"]["Count"], darkeye._MAX_RECORDS) + check.is_true(row["Details"]["Truncated"]) + check.equal(len(api.calls), 1) + + +def test_breach_house_total_larger_than_page_marks_truncated(api): + """A Breach House total larger than the results returned means more exist.""" + body = { + "ransomware": {"results": [_bh_item("a")], "total": 99}, + "breaches": [_bh_item("b")], + } + api.get(_BH_SEARCH).respond(json=body) + row = _lookup(DarkEye(BreachHouseAuthKey=_BH_KEY), "example.com", "dns") + + check.is_true(row["Details"]["Truncated"]) + check.equal(row["Details"]["Count"], 2) + + +def test_breach_house_full_page_without_total_marks_truncated(api): + """A section as long as the limit, with no total, may have more.""" + items = [_bh_item(str(i)) for i in range(darkeye._BREACH_HOUSE_LIMIT)] + api.get(_BH_SEARCH).respond(json={"ransomware": items}) + row = _lookup(DarkEye(BreachHouseAuthKey=_BH_KEY), "example.com", "dns") + + check.is_true(row["Details"]["Truncated"]) + + +def test_breach_house_record_cap_and_dedup(api, monkeypatch): + """The record cap applies to Breach House too and a repeated id is dropped.""" + monkeypatch.setattr(darkeye, "_MAX_RECORDS", 2) + items = [_bh_item("a"), _bh_item("a"), _bh_item("b"), _bh_item("c")] + api.get(_BH_SEARCH).respond(json=_bh_body(ransomware=items)) + row = _lookup(DarkEye(BreachHouseAuthKey=_BH_KEY), "example.com", "dns") + + check.equal([r["id"] for r in row["Details"]["Records"]], ["a", "b"]) + check.is_true(row["Details"]["Truncated"]) + + +def test_same_id_in_two_breach_house_categories_is_kept_twice(api): + """Deduplication is per category: the same id may be a breach and a lead.""" + api.get(_BH_SEARCH).respond(json=_bh_body(breaches=[_bh_item("1")], leads=[_bh_item("1")])) + row = _lookup(DarkEye(BreachHouseAuthKey=_BH_KEY), "example.com", "dns") + + check.equal([r["category"] for r in row["Details"]["Records"]], ["breach", "lead"]) + + +@pytest.mark.parametrize( + "bad_page", + [ + {"sources": [_hibr_item()]}, # no has_next + {"sources": "nope", "has_next": False}, + {"sources": [_hibr_item(), "x"], "has_next": False}, + {"sources": [{"leak_type": "traditional_breach"}], "has_next": False}, # no id/title + {"has_next": False}, + ], + ids=["no has_next", "sources not list", "item not dict", "empty item", "no sources"], +) +def test_malformed_hibr_responses_are_errors(api, bad_page): + """A response that does not have the expected shape is an error, not an empty result.""" + api.get(_HIBR_DOMAIN).respond(json=bad_page) + row = _lookup(DarkEye(HIBRAuthKey=_HIBR_KEY), "example.com", "dns") + + check.equal(row["Status"], LookupStatus.QUERY_FAILED.value) + check.is_in("unexpected response", row["Details"]) + + +@pytest.mark.parametrize( + "bad_body", + [ + {"ransomware": "nope"}, + {"ransomware": {"results": "nope", "total": 1}}, + {"ransomware": [_bh_item(), "x"]}, + {"unrelated": []}, + ], + ids=["str section", "results not list", "item not dict", "no known section"], +) +def test_malformed_breach_house_responses_are_errors(api, bad_body): + """A Breach House body that does not have the expected shape is an error.""" + api.get(_BH_SEARCH).respond(json=bad_body) + row = _lookup(DarkEye(BreachHouseAuthKey=_BH_KEY), "example.com", "dns") + + check.equal(row["Status"], LookupStatus.QUERY_FAILED.value) + check.is_in("unexpected response", row["Details"]) + + +# --- parse_results --------------------------------------------------------- + + +def test_parse_results_of_a_built_result(api, both): + """parse_results reports the same positive and severity from a built result.""" + api.get(_HIBR_DOMAIN).respond(json=_hibr_page([_hibr_item("1", "ransomware_leak")])) + api.get(_BH_SEARCH).respond(json=_bh_body()) + frame = both.lookup_ioc(ioc="example.com", ioc_type="dns") + row = frame.to_dict(orient="records")[0] + + positive, severity, details = both.parse_results(row) + check.is_true(positive) + check.equal(severity, ResultSeverity.warning) + check.equal(details["Count"], 1) + check.equal( + both.parse_results({"Status": 3}), (False, ResultSeverity.information, "Not found.") + ) + + +# --- through TILookup and the config --------------------------------------- + + +def test_loads_from_config_and_looks_up_through_tilookup(api): + """The provider loads from msticpyconfig, gets both keys and returns one result.""" + config_path = Path(get_test_data_path()).parent.joinpath("msticpyconfig-test.yaml") + with custom_mp_config(config_path): + with warnings.catch_warnings(): + warnings.simplefilter("ignore", category=UserWarning) + ti_lookup = TILookup() + provider = ti_lookup.loaded_providers["DarkEye"] + check.is_instance(provider, DarkEye) + # These only arrive if lookup.py lets the two key names through + check.equal(provider._hibr_key, "[PLACEHOLDER]") + check.equal(provider._breach_house_key, "[PLACEHOLDER]") + + api.get(_HIBR_DOMAIN).respond(json=_hibr_page([_hibr_item("1", "ransomware_leak")])) + api.get(_BH_SEARCH).respond(json=_bh_body(leads=[_bh_item("l1")])) + result = ti_lookup.lookup_ioc( + ioc="example.com", ioc_type="dns", providers=["DarkEye"], show_not_supported=True + ) + + check.equal(len(result), 1) + row = result.to_dict(orient="records")[0] + check.equal(row["Provider"], "DarkEye") + check.is_true(row["Result"]) + check.equal(row["Details"]["Count"], 2) + for call in api.calls: + check.equal(call.request.headers["Authorization"], "Bearer [PLACEHOLDER]") + + # a mixed batch: unsupported types are left out, supported ones are looked up + batch = ti_lookup.lookup_iocs( + data={"example.com": "dns", "185.92.220.35": "ipv4"}, providers=["DarkEye"] + ) + check.equal(list(batch["Ioc"]), ["example.com"]) + ti_lookup.browse_results(batch, severities=["information", "warning", "high"]) + + +@pytest.mark.parametrize( + "args", + [ + {"HIBRAuthKey": "[PLACEHOLDER]", "BreachHouseAuthKey": "[PLACEHOLDER]"}, + {"HIBRAuthKey": "[PLACEHOLDER]"}, + {"BreachHouseAuthKey": {"EnvironmentVar": "DARKEYE_BH_KEY"}}, + ], + ids=["both keys", "HIBR only", "Breach House only, from environment"], +) +def test_default_config_accepts_each_key_as_optional(args): + """The shipped config definition accepts either key or both for DarkEye.""" + conf_settings = { + "TIProviders": { + "DarkEye": {"Args": args, "Primary": True, "Provider": "DarkEye"}, + } + } + controls = MpConfigControls( + mp_config_def=get_mpconfig_definitions(), mp_config=conf_settings + ) + + results = controls.validate_setting("TIProviders.DarkEye") + check.equal(results, []) + check.is_in("DarkEye", get_mpconfig_definitions()["TIProviders"]) diff --git a/tests/msticpyconfig-test.yaml b/tests/msticpyconfig-test.yaml index e62fb909..bb82f895 100644 --- a/tests/msticpyconfig-test.yaml +++ b/tests/msticpyconfig-test.yaml @@ -71,6 +71,12 @@ TIProviders: ScanMalware: Primary: True Provider: "ScanMalware" + DarkEye: + Args: + HIBRAuthKey: "[PLACEHOLDER]" + BreachHouseAuthKey: "[PLACEHOLDER]" + Primary: True + Provider: "DarkEye" OpenPageRank: Args: AuthKey: