import gradio as gr import json import re import time from typing import List, Dict, Tuple import pandas as pd # Mock security entity patterns (in production, use spaCy/transformers) ENTITY_PATTERNS = { 'ThreatActor': [r'APT\d+', r'Cozy Bear', r'Lazarus', r'FIN\d+', r'Carbanak'], 'Vulnerability': [r'CVE-\d{4}-\d{4,7}', r'MS\d{2}-\d{3}'], 'Software': [r'Microsoft \w+', r'Apache \w+', r'Windows \d+', r'Linux', r'Chrome'], 'Tool': [r'Cobalt Strike', r'Metasploit', r'PowerShell', r'Mimikatz', r'PsExec'], 'IOC': [r'\b(?:\d{1,3}\.){3}\d{1,3}\b', r'\b[a-fA-F0-9]{32,64}\b', r'https?://[^\s]+'], 'DetectionRule': [r'SIG-\d{4}-\d{3}', r'YARA-\d+', r'Sigma-\w+'] } # Mock MITRE ATT&CK techniques MITRE_TECHNIQUES = { 'T1059.001': 'PowerShell', 'T1566.001': 'Spearphishing Attachment', 'T1055': 'Process Injection', 'T1003': 'OS Credential Dumping' } class SecurityKnowledgeGraph: def __init__(self): self.entities = [] self.relationships = [] def extract_entities(self, text: str) -> List[Dict]: """Mock entity extraction using regex patterns""" entities = [] entity_id = 0 for entity_type, patterns in ENTITY_PATTERNS.items(): for pattern in patterns: matches = re.finditer(pattern, text, re.IGNORECASE) for match in matches: entities.append({ 'id': entity_id, 'text': match.group(), 'type': entity_type, 'start': match.start(), 'end': match.end(), 'confidence': round(0.85 + (hash(match.group()) % 15) / 100, 2) }) entity_id += 1 # Remove duplicates seen = set() unique_entities = [] for entity in entities: if entity['text'].lower() not in seen: seen.add(entity['text'].lower()) unique_entities.append(entity) return sorted(unique_entities, key=lambda x: x['start']) def extract_relationships(self, entities: List[Dict], text: str) -> List[Dict]: """Mock relationship extraction based on proximity and keywords""" relationships = [] # Define relationship keywords rel_patterns = { 'exploits': ['exploit', 'exploits', 'exploiting', 'leverages'], 'uses': ['uses', 'utilizing', 'deploys', 'employs'], 'targets': ['targets', 'targeting', 'affects'], 'detects': ['detects', 'identifies', 'monitors'], 'mitigates': ['mitigates', 'prevents', 'blocks'] } text_lower = text.lower() for i, source in enumerate(entities): for j, target in enumerate(entities): if i >= j: # Avoid self-relationships and duplicates continue # Check if entities are close to each other in text distance = abs(source['start'] - target['start']) if distance > 200: # Skip if too far apart continue # Find relationship type based on context context_start = min(source['start'], target['start']) - 50 context_end = max(source['end'], target['end']) + 50 context = text_lower[max(0, context_start):context_end] for rel_type, keywords in rel_patterns.items(): if any(keyword in context for keyword in keywords): # Determine relationship direction based on entity types if self._is_valid_relationship(source['type'], target['type'], rel_type): relationships.append({ 'source': source['text'], 'target': target['text'], 'relationship': rel_type, 'confidence': round(0.75 + (hash(source['text'] + target['text']) % 20) / 100, 2), 'source_type': source['type'], 'target_type': target['type'] }) break return relationships def _is_valid_relationship(self, source_type: str, target_type: str, rel_type: str) -> bool: """Check if relationship makes sense given entity types""" valid_relationships = { 'exploits': [('ThreatActor', 'Vulnerability'), ('Tool', 'Vulnerability')], 'uses': [('ThreatActor', 'Tool'), ('ThreatActor', 'Software')], 'targets': [('ThreatActor', 'Software'), ('Tool', 'Software'), ('Vulnerability', 'Software')], 'detects': [('DetectionRule', 'Tool'), ('DetectionRule', 'ThreatActor')], 'affects': [('Vulnerability', 'Software')] } return (source_type, target_type) in valid_relationships.get(rel_type, []) # Initialize the knowledge graph kg = SecurityKnowledgeGraph() def process_threat_intel(text: str) -> Tuple[str, str, str]: """Process threat intelligence text and return formatted results""" if not text.strip(): return "Please provide threat intelligence text to analyze.", "", "" # Extract entities entities = kg.extract_entities(text) # Extract relationships relationships = kg.extract_relationships(entities, text) # Format entities output entities_html = "
Secure AI-powered threat intelligence without vector database vulnerabilities
Build explainable, auditable security relationships instead of relying on risky RAG embeddings
🔒 Secure-by-Design Threat Intelligence - No risky vector embeddings, just explainable relationships
Built for blue teams who need trustworthy, auditable AI in cybersecurity operations