100 lines
5.2 KiB
PowerShell
100 lines
5.2 KiB
PowerShell
param(
|
|
[Parameter(Mandatory = $true)][string]$AppDirectory,
|
|
[Parameter(Mandatory = $true)][string]$InstallerPath,
|
|
[Parameter(Mandatory = $true)][string]$ReportPath
|
|
)
|
|
|
|
# File scanning is not a replacement for behavioral analysis or vendor adjudication.
|
|
# This script never changes protection settings, restores files or adds exclusions.
|
|
$ErrorActionPreference = 'Stop'
|
|
$appRoot = [IO.Path]::GetFullPath($AppDirectory)
|
|
$installer = [IO.Path]::GetFullPath($InstallerPath)
|
|
$reportFile = [IO.Path]::GetFullPath($ReportPath)
|
|
if (-not (Test-Path -LiteralPath $appRoot -PathType Container)) { throw 'Application directory missing' }
|
|
if (-not (Test-Path -LiteralPath $installer -PathType Leaf)) { throw 'Installer missing' }
|
|
$mainFiles = @(Get-ChildItem -LiteralPath $appRoot -File -Filter 'ZhenAI-WeCom-Assistant-v*.exe')
|
|
if ($mainFiles.Count -ne 1) { throw 'Expected exactly one main executable' }
|
|
$started = Get-Date
|
|
$report = [ordered]@{
|
|
startedAt = $started.ToString('o')
|
|
appDirectory = $appRoot
|
|
installer = $installer
|
|
scope = 'File scan only; no real protocol send performed by this script'
|
|
status = 'incomplete'
|
|
signatures = @()
|
|
scans = @()
|
|
relatedDetections = @()
|
|
securityReviewRequired = $true
|
|
behaviorValidated = $false
|
|
}
|
|
try {
|
|
$state = Get-MpComputerStatus -ErrorAction Stop
|
|
$report['defender'] = [ordered]@{
|
|
antivirusEnabled = [bool]$state.AntivirusEnabled
|
|
realtimeEnabled = [bool]$state.RealTimeProtectionEnabled
|
|
behaviorMonitoringEnabled = [bool]$state.BehaviorMonitorEnabled
|
|
intelligenceVersion = $state.AntivirusSignatureVersion
|
|
engineVersion = $state.AMEngineVersion
|
|
}
|
|
foreach ($file in @($mainFiles[0].FullName, (Join-Path $appRoot 'ZhenAIUpdater.exe'), $installer)) {
|
|
$signature = Get-AuthenticodeSignature -LiteralPath $file
|
|
$report.signatures += [ordered]@{
|
|
filename = [IO.Path]::GetFileName($file)
|
|
sha256 = (Get-FileHash -LiteralPath $file -Algorithm SHA256).Hash.ToLowerInvariant()
|
|
authenticode = [string]$signature.Status
|
|
}
|
|
}
|
|
if (-not $state.AntivirusEnabled -or -not $state.RealTimeProtectionEnabled -or -not $state.BehaviorMonitorEnabled) {
|
|
throw 'Defender protection is not fully active; cannot record a valid protected-environment scan'
|
|
}
|
|
$platform = Join-Path $env:ProgramData 'Microsoft\Windows Defender\Platform'
|
|
$scanner = Get-ChildItem -LiteralPath $platform -Directory | Sort-Object Name -Descending |
|
|
ForEach-Object { Join-Path $_.FullName 'MpCmdRun.exe' } |
|
|
Where-Object { Test-Path -LiteralPath $_ } | Select-Object -First 1
|
|
if (-not $scanner) { $scanner = Join-Path $env:ProgramFiles 'Windows Defender\MpCmdRun.exe' }
|
|
if (-not (Test-Path -LiteralPath $scanner)) { throw 'Defender scanner not found' }
|
|
foreach ($target in @($appRoot, $installer)) {
|
|
Write-Output ('Scanning with Defender: ' + $target)
|
|
& $scanner -Scan -ScanType 3 -File $target
|
|
$scanExit = $LASTEXITCODE
|
|
$report.scans += [ordered]@{path=$target;exitCode=$scanExit}
|
|
}
|
|
# Include prior versions: a new filename/hash does not resolve a known behavior detection.
|
|
$detections = @(Get-MpThreatDetection -ErrorAction Stop | Where-Object {
|
|
($_.ProcessName -match 'ZhenAI-WeCom-Assistant-v') -or
|
|
(($_.Resources -join "`n") -match 'ZhenAI-WeCom-Assistant-v|ZhenAIUpdater|甄AI客服-安装包')
|
|
})
|
|
$threatNames = @{}
|
|
foreach ($threat in @(Get-MpThreat -ErrorAction Stop)) { $threatNames[[string]$threat.ThreatID] = $threat.ThreatName }
|
|
foreach ($detection in $detections) {
|
|
$report.relatedDetections += [ordered]@{
|
|
threatId = $detection.ThreatID
|
|
threatName = $threatNames[[string]$detection.ThreatID]
|
|
firstDetectedAt = $detection.InitialDetectionTime.ToString('o')
|
|
actionSucceeded = $detection.ActionSuccess
|
|
executable = [IO.Path]::GetFileName($detection.ProcessName)
|
|
detectedDuringThisScan = ($detection.LastThreatStatusChangeTime -ge $started)
|
|
}
|
|
}
|
|
$scanFailed = @($report.scans | Where-Object { $_.exitCode -ne 0 }).Count -gt 0
|
|
$newDetection = @($report.relatedDetections | Where-Object { $_.detectedDuringThisScan }).Count -gt 0
|
|
$report['fileScanPassed'] = -not $scanFailed -and -not $newDetection -and
|
|
(Test-Path -LiteralPath $mainFiles[0].FullName) -and (Test-Path -LiteralPath $installer)
|
|
$report.status = if ($report.fileScanPassed) { 'file_scan_completed_behavior_review_pending' } else { 'scan_failed_or_threat_detected' }
|
|
}
|
|
catch {
|
|
$report['error'] = $_.Exception.Message
|
|
$report.status = 'scan_incomplete'
|
|
}
|
|
finally {
|
|
$report['finishedAt'] = (Get-Date).ToString('o')
|
|
$directory = Split-Path -Parent $reportFile
|
|
New-Item -ItemType Directory -Path $directory -Force | Out-Null
|
|
[IO.File]::WriteAllText($reportFile, ($report | ConvertTo-Json -Depth 8), [Text.UTF8Encoding]::new($false))
|
|
Write-Output ('Security report: ' + $reportFile)
|
|
Write-Output ('Status: ' + $report.status)
|
|
}
|
|
if ($report.status -eq 'scan_incomplete' -or -not $report.fileScanPassed) { exit 1 }
|
|
# Exit 2 means file scanning completed, but behavior/vendor review is still required.
|
|
exit 2
|