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

3yhwsihp  于 2022-11-06  发布在  Shell
关注(0)|答案(1)|浏览(166)

我必须保护一些服务器免受CVE-2021-44228 aka log4shell的攻击。这些机器运行的是Linux,到处都是大量的log4j jar文件,有些来自应用服务器,有些来自遗留软件等。我担心不可能将所有这些文件都更新到最新的log4j。
但是,如果我理解正确的话,可以通过删除受影响的类来防止log4j 1.x中的log 4shell,如下所示:

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失去所有JNDI查找和/或JMS功能。
  • log4j 1.x有其他安全问题。所以最好升级到最新的logj 2.x。
cyvaqqii

cyvaqqii1#

这是我自己的尝试。不是很优雅(相当重复),但输出颜色很好。而且它只在log4j版本被破坏时删除类(FIX_VERSION=“2.17.1”也可以防止CVE-2021-45105)和CVE-2021-44832


# !/bin/bash

echo " ┌──────────────────────────────────────────────┐"
echo " │ This is securelog4j version 1.5              │"
echo " │ https://stackoverflow.com/a/70362694/1948252 │"
echo " └──────────────────────────────────────────────┘"
preventLog4Shell() {
    readonly FIX_VERSION="2.17.1" # see https://logging.apache.org/log4j/2.x/security.html
    readonly VERSION=$(unzip -p "$1" META-INF/MANIFEST.MF | grep Implementation-Version | cut -d " " -f2 | sed -r 's/\s*$//g')
    readonly RED="\033[31m"
    readonly GREEN="\033[32m"
    readonly YELLOW="\033[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 $1
    else
        hasJms=$(zip -v "$1" org/apache/log4j/net/JMSAppender.class | grep including)
        hasJndi=$(zip -v "$1" org/apache/logging/log4j/core/lookup/JndiLookup.class | grep including)
        if [ "$hasJms" ] || [ "$hasJndi" ]
        then
            if [ "$hasJms" ]
            then
                zip -d -q "$1" org/apache/log4j/net/JMSAppender.class
                hasJms=$(zip -v "$1" org/apache/log4j/net/JMSAppender.class | grep including)
                if [ "$hasJms" ]
                then
                    echo -e $BOLD$RED unable to secure:$RESET $1
                else
                    echo -e $BOLD$YELLOW secured:$RESET $1
                fi
            else 
                zip -d -q "$1" org/apache/logging/log4j/core/lookup/JndiLookup.class
                hasJndi=$(zip -v "$1" org/apache/logging/log4j/core/lookup/JndiLookup.class | grep including)
                if [ "$hasJndi" ]
                then
                    echo -e $BOLD$RED unable to secure:$RESET $1
                else
                    echo -e $BOLD$YELLOW secured:$RESET $1
                fi
            fi
            else echo -e $BOLD$GREEN found clean:$RESET $1
        fi
    fi
}

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

输出如下所示:

截图备注:

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

相关问题