import numpy as np from dataclasses import dataclass from typing import List, Dict, Tuple, Set, Optional import torch from scipy.spatial import Delaunay import time @dataclass class SpatialPoint: coordinates: np.ndarray # (x, y, z) security_level: int connections: Set[int] # IDs of connected points dimension_id: int hardpoint_type: str resistance: float # 0-1 resistance level last_activity: float # timestamp @dataclass class DimensionalSpace: dim_id: int points: Dict[int, SpatialPoint] bridges: List[Tuple[int, int, float]] # point1, point2, bridge_strength topology: np.ndarray # Spatial relationship matrix honeypot_zones: List[List[int]] # Lists of point IDs in honeypot zones monitoring_points: Set[int] # Points that monitor activity class SpatialSecurityMatrix: def __init__( self, num_dimensions: int = 5, points_per_dim: int = 100, honeypot_ratio: float = 0.2 ): self.num_dimensions = num_dimensions self.points_per_dim = points_per_dim self.honeypot_ratio = honeypot_ratio # Initialize dimensional spaces self.dimensions: Dict[int, DimensionalSpace] = {} self._initialize_dimensions() # Track interdimensional bridges self.dim_bridges: List[Tuple[int, int, List[Tuple[int, int]]]] = [] self._create_dimensional_bridges() # Monitoring system self.activity_log = [] self.trace_paths = {} def _initialize_dimensions(self): """Initialize each dimensional space with 3D topology""" for dim_id in range(self.num_dimensions): points = self._generate_spatial_points(dim_id) topology = self._create_topology(points) honeypots = self._designate_honeypot_zones(points) monitors = self._place_monitoring_points(points) self.dimensions[dim_id] = DimensionalSpace( dim_id=dim_id, points=points, bridges=self._create_internal_bridges(points), topology=topology, honeypot_zones=honeypots, monitoring_points=monitors ) def _generate_spatial_points(self, dim_id: int) -> Dict[int, SpatialPoint]: """Generate 3D points with security attributes""" points = {} # Create base grid with random variations for i in range(self.points_per_dim): # Generate position with controlled randomness x = np.random.normal(i % 10, 0.2) y = np.random.normal((i // 10) % 10, 0.2) z = np.random.normal(i // 100, 0.2) # Add some clustered points around "interesting" areas if i % 20 == 0: # Create security clusters cluster_points = self._generate_cluster_points(x, y, z, 5) points.update(cluster_points) points[i] = SpatialPoint( coordinates=np.array([x, y, z]), security_level=self._calculate_security_level(x, y, z), connections=set(), dimension_id=dim_id, hardpoint_type=self._determine_hardpoint_type(i), resistance=np.random.random() * 0.5 + 0.5, # 0.5-1.0 last_activity=time.time() ) return points def _generate_cluster_points( self, x: float, y: float, z: float, num_points: int ) -> Dict[int, SpatialPoint]: """Generate a cluster of points around a center point""" cluster = {} base_id = int(time.time() * 1000) # Unique base ID for cluster for i in range(num_points): # Generate point with small random offset from center offset = np.random.normal(0, 0.1, 3) point_id = base_id + i cluster[point_id] = SpatialPoint( coordinates=np.array([x + offset[0], y + offset[1], z + offset[2]]), security_level=self._calculate_security_level(x, y, z) + 2, connections=set(), dimension_id=self.num_dimensions, # Special dimension for clusters hardpoint_type='CLUSTER_NODE', resistance=np.random.random() * 0.3 + 0.7, # 0.7-1.0 higher resistance last_activity=time.time() ) return cluster def _create_topology(self, points: Dict[int, SpatialPoint]) -> np.ndarray: """Create 3D topology using Delaunay triangulation""" coordinates = np.array([p.coordinates for p in points.values()]) triangulation = Delaunay(coordinates) # Create adjacency matrix from triangulation n_points = len(points) topology = np.zeros((n_points, n_points)) for simplex in triangulation.simplices: for i in range(4): # Tetrahedron has 4 vertices for j in range(i + 1, 4): topology[simplex[i], simplex[j]] = 1 topology[simplex[j], simplex[i]] = 1 return topology def _designate_honeypot_zones( self, points: Dict[int, SpatialPoint] ) -> List[List[int]]: """Create sophisticated honeypot zones""" honeypot_zones = [] total_honeypots = int(len(points) * self.honeypot_ratio) # Create different types of honeypot zones zones_to_create = [ ('HIGH_VALUE', total_honeypots // 3), ('VULNERABLE', total_honeypots // 3), ('BRIDGE', total_honeypots // 3) ] point_ids = list(points.keys()) for zone_type, count in zones_to_create: zone = [] for _ in range(count): if not point_ids: break # Select point based on zone type if zone_type == 'HIGH_VALUE': # Choose points with high security for deception candidate_points = [ p_id for p_id in point_ids if points[p_id].security_level > 7 ] elif zone_type == 'VULNERABLE': # Choose points that appear vulnerable candidate_points = [ p_id for p_id in point_ids if points[p_id].resistance < 0.7 ] else: # BRIDGE # Choose well-connected points candidate_points = [ p_id for p_id in point_ids if len(points[p_id].connections) > 3 ] if candidate_points: selected = np.random.choice(candidate_points) zone.append(selected) point_ids.remove(selected) if zone: honeypot_zones.append(zone) return honeypot_zones def _place_monitoring_points( self, points: Dict[int, SpatialPoint] ) -> Set[int]: """Strategically place monitoring points""" monitor_points = set() # Place monitors at strategic locations for point_id, point in points.items(): # High connectivity points if len(point.connections) > 5: monitor_points.add(point_id) # Points near honeypots if any( np.linalg.norm(point.coordinates - points[p].coordinates) < 0.5 for p in point.connections ): monitor_points.add(point_id) # High security points if point.security_level > 8: monitor_points.add(point_id) return monitor_points def _create_internal_bridges( self, points: Dict[int, SpatialPoint] ) -> List[Tuple[int, int, float]]: """Create bridges between points within a dimension""" bridges = [] point_ids = list(points.keys()) for i in range(len(point_ids)): for j in range(i + 1, len(point_ids)): point1, point2 = points[point_ids[i]], points[point_ids[j]] # Create bridges between close points distance = np.linalg.norm( point1.coordinates - point2.coordinates ) if distance < 1.0: # Close points bridge_strength = 1.0 - (distance / 2) bridges.append((point_ids[i], point_ids[j], bridge_strength)) # Update point connections point1.connections.add(point_ids[j]) point2.connections.add(point_ids[i]) return bridges def _create_dimensional_bridges(self): """Create bridges between dimensions""" for dim1 in range(self.num_dimensions): for dim2 in range(dim1 + 1, self.num_dimensions): bridges = self._connect_dimensions(dim1, dim2) if bridges: self.dim_bridges.append((dim1, dim2, bridges)) def _connect_dimensions( self, dim1: int, dim2: int ) -> List[Tuple[int, int]]: """Create sophisticated connections between two dimensions""" bridges = [] space1 = self.dimensions[dim1] space2 = self.dimensions[dim2] # Find potential bridge points for p1_id, point1 in space1.points.items(): if point1.hardpoint_type == 'HARDPOINT': # Find compatible points in other dimension for p2_id, point2 in space2.points.items(): if point2.hardpoint_type == 'HARDPOINT': # Create bridge if security levels are compatible if abs(point1.security_level - point2.security_level) <= 2: bridges.append((p1_id, p2_id)) return bridges def _calculate_security_level(self, x: float, y: float, z: float) -> int: """Calculate security level based on spatial position""" # Create varying security levels based on position base_level = int( (np.sin(x) + np.cos(y) + np.sin(z)) * 3 + 5 ) # 0-10 range # Add random variation variation = np.random.randint(-1, 2) return max(1, min(10, base_level + variation)) def _determine_hardpoint_type(self, point_id: int) -> str: """Determine the type of hardpoint for a given point""" if point_id % 10 == 0: return 'HARDPOINT' elif point_id % 7 == 0: return 'MONITOR' elif point_id % 5 == 0: return 'BRIDGE_POINT' else: return 'STANDARD' def monitor_activity(self, point_id: int, dimension_id: int) -> Dict: """Monitor activity at a specific point""" space = self.dimensions[dimension_id] point = space.points[point_id] # Update activity timestamp point.last_activity = time.time() # Check if point is in honeypot zone is_honeypot = any( point_id in zone for zone in space.honeypot_zones ) # Get connected monitoring points active_monitors = point.connections & space.monitoring_points # Record activity activity = { 'timestamp': time.time(), 'point_id': point_id, 'dimension': dimension_id, 'is_honeypot': is_honeypot, 'security_level': point.security_level, 'monitoring_points': list(active_monitors), 'connected_points': list(point.connections) } self.activity_log.append(activity) return activity def trace_path(self, start_point: Tuple[int, int]) -> List[Dict]: """Trace path from a starting point through the matrix""" dimension_id, point_id = start_point path = [] current = (dimension_id, point_id) visited = set([current]) while current: dim_id, p_id = current space = self.dimensions[dim_id] point = space.points[p_id] # Record path step path.append({ 'dimension': dim_id, 'point': p_id, 'security_level': point.security_level, 'hardpoint_type': point.hardpoint_type, 'timestamp': time.time() }) # Find next point to trace next_point = None # Check connections in current dimension for connected_id in point.connections: if (dim_id, connected_id) not in visited: next_point = (dim_id, connected_id) break # Check dimensional bridges if not next_point: for bridge_dim1, bridge_dim2, bridges in self.dim_bridges: if dim_id == bridge_dim1: for p1, p2 in bridges: if p1 == p_id and (bridge_dim2, p2) not in visited: next_point = (bridge_dim2, p2) break elif dim_id == bridge_dim2: for p1, p2 in bridges: if p2 == p_id and (bridge_dim1, p1) not in visited: next_point = (bridge_dim1, p1) break if next_point and next_point not in visited: visited.add(next_point) current = next_point else: current = None return path