Skip to content

Commit cd56bce

Browse files
committed
Added --restore option. v2.5.0
1 parent 9052bed commit cd56bce

File tree

7 files changed

+259
-55
lines changed

7 files changed

+259
-55
lines changed

README.md

+20-11
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
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. It also detects CVE-2021-45046 (log4j 2.15.0), CVE-2021-45105 (log4j 2.16.0), CVE-2021-4104 (log4j 1.x), and CVE-2021-42550 (logback 0.9-1.2.7) vulnerabilities.
44

55
### Download
6-
* [log4j2-scan 2.4.2 (Windows x64, 7z)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v2.4.2/logpresso-log4j2-scan-2.4.2-win64.7z)
7-
* [log4j2-scan 2.4.2 (Windows x64, zip)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v2.4.2/logpresso-log4j2-scan-2.4.2-win64.zip)
6+
* [log4j2-scan 2.5.0 (Windows x64, 7z)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v2.5.0/logpresso-log4j2-scan-2.5.0-win64.7z)
7+
* [log4j2-scan 2.5.0 (Windows x64, zip)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v2.5.0/logpresso-log4j2-scan-2.5.0-win64.zip)
88
* If you get `VCRUNTIME140.dll not found` error, install [Visual C++ Redistributable](https://docs.microsoft.com/en-US/cpp/windows/latest-supported-vc-redist?view=msvc-170).
99
* If native executable doesn't work, use the JAR instead. 32bit is not supported.
1010
* 7zip is available from www.7zip.org, and is open source and free.
11-
* [log4j2-scan 2.4.2 (Linux x64)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v2.4.2/logpresso-log4j2-scan-2.4.2-linux.tar.gz)
12-
* [log4j2-scan 2.4.2 (Linux aarch64)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v2.4.2/logpresso-log4j2-scan-2.4.2-linux-aarch64.tar.gz)
11+
* [log4j2-scan 2.5.0 (Linux x64)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v2.5.0/logpresso-log4j2-scan-2.5.0-linux.tar.gz)
12+
* [log4j2-scan 2.5.0 (Linux aarch64)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v2.5.0/logpresso-log4j2-scan-2.5.0-linux-aarch64.tar.gz)
1313
* If native executable doesn't work, use the JAR instead. 32bit is not supported.
14-
* [log4j2-scan 2.4.2 (Mac OS)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v2.4.2/logpresso-log4j2-scan-2.4.2-darwin.tar.gz)
15-
* [log4j2-scan 2.4.2 (Any OS, 20KB)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v2.4.2/logpresso-log4j2-scan-2.4.2.jar)
14+
* [log4j2-scan 2.5.0 (Mac OS)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v2.5.0/logpresso-log4j2-scan-2.5.0-darwin.tar.gz)
15+
* [log4j2-scan 2.5.0 (Any OS, 20KB)](https://github.com/logpresso/CVE-2021-44228-Scanner/releases/download/v2.5.0/logpresso-log4j2-scan-2.5.0.jar)
1616

1717
### Build
1818
* [How to build Native Image](https://github.com/logpresso/CVE-2021-44228-Scanner/wiki/FAQ#how-to-build-native-image)
@@ -22,7 +22,7 @@ Just run log4j2-scan.exe or log4j2-scan with target directory path. The logpress
2222

2323
Usage
2424
```
25-
Logpresso CVE-2021-44228 Vulnerability Scanner 2.4.2 (2021-12-21)
25+
Logpresso CVE-2021-44228 Vulnerability Scanner 2.5.0 (2021-12-21)
2626
Usage: log4j2-scan [--scan-log4j1] [--fix] target_path1 target_path2
2727
2828
-f [config_file_path]
@@ -41,20 +41,26 @@ Usage: log4j2-scan [--scan-log4j1] [--fix] target_path1 target_path2
4141
With --scan-log4j1 option, it also removes JMSAppender.class, SocketServer.class, SMTPAppender.class, SMTPAppender$1.class
4242
--force-fix
4343
Do not prompt confirmation. Don't use this option unless you know what you are doing.
44+
--restore [backup_file_path]
45+
Unfix JAR files using zip archived file.
4446
--all-drives
4547
Scan all drives on Windows
4648
--drives c,d
4749
Scan specified drives on Windows. Spaces are not allowed here.
4850
--no-symlink
4951
Do not detect symlink as vulnerable file.
5052
--exclude [path_prefix]
51-
Full paths of directories whose absolute path starts with the specified value will be excluded. Does not support relative paths. You can specify multiple --exclude [path_prefix] pairs
53+
Full paths of directories whose absolute path starts with the specified value will be excluded.
54+
Does not support relative paths. You can specify multiple --exclude [path_prefix] pairs
5255
--exclude-config [config_file_path]
5356
Specify exclude path list in text file. Paths should be separated by new line. Prepend # for comment.
5457
--exclude-pattern [pattern]
55-
Exclude specified paths of directories by pattern. Supports fragments. You can specify multiple --exclude-pattern [pattern] pairs (non regex)
58+
Exclude specified paths of directories by pattern. Supports fragments.
59+
You can specify multiple --exclude-pattern [pattern] pairs (non regex)
5660
--exclude-fs nfs,tmpfs
5761
Exclude paths by file system type. nfs, nfs3, nfs4, cifs, tmpfs, devtmpfs, fuse.sshfs and iso9660 is ignored by default.
62+
--syslog-udp [host:port]
63+
Send alert to remote syslog host for vulnerable or potentially vulnerable binaries
5864
--report-csv
5965
Generate log4j2_scan_report_yyyyMMdd_HHmmss.csv in working directory if not specified otherwise via --report-path [path]
6066
--report-json
@@ -87,10 +93,12 @@ On Linux
8793
```
8894
On UNIX (AIX, Solaris, and so on)
8995
```
90-
java -jar logpresso-log4j2-scan-2.4.2.jar [--fix] target_path
96+
java -jar logpresso-log4j2-scan-2.5.0.jar [--fix] target_path
9197
```
9298

93-
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. Depending the Operating System:
99+
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. All .bak files are archived into the single zip file which is named by `log4j2_scan_backup_yyyyMMdd_HHmmss.zip`, then deleted safely. In most environments, JNDI lookup feature will not be used. However, you must use this option at your own risk. You can easily restore original vulnerable JAR files using `--restore` option.
100+
101+
Depending the Operating System:
94102

95103
- Windows: It is necessary to shutdown any running JVM process before applying patch due to lock files. Start affected JVM process after fix.
96104
- Linux/macOS: Apply patch, restart the JVM after
@@ -127,6 +135,7 @@ Run in 5 steps:
127135
4. Compare log4j2 version and print vulnerable version.
128136
5. If --fix option is used, backup vulnerable file and patch it.
129137
* For example, original vulnerable.jar is copied to vulnerable.jar.bak
138+
6. Archive all backup files into the zip file `log4j2_scan_backup_yyyyMMdd_HHmmss.zip`, then delete .bak files.
130139

131140
### Exit code for automation
132141
* -1 failed to run

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>2.4.3</version>
9+
<version>2.5.0</version>
1010
<packaging>jar</packaging>
1111
<name>Logpresso Log4j2 Scanner</name>
1212

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

+72-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class Configuration {
3636
private boolean oldExitCode = false;
3737
private Charset zipCharset = null;
3838

39+
private File backupPath = null;
3940
private String reportPath = null;
4041
private String reportDir = null;
4142
private InetSocketAddress udpSyslogAddr = null;
@@ -67,20 +68,23 @@ public static void pringUsage() {
6768
"\tWith --scan-log4j1 option, it also removes JMSAppender.class, SocketServer.class, SMTPAppender.class, SMTPAppender$1.class");
6869
System.out.println("--force-fix");
6970
System.out.println("\tDo not prompt confirmation. Don't use this option unless you know what you are doing.");
71+
System.out.println("--restore [backup_file_path]");
72+
System.out.println("\tUnfix JAR files using zip archived file.");
7073
System.out.println("--all-drives");
7174
System.out.println("\tScan all drives on Windows");
7275
System.out.println("--drives c,d");
7376
System.out.println("\tScan specified drives on Windows. Spaces are not allowed here.");
7477
System.out.println("--no-symlink");
7578
System.out.println("\tDo not detect symlink as vulnerable file.");
7679
System.out.println("--exclude [path_prefix]");
77-
System.out.println("\tFull paths of directories whose absolute path starts with the specified value will be excluded. Does not support relative paths. You can specify multiple --exclude [path_prefix] pairs");
80+
System.out.println("\tFull paths of directories whose absolute path starts with the specified value will be excluded.\n"
81+
+ "\tDoes not support relative paths. You can specify multiple --exclude [path_prefix] pairs");
7882
System.out.println("--exclude-config [config_file_path]");
7983
System.out.println(
8084
"\tSpecify exclude path list in text file. Paths should be separated by new line. Prepend # for comment.");
8185
System.out.println("--exclude-pattern [pattern]");
82-
System.out.println(
83-
"\tExclude specified paths of directories by pattern. Supports fragments. You can specify multiple --exclude-pattern [pattern] pairs (non regex)");
86+
System.out.println("\tExclude specified paths of directories by pattern. Supports fragments.\n"
87+
+ "\tYou can specify multiple --exclude-pattern [pattern] pairs (non regex)");
8488
System.out.println("--exclude-fs nfs,tmpfs");
8589
System.out.println(
8690
"\tExclude paths by file system type. nfs, nfs3, nfs4, cifs, tmpfs, devtmpfs, fuse.sshfs and iso9660 is ignored by default.");
@@ -120,6 +124,16 @@ public static Configuration parseArguments(String[] args) throws Exception {
120124
} else if (args[i].equals("--force-fix")) {
121125
c.fix = true;
122126
c.force = true;
127+
} else if (args[i].equals("--restore")) {
128+
verifyArgument(args, i, "Backup file path", "Specify backup file path.");
129+
c.backupPath = new File(args[i + 1]);
130+
if (!c.backupPath.exists())
131+
throw new IllegalArgumentException("Backup file not found: " + c.backupPath.getAbsolutePath());
132+
133+
if (!c.backupPath.getName().toLowerCase().endsWith(".zip"))
134+
throw new IllegalArgumentException("Backup file should be zip format: " + c.backupPath.getAbsolutePath());
135+
136+
i++;
123137
} else if (args[i].equals("--debug")) {
124138
c.debug = true;
125139
} else if (args[i].equals("--trace")) {
@@ -300,7 +314,8 @@ else if (!reportFile.isDirectory())
300314
if (c.allDrives && !c.driveLetters.isEmpty())
301315
throw new IllegalArgumentException("Cannot specify both --all-drives and --drives options.");
302316

303-
if (!c.allDrives && c.driveLetters.isEmpty() && c.includeFilePath == null && c.targetPaths.isEmpty())
317+
if (!c.allDrives && c.driveLetters.isEmpty() && c.includeFilePath == null && c.targetPaths.isEmpty()
318+
&& c.getBackupPath() == null)
304319
throw new IllegalArgumentException("Specify scan target path.");
305320

306321
if (c.includeFilePath != null && c.allDrives)
@@ -309,9 +324,58 @@ else if (!reportFile.isDirectory())
309324
if (c.includeFilePath != null && !c.driveLetters.isEmpty())
310325
throw new IllegalArgumentException("Cannot specify both --drives and -f options.");
311326

327+
if (c.getBackupPath() != null) {
328+
// cannot use any other options
329+
rejectInvalidOptionForRestore(c);
330+
}
331+
312332
return c;
313333
}
314334

335+
private static void rejectInvalidOptionForRestore(Configuration c) {
336+
if (!c.targetPaths.isEmpty())
337+
throw new IllegalArgumentException("Cannot specify scan target path with --restore option.");
338+
339+
if (c.getIncludeFilePath() != null)
340+
throw new IllegalArgumentException("Cannot use --restore option with -f option.");
341+
342+
if (c.isScanZip())
343+
throw new IllegalArgumentException("Cannot use --restore option with --scan-zip option.");
344+
345+
if (c.isScanForLog4j1())
346+
throw new IllegalArgumentException("Cannot use --restore option with --scan-log4j1 option.");
347+
348+
if (c.isScanForLogback())
349+
throw new IllegalArgumentException("Cannot use --restore option with --scan-logback option.");
350+
351+
if (c.isFix())
352+
throw new IllegalArgumentException("Cannot use --restore option with --fix option.");
353+
354+
if (c.isForce())
355+
throw new IllegalArgumentException("Cannot use --restore option with --force-fix option.");
356+
357+
if (c.isAllDrives())
358+
throw new IllegalArgumentException("Cannot use --restore option with --all-drives option.");
359+
360+
if (!c.getDriveLetters().isEmpty())
361+
throw new IllegalArgumentException("Cannot use --restore option with --drives option.");
362+
363+
if (c.isReportCsv())
364+
throw new IllegalArgumentException("Cannot use --restore option with --report-csv option.");
365+
366+
if (c.isReportJson())
367+
throw new IllegalArgumentException("Cannot use --restore option with --report-json option.");
368+
369+
if (c.getReportDir() != null)
370+
throw new IllegalArgumentException("Cannot use --restore option with --report-dir option.");
371+
372+
if (c.getReportPath() != null)
373+
throw new IllegalArgumentException("Cannot use --restore option with --report-path option.");
374+
375+
if (c.getUdpSyslogAddr() != null)
376+
throw new IllegalArgumentException("Cannot use --restore option with --syslog-udp option.");
377+
}
378+
315379
private static InetSocketAddress parseAddress(String s) {
316380
int portNum = 514;
317381
int p = s.indexOf(':');
@@ -467,6 +531,10 @@ public boolean isOldExitCode() {
467531
return oldExitCode;
468532
}
469533

534+
public File getBackupPath() {
535+
return backupPath;
536+
}
537+
470538
public String getReportPath() {
471539
return reportPath;
472540
}

0 commit comments

Comments
 (0)