ARES - authorized red-team engagement automation with dashboard, campaign scope, module orchestration, OPSEC controls, encrypted vault, and reporting.
Python active-directorycloud-securitydefensive-validationfastapigood-first-issuemitre-attackopsecreactred-teamreportingsecurity
fix(security): enforce egress scope guard, UTF-8 Windows invariance, and MCP error sanitization
Mafifrizi918ecf49cc36
diff --git a/ares/cli/_store.py b/ares/cli/_store.pyindex fac64d7..026666a 100644--- a/ares/cli/_store.py+++ b/ares/cli/_store.py@@ -22,7 +22,7 @@ def save_campaign(campaign: Any) -> None:def load_campaign(partial_id: str) -> dict[str, Any] | None:for p in campaigns_dir().glob("*.json"):- data: dict[str, Any] = json.loads(p.read_text())+ data: dict[str, Any] = json.loads(p.read_text(encoding="utf-8"))if data["id"].startswith(partial_id):return datareturn None@@ -30,7 +30,7 @@ def load_campaign(partial_id: str) -> dict[str, Any] | None:def load_all_campaigns() -> list[dict[str, Any]]:return [- json.loads(p.read_text())+ json.loads(p.read_text(encoding="utf-8"))for p in sorted(campaigns_dir().glob("*.json"),key=lambda x: x.stat().st_mtime,@@ -84,7 +84,7 @@ class CampaignStore:ip_str = str(target)for p in campaigns_dir().glob("*.json"):- data: dict = json.loads(p.read_text())+ data: dict = json.loads(p.read_text(encoding="utf-8"))if data["id"].startswith(campaign_partial_id):targets = data.get("targets", [])# Avoid duplicate IPs@@ -138,7 +138,7 @@ class CampaignStore:# Mark campaign as pausedfor p in campaigns_dir().glob("*.json"):- data = json.loads(p.read_text())+ data = json.loads(p.read_text(encoding="utf-8"))if data["id"] == c["id"]:data["status"] = "paused"data["last_checkpoint"] = cp_id@@ -168,7 +168,7 @@ class CampaignStore:matches = [p for p in checkpoints if cp_id in p.name]target = matches[0] if matches else checkpoints[0]- return json.loads(target.read_text())+ return json.loads(target.read_text(encoding="utf-8"))def list_reports(self, campaign_partial_id: str = "") -> list[dict]:"""List generated report files from ~/.ares/reports/."""diff --git a/ares/cli/typer_main.py b/ares/cli/typer_main.pyindex 91f2c9a..02010a6 100644--- a/ares/cli/typer_main.py+++ b/ares/cli/typer_main.py@@ -465,7 +465,7 @@ def campaign_resume(from pathlib import Pathcampaigns_path = Path.home() / ".ares" / "campaigns"for p in campaigns_path.glob("*.json"):- data = json.loads(p.read_text())+ data = json.loads(p.read_text(encoding="utf-8"))if data["id"].startswith(campaign_id):data["status"] = "active"p.write_text(json.dumps(data, indent=2), encoding="utf-8")@@ -536,7 +536,7 @@ def target_import(console.print(f"[red]File not found: {file}[/]")raise typer.Exit(1)- lines = file.read_text().strip().splitlines()+ lines = file.read_text(encoding="utf-8").strip().splitlines()targets = [l.strip() for l in lines if l.strip() and not l.startswith("#")]console.print(f"[green][+] Imported {len(targets)} targets[/]")@@ -1014,7 +1014,7 @@ def signing_add_key("""Add a trusted public key to the registry."""from ares.core.signing import KeyRegistryregistry = KeyRegistry()- pem = pub_key.read_text()+ pem = pub_key.read_text(encoding="utf-8")registry.add_trusted_key(key_id, pem, author, added_by=operator)console.print(f"[green][+] Trusted key added:[/] {key_id} ({author})")@@ -1623,7 +1623,7 @@ def doctor(# .envenv_path = Path(".env")if env_path.exists():- content = env_path.read_text()+ content = env_path.read_text(encoding="utf-8")if "CHANGE_ME" in content:check(".env configured", "fail","CHANGE_ME placeholders found - run: ares-setup to generate secure keys")diff --git a/ares/core/signing.py b/ares/core/signing.pyindex b42fde9..0a57e83 100644--- a/ares/core/signing.py+++ b/ares/core/signing.py@@ -99,11 +99,11 @@ class ModuleSignature:def save(self, path: Path) -> None:"""Write signature to <module_file>.sig"""- path.write_text(json.dumps(self.to_dict(), indent=2))+ path.write_text(json.dumps(self.to_dict(), indent=2), encoding="utf-8")@classmethoddef load(cls, path: Path) -> "ModuleSignature":- data = json.loads(path.read_text())+ data = json.loads(path.read_text(encoding="utf-8"))return cls(**data)@@ -153,13 +153,13 @@ class KeyRegistry:def _load(self) -> None:if self._path.exists():- data = json.loads(self._path.read_text())+ data = json.loads(self._path.read_text(encoding="utf-8"))self._keys = data.get("trusted_keys", {})self._revoked = set(data.get("revoked_keys", []))rev_path = REVOKED_KEYS_PATHif rev_path.exists():- data = json.loads(rev_path.read_text())+ data = json.loads(rev_path.read_text(encoding="utf-8"))self._revoked.update(data.get("revoked_keys", []))def _save(self) -> None:@@ -168,7 +168,7 @@ class KeyRegistry:"trusted_keys": self._keys,"revoked_keys": list(self._revoked),"updated_at": time.time(),- }, indent=2))+ }, indent=2), encoding="utf-8")def add_trusted_key(self,diff --git a/ares/credential/cracker.py b/ares/credential/cracker.pyindex ec0a927..e1866ea 100644--- a/ares/credential/cracker.py+++ b/ares/credential/cracker.py@@ -193,7 +193,7 @@ class CrackingWorker:"""hash_file = self.tmpdir / f"{job.job_id}.hash"potfile = self.tmpdir / f"{job.job_id}.pot"- hash_file.write_text(job.hash_value)+ hash_file.write_text(job.hash_value, encoding="utf-8")cmd = [self._hashcat,@@ -233,7 +233,7 @@ class CrackingWorker:# Read cracked plaintext from potfileif potfile.exists():- content = potfile.read_text().strip()+ content = potfile.read_text(encoding="utf-8", errors="replace").strip()if content:# Potfile format: hash:plaintextparts = content.split(":", 1)diff --git a/ares/knowledge/base.py b/ares/knowledge/base.pyindex e2a1c07..0b7b53d 100644--- a/ares/knowledge/base.py+++ b/ares/knowledge/base.py@@ -385,7 +385,7 @@ class OutcomeTracker:def _load(self) -> dict[str, dict[str, int]]:try:if self._path.exists():- return _json.loads(self._path.read_text())+ return _json.loads(self._path.read_text(encoding="utf-8"))except Exception:passreturn {}diff --git a/ares/marketplace/installer.py b/ares/marketplace/installer.pyindex 4053854..0ea2ab1 100644--- a/ares/marketplace/installer.py+++ b/ares/marketplace/installer.py@@ -290,7 +290,7 @@ class LocalRegistry:reg_path = getattr(_self_module, "INSTALLED_REGISTRY", REGISTRY_FILE)try:if reg_path.exists():- return json.loads(reg_path.read_text())+ return json.loads(reg_path.read_text(encoding="utf-8"))except (OSError, ValueError, AttributeError):passreturn {"plugins": {}}@@ -455,7 +455,7 @@ class ModuleInstaller:next(path.glob("*.py"), None) or path, source_url=str(path))else:- manifest = ModuleManifest.from_dict(json.loads(manifest_file.read_text()))+ manifest = ModuleManifest.from_dict(json.loads(manifest_file.read_text(encoding="utf-8")))manifest.source_url = str(path)return self._finalize_install(path, manifest, force)@@ -470,7 +470,7 @@ class ModuleInstaller:sidecar = path.parent / "manifest.json"if sidecar.exists():import json as _json- return _json.loads(sidecar.read_text())+ return _json.loads(sidecar.read_text(encoding="utf-8"))# No sidecar - cannot extract sha256 from source alonereturn Noneexcept Exception:diff --git a/ares/mcp/security.py b/ares/mcp/security.pyindex a5f068a..2e63843 100644--- a/ares/mcp/security.py+++ b/ares/mcp/security.py@@ -140,12 +140,12 @@ class McpScopeGate:if not target or not scope_rules:return False- # Extract host if target includes port notation (e.g. "10.0.0.1:445" or "dc01.corp.local:88")+ # Extract host if target includes port notation (e.g. "10.0.0.1:445", "[2001:db8::1]:445", or "dc01.corp.local:88")target_host = target- if ":" in target and not target.startswith("["):- parts = target.split(":")- if len(parts) == 2:- target_host = parts[0].strip()+ if target.startswith("[") and "]" in target:+ target_host = target[1:target.index("]")]+ elif ":" in target and target.count(":") == 1:+ target_host = target.split(":")[0].strip()for rule in scope_rules:rule = rule.strip().lower()diff --git a/ares/mcp/server.py b/ares/mcp/server.pyindex c463982..674ccb8 100644--- a/ares/mcp/server.py+++ b/ares/mcp/server.py@@ -183,5 +183,8 @@ class AresMcpServer:logger.exception("Internal error handling method %s: %s", method, e)return JSONRPCResponse(id=req.id,- error=JSONRPCError(code=ErrorCode.INTERNAL_ERROR.value, message=f"Internal MCP server error: {e}"),+ error=JSONRPCError(+ code=ErrorCode.INTERNAL_ERROR.value,+ message="Internal MCP server error. Consult system audit logs.",+ ),).model_dump(exclude_none=True)diff --git a/ares/modules/exfil/staged_collection.py b/ares/modules/exfil/staged_collection.pyindex a13fc93..380d894 100644--- a/ares/modules/exfil/staged_collection.py+++ b/ares/modules/exfil/staged_collection.py@@ -63,8 +63,6 @@ def _audit_lots_egress_sync(target: str) -> dict[str, Any]:}ctx = ssl.create_default_context()- ctx.check_hostname = False- ctx.verify_mode = ssl.CERT_NONEfor service_name, url in cloud_endpoints.items():try:@@ -171,11 +169,14 @@ class StagedCollectionModule(BaseModule):destination = pdict.get("destination", "")max_files = int(pdict.get("max_files", 200))+ audit_lots_egress = bool(pdict.get("audit_lots_egress", False))+findings, raw = await self.run(target=target, username=username, password=password,key_path=key_path, platform=platform,search_paths=search_paths, max_files=max_files,destination=destination,+ audit_lots_egress=audit_lots_egress,)# Cryptographic Evidence Records with SHA-256 Merkle Provenance@@ -353,8 +354,17 @@ class StagedCollectionModule(BaseModule):host=target, confidence=0.9,)- lots_audit_func = kwargs.get("lots_audit_func") or _audit_lots_egress_sync- lots_data = await loop.run_in_executor(None, lots_audit_func, target)+ audit_lots_egress = kwargs.get("audit_lots_egress", False)+ lots_audit_func = kwargs.get("lots_audit_func")+ if lots_audit_func or audit_lots_egress:+ lots_func = lots_audit_func or _audit_lots_egress_sync+ lots_data = await loop.run_in_executor(None, lots_func, target)+ else:+ lots_data = {+ "lots_routes_open": [],+ "tenant_restrictions_enforced": False,+ "checked_endpoints": [],+ }if lots_data.get("lots_routes_open") and not lots_data.get("tenant_restrictions_enforced"):self.finding(diff --git a/ares/modules/params.py b/ares/modules/params.pyindex aef4432..c35ca0e 100644--- a/ares/modules/params.py+++ b/ares/modules/params.py@@ -834,6 +834,11 @@ class StagedCollectionParams(ModuleParams):max_files: int = param("Max files to collect", required=False, default=200, ge=1, le=1000)+ audit_lots_egress: bool = param(+ "Enable Living-off-the-Trusted-Services cloud egress audit",+ required=False,+ default=False,+ )# ── Lateral movement modules ──────────────────────────────────────────────────diff --git a/tests/unit/test_structured_maintenance.py b/tests/unit/test_structured_maintenance.pynew file mode 100644index 0000000..c07b3f7--- /dev/null+++ b/tests/unit/test_structured_maintenance.py@@ -0,0 +1,257 @@+"""+Unit tests verifying Structured Maintenance & Security Hardening Remediations.++Covers:+1. StagedCollectionModule LOTS cloud egress audit default-disabled invariant & SSL verification.+2. McpScopeGate bracketed IPv6 and host:port parsing against CIDR and wildcard scopes.+3. AresMcpServer error sanitization preventing raw exception string disclosure.+4. Windows UTF-8 invariance across CampaignStore, KeyRegistry, and potfile handling.+"""+from __future__ import annotations++import asyncio+import json+import ssl+from pathlib import Path+from unittest.mock import AsyncMock, MagicMock, patch+import pytest++from ares.core.campaign import Campaign, NoiseProfile, ScopeEntry+from ares.core.config import AresSettings+from ares.core.context import ExecutionContext+from ares.core.noise import NoiseController+from ares.mcp.protocol import ErrorCode, JSONRPCRequest+from ares.mcp.security import McpScopeGate+from ares.mcp.server import AresMcpServer+from ares.modules.exfil.staged_collection import StagedCollectionModule, _audit_lots_egress_sync+from ares.modules.params import StagedCollectionParams+++def _make_module(cls):+ settings = AresSettings()+ campaign = Campaign(+ name="Maintenance-Test",+ client="TestClient",+ operator="tester",+ scope=[ScopeEntry(cidr="10.0.0.0/8"), ScopeEntry(cidr="2001:db8::/32")],+ noise_profile=NoiseProfile.NORMAL,+ )+ noise = NoiseController(campaign)+ return cls(settings=settings, campaign=campaign, noise=noise)+++# ── 1. Staged Collection LOTS Egress Guard & SSL Safety ───────────────────────++class TestStagedCollectionSafety:+ """Verifies that staged collection does not leak un-scoped egress or disable SSL validation."""++ def test_staged_collection_params_defaults_lots_egress_to_false(self):+ params = StagedCollectionParams(+ target="10.0.0.1",+ username="operator",+ destination="/tmp/stage",+ )+ assert params.audit_lots_egress is False++ @pytest.mark.asyncio+ async def test_staged_collection_run_skips_lots_egress_by_default(self):+ mod = _make_module(StagedCollectionModule)+ mod.before_request = AsyncMock()+ mod.noise.jitter.sleep = AsyncMock()+ mod.noise.rate_limiter.acquire = AsyncMock()++ mock_ssh = MagicMock()+ mock_stdout = MagicMock()+ mock_stdout.read.return_value = b""+ mock_ssh.exec_command.return_value = (MagicMock(), mock_stdout, MagicMock())++ with patch("paramiko.SSHClient", return_value=mock_ssh), \+ patch("ares.modules.exfil.staged_collection._audit_lots_egress_sync") as mock_lots:+ findings, raw = await mod.run(+ target="10.0.0.1",+ username="operator",+ destination="/tmp/stage",+ )+ # When audit_lots_egress is False and no custom func is provided, LOTS audit must NOT be called+ assert not mock_lots.called+ assert raw["lots_routes_open"] == []+ assert raw["tenant_restrictions_enforced"] is False+ assert not any("LOTS Exfiltration Route Open" in f.title for f in findings)++ @pytest.mark.asyncio+ async def test_staged_collection_run_invokes_lots_when_explicitly_requested(self):+ mod = _make_module(StagedCollectionModule)+ mod.before_request = AsyncMock()+ mod.noise.jitter.sleep = AsyncMock()+ mod.noise.rate_limiter.acquire = AsyncMock()++ mock_ssh = MagicMock()+ mock_stdout = MagicMock()+ mock_stdout.read.return_value = b""+ mock_ssh.exec_command.return_value = (MagicMock(), mock_stdout, MagicMock())++ mock_lots_result = {+ "lots_routes_open": ["m365_graph"],+ "tenant_restrictions_enforced": False,+ "checked_endpoints": ["m365_graph", "aws_s3", "azure_blob"],+ }++ with patch("paramiko.SSHClient", return_value=mock_ssh), \+ patch("ares.modules.exfil.staged_collection._audit_lots_egress_sync", return_value=mock_lots_result) as mock_lots:+ findings, raw = await mod.run(+ target="10.0.0.1",+ username="operator",+ destination="/tmp/stage",+ audit_lots_egress=True,+ )+ assert mock_lots.called+ assert raw["lots_routes_open"] == ["m365_graph"]+ assert any("LOTS Exfiltration Route Open" in f.title for f in findings)++ def test_audit_lots_egress_uses_verified_ssl_context(self):+ """Verifies _audit_lots_egress_sync uses ssl.create_default_context() without CERT_NONE."""+ with patch("urllib.request.urlopen") as mock_urlopen, \+ patch("ssl.create_default_context") as mock_ssl_ctx:+ fake_ctx = MagicMock()+ mock_ssl_ctx.return_value = fake_ctx+ mock_resp = MagicMock()+ mock_resp.headers = {}+ mock_resp.__enter__.return_value = mock_resp+ mock_urlopen.return_value = mock_resp++ _audit_lots_egress_sync("10.0.0.1")++ assert mock_ssl_ctx.called+ # Ensure CERT_NONE and check_hostname = False were NOT set+ assert fake_ctx.verify_mode != ssl.CERT_NONE+ assert fake_ctx.check_hostname is not False+++# ── 2. McpScopeGate IPv6 & Host:Port Parsing ─────────────────────────────────++class TestMcpScopeGateIPv6Parsing:+ """Verifies that McpScopeGate parses bracketed IPv6 and host:port combinations accurately."""++ def test_bracketed_ipv6_with_port_matches_cidr(self):+ scope_rules = ["2001:db8::/32", "10.0.0.0/8"]+ assert McpScopeGate.is_in_scope("[2001:db8::1]:445", scope_rules) is True+ assert McpScopeGate.is_in_scope("[2001:db8::1]", scope_rules) is True+ assert McpScopeGate.is_in_scope("2001:db8::1", scope_rules) is True++ def test_bracketed_ipv6_out_of_scope_rejected(self):+ scope_rules = ["2001:db8::/32"]+ assert McpScopeGate.is_in_scope("[2001:cafe::1]:445", scope_rules) is False+ assert McpScopeGate.is_in_scope("[fe80::1]:80", scope_rules) is False++ def test_ipv4_with_port_matches_cidr(self):+ scope_rules = ["192.168.1.0/24"]+ assert McpScopeGate.is_in_scope("192.168.1.50:8080", scope_rules) is True+ assert McpScopeGate.is_in_scope("192.168.2.50:8080", scope_rules) is False++ def test_hostname_with_port_matches_wildcard(self):+ scope_rules = ["*.corp.local"]+ assert McpScopeGate.is_in_scope("dc01.corp.local:88", scope_rules) is True+ assert McpScopeGate.is_in_scope("fileserver.internal.net:445", scope_rules) is False+++# ── 3. AresMcpServer Internal Error Sanitization ──────────────────────────────++class TestAresMcpServerErrorSanitization:+ """Verifies that internal server exceptions do not leak raw details to JSON-RPC clients."""++ @pytest.mark.asyncio+ async def test_internal_error_does_not_leak_raw_exception_string(self):+ server = AresMcpServer()+ # Mock tool_registry.list_tools to raise an internal exception with sensitive paths/queries+ server.tool_registry.list_tools = MagicMock(+ side_effect=RuntimeError("SELECT * FROM credentials WHERE key='SUPER_SECRET_123'; /var/run/ares.sock failed")+ )++ msg = {+ "jsonrpc": "2.0",+ "id": "req-99",+ "method": "tools/list",+ "params": {},+ }+ res = await server.handle_message(msg)++ assert res is not None+ assert res["id"] == "req-99"+ assert "error" in res+ assert res["error"]["code"] == ErrorCode.INTERNAL_ERROR.value+ # Raw exception string MUST NOT be disclosed+ assert "SUPER_SECRET_123" not in res["error"]["message"]+ assert "SELECT * FROM" not in res["error"]["message"]+ assert res["error"]["message"] == "Internal MCP server error. Consult system audit logs."+++# ── 4. Cross-Platform UTF-8 Invariance ────────────────────────────────────────++class TestCrossPlatformUtf8Invariance:+ """Verifies that persistence layers read and write UTF-8 safely without Windows code page corruption."""++ def test_campaign_store_roundtrip_with_multibyte_unicode(self, tmp_path):+ from ares.cli._store import save_campaign, load_campaign, campaigns_dir++ with patch("ares.cli._store.campaigns_dir", return_value=tmp_path):+ mock_campaign = MagicMock()+ mock_campaign.id = "camp-unicode-01"+ mock_campaign.model_dump_json.return_value = json.dumps({+ "id": "camp-unicode-01",+ "name": "Audit Keamanan ARES — Uji Aksara & Simbol 🛡️",+ "notes": "Penetrasi sistem internal: Target terkonfirmasi di lab pengujian (Jakarta/IDN).",+ "findings": [],+ }, ensure_ascii=False)++ save_campaign(mock_campaign)+ loaded = load_campaign("camp-unicode-01")++ assert loaded is not None+ assert loaded["id"] == "camp-unicode-01"+ assert "Uji Aksara & Simbol 🛡️" in loaded["name"]+ assert "Jakarta/IDN" in loaded["notes"]++ def test_key_registry_roundtrip_with_utf8(self, tmp_path):+ from ares.core.signing import KeyRegistry++ key_file = tmp_path / "trusted_keys.json"+ rev_file = tmp_path / "revoked_keys.json"++ with patch("ares.core.signing.TRUSTED_KEYS_PATH", key_file), \+ patch("ares.core.signing.REVOKED_KEYS_PATH", rev_file):+ registry = KeyRegistry(path=key_file)+ registry.add_trusted_key(+ key_id="key-001",+ public_key_pem="-----BEGIN PUBLIC KEY-----\nMIIB...IDN\n-----END PUBLIC KEY-----",+ author="Tim Keamanan & Audit — ARES Cyber Security",+ added_by="operator_utama",+ )++ # Reload into a fresh registry instance from disk+ fresh_registry = KeyRegistry(path=key_file)+ assert "key-001" in fresh_registry._keys+ assert "Tim Keamanan & Audit — ARES Cyber Security" in fresh_registry._keys["key-001"]["author"]++ @pytest.mark.asyncio+ async def test_cracking_worker_potfile_utf8_reading(self, tmp_path):+ from ares.credential.cracker import CrackingWorker, CrackJob++ worker = CrackingWorker(vault=MagicMock(), timeout_s=5)+ worker.tmpdir = tmp_path+ worker._hashcat = "hashcat"++ job = CrackJob(+ job_id="job-1",+ hash_value="aad3b435b51404eeaad3b435b51404ee",+ hash_type="ntlm",+ )++ potfile = tmp_path / f"{job.job_id}.pot"+ potfile.write_text("aad3b435b51404eeaad3b435b51404ee:RahasiaKunci2026!🔒\n", encoding="utf-8")++ mock_proc = MagicMock()+ mock_proc.communicate = AsyncMock(return_value=(b"", b""))++ with patch("asyncio.create_subprocess_exec", return_value=mock_proc):+ plain = await worker._run_hashcat(job)+ assert plain == "RahasiaKunci2026!🔒"