-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest_log4shell.py
executable file
·1315 lines (1163 loc) · 48.1 KB
/
test_log4shell.py
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import argparse
import csv
import itertools
import logging
import json
import os
import pathlib
import platform
import socket
import sys
import time
import zipfile
import concurrent.futures
from enum import Flag, Enum, auto
from shlex import shlex
from collections import Counter
from datetime import timedelta
VERSION = "1.22-20220222"
DEFAULT_LOG_NAME = 'log4shell-finder.log'
log = logging.getLogger("log4shell-finder")
CLASS_EXTS = (".class", ".esclazz", ".classdata")
ZIP_EXTS = (".zip", ".jar", ".war", ".ear", ".aar", ".jpi",
".hpi", ".rar", ".nar", ".wab", ".eba", ".ejb", ".sar",
".apk", ".par", ".kar", )
DELIMITER = " > "
APPENDER = "core/Appender"
DRFAPPENDER = "log4j/DailyRollingFileAppender"
FILTER = "core/Filter"
JDBC_DSCS = "core/appender/db/jdbc/DataSourceConnectionSource"
JMSAPPENDER = "net/JMSAppender"
JDBCAPPENDER = "jdbc/JDBCAppender"
JDBCPATTPARSER = "jdbc/JdbcPatternParser"
JNDILOOKUP = "core/lookup/JndiLookup"
# MSGPATCONV = "core/pattern/MessagePatternConverter"
CFGSTRSUBST = "core/lookup/ConfigurationStrSubstitutor"
JNDIMANAGER = "core/net/JndiManager"
JNDIUTIL = "net/JNDIUtil"
SOCKETSERVER = "net/SocketServer"
HARDENEDOIS = "net/HardenedObjectInputStream"
HARDENEDLEIS = "net/HardenedLoggingEventInputStream"
JMSSINK = "net/JMSSink"
LAYOUT = "core/Layout"
LOGEVENT = "core/LogEvent"
LOGGERCONTEXT = "core/LoggerContext"
NOSQL_APPENDER = "core/appender/nosql/NoSqlAppender"
POM_PROPS = "META-INF/maven/org.apache.logging.log4j/log4j-core/pom.properties"
MANIFEST = "META-INF/MANIFEST.MF"
SETUTILS = "core/util/SetUtils"
# https://github.com/qos-ch/logback/commit/21d772f2bc2ed780b01b4fe108df7e29707763f1
JNDICONNSRC = "core/db/JNDIConnectionSource"
# in 2.8.x and < 2.9.0
ABSSOCKETSRV = "core/net/server/AbstractSocketServer"
FILOBJINPSTREAM = "FilteredObjectInputStream"
CHAINSAW = "chainsaw/Main"
CLASSES = [
APPENDER,
DRFAPPENDER,
FILTER,
JDBC_DSCS,
JMSAPPENDER,
JDBCAPPENDER,
JDBCPATTPARSER,
JNDILOOKUP,
CFGSTRSUBST,
JNDIMANAGER,
JNDIUTIL,
SOCKETSERVER,
HARDENEDOIS,
HARDENEDLEIS,
JMSSINK,
LAYOUT,
LOGEVENT,
LOGGERCONTEXT,
NOSQL_APPENDER,
POM_PROPS,
SETUTILS,
ABSSOCKETSRV,
FILOBJINPSTREAM,
CHAINSAW,
]
progress = None
def get_class_names(base):
return tuple([a[0]+a[1] for a in itertools.product([base], CLASS_EXTS)])
CLASS_VARIANTS = {cls: get_class_names(cls) for cls in CLASSES}
CLASS_VARIANTS_NATIVE = {k: tuple(
[str(pathlib.PurePath(a)) for a in v]) for k, v in CLASS_VARIANTS.items()}
# This occurs in "JndiManager.class" in 2.15.0
IN_2_15_0 = b"Invalid JNDI URI - {}"
# This occurs in "JndiManager.class" in 2.16.0
IN_2_16_0 = b"log4j2.enableJndi"
# This occurs in "JndiLookup.class" in 2.17.0
IN_2_17_0 = b"JNDI must be enabled by setting log4j2.enableJndiLookup=true"
# This occurs in "JndiLookup.class" other than 2.12.2
NOT_IN_2_12_2 = b"Error looking up JNDI resource [{}]."
# This occurs in "JndiManager.class" in 2.3.1
IN_2_3_1 = b"Unsupported JNDI URI - {}"
# This occurs in "DataSourceConnectionSource.class" in 2.17.1 and friends.
IS_CVE_2021_44832_SAFE = b"JNDI must be enabled by setting log4j2.enableJndiJdbc=true"
# This is part of fix for CVE-2017-5645 in "AbstractSocketServer.java" of 2.8.2
IN_2_8_2 = b"Additional classes to allow deserialization"
# This disappears from JMSSink in fix of CVE-2022-23303
IN_JMSSINK = b"Could not find name "
# Patched in JndiLookup from 2.12.2
IN_2_12_2_PATCHED = b"JNDI is not supported"
# Patched in MessagePatternConverter of 2.16.0
# IN_2_16_0_PATCHED = b"Message Lookups are no longer supported"
class FileType(Enum):
CLASS = 0
ZIP = 1
OTHER = -1
class Status(Flag):
FIXED = auto()
CANNOTFIX = auto()
NOTOKAY = auto()
OLD = auto()
OLDUNSAFE = auto()
STRANGE = auto()
V1_2_17_SAFE = auto()
V2_0_BETA8 = auto()
V2_0_BETA9 = auto()
V2_3_1 = auto()
V2_3_2 = auto()
V2_8_1 = auto()
V2_10_0 = auto()
V2_12_2 = auto()
V2_12_3 = auto()
V2_12_4 = auto()
V2_15_0 = auto()
V2_16_0 = auto()
V2_17_0 = auto()
V2_17_1 = auto()
NOJNDILOOKUP = auto()
CVE_2019_17571 = auto()
CVE_2021_4104 = auto()
CVE_2022_23307 = auto()
CVE_2022_23305 = auto()
CVE_2022_23302 = auto()
CVE_2017_5645 = auto()
CVE_2021_44228 = auto()
CVE_2021_45046 = auto()
CVE_2021_45105 = auto()
CVE_2021_44832 = auto()
# CVE_2017_5645 = V2_8_1 | V2_0_BETA9 | V2_0_BETA8 | V2_3_1 | V2_3_2
# CVE_2021_44228 = V2_10_0 | V2_0_BETA9
# CVE_2021_45046 = CVE_2021_44228 | V2_15_0
# CVE_2021_45105 = CVE_2021_45046 | V2_12_2 | V2_16_0
# CVE_2021_44832 = V2_0_BETA8 | CVE_2021_45105 | V2_3_1 | V2_12_3 | V2_17_0
VULNERABLE = (CVE_2021_44832 | CVE_2021_44228 | CVE_2021_45046 |
CVE_2021_45105 | CVE_2021_4104 | CVE_2017_5645 |
CVE_2019_17571 | CVE_2022_23307 | CVE_2022_23305 |
CVE_2022_23302)
SAFE = V2_3_2 | V2_12_4 | V2_17_1
vuldesc = {
Status.CVE_2021_44228: ["CVE-2021-44228", "10.0", "Critical", "8", "2.0-beta9", "2.14.1", "2.15.0"],
Status.CVE_2017_5645: ["CVE-2017-5645", "9.8", "Critical", "7", "2.0-alpha1", "2.8.1", "2.8.2"],
Status.CVE_2019_17571: ["CVE-2019-17571", "9.8", "Critical", "", "1.2.0", "1.2.17", "nofix"],
Status.CVE_2021_45046: ["CVE-2021-45046", "9.0", "Critical", "7/8", "2.0-beta9", "2.15.0 excluding 2.12.2", "2.12.2/2.16.0"],
Status.CVE_2022_23305: ["CVE-2022-23305", "8.1", "High", "", "1.2.0", "1.2.17", "nofix / 1.2.18.1"],
Status.CVE_2022_23307: ["CVE-2022-23307", "8.1", "High", "", "1.2.0", "1.2.17", "nofix / 1.2.18.1"],
Status.CVE_2021_4104: ["CVE-2021-4104", "7.5", "High", "-", "1.0", "1.2.17"],
Status.CVE_2021_44832: ["CVE-2021-44832", "6.6", "Medium", "6/7/8", "2.0-alpha7", "2.17.0, excluding 2.3.2/2.12.4", "2.3.2/2.12.4/2.17.1"],
Status.CVE_2022_23302: ["CVE-2022-23302", "6.6", "Medium", "", "1.0", "1.2.17", "nofix / 1.2.18.1"],
Status.CVE_2021_45105: ["CVE-2021-45105", "5.9", "Medium", "6/7/8", "2.0-beta9", "2.16.0, excluding 2.12.3", "2.3.1/2.12.3/2.17.0"],
}
def get_status_text(status):
flag = "*"
vulns = []
if status & Status.VULNERABLE:
flag = "+"
if log.isEnabledFor(logging.DEBUG):
vulns.append(f"*{status.value}*")
for s, d in vuldesc.items():
if status & s:
v = d[0] + "(" + d[1] + ")"
if log.isEnabledFor(logging.DEBUG):
v += f" {d[4]} > {d[5]}"
vulns.append(v)
# if status & Status.CVE_2021_44228 and not (status & Status.NOJNDILOOKUP):
# if status & Status.CVE_2021_45046 and not (status & Status.NOJNDILOOKUP):
if not vulns and (status & Status.SAFE):
vulns.append("SAFE")
flag = "-"
if not vulns and (status & Status.V1_2_17_SAFE):
vulns.append("OLDSAFE")
flag = "-"
if status & Status.FIXED:
vulns.append("FIXED")
if status & Status.CANNOTFIX:
vulns.append("CANNOTFIX")
if status & Status.NOJNDILOOKUP:
vulns.append("NOJNDILOOKUP")
if status & Status.STRANGE:
vulns.append("STRANGE")
return flag, sorted(vulns)
class Container(Enum):
UNDEFINED = 0
PACKAGE = 1
FOLDER = 2
def log_item(path, status, message, pom_version="unknown", product="log4j", container=Container.UNDEFINED):
global args
if not args.strange and status & Status.STRANGE:
return
flag, vulns = get_status_text(status)
if status & Status.NOJNDILOOKUP:
message += ", JndiLookup.class not found"
log_item.found_items.append({
"container": container.name.title(),
"path": str(path),
"status": vulns,
"message": message,
"pom_version": pom_version,
"product": product,
})
message = f"[{flag}] [{', '.join(vulns)}] {container.name.title()} {path} {message}"
log.info(message)
log_item.found_items = []
def get_version_from_manifest(lines):
try:
kv = {}
for line in lines:
if ":" not in line:
continue
line = line.split(":", 1)
kv[line[0]] = line[1].strip()
# Implementation-Title: log4j
# Implementation-Version: 1.1.3
if "Implementation-Title" in kv:
product = kv["Implementation-Title"]
if (product.lower().startswith(('log4j', 'reload4j')) and
"Implementation-Version" in kv):
return kv["Implementation-Title"], kv['Implementation-Version']
except:
raise
pass
return
def parse_kv_pairs(text, item_sep=None, value_sep=".=-", final_sep="="):
"""Parse key-value pairs from a shell-like text."""
# https://stackoverflow.com/questions/38737250/extracting-key-value-pairs-from-string-with-quotes
lexer = shlex(text, posix=True)
if item_sep:
lexer.whitespace = item_sep
lexer.wordchars += value_sep
return dict(word.split(final_sep, maxsplit=1) for word in lexer)
def scan_archive(f, path):
global args
log.debug("Scanning " + path)
with zipfile.ZipFile(f, mode="r") as zf:
nl = zf.namelist()
# print(f'{path} total files size={sum(e.file_size for e in zf.infolist())}')
log4jProbe = [False] * 5
isLog4j2_10 = False
hasJndiLookup = False
hasJndiManager = False
hasJdbcJndiDisabled = False
hasSetUtils = False
isLog4j1_x = False
hasJMSAppender = False
hasJNDIUtil = False
isLog4j2_15 = False
isLog4j2_16 = False
isLog4j2_17 = False
isLog4j2_15_override = False
isLog4j2_12_2 = False
isLog4j2_12_2_override = False
isLog4j2_12_3 = False
isLog4j2_3_1 = False
hasCVE_2017_5645 = False
hasChainsaw = False
hasHardenedLoggingEventInputStream = False
hasJDBCAppender = False
hasJDBCPatternParser = False
hasSocketServer = False
hasHardenedObjectInputStream = False
hasFilteredObjectInputStream = False
hasVulnerableJMSSink = False
pom_path = None
manifest_path = None
jndilookup_path = None
for fn in nl:
fnl = fn.lower()
if fnl.endswith(ZIP_EXTS):
with zf.open(fn, "r") as inner_zip:
scan_archive(inner_zip, path+DELIMITER+fn)
elif fnl.endswith("log4j-core/pom.properties"):
pom_path = fn
elif fnl.endswith("log4j/pom.properties") and not pom_path:
pom_path = fn
elif fnl.endswith("meta-inf/manifest.mf"):
manifest_path = fn
elif not fnl.endswith(CLASS_EXTS):
continue
elif fn.endswith(CLASS_VARIANTS[JDBC_DSCS]):
with zf.open(fn, "r") as inner_class:
class_content = inner_class.read()
if class_content.find(IS_CVE_2021_44832_SAFE) >= 0:
hasJdbcJndiDisabled = True
elif fn.endswith(CLASS_VARIANTS[JNDILOOKUP]):
jndilookup_path = pathlib.PurePosixPath(fn)
hasJndiLookup = True
with zf.open(fn, "r") as inner_class:
class_content = inner_class.read()
if class_content.find(IN_2_17_0) >= 0:
isLog4j2_17 = True
elif class_content.find(IN_2_12_2_PATCHED) >= 0:
isLog4j2_12_2 = True
elif class_content.find(NOT_IN_2_12_2) >= 0:
isLog4j2_12_2_override = True
else:
isLog4j2_12_2 = True
elif fn.endswith(CLASS_VARIANTS[CFGSTRSUBST]):
isLog4j2_17 = True
elif fn.endswith(CLASS_VARIANTS[JNDIMANAGER]):
hasJndiManager = True
with zf.open(fn, "r") as inner_class:
class_content = inner_class.read()
if class_content.find(IN_2_15_0) >= 0:
isLog4j2_15 = True
if class_content.find(IN_2_16_0) >= 0:
isLog4j2_16 = True
else:
isLog4j2_15_override = True
if class_content.find(IN_2_3_1) >= 0:
isLog4j2_3_1 = True
elif fn.endswith(CLASS_VARIANTS[ABSSOCKETSRV]):
with zf.open(fn, "r") as inner_class:
class_content = inner_class.read()
if class_content.find(IN_2_8_2) < 0:
hasCVE_2017_5645 = True
elif fn.endswith(CLASS_VARIANTS[SETUTILS]):
hasSetUtils = True
elif fn.endswith(CLASS_VARIANTS[DRFAPPENDER]):
isLog4j1_x = True
elif fn.endswith(CLASS_VARIANTS[JMSAPPENDER]):
hasJMSAppender = True
elif fn.endswith(CLASS_VARIANTS[JNDIUTIL]):
hasJNDIUtil = True
elif fn.endswith(CLASS_VARIANTS[SOCKETSERVER]):
hasSocketServer = True
elif fn.endswith(CLASS_VARIANTS[FILOBJINPSTREAM]):
hasFilteredObjectInputStream = True
elif fn.endswith(CLASS_VARIANTS[HARDENEDOIS]):
hasHardenedObjectInputStream = True
elif fn.endswith(CLASS_VARIANTS[HARDENEDLEIS]):
hasHardenedLoggingEventInputStream = True
elif fn.endswith(CLASS_VARIANTS[CHAINSAW]):
hasChainsaw = True
elif fn.endswith(CLASS_VARIANTS[JDBCAPPENDER]):
hasJDBCAppender = True
elif fn.endswith(CLASS_VARIANTS[JDBCPATTPARSER]):
hasJDBCPatternParser = True
elif fn.endswith(CLASS_VARIANTS[JMSSINK]):
with zf.open(fn, "r") as inner_class:
class_content = inner_class.read()
if class_content.find(IN_JMSSINK) >= 0:
hasVulnerableJMSSink = True
elif fn.endswith(CLASS_VARIANTS[LOGEVENT]):
log4jProbe[0] = True
elif fn.endswith(CLASS_VARIANTS[APPENDER]):
log4jProbe[1] = True
elif fn.endswith(CLASS_VARIANTS[FILTER]):
log4jProbe[2] = True
elif fn.endswith(CLASS_VARIANTS[LAYOUT]):
log4jProbe[3] = True
elif fn.endswith(CLASS_VARIANTS[LOGGERCONTEXT]):
log4jProbe[4] = True
elif fn.endswith(CLASS_VARIANTS[NOSQL_APPENDER]):
isLog4j2_10 = True
if log.isEnabledFor(logging.DEBUG):
log.debug(f"### log4jProbe = {log4jProbe}, isLog4j2_10 = {isLog4j2_10}," +
f" hasJndiLookup = {hasJndiLookup}, hasJndiManager = {hasJndiManager}, " +
f"isLog4j1_x = {isLog4j1_x}, isLog4j2_15 = {isLog4j2_15}, " +
f"isLog4j2_16 = {isLog4j2_16}, isLog4j2_15_override =" +
f" {isLog4j2_15_override}, isLog4j2_12_2 = {isLog4j2_12_2}," +
f" isLog4j2_12_2_override = {isLog4j2_12_2_override}, " +
f"isLog4j2_17 = {isLog4j2_17} ")
isLog4j2 = False
isLog4j_2_10_0 = False
isLog4j_2_12_2 = False
isRecent = False
if (log4jProbe[0] and log4jProbe[1] and log4jProbe[2] and
log4jProbe[3] and log4jProbe[4]):
isLog4j2 = True
if hasJndiManager:
if (isLog4j2_17 or (isLog4j2_15 and not isLog4j2_15_override) or
(isLog4j2_12_2 and not isLog4j2_12_2_override)):
isRecent = True
isLog4j_2_12_2 = (
isLog4j2_12_2 and not isLog4j2_12_2_override)
if isLog4j2_17 and hasSetUtils:
isLog4j2_12_3 = True
isLog4j2_17 = False
product = "log4j"
if isLog4j2:
version = "2.x"
elif isLog4j1_x:
version = "1.x"
else:
version = None
if pom_path:
with zf.open(pom_path, "r") as inf:
content = inf.read().decode('UTF-8')
kv = parse_kv_pairs(content)
if log.isEnabledFor(logging.DEBUG):
log.debug(f"pom.properties found at {path}:{pom_path}, {kv}")
if "version" in kv:
version = kv['version']
if "artifactId" in kv:
product = kv['artifactId']
elif manifest_path:
with zf.open(manifest_path, "r") as inf:
lines = inf.read().decode('UTF-8').splitlines()
if log.isEnabledFor(logging.DEBUG):
log.debug(
f"MANIFEST.MF found at {path}:{pom_path}")
product, version = get_version_from_manifest(
lines) or (product, version)
if log.isEnabledFor(logging.DEBUG):
log.debug(
f"### isLog4j2 = {isLog4j2}, isLog4j_2_10_0 = {isLog4j_2_10_0}," +
f" isLog4j_2_12_2 = {isLog4j_2_12_2}, isRecent = {isRecent}," +
f" isLog4j2_17 = {isLog4j2_17}, isLog4j2_12_3 = {isLog4j2_12_3}")
status = Status(0)
if not isLog4j2:
if isLog4j1_x:
if hasSocketServer and not (hasFilteredObjectInputStream or
hasHardenedObjectInputStream):
status |= Status.CVE_2019_17571
if hasVulnerableJMSSink:
status |= Status.CVE_2022_23302
if hasChainsaw and hasVulnerableJMSSink:
# not hasHardenedLoggingEventInputStream and
# not hasJDBCAppender):
status |= Status.CVE_2022_23307
if hasJDBCAppender and not hasJDBCPatternParser:
status |= Status.CVE_2022_23305
if hasJMSAppender and not hasJNDIUtil:
status |= Status.CVE_2021_4104
if not status:
status = Status.V1_2_17_SAFE
log_item(path, status,
f"contains {product}-{version}",
version, product, Container.PACKAGE)
return
elif version:
log_item(path, Status.STRANGE,
f"contains pom.properties for {product}-{version}, but binary classes missing",
version, product, Container.PACKAGE)
return
else:
return
# isLog4j2 == True
if isLog4j1_x:
prefix = f"contains {product}-1.x AND {product}-"
else:
prefix = f"contains {product}-"
prefix += version
buf = ""
# CVE_2017_5645 = V2_8_1 | V2_0_BETA9 | V2_0_BETA8 | V2_3_1 | V2_3_2
# CVE_2021_44228 = V2_10_0 | V2_0_BETA9
# CVE_2021_45046 = CVE_2021_44228 | V2_15_0
# CVE_2021_45105 = CVE_2021_45046 | V2_12_2 | V2_16_0
# CVE_2021_44832 = V2_0_BETA8 | CVE_2021_45105 | V2_3_1 | V2_12_3 | V2_17_0
# SAFE = V2_3_2 | V2_12_4 | V2_17_1
if isLog4j2_10:
if isRecent:
if isLog4j2_12_3:
if hasJdbcJndiDisabled:
buf = " == 2.12.4"
status |= Status.V2_12_4 # SAFE
else:
buf = " == 2.12.3"
# status = Status.V2_12_3 # CVE_2021_44832
status |= Status.CVE_2021_44832
elif isLog4j2_17:
if hasJdbcJndiDisabled:
buf = " >= 2.17.1"
status |= Status.V2_17_1 # SAFE
else:
buf = " >= 2.17.0"
# status = Status.V2_17_0 # CVE_2021_44832
status |= Status.CVE_2021_44832
elif isLog4j2_16:
buf = " >= 2.16.0"
# status = Status.V2_16_0 # CVE_2021_45105
status |= Status.CVE_2021_45105 | Status.CVE_2021_44832
elif isLog4j_2_12_2:
buf = " == 2.12.2"
# status = Status.V2_12_2 # CVE_2021_45105
status |= Status.CVE_2021_45105 | Status.CVE_2021_44832
else:
buf = " == 2.15.0"
# status = Status.V2_15_0 # CVE_2021_45046
status |= (Status.CVE_2021_45046 | Status.CVE_2021_45105 |
Status.CVE_2021_44832)
if hasJdbcJndiDisabled:
status &= ~Status.CVE_2021_44832
else:
buf = " >= 2.10.0"
# status = Status.V2_10_0 # CVE_2021_44228
status |= (Status.CVE_2021_44228 | Status.CVE_2021_45046 |
Status.CVE_2021_45105 | Status.CVE_2021_44832)
elif isLog4j2_3_1:
if hasJdbcJndiDisabled:
buf = " >= 2.3.2"
# status = Status.V2_3_2 # SAFE
#status |= Status.CVE_2017_5645
else:
buf = " == 2.3.1"
# status = Status.V2_3_1 # CVE_2021_44832
status |= Status.CVE_2021_44832
elif hasCVE_2017_5645:
buf = " <= 2.8.1"
# status = Status.V2_8_1
# status |= Status.CVE_2017_5645
elif not hasJndiLookup:
if not buf:
buf += " <= 2.0-beta8"
# status = Status.V2_0_BETA8
# status |= Status.CVE_2017_5645
else:
buf = " >= 2.0-beta9 (< 2.10.0)"
# status = Status.V2_0_BETA9 # CVE_2021_44228
status |= (Status.CVE_2021_44228 | Status.CVE_2021_45046 |
Status.CVE_2021_45105 | Status.CVE_2021_44832)
if hasCVE_2017_5645:
status |= Status.CVE_2017_5645
buf = prefix + buf
if not hasJndiLookup:
status |= Status.NOJNDILOOKUP
fix_msg = ""
if (status & (Status.CVE_2021_45046 | Status.CVE_2021_44228)) and args.fix:
if not jndilookup_path:
log.info("[W] Cannot fix %s, JndiLookup.class not found", path)
status |= Status.CANNOTFIX
elif DELIMITER in path:
log.info("[W] Cannot fix %s, nested archive", path)
status |= Status.CANNOTFIX
else:
suffix_len = len(jndilookup_path.suffix)
if suffix_len < 3:
log.info(
"[W] Cannot fix %s, suffix of %s too short - %s",
path, jndilookup_path, suffix_len)
status |= Status.CANNOTFIX
else:
suffix_replacement = ".vulnerable"
if suffix_len > len(suffix_replacement):
suffix_replacement += "x" * \
(suffix_len - len(suffix_replacement))
new_fn = jndilookup_path.with_suffix(
".vulnerable"[:suffix_len])
fix_msg = f", fixing, {jndilookup_path} has been renamed to {new_fn.name}"
f.seek(0)
fcontent = f.read()
bstr_from = str(jndilookup_path).encode('utf-8')
bstr_to = str(new_fn).encode('utf-8')
where = 0
replacement_count = 0
while True:
where = fcontent.find(bstr_from, where + 1)
if where < 0:
break
f.seek(where)
f.write(bstr_to)
replacement_count += 1
if replacement_count:
f.flush()
status |= Status.FIXED
log_item(path, status, buf + fix_msg, version, product, Container.PACKAGE)
def check_path_exists(folder, file_name, mangle=True):
path = folder.joinpath(pathlib.PurePosixPath(file_name))
if not mangle:
if os.path.exists(path):
return path
else:
return False
for ext in CLASS_EXTS:
p = path.with_suffix(ext)
log.debug("Checking if %s exists", p)
if os.path.exists(p):
return p
return False
def fix_jndilookup_class(fn):
try:
new_fn = fn.with_suffix('.vulnerable')
os.rename(fn, new_fn)
return f", fixing, {fn} has been renamed to {new_fn.name}"
except Exception as ex:
log.error(f"Error renaming file {fn} {ex}")
return ""
def get_version_from_path(parent):
product = "log4j"
version = None
pom_path = check_path_exists(
parent.parent.parent.parent.parent.parent, POM_PROPS, mangle=False)
if pom_path:
with open(pom_path, "r") as inf:
content = inf.read()
kv = parse_kv_pairs(content)
log.debug("pom.properties found at %s, %s", pom_path, kv)
if "version" in kv:
version = kv['version']
if "artifactId" in kv:
product = kv['artifactId']
else:
p = parent
for i in range(5):
manifest_path = check_path_exists(p, MANIFEST, mangle=False)
if manifest_path:
break
p = p.parent
if manifest_path:
with open(manifest_path, "r") as inf:
lines = inf.readlines()
log.debug("MANIFEST.MF found at %s", manifest_path)
product, version = get_version_from_manifest(
lines) or (product, version)
return product, version
def check_class(class_file):
global args
parent = pathlib.PurePath(class_file).parent
product = "log4j"
status = Status(0)
if class_file.endswith(CLASS_VARIANTS_NATIVE[DRFAPPENDER]):
product, version = get_version_from_path(parent) or (product, "1.x")
hasVulnerableJMSSink = False
fn = check_path_exists(parent, JMSSINK)
if fn:
with open(fn, "rb") as f:
if f.read().find(IN_JMSSINK) >= 0:
hasVulnerableJMSSink = True
status |= Status.CVE_2022_23302
if (check_path_exists(parent, SOCKETSERVER) and not
(check_path_exists(parent, FILOBJINPSTREAM)
or check_path_exists(parent, HARDENEDOIS))):
status |= Status.CVE_2019_17571
if (check_path_exists(parent, CHAINSAW) and
hasVulnerableJMSSink):
status |= Status.CVE_2022_23307
if (check_path_exists(parent, JDBCAPPENDER) and
not check_path_exists(parent, JDBCPATTPARSER)):
status |= Status.CVE_2022_23305
if (check_path_exists(parent, JMSAPPENDER) and
not check_path_exists(parent, JNDIUTIL)):
status |= Status.CVE_2021_4104
if not status:
status = Status.V1_2_17_SAFE
log_item(parent, status,
f"contains {product}-{version}",
version, product, container=Container.FOLDER)
return
if not class_file.endswith(CLASS_VARIANTS_NATIVE[LOGEVENT]):
return
log.debug("Match on %s", class_file)
product, version = get_version_from_path(parent) or (product, "2.x")
msg = f"contains {product}-" + version
log4j_dir = parent.parent
for fn in [APPENDER, FILTER, LAYOUT,
LOGGERCONTEXT]:
if not check_path_exists(log4j_dir, fn):
log_item(parent, Status.STRANGE,
f"{msg} {fn} not found",
version, product, container=Container.FOLDER)
return
fn = check_path_exists(log4j_dir, ABSSOCKETSRV)
if fn:
with open(fn, "rb") as f:
if f.read().find(IN_2_8_2) < 0:
hasCVE_2017_5645 = True
msg += " <= 2.8.1"
# status |= Status.V2_8_1
status |= Status.CVE_2017_5645
jndilookup_path = check_path_exists(log4j_dir, JNDILOOKUP)
if not jndilookup_path:
status |= Status.NOJNDILOOKUP
fix_msg = ""
hasJdbcJndiDisabled = False
fn = check_path_exists(log4j_dir, JDBC_DSCS)
if fn:
with open(fn, "rb") as f:
if f.read().find(IS_CVE_2021_44832_SAFE) >= 0:
hasJdbcJndiDisabled = True
if not check_path_exists(log4j_dir, NOSQL_APPENDER):
fn = check_path_exists(log4j_dir, JNDIMANAGER)
if fn:
with open(fn, "rb") as f:
if f.read().find(IN_2_3_1) >= 0:
if hasJdbcJndiDisabled:
log_item(parent, status | Status.SAFE,
msg + " >= 2.3.2",
version, product, container=Container.FOLDER)
return
else:
status |= Status.CVE_2021_44832
log_item(parent, status,
msg + " == 2.3.1",
version, product, container=Container.FOLDER)
return
# status |= Status.V2_0_BETA9 # CVE_2021_44228
status |= (Status.CVE_2021_44228 |
Status.CVE_2021_45046 | Status.CVE_2021_45105 |
Status.CVE_2021_44832)
if args.fix:
fix_msg = fix_jndilookup_class(jndilookup_path)
if fix_msg:
status |= Status.FIXED
log_item(parent, status,
msg + " >= 2.0-beta9 (< 2.10.0)" + fix_msg,
version, product, container=Container.FOLDER)
return
else:
# Check for 2.12.2...
fn = check_path_exists(log4j_dir, JNDILOOKUP)
if fn:
with open(fn, "rb") as f:
fcontent = f.read()
if fcontent.find(IN_2_12_2_PATCHED) > 0 or fcontent.find(NOT_IN_2_12_2) == -1:
status |= Status.CVE_2021_45105 | Status.CVE_2021_44832
log_item(parent, status,
msg + " == 2.12.2",
version, product, container=Container.FOLDER)
return
if fcontent.find(IN_2_17_0) >= 0:
if not check_path_exists(log4j_dir, SETUTILS):
if hasJdbcJndiDisabled:
log_item(parent, status | Status.V2_17_1, # SAFE,
msg + " >= 2.17.1",
version, product, container=Container.FOLDER)
return
else:
status |= Status.CVE_2021_44832
log_item(parent, status,
msg + " == 2.17.0",
version, product, container=Container.FOLDER)
return
else:
if hasJdbcJndiDisabled:
log_item(parent, status | Status.V2_12_4, # SAFE,
msg + " >= 2.12.4",
version, product, container=Container.FOLDER)
return
else:
status |= Status.CVE_2021_44832
log_item(parent, status,
msg + " == 2.12.3",
version, product, container=Container.FOLDER)
return
# CVE_2017_5645 = V2_8_1 | V2_0_BETA9 | V2_0_BETA8 | V2_3_1 | V2_3_2
# CVE_2021_44228 = V2_10_0 | V2_0_BETA9
# CVE_2021_45046 = CVE_2021_44228 | V2_15_0
# CVE_2021_45105 = CVE_2021_45046 | V2_12_2 | V2_16_0
# CVE_2021_44832 = V2_0_BETA8 | CVE_2021_45105 | V2_3_1 | V2_12_3 | V2_17_0
# SAFE = V2_3_2 | V2_12_4 | V2_17_1
elif check_path_exists(log4j_dir, CFGSTRSUBST):
if hasJdbcJndiDisabled:
log_item(parent, status | Status.V2_17_1, # SAFE,
msg + " >= 2.17.1",
version, product, container=Container.FOLDER)
return
else:
status |= Status.CVE_2021_44832
log_item(parent, status,
msg + " == 2.17.0",
version, product, container=Container.FOLDER)
return
fn = check_path_exists(log4j_dir, JNDIMANAGER)
if fn:
with open(fn, "rb") as f:
fcontent = f.read()
if fcontent.find(IN_2_16_0) >= 0:
status |= Status.CVE_2021_45105 | Status.CVE_2021_44832
log_item(parent, status,
msg + " == 2.16.0",
version, product, container=Container.FOLDER)
return
elif fcontent.find(IN_2_15_0) >= 0:
status |= (Status.CVE_2021_45046 | Status.CVE_2021_45105 |
Status.CVE_2021_44832)
if args.fix:
fix_msg = fix_jndilookup_class(jndilookup_path)
if fix_msg:
status |= Status.FIXED
log_item(parent, status,
msg + " == 2.15.0" + fix_msg,
version, product, container=Container.FOLDER)
return
status |= (Status.CVE_2021_44228 | Status.CVE_2021_45046 |
Status.CVE_2021_45105 | Status.CVE_2021_44832)
if args.fix:
fix_msg = fix_jndilookup_class(jndilookup_path)
if fix_msg:
status |= Status.FIXED
log_item(parent, status,
msg + " >= 2.10.0" + fix_msg,
version, product, container=Container.FOLDER)
return
def get_file_type(file_name):
_, ext = os.path.splitext(file_name)
ext = ext.lower()
if ext in CLASS_EXTS:
return FileType.CLASS
if ext in ZIP_EXTS:
return FileType.ZIP
return FileType.OTHER
def process_file(dirpath, filename, file_type):
global args
process_file.files_checked += 1
fullname = filename
try:
fullname = os.path.join(dirpath, filename)
if file_type == FileType.CLASS:
check_class(fullname)
elif file_type == FileType.ZIP:
with open(fullname, "r+b" if args.fix else "rb") as f:
scan_archive(f, fullname)
return fullname
except Exception as ex:
log.error("[E] Error processing %s: %s", fullname, ex)
process_file.files_checked = 0
def report_progress(fullname):
global progress
if not progress:
return
cl = time.time()
if (cl - progress) > report_progress.last_progress:
log.info(
f" After {int(cl-report_progress.start_time)} secs," +
f" scanned {process_file.files_checked} files " +
f"in {analyze_directory.dirs_checked} folders.\n" +
"\tCurrently at: " + fullname)
report_progress.last_progress = cl
report_progress.last_progress = time.time()
report_progress.start_time = time.time()
def process_files(dirpath, filenames):
for filename in filenames:
ft = get_file_type(filename)
if ft == FileType.OTHER:
process_file.files_checked += 1
continue
fullname = process_file(dirpath, filename, ft)
if fullname:
report_progress(fullname)
def analyze_directory(f, blacklist):
global args
# f = os.path.realpath(f)
if os.path.isdir(f):
log.info(f"[I] Scanning {f} in {args.threads} parallel threads")
walk_iter = os.walk(f, topdown=True)
with concurrent.futures.ThreadPoolExecutor(max_workers=args.threads) as executor:
futures = set()
while True:
(dirpath, dirnames, filenames) = next(
walk_iter, (None, None, None))
if dirpath is None:
done, not_done = concurrent.futures.wait(
futures, return_when=concurrent.futures.ALL_COMPLETED)
break
if not os.path.isdir(dirpath):
continue
if args.same_fs and not os.path.samefile(f, dirpath) and os.path.ismount(dirpath):
log.info("[I] Skipping mount point: " + dirpath)
dirnames.clear()
continue
if any(os.path.samefile(dirpath, p) for p in blacklist):
log.info("[I] Skipping blaclisted folder: " + dirpath)
dirnames.clear()
continue
analyze_directory.dirs_checked += 1
futures.add(executor.submit(
process_files, dirpath, filenames))
if len(futures) > args.threads:
done, futures = concurrent.futures.wait(
futures, return_when=concurrent.futures.FIRST_COMPLETED)
for ftr in done:
_ = ftr.result()
elif os.path.isfile(f):
ft = get_file_type(f)
fullname = process_file("", f, ft)
if fullname:
report_progress(fullname)
return
analyze_directory.dirs_checked = 0
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try: