mirror of
https://github.com/SpecterOps/Nemesis
synced 2026-06-08 12:36:42 +00:00
9d62493623
* fix: upgrade python-multipart to 0.0.22 to address CVE-2026-24486 (Dependabot #683) * update deps+skill, pyright * pyright + tests in CLI * Add pyright type checking and enhance registry hive analysis Registry Hive Analyzer Enhancements: - Extract machine SID from SAM domain V value with binary SID decoding - Extract per-user metadata via regipy (ACB flags, timestamps, full name, comment via USER_ACCOUNT_V) - Compute password expiration from domain max password age policy - Detect empty LM/NT hashes via well-known constants - Parse DCC cached domain credentials into structured entries - Store DPAPI system machine_key/user_key as separate fields - Add structured secret_type field to LSA secrets (dcc, dpapi_system, hex_blob, generic) - Add detailed markdown and plain-text formatters for SAM accounts and LSA secrets, replacing _get_lsa_secret_output_string - Fix SYSTEM hive attribute names: computer_name -> machinename, current_control_set -> currentcontrol - Switch SAM user iteration from sam.users to sam.secrets (pypykatz API) - Add FILETIME-to-UTC and regipy value-to-bytes helpers New Tests: - Add test_registry_hive.py with SAM, SECURITY, and SYSTEM hive fixtures - Add test_container.py for container analyzer - Add SAM/SECURITY/SYSTEM binary test fixtures Pyright Setup: - Add pyrightconfig.json (basic mode, Python 3.13) to all libs and projects - Add pyright>=1.1 as dev dependency to all pyproject.toml files - Update all uv.lock files accordingly - Update lint.sh to deactivate active venvs and verify pyright availability Type Annotation Fixes: - Fix globals initialized as None without Optional type across file_enrichment, document_conversion, and agents global_vars - Fix StorageMinio return types: upload/upload_file/upload_uploadfile return str, not uuid.UUID - Fix MockStorageMinio to match updated StorageMinio return types - Add explicit type annotations to dict literals in chromekey.py, pdf/analyzer.py, registry_hive/analyzer.py, and publish_findings.py - Fix kubeconfig current_context parameter to accept str | None - Fix container_contents allowed_extensions: set = None -> set | None = None - Fix file subscription file_queue: asyncio.Queue = None -> asyncio.Queue | None = None - Fix web_api upload_file to wrap object_id in uuid.UUID() for response None-safety Assertions: - Add assert statements for asyncpg_pool, tracking_service, workflow_client, workflow_manager, file_linking_engine, file_queue, gotenberg_url, and process.stdout across all activity, subscription, route, and workflow files in file_enrichment and document_conversion - Add assertions for asyncpg_pool in all chromium processors - Add assertions for File.from_metadata timestamp/expiration fields - Add assertion for alerting GQL client session type Pyright Ignore Annotations: - Suppress third-party type issues in Dapr workflow/activity APIs, gRPC subscription imports, ccache/lnk/office_doc attribute access, and nemesis_dpapi FlagMixin operators - Add file-level suppression for office2john.py, pdf2john.py, pe/analyzer.py, and test harness files Bug Fixes: - Fix office2john.py format string: bare % filename -> % (filename, stream) - Fix container analyzer 7z iteration: iterate sz.files list instead of calling .items() - Fix file_linking rules_engine: store match result to avoid double call - Fix logger.exception calls: remove exception object as first arg in storage.py, cookies.py, enrichments.py, housekeeping/main.py - Fix document_conversion lifespan: use stack.callback() for sync shutdown - Fix NoseyParkerOutput fallback: add missing workflow_id field - Fix regipy hive_type: handle None return from RegistryHive.hive_type DPAPI Manager: - Remove unused guid parameter from get_system_credentials across DpapiManager, NullDpapiManager, and DpapiManagerProtocol Added Missing Dependencies (file_enrichment_modules): - pypykatz>=0.6.11, pyarrow>=19.0.1, msoffcrypto-tool>=5.4.2, oletools>=0.60.2, regipy>=5.2.0, pillow>=11.3.0 * quiet console logs * feat: lazy file loading with backend range request support Add offset/length query params to the download endpoint so the frontend can request partial file content. FileViewer now fetches data on demand — hex, transform (Strings, etc.), ZIP, SQLite, and image tabs only load when activated. Text-based content is capped at 10 MB previews. * fix: transform tabs stuck on "Loading content..." - Prevent stale WS subscription from overwriting fetched content - Show retry button when transform content fails to load - Reset fetch guard on non-OK HTTP responses * fix: hex tab deferred loading with full file content * feat: truncation dropdown, spinner overlay, and tab render refactor - Add truncation dropdown to MonacoContentViewer for files > 10MB, replacing the old banner alert and hex "Load Hex View" button - Add spinner overlay on Monaco editor while loading full content - Wire truncation support to transform tabs (monaco/json types), enrichment tabs, and text tabs - Change hex tab to auto-load first 10MB preview with dropdown for full file instead of requiring manual load - Default word wrap to off - Remove "File is too large" warning message - Refactor ~150-line ternary chain into explicit per-tab render functions (renderPreviewTab, renderZipTab, renderSqliteTab, renderTextTab, renderHexTab, renderTabContent) with shared helpers (renderFullFileContent, getTextContent) * feat: improve Yara Rules UI - Compact table rows with tighter padding - Add X button and Esc key to close editor dialog - Disable Save button when rule content is unchanged - Disable Create button with inline warning when rule name already exists - Default source to "Created manually by <user>" for new rules - Update source placeholder to "e.g. /yara_rules/custom.yara" * update gitignore --------- Co-authored-by: Lee Chagolla-Christensen <lee@localhost>
356 lines
12 KiB
Python
356 lines
12 KiB
Python
"""Tests for cli.config module - StrictHttpUrl, credential models, and config validation."""
|
|
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
import yaml
|
|
from cli.config import (
|
|
CobaltStrikeConfig,
|
|
Config,
|
|
MythicConfig,
|
|
NemesisConfig,
|
|
OutflankConfig,
|
|
PasswordCredential,
|
|
StrictHttpUrl,
|
|
TokenCredential,
|
|
load_config,
|
|
)
|
|
from pydantic import ValidationError
|
|
|
|
# --- StrictHttpUrl ---
|
|
|
|
|
|
class TestStrictHttpUrl:
|
|
def test_valid_https_url(self):
|
|
url = StrictHttpUrl("https://example.com")
|
|
assert str(url) == "https://example.com"
|
|
|
|
def test_valid_http_url(self):
|
|
url = StrictHttpUrl("http://example.com")
|
|
assert str(url) == "http://example.com"
|
|
|
|
def test_valid_url_with_port(self):
|
|
url = StrictHttpUrl("https://example.com:8443")
|
|
assert str(url) == "https://example.com:8443"
|
|
|
|
def test_strips_trailing_slash(self):
|
|
url = StrictHttpUrl("https://example.com/")
|
|
assert str(url) == "https://example.com"
|
|
|
|
def test_strips_multiple_trailing_slashes(self):
|
|
url = StrictHttpUrl("https://example.com///")
|
|
assert str(url) == "https://example.com"
|
|
|
|
def test_repr(self):
|
|
url = StrictHttpUrl("https://example.com/")
|
|
assert repr(url) == "StrictHttpUrl('https://example.com')"
|
|
|
|
def test_is_string_subclass(self):
|
|
url = StrictHttpUrl("https://example.com")
|
|
assert isinstance(url, str)
|
|
|
|
def test_url_with_path(self):
|
|
url = StrictHttpUrl("https://example.com/api/v1")
|
|
assert str(url) == "https://example.com/api/v1"
|
|
|
|
|
|
# --- Credential Models ---
|
|
|
|
|
|
class TestPasswordCredential:
|
|
def test_create(self):
|
|
cred = PasswordCredential(username="admin", password="secret")
|
|
assert cred.username == "admin"
|
|
assert cred.password == "secret"
|
|
|
|
def test_extra_fields_forbidden(self):
|
|
with pytest.raises(ValidationError):
|
|
PasswordCredential(username="admin", password="secret", extra="nope")
|
|
|
|
|
|
class TestTokenCredential:
|
|
def test_create(self):
|
|
cred = TokenCredential(token="abc123")
|
|
assert cred.token == "abc123"
|
|
|
|
def test_extra_fields_forbidden(self):
|
|
with pytest.raises(ValidationError):
|
|
TokenCredential(token="abc123", extra="nope")
|
|
|
|
|
|
# --- NemesisConfig ---
|
|
|
|
|
|
class TestNemesisConfig:
|
|
def test_create_with_defaults(self):
|
|
cfg = NemesisConfig(
|
|
url=StrictHttpUrl("https://nemesis.local:8080"),
|
|
credential=PasswordCredential(username="u", password="p"),
|
|
)
|
|
assert str(cfg.url) == "https://nemesis.local:8080"
|
|
assert cfg.expiration_days == 100
|
|
assert cfg.max_file_size == 1_000_000_000
|
|
|
|
def test_create_with_custom_values(self):
|
|
cfg = NemesisConfig(
|
|
url=StrictHttpUrl("https://nemesis.local:8080"),
|
|
credential=PasswordCredential(username="u", password="p"),
|
|
expiration_days=30,
|
|
max_file_size=500_000,
|
|
)
|
|
assert cfg.expiration_days == 30
|
|
assert cfg.max_file_size == 500_000
|
|
|
|
def test_expiration_days_must_be_positive(self):
|
|
with pytest.raises(ValidationError):
|
|
NemesisConfig(
|
|
url=StrictHttpUrl("https://nemesis.local"),
|
|
credential=PasswordCredential(username="u", password="p"),
|
|
expiration_days=0,
|
|
)
|
|
|
|
def test_max_file_size_must_be_positive(self):
|
|
with pytest.raises(ValidationError):
|
|
NemesisConfig(
|
|
url=StrictHttpUrl("https://nemesis.local"),
|
|
credential=PasswordCredential(username="u", password="p"),
|
|
max_file_size=-1,
|
|
)
|
|
|
|
|
|
# --- MythicConfig ---
|
|
|
|
|
|
class TestMythicConfig:
|
|
def test_create_with_password_credential(self):
|
|
cfg = MythicConfig(
|
|
url=StrictHttpUrl("https://mythic.local:7443"),
|
|
credential=PasswordCredential(username="mythic_admin", password="pass"),
|
|
)
|
|
assert isinstance(cfg.credential, PasswordCredential)
|
|
|
|
def test_create_with_token_credential(self):
|
|
cfg = MythicConfig(
|
|
url=StrictHttpUrl("https://mythic.local:7443"),
|
|
credential=TokenCredential(token="my-api-token"),
|
|
)
|
|
assert isinstance(cfg.credential, TokenCredential)
|
|
|
|
def test_validate_credential_from_dict_token(self):
|
|
"""Test that the field_validator correctly handles dict input with token."""
|
|
cfg = MythicConfig(
|
|
url=StrictHttpUrl("https://mythic.local:7443"),
|
|
credential={"token": "my-api-token"},
|
|
)
|
|
assert isinstance(cfg.credential, TokenCredential)
|
|
assert cfg.credential.token == "my-api-token"
|
|
|
|
def test_validate_credential_from_dict_password(self):
|
|
"""Test that the field_validator correctly handles dict input with username/password."""
|
|
cfg = MythicConfig(
|
|
url=StrictHttpUrl("https://mythic.local:7443"),
|
|
credential={"username": "admin", "password": "secret"},
|
|
)
|
|
assert isinstance(cfg.credential, PasswordCredential)
|
|
assert cfg.credential.username == "admin"
|
|
|
|
|
|
# --- OutflankConfig ---
|
|
|
|
|
|
class TestOutflankConfig:
|
|
def test_create_minimal(self):
|
|
cfg = OutflankConfig(
|
|
url=StrictHttpUrl("https://outflank.local"),
|
|
credential=PasswordCredential(username="u", password="p"),
|
|
)
|
|
assert cfg.downloads_dir_path is None
|
|
assert cfg.poll_interval_sec == 3
|
|
|
|
def test_create_with_downloads_dir(self):
|
|
cfg = OutflankConfig(
|
|
url=StrictHttpUrl("https://outflank.local"),
|
|
credential=PasswordCredential(username="u", password="p"),
|
|
downloads_dir_path="/tmp/downloads",
|
|
)
|
|
assert cfg.downloads_dir_path == Path("/tmp/downloads")
|
|
|
|
def test_poll_interval_must_be_positive(self):
|
|
with pytest.raises(ValidationError):
|
|
OutflankConfig(
|
|
url=StrictHttpUrl("https://outflank.local"),
|
|
credential=PasswordCredential(username="u", password="p"),
|
|
poll_interval_sec=0,
|
|
)
|
|
|
|
|
|
# --- CobaltStrikeConfig ---
|
|
|
|
|
|
class TestCobaltStrikeConfig:
|
|
def test_create(self):
|
|
cfg = CobaltStrikeConfig(
|
|
url=StrictHttpUrl("https://cs.local"),
|
|
credential=PasswordCredential(username="u", password="p"),
|
|
project="assessment-1",
|
|
)
|
|
assert cfg.project == "assessment-1"
|
|
assert cfg.poll_interval_sec == 3
|
|
|
|
def test_poll_interval_must_be_positive(self):
|
|
with pytest.raises(ValidationError):
|
|
CobaltStrikeConfig(
|
|
url=StrictHttpUrl("https://cs.local"),
|
|
credential=PasswordCredential(username="u", password="p"),
|
|
project="test",
|
|
poll_interval_sec=-1,
|
|
)
|
|
|
|
|
|
# --- Config (root) ---
|
|
|
|
|
|
class TestConfig:
|
|
def _nemesis_cfg(self):
|
|
return {
|
|
"url": "https://nemesis.local:8080",
|
|
"credential": {"username": "u", "password": "p"},
|
|
}
|
|
|
|
def test_create_minimal(self):
|
|
cfg = Config(nemesis=self._nemesis_cfg())
|
|
assert cfg.nemesis is not None
|
|
assert cfg.mythic == []
|
|
assert cfg.outflank == []
|
|
assert cfg.cobaltstrike == []
|
|
|
|
def test_ensure_list_wraps_dict(self):
|
|
"""ensure_list validator should wrap a single dict in a list."""
|
|
cfg = Config(
|
|
nemesis=self._nemesis_cfg(),
|
|
mythic={
|
|
"url": "https://mythic.local:7443",
|
|
"credential": {"username": "u", "password": "p"},
|
|
},
|
|
)
|
|
assert isinstance(cfg.mythic, list)
|
|
assert len(cfg.mythic) == 1
|
|
|
|
def test_ensure_list_none_becomes_empty(self):
|
|
cfg = Config(nemesis=self._nemesis_cfg(), mythic=None)
|
|
assert cfg.mythic == []
|
|
|
|
def test_cache_db_path_default(self):
|
|
cfg = Config(nemesis=self._nemesis_cfg())
|
|
assert cfg.cache_db_path == Path("/tmp/connectors")
|
|
|
|
def test_cache_db_path_custom(self):
|
|
cfg = Config(nemesis=self._nemesis_cfg(), cache_db_path="/custom/path")
|
|
assert cfg.cache_db_path == Path("/custom/path")
|
|
|
|
def test_conn_timeout_default(self):
|
|
cfg = Config(nemesis=self._nemesis_cfg())
|
|
assert cfg.conn_timeout_sec == 30
|
|
|
|
|
|
# --- load_config ---
|
|
|
|
|
|
class TestLoadConfig:
|
|
def test_load_valid_yaml(self):
|
|
config_data = {
|
|
"nemesis": {
|
|
"url": "https://nemesis.local:8080",
|
|
"credential": {"username": "admin", "password": "pass"},
|
|
},
|
|
}
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
|
|
yaml.dump(config_data, f)
|
|
f.flush()
|
|
cfg = load_config(f.name)
|
|
|
|
assert isinstance(cfg, Config)
|
|
assert str(cfg.nemesis.url) == "https://nemesis.local:8080"
|
|
|
|
def test_load_nonexistent_file_raises(self):
|
|
with pytest.raises(FileNotFoundError):
|
|
load_config("/nonexistent/path/config.yaml")
|
|
|
|
|
|
# --- Settings file integration tests ---
|
|
# Validate that the example settings YAML files parse correctly through the Config model.
|
|
|
|
SETTINGS_DIR = Path(__file__).resolve().parent.parent
|
|
|
|
|
|
class TestSettingsCobaltstrike:
|
|
"""Validate settings_cobaltstrike.yaml parses correctly through the Config model."""
|
|
|
|
@pytest.fixture()
|
|
def cfg(self):
|
|
return load_config(str(SETTINGS_DIR / "settings_cobaltstrike.yaml"))
|
|
|
|
def test_loads_as_config(self, cfg):
|
|
assert isinstance(cfg, Config)
|
|
|
|
def test_root_fields(self, cfg):
|
|
assert cfg.cache_db_path == Path("/tmp/nemesis_connectors")
|
|
assert cfg.conn_timeout_sec == 5
|
|
assert cfg.validate_https_certs is False
|
|
|
|
def test_nemesis_section(self, cfg):
|
|
assert str(cfg.nemesis.url) == "https://nemesis.example.com"
|
|
assert cfg.nemesis.credential.username == "connector_bot"
|
|
assert cfg.nemesis.credential.password == "pass"
|
|
assert cfg.nemesis.expiration_days == 100
|
|
assert cfg.nemesis.max_file_size == 1_000_000_000
|
|
|
|
def test_cobaltstrike_section(self, cfg):
|
|
assert isinstance(cfg.cobaltstrike, list)
|
|
assert len(cfg.cobaltstrike) == 1
|
|
cs = cfg.cobaltstrike[0]
|
|
assert str(cs.url) == "https://cobaltstrike.example.com:50443"
|
|
assert cs.credential.username == "nemesis_bot"
|
|
assert cs.credential.password == "cobaltstrike_password"
|
|
assert cs.project == "my-assessment"
|
|
assert cs.poll_interval_sec == 3
|
|
|
|
def test_unset_connectors_are_empty(self, cfg):
|
|
assert cfg.mythic == []
|
|
assert cfg.outflank == []
|
|
|
|
|
|
class TestSettingsOutflank:
|
|
"""Validate settings_outflank.yaml parses correctly through the Config model."""
|
|
|
|
@pytest.fixture()
|
|
def cfg(self):
|
|
return load_config(str(SETTINGS_DIR / "settings_outflank.yaml"))
|
|
|
|
def test_loads_as_config(self, cfg):
|
|
assert isinstance(cfg, Config)
|
|
|
|
def test_root_fields(self, cfg):
|
|
assert cfg.cache_db_path == Path("/tmp/nemesis_connectors")
|
|
assert cfg.conn_timeout_sec == 5
|
|
assert cfg.validate_https_certs is True
|
|
|
|
def test_nemesis_section(self, cfg):
|
|
assert str(cfg.nemesis.url) == "https://nemesis.example.com"
|
|
assert cfg.nemesis.credential.username == "connector_bot"
|
|
|
|
def test_outflank_section(self, cfg):
|
|
assert isinstance(cfg.outflank, list)
|
|
assert len(cfg.outflank) == 1
|
|
of = cfg.outflank[0]
|
|
assert str(of.url) == "https://stage1.example.com"
|
|
assert of.credential.username == "nemesis_bot"
|
|
assert of.credential.password == "outflank_password"
|
|
assert of.downloads_dir_path is None
|
|
assert of.poll_interval_sec == 3 # default
|
|
|
|
def test_unset_connectors_are_empty(self, cfg):
|
|
assert cfg.mythic == []
|
|
assert cfg.cobaltstrike == []
|