Skip to content

Instantly share code, notes, and snippets.

@Emusp
Last active December 17, 2021 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Emusp/e43e86f90b14477df4a5939ae120ed78 to your computer and use it in GitHub Desktop.
Save Emusp/e43e86f90b14477df4a5939ae120ed78 to your computer and use it in GitHub Desktop.
Prompts to select folder to scan for .jar files with JndiLookup.Class present (Log4j) - Run script as Administrator - Requires .NET 4.5 installed on server
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$FolderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog
$FolderBrowserDialog.RootFolder = 'MyComputer'
$FolderBrowserDialog.Description = 'Select the folder containing the log4j .jar files'
$result = $FolderBrowserDialog.ShowDialog((New-Object System.Windows.Forms.Form -Property @{TopMost = $true }))
If($result -eq "OK") {
$folderToScan = $FolderBrowserDialog.SelectedPath
Write-Host $folderToScan
}else {
Exit
}
if($folderToScan){
[System.IO.Directory]::SetCurrentDirectory($folderToScan)
[Reflection.Assembly]::LoadWithPartialName('System.IO.Compression')
foreach($sourceFile in (Get-ChildItem -Path $folderToScan -File -Filter '*.jar')){
$stream = New-Object IO.FileStream($sourceFile, [IO.FileMode]::Open)
$mode = [IO.Compression.ZipArchiveMode]::Update
$zip = New-Object IO.Compression.ZipArchive($stream, $mode)
($zip.Entries | Where-Object { $_.FullName -like 'org/apache/logging/log4j/core/lookup/JndiLookup.class'}) | ForEach-Object {
$inFile = $sourceFile.FullName
$outFile = $inFile + '.bak'
Write-Host "Found JndiLookup.class in -> $inFile" -ForegroundColor Yellow
Write-host "Copying backup to -> $outFile" -ForegroundColor Magenta
Copy-Item -Path $inFile -Destination $outFile
$_.Delete()
}
$zip.Dispose()
$stream.Close()
$stream.Dispose()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment