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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
"""
Knowledge Graph Integration for Cyber-LLM
Real-time threat intelligence and cybersecurity knowledge management

Author: Muzan Sano <[email protected]>
"""

import asyncio
import json
import logging
from datetime import datetime, timedelta
from typing import Dict, List, Any, Optional, Tuple, Union, Set
from dataclasses import dataclass, field
from enum import Enum
import numpy as np
import networkx as nx
from neo4j import GraphDatabase
import requests
import feedparser
from bs4 import BeautifulSoup

from ..utils.logging_system import CyberLLMLogger, CyberLLMError, ErrorCategory
from ..memory.persistent_memory import PersistentMemoryManager

class EntityType(Enum):
    """Knowledge graph entity types"""
    VULNERABILITY = "vulnerability"
    THREAT_ACTOR = "threat_actor"
    MALWARE = "malware"
    ATTACK_TECHNIQUE = "attack_technique"
    INDICATOR = "indicator"
    ASSET = "asset"
    ORGANIZATION = "organization"
    CAMPAIGN = "campaign"
    TOOL = "tool"
    MITIGATION = "mitigation"

class RelationType(Enum):
    """Knowledge graph relationship types"""
    EXPLOITS = "exploits"
    MITIGATES = "mitigates"
    TARGETS = "targets"
    USES = "uses"
    ATTRIBUTED_TO = "attributed_to"
    SIMILAR_TO = "similar_to"
    PART_OF = "part_of"
    DETECTS = "detects"
    IMPLEMENTS = "implements"
    COMMUNICATES_WITH = "communicates_with"

class ConfidenceLevel(Enum):
    """Confidence levels for knowledge assertions"""
    LOW = 0.3
    MEDIUM = 0.6
    HIGH = 0.8
    VERY_HIGH = 0.95

@dataclass
class KnowledgeEntity:
    """Knowledge graph entity"""
    entity_id: str
    entity_type: EntityType
    name: str
    
    # Properties
    properties: Dict[str, Any] = field(default_factory=dict)
    
    # Metadata
    created_at: datetime = field(default_factory=datetime.now)
    updated_at: datetime = field(default_factory=datetime.now)
    source: Optional[str] = None
    confidence: float = 0.8
    
    # Relationships
    relationships: List['KnowledgeRelationship'] = field(default_factory=list)
    
    # Tags and classification
    tags: Set[str] = field(default_factory=set)
    classification: Optional[str] = None

@dataclass
class KnowledgeRelationship:
    """Knowledge graph relationship"""
    relationship_id: str
    source_entity: str
    target_entity: str
    relationship_type: RelationType
    
    # Properties
    properties: Dict[str, Any] = field(default_factory=dict)
    
    # Metadata
    created_at: datetime = field(default_factory=datetime.now)
    confidence: float = 0.8
    source: Optional[str] = None
    
    # Temporal aspects
    valid_from: Optional[datetime] = None
    valid_until: Optional[datetime] = None

@dataclass
class ThreatIntelligenceData:
    """Threat intelligence data structure"""
    intel_id: str
    title: str
    description: str
    
    # Classification
    threat_type: str
    severity: str
    confidence: ConfidenceLevel
    
    # Temporal information
    discovered_at: datetime
    published_at: Optional[datetime] = None
    expires_at: Optional[datetime] = None
    
    # Indicators
    indicators: List[Dict[str, Any]] = field(default_factory=list)
    
    # Attribution
    threat_actors: List[str] = field(default_factory=list)
    campaigns: List[str] = field(default_factory=list)
    
    # Source information
    source: str
    source_reliability: str
    
    # References
    references: List[str] = field(default_factory=list)
    
    # Structured data
    mitre_techniques: List[str] = field(default_factory=list)
    affected_products: List[str] = field(default_factory=list)

