针对 log4shell 保护许多 log4j jar 的修补程序

hotfix securing many log4j jars against log4shell

我必须保护一些服务器免受 CVE-2021-44228 aka log4shell 的侵害。这些机器是 运行 Linux 并且到处都有大量的 log4j jar,一些来自应用程序服务器,一些来自遗留软件等。我担心不可能将它们全部更新到最新的 log4j.

但是,如果我理解正确,可以通过像这样删除受影响的 class 来防止 log4j 1.x 中的 log4shell:

zip -q -d log4j.jar org/apache/log4j/net/JMSAppender.class

在 log4j 中 2.x 像这样(参见 logging.apache.org):

zip -q -d log4j-core.jar org/apache/logging/log4j/core/lookup/JndiLookup.class

我想到了一个 shell 脚本来查找所有 log4j jar,可能是这样的:

find / -name "log4j*.jar"

并对所有结果调用这两个 zip 命令。不幸的是,我不擅长 shell 编写脚本。有人可以根据这个想法写一个脚本吗?

警告:

好吧,这是我自己的镜头。不是很优雅(相当重复),但输出颜色很好。如果 log4j 版本受到威胁,它只会删除 class(FIX_VERSION="2.17.1" 也可以防止 CVE-2021-45105) and CVE-2021-44832.

#!/bin/bash
echo " ┌──────────────────────────────────────────────┐"
echo " │ This is securelog4j version 1.5              │"
echo " │  │"
echo " └──────────────────────────────────────────────┘"
preventLog4Shell() {
    readonly FIX_VERSION="2.17.1" # see https://logging.apache.org/log4j/2.x/security.html
    readonly VERSION=$(unzip -p "" META-INF/MANIFEST.MF | grep Implementation-Version | cut -d " " -f2 | sed -r 's/\s*$//g')
    readonly RED="3[31m"
    readonly GREEN="3[32m"
    readonly YELLOW="3[33m"
    readonly BOLD=$(tput bold)
    readonly RESET=$(tput sgr0)
    readonly SMALL=$(echo -e $VERSION'\n'$FIX_VERSION | sort -V | head -n1)
    if [ "$SMALL" = "$FIX_VERSION" ]
    then
        echo -e $BOLD$GREEN good version $VERSION:$RESET 
    else
        hasJms=$(zip -v "" org/apache/log4j/net/JMSAppender.class | grep including)
        hasJndi=$(zip -v "" org/apache/logging/log4j/core/lookup/JndiLookup.class | grep including)
        if [ "$hasJms" ] || [ "$hasJndi" ]
        then
            if [ "$hasJms" ]
            then
                zip -d -q "" org/apache/log4j/net/JMSAppender.class
                hasJms=$(zip -v "" org/apache/log4j/net/JMSAppender.class | grep including)
                if [ "$hasJms" ]
                then
                    echo -e $BOLD$RED unable to secure:$RESET 
                else
                    echo -e $BOLD$YELLOW secured:$RESET 
                fi
            else 
                zip -d -q "" org/apache/logging/log4j/core/lookup/JndiLookup.class
                hasJndi=$(zip -v "" org/apache/logging/log4j/core/lookup/JndiLookup.class | grep including)
                if [ "$hasJndi" ]
                then
                    echo -e $BOLD$RED unable to secure:$RESET 
                else
                    echo -e $BOLD$YELLOW secured:$RESET 
                fi
            fi
            else echo -e $BOLD$GREEN found clean:$RESET 
        fi
    fi
}

if command -v zip &> /dev/null
then 
    export -f preventLog4Shell
    find / -name "log4j*.jar" -exec bash -c 'preventLog4Shell "[=10=]"' {} \;
else 
    echo You have to install “zip” first.
fi

输出如下所示:

截图备注:

  • 之前已经清理了两个文件,因此“发现干净”。
  • 最后一个文件是只读的,因此“无法保护”。