Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /api /v1 /siem.py
| """ | |
| SIEM Integration API v1.0 | |
| """ | |
| from datetime import datetime, timezone | |
| from typing import Dict, List, Optional | |
| from fastapi import APIRouter, HTTPException | |
| from pydantic import BaseModel | |
| from modules.siem_integration import ( | |
| SecurityEvent, | |
| SIEMConfig, | |
| SIEMIntegrationManager, | |
| ) | |
| router = APIRouter() | |
| # Global SIEM manager instance | |
| siem_manager = SIEMIntegrationManager() | |
| class SIEMConnectionRequest(BaseModel): | |
| """SIEM connection configuration""" | |
| name: str | |
| type: str # splunk, elastic, sentinel, qradar | |
| url: str | |
| api_key: Optional[str] = None | |
| username: Optional[str] = None | |
| password: Optional[str] = None | |
| index: Optional[str] = None | |
| verify_ssl: bool = True | |
| class SecurityEventRequest(BaseModel): | |
| """Security event for SIEM ingestion""" | |
| severity: str | |
| event_type: str | |
| source: str | |
| target: str | |
| description: str | |
| cve_id: Optional[str] = None | |
| cvss_score: Optional[float] = None | |
| timestamp: Optional[datetime] = None | |
| class SIEMStatusResponse(BaseModel): | |
| """SIEM connection status""" | |
| name: str | |
| type: str | |
| connected: bool | |
| url: str | |
| class SIEMConnectionResponse(BaseModel): | |
| """SIEM connection response""" | |
| success: bool | |
| message: str | |
| timestamp: str | |
| class SecurityEventResponse(BaseModel): | |
| """Security event submission response""" | |
| success: bool | |
| results: Dict[str, bool] | |
| timestamp: str | |
| class BatchEventResponse(BaseModel): | |
| """Batch security event submission response""" | |
| success: bool | |
| sent_count: int | |
| results: Dict[str, bool] | |
| timestamp: str | |
| async def connect_siem(config: SIEMConnectionRequest): | |
| """ | |
| Connect to a SIEM platform | |
| Supported types: | |
| - splunk: Splunk HTTP Event Collector | |
| - elastic: Elasticsearch/Elastic SIEM | |
| - sentinel: Azure Sentinel | |
| - qradar: IBM QRadar | |
| """ | |
| siem_config = SIEMConfig( | |
| name=config.name, | |
| type=config.type, | |
| url=config.url, | |
| api_key=config.api_key, | |
| username=config.username, | |
| password=config.password, | |
| index=config.index, | |
| verify_ssl=config.verify_ssl, | |
| ) | |
| success = siem_manager.add_siem(siem_config) | |
| if success: | |
| return SIEMConnectionResponse( | |
| success=True, | |
| message=f"Connected to {config.name} ({config.type})", | |
| timestamp=datetime.now(timezone.utc).isoformat(), | |
| ) | |
| else: | |
| raise HTTPException(status_code=400, detail=f"Failed to connect to {config.name}") | |
| async def get_siem_status(): | |
| """Get status of all configured SIEM connections""" | |
| status = siem_manager.get_status() | |
| results = [] | |
| for name, connected in status.items(): | |
| connector = siem_manager.connectors.get(name) | |
| if connector: | |
| results.append( | |
| SIEMStatusResponse( | |
| name=name, | |
| type=connector.config.type, | |
| connected=connected, | |
| url=connector.config.url, | |
| ) | |
| ) | |
| return results | |
| async def send_security_event(event: SecurityEventRequest): | |
| """ | |
| Send security event to all configured SIEMs | |
| Severity levels: critical, high, medium, low, info | |
| """ | |
| security_event = SecurityEvent( | |
| timestamp=event.timestamp or datetime.now(timezone.utc), | |
| severity=event.severity, | |
| event_type=event.event_type, | |
| source=event.source, | |
| target=event.target, | |
| description=event.description, | |
| cve_id=event.cve_id, | |
| cvss_score=event.cvss_score, | |
| ) | |
| results = siem_manager.send_to_all(security_event) | |
| return SecurityEventResponse( | |
| success=any(results.values()), | |
| results=results, | |
| timestamp=datetime.now(timezone.utc).isoformat(), | |
| ) | |
| async def send_security_events_batch(events: List[SecurityEventRequest]): | |
| """Send multiple security events to all SIEMs""" | |
| security_events = [ | |
| SecurityEvent( | |
| timestamp=e.timestamp or datetime.now(timezone.utc), | |
| severity=e.severity, | |
| event_type=e.event_type, | |
| source=e.source, | |
| target=e.target, | |
| description=e.description, | |
| cve_id=e.cve_id, | |
| cvss_score=e.cvss_score, | |
| ) | |
| for e in events | |
| ] | |
| results = siem_manager.send_batch_to_all(security_events) | |
| return BatchEventResponse( | |
| success=any(results.values()), | |
| sent_count=len(events), | |
| results=results, | |
| timestamp=datetime.now(timezone.utc).isoformat(), | |
| ) | |
| async def query_threat_intel(siem_name: str, indicator: str): | |
| """Query threat intelligence from a specific SIEM""" | |
| connector = siem_manager.connectors.get(siem_name) | |
| if not connector: | |
| raise HTTPException(status_code=404, detail=f"SIEM '{siem_name}' not found") | |
| result = connector.query_threat_intel(indicator) | |
| return { | |
| "siem": siem_name, | |
| "indicator": indicator, | |
| "found": result is not None, | |
| "data": result, | |
| } | |
| async def get_supported_siems(): | |
| """List supported SIEM platforms""" | |
| return { | |
| "supported_siems": [ | |
| { | |
| "type": "splunk", | |
| "name": "Splunk HTTP Event Collector", | |
| "auth_methods": ["api_key"], | |
| "features": ["events", "batch", "threat_intel"], | |
| }, | |
| { | |
| "type": "elastic", | |
| "name": "Elasticsearch/Elastic SIEM", | |
| "auth_methods": ["api_key"], | |
| "features": ["events", "batch", "threat_intel"], | |
| }, | |
| { | |
| "type": "sentinel", | |
| "name": "Azure Sentinel", | |
| "auth_methods": ["workspace_id", "shared_key"], | |
| "features": ["events", "batch", "threat_intel"], | |
| }, | |
| { | |
| "type": "qradar", | |
| "name": "IBM QRadar", | |
| "auth_methods": ["api_key", "username/password"], | |
| "features": ["events", "batch", "threat_intel"], | |
| }, | |
| ] | |
| } | |
Xet Storage Details
- Size:
- 6.43 kB
- Xet hash:
- fb5eed4ac728c45dd38019bdad8deac61dea5726ce238f5d29b341ffa0e61a48
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.