Spaces:
Running
Running
Download src/services/context_aggregator.py from reputeradar/ioc-core: direct link, hf CLI and curl.
- Browser
- Download file 5.93 kB
-
https://huggingface.co/spaces/reputeradar/ioc-core/resolve/main/src/services/context_aggregator.py
- Command line
-
hf download hf://spaces/reputeradar/ioc-core/src/services/context_aggregator.py
-
curl -L -o context_aggregator.py https://huggingface.co/spaces/reputeradar/ioc-core/resolve/main/src/services/context_aggregator.py
5.93 kB
| """ | |
| Context Aggregator Service. | |
| Aggregates threat context from multiple intelligence sources into a unified view. | |
| This is the central point for merging APT, MITRE, malware, and campaign data. | |
| """ | |
| import logging | |
| from typing import Any | |
| from src.models.threat_context import ThreatContext | |
| logger = logging.getLogger(__name__) | |
| class ContextAggregator: | |
| """ | |
| Aggregates threat context from all threat intelligence sources. | |
| This service merges threat actors, MITRE techniques, malware families, | |
| and campaigns from VirusTotal, OTX, ThreatFox, MalwareBazaar, etc. | |
| into a unified ThreatContext object. | |
| """ | |
| def aggregate(self, source_results: dict[str, Any]) -> ThreatContext: | |
| """ | |
| Merge threat context from all source results. | |
| Args: | |
| source_results: Dictionary of source name -> SourceResult objects | |
| Returns: | |
| ThreatContext with aggregated data from all sources | |
| """ | |
| threat_actors = set() | |
| campaigns = set() | |
| malware_families = set() | |
| mitre_tactics = set() | |
| mitre_techniques = set() | |
| tags = set() | |
| sources_contributing = [] | |
| first_seen = None | |
| last_seen = None | |
| for source_name, source_result in source_results.items(): | |
| if source_result is None: | |
| continue | |
| # Handle SourceResult object (has .data attribute) | |
| data = getattr(source_result, 'data', None) | |
| if data is None: | |
| continue | |
| # Skip if source had error | |
| if not data.get("found", False): | |
| continue | |
| contributed = False | |
| # Extract threat actors | |
| actors = data.get("threat_actors", []) | |
| if actors: | |
| for actor in actors: | |
| threat_actors.add(actor) | |
| contributed = True | |
| # Extract campaigns | |
| camps = data.get("campaigns", []) | |
| if camps: | |
| for camp in camps: | |
| campaigns.add(camp) | |
| contributed = True | |
| # Extract malware families | |
| families = data.get("malware_families", []) | |
| if families: | |
| for family in families: | |
| malware_families.add(family) | |
| contributed = True | |
| # Also check malware_family (singular) field | |
| family = data.get("malware_family") | |
| if family: | |
| malware_families.add(family) | |
| contributed = True | |
| # Also check malware field (raw name) | |
| malware = data.get("malware") | |
| if malware and not families and not family: | |
| # Normalize: take first word as family name | |
| normalized = malware.split()[0] if malware else None | |
| if normalized: | |
| malware_families.add(normalized) | |
| contributed = True | |
| # Also check signature field (MalwareBazaar) | |
| signature = data.get("signature") | |
| if signature and not families and not family: | |
| # Extract family from signature (e.g., "Emotet.1234" -> "Emotet") | |
| family_from_sig = signature.split(".")[0] if "." in signature else signature | |
| malware_families.add(family_from_sig) | |
| contributed = True | |
| # Extract MITRE techniques | |
| techniques = data.get("mitre_techniques", []) | |
| if techniques: | |
| for tech in techniques: | |
| mitre_techniques.add(tech) | |
| contributed = True | |
| # Extract tags | |
| source_tags = data.get("tags", []) | |
| if source_tags: | |
| for tag in source_tags[:10]: # Limit per source | |
| tags.add(tag) | |
| # Extract temporal data | |
| fs = data.get("first_seen") | |
| if fs: | |
| if first_seen is None or fs < first_seen: | |
| first_seen = fs | |
| ls = data.get("last_seen") | |
| if ls: | |
| if last_seen is None or ls > last_seen: | |
| last_seen = ls | |
| if contributed: | |
| sources_contributing.append(source_name) | |
| # Calculate confidence based on number of corroborating sources | |
| confidence = self._calculate_confidence( | |
| len(sources_contributing), | |
| len(threat_actors), | |
| len(malware_families), | |
| len(mitre_techniques), | |
| ) | |
| return ThreatContext( | |
| threat_actors=sorted(threat_actors), | |
| campaigns=sorted(campaigns)[:5], | |
| malware_families=sorted(malware_families), | |
| malware_aliases=[], # Can be populated if needed | |
| mitre_tactics=sorted(mitre_tactics), | |
| mitre_techniques=sorted(mitre_techniques), | |
| tags=sorted(tags)[:20], | |
| first_seen=first_seen, | |
| last_seen=last_seen, | |
| confidence=confidence, | |
| sources_contributing=sorted(sources_contributing), | |
| ) | |
| def _calculate_confidence( | |
| self, | |
| num_sources: int, | |
| num_actors: int, | |
| num_families: int, | |
| num_techniques: int, | |
| ) -> str: | |
| """ | |
| Calculate confidence level based on corroboration. | |
| High: 3+ sources contributing OR 2+ threat actors identified | |
| Medium: 2 sources OR 1 threat actor OR 2+ malware families | |
| Low: 1 source with some data | |
| Unknown: No meaningful data | |
| """ | |
| if num_sources >= 3 or num_actors >= 2: | |
| return "high" | |
| elif num_sources >= 2 or num_actors >= 1 or num_families >= 2: | |
| return "medium" | |
| elif num_sources >= 1 or num_families >= 1 or num_techniques >= 1: | |
| return "low" | |
| else: | |
| return "unknown" | |
| # Global aggregator instance | |
| context_aggregator = ContextAggregator() | |