|
| 1 | +"""Cross-domain campaign query helpers.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Any, Callable, Dict, List, Union |
| 6 | + |
| 7 | +from stix2 import CompositeDataSource, Filter |
| 8 | +from stix2.v21.sdo import Campaign as CampaignV21 |
| 9 | + |
| 10 | +from ...models import Campaign as CampaignModel |
| 11 | + |
| 12 | + |
| 13 | +class CampaignsClient: |
| 14 | + """Campaigns query helper class.""" |
| 15 | + |
| 16 | + def __init__( |
| 17 | + self, |
| 18 | + *, |
| 19 | + data_source: CompositeDataSource, |
| 20 | + remove_fn: Callable | None = None, |
| 21 | + parse_fn: Callable | None = None, |
| 22 | + ) -> None: |
| 23 | + """Initialize the client with a composite data source.""" |
| 24 | + self._data_source = data_source |
| 25 | + self._remove_fn = remove_fn |
| 26 | + self._parse_fn = parse_fn |
| 27 | + |
| 28 | + def get_campaigns(self, *, skip_revoked_deprecated: bool = True, stix_format: bool = True) -> List[Union[CampaignV21, Dict[str, Any]]]: |
| 29 | + """Return campaigns across ATT&CK matrices. |
| 30 | +
|
| 31 | + Parameters |
| 32 | + ---------- |
| 33 | + skip_revoked_deprecated |
| 34 | + When `True`, omit revoked/deprecated campaigns. |
| 35 | + stix_format |
| 36 | + When `True`, return STIX objects; when `False`, parse to the |
| 37 | + `Campaign` Pydantic model. |
| 38 | +
|
| 39 | + Returns |
| 40 | + ------- |
| 41 | + List[Union[CampaignV21, Dict[str, Any]]] |
| 42 | + Campaign objects in the requested format. |
| 43 | + """ |
| 44 | + all_campaigns = self._data_source.query([Filter("type", "=", "campaign")]) |
| 45 | + if skip_revoked_deprecated: |
| 46 | + all_campaigns = self._remove_fn(all_campaigns) |
| 47 | + if not stix_format: |
| 48 | + all_campaigns = self._parse_fn(all_campaigns, CampaignModel) |
| 49 | + return all_campaigns |
| 50 | + |
| 51 | + |
| 52 | + def get_campaign_by_alias(self, *, alias: str, case: bool = True, stix_format: bool = True) -> List[Union[CampaignV21, Dict[str, Any]]]: |
| 53 | + """Return campaigns that match a provided alias. |
| 54 | +
|
| 55 | + Parameters |
| 56 | + ---------- |
| 57 | + alias |
| 58 | + Alias to match. |
| 59 | + case |
| 60 | + When `True`, perform case-sensitive match; otherwise performs |
| 61 | + case-insensitive containment match. |
| 62 | + stix_format |
| 63 | + When `True`, return STIX objects; when `False`, parse to the |
| 64 | + `Campaign` Pydantic model. |
| 65 | +
|
| 66 | + Returns |
| 67 | + ------- |
| 68 | + List[Union[CampaignV21, Dict[str, Any]]] |
| 69 | + Matching campaign objects in the requested format. |
| 70 | + """ |
| 71 | + if not case: |
| 72 | + all_campaigns = self.get_campaigns(stix_format=True) |
| 73 | + out: list[Any] = [] |
| 74 | + for campaign in all_campaigns: |
| 75 | + if "aliases" in campaign.keys(): |
| 76 | + for campaign_alias in campaign["aliases"]: |
| 77 | + if alias.lower() in campaign_alias.lower(): |
| 78 | + out.append(CampaignModel) |
| 79 | + else: |
| 80 | + filter_objects = [Filter("type", "=", "campaign"), Filter("aliases", "contains", alias)] |
| 81 | + out = self._data_source.query(filter_objects) |
| 82 | + |
| 83 | + if not stix_format: |
| 84 | + out = self._parse_fn(out, CampaignModel) |
| 85 | + return out |
| 86 | + |
| 87 | + |
| 88 | + def get_campaigns_since_time(self, *, timestamp: str, stix_format: bool = True) -> List[Union[CampaignV21, Dict[str, Any]]]: |
| 89 | + """Return campaigns created after the provided timestamp. |
| 90 | +
|
| 91 | + Parameters |
| 92 | + ---------- |
| 93 | + timestamp |
| 94 | + Timestamp string for filtering. |
| 95 | + stix_format |
| 96 | + When `True`, return STIX objects; when `False`, parse to the |
| 97 | + `Campaign` Pydantic model. |
| 98 | +
|
| 99 | + Returns |
| 100 | + ------- |
| 101 | + List[Union[CampaignV21, Dict[str, Any]]] |
| 102 | + Campaign objects in the requested format. |
| 103 | + """ |
| 104 | + filter_objects = [Filter("type", "=", "campaign"), Filter("created", ">", timestamp)] |
| 105 | + out = self._data_source.query(filter_objects) |
| 106 | + if not stix_format: |
| 107 | + out = self._parse_fn(out, CampaignModel) |
| 108 | + return out |
0 commit comments