Spaces:
Running

File size: 5,933 Bytes
c43a9f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd1eee1
c43a9f9
 
 
 
dd1eee1
c43a9f9
 
 
dd1eee1
c43a9f9
 
dd1eee1
c43a9f9
 
 
 
 
 
 
 
 
 
 
 
dd1eee1
c43a9f9
 
 
dd1eee1
c43a9f9
 
 
 
dd1eee1
c43a9f9
 
 
dd1eee1
c43a9f9
dd1eee1
c43a9f9
 
 
 
 
 
dd1eee1
c43a9f9
 
 
 
 
 
dd1eee1
c43a9f9
 
 
 
 
 
dd1eee1
c43a9f9
 
 
 
 
dd1eee1
c43a9f9
 
 
 
 
 
 
 
dd1eee1
c43a9f9
 
 
 
 
 
 
dd1eee1
c43a9f9
 
 
 
 
 
dd1eee1
c43a9f9
 
 
 
 
dd1eee1
c43a9f9
 
 
 
 
dd1eee1
c43a9f9
 
 
 
dd1eee1
c43a9f9
 
dd1eee1
c43a9f9
 
 
 
 
 
 
dd1eee1
c43a9f9
 
 
 
 
 
 
 
 
 
 
 
 
dd1eee1
c43a9f9
 
 
 
 
 
 
 
 
dd1eee1
c43a9f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""
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()