
Detection scripts for CVE-2023-50164 in Apache Struts2, providing PowerShell and Bash tools to scan for vulnerable versions across file systems and archives.
For PowerShell 5.1 and newer. Written by Kjetil Sigvartsen at Norsk helsenett SF.
[String[]]$Extensions = @('*.jar', '*.war', '*.ear')
[string]$searchString = 'struts2-core'
foreach ($Disk in (Get-CimInstance Win32_LogicalDisk)) {
[string]$DriveLetter = $Disk.DeviceID
[string]$Path = "$($driveLetter)\"
foreach ($ChildItem in (Get-ChildItem -Path $Path -Recurse -Include $Extensions -File -ErrorAction SilentlyContinue)) {
[String]$FilePath = $ChildItem.FullName
$Content = Get-Content -Path $filePath -Raw
if ($Content -like "*$searchString*") {
Write-Output $filePath
} #if
} #foreach
} #foreach
For PowerShell 5.1 and newer. Written by Kjetil Sigvartsen at Norsk helsenett SF.
This one can be somewhat more CPU-intensive, but will be significantly faster than the variant above. Note that C:\Windows is also filtered out for speed reasons.
If more detailed output is desired, add the following at the top of the script:
$VerbosePreference = 'Continue'
The code is otherwise as follows:
[String[]]$Extensions = @('*.jar', '*.war', '*.ear')
[string]$searchString = 'struts2-core'
[string[]]$Exceptions = @('C:\Windows')
foreach ($Disk in (Get-CimInstance Win32_LogicalDisk)) {
[string]$DriveLetter = $Disk.DeviceID
[string]$Path = "$($driveLetter)\"
Write-Verbose -Message "Working on $Path"
try {
[System.IO.DirectoryInfo[]]$Folders = Get-ChildItem -Path $Path -Directory -ErrorAction Stop
} #try
catch {
Write-Verbose -Message "Unable to get child folders in disk $Path"
continue
} #catch
[System.Management.Automation.Job[]]$Jobs = $Null
[System.Management.Automation.Job[]]$Jobs = foreach ($Folder in $Folders) {
[string]$JobName = $Path + $Folder.Name
if ($Exceptions -contains $JobName) {
Write-Verbose -Message "Skipping $JobName, in exception list"
continue
} #if
Write-Verbose -Message "Starting jobs for $JobName"
Start-Job -Name $JobName -ScriptBlock {
Return (Get-ChildItem -Path $Using:JobName -Recurse -Include $Using:Extensions -File -ErrorAction SilentlyContinue)
} #Start-Job
} #Foreach
[System.Object[]]$JobResults = $Null
[System.Object[]]$JobResults = Receive-Job -Job $Jobs -AutoRemoveJob -Wait -ErrorAction Stop
[System.Management.Automation.Job[]]$RemainingJobs = $Null
[System.Management.Automation.Job[]]$RemainingJobs = get-Job -Name "$Path*" -ErrorAction Stop
if ($RemainingJobs) {
Write-Verbose -Message "$($RemainingJobs.count) jobs remaining"
} #if
foreach ($ChildItem in $JobResults) {
[String]$FilePath = $ChildItem.FullName
[string]$Content = Get-Content -Path $filePath -Raw
if ($Content -like "*$searchString*" -or $FilePath -like "*$searchString*") {
Write-Output $filePath
} #if
} #foreach
} #foreach
sudo find / -type f \( -iname "*.jar" -o -iname "*.war" -o -iname "*.ear" \) -exec grep -Fl "struts2-core" {} 2>/dev/null \;
The scripts will list files that are, or contain, the struts2 core library. A couple of examples:
/path/to/folder/struts2-core-6.3.0.2.jar - here the struts2 core library lies directly on the filesystem, and the version is 6.3.0.2, where the vulnerability is fixed.
/path/to/folder/apps/struts2-showcase-6.3.0.2.war - here the struts2 core library is inside the .war file, where the contents must be listed to see which version of Struts2 is included:
$ unzip -l /path/to/folder/apps/struts2-showcase-6.3.0.2.war | fgrep struts2-core
1519992 2023-12-05 05:58 WEB-INF/lib/struts2-core-6.3.0.2.jar
For PowerShell 5.1 and newer. Written by Kjetil Sigvartsen at Norsk helsenett SF.
[string[]]$ZipFiles = @(
'C:\path\to\folder\apps\struts2-showcase-6.3.0.2.war'
)
Add-Type -AssemblyName System.IO.Compression.FileSystem
foreach ($ZipFile in $ZipFiles) {
foreach ($Entry in ([System.IO.Compression.ZipFile]::OpenRead($zipFile).Entries)) {
if ($Entry.FullName -like "*struts2-core*") {
Write-Output $Entry.FullName
} #if
} #foreach
} #foreach