|
| 1 | +# Attack Surface Analyzer Testing |
| 2 | + |
| 3 | +This directory contains tools for running Attack Surface Analyzer (ASA) tests on PowerShell MSI installations using Docker. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Attack Surface Analyzer is a Microsoft tool that helps analyze changes to a system's attack surface. These scripts allow you to run ASA tests locally in a clean Windows container to analyze what changes when PowerShell is installed. |
| 8 | + |
| 9 | +## Files |
| 10 | + |
| 11 | +- **Run-AttackSurfaceAnalyzer.ps1** - PowerShell script to run ASA tests with official MSIs |
| 12 | +- **Summarize-AsaResults.ps1** - PowerShell script to analyze and summarize ASA results |
| 13 | +- **docker/Dockerfile** - Multi-stage Dockerfile for building a container image with ASA pre-installed |
| 14 | +- **README.md** - This documentation file |
| 15 | + |
| 16 | +## Docker Architecture |
| 17 | + |
| 18 | +The Docker implementation uses a multi-stage build to optimize the testing and result extraction process: |
| 19 | + |
| 20 | +### Multi-Stage Build Stages |
| 21 | + |
| 22 | +1. **asa-runner**: Main execution environment |
| 23 | + - Base: `mcr.microsoft.com/dotnet/sdk:9.0-windowsservercore-ltsc2022` |
| 24 | + - Contains Attack Surface Analyzer CLI tools |
| 25 | + - Runs the complete test workflow |
| 26 | + - Generates reports in both `C:\work` and `C:\reports` directories |
| 27 | + |
| 28 | +1. **asa-reports**: Minimal results layer |
| 29 | + - Base: `mcr.microsoft.com/windows/nanoserver:ltsc2022` |
| 30 | + - Contains only the test reports from the runner stage |
| 31 | + - Enables clean extraction of results without container internals |
| 32 | + |
| 33 | +1. **final**: Default stage (inherits from asa-runner) |
| 34 | + - Provides backward compatibility |
| 35 | + - Used when no specific build target is specified |
| 36 | + |
| 37 | +### Benefits |
| 38 | + |
| 39 | +- **Clean Result Extraction**: Reports are isolated in a dedicated layer |
| 40 | +- **Efficient Transfer**: Only test results are copied, not the entire container filesystem |
| 41 | +- **Fallback Support**: Script includes fallback to volume-based extraction if needed |
| 42 | +- **Minimal Footprint**: Final results layer contains only the necessary output files |
| 43 | + |
| 44 | +## Prerequisites |
| 45 | + |
| 46 | +- Windows 10/11 or Windows Server |
| 47 | +- Docker Desktop with Windows containers enabled |
| 48 | +- PowerShell 5.1 or later |
| 49 | +- **An official signed PowerShell MSI file** from a released build |
| 50 | + |
| 51 | +### MSI Requirements |
| 52 | + |
| 53 | +**Important:** This tool now requires an official, digitally signed PowerShell MSI from Microsoft releases: |
| 54 | + |
| 55 | +- **Must be signed** by Microsoft Corporation |
| 56 | +- **Must be from an official release** (downloaded from [PowerShell Releases](https://github.com/PowerShell/PowerShell/releases)) |
| 57 | +- **Local builds are not supported** - unsigned or development MSIs will be rejected |
| 58 | +- The script automatically verifies the digital signature before proceeding |
| 59 | + |
| 60 | +**Where to get official MSIs:** |
| 61 | + |
| 62 | +- Download from: https://github.com/PowerShell/PowerShell/releases |
| 63 | +- Look for files like: `PowerShell-7.x.x-win-x64.msi` |
| 64 | + |
| 65 | +## Quick Start |
| 66 | + |
| 67 | +### Option 1: Using the PowerShell Script (Recommended) |
| 68 | + |
| 69 | +The script requires an official signed PowerShell MSI file: |
| 70 | + |
| 71 | +```powershell |
| 72 | +# Run ASA test with official MSI (MsiPath is required) |
| 73 | +.\tools\AttackSurfaceAnalyzer\Run-AttackSurfaceAnalyzer.ps1 -MsiPath "C:\path\to\PowerShell-7.4.0-win-x64.msi" |
| 74 | +
|
| 75 | +# Specify custom output directory for results |
| 76 | +.\tools\AttackSurfaceAnalyzer\Run-AttackSurfaceAnalyzer.ps1 -MsiPath ".\PowerShell-7.4.0-win-x64.msi" -OutputPath "C:\asa-results" |
| 77 | +
|
| 78 | +# Keep the temporary work directory for debugging |
| 79 | +.\tools\AttackSurfaceAnalyzer\Run-AttackSurfaceAnalyzer.ps1 -MsiPath ".\PowerShell-7.4.0-win-x64.msi" -KeepWorkDirectory |
| 80 | +``` |
| 81 | + |
| 82 | +The script will: |
| 83 | + |
| 84 | +1. **Verify MSI signature** - Ensures the MSI is officially signed by Microsoft Corporation |
| 85 | +1. Create a temporary work directory |
| 86 | +1. Build a custom Docker container from the static Dockerfile |
| 87 | +1. Start the Windows container with Attack Surface Analyzer |
| 88 | +1. Take a baseline snapshot |
| 89 | +1. Install the PowerShell MSI |
| 90 | +1. Take a post-installation snapshot |
| 91 | +1. Export comparison results |
| 92 | +1. Copy results back to your specified output directory |
| 93 | + |
| 94 | +**Security Note:** The script will reject any MSI that is not digitally signed by Microsoft Corporation to ensure analysis is performed only on official releases. |
| 95 | + |
| 96 | +### Option 2: Using the Dockerfile |
| 97 | + |
| 98 | +If you prefer to build and use the container image directly: |
| 99 | + |
| 100 | +```powershell |
| 101 | +# Build the Docker image (Dockerfile is in docker subfolder with clean context) |
| 102 | +docker build -f tools\AttackSurfaceAnalyzer\docker\Dockerfile -t powershell-asa-test tools\AttackSurfaceAnalyzer\docker\ |
| 103 | +
|
| 104 | +# Run the container with your MSI (script is built into the container) |
| 105 | +docker run --rm --isolation process ` |
| 106 | + -v "C:\path\to\msi\directory:C:\work" ` |
| 107 | + powershell-asa-test |
| 108 | +``` |
| 109 | + |
| 110 | +## Output Files |
| 111 | + |
| 112 | +The test will generate output files in the `./asa-results/` directory (or your specified `-OutputPath`): |
| 113 | + |
| 114 | +- **`asa.sqlite`** - SQLite database with full analysis data (primary result file) |
| 115 | +- **`install.log`** - MSI installation log file |
| 116 | +- **`*_summary.json.txt`** - Summary of detected changes (if generated) |
| 117 | +- **`*_results.json.txt`** - Detailed results in JSON format (if generated) |
| 118 | +- **`*.sarif`** - SARIF format results (if generated, can be viewed in VS Code) |
| 119 | + |
| 120 | +## Analyzing Results |
| 121 | + |
| 122 | +### Using the Summary Script (Recommended) |
| 123 | + |
| 124 | +Use the included summary script to get a comprehensive analysis: |
| 125 | + |
| 126 | +```powershell |
| 127 | +# Basic summary of ASA results |
| 128 | +.\tools\AttackSurfaceAnalyzer\Summarize-AsaResults.ps1 |
| 129 | +
|
| 130 | +# Detailed analysis with rule breakdowns |
| 131 | +.\tools\AttackSurfaceAnalyzer\Summarize-AsaResults.ps1 -ShowDetails |
| 132 | +
|
| 133 | +# Analyze results from a specific location |
| 134 | +.\tools\AttackSurfaceAnalyzer\Summarize-AsaResults.ps1 -Path "C:\custom\path\asa-results.json" -ShowDetails |
| 135 | +``` |
| 136 | + |
| 137 | +The summary script provides: |
| 138 | + |
| 139 | +- **Overall statistics** - Total findings, analysis levels, category breakdowns |
| 140 | +- **Rule analysis** - Which security rules were triggered and how often |
| 141 | +- **File analysis** - Detailed breakdown of file-related security issues by rule type |
| 142 | +- **Category cross-reference** - Shows which rules affect which categories |
| 143 | + |
| 144 | +### Using VS Code |
| 145 | + |
| 146 | +The SARIF files can be opened directly in VS Code with the SARIF Viewer extension to see a formatted view of the findings. |
| 147 | + |
| 148 | +### Using PowerShell |
| 149 | + |
| 150 | +```powershell |
| 151 | +# Read the JSON results directly |
| 152 | +$results = Get-Content "asa-results\asa-results.json" | ConvertFrom-Json |
| 153 | +$results.Results.FILE_CREATED.Count # Number of files created |
| 154 | +
|
| 155 | +# Query the SQLite database (requires SQLite tools) |
| 156 | +# Example: List all file changes |
| 157 | +# sqlite3 asa.sqlite "SELECT * FROM file_system WHERE change_type != 'NONE'" |
| 158 | +``` |
| 159 | + |
| 160 | +## Troubleshooting |
| 161 | + |
| 162 | +### Docker Not Available |
| 163 | + |
| 164 | +The script automatically handles Docker Desktop installation and startup: |
| 165 | + |
| 166 | +**If Docker Desktop is installed but not running:** |
| 167 | + |
| 168 | +- The script will automatically start Docker Desktop for you |
| 169 | +- It waits up to 60 seconds for Docker to become available |
| 170 | +- You'll be prompted for confirmation (supports `-Confirm` and `-WhatIf`) |
| 171 | + |
| 172 | +**If Docker Desktop is not installed:** |
| 173 | + |
| 174 | +- The script will prompt you to install it automatically using winget |
| 175 | +- After installation completes, start Docker Desktop and run the script again |
| 176 | + |
| 177 | +**Manual Installation:** |
| 178 | + |
| 179 | +1. Install Docker Desktop from https://www.docker.com/products/docker-desktop |
| 180 | +1. Ensure Docker is running |
| 181 | +1. Switch to Windows containers (right-click Docker tray icon → "Switch to Windows containers") |
| 182 | + |
| 183 | +### Container Fails to Start |
| 184 | + |
| 185 | +- Ensure you have enough disk space (containers can be large) |
| 186 | +- Check that Windows containers are enabled in Docker settings |
| 187 | +- Try pulling the base image manually: `docker pull mcr.microsoft.com/dotnet/sdk:9.0-windowsservercore-ltsc2022` |
| 188 | + |
| 189 | +### MSI Signature Verification Fails |
| 190 | + |
| 191 | +If you get signature verification errors: |
| 192 | + |
| 193 | +- **Ensure you're using an official MSI** from [PowerShell Releases](https://github.com/PowerShell/PowerShell/releases) |
| 194 | +- **Do not use local builds** - only signed release MSIs are supported |
| 195 | +- **Check certificate validity** - very old MSIs may have expired certificates |
| 196 | +- **Verify file integrity** - redownload the MSI if it may be corrupted |
| 197 | + |
| 198 | +### No Results Generated |
| 199 | + |
| 200 | +- Check the install.log file for MSI installation errors |
| 201 | +- Run with `-KeepWorkDirectory` to inspect the temporary work directory |
| 202 | +- Verify the MSI file is valid and not corrupted |
| 203 | + |
| 204 | +## Advanced Usage |
| 205 | + |
| 206 | +### Parameters |
| 207 | + |
| 208 | +The `Run-AttackSurfaceAnalyzer.ps1` script supports these parameters: |
| 209 | + |
| 210 | +- **`-MsiPath`** (Required) - Path to the official signed PowerShell MSI file |
| 211 | +- **`-OutputPath`** (Optional) - Directory for results (defaults to `./asa-results`) |
| 212 | +- **`-ContainerImage`** (Optional) - Custom container base image |
| 213 | +- **`-KeepWorkDirectory`** (Optional) - Keep temp directory for debugging |
| 214 | + |
| 215 | +Example with custom container image: |
| 216 | + |
| 217 | +```powershell |
| 218 | +.\tools\AttackSurfaceAnalyzer\Run-AttackSurfaceAnalyzer.ps1 ` |
| 219 | + -MsiPath ".\PowerShell-7.4.0-win-x64.msi" ` |
| 220 | + -ContainerImage "mcr.microsoft.com/dotnet/sdk:8.0-windowsservercore-ltsc2022" |
| 221 | +``` |
| 222 | + |
| 223 | +### Debugging |
| 224 | + |
| 225 | +To debug issues, keep the work directory and examine the files: |
| 226 | + |
| 227 | +```powershell |
| 228 | +.\tools\AttackSurfaceAnalyzer\Run-AttackSurfaceAnalyzer.ps1 -KeepWorkDirectory |
| 229 | +
|
| 230 | +# The script will print the work directory path |
| 231 | +# You can then examine: |
| 232 | +# - run-asa.ps1 - The script that runs in the container |
| 233 | +# - install.log - MSI installation log |
| 234 | +# - Any other generated files |
| 235 | +``` |
| 236 | + |
| 237 | +## Integration with CI/CD |
| 238 | + |
| 239 | +These tools were extracted from the GitHub Actions workflow to allow local testing. If you need to integrate ASA testing back into a CI/CD pipeline, you can: |
| 240 | + |
| 241 | +1. Use the PowerShell script directly in your pipeline |
| 242 | +1. Build and push the Docker image to a registry |
| 243 | +1. Use the Dockerfile as a base for custom testing scenarios |
| 244 | + |
| 245 | +## More Information |
| 246 | + |
| 247 | +- [Attack Surface Analyzer on GitHub](https://github.com/microsoft/AttackSurfaceAnalyzer) |
| 248 | +- [Docker for Windows Documentation](https://docs.docker.com/desktop/windows/) |
| 249 | +- [SARIF Documentation](https://sarifweb.azurewebsites.net/) |
0 commit comments