Buckets:

download
raw
16.5 kB
"""
Background task functions extracted from api/main.py.
Contains scan execution, tool execution, report generation,
and scheduled scan logic.
"""
import json
import logging
from datetime import datetime, timezone
from typing import Any, List, Optional
from api.security_logging import sanitize_for_log
from database.crud import create_finding, create_scan, update_scan_status
from database.models import Finding, Report, SessionLocal
logger = logging.getLogger(__name__)
# In-memory storage for scheduled scans (in production: use database)
SCHEDULED_SCANS: list = []
SCHEDULE_ID_COUNTER: int = 1
# ============================================================================
# SCAN EXECUTION
# ============================================================================
async def run_scan_task(scan_id: int, scan_config: dict):
"""Background task for running a scan"""
from agents.react_agent import ReActAgent, ReActAgentConfig
db = SessionLocal()
try:
update_scan_status(db, scan_id, "running")
# Lazy import ws_manager to avoid circular import
from api.main import ws_manager
# Notify via WebSocket
await ws_manager.broadcast_to_scan(
scan_id,
{"type": "status", "status": "running", "message": "Scan started"},
)
# Run agent
config = ReActAgentConfig(max_iterations=10)
agent = ReActAgent(config)
result = agent.run(
target=scan_config["target"],
objective=scan_config.get("objective", "comprehensive scan"),
)
# Save findings
for finding_data in result.get("findings", []):
create_finding(
db,
scan_id=scan_id,
title=f"Finding from {finding_data.get('tool', 'unknown')}",
description=str(finding_data.get("result", ""))[:500],
severity="medium",
tool=finding_data.get("tool"),
)
update_scan_status(
db,
scan_id,
"completed",
{
"result": result.get("final_message", ""),
"iterations": result.get("iterations", 0),
},
)
# Notify completion
await ws_manager.broadcast_to_scan(
scan_id,
{
"type": "status",
"status": "completed",
"message": "Scan completed",
"findings_count": len(result.get("findings", [])),
},
)
except Exception as e:
logger.error(f"Scan task error: {sanitize_for_log(str(e))}")
update_scan_status(db, scan_id, "failed", {"error": str(e)})
from api.main import ws_manager
await ws_manager.broadcast_to_scan(scan_id, {"type": "error", "message": str(e)})
finally:
db.close()
async def execute_tool_task(scan_id: int, tool_name: str, target: str, parameters: dict):
"""Execute a single tool"""
from tools import TOOL_REGISTRY
db = SessionLocal()
try:
tool_func = TOOL_REGISTRY.get(tool_name)
if not tool_func:
raise ValueError(f"Tool {tool_name} not found")
# Execute tool
result = tool_func(target, **parameters)
# Save finding
create_finding(
db,
scan_id=scan_id,
title=f"{tool_name} result",
description=str(result)[:1000],
severity="info",
tool=tool_name,
)
update_scan_status(db, scan_id, "completed")
except Exception as e:
logger.error(f"Tool execution error: {sanitize_for_log(str(e))}")
update_scan_status(db, scan_id, "failed", {"error": str(e)})
finally:
db.close()
async def generate_report_task(report_id: int, scan_id: int, format: str):
"""Generate report in background"""
from reports.generator import ReportGenerator
db = SessionLocal()
try:
generator = ReportGenerator()
if format == "pdf":
file_path = generator.generate_pdf(scan_id)
elif format == "html":
file_path = generator.generate_html(scan_id)
else:
file_path = generator.generate_json(scan_id)
# Update report
report = db.query(Report).filter(Report.id == report_id).first()
if report:
report.file_path = file_path
report.status = "completed"
db.commit()
except Exception as e:
logger.error(f"Report generation error: {sanitize_for_log(str(e))}")
report = db.query(Report).filter(Report.id == report_id).first()
if report:
report.status = "failed"
db.commit()
finally:
db.close()
async def execute_tool_internal(tool_name: str, target: str, options: dict = None) -> dict:
"""
Execute a single tool and return results.
Uses the tool registry to find and execute tools.
"""
from tools.tool_registry import registry
start_time = datetime.now(timezone.utc)
logger.info(f"Executing tool '{tool_name}' on target '{target}'")
try:
# Get tool from registry
registered_tool = registry.get(tool_name)
if not registered_tool:
logger.warning(f"Tool '{tool_name}' not found in registry")
return {
"tool": tool_name,
"target": target,
"success": False,
"error": f"Tool '{tool_name}' not found in registry",
"findings": [],
"execution_time": 0.0,
}
# Check if tool is enabled
if not registered_tool.enabled:
logger.warning(f"Tool '{tool_name}' is disabled")
return {
"tool": tool_name,
"target": target,
"success": False,
"error": f"Tool '{tool_name}' is disabled",
"findings": [],
"execution_time": 0.0,
}
# Execute tool with target
tool = registered_tool.tool
tool_input = target
if options:
tool_input = f"{target} {json.dumps(options)}"
# Execute the tool (sync tools run in executor)
import asyncio
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(None, tool.run, tool_input)
# Calculate execution time
execution_time = (datetime.now(timezone.utc) - start_time).total_seconds()
# Record invocation in registry
registry.record_invocation(tool_name, execution_time)
# Parse result to extract findings
findings = parse_tool_findings(tool_name, target, result)
logger.info(f"Tool '{tool_name}' completed in {execution_time:.2f}s with {len(findings)} findings")
return {
"tool": tool_name,
"target": target,
"success": True,
"result": result,
"findings": findings,
"execution_time": execution_time,
}
except Exception as e:
execution_time = (datetime.now(timezone.utc) - start_time).total_seconds()
logger.error(f"Tool '{tool_name}' execution failed: {e}")
return {
"tool": tool_name,
"target": target,
"success": False,
"error": str(e),
"findings": [],
"execution_time": execution_time,
}
def parse_tool_findings(tool_name: str, target: str, result: str) -> list:
"""
Parse tool output to extract structured findings.
This is a simplified implementation - in production, each tool
would have its own parser.
"""
findings = []
result_lower = result.lower() if result else ""
# Common vulnerability keywords to look for
severity_keywords = {
"critical": [
"critical",
"rce",
"remote code execution",
"sql injection",
"unauthenticated",
],
"high": [
"high",
"vulnerable",
"cve-",
"exploit",
"default credentials",
"weak password",
],
"medium": [
"medium",
"warning",
"deprecated",
"outdated",
"information disclosure",
],
"low": ["low", "info", "informational", "note", "missing header"],
}
# Determine severity based on keywords in result
severity = "info"
for sev, keywords in severity_keywords.items():
if any(kw in result_lower for kw in keywords):
severity = sev
break
# Create finding if there's meaningful content
if result and len(result) > 10:
finding = {
"title": f"{tool_name.upper()} finding on {target}",
"description": (result[:1000] if len(result) > 1000 else result), # Limit length
"severity": severity,
"tool": tool_name,
"target": target,
"evidence": result[:2000] if len(result) > 2000 else result,
}
findings.append(finding)
return findings
def save_finding_to_db(db, scan_id: int, finding: dict):
"""Save a finding to the database"""
try:
db_finding = Finding(
scan_id=scan_id,
title=finding.get("title", "Unknown finding"),
description=finding.get("description", ""),
severity=finding.get("severity", "medium"),
evidence=finding.get("evidence", ""),
tool=finding.get("tool", ""),
target=finding.get("target", ""),
cvss_score=finding.get("cvss_score"),
cve_id=finding.get("cve_id"),
)
db.add(db_finding)
db.commit()
logger.debug(f"Finding saved for scan {scan_id}: {finding.get('title', 'Unknown')}")
return db_finding
except Exception as e:
db.rollback()
logger.error(f"Failed to save finding: {e}")
raise
async def execute_scan_internal(scan_id: int, config: dict, db) -> dict:
"""
Actually execute a scan with the given configuration.
Args:
scan_id: The database ID of the scan
config: Scan configuration containing target, tools, etc.
db: Database session
Returns:
dict: Execution results with status, results, and findings count
"""
try:
logger.info(f"Starting scan execution for scan_id={scan_id}")
# 1. Update scan status to running
update_scan_status(db, scan_id, "running")
# 2. Get target and tools from config
target = config.get("target")
tools = config.get("tools", ["nmap"])
tool_options = config.get("tool_options", {})
if not target:
raise ValueError("No target specified in scan config")
logger.info(f"Scan {scan_id}: target={target}, tools={tools}")
# 3. Execute tools sequentially
results = []
total_findings = 0
for tool_name in tools:
try:
# Get tool-specific options
options = tool_options.get(tool_name, {})
# Execute the tool
tool_result = await execute_tool_internal(tool_name, target, options)
results.append(tool_result)
# 4. Save findings to database
if tool_result.get("findings"):
for finding in tool_result["findings"]:
save_finding_to_db(db, scan_id, finding)
total_findings += 1
# Small delay between tools to avoid overwhelming target
if len(tools) > 1:
import asyncio
await asyncio.sleep(1)
except Exception as tool_error:
logger.error(f"Tool '{tool_name}' failed: {tool_error}")
results.append(
{
"tool": tool_name,
"target": target,
"success": False,
"error": str(tool_error),
"findings": [],
}
)
# Continue with next tool even if one fails
# 5. Update scan status to completed
summary = {
"total_tools": len(tools),
"successful_tools": sum(1 for r in results if r.get("success")),
"failed_tools": sum(1 for r in results if not r.get("success")),
"total_findings": total_findings,
}
update_scan_status(db, scan_id, "completed", result=summary)
logger.info(f"Scan {scan_id} completed: {summary}")
return {
"status": "success",
"scan_id": scan_id,
"results": results,
"summary": summary,
}
except Exception as e:
logger.error(f"Scan {scan_id} failed: {e}")
update_scan_status(db, scan_id, "failed", result={"error": str(e)})
raise
# ============================================================================
# SCHEDULED SCAN LOGIC
# ============================================================================
def calculate_next_run(frequency: str, time_str: str, day: Optional[int] = None) -> datetime:
"""Calculate the next run time for a schedule"""
from datetime import timedelta, timezone
now = datetime.now(timezone.utc)
hour, minute = map(int, time_str.split(":"))
next_run = now.replace(hour=hour, minute=minute, second=0, microsecond=0)
if frequency == "once":
if next_run <= now:
next_run += timedelta(days=1)
elif frequency == "daily":
if next_run <= now:
next_run += timedelta(days=1)
elif frequency == "weekly":
days_ahead = day - now.weekday() if day is not None else 0
if days_ahead <= 0:
days_ahead += 7
next_run += timedelta(days=days_ahead)
elif frequency == "monthly":
# Simplified: run on the first of next month
if now.day > 1 or (now.day == 1 and next_run <= now):
next_run = next_run.replace(month=now.month + 1 if now.month < 12 else 1)
if next_run.month == 1:
next_run = next_run.replace(year=next_run.year + 1)
return next_run
async def execute_scheduled_scan(schedule: dict, user_id: str):
"""Execute a scheduled scan"""
db = SessionLocal()
try:
# Update last run
schedule["last_run_at"] = datetime.now(timezone.utc)
schedule["last_run_status"] = "running"
# Create scan
scan_data = type(
"obj",
(object,),
{
"name": schedule["name"],
"target": schedule["target"],
"scan_type": schedule["scan_type"],
"config": {},
"user_id": user_id,
},
)
db_scan = create_scan(db, scan_data)
# Prepare scan configuration
scan_config = {
"target": schedule["target"],
"tools": schedule.get("tools", ["nmap"]),
"scan_type": schedule.get("scan_type", "basic"),
"tool_options": schedule.get("config", {}),
}
# Execute the actual scan
await execute_scan_internal(db_scan.id, scan_config, db)
schedule["last_run_status"] = "completed"
# Calculate next run
schedule["next_run_at"] = calculate_next_run(
schedule["frequency"],
schedule["schedule_time"],
schedule.get("schedule_day"),
)
# Send notifications
if schedule.get("notification_email"):
await send_email_notification(schedule, db_scan.id)
if schedule.get("notification_slack"):
await send_slack_notification(schedule, db_scan.id)
except Exception as e:
schedule["last_run_status"] = "failed"
logger.error(f"Scheduled scan error: {e}")
finally:
db.close()
async def send_email_notification(schedule: dict, scan_id: int):
"""Send email notification"""
logger.info(f"Would send email to {schedule['notification_email']} for scan {scan_id}")
async def send_slack_notification(schedule: dict, scan_id: int):
"""Send Slack notification"""
logger.info(f"Would send Slack notification for scan {scan_id}")

Xet Storage Details

Size:
16.5 kB
·
Xet hash:
9536dab03704898b32ac63bc93f22a0c899593ae8cbd20f45ee807b79ddbfce3

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.