Skip to content

Commit 0e2e800

Browse files
authored
Merge pull request #2459 from DKhrebin/DKhrebin-amsi-detection
Improve AMSI detection in Test-ExchAVExclusions
2 parents b4d7547 + cb26178 commit 0e2e800

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

Diagnostics/AVTester/Test-ExchAVExclusions.ps1

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,7 @@ Write-Host "Access EICAR Files Finished"
313313
$currentDiff = $initialDiff
314314
$firstExecution = $true
315315
$SuspiciousProcessList = New-Object Collections.Generic.List[string]
316-
$SuspiciousW3wpProcessList = New-Object Collections.Generic.List[string]
317-
$SuspiciousAMSIinW3wpProcessList = New-Object Collections.Generic.List[string]
316+
$SuspiciousAMSIProcessList = New-Object Collections.Generic.List[string]
318317

319318
# Get AMSI Dlls registered
320319
# Define the AMSI providers registry path
@@ -334,7 +333,7 @@ if ($subKeys) {
334333
foreach ($m in $Matches.Values) {
335334
$foundDll = (Get-Item "HKLM:\SOFTWARE\Classes\ClSid\{$m}\InprocServer32" -ErrorAction SilentlyContinue).GetValue("").trim('"')
336335
if ($null -eq $foundDll) {
337-
Write-Host "No AMSI Dlls was found for $m, possible AMSI misconfiguration" -ForegroundColor Red
336+
Write-Host "No AMSI Dlls were found for $m, possible AMSI misconfiguration" -ForegroundColor Red
338337
} else {
339338
Write-Verbose "AMSI $m was found"
340339
$AMSIDll.Add($foundDll)
@@ -487,7 +486,7 @@ while ($currentDiff -gt 0) {
487486

488487
# Remove Microsoft modules
489488
Write-Verbose "Removing Microsoft Modules"
490-
$ProcessModules = $ProcessModules | Where-Object { $_.FileVersionInfo.CompanyName -notin $CompanyNameAllowList }
489+
$ProcessModules = $ProcessModules | Where-Object { ($_.FileVersionInfo.CompanyName -notin $CompanyNameAllowList) -or ($_.FileName -like "*Windows Defender*") }
491490

492491
# Remove Oracle modules on FIPS
493492
Write-Verbose "Removing Oracle Modules"
@@ -500,17 +499,33 @@ while ($currentDiff -gt 0) {
500499
}
501500

502501
if ($ProcessModules.count -gt 0) {
502+
$hasAmsiDll = $process.modules | Where-Object { $_.ModuleName -like "amsi.dll" }
503503
foreach ($module in $ProcessModules) {
504504
$OutString = ("PROCESS: $($process.ProcessName) PID($($process.Id)) UNEXPECTED MODULE: $($module.ModuleName) COMPANY: $($module.Company)`n`tPATH: $($module.FileName)`n`tFileVersion: $($module.FileVersion)")
505-
if ($process.MainModule.ModuleName -eq "W3wp.exe") {
506-
if ($AMSIDll -contains $module.FileName) {
507-
$OutString = ("PROCESS: $($process.ProcessName) PID($($process.Id)) MODULE: $($module.ModuleName) COMPANY: $($module.Company)`n`tPATH: $($module.FileName)`n`tFileVersion: $($module.FileVersion)")
508-
Write-Host "[WARNING] - AMSI DLL Detected: $OutString" -ForegroundColor Yellow
509-
$SuspiciousAMSIinW3wpProcessList += $OutString
510-
} else {
511-
Write-Host "[FAIL] - $OutString" -ForegroundColor Red
512-
$SuspiciousW3wpProcessList += $OutString
505+
# If there is amsi.dll in the process and the module name matches any AMSI provider DLL name, it is the AMSI provider
506+
$isAmsiProvider = $false
507+
if ($hasAmsiDll) {
508+
foreach ($amsiDll in $AMSIDll) {
509+
if ([System.IO.Path]::GetFileName($module.FileName) -eq [System.IO.Path]::GetFileName($amsiDll)) {
510+
$isAmsiProvider = $true
511+
break
512+
}
513513
}
514+
# Additionally check for known Windows Defender AMSI-related modules by filename
515+
# This handles version mismatches where MPCLIENT.DLL path was constructed from registry
516+
if (-not $isAmsiProvider) {
517+
$moduleFileName = [System.IO.Path]::GetFileName($module.FileName)
518+
# cSpell:ignore MPCLIENT MpOav
519+
$knownDefenderModules = @("MPCLIENT.DLL", "MpOav.dll")
520+
if ($knownDefenderModules -contains $moduleFileName) {
521+
$isAmsiProvider = $true
522+
}
523+
}
524+
}
525+
if ($isAmsiProvider) {
526+
$OutString = ("PROCESS: $($process.ProcessName) PID($($process.Id)) MODULE: $($module.ModuleName) COMPANY: $($module.Company)`n`tPATH: $($module.FileName)`n`tFileVersion: $($module.FileVersion)")
527+
Write-Host "[WARNING] - AMSI Provider Detected: $OutString" -ForegroundColor Yellow
528+
$SuspiciousAMSIProcessList += $OutString
514529
} else {
515530
Write-Host "[FAIL] - $OutString" -ForegroundColor Red
516531
$SuspiciousProcessList += $OutString
@@ -595,7 +610,7 @@ $OutputPath = Join-Path $PSScriptRoot BadExclusions-$StartDateFormatted.txt
595610
"###########################################################################################" | Out-File $OutputPath -Append
596611

597612
# Report what we found
598-
if ($BadFolderList.count -gt 0 -or $BadExtensionList.Count -gt 0 -or $SuspiciousProcessList.count -gt 0 -or $SuspiciousW3wpProcessList.count -gt 0 -or $SuspiciousAMSIinW3wpProcessList.count -gt 0) {
613+
if ($BadFolderList.count -gt 0 -or $BadExtensionList.Count -gt 0 -or $SuspiciousProcessList.count -gt 0 -or $SuspiciousAMSIProcessList.count -gt 0) {
599614

600615
Write-Host "Possible AV Scanning found" -ForegroundColor Red
601616
if ($BadFolderList.count -gt 0 ) {
@@ -613,21 +628,13 @@ if ($BadFolderList.count -gt 0 -or $BadExtensionList.Count -gt 0 -or $Suspicious
613628
$SuspiciousProcessList | Out-File $OutputPath -Append
614629
Write-Warning ("Found $($SuspiciousProcessList.count) UnExpected modules loaded into Exchange Processes ")
615630
}
616-
if ($SuspiciousW3wpProcessList.count -gt 0 ) {
617-
$SuspiciousW3wpProcessListString = "`nW3wp.exe is not present in the recommended Exclusion list but we found 3rd Party modules on it and could affect Exchange performance or functionality."
618-
$SuspiciousW3wpProcessListString | Out-File $OutputPath -Append
619-
Write-Warning $SuspiciousW3wpProcessListString
620-
"`n[Non-Default Modules Loaded on W3wp.exe]" | Out-File $OutputPath -Append
621-
$SuspiciousW3wpProcessList | Out-File $OutputPath -Append
622-
Write-Warning ("Found $($SuspiciousW3wpProcessList.count) UnExpected modules loaded into W3wp.exe ")
623-
}
624-
if ($SuspiciousAMSIinW3wpProcessList.count -gt 0) {
625-
$SuspiciousAMSIinW3wpProcessListString = "`nFound AMSI modules in w3wp processes`nThat may impact Exchange performance and Outlook connectivity in some scenarios.`nThese modules are not necessarily anomalies, but we recommend checking the following articles: `n`thttps://learn.microsoft.com/en-us/exchange/antispam-and-antimalware/amsi-integration-with-exchange `n`thttps://aka.ms/Test-AMSI"
626-
$SuspiciousAMSIinW3wpProcessListString | Out-File $OutputPath -Append
627-
Write-Warning $SuspiciousAMSIinW3wpProcessListString
628-
"`n[AMSI Modules Loaded on W3wp.exe]" | Out-File $OutputPath -Append
629-
$SuspiciousAMSIinW3wpProcessList | Out-File $OutputPath -Append
630-
Write-Warning ("Found $($SuspiciousAMSIinW3wpProcessList.count) AMSI modules loaded into W3wp.exe ")
631+
if ($SuspiciousAMSIProcessList.count -gt 0 ) {
632+
$SuspiciousAMSIProcessListString = "`nAMSI.dll detected in Exchange processes with AMSI providers loaded. This may indicate antivirus integration that could affect Exchange performance."
633+
$SuspiciousAMSIProcessListString | Out-File $OutputPath -Append
634+
Write-Warning $SuspiciousAMSIProcessListString
635+
"`n[AMSI Provider Modules Detected in Exchange Processes]" | Out-File $OutputPath -Append
636+
$SuspiciousAMSIProcessList | Out-File $OutputPath -Append
637+
Write-Warning ("Found $($SuspiciousAMSIProcessList.count) AMSI provider modules loaded into Exchange processes ")
631638
}
632639
Write-Warning ("Review " + $OutputPath + " For the full list.")
633640
} else {

Shared/Get-ExchAVExclusions.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ function Get-ExchAVExclusionsProcess {
311311
$ProcessList.Add((Join-Path $ExchangePath 'FIP-FS\Bin\fms.exe'))
312312
$ProcessList.Add((Join-Path $ExchangePath 'Bin\Search\Ceres\HostController\hostcontrollerservice.exe'))
313313
$ProcessList.Add((Join-Path $env:SystemRoot '\System32\inetSrv\inetInfo.exe'))
314+
$ProcessList.Add((Join-Path $env:SystemRoot '\System32\inetSrv\w3wp.exe'))
314315
$ProcessList.Add((Join-Path $ExchangePath 'Bin\Microsoft.Exchange.Directory.TopologyService.exe'))
315316
$ProcessList.Add((Join-Path $ExchangePath 'Bin\Microsoft.Exchange.EdgeSyncSvc.exe'))
316317
$ProcessList.Add((Join-Path $ExchangePath 'FrontEnd\PopImap\Microsoft.Exchange.Imap4.exe'))

0 commit comments

Comments
 (0)