log4j JNDI漏洞CVE-2021-44228 针对于不同版本的解决方案

问题描述:

Apache Log4j 2存在远程代码执行漏洞,攻击者可通过构造恶意请求利用该漏洞实现在目标服务器上执行任意代码

漏洞影响版本:2.0 <= Apache Log4j 2 <= log4j-2.15.0-rc1

看一下目前log4j的maven官方仓库版本,几乎是主流常用版本都受影响
在这里插入图片描述

解决方案:

试了一下网上几种方案,绝大部分都是解决 >=2.10 版本的方案 :

1、第一种方案,更新log4j版本
https://github.com/apache/logging-log4j2/releases/tag/log4j-2.15.0-rc2

2、现在普遍的修复方案主要集中在配置修改上在项目的log4j2.component.properties配置文件中添加配置 :

log4j2.formatMsgNoLookups = true

在这里插入图片描述在这里插入图片描述

3、在JVM参数启动项上加入

-Dlog4j2.formatMsgNoLookups=true

4、对于 2.0-beta9 ~ 2.10.0版本的官方解决方案是删掉log4j-core jar包中的jndi相关的class类
在这里插入图片描述


测试不同版本解决方案

首先去掉boot自带的logback依赖,添加log4j依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions><!-- 去掉springboot默认配置 -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency> <!-- 引入log4j2依赖 -->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

2.10以上用人气较高的 2.12.1测试
在这里插入图片描述
在这里插入图片描述
编写客户端进行测试(服务端搭建这里不演示)

public class Client {
    private static final Logger logger = LogManager.getLogger();
    public static void main(String[] args) throws Exception {
        // 这里触发有个前置条件,jdk8开始,jndi的trustURLCodebase默认为false,这里需要手动开启。
        System.setProperty("com.sun.jndi.rmi.object.trustURLCodebase", "true");
        logger.info("${jndi:rmi://127.0.0.1:xx/xxx}");
    }
}
// ......
try {
    //调用计算器
    Process rt=Runtime.getRuntime().exec("calc");
    Process rt2=Runtime.getRuntime().exec("notepad");
}
catch (IOException ex) {
    ex.printStackTrace();
}

运行客户端,可以看到堆栈信息输出报错,并且弹出了在这里插入图片描述

方法2、3配置好后,可以在控制台观察到,原本的指令变成了普通的日志输出。
在这里插入图片描述
方法4 针对于2.0-beta9 ~ 2.10.0 版本测试 :

这里就有点麻烦,需要把jar包里面的log4j-core类中的对应class文件删掉后再打包

参考这位大佬博客
Springboot替换lib里面的引用jar

删掉之前的log4j-core
在这里插入图片描述
删掉JndiLookup.class后
在这里插入图片描述
测试打包好后的jar包文件
在这里插入图片描述
控制台运行,成功打印注入指令,不再跳转
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/smlie_shuihan/article/details/121893194