mirror of
https://github.com/FunnyWolf/agentic-soc-platform.git
synced 2026-08-22 13:12:56 +02:00
update
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
from typing import Annotated, List, Any
|
||||
|
||||
from langchain_core.messages import AIMessage, HumanMessage
|
||||
from langgraph.graph import END, StateGraph, add_messages
|
||||
from langgraph.graph.state import CompiledStateGraph
|
||||
from pydantic import BaseModel, Field, ConfigDict
|
||||
|
||||
from Lib.api import get_current_time_str
|
||||
from Lib.baseplaybook import LanggraphPlaybook
|
||||
from PLUGINS.LLM.llmapi import LLMAPI
|
||||
|
||||
AGENT_NODE_SUMMARIZE = "SUMMARIZE"
|
||||
AGENT_NODE_REPORT = "REPORT"
|
||||
MAX_EVIDENCE_CHARS = 120000
|
||||
|
||||
_graph_agent_instance = None
|
||||
|
||||
|
||||
def get_report_graph() -> CompiledStateGraph:
|
||||
global _graph_agent_instance
|
||||
if _graph_agent_instance is None:
|
||||
_graph_agent_instance = GraphAgentReport()
|
||||
return _graph_agent_instance.graph
|
||||
|
||||
|
||||
def _normalize_content(content: Any) -> str:
|
||||
if content is None:
|
||||
return ""
|
||||
if isinstance(content, str):
|
||||
return content
|
||||
return str(content)
|
||||
|
||||
|
||||
def _build_evidence_entries(messages: List[Any]) -> List[dict]:
|
||||
entries = []
|
||||
for idx, message in enumerate(messages, 1):
|
||||
role = getattr(message, "type", message.__class__.__name__)
|
||||
content = _normalize_content(getattr(message, "content", ""))
|
||||
tool_calls = getattr(message, "tool_calls", None)
|
||||
if tool_calls:
|
||||
content = f"{content}\n\nTool calls: {tool_calls}"
|
||||
entries.append({"id": f"E{idx}", "role": role, "content": content})
|
||||
return entries
|
||||
|
||||
|
||||
def _build_evidence_text(entries: List[dict]) -> str:
|
||||
lines = []
|
||||
for entry in entries:
|
||||
lines.append(f"{entry['id']} | role={entry['role']} | {entry['content']}")
|
||||
return "\n\n".join(lines)
|
||||
|
||||
|
||||
def _build_citations(entries: List[dict]) -> List[dict]:
|
||||
citations = []
|
||||
for entry in entries:
|
||||
excerpt = entry["content"].replace("\n", " ").strip()
|
||||
if len(excerpt) > 500:
|
||||
excerpt = excerpt[:500] + "..."
|
||||
citations.append({"id": entry["id"], "role": entry["role"], "excerpt": excerpt})
|
||||
return citations
|
||||
|
||||
|
||||
def _split_entries(entries: List[dict], max_chars: int) -> List[List[dict]]:
|
||||
chunks = []
|
||||
current = []
|
||||
current_len = 0
|
||||
for entry in entries:
|
||||
entry_text = f"{entry['id']} | role={entry['role']} | {entry['content']}"
|
||||
entry_len = len(entry_text) + 2
|
||||
if current and current_len + entry_len > max_chars:
|
||||
chunks.append(current)
|
||||
current = []
|
||||
current_len = 0
|
||||
current.append(entry)
|
||||
current_len += entry_len
|
||||
if current:
|
||||
chunks.append(current)
|
||||
return chunks
|
||||
|
||||
|
||||
class AgentState(BaseModel):
|
||||
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||||
messages: Annotated[List[Any], add_messages] = Field(default_factory=list)
|
||||
summary_digest: str = Field(default="")
|
||||
report_markdown: str = Field(default="")
|
||||
citations: List[dict] = Field(default_factory=list)
|
||||
|
||||
|
||||
class GraphAgentReport(LanggraphPlaybook):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self._summary_prompt_template = self.load_system_prompt_template("summary")
|
||||
self._summary_merge_prompt_template = self.load_system_prompt_template("summary_merge")
|
||||
self._report_prompt_template = self.load_system_prompt_template("system")
|
||||
self._llm_api = LLMAPI()
|
||||
self._llm_base = self._llm_api.get_model(tag=["powerful"])
|
||||
self.graph = self._build_graph()
|
||||
|
||||
def _build_graph(self) -> CompiledStateGraph:
|
||||
def summarize_node(state: AgentState):
|
||||
entries = _build_evidence_entries(state.messages)
|
||||
citations = _build_citations(entries)
|
||||
evidence_text = _build_evidence_text(entries)
|
||||
if len(evidence_text) <= MAX_EVIDENCE_CHARS:
|
||||
system_message = self._summary_prompt_template.format(
|
||||
CURRENT_UTC_TIME=get_current_time_str(),
|
||||
REPORT_LANGUAGE="English"
|
||||
)
|
||||
human_message = HumanMessage(content=evidence_text)
|
||||
response: AIMessage = self._llm_base.invoke([system_message, human_message])
|
||||
return {"summary_digest": response.content, "citations": citations}
|
||||
chunks = _split_entries(entries, MAX_EVIDENCE_CHARS)
|
||||
partials = []
|
||||
for chunk in chunks:
|
||||
chunk_text = _build_evidence_text(chunk)
|
||||
system_message = self._summary_prompt_template.format(
|
||||
CURRENT_UTC_TIME=get_current_time_str(),
|
||||
REPORT_LANGUAGE="English"
|
||||
)
|
||||
human_message = HumanMessage(content=f"PARTIAL_DIGEST: true\n\n{chunk_text}")
|
||||
response: AIMessage = self._llm_base.invoke([system_message, human_message])
|
||||
partials.append(response.content)
|
||||
merge_system_message = self._summary_merge_prompt_template.format(
|
||||
CURRENT_UTC_TIME=get_current_time_str(),
|
||||
REPORT_LANGUAGE="English"
|
||||
)
|
||||
merge_human_message = HumanMessage(content="\n\n".join(partials))
|
||||
merged: AIMessage = self._llm_base.invoke([merge_system_message, merge_human_message])
|
||||
return {"summary_digest": merged.content, "citations": citations}
|
||||
|
||||
def report_node(state: AgentState):
|
||||
system_message = self._report_prompt_template.format(
|
||||
CURRENT_UTC_TIME=get_current_time_str(),
|
||||
REPORT_LANGUAGE="English"
|
||||
)
|
||||
human_message = HumanMessage(content=state.summary_digest)
|
||||
response: AIMessage = self._llm_base.invoke([system_message, human_message])
|
||||
return {
|
||||
"messages": [response],
|
||||
"report_markdown": response.content
|
||||
}
|
||||
|
||||
workflow = StateGraph(AgentState)
|
||||
workflow.add_node(AGENT_NODE_SUMMARIZE, summarize_node)
|
||||
workflow.add_node(AGENT_NODE_REPORT, report_node)
|
||||
workflow.set_entry_point(AGENT_NODE_SUMMARIZE)
|
||||
workflow.add_edge(AGENT_NODE_SUMMARIZE, AGENT_NODE_REPORT)
|
||||
workflow.add_edge(AGENT_NODE_REPORT, END)
|
||||
return workflow.compile()
|
||||
@@ -0,0 +1,35 @@
|
||||
You are a SOC summarization subgraph. Your task is to read the provided evidence list and produce a summary digest for report generation.
|
||||
|
||||
Hard rules:
|
||||
- Output language: {REPORT_LANGUAGE}.
|
||||
- Use ONLY information present in the evidence list. Do not invent facts.
|
||||
- Every material claim MUST be backed by a cited Evidence ID.
|
||||
- Do not drop information. If you must compress, keep all material facts and preserve all IOCs, assets, identities, alerts, and timestamps.
|
||||
- The evidence list is ordered and uses Evidence IDs (E1, E2, ...). Use these IDs in your output.
|
||||
- Output must be valid JSON only, no surrounding text.
|
||||
|
||||
If the input begins with "PARTIAL_DIGEST: true", produce a partial digest in the same JSON schema, preserving all items from the chunk.
|
||||
|
||||
JSON schema to output:
|
||||
{
|
||||
"evidence_index": [{"id": "E1", "role": "human|ai|tool|system|other", "excerpt": "..."}],
|
||||
"facts": [{"statement": "...", "evidence": ["E1", "E2"]}],
|
||||
"timeline": [{"time": "...", "event": "...", "evidence": ["E1"]}],
|
||||
"observations": [{"category": "Network|Endpoint|Identity|Email|Cloud|Other", "detail": "...", "evidence": ["E1"]}],
|
||||
"entities": {
|
||||
"assets": ["..."],
|
||||
"identities": ["..."],
|
||||
"iocs": ["..."],
|
||||
"alerts": ["..."],
|
||||
"tools_queries": ["..."],
|
||||
"files": ["..."],
|
||||
"processes": ["..."],
|
||||
"urls_domains": ["..."],
|
||||
"emails": ["..."],
|
||||
"ips": ["..."],
|
||||
"hashes": ["..."]
|
||||
},
|
||||
"decisions_actions": [{"action": "...", "evidence": ["E1"]}],
|
||||
"uncertainties": ["..."]
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
You are a SOC digest merger. Your task is to merge multiple JSON summary digests into one comprehensive digest.
|
||||
|
||||
Hard rules:
|
||||
- Output language: {REPORT_LANGUAGE}.
|
||||
- Use ONLY information present in the partial digests. Do not invent facts.
|
||||
- Do not drop information. If duplicates exist, consolidate but keep all unique facts and evidence IDs.
|
||||
- Output must be valid JSON only, no surrounding text.
|
||||
|
||||
Merge logic:
|
||||
- evidence_index: union by id; keep the most informative excerpt.
|
||||
- facts, timeline, observations, decisions_actions: union items; merge if statements are equivalent; combine evidence lists.
|
||||
- entities: union unique items per list.
|
||||
- uncertainties: union unique items.
|
||||
|
||||
Output JSON schema must match the summary digest schema exactly.
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
You are a SOC reporting subgraph. Your task is to read the provided summary digest and generate a cybersecurity analysis
|
||||
report in Markdown.
|
||||
|
||||
Hard rules:
|
||||
|
||||
- Output language: {REPORT_LANGUAGE}. If the user explicitly requests another language in the messages, follow the user
|
||||
request.
|
||||
- Use ONLY information present in the summary digest. Do not invent facts.
|
||||
- Every material claim MUST be backed by a cited Evidence ID.
|
||||
- If evidence is missing, write "Unknown" or "Not provided in messages".
|
||||
- The output must strictly follow the fixed template and headings below.
|
||||
- Evidence IDs must reference the order of the input messages, starting from E1 for the first message.
|
||||
- Provide a concise evidence excerpt in the Evidence Index.
|
||||
- Do not add extra sections or reorder headings.
|
||||
|
||||
Report time: {CURRENT_UTC_TIME}
|
||||
|
||||
# SOC Cybersecurity Analysis Report
|
||||
|
||||
## Executive Summary
|
||||
|
||||
- Overall assessment:
|
||||
- Severity:
|
||||
- Rationale (Evidence: )
|
||||
|
||||
## Detection and Alert Context
|
||||
|
||||
- Detection sources:
|
||||
- Rule/signature/alert IDs:
|
||||
- Initial trigger:
|
||||
- Confidence level:
|
||||
|
||||
## Scope and Assumptions
|
||||
|
||||
- Data sources: Summary digest only
|
||||
- Time window inferred:
|
||||
- Environment inferred:
|
||||
- Known limitations:
|
||||
|
||||
## Key Findings (TL;DR)
|
||||
-
|
||||
|
||||
## Timeline (Inferred)
|
||||
|
||||
| Time (UTC if available) | Event | Supporting Evidence IDs |
|
||||
|-------------------------|-------|-------------------------|
|
||||
| | | |
|
||||
|
||||
## Observations and Evidence
|
||||
|
||||
### Network
|
||||
-
|
||||
|
||||
### Endpoint
|
||||
-
|
||||
|
||||
### Identity and Access
|
||||
-
|
||||
|
||||
### Email and Collaboration
|
||||
-
|
||||
|
||||
### Cloud and SaaS
|
||||
-
|
||||
|
||||
### Other
|
||||
-
|
||||
|
||||
## Asset and Identity Context
|
||||
|
||||
- Affected assets (hostnames/IPs/roles):
|
||||
- Affected identities (users/roles/privilege):
|
||||
- Business criticality:
|
||||
|
||||
## Threat Assessment
|
||||
|
||||
- Likely attack path / kill chain:
|
||||
- MITRE ATT&CK mapping (Technique + rationale + Evidence IDs):
|
||||
- Adversary intent or goal (if inferable):
|
||||
|
||||
## Impact and Exposure Assessment
|
||||
|
||||
- Affected assets/users:
|
||||
- Potential impact types:
|
||||
- Exposure window:
|
||||
- Potential blast radius:
|
||||
|
||||
## Containment, Eradication, and Recovery
|
||||
|
||||
### Immediate (0-24h)
|
||||
-
|
||||
|
||||
### Short-term (1-7d)
|
||||
-
|
||||
|
||||
### Long-term Hardening
|
||||
-
|
||||
|
||||
## Validation Steps
|
||||
-
|
||||
|
||||
## Open Questions and Data Requests
|
||||
-
|
||||
|
||||
## Lessons Learned (Evidence-based only)
|
||||
-
|
||||
|
||||
## Appendix
|
||||
|
||||
### Indicators (IOCs)
|
||||
-
|
||||
|
||||
### Evidence Index
|
||||
|
||||
- E1:
|
||||
@@ -10,7 +10,6 @@ from langgraph.prebuilt import ToolNode
|
||||
from langgraph.types import Send
|
||||
from pydantic import BaseModel, Field, ConfigDict
|
||||
|
||||
from AGENTS.agent_cmdb import AgentCMDB
|
||||
from AGENTS.agent_siem import AgentSIEM
|
||||
from AGENTS.agent_threat_intelligence import AgentThreatIntelligence
|
||||
from Lib.api import get_current_time_str
|
||||
@@ -22,6 +21,10 @@ from PLUGINS.SIRP.sirpmodel import PlaybookJobStatus, PlaybookModel, CaseModel
|
||||
MAX_ITERATIONS = 3
|
||||
MAX_ITERATIONS_OF_FUNCTIONS_CALL = 2
|
||||
PROMPT_LANG = None
|
||||
tools = [
|
||||
AgentSIEM.siem_search_by_natural_language,
|
||||
AgentThreatIntelligence.threat_intelligence_lookup
|
||||
]
|
||||
|
||||
|
||||
class PlanningRecord(BaseModel):
|
||||
@@ -30,7 +33,6 @@ class PlanningRecord(BaseModel):
|
||||
rationale: str = Field(description="The reasoning for the plan")
|
||||
plan: List[str] = Field(description="The specific list of tasks generated")
|
||||
|
||||
# Helper methods can be added here to facilitate string conversion
|
||||
def to_markdown(self) -> str:
|
||||
tasks_str = ", ".join(self.plan)
|
||||
return (f"#### Round {self.iteration}\n"
|
||||
@@ -43,9 +45,7 @@ class AnalystOutput(BaseModel):
|
||||
reasoning: str | List[str] = Field(description="The detailed reasoning process and key evidence supporting the final conclusion.")
|
||||
|
||||
|
||||
# Define the structured output of the Planner
|
||||
class HuntingPlan(BaseModel):
|
||||
# Allows generating multiple tasks at once, or an empty list to indicate the end
|
||||
current_plan: List[str] = Field(
|
||||
description="A list of specific questions to be investigated in parallel next. Returns an empty list if there are no more questions.")
|
||||
rationale: str = Field(description="The reason for making this plan")
|
||||
@@ -83,23 +83,19 @@ class AnalystState(BaseModel):
|
||||
description="Used to store the message passing history between all nodes."
|
||||
)
|
||||
|
||||
# Input: The specific question to be investigated
|
||||
question: str = Field(
|
||||
description="The specific question to be investigated, usually the starting point of user input."
|
||||
)
|
||||
|
||||
# Input: Context
|
||||
case: CaseModel = Field(
|
||||
description="External context or additional data provided for this investigation task."
|
||||
)
|
||||
|
||||
# Output: Final conclusion
|
||||
answer: str = Field(
|
||||
default="",
|
||||
description="The final conclusion or summary of the investigation task."
|
||||
)
|
||||
|
||||
# Output: Reasoning process
|
||||
reasoning: str | List[str] = Field(
|
||||
default="",
|
||||
description="Detailed reasoning steps and evidence for the conclusion."
|
||||
@@ -126,26 +122,22 @@ class MainState(BaseModel):
|
||||
model_config = ConfigDict(
|
||||
arbitrary_types_allowed=True
|
||||
)
|
||||
# Original case/global context
|
||||
|
||||
case: CaseModel = Field(
|
||||
default_factory=dict,
|
||||
description="Original case or global context data."
|
||||
)
|
||||
|
||||
# User intent
|
||||
user_intent: str = Field(
|
||||
default="",
|
||||
description="The initial request or core intent proposed by the user."
|
||||
)
|
||||
|
||||
# Established overall goal
|
||||
hunting_objective: str = Field(
|
||||
default="",
|
||||
description="The overall goal that the entire graph needs to achieve, established based on the user's intent."
|
||||
)
|
||||
|
||||
# Core memory: summary of subgraph return results
|
||||
# Use operator.add to ensure that the results returned by parallel subgraphs can be correctly appended and merged
|
||||
findings: Annotated[
|
||||
List[Finding],
|
||||
operator.add
|
||||
@@ -154,13 +146,11 @@ class MainState(BaseModel):
|
||||
description="A list of all results or findings collected from subgraphs or subtasks."
|
||||
)
|
||||
|
||||
# List of planned tasks for the current batch
|
||||
current_plan: List[str] = Field(
|
||||
default_factory=list,
|
||||
description="A list of planned tasks to be executed in the current iteration or batch."
|
||||
)
|
||||
|
||||
# Iteration count
|
||||
iteration_count: int = Field(
|
||||
default=0,
|
||||
description="A count of the number of main graph loops or iterations."
|
||||
@@ -198,7 +188,6 @@ class Playbook(LanggraphPlaybook):
|
||||
|
||||
messages = state.messages
|
||||
|
||||
# 1. 构造初始消息(如果是第一轮)
|
||||
if not messages:
|
||||
system_prompt_template = self.load_system_prompt_template("Analyst_System", lang=PROMPT_LANG)
|
||||
system_message = system_prompt_template.format()
|
||||
@@ -210,41 +199,30 @@ class Playbook(LanggraphPlaybook):
|
||||
|
||||
llm_api = LLMAPI()
|
||||
|
||||
# 2. 判断是否触发“强制总结”模式
|
||||
# 如果达到最大次数,就不再绑定工具,并修改 Prompt 要求其收尾
|
||||
if state.loop_count >= MAX_ITERATIONS_OF_FUNCTIONS_CALL - 1:
|
||||
self.logger.warning("Approaching max iterations, forcing analyst to summarize.")
|
||||
|
||||
# 注入一条强制性指令,告诉 LLM 不要再查了
|
||||
stop_instruction = (
|
||||
"\n\n[SYSTEM NOTICE]: You have reached the search limit. "
|
||||
"Do not call any more tools. Please provide your final conclusion "
|
||||
"based ONLY on the information gathered above."
|
||||
)
|
||||
# 将指令追加到最后一条消息中,或者作为新的 HumanMessage
|
||||
|
||||
messages.append(HumanMessage(content=stop_instruction))
|
||||
|
||||
# 使用不带工具绑定的模型,确保它只能输出文本
|
||||
base_llm = llm_api.get_model(tag=["powerful"])
|
||||
response: AIMessage = base_llm.invoke(messages)
|
||||
else:
|
||||
# 正常调查模式:绑定工具
|
||||
|
||||
base_llm = llm_api.get_model(tag=["powerful", "function_calling"])
|
||||
llm_with_tools = base_llm.bind_tools([
|
||||
AgentSIEM.siem_search_by_natural_language,
|
||||
AgentCMDB.cmdb_query_asset,
|
||||
AgentThreatIntelligence.threat_intelligence_lookup
|
||||
])
|
||||
llm_with_tools = base_llm.bind_tools(tools)
|
||||
response: AIMessage = llm_with_tools.invoke(messages)
|
||||
|
||||
# 3. 兜底逻辑:如果模型在最后关头依然执意返回 tool_calls(即便没绑定,有时也会幻觉)
|
||||
# 我们可以手动清除 tool_calls 以确保 should_continue 路由到 finalizer
|
||||
if state.loop_count >= MAX_ITERATIONS_OF_FUNCTIONS_CALL - 1:
|
||||
if response.tool_calls:
|
||||
self.logger.info("Stripping hallucinated tool calls in final round.")
|
||||
response.tool_calls = []
|
||||
|
||||
# update record
|
||||
for message in messages:
|
||||
self.add_message_to_playbook(message, node="analyst_node")
|
||||
|
||||
@@ -253,9 +231,8 @@ class Playbook(LanggraphPlaybook):
|
||||
return {"loop_count": state.loop_count + 1, "messages": [response]}
|
||||
|
||||
# Tool node
|
||||
tool_node = ToolNode([AgentSIEM.siem_search_by_natural_language, AgentCMDB.cmdb_query_asset, AgentThreatIntelligence.threat_intelligence_lookup])
|
||||
tool_node = ToolNode(tools)
|
||||
|
||||
# Result generation node: when there is no tool call, it is responsible for converting the last message into a structured output
|
||||
def final_answer_node(state: AnalystState):
|
||||
self.logger.debug("Final Answer Node Invoked")
|
||||
|
||||
@@ -323,7 +300,7 @@ class Playbook(LanggraphPlaybook):
|
||||
|
||||
builder.add_node('analyst_node', analyst_node)
|
||||
builder.add_node('tool', tool_node)
|
||||
builder.add_node('finalizer', final_answer_node) # Add new node
|
||||
builder.add_node('finalizer', final_answer_node)
|
||||
|
||||
builder.add_edge(START, 'analyst_node')
|
||||
|
||||
@@ -332,8 +309,8 @@ class Playbook(LanggraphPlaybook):
|
||||
should_continue,
|
||||
)
|
||||
|
||||
builder.add_edge('tool', 'analyst_node') # After the tool is executed -> return to the model to see the result
|
||||
builder.add_edge('finalizer', END) # After formatting -> end
|
||||
builder.add_edge('tool', 'analyst_node')
|
||||
builder.add_edge('finalizer', END)
|
||||
|
||||
self.analyst_graph = builder.compile(name='analyst_graph')
|
||||
|
||||
@@ -342,39 +319,32 @@ class Playbook(LanggraphPlaybook):
|
||||
"""Intent recognition: determine the overall goal"""
|
||||
self.logger.debug("Intent Node Invoked")
|
||||
|
||||
# Get Case data
|
||||
case: CaseModel = Case.get(rowid=self.param_source_rowid)
|
||||
|
||||
# User intent
|
||||
user_intent = self.param_user_input
|
||||
|
||||
if not user_intent:
|
||||
user_intent = "None (Auto-Pilot Mode)"
|
||||
|
||||
# Load system prompt
|
||||
system_prompt_template = self.load_system_prompt_template("Intent_System", lang=PROMPT_LANG)
|
||||
system_message = system_prompt_template.format()
|
||||
|
||||
human_message = self.load_human_prompt_template("Intent_Human", lang=PROMPT_LANG).format(case=case.model_dump_json_for_ai(),
|
||||
user_intent=user_intent)
|
||||
|
||||
# Construct few-shot examples
|
||||
few_shot_examples = [
|
||||
]
|
||||
|
||||
# Construct message list
|
||||
messages = [
|
||||
system_message,
|
||||
*few_shot_examples,
|
||||
human_message
|
||||
]
|
||||
|
||||
# Run
|
||||
llm_api = LLMAPI()
|
||||
llm = llm_api.get_model(tag="fast")
|
||||
response: AIMessage = llm.invoke(messages)
|
||||
|
||||
# update record
|
||||
for message in messages:
|
||||
self.add_message_to_playbook(message, node="intent_node")
|
||||
|
||||
@@ -391,7 +361,6 @@ class Playbook(LanggraphPlaybook):
|
||||
|
||||
def planner_node(state: MainState):
|
||||
"""
|
||||
[Core logic]
|
||||
Check existing findings to decide what else to look for.
|
||||
Generate a batch of tasks at once.
|
||||
"""
|
||||
@@ -403,13 +372,11 @@ class Playbook(LanggraphPlaybook):
|
||||
|
||||
iteration_count = iteration_count + 1
|
||||
|
||||
# Forced exit mechanism
|
||||
if iteration_count > MAX_ITERATIONS:
|
||||
self.logger.debug("Max iterations reached, terminating planning.")
|
||||
node_out = {"current_plan": []}
|
||||
return node_out
|
||||
|
||||
# Load system prompt
|
||||
system_prompt_template = self.load_system_prompt_template("Planner_System", lang=PROMPT_LANG)
|
||||
|
||||
system_message = system_prompt_template.format()
|
||||
@@ -417,7 +384,6 @@ class Playbook(LanggraphPlaybook):
|
||||
history_md_list = []
|
||||
for record in findings:
|
||||
record: Finding
|
||||
# Call the object's own method, or customize the format here
|
||||
history_md_list.append(record.to_markdown())
|
||||
|
||||
findings_str = "\n".join(history_md_list)
|
||||
@@ -425,7 +391,7 @@ class Playbook(LanggraphPlaybook):
|
||||
human_message = self.load_human_prompt_template("Planner_Human", lang=PROMPT_LANG).format(case=state.case, hunting_objective=hunting_objective,
|
||||
findings=findings_str, iteration_count=iteration_count,
|
||||
additional_info=additional_info)
|
||||
# Construct few-shot examples
|
||||
|
||||
few_shot_examples = [
|
||||
]
|
||||
|
||||
@@ -434,7 +400,6 @@ class Playbook(LanggraphPlaybook):
|
||||
|
||||
llm = llm_api.get_model(tag=["powerful", "structured_output"])
|
||||
|
||||
# Construct message list
|
||||
messages = [
|
||||
system_message,
|
||||
*few_shot_examples,
|
||||
@@ -453,7 +418,6 @@ class Playbook(LanggraphPlaybook):
|
||||
|
||||
self.logger.debug(f"Generated Plan for Round {iteration_count}")
|
||||
|
||||
# update record
|
||||
for message in messages:
|
||||
self.add_message_to_playbook(message, node="planner_node")
|
||||
self.add_message_to_playbook(response, node="planner_node")
|
||||
@@ -480,8 +444,6 @@ class Playbook(LanggraphPlaybook):
|
||||
self.logger.debug(f"Round {iteration_count},No more tasks in plan, proceeding to report.")
|
||||
return "report"
|
||||
|
||||
# There are tasks, parallel distribution (Map)
|
||||
# Send(target node name, State passed to this node)
|
||||
self.logger.debug(f"Round {iteration_count},Dispatching {len(current_plan)} tasks to analyst subgraph.")
|
||||
return [
|
||||
Send("analyst_subgraph", AnalystState(question=question, case=case))
|
||||
@@ -518,7 +480,6 @@ class Playbook(LanggraphPlaybook):
|
||||
planning_history_str = "\n".join(history_md_list)
|
||||
|
||||
# findings
|
||||
|
||||
history_md_list = []
|
||||
for record in findings:
|
||||
record: Finding
|
||||
@@ -535,18 +496,16 @@ class Playbook(LanggraphPlaybook):
|
||||
findings=findings_str,
|
||||
planning_history=planning_history_str,
|
||||
additional_info=additional_info)
|
||||
# Construct few-shot examples
|
||||
|
||||
few_shot_examples = [
|
||||
]
|
||||
|
||||
# Construct message list
|
||||
messages = [
|
||||
system_message,
|
||||
*few_shot_examples,
|
||||
human_message
|
||||
]
|
||||
|
||||
# Run
|
||||
llm_api = LLMAPI()
|
||||
llm = llm_api.get_model(tag=["powerful"])
|
||||
response = llm.invoke(messages)
|
||||
@@ -554,14 +513,12 @@ class Playbook(LanggraphPlaybook):
|
||||
case_new = CaseModel(rowid=self.param_source_rowid, threat_hunting_report_ai=response.content)
|
||||
Case.update(case_new)
|
||||
|
||||
# update record
|
||||
for message in messages:
|
||||
self.add_message_to_playbook(message, node="planner_node")
|
||||
self.add_message_to_playbook(response, node="planner_node")
|
||||
|
||||
node_out = {"report": response.content}
|
||||
|
||||
# update playbook status
|
||||
self.update_playbook_status(PlaybookJobStatus.SUCCESS, "Threat Hunting Agent Finish.")
|
||||
return node_out
|
||||
|
||||
@@ -570,24 +527,19 @@ class Playbook(LanggraphPlaybook):
|
||||
|
||||
main_builder.add_node("intent", intent_node)
|
||||
main_builder.add_node("planner", planner_node)
|
||||
# Note: The subgraph wrapper function is registered here
|
||||
|
||||
main_builder.add_node("analyst_subgraph", run_analyst_subgraph)
|
||||
main_builder.add_node("report", reporter_node)
|
||||
|
||||
# Process: Start -> Intent -> Planner
|
||||
main_builder.add_edge(START, "intent")
|
||||
main_builder.add_edge("intent", "planner")
|
||||
|
||||
# Process: Planner -> (Map: execute multiple subgraphs in parallel) OR (Report)
|
||||
main_builder.add_conditional_edges(
|
||||
"planner",
|
||||
continue_to_analysts,
|
||||
["analyst_subgraph", "report"]
|
||||
)
|
||||
|
||||
# Process: After all subgraphs are executed -> (Reduce/Gather) -> automatically return to Planner
|
||||
# The mechanism of LangGraph is: after all parallel branches are executed, the next common node will be entered.
|
||||
# Since we want to go back to the Planner to reflect, we connect the edge back to the Planner.
|
||||
main_builder.add_edge("analyst_subgraph", "planner")
|
||||
|
||||
main_builder.add_edge("report", END)
|
||||
|
||||
+12
-12
@@ -9,18 +9,18 @@ if __name__ == "__main__":
|
||||
django.setup()
|
||||
|
||||
for case in [
|
||||
case1_phishing,
|
||||
case2_lateral_movement,
|
||||
case3_dns_tunnel,
|
||||
case4_brute_force,
|
||||
case5_ransomware,
|
||||
case6_unauthorized_access,
|
||||
case7_data_exfil,
|
||||
case8_email_campaign,
|
||||
case9_priv_esc,
|
||||
case10_cloud_misconfig,
|
||||
# case1_phishing,
|
||||
# case2_lateral_movement,
|
||||
# case3_dns_tunnel,
|
||||
# case4_brute_force,
|
||||
# case5_ransomware,
|
||||
# case6_unauthorized_access,
|
||||
# case7_data_exfil,
|
||||
# case8_email_campaign,
|
||||
# case9_priv_esc,
|
||||
# case10_cloud_misconfig,
|
||||
case11_brute_force,
|
||||
case12_sql_injection,
|
||||
case13_ransomware
|
||||
# case12_sql_injection,
|
||||
# case13_ransomware
|
||||
]:
|
||||
Case.update_or_create(case)
|
||||
|
||||
@@ -314,7 +314,7 @@ case11_brute_force = CaseModel(
|
||||
confidence_ai=Confidence.HIGH,
|
||||
threat_hunting_report_ai="Query: Show all failed login attempts from external IPs in last 7 days | Show all successful logins from 45.95.11.22 in last 7 days | Check for similar brute force patterns from other IP ranges",
|
||||
tickets=[ticket_pagerduty],
|
||||
enrichments=[enrichment_greynoise_scanner, enrichment_geoip_russia],
|
||||
enrichments=[],
|
||||
alerts=[alert_brute_force_siem]
|
||||
)
|
||||
|
||||
|
||||
@@ -35,11 +35,11 @@ enrichment_otx_8888 = EnrichmentModel(
|
||||
data=json.dumps({"pulse_count": 42, "tags": ["malware", "c2", "gootkit"]})
|
||||
)
|
||||
enrichment_greynoise_scanner = EnrichmentModel(
|
||||
name="GreyNoise Report for 185.220.101.45",
|
||||
name="GreyNoise Report for 45.95.11.22",
|
||||
type="Threat Intelligence",
|
||||
provider="GreyNoise",
|
||||
value="185.220.101.45",
|
||||
src_url="https://www.greynoise.io/viz/ip/185.220.101.45",
|
||||
value="45.95.11.22",
|
||||
src_url="https://www.greynoise.io/viz/ip/45.95.11.22",
|
||||
desc="Known mass-scanner. Classification: malicious. Last seen scanning 2 hours ago.",
|
||||
data=json.dumps({"classification": "malicious", "tags": ["SSH Bruteforce", "Mass Scanner"], "last_seen": "2h"})
|
||||
)
|
||||
@@ -80,10 +80,10 @@ enrichment_whois_domain = EnrichmentModel(
|
||||
data=json.dumps({"created": "2024-01-18", "registrar": "NameCheap", "privacy": True})
|
||||
)
|
||||
enrichment_geoip_russia = EnrichmentModel(
|
||||
name="GeoIP Location for 185.220.101.45",
|
||||
name="GeoIP Location for 45.95.11.22",
|
||||
type="Geolocation",
|
||||
provider="MaxMind GeoIP",
|
||||
value="185.220.101.45",
|
||||
value="45.95.11.22",
|
||||
desc="Location: Moscow, Russia. ASN: AS12345 (SuspiciousHosting LLC)",
|
||||
data=json.dumps({"country": "RU", "city": "Moscow", "asn": "AS12345", "org": "SuspiciousHosting LLC"})
|
||||
)
|
||||
|
||||
@@ -337,6 +337,12 @@ class BaseSystemModel(BaseModel):
|
||||
# wfftime: Optional[Any] = Field(default=None, description="工作流关注时间")
|
||||
# wfstatus: Optional[WfStatus] = Field(default=None, description="工作流状态")
|
||||
|
||||
@field_validator("ownerid", mode="before")
|
||||
def empty_list_to_none(cls, v):
|
||||
if isinstance(v, list) and len(v) == 0:
|
||||
return None
|
||||
return v
|
||||
|
||||
@field_validator(
|
||||
"ctime", "utime", "wfctime", "wfrtime", "wfcotime", "wfdtime",
|
||||
"created_time", "modified_time", "first_seen_time", "last_seen_time", "acknowledged_time", "closed_time",
|
||||
|
||||
Reference in New Issue
Block a user