Architecture Overview
This document provides a comprehensive overview of AI Shell's architecture, design patterns, and component interactions.
๐๏ธ High-Level Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ User Interface โ
โ (ui.py) โ
โโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Main Application โ
โ (main.py) โ
โ โโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโ
โ โ Mode โ Provider โ Security โโ
โ โ Selection โ Management โ Validation โโ
โ โโโโโโโโโโโโโโโดโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ
โConfigurationโ โ LLM Provider โ โ Command โ
โ Management โ โ (llm.py) โ โ Executor โ
โ (config.py) โ โ โ โ (executor.py)โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ
โ โ โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโโโโ
โ External Services โ
โ โ
โ โโโโโโโโโโโ โโโโโโโโโโโโโ โ
โ โ Gemini โ โ Ollama โ โ
โ โ API โ โ Local LLM โ โ
โ โโโโโโโโโโโ โโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโ โโโโโโโโโโโโโ โ
โ โMSFConsoleโ โ System โ โ
โ โ PTY โ โ Commands โ โ
โ โโโโโโโโโโโ โโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐งฉ Core Components
1. Main Application (main.py)
Responsibilities: - Application entry point and CLI argument parsing - Mode selection and workflow orchestration - Provider initialization and management - Interactive terminal handling with PTY support
Key Functions:
- main(): Primary application entry point
- setup_logging(): Logging configuration
- parse_arguments(): CLI argument handling
- pty_loop_base(): Generic PTY-based tool integration loop
- metasploit_loop() / wapiti_loop(): Mode-specific async PTY wrappers
Design Patterns: - Command Pattern: Mode selection and execution - Factory Pattern: Provider instantiation - Observer Pattern: Real-time output streaming
2. Configuration Management (config.py)
Responsibilities: - YAML-based configuration loading and validation - Environment variable integration - Default value management - Dynamic configuration updates
Key Classes:
class Config:
def __init__(self, config_data=None, config_file=None)
def get(self, key, default=None)
def set(self, key, value)
def save(self, filename)
Configuration Structure:
llm:
provider: gemini|local
gemini:
api_key: string
model: string
local:
host: string
port: integer
model: string
security:
require_confirmation: boolean
dangerous_commands: list
logging:
level: DEBUG|INFO|WARNING|ERROR
file: string
format: string
training:
dataset_file: string
auto_log: boolean
3. LLM Provider System (llm.py)
Architecture:
# Base Provider Interface
class LLMProvider:
def generate_response(
self,
prompt: str,
mode: str,
system_prompt: str = ASSISTANT_SYSTEM_PROMPT,
chat_session: Any = None,
) -> Tuple[Optional[str], Any]:
raise NotImplementedError
# Concrete Implementations
class GeminiProvider(LLMProvider) # Google Gemini via google-generativeai
class LocalLLMProvider(LLMProvider) # Local models via Ollama REST API
Provider Selection Logic:
def get_llm_provider() -> LLMProvider:
config = get_config()
provider_type = config.get("llm.provider", "gemini")
if provider_type == "gemini":
return GeminiProvider(config.get("llm.gemini", {}))
elif provider_type == "local":
return LocalLLMProvider(config.get("llm.local", {}))
System Prompts:
- TRANSLATOR_SYSTEM_PROMPT: Command translation mode
- ASSISTANT_SYSTEM_PROMPT: Conversational assistance mode
- METASPLOIT_SYSTEM_PROMPT: Security testing mode
- WAPITI_SYSTEM_PROMPT: Web application scanning mode
4. Command Execution (executor.py)
Security Architecture:
class SecurityChecker:
def is_dangerous_command(self, command: str) -> bool
def validate_command(self, command: str) -> Tuple[bool, Optional[str]]
class CommandExecutor:
def execute_command(self, command: str, user_prompt: str) -> bool
class TrainingDataLogger:
def log_training_pair(self, prompt: str, command: str, feedback: str) -> None
def get_executor() -> CommandExecutor # Returns global singleton instance
Security Layers: 1. Input Validation: Command syntax and structure validation 2. Dangerous Command Detection: Pattern matching against known dangerous commands 3. User Confirmation: Interactive approval for command execution 4. Output Sanitization: Safe handling of command output
Execution Modes: - Interactive: Real-time output streaming with PTY - Batch: Standard subprocess execution - Dry Run: Validation without execution
5. User Interface (ui.py)
Component Categories:
# Color and Formatting
colors = {
'primary': '\033[96m', # Cyan
'secondary': '\033[95m', # Magenta
'success': '\033[92m', # Green
'warning': '\033[93m', # Yellow
'error': '\033[91m', # Red
'info': '\033[94m', # Blue
'reset': '\033[0m' # Reset
}
# UI Components
def print_banner()
def print_mode_selection()
def print_provider_selection()
def format_command_output(output: str) -> str
Responsive Design: - Terminal width detection - Dynamic content wrapping - Cross-platform color support
๐ Data Flow
Command Translation Flow
graph TD
A[User Input] --> B[Input Validation]
B --> C[LLM Provider Selection]
C --> D[Generate Response]
D --> E[Extract Command]
E --> F[Security Validation]
F --> G{User Confirmation}
G -->|Yes| H[Execute Command]
G -->|No| I[Return to Input]
H --> J[Stream Output]
J --> K[Log Training Data]
Configuration Loading Flow
graph TD
A[Application Start] --> B{Config File Exists?}
B -->|Yes| C[Load YAML Config]
B -->|No| D[Use Default Config]
C --> E[Environment Variable Override]
D --> E
E --> F[Validate Configuration]
F --> G[Initialize Components]
PTY Session Flow
graph TD
A[Start PTY Session] --> B[Fork Process]
B --> C[Setup Master/Slave PTY]
C --> D[Launch Target Application]
D --> E[Real-time I/O Handling]
E --> F{User Input or Tool Output?}
F -->|User Input| G[Process with LLM if needed]
F -->|Tool Output| H[Display to User]
G --> I[Send to Tool]
H --> E
I --> E
๐ Security Architecture
Defense in Depth
- Input Layer
- Command injection prevention
- Input sanitization
-
Length and format validation
-
Validation Layer
- Dangerous command detection
- Pattern matching
-
Whitelist/blacklist enforcement
-
Execution Layer
- User confirmation requirements
- Process isolation
-
Resource limiting
-
Output Layer
- Output sanitization
- Sensitive data filtering
- Logging and auditing
Threat Model
Threats Addressed: - Command injection attacks - Malicious LLM responses - Privilege escalation - Data exfiltration - Denial of service
Mitigations: - Input validation and sanitization - Command whitelisting/blacklisting - User confirmation workflows - Process sandboxing - Resource monitoring
๐งช Testing Architecture
Test Categories
- Unit Tests
- Individual component testing
- Mock external dependencies
-
Edge case validation
-
Integration Tests
- Component interaction testing
- Configuration validation
-
Provider integration
-
Security Tests
- Command injection prevention
- Dangerous command detection
-
Input validation
-
End-to-End Tests
- Full workflow testing
- User interaction simulation
- Real provider integration
Test Structure
tests/
โโโ unit/
โ โโโ test_config.py
โ โโโ test_llm.py
โ โโโ test_executor.py
โ โโโ test_ui.py
โโโ integration/
โ โโโ test_provider_integration.py
โ โโโ test_workflow_integration.py
โโโ security/
โ โโโ test_command_validation.py
โ โโโ test_injection_prevention.py
โโโ e2e/
โโโ test_full_workflow.py
๐ Performance Considerations
Optimization Strategies
- Async Operations
- Non-blocking LLM API calls
- Concurrent request processing
-
Real-time response streaming
-
Caching
- Configuration caching
- Provider initialization caching
-
Response pattern caching
-
Resource Management
- Connection pooling for API calls
- Memory-efficient output streaming
- Process lifecycle management
Scalability
- Horizontal Scaling: Multiple provider instances
- Vertical Scaling: Resource optimization
- Load Balancing: Provider selection strategies
๐ฎ Future Architecture
Planned Enhancements
- Plugin System
- Modular tool integration
- Third-party provider support
-
Custom command handlers
-
Distributed Architecture
- Remote LLM provider support
- Distributed configuration management
-
Multi-user support
-
Advanced Security
- Role-based access control
- Audit logging
-
Compliance frameworks
-
Machine Learning
- Local model fine-tuning
- Usage pattern analysis
- Predictive command suggestions
This architecture enables AI Shell to be maintainable, extensible, and secure while providing a rich user experience for command-line operations.