Skip to content

Commit b740e65

Browse files
committed
support --force-fix option, support .ear file, added fix error log, v1.2.3
1 parent 75109d8 commit b740e65

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,30 @@
33
log4j2-scan is a single binary command-line tool for CVE-2021-44228 vulnerability scanning and mitigation patch. It also supports nested JAR file scanning and patch.
44

55
### Download
6-
* [log4j2-scan 1.2.2 (Windows x64)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v1.2.2/logpresso-log4j2-scan-1.2.2-win64.7z)
7-
* [log4j2-scan 1.2.2 (Linux x64)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v1.2.2/logpresso-log4j2-scan-1.2.2-linux.tar.gz)
8-
* [log4j2-scan 1.2.2 (Any OS, 10KB)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v1.2.2/logpresso-log4j2-scan-1.2.2.jar)
6+
* [log4j2-scan 1.2.3 (Windows x64)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v1.2.3/logpresso-log4j2-scan-1.2.3-win64.7z)
7+
* [log4j2-scan 1.2.3 (Linux x64)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v1.2.3/logpresso-log4j2-scan-1.2.3-linux.tar.gz)
8+
* [log4j2-scan 1.2.3 (Any OS, 10KB)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v1.2.3/logpresso-log4j2-scan-1.2.3.jar)
99

1010
### How to use
1111
Just run log4j2-scan.exe or log4j2-scan with target directory path.
1212

1313
On Windows
1414
```
15-
log4j2-scan [--fix] target_path
15+
log4j2-scan [--fix] [--trace] target_path
1616
```
1717
On Linux
1818
```
19-
./log4j2-scan [--fix] target_path
19+
./log4j2-scan [--fix] [--trace] target_path
2020
```
2121
On UNIX (AIX, Solaris, and so on)
2222
```
23-
java -jar logpresso-log4j2-scan-1.2.2.jar [--fix] [--trace] target_path
23+
java -jar logpresso-log4j2-scan-1.2.3.jar [--fix] [--trace] target_path
2424
```
2525

2626
If you add `--fix` option, this program will copy vulnerable original JAR file to .bak file, and create new JAR file without `org/apache/logging/log4j/core/lookup/JndiLookup.class` entry. In most environments, JNDI lookup feature will not be used. However, you must use this option at your own risk. It is necessary to shutdown any running JVM process before applying patch. Start affected JVM process after fix.
2727

28+
If you want to automate patch job, use `--force-fix` option. With this option, this program will no longer prompt for confirmation.
29+
2830
`(mitigated)` tag will be displayed if `org/apache/logging/log4j/core/lookup/JndiLookup.class` entry is removed from JAR file.
2931

3032
If you add `--trace` option, this program will print all visited directories and files. Use this option only for debugging.
@@ -49,7 +51,7 @@ Completed in 0.42 seconds
4951

5052
### How it works
5153
Run in 5 steps:
52-
1. Find all .jar files recursively.
54+
1. Find all .jar, .war, .ear files recursively.
5355
2. Find `META-INF/maven/org.apache.logging.log4j/log4j-core/pom.properties` entry from JAR file.
5456
3. Read groupId, artifactId, and version.
5557
4. Compare log4j2 version and print vulnerable version.

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<modelVersion>4.0.0</modelVersion>
77
<groupId>com.logpresso</groupId>
88
<artifactId>log4j2-scanner</artifactId>
9-
<version>1.2.2</version>
9+
<version>1.2.3</version>
1010
<packaging>jar</packaging>
1111
<name>Logpresso Log4j2 Scanner</name>
1212

src/main/java/com/logpresso/scanner/Log4j2Scanner.java

+20-6
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,28 @@ public enum Status {
2626
private long scanDirCount = 0;
2727
private long scanFileCount = 0;
2828
private long vulnerableFileCount = 0;
29+
private long fixedFileCount = 0;
2930

3031
private Set<File> vulnerableFiles = new LinkedHashSet<File>();
3132

3233
public static void main(String[] args) {
3334
if (args.length < 1) {
34-
System.out.println("Logpresso CVE-2021-44228 Vulnerability Scanner 1.2.2 (2021-12-13)");
35-
System.out.println("Usage: log4j2-scan [--fix] [--trace] target_path");
35+
System.out.println("Logpresso CVE-2021-44228 Vulnerability Scanner 1.2.3 (2021-12-14)");
36+
System.out.println("Usage: log4j2-scan [--fix] [--force-fix] [--trace] target_path");
37+
System.out.println(" Do not use --force-fix unless you know what you are doing");
3638
return;
3739
}
3840

3941
boolean trace = false;
4042
boolean fix = false;
43+
boolean force = false;
4144

4245
for (int i = 0; i < args.length - 1; i++) {
4346
if (args[i].equals("--fix")) {
4447
fix = true;
48+
} else if (args[i].equals("--force-fix")) {
49+
fix = true;
50+
force = true;
4551
} else if (args[i].equals("--trace")) {
4652
trace = true;
4753
} else {
@@ -52,7 +58,7 @@ public static void main(String[] args) {
5258

5359
String path = args[args.length - 1];
5460

55-
if (fix) {
61+
if (fix && !force) {
5662
try {
5763
System.out.print("This command will remove JndiLookup.class from log4j2-core binaries. Are you sure [y/N]? ");
5864
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
@@ -83,20 +89,28 @@ public void run(File f, boolean fix, boolean trace) {
8389
System.out.println("Scanned " + scanDirCount + " directories and " + scanFileCount + " files");
8490
System.out.println("Found " + vulnerableFileCount + " vulnerable files");
8591
if (fix)
86-
System.out.println("Fixed " + vulnerableFiles.size() + " vulnerable files");
92+
System.out.println("Fixed " + fixedFileCount + " vulnerable files");
8793

8894
System.out.printf("Completed in %.2f seconds\n", elapsed / 1000.0);
8995
}
9096
}
9197

9298
private void fix(boolean trace) {
99+
if (!vulnerableFiles.isEmpty())
100+
System.out.println("");
101+
93102
for (File f : vulnerableFiles) {
94103
if (trace)
95104
System.out.println("Patching " + f.getAbsolutePath());
96105

97106
File backupFile = new File(f.getAbsolutePath() + ".bak");
98-
if (f.renameTo(backupFile))
107+
if (f.renameTo(backupFile)) {
99108
copyExceptJndiLookup(backupFile, f);
109+
fixedFileCount++;
110+
System.out.println("Fixed: " + f.getAbsolutePath());
111+
} else {
112+
System.out.println("Error: Cannot patch locked file " + f.getAbsolutePath());
113+
}
100114
}
101115
}
102116

@@ -359,7 +373,7 @@ private String loadVulnerableLog4jVersion(InputStream is) throws IOException {
359373

360374
private boolean isScanTarget(String name) {
361375
String loweredName = name.toLowerCase();
362-
return loweredName.endsWith(".jar") || loweredName.endsWith(".war");
376+
return loweredName.endsWith(".jar") || loweredName.endsWith(".war") || loweredName.endsWith(".ear");
363377
}
364378

365379
private boolean isVulnerable(int major, int minor, int patch) {

0 commit comments

Comments
 (0)