Ryan2065 / Log4ShellDetection

MIT License
5 stars 1 forks source link

Log4ShellDetection

Yet another Log4Shell detection script

This is written in PowerShell, targets Windows, and is meant to find Log4Shell vulnerabilities wherever they may be

CVEs detected

How I got the CVE information

I wrote a script to scrape Maven (site where Log4J is hosted) Log4J versions and compile which ones were vulnerable to which CVEs as direct vulnerabilities or indrect vulnerabilities. This script also downloaded those Log4J versions and looked for the problem class files socketnode.class, jdnimanager.class, or JndiLookup.class. A SHA256 hash of each file was taken and saved for use in the detection script. With those hashes, we can scan any jar file and know if it has vulnerable code associated with one of the Log4Shell CVEs.

CVE Rules

How do I use it?

The script is compiled into a single file for easy portability. Download the latest version and run with it!

You can also get it from the gallery:

Install-Script -Name Log4ShellDetectionScript

Once downloaded, just run it with:

$results = . .\Log4ShellDetectionScript.ps1 -OutputType "Objects"

OutputType can be changed based on how it's running - see notes below.

If you want to pick the CVEs it searches for, run it with:

$results = . .\Log4ShellDetectionScript.ps1 -OutputType "Objects" -CVEsToDetect @("CVE-2021-4104")

The above will only search for CVE-2021-4104 instead of all 4 CVEs.

Why this script?

There are a number of other PowerShell scripts out there, but they have a number of gaps. I stumbled upon a utility written in Go that I thought had a great methodoligy. It opens up the Jar files (jar files are just archives) and looks at the hash of the problem .class files. If the hash matches a vulnerable hash, it's marked as detected.

I looked at numerous PowerShell solutions, but they all had some issues as time went on. First off, most PowerShell scripts only look for log4shell in the filename, which misses a whole host of problems. There are some others that will open the jar files and look for the problem .class files, but they just look for them by name. Newer version of log4j that are patched will show up as vulnerable from these scripts also.

So I wrote this script to hopefully get some good in-dept detection on Windows.

Special thanks

1) Robocopy Search: I had no clue this was possible until I saw it in Jordan Benzing's script to search for Log4Shell. 2) Hillu Log4j vulnerability Scanner 3) Everyone else who has shared a script to find these vulnerabilities 4) Reddit / Twitter communities

How does it work?

1) Searches all drives for .jar .war *.ear files

I want to look into the source code

If you want to browse the source code, make edits, submit changes, anything, great! The source code is in the folder .\src\Detection.

I did not think it'd be fun to author a huge .ps1 file, so it's broken up into multiple files in the .\src folder. Once changes are made, simply run the script .\src\BuildLog4ShellSingleFile.ps1 and it will build the single file.

The only other script in this repo that's not obvious is Search-DownloadedJars.ps1. I wrote this file to scrape Maven (where log4j is stored) for all the vulnerable .jar files. Once downloaded, the script opens them up and gathers identifying information. It'll compile all the data as JSON and then it just needs to be pasted into Get-Log4ShellIdentifiers.ps1 where the existing json is.

I have an issue!

Great, glad someone's using the script. Post the issue in GitHub and I'll take a look when I can.

Known issues

1) One known issue that I came across when testing - If there's only a match on .class files and the script can't find the version in the manifest, it will report back as multiple versions. So if you see a .jar and the version reporeted of log4j is a comma separated list, that's why. It found a class that's vulnerable, and is associated with one of those versions, but was not able to narrow it down. It should only ever return 2 or 3 versions this way if there's a match.

Versions