|
| 1 | + |
| 2 | +// Copyright 2021 Google LLC |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | +//////////////////////////////////////////////////////////////////////////////// |
| 17 | +import com.code_intelligence.jazzer.api.FuzzedDataProvider; |
| 18 | +import org.apache.logging.log4j.Level; |
| 19 | +import org.apache.logging.log4j.LogManager; |
| 20 | +import org.apache.logging.log4j.Logger; |
| 21 | +import org.apache.logging.log4j.core.appender.FileAppender; |
| 22 | +import org.apache.logging.log4j.core.config.Configurator; |
| 23 | +import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder; |
| 24 | +import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder; |
| 25 | +import org.apache.logging.log4j.core.config.builder.impl.DefaultConfigurationBuilder; |
| 26 | +import org.apache.logging.log4j.status.StatusLogger; |
| 27 | + |
| 28 | +// This fuzzer reproduces the log4j RCE vulnerability CVE-2021-44228. |
| 29 | +public class Log4jFuzzer { |
| 30 | + private final static Logger log = LogManager.getLogger(Log4jFuzzer.class.getName()); |
| 31 | + |
| 32 | + public static void fuzzerTestOneInput(FuzzedDataProvider data) { |
| 33 | + log.error(data.consumeRemainingAsString()); |
| 34 | + } |
| 35 | + |
| 36 | + public static void fuzzerInitialize() { |
| 37 | + // Install a logger that constructs the log message, but never prints it. |
| 38 | + // This noticeably increases the fuzzing performance |
| 39 | + DefaultConfigurationBuilder configBuilder = new DefaultConfigurationBuilder(); |
| 40 | + AppenderComponentBuilder fuzzingAppender = configBuilder.newAppender("nullAppender", FileAppender.PLUGIN_NAME); |
| 41 | + fuzzingAppender.addAttribute("fileName", "/dev/null"); |
| 42 | + configBuilder.add(fuzzingAppender); |
| 43 | + RootLoggerComponentBuilder rootLogger = configBuilder.newRootLogger(); |
| 44 | + rootLogger.add(configBuilder.newAppenderRef("nullAppender")); |
| 45 | + configBuilder.add(rootLogger); |
| 46 | + Configurator.reconfigure(configBuilder.build()); |
| 47 | + |
| 48 | + // Disable logging of exceptions caught in log4j itself. |
| 49 | + StatusLogger.getLogger().reset(); |
| 50 | + StatusLogger.getLogger().setLevel(Level.OFF); |
| 51 | + } |
| 52 | +} |
0 commit comments