Spaces:
Runtime error
Runtime error
Create app/agents/threat_modeling.py
Browse files
app/agents/threat_modeling.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any
|
| 2 |
+
from app.utils.llm_client import LLMClient
|
| 3 |
+
|
| 4 |
+
class BaseAgent:
|
| 5 |
+
def __init__(self, name: str, template: str, client: LLMClient):
|
| 6 |
+
self.name = name
|
| 7 |
+
self.template = template
|
| 8 |
+
self.client = client
|
| 9 |
+
|
| 10 |
+
def render(self, **kwargs) -> str:
|
| 11 |
+
return self.template.format(**kwargs)
|
| 12 |
+
|
| 13 |
+
def invoke(self, **kwargs) -> Any:
|
| 14 |
+
prompt = self.render(**kwargs)
|
| 15 |
+
return self.client.invoke(prompt)
|
| 16 |
+
|
| 17 |
+
# --- Agent templates ---------------------------------------------------------
|
| 18 |
+
|
| 19 |
+
THREAT_MODELER_PROMPT = """You are a cybersecurity threat-modeling expert.
|
| 20 |
+
Input: {system_description}
|
| 21 |
+
1. Identify assets and trust boundaries.
|
| 22 |
+
2. Apply STRIDE categories.
|
| 23 |
+
3. Map to MITRE ATT&CK techniques.
|
| 24 |
+
Return JSON objects: asset, threat, stride_category, mitre_id, mitigation.
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
THREAT_PATTERN_PROMPT = """Extract reusable threat patterns from this text.
|
| 28 |
+
Input: {text_chunk}
|
| 29 |
+
Return JSON list: pattern_name, description, preconditions, mitigations.
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
ARCH_MAPPER_PROMPT = """Convert this architecture description into a DFD model.
|
| 33 |
+
Input: {components_and_interactions}
|
| 34 |
+
Return JSON: nodes, edges, trust_boundaries.
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
# --- Agent classes -----------------------------------------------------------
|
| 38 |
+
|
| 39 |
+
class ThreatModelerAgent(BaseAgent):
|
| 40 |
+
def run(self, system_description: str) -> Any:
|
| 41 |
+
return self.invoke(system_description=system_description)
|
| 42 |
+
|
| 43 |
+
class ThreatPatternExtractor(BaseAgent):
|
| 44 |
+
def run(self, text_chunk: str) -> Any:
|
| 45 |
+
return self.invoke(text_chunk=text_chunk)
|
| 46 |
+
|
| 47 |
+
class ArchitectureMapper(BaseAgent):
|
| 48 |
+
def run(self, components_and_interactions: str) -> Any:
|
| 49 |
+
return self.invoke(components_and_interactions=components_and_interactions)
|
| 50 |
+
|
| 51 |
+
# --- Factory -----------------------------------------------------------------
|
| 52 |
+
|
| 53 |
+
def build_agents(model_name: str = "gpt-threat-intel-v1"):
|
| 54 |
+
client = LLMClient(model_name)
|
| 55 |
+
return {
|
| 56 |
+
"modeler": ThreatModelerAgent("ThreatModelerAgent", THREAT_MODELER_PROMPT, client),
|
| 57 |
+
"extractor": ThreatPatternExtractor("ThreatPatternExtractor", THREAT_PATTERN_PROMPT, client),
|
| 58 |
+
"mapper": ArchitectureMapper("ArchitectureMapper", ARCH_MAPPER_PROMPT, client),
|
| 59 |
+
}
|