forked from Maelstromage/Log4jSherlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext4Sherlock.ps1
310 lines (268 loc) · 14.7 KB
/
text4Sherlock.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
$code={
param($global:remediation = 'Search Only')
Add-Type -AssemblyName System.IO.Compression
Add-Type -AssemblyName System.IO.Compression.FileSystem
# Config
$filetypes = @('*.JAR','*.WAR','*.EAR','*.JPI','*.HPI')
#Init
$global:Errors = @()
$global:vulnerabilityresults = @()
$global:debuglog = @()
$global:csv = @()
# CVEs Scanned for
# CVE-2022-42889 Apache commons-text CVSSScore 9.8 CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H
function Check-Version{
param($version,$hasCommons)
if($version -ne $null){
$parsedVer = $version.replace('version=','').split('.')
}else{
$parsedVer = (0,0,0)
}
$CVE = 'CVE-2022-42889'
$CVSSScore = '9.8'
$FixedVersion = $false
if($hasCommons -eq $false){$CVE = $null; $CVSSScore = $null; $FixedVersion = $false}
if($version -eq 'version=1.5.0'){$CVE = 'CVE-2022-42889'; $CVSSScore = '9.8'; $FixedVersion = $false}
if($version -eq 'version=1.6.0'){$CVE = 'CVE-2022-42889'; $CVSSScore = '9.8'; $FixedVersion = $false}
if($version -eq 'version=1.7.0'){$CVE = 'CVE-2022-42889'; $CVSSScore = '9.8'; $FixedVersion = $false}
if($version -eq 'version=1.8.0'){$CVE = 'CVE-2022-42889'; $CVSSScore = '9.8'; $FixedVersion = $false}
if($version -eq 'version=1.9.0'){$CVE = 'CVE-2022-42889'; $CVSSScore = '9.8'; $FixedVersion = $false}
if($parsedVer[0] -gt '2'){$CVE = $null; $CVSSScore = $null; $FixedVersion = $true}
if($parsedVer[0] -eq '2' -and $parsedVer[1] -gt '16'){$CVE = $null; $CVSSScore = $null; $FixedVersion = $true}
#if($version -eq 'version=2.17.0'){$CVE = $null; $CVSSScore = $null; $FixedVersion = $true}
$return = @{CVE = $CVE; CVSSScore = $CVSSScore; fixedversion = $fixedversion}
return $return
}
function write-console{
param($CVE,$path,$version)
$color = 'magenta'
write-host "┌[$CVE] Version: $version" -ForegroundColor $color
write-host "└─[" -ForegroundColor $color -NoNewline
write-host " Located: $path"
$global:vulnerabilityresults += "┌[$CVE] Version: $version`r`n└─[ Located: $path"
}
function remove-file{
if($global:remediation -eq 'Search Only'){return 'Search Only mode: not remediated'}
if($global:remediation -eq 'Remove JNDILookup.class'){return 'JNDILookup.class removed'}
if($global:remediation -eq 'Remove Java file'){return 'Java file removed'}
return $remresult
}
function Scan-File{
param($path)
$path = $path.fullname
$hasCommons = $false
try{
$nestedfiles = ([System.IO.Compression.ZipArchive]([System.IO.Compression.ZipFile]::OpenRead($path))).Entries | where {$_.name -eq 'StringLookupFactory.class'}
}catch{
$global:Errors += @{Error=$_.exception.Message;path=$path}
}
foreach($nestedfile in $nestedfiles) {
if ($nestedfile.name -eq 'StringLookupFactory.class'){
$hasCommons = $true
$JNDIfile = $nestedfile.fullname
}
}
try{
$zip = [io.compression.zipfile]::OpenRead($path)
}catch{
$global:Errors += @{Error=$_.exception.Message;path=$path}
}
$file = $zip.Entries | where-object { $_.Name -eq "pom.properties" -and $_.FullName -match 'log4j'}
if($file -ne $null){
$stream = $file.Open()
$reader = New-Object IO.StreamReader($stream)
$text = $reader.ReadToEnd()
$version = -split $text | select-string -Pattern "Version"
$reader.Close()
$stream.Close()
$zip.Dispose()
}
$versionCVE = (Check-Version -version $version.line -hasCommons $hasCommons)
if ($hasCommons -and !($versionCVE.fixedversion)){
$vuln = $true
write-console -CVE $versionCVE.CVE -path $path -version $version
$remresult = remove-file
}else{$vuln = $false}
$return = @{
path = $path;
version = $version.line;
text=$text;
pomLocation=$file.FullName;
hasCommons=$hasCommons;
JNDILocation=$JNDIfile
CVE = $versionCVE.CVE
CVSSScore = $versionCVE.CVSSScore
FixedVersion = $versionCVE.fixedversion
Vulnerable = $vuln
ComputerName = $env:COMPUTERNAME
Remediated = $remresult
}
return $return
}
function Scan-System{
param($filetypes)
$scannedfiles =@()
$DriveErrors = @()
$drives = (Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType ='3'").DeviceID
#$Drives = (Get-PSDrive -PSProvider FileSystem | Select-Object Root, DisplayRoot | Where-Object {$_.DisplayRoot -eq $null}).root
$drives = @('c:\test','g:\test\')
foreach ($Drive in $Drives) {
$drive = "$drive\"
$searchingmessage = "Searching Drive $drive on host $env:ComputerName..."
write-host $searchingmessage -ForegroundColor Cyan
$global:vulnerabilityresults += $searchingmessage
$javaFiles = Get-ChildItem $Drive -Recurse -ErrorVariable DriveError -include $filetypes -ErrorAction SilentlyContinue #| out-null
foreach ($javaFile in $javaFiles){
$scannedfiles += [pscustomobject](Scan-File $javaFile)
}
}
$global:csv = $Scannedfiles
$global:Errors += @{Error=$DriveError.exception.message}
$scannedfiles += $global:Errors
$scannedfiles = convertto-json $scannedfiles
return $scannedfiles
}
Function Main{
If(!(Test-Path "$env:SystemDrive\Text4Sherlock")) { New-Item -ItemType Directory "$env:SystemDrive\Text4Sherlock" -Force }
$date = get-date -Format "yyyy-MM-dd_HH-mm-ss"
$results = Scan-System -filetypes $filetypes
write-host "`r`nErrors:`r`n"
$global:vulnerabilityresults += "`r`nErrors:`r`n"
$global:Errors | foreach {
$errorMessage = "┌[$($_.Error)`r`n└─[$($_.path)"
write-host $errorMessage -ForegroundColor red
$global:vulnerabilityresults += $errorMessage
}
$jsonpath = "$env:SystemDrive\Text4Sherlock\Text4Sherlock $date.json"
$resultpath = "$env:SystemDrive\Text4Sherlock\Text4Sherlock $date.txt"
$csvpath = "$env:SystemDrive\Text4Sherlock\Text4Sherlock $date.csv"
write-host "`r`nWriting Log Files to $env:SystemDrive\Text4Sherlock\..."
set-content -path $jsonpath -value $results
Set-Content -path $resultpath -Value $global:vulnerabilityresults
$global:csv | export-csv $csvpath -NoTypeInformation
return @{json=$results;txt=$global:vulnerabilityresults;csv=($global:csv | convertto-csv);comp = $env:COMPUTERNAME}
}
main
}
function get-menu{
$user = $env:username
$continue = $true
$creds = $null
$remediation = 'Search Only'
$remmessage = 'Text4Sherlock will only scan and report but will not remediate.'
do{
cls
$logo = " ██▓ ▒█████ ▄████ ▄▄▄ ▄▄▄██▀▀▀██████ ██░ ██ ▓█████ ██▀███ ██▓ ▒█████ ▄████▄ ██ ▄█▀`r`n▓██▒ ▒██▒ ██▒ ██▒ ▀█▒ ▄████▒ ▒██ ▒██ ▒ ▓██░ ██▒▓█ ▀ ▓██ ▒ ██▒▓██▒ ▒██▒ ██▒▒██▀ ▀█ ██▄█▒ `r`n▒██░ ▒██░ ██▒▒██░▄▄▄░ ▄█▀ ██▒ ░██ ░ ▓██▄ ▒██▀▀██░▒███ ▓██ ░▄█ ▒▒██░ ▒██░ ██▒▒▓█ ▄ ▓███▄░ `r`n▒██░ ▒██ ██░░▓█ ██▓ ██▄▄▄▄██░▓██▄██▓ ▒ ██▒░▓█ ░██ ▒▓█ ▄ ▒██▀▀█▄ ▒██░ ▒██ ██░▒▓▓▄ ▄██▒▓██ █▄ `r`n░██████▒░ ████▓▒░░▒▓███▀▒▒▓▓▓ ██ ▓███▒ ▒██████▒▒░▓█▒░██▓░▒████▒░██▓ ▒██▒░██████▒░ ████▓▒░▒ ▓███▀ ░▒██▒ █▄`r`n░ ▒░▓ ░░ ▒░▒░▒░ ░▒ ▒ ░░▒▓ █▓ ▒▓▒▒░ ▒ ▒▓▒ ▒ ░ ▒ ░░▒░▒░░ ▒░ ░░ ▒▓ ░▒▓░░ ▒░▓ ░░ ▒░▒░▒░ ░ ░▒ ▒ ░▒ ▒▒ ▓▒`r`n░ ░ ▒ ░ ░ ▒ ▒░ ░ ░ ░ ▒▒ ▒ ▒ ░▒░ ░ ░▒ ░ ░ ▒ ░▒░ ░ ░ ░ ░ ░▒ ░ ▒░░ ░ ▒ ░ ░ ▒ ▒░ ░ ▒ ░ ░▒ ▒░`r`n ░ ░ ░ ░ ░ ▒ ░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░ ░░ ░ ░ ░░ ░ ░ ░ ░ ░ ░ ▒ ░ ░ ░░ ░ `r`n ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ `r`n ░ `r`n"
write-host $logo -foreground 'magenta'
write-host "Version: 1.1.2022.1.12"
write-host "Written by Harley Schaeffer"
write-host "https://github.com/Maelstromage/Text4Sherlock`r`n"
write-host '[*]==================== Menu ====================[*]'
write-host ""
write-host "[U] - Run as a different user. Current user: [" -nonewline
write-host $user -nonewline -foreground magenta
write-host "]"
<#
write-host "[V] - Remediate Vulnerability type: [" -nonewline
write-host "$remediation" -nonewline -foreground magenta
write-host "]"
write-host " ($remmessage)" -ForegroundColor red
#>
write-host "[R] - Run with above parameters"
write-host "Enter a Letter to continue"
$readhost = Read-Host
switch ($readhost){
'U' {
$creds = Get-Credential
$user = $creds.username
}
<#
'V' {
write-host "`r`nSelect a remediation type"
write-host "1. Search Only (Text4Sherlock will only scan and report but will not remediate.)"
write-host "2. Remove JNDILookup.class (Remediates the vulnerability and has the best chance to not break functinality. Other scanners might read as false positive.)"
write-host "3. Remove Java file (Removes the entire JAR, WAR, EAR, JPI, or HPI file. Will break Log4j functionality. )"
write-host "Warning: removing JNDILookup.class or the Java file itself can break the functionality of the machine in question, do so at your own risk." -ForegroundColor red
$remediationselection = read-host
switch ($remediationselection){
1 {
$remediation = 'Search Only'
$remmessage = "Text4Sherlock will only scan and report but will not remediate."
}
2 {
$remediation = 'Remove JNDILookup.class'
$remmessage = "Warning: removing JNDILookup.class or the Java file itself can break the functionality of the machine in question, do so at your own risk. `r`n Remediates the vulnerability and has the best chance to not break functinality. Other scanners might read as false positive."
}
3 {
$remediation = 'Remove Java file'
$remmessage = "Warning: removing JNDILookup.class or the Java file itself can break the functionality of the machine in question, do so at your own risk. `r`n Removes the entire JAR, WAR, EAR, JPI, or HPI file. Will break Log4j functionality."
}
default {
write-host "Incorrect input. Press any key to continue"
Read-Host
}
}
}
#>
'R' {
$continue = $false
Scan-MultipleSystems -creds $creds
}
default {
write-host "Please enter one of the values above. example: 1, 2, 3. Press any key to continue."
read-host
}
}
}while($continue -eq $true)
}
function Scan-MultipleSystems{
param($creds=$null)
get-job | stop-job
get-job | remove-job
$date = get-date -Format "yyyy-MM-dd_HH-mm-ss"
$comps = get-content "$PSScriptRoot\Computers.txt"
#$creds = Get-Credential -Message "Caution: Script will run even if you do not type your password correctly probably locking you out:"
foreach ($comp in $comps){
Get-Service -Name WinRM -ComputerName $comp -ErrorAction silentlycontinue | out-null | Set-Service -Status Running
if ($creds -ne $null){
Invoke-Command -credential $creds -computername $comp -ScriptBlock $code -AsJob
}else{
Invoke-Command -computername $comp -ScriptBlock $code -AsJob
}
}
#$exit = $false
$combinedresults = @()
$continue = $true
do{
if ((Get-Job -state running) -eq $null){$continue = $false}
foreach($job in get-job){
if ($job.state -eq 'Completed'){
$Received = $job | Receive-Job
$csv=$Received.csv
$txt=$Received.txt
$json=$Received.json
write-logs -csv $csv -txt $txt -json $json -date $date -comp $received.comp
$job | remove-job
get-job
}
} foreach($job in get-job){
if ($job.state -eq 'Failed'){
$Received = $job | Receive-Job
$txt=$Received
write-logs -csv $null -txt $txt -json $null -date $date -comp $received.comp
$job | remove-job
}
}
}while($continue -ne $false)
get-job
}
function write-logs{
param($csv,$txt,$json,$date,$comp)
if(!(test-path "$PSScriptRoot\Logs $date")){New-Item -ItemType Directory -Path $PSScriptRoot -Name "Logs $date" | out-null}
$path = "$PSScriptRoot\Logs $date\$comp $date"
write-host "`r`nWriting logs to $PSScriptRoot\Logs $date\$comp $date "
if($json -ne $null){set-content -path "$path.json" -value $json}
if($txt -ne $null){Set-Content -path "$path.txt" -Value $txt}
if($csv -ne $null){Set-Content -path "$path.csv" -Value $csv}
}
get-menu