class CyberKnowledgeGraph:
    """Comprehensive cybersecurity knowledge graph"""
    
    def __init__(self, 
                 neo4j_uri: str,
                 neo4j_user: str,
                 neo4j_password: str,
                 memory_manager: PersistentMemoryManager,
                 logger: Optional[CyberLLMLogger] = None):
        
        self.memory_manager = memory_manager
        self.logger = logger or CyberLLMLogger(name="knowledge_graph")
        
        # Graph database connection
        self.driver = GraphDatabase.driver(neo4j_uri, auth=(neo4j_user, neo4j_password))
        
        # In-memory graph for fast operations
        self.graph = nx.MultiDiGraph()
        
        # Entity and relationship tracking
        self.entities = {}
        self.relationships = {}
        
        # Intelligence sources
        self.threat_intel_sources = {}
        self.cve_sources = {}
        self.news_sources = {}
        
        # Update tracking
        self.last_update = {}
        self.update_frequencies = {}
        
        # Initialize knowledge graph
        asyncio.create_task(self._initialize_knowledge_graph())
        
        self.logger.info("Cyber Knowledge Graph initialized")
    
    async def _initialize_knowledge_graph(self):
        """Initialize knowledge graph with base data"""
        
        try:
            # Create database constraints and indexes
            await self._create_database_schema()
            
            # Load base cybersecurity ontology
            await self._load_base_ontology()
            
            # Initialize threat intelligence sources
            await self._initialize_threat_intel_sources()
            
            # Start periodic updates
            asyncio.create_task(self._periodic_updates())
            
            self.logger.info("Knowledge graph initialization completed")
            
        except Exception as e:
            self.logger.error("Knowledge graph initialization failed", error=str(e))
    
    async def add_entity(self, entity: KnowledgeEntity) -> bool:
        """Add entity to knowledge graph"""
        
        try:
            # Store in Neo4j
            with self.driver.session() as session:
                query = f"""
                CREATE (e:{entity.entity_type.value.title()} {{
                    entity_id: $entity_id,
                    name: $name,
                    properties: $properties,
                    created_at: $created_at,
                    updated_at: $updated_at,
                    source: $source,
                    confidence: $confidence,
                    tags: $tags,
                    classification: $classification
                }})
                """
                
                session.run(query, {
                    "entity_id": entity.entity_id,
                    "name": entity.name,
                    "properties": json.dumps(entity.properties),
                    "created_at": entity.created_at.isoformat(),
                    "updated_at": entity.updated_at.isoformat(),
                    "source": entity.source,
                    "confidence": entity.confidence,
                    "tags": list(entity.tags),
                    "classification": entity.classification
                })
            
            # Store in memory
            self.entities[entity.entity_id] = entity
            self.graph.add_node(entity.entity_id, **entity.properties)
            
            self.logger.info("Entity added to knowledge graph",
                           entity_id=entity.entity_id,
                           entity_type=entity.entity_type.value)
            
            return True
            
        except Exception as e:
            self.logger.error("Failed to add entity", error=str(e))
            return False
    
    async def add_relationship(self, relationship: KnowledgeRelationship) -> bool:
        """Add relationship to knowledge graph"""
        
        try:
            # Store in Neo4j
            with self.driver.session() as session:
                query = f"""
                MATCH (source {{entity_id: $source_entity}})
                MATCH (target {{entity_id: $target_entity}})
                CREATE (source)-[r:{relationship.relationship_type.value.upper()} {{
                    relationship_id: $relationship_id,
                    properties: $properties,
                    created_at: $created_at,
                    confidence: $confidence,
                    source: $source,
                    valid_from: $valid_from,
                    valid_until: $valid_until
                }}]->(target)
                """
                
                session.run(query, {
                    "source_entity": relationship.source_entity,
                    "target_entity": relationship.target_entity,
                    "relationship_id": relationship.relationship_id,
                    "properties": json.dumps(relationship.properties),
                    "created_at": relationship.created_at.isoformat(),
                    "confidence": relationship.confidence,
                    "source": relationship.source,
                    "valid_from": relationship.valid_from.isoformat() if relationship.valid_from else None,
                    "valid_until": relationship.valid_until.isoformat() if relationship.valid_until else None
                })
            
            # Store in memory
            self.relationships[relationship.relationship_id] = relationship
            self.graph.add_edge(
                relationship.source_entity,
                relationship.target_entity,
                key=relationship.relationship_id,
                relationship_type=relationship.relationship_type.value,
                **relationship.properties
            )
            
            self.logger.info("Relationship added to knowledge graph",
                           relationship_id=relationship.relationship_id,
                           relationship_type=relationship.relationship_type.value)
            
            return True
            
        except Exception as e:
            self.logger.error("Failed to add relationship", error=str(e))
            return False
    
    async def query_entities(self, 
                           entity_type: Optional[EntityType] = None,
                           properties: Optional[Dict[str, Any]] = None,
                           tags: Optional[Set[str]] = None) -> List[KnowledgeEntity]:
        """Query entities from knowledge graph"""
        
        try:
            # Build query
            conditions = []
            params = {}
            
            if entity_type:
                label = entity_type.value.title()
            else:
                label = ""
            
            if properties:
                for key, value in properties.items():
                    conditions.append(f"e.properties CONTAINS $prop_{key}")
                    params[f"prop_{key}"] = json.dumps({key: value})
            
            if tags:
                for i, tag in enumerate(tags):
                    conditions.append(f"$tag_{i} IN e.tags")
                    params[f"tag_{i}"] = tag
            
            where_clause = " AND ".join(conditions) if conditions else ""
            if where_clause:
                where_clause = "WHERE " + where_clause
            
            query = f"""
            MATCH (e{':' + label if label else ''})
            {where_clause}
            RETURN e
            """
            
            # Execute query
            with self.driver.session() as session:
                result = session.run(query, params)
                
                entities = []
                for record in result:
                    node = record["e"]
                    entity = KnowledgeEntity(
                        entity_id=node["entity_id"],
                        entity_type=EntityType(node.labels),
                        name=node["name"],
                        properties=json.loads(node.get("properties", "{}")),
                        created_at=datetime.fromisoformat(node["created_at"]),
                        updated_at=datetime.fromisoformat(node["updated_at"]),
                        source=node.get("source"),
                        confidence=node.get("confidence", 0.8),
                        tags=set(node.get("tags", [])),
                        classification=node.get("classification")
                    )
                    entities.append(entity)
                
                return entities
            
        except Exception as e:
            self.logger.error("Entity query failed", error=str(e))
            return []
    
    async def find_paths(self, 
                        source_entity: str, 
                        target_entity: str,
                        max_depth: int = 3) -> List[List[str]]:
        """Find paths between entities"""
        
        try:
            # Use NetworkX for efficient path finding
            if self.graph.has_node(source_entity) and self.graph.has_node(target_entity):
                paths = list(nx.all_simple_paths(
                    self.graph, 
                    source_entity, 
                    target_entity, 
                    cutoff=max_depth
                ))
                return paths
            
            return []
            
        except Exception as e:
            self.logger.error("Path finding failed", error=str(e))
            return []
    
    async def get_entity_neighbors(self, entity_id: str, relationship_types: Optional[List[RelationType]] = None) -> List[KnowledgeEntity]:
        """Get neighboring entities"""
        
        try:
            neighbors = []
            
            if entity_id in self.graph:
                for neighbor in self.graph.neighbors(entity_id):
                    if relationship_types:
                        # Check if any edge has the required relationship type
                        edges = self.graph[entity_id][neighbor]
                        for edge_data in edges.values():
                            if edge_data.get('relationship_type') in [rt.value for rt in relationship_types]:
                                if neighbor in self.entities:
                                    neighbors.append(self.entities[neighbor])
                                break
                    else:
                        if neighbor in self.entities:
                            neighbors.append(self.entities[neighbor])
            
            return neighbors
            
        except Exception as e:
            self.logger.error("Failed to get entity neighbors", error=str(e))
            return []

