SSF Tools - Analysis Components Architecture¶
Overview¶
The Analysis Components architecture provides the core infrastructure for file analysis operations within SSF Tools. This system implements entropy analysis, credential detection, and content-aware threshold management through a protocol-based design with dependency injection.
The architecture supports multiple analysis types including Shannon entropy calculation, detect-secrets integration for credential scanning, and content-aware file classification. All components follow SOLID principles and integrate seamlessly with the SSF Tools container system.
Architectural Principles¶
Design Goals¶
- Protocol-First Design: All services implement well-defined protocols for testability and modularity
- Content-Aware Analysis: File-type-specific thresholds and analysis strategies reduce false positives
- Streaming Processing: Memory-efficient analysis of large files through chunk-based processing
- Dependency Injection: All components receive dependencies through the container system
- Research-Based Thresholds: Threshold values derived from peer-reviewed academic research
Key Benefits¶
- Modular Architecture: Easy to extend with new analysis types or detection methods
- Memory Efficiency: Streaming analysis supports files of any size without memory constraints
- High Accuracy: Content-aware thresholds significantly reduce false positive rates
- Container Integration: Seamless integration with SSF Tools dependency injection system
- CLI Ready: Direct integration with command-line interface through injected services
Architecture Overview¶
Analysis Service Protocols¶
The analysis architecture is built around well-defined protocols that enable testing, modularity, and dependency injection. For detailed protocol specifications, see the Architecture and Design Patterns document.
Key Protocol Overview¶
The architecture defines protocols for consistent service interfaces. For complete protocol definitions and specifications, see the Architecture and Design Patterns document.
- EntropyAnalyzerProtocol: Primary protocol for Shannon entropy analysis with content-aware thresholds
- CredentialDetectionProtocol: Protocol for credential detection using detect-secrets integration
- Threshold Management: Protocol for content-aware threshold management
Configuration Models¶
AnalysisConfiguration¶
Main configuration model for analysis operations.
Configuration Model¶
The configuration model defines the structure and validation rules for analysis settings. For the complete configuration protocol and validation details, see the Architecture and Design Patterns document.
Global Configuration Integration¶
The analysis system integrates with SSF Tools global configuration.
# In ssf-tools-config.yaml
global:
analysis_block_size: 8192 # Default block size for analysis
max_concurrent_files: 10 # Maximum files to process concurrently
analyze:
entropy:
content_aware_enabled: true # Enable content-aware thresholds
streaming_enabled: true # Enable streaming analysis
max_regions_per_file: 1000 # Maximum entropy regions per file
credentials:
detect_secrets_baseline: null # Path to detect-secrets baseline
exclude_patterns: [] # Patterns to exclude from scanning
Service Implementation¶
This section outlines the concrete implementations of the analysis services. For protocol definitions and interface specifications, see the Architecture and Design Patterns document.
EntropyAnalyzer¶
Primary implementation of entropy analysis with streaming support. See the architecture document for the complete implementation details and protocol definition.
DetectSecretsCredentialService¶
Implementation of credential detection using detect-secrets. See the architecture document for the complete implementation details and subprocess integration patterns.
ContentAwareThresholdManager¶
Service for managing content-aware entropy thresholds by file type. See the architecture document for the complete implementation details and protocol definition.
Container Integration¶
AnalysisContainer Registration¶
Services are registered in the dependency injection container for proper lifecycle management.
kp_ssf_tools.containers.analysis.AnalysisContainer
¶
Bases: DeclarativeContainer
Container for analysis services (entropy, wordlists, crypto detection).
Source code in src\kp_ssf_tools\containers\analysis.py
CLI Integration¶
Analyze Command Group¶
CLI commands that interact with analysis services through dependency injection.
kp_ssf_tools.cli.commands.analyze
¶
Entropy analysis CLI commands with streaming Excel export.
Classes¶
AnalysisSummary
¶
ExportContext
¶
Bases: NamedTuple
Context for Excel export operations.
Source code in src\kp_ssf_tools\cli\commands\analyze.py
ProcessingConfig
¶
Bases: NamedTuple
Configuration for file processing.
Source code in src\kp_ssf_tools\cli\commands\analyze.py
ProcessingContext
¶
Bases: NamedTuple
Context for file processing with analyzer and configs.
Source code in src\kp_ssf_tools\cli\commands\analyze.py
Functions¶
analyze_group()
¶
credentials(target, *, recursive, file_extensions, context_lines, scan_binary, max_binary_size, credential_service=Provide[ApplicationContainer.analysis.active_credential_service], rich_output=Provide[ApplicationContainer.core.rich_output], excel_service=Provide[ApplicationContainer.core.excel_export_service], timestamp_service=Provide[ApplicationContainer.core.timestamp], global_config_service=Provide[ApplicationContainer.core.global_config_service], analysis_config_service=Provide[ApplicationContainer.core.entropy_config_service])
¶
Detect credentials in files for PCI SSF 2.3 compliance.
Analyzes files for embedded credentials including usernames, passwords, API keys, and other sensitive information. Uses wordlists from SecLists and regex patterns to identify potential security issues.
Results are automatically exported to Excel with per-file worksheets
and a summary sheet. Output filename: analyze-credentials-
Arguments:
Examples:
# Basic credential detection
ssf_tools analyze credentials sample.py
# Analyze specific file types only
ssf_tools analyze credentials data/ --file-extensions .py --file-extensions .js
# Include more context around matches
ssf_tools analyze credentials config/ --context-lines 5
# Skip binary files to speed up analysis
ssf_tools analyze credentials project/ --no-scan-binary
Source code in src\kp_ssf_tools\cli\commands\analyze.py
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 | |
entropy(target, risk_threshold, file_block_size, analysis_block_size, step_size, ignore_pattern, *, no_recurse, include_samples, analyzer=Provide[ApplicationContainer.entropy.analyzer], rich_output=Provide[ApplicationContainer.core.rich_output], file_discovery=Provide[ApplicationContainer.core.file_discoverer], global_config_service=Provide[ApplicationContainer.core.global_config_service], entropy_config_service=Provide[ApplicationContainer.core.entropy_config_service], timestamp_service=Provide[ApplicationContainer.core.timestamp])
¶
Analyze entropy of files for PCI SSF 2.3 compliance.
Performs Shannon entropy analysis using content-aware thresholds to detect potentially suspicious patterns in files. Results are streamed directly to Excel with minimal memory usage.
Arguments:
Examples:
# Basic file analysis
ssf_tools analyze entropy sample.bin
# Analyze with higher risk threshold (fewer results)
ssf_tools analyze entropy sample.bin --risk-threshold high
# Analyze with custom block size
ssf_tools analyze entropy sample.bin --analysis-block-size 128
# Override file type detection
ssf_tools analyze entropy app.exe --force-file-type windows_pe
# Analyze directory non-recursively
ssf_tools analyze entropy data/ --no-recurse
Source code in src\kp_ssf_tools\cli\commands\analyze.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | |
Service Integration Patterns¶
File Processing Integration¶
Analysis services integrate with the core file processing system for efficient I/O operations through direct CLI orchestration.
The current implementation uses a streaming-based approach where CLI commands directly coordinate analysis workflows. See the Architecture and Design Patterns document for the complete implementation details of the file processing workflow.
This pattern provides: - Memory Efficiency: Streaming analysis with direct Excel export - Error Isolation: Per-file error handling without stopping the entire analysis - Progress Reporting: Real-time feedback on analysis progress - Configuration Flexibility: CLI overrides for analysis parameters
Core Models¶
Analysis Models Overview¶
The analysis system uses well-defined Pydantic models for inputs and results. For detailed model specifications, see the Architecture and Design Patterns document.
Key model categories include:
- Analysis Results: Structured outputs for entropy and credential detection operations
- Pattern Models: Detection result containers for identified patterns and regions
- Configuration Models: Input validation and type safety for analysis operations
Content-Aware Threshold Models¶
File-type-specific threshold definitions for accurate entropy classification.
kp_ssf_tools.analyze.models.content_aware
¶
Content-Aware Thresholds module.
Classes¶
ContentAwareThresholds
¶
Bases: SSFToolsBaseModel
File type-specific entropy thresholds loaded from configuration.
Source code in src\kp_ssf_tools\analyze\models\content_aware.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | |
Functions¶
for_file_type(file_type)
classmethod
¶
Factory method to create a threshold model for a specific file type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_type
|
FileType
|
The file type to get thresholds for |
required |
Returns:
| Type | Description |
|---|---|
ContentAwareThresholds
|
ContentAwareThresholds model instance with validated data |
Raises:
| Type | Description |
|---|---|
KeyError
|
If file_type is not supported |
Source code in src\kp_ssf_tools\analyze\models\content_aware.py
get_default_models()
classmethod
¶
Get pre-built Pydantic model instances for all file types.
Returns validated ContentAwareThresholds models instead of raw dicts. Use this method for runtime threshold management to avoid dict-to-model conversion.
Source code in src\kp_ssf_tools\analyze\models\content_aware.py
get_default_values()
classmethod
¶
Default threshold values for configuration file generation.
These values are derived from extensive academic research documented in docs/file-entropy-research.md, including: - Lyda & Hamrock (2007) IEEE foundational paper - Davies et al. (2022) NapierOne dataset (500,000+ files) - Practical Security Analytics (500,000 PE file analysis) - Multiple peer-reviewed studies with statistical validation
Returns a dict suitable for YAML configuration file generation.
Source code in src\kp_ssf_tools\analyze\models\content_aware.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
Analysis Type Definitions¶
Enumerations and type definitions used throughout the analysis system.
kp_ssf_tools.analyze.models.types
¶
Types for entropy analysis models.
Classes¶
ComplianceStatus
¶
Bases: StrEnum
PCI SSF compliance status levels.
Source code in src\kp_ssf_tools\analyze\models\types.py
CredentialRiskLevel
¶
Bases: StrEnum
Risk levels for detected credentials.
Source code in src\kp_ssf_tools\analyze\models\types.py
CryptoStructureType
¶
Bases: StrEnum
Types of cryptographic structures and credential patterns.
Source code in src\kp_ssf_tools\analyze\models\types.py
EntropyLevel
¶
Bases: StrEnum
Entropy classification levels (thresholds are file-type adaptive).
Source code in src\kp_ssf_tools\analyze\models\types.py
FileType
¶
Bases: StrEnum
File types for entropy analysis using Pygments lexer names where applicable.
Source code in src\kp_ssf_tools\analyze\models\types.py
Functions¶
from_pygments_lexer(lexer_name)
classmethod
¶
Map Pygments lexer names to FileType enums.
Handles multiple lexer names for the same language (e.g., "Python" vs "Python 3").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lexer_name
|
str
|
Name from Pygments lexer.name |
required |
Returns:
| Type | Description |
|---|---|
FileType
|
Corresponding FileType enum, defaults to UNKNOWN for unrecognized lexers |
Source code in src\kp_ssf_tools\analyze\models\types.py
Usage Examples¶
Basic Entropy Analysis¶
Command-line examples of entropy analysis operations.
# Basic entropy analysis
ssf_tools analyze entropy /path/to/file.bin
# Entropy analysis with custom block size
ssf_tools analyze entropy /path/to/directory --block-size 16384
# Output to specific file
ssf_tools analyze entropy /path/to/files --output analysis_results.xlsx
Credential Detection¶
Examples of credential scanning operations.
Service Usage in Code¶
Programmatic usage of analysis services within other commands.
@inject
def custom_analysis(
target: Path,
entropy_analyzer=Provide[ApplicationContainer.analysis.entropy_analyzer],
threshold_service=Provide[ApplicationContainer.analysis.threshold_service],
):
"""Custom analysis combining multiple services."""
# Get content-aware thresholds
file_type = FileType.PYTHON
thresholds = threshold_service.get_thresholds_for_file_type(file_type)
# Perform entropy analysis
for region in entropy_analyzer.analyze_file_generator(target, file_type):
level = threshold_service.classify_entropy_level(region.entropy, file_type)
if level in [EntropyLevel.HIGH, EntropyLevel.MEDIUM_HIGH]:
print(f"Suspicious region: {region.offset}-{region.offset + region.size}")
return analysis_results
Performance Considerations¶
Memory Efficiency¶
The analysis system uses streaming techniques to handle large files efficiently.
- Input File Streaming Analysis: Files are processed in chunks to maintain constant memory usage
- Results File Streaming: Results are exported to Excel as they are received using
xlsxwriter'sconstant_memoryoption. Memory consumption is set to10MBregardless of input file or result set size - Generator Patterns: Results are yielded incrementally to avoid loading entire result sets
- Resource Management: File handles and streams are properly managed through context managers
Processing Optimization¶
Multiple optimization strategies improve analysis performance.
- Content-Aware Processing: Different file types use optimized analysis strategies
- Parallel Processing: Multiple files can be analyzed concurrently when memory allows
- Early Termination: Analysis can stop early when maximum regions are reached
Testing Patterns¶
Mock Analysis Services¶
Create mock services for testing analysis workflows.
@pytest.fixture
def mock_entropy_analyzer():
"""Mock entropy analyzer for testing."""
mock_analyzer = Mock(spec=EntropyAnalyzerProtocol)
mock_analyzer.analyze_file_generator.return_value = iter([
EntropyRegion(offset=0, size=1024, entropy=7.8, level=EntropyLevel.HIGH)
])
return mock_analyzer
def test_entropy_analysis_with_mock(mock_entropy_analyzer):
"""Test entropy analysis with mocked analyzer."""
result = perform_analysis_with_analyzer(mock_entropy_analyzer)
assert len(result.high_entropy_regions) == 1
Integration Testing¶
Test analysis services with real dependencies and temporary files.
def test_entropy_analyzer_integration():
"""Test entropy analyzer with real dependencies."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create test file with known entropy characteristics
test_file = Path(temp_dir) / "test.bin"
with open(test_file, "wb") as f:
f.write(os.urandom(8192)) # High entropy data
# Test with real analyzer
analyzer = create_real_analyzer()
regions = list(analyzer.analyze_file_generator(test_file, FileType.UNKNOWN))
assert len(regions) > 0
assert all(region.entropy > 7.0 for region in regions)
The Analysis Components architecture provides a robust, extensible foundation for file analysis operations within SSF Tools, supporting both entropy analysis and credential detection through a unified, protocol-based design that integrates seamlessly with the broader SSF Tools ecosystem.