Summary
Thecd function changes the Apollo agent’s current working directory to a specified directory path. This command affects the working directory for all subsequent file system operations and supports both absolute and relative paths, including common relative identifiers like .. for parent directory navigation. The command validates directory existence before changing and updates the Mythic callback with the new working directory.
- Needs Admin: False
- Version: 2
- Author: @djhohnstein
Arguments
- path (String) - Directory path to change to
- CLI Name: Path
- Display Name: Path to Directory
- Description: Directory to change to
Usage
Example 1: Absolute Path Navigation
Command:Example 2: Root Directory Navigation
Command:Example 3: Relative Path Navigation
Command:Example 4: Directory with Spaces
Command:Example 5: Directory Not Found
Command:Example 6: Relative Navigation Examples
Command:Detailed Summary
Thecd function implements a straightforward directory navigation system that manages the agent’s current working directory state:
1. Parameter Processing and Validation
The function uses a simple parameter structure for directory paths:- JSON Deserialization: Uses
CdParametersstructure to extract the target directory path - Path Validation: Checks directory existence using
Directory.Exists()before attempting to change - Error Handling: Returns specific error message if the target directory does not exist
2. Core Implementation
The C# implementation is straightforward and synchronous:- Directory Validation: Uses
Directory.Exists()to verify target directory exists - Directory Change: Uses
Directory.SetCurrentDirectory()to change the working directory - Confirmation: Uses
Directory.GetCurrentDirectory()to get the actual new working directory - Callback Update: Sends
CallbackUpdatemessage to update Mythic with new working directory
3. Working Directory Management
The command affects the global state of the Apollo agent: State Changes:- Process Working Directory: Changes the current working directory for the entire Apollo process
- Global Impact: Affects all subsequent file system operations that use relative paths
- Persistent Change: The directory change persists until another
cdcommand or agent restart - Thread Safety: Uses .NET’s built-in thread-safe directory operations
4. Mythic Integration and Callback Updates
The command integrates with Mythic’s callback management system:- Callback Update: Sends
CallbackUpdatemessage with new current working directory - UI Synchronization: Updates Mythic’s UI to reflect the agent’s new working directory
- State Persistence: Mythic maintains the current directory state for display and reference
- Confirmation Response: Provides both success message and directory confirmation
5. Path Handling and Resolution
The system handles various path formats and types: Supported Path Types:- Absolute Paths: Full paths starting from drive root (e.g.,
C:\Windows\System32) - Relative Paths: Paths relative to current directory (e.g.,
Subdirectory,.\Folder) - Parent Directory: Navigate up using
..(e.g.,..,..\..,..\OtherFolder) - Current Directory: Reference current directory using
.(though rarely needed)
- .NET Path Resolution: Leverages .NET’s built-in path resolution for relative paths
- Drive Changes: Can change to different drives if accessible
- UNC Path Support: Supports UNC paths to network locations (e.g.,
\\server\share) - Long Path Support: Inherits .NET Framework’s path length limitations and support
6. Error Handling and Validation
The implementation includes specific error handling for common scenarios: Error Scenarios:- Directory Not Found: Returns
"Directory {path} does not exist"error message - Access Denied: .NET will throw exceptions for inaccessible directories
- Invalid Paths: .NET handles invalid path format validation
- Network Issues: UNC path failures are handled by underlying .NET exceptions
- Parameter Deserialization: Extracts path from JSON parameters
- Existence Check: Verifies directory exists using
Directory.Exists() - Change Attempt: Uses
Directory.SetCurrentDirectory()for actual change - Confirmation: Gets actual current directory for response
7. Command Line Parsing Flexibility
The Python handler provides flexible path input handling:- Quote Removal: Automatically removes surrounding double or single quotes from paths
- JSON Support: Accepts JSON-formatted parameters
- Error Handling: Provides usage information when no path is provided
- Direct Path Support: Accepts directory path directly without parameter flags
- Space Handling: Properly handles paths with spaces through quote removal
8. Response and Display Management
The command provides clear feedback about directory changes: Success Response Format:- Confirmation Message: Shows the actual directory that was set (may differ from input due to path resolution)
- Error Specificity: Provides specific error message indicating the problematic path
- Status Indicators: Uses appropriate success/error status flags
- Display Parameters: Shows the input path in Mythic’s task display
9. Security and Access Control
The command respects Windows file system security: Security Considerations:- Access Control Lists: Respects Windows ACLs and directory permissions
- User Context: Operates within the security context of the Apollo agent process
- Network Access: Can access network paths if the agent has appropriate credentials
- Privilege Requirements: May require specific privileges for certain system directories
10. Impact on Other Commands
The directory change affects subsequent file system operations: Affected Commands:- File Operations: Commands like
cat,ls,upload,downloaduse relative paths from new directory - Process Creation: Commands that spawn processes inherit the new working directory
- Script Execution: PowerShell and other script executions start from new directory
- Path Resolution: All relative path references resolve from the new working directory
MITRE ATT&CK Mapping
- T1083 - File and Directory Discovery
Technical Deep Dive
.NET Directory Operations
Thecd command leverages .NET Framework’s directory management capabilities:
Directory.SetCurrentDirectory()
- Process-Wide: Changes working directory for entire process, not just current thread
- Exception Handling: Throws exceptions for invalid paths, access denied, etc.
- Path Resolution: Automatically resolves relative paths and normalizes the result
- Thread Safety: Method is thread-safe and atomic
Directory.GetCurrentDirectory()
- Actual Path: Returns the actual resolved path, which may differ from input path
- Normalized Format: Returns path in normalized Windows format
- Drive Information: Includes drive letter and full path information
- Real-Time: Returns current state at time of call
Path Resolution Behavior
The .NET Framework handles various path resolution scenarios:Relative Path Resolution
- Single Dot (.): References current directory (rarely used with cd)
- Double Dot (..): References parent directory
- Subdirectory: Direct subdirectory name resolves from current location
- Complex Relative: Paths like
..\..\Windowsare fully resolved
Drive and UNC Path Handling
- Drive Changes: Can change to different drives (C:, D:, etc.)
- UNC Paths: Supports network paths (\server\share\folder)
- Path Validation: Validates path format before attempting change
- Access Verification: Checks access permissions during change attempt
Error Handling Patterns
The command implements a validation-first approach:Pre-Change Validation
- Early Error Detection: Catches non-existent directories before attempting change
- Cleaner Error Messages: Provides specific error message for directory existence
- Resource Efficiency: Avoids exception handling for common error case
- User Experience: Immediate feedback for invalid directories
Exception Scenarios
While not explicitly shown in the code,Directory.SetCurrentDirectory() can throw:
- DirectoryNotFoundException: Directory path is invalid
- SecurityException: Insufficient permissions to access directory
- ArgumentException: Path contains invalid characters
- PathTooLongException: Path exceeds system limits
Performance Characteristics
Thecd command has minimal performance impact:
Operation Timing
- Fast Execution: Directory operations are typically very fast
- Synchronous: Operation completes before returning (no async overhead)
- Single System Call: Primary operation is one system call to change directory
- Minimal Memory: Very low memory footprint
System Impact
- No File I/O: Only changes process state, doesn’t read/write files
- Registry Access: May involve registry access for drive mapping resolution
- Network Latency: UNC paths may involve network round-trips
- Cache Effects: May affect file system cache behavior for subsequent operations
APIs Used and Their Purposes
| API | Purpose | DLL | Documentation |
|---|---|---|---|
Directory.Exists() | Validates directory existence before changing | mscorlib.dll | Directory.Exists |
Directory.SetCurrentDirectory() | Changes process working directory | mscorlib.dll | Directory.SetCurrentDirectory |
Directory.GetCurrentDirectory() | Gets current working directory path | mscorlib.dll | Directory.GetCurrentDirectory |
JsonSerializer.Deserialize<T>() | Deserializes JSON task parameters | Apollo Agent | Internal Apollo JSON serialization |
Security Considerations
Access Control and Permissions
- Windows ACLs: Respects file system access control lists
- User Context: Limited by the privileges of the Apollo agent process
- Network Authentication: UNC paths require appropriate network credentials
- System Directories: Some directories may require elevated privileges
Operational Security
- Directory Enumeration: Successful directory changes reveal file system structure
- Access Patterns: Directory navigation patterns may indicate reconnaissance activity
- Network Discovery: UNC path usage may reveal network topology
- Privilege Indication: Successful access to restricted directories indicates privilege level
Audit and Detection
- File System Auditing: Directory access may be logged by Windows audit policies
- Process Monitoring: Working directory changes can be monitored by security tools
- Network Access: UNC path access generates network authentication events
- Behavioral Analysis: Navigation patterns may trigger behavioral detection
Defensive Considerations
- Access Monitoring: Monitor for unusual directory access patterns
- Privilege Escalation: Watch for access to typically restricted directories
- Network Indicators: UNC path access from workstations may be suspicious
- Timeline Analysis: Correlate directory changes with other malicious activities
Limitations
- Path Length Limits: Subject to Windows maximum path length restrictions
- Permission Requirements: Cannot access directories without appropriate permissions
- Network Dependencies: UNC paths require network connectivity and credentials
- Process Scope: Only changes working directory for the Apollo agent process
- No Bookmark Support: Cannot save or return to previous directories automatically
- Single Directory: Cannot change to multiple directories simultaneously
- No Directory Stack: No built-in support for directory history or stack operations
Troubleshooting
Common Issues and Solutions
| Issue | Possible Causes | Solutions |
|---|---|---|
| ”Directory does not exist” | Incorrect path, directory deleted | Verify path with ls command, check spelling |
| Access denied errors | Insufficient permissions | Run with elevated privileges or check directory ACLs |
| UNC path failures | Network connectivity, credentials | Verify network access and authentication |
| Path too long errors | Windows path length limits | Use shorter paths or UNC path alternatives |
| Drive not accessible | Unmapped network drives, disconnected media | Verify drive mapping and media connectivity |
Debugging Steps
- Verify Current Directory: Use
pwdto confirm current location - List Available Directories: Use
lsto see accessible directories - Test with Simple Paths: Start with basic absolute paths before trying complex relative paths
- Check Permissions: Verify access to target directory with
lson parent directory - Network Diagnostics: For UNC paths, verify network connectivity and credentials
Best Practices
- Path Quoting: Quote paths containing spaces or special characters
- Absolute Paths: Use absolute paths when unsure about current location
- Incremental Navigation: Navigate step-by-step for complex relative paths
- Permission Awareness: Understand privilege requirements for target directories
- Network Path Testing: Test UNC paths with simple operations before complex tasks