class ThreatIntelligenceAggregator:
    """Aggregates threat intelligence from multiple sources"""
    
    def __init__(self, 
                 knowledge_graph: CyberKnowledgeGraph,
                 logger: Optional[CyberLLMLogger] = None):
        
        self.knowledge_graph = knowledge_graph
        self.logger = logger or CyberLLMLogger(name="threat_intel")
        
        # Intelligence sources
        self.sources = {
            "cve": {
                "url": "https://cve.mitre.org/data/downloads/",
                "update_frequency": timedelta(hours=6)
            },
            "mitre_attack": {
                "url": "https://attack.mitre.org/",
                "update_frequency": timedelta(days=1)
            },
            "threat_feeds": []
        }
        
        # Processing state
        self.last_updates = {}
        self.processing_queue = asyncio.Queue()
        
        # Start processing worker
        asyncio.create_task(self._processing_worker())
        
        self.logger.info("Threat Intelligence Aggregator initialized")
    
    async def aggregate_cve_data(self) -> int:
        """Aggregate CVE data from MITRE"""
        
        try:
            self.logger.info("Starting CVE data aggregation")
            
            # Fetch CVE JSON feed
            cve_url = "https://cve.mitre.org/data/downloads/allitems.json"
            async with aiohttp.ClientSession() as session:
                async with session.get(cve_url) as response:
                    if response.status == 200:
                        cve_data = await response.json()
                    else:
                        raise Exception(f"Failed to fetch CVE data: {response.status}")
            
            processed_count = 0
            
            # Process CVE entries
            for cve_item in cve_data.get("CVE_Items", []):
                cve_id = cve_item["cve"]["CVE_data_meta"]["ID"]
                
                # Create CVE entity
                entity = KnowledgeEntity(
                    entity_id=cve_id,
                    entity_type=EntityType.VULNERABILITY,
                    name=cve_id,
                    properties={
                        "description": cve_item["cve"]["description"]["description_data"][0]["value"],
                        "published_date": cve_item.get("publishedDate"),
                        "modified_date": cve_item.get("lastModifiedDate"),
                        "cvss_score": self._extract_cvss_score(cve_item),
                        "severity": self._determine_severity(cve_item),
                        "affected_products": self._extract_affected_products(cve_item)
                    },
                    source="mitre_cve",
                    confidence=0.95
                )
                
                await self.knowledge_graph.add_entity(entity)
                processed_count += 1
                
                # Add relationships to affected products
                for product in entity.properties.get("affected_products", []):
                    # Create or get product entity
                    product_entity = await self._get_or_create_product_entity(product)
                    
                    # Create vulnerability relationship
                    relationship = KnowledgeRelationship(
                        relationship_id=f"{cve_id}_affects_{product_entity.entity_id}",
                        source_entity=cve_id,
                        target_entity=product_entity.entity_id,
                        relationship_type=RelationType.TARGETS,
                        confidence=0.9,
                        source="mitre_cve"
                    )
                    
                    await self.knowledge_graph.add_relationship(relationship)
            
            self.last_updates["cve"] = datetime.now()
            
            self.logger.info("CVE data aggregation completed",
                           processed_count=processed_count)
            
            return processed_count
            
        except Exception as e:
            self.logger.error("CVE data aggregation failed", error=str(e))
            return 0
    
    async def aggregate_mitre_attack(self) -> int:
        """Aggregate MITRE ATT&CK framework data"""
        
        try:
            self.logger.info("Starting MITRE ATT&CK data aggregation")
            
            # MITRE ATT&CK STIX data
            attack_url = "https://raw.githubusercontent.com/mitre/cti/master/enterprise-attack/enterprise-attack.json"
            
            async with aiohttp.ClientSession() as session:
                async with session.get(attack_url) as response:
                    if response.status == 200:
                        attack_data = await response.json()
                    else:
                        raise Exception(f"Failed to fetch MITRE ATT&CK data: {response.status}")
            
            processed_count = 0
            
            # Process STIX objects
            for stix_object in attack_data.get("objects", []):
                if stix_object["type"] == "attack-pattern":
                    # Create technique entity
                    technique_id = stix_object.get("external_references", [{}])[0].get("external_id", "")
                    
                    entity = KnowledgeEntity(
                        entity_id=technique_id,
                        entity_type=EntityType.ATTACK_TECHNIQUE,
                        name=stix_object["name"],
                        properties={
                            "description": stix_object.get("description", ""),
                            "kill_chain_phases": [phase["phase_name"] for phase in stix_object.get("kill_chain_phases", [])],
                            "platforms": stix_object.get("x_mitre_platforms", []),
                            "tactics": [ref["external_id"] for ref in stix_object.get("external_references", []) if ref.get("source_name") == "mitre-attack"]
                        },
                        source="mitre_attack",
                        confidence=0.98
                    )
                    
                    await self.knowledge_graph.add_entity(entity)
                    processed_count += 1
            
            self.last_updates["mitre_attack"] = datetime.now()
            
            self.logger.info("MITRE ATT&CK data aggregation completed",
                           processed_count=processed_count)
            
            return processed_count
            
        except Exception as e:
            self.logger.error("MITRE ATT&CK data aggregation failed", error=str(e))
            return 0
    
    async def _processing_worker(self):
        """Background worker for processing intelligence data"""
        
        while True:
            try:
                # Check for scheduled updates
                for source, config in self.sources.items():
                    last_update = self.last_updates.get(source)
                    update_frequency = config.get("update_frequency")
                    
                    if not last_update or (datetime.now() - last_update) > update_frequency:
                        if source == "cve":
                            await self.aggregate_cve_data()
                        elif source == "mitre_attack":
                            await self.aggregate_mitre_attack()
                
                # Wait before next check
                await asyncio.sleep(3600)  # Check every hour
                
            except Exception as e:
                self.logger.error("Intelligence processing worker error", error=str(e))
                await asyncio.sleep(300)  # Wait 5 minutes on error

# Factory functions
def create_knowledge_graph(neo4j_uri: str,
                         neo4j_user: str,
                         neo4j_password: str,
                         memory_manager: PersistentMemoryManager,
                         **kwargs) -> CyberKnowledgeGraph:
    """Create cyber knowledge graph"""
    return CyberKnowledgeGraph(neo4j_uri, neo4j_user, neo4j_password, memory_manager, **kwargs)

def create_threat_intelligence_aggregator(knowledge_graph: CyberKnowledgeGraph,
                                        **kwargs) -> ThreatIntelligenceAggregator:
    """Create threat intelligence aggregator"""
    return ThreatIntelligenceAggregator(knowledge_graph, **kwargs)