fastjson漏洞--以运维角度进行修复
文章目录
- 前言
- 一、漏洞详情
- 二、修复过程
- 1.通过脚本方式修复
- 1.1.脚本修复原理
- 1.2.脚本演示
- 1.3.执行脚本
- 2. 手动升级包
- 2.1.修复步骤
- 2.2.遇到的问题
前言
该漏洞是三个月前由安全团队扫描出来的,主要影响是: FastJSON是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。由于具有执行效率高的特点,应用范围广泛。FastJSON 存在反序列化远程代码执行漏洞,漏洞成因是Fastjson autoType开关的限制可被绕过,然后反序列化有安全风险的类。攻击者利用该漏洞可实现在目标机器上的远程代码执行。本文主要从运维侧修复手段介绍了该漏洞的两种修复方式。
提示:以下是本篇文章正文内容,下面案例如果不放心,先自行在测试环境验证,验证通过后再上生产环境操作
一、漏洞详情
FastJSON是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。由于具有执行效率高的特点,应用范围广泛。 FastJSON 存在反序列化远程代码执行漏洞,漏洞成因是Fastjson autoType开关的限制可被绕过,然后反序列化有安全风险的类。攻击者利用该漏洞可实现在目标机器上的远程代码执行。 主要影响FastJSON<=1.2.80 版本
二、修复过程
修复过程涉及到服务重启,请自行确认重启时间
1.通过脚本方式修复
该版本涉及autotype行为变更,在某些场景会出现不兼容的情况
注意事项:1、该脚本适合以 lib 库形式依赖 fastjson-*.jar 包的Java微服务包,如 Springboot 打包的 jar 包。2、该脚本不支持直接打包 fastjson class 的 jar 包,即 jar 包中存在 "com/alibaba/fastjson/..." 类似结构,需要从代码升级修复。3、该脚本不支持docker 镜像包据说有签名校验,直接改镜像文件可能会导致不可用。由于环境所限,这种情况没有验证过。4、脚本适合 fastjson 全部版本。
1.1.脚本修复原理
1、扫描当前服务器所有jar包。
2、查看jar包依赖包中是否包括 fastjson-*.jar 包。
3、如果有,更新该依赖包为fastjson-1.2.83.jar。同时脚本在更新 jar 包之前,会备份该jar包到/export/fastjson_repair_workspace/backup_jars/ 目录下,防止替换后服务启动失败,后续方便回滚。
1.2.脚本演示
#!/bin/bash
# 2024-09-02
WORK_DIR="/root/fastjson_repair_workspace"
BACKUP_DIR="$WORK_DIR/backup_jars"
ALL_JARS="$WORK_DIR/all_jars.txt"
TARGET_JARS="$WORK_DIR/target_jars.txt"
DONE_FILE="$WORK_DIR/DONEFILE"
TARGET_VERSION="fastjson-1.2.81.jar" #修改为当前jar包中对应的fastjson包名,去lib目录下确认即可
FIX_VERSION="fastjson-1.2.83.jar" #要确认升级的包#检查脚本执行环境是否具备超管权限
function check_env() {mkdir -p $BACKUP_DIRif [ $? -eq 1 ];thenecho "Permission denied, need root user"exitfi
}#检查脚本执行用户,必须以服务器管理员root用户执行
function check_user() {if [ "$(whoami)" != "root" ];thenecho "Need root user"exitfi
}#检查是否有zip命令
function check_zip() {which zip &> /dev/nullif [ $? -eq 1 ];thenecho "Install zip first"exitfi
}
#检查是否有unzip命令
function check_unzip() {which unzip &> /dev/nullif [ $? -eq 1 ];thenecho "Install unzip first"exitfi
}
#检查当前服务器环境中是否存在要修复的fastjson包
check_fastjson_jar() {if [ ! -f ./$FIX_VERSION ];thenecho "Fix task need $FIX_VERSION"exitfi
}#环境检查
function check_find_env() {check_usercheck_envcheck_zip
}function check_fix_env() {check_usercheck_envcheck_zipcheck_unzipcheck_fastjson_jar
}#查看服务器所有jar包
function find_all_jars() {echo "Finding all jars ..."mkdir -p $WORK_DIRif [ -f $ALL_JARS ];thenif [ -f $DONE_FILE ];thenecho "Jarlist exsit: $ALL_JARS"elserm -f $ALL_JARSecho "Jarlist broken, run again"exitfielsefind / -path "$BACKUP_DIR" -prune -o -path "/proc" -prune -o -print | grep "\.jar$" > $ALL_JARSif [ $? -eq 0 ];thentouch $DONE_FILEfifiecho "Find " $(wc -l $ALL_JARS|awk '{print$1}') " jars."echo
}function find_lib_jars() {echo $* > /tmp/tmp_jarlistwhile read line;dofastjson_version=$(echo $line|awk -F'/' '{print$NF}')if [[ "$fastjson_version" < "$TARGET_VERSION" ]];thenreturn 1fidone < /tmp/tmp_jarlistreturn 0
}#检查查找出来的jar包是否包含target_version版本的包
function find_target_jars() {echo "Finding target jars ..."> $TARGET_JARSnum=1while read jar;doret=$(unzip -l $jar 2> /dev/null | grep fastjson.*jar| grep -v "Archive")if [ -n "$ret" ];thenfind_lib_jars $retif [ $? -eq 1 ];thenecho "Find [$num] $jar"echo $jar >> $TARGET_JARS((num++))fielseret=$(unzip -l $jar 2> /dev/null | grep "com/alibaba/fastjson" |grep -v "Archive")if [ -n "$ret" ];thenecho "[NOT support] Find [$num] $jar"((num++))fifidone < $ALL_JARSecho "Find " $(wc -l $TARGET_JARS|awk '{print$1}') " target jars."echo
}#替换jar包
function update_jars() {jarpath=$1filepath=$2jarname=$(basename $jarpath)filedir=$(dirname $filepath)mkdir -p /export/fastjson_repair_workspace/libjars/cp $jarpath /export/fastjson_repair_workspace/libjars/$jarnamemkdir -p /export/fastjson_repair_workspace/libjars/$filedircp ./$FIX_VERSION /export/fastjson_repair_workspace/libjars/$filedircd /export/fastjson_repair_workspace/libjars/zip -d $jarname $filepathecho "adding: $filedir/$FIX_VERSION"jar -uf0 $jarname $filedir/$FIX_VERSIONcp -f $jarname $jarpathcd - &> /dev/nullrm -rf /export/fastjson_repair_workspace/libjars/*
}
#修复jar包
function fix_lib_jars() {jarpath=$1if [ ! -n "$(unzip -l $jarpath 2> /dev/null | grep fastjson.*jar | grep -v "Archive")" ];thenecho "[WARN] NOT Fixed"fiunzip -l $jarpath 2> /dev/null | grep fastjson.*jar | grep -v "Archive" > /tmp/tmp_jarlistwhile read line;dofastjson_version=$(echo $line|awk -F'/' '{print$NF}')if [[ "$fastjson_version" < "$TARGET_VERSION" ]];thenlibjar=$(echo $line|awk '{print$NF}')update_jars $jarpath $libjarecho "Fixed"fidone < /tmp/tmp_jarlist
}function fix_target_jars() {if [ ! -f $TARGET_JARS ];thenecho "run check first."exitfiecho "Fix target jars ..."echonum=1while read jar;doecho "==== $num ===="echo "Backup $jar"mkdir -p ${BACKUP_DIR}$(dirname $jar)cp -f $jar ${BACKUP_DIR}$(dirname $jar)echo "Fix $(basename $jar) ..."fix_lib_jars $jarecho((num++))done < $TARGET_JARSecho "Fixed " $(wc -l $TARGET_JARS|awk '{print$1}') " target jars."mv -f $TARGET_JARS ${TARGET_JARS}.fixed.$(date +%Y%m%d_%H%M%S)
}function usage() {echo "Version 1.0.0"echo "Usage: $0 [check|fix]"
}function check() {check_find_envfind_all_jarsfind_target_jars
}function fix() {check_fix_envfix_target_jars
}if [ "$1" = "check" ];thencheck
elif [ "$1" = "fix" ];thenfix
elseusage
fi
1.3.执行脚本
``
[root@prometheus ~]# chmod +x fastjson_repair.sh
[root@prometheus ~]# sh fastjson_repair.sh
Version 1.0.0
Usage: fastjson_repair.sh [check|fix] #check是检查包 fix是对fastjson包进行升级处理
[root@prometheus ~]# sh fastjson_repair.sh check
Finding all jars ...
Find 751 jars.Finding target jars ...
Find [1] /export/server/icity-client/icity-client-system-1.0.0.jar
Find [2] /export/server/icity-server/icity-server-system-1.0.0.jar
Find 2 target jars. #发现了两个fastjson版本低于1.28.3版本的包
执行 ./fast_repaire.sh check脚本如下所示
vim java微服务包 查看jar中包含的fastjson包,果然低于1.28.3版本
执行fix命令
2. 手动升级包
有时,java微服务包使用上述脚本无法完成修复,而且研发不能快速配合修复,那么从运维角度出发进行修复
2.1.修复步骤
如下图所示
1、拷贝需要修复的jar包至某一个空目录下2、执行jar -xf xxxx.jar 对包进行解压3、将解压后META-INF目录下的MANIFEST.MF文件 移动到跟META-INF目录平级 mv META-INF/MANIFEST.MF ../4、cd BOOT-INF/lib/目录下,将低版本的fastjson包移除,将高版本的包移动到该目录下5、执行打包命令 jar -cfm0 xxx.jar /xxx/xxx/MANIFEST.MF *6、将第5步构建好的包移动到对应的服务目录下,重新启动该服务,观察日志,没有报错;且vim xxxx.jar 发现fastjson包为替换后的版本的包,则修复成功
将上述低版本的fastjson jar包从当前lib目录下删除,将fastjson-1.2.83高版本的包拷贝到lib目录下
重新执行打包命令构建新的java微服务包
将上述新构建好的jar包拷贝到原来的位置,并重新启动服务,查看服务日志是否报错,如果没报错,则fastjson漏洞修复完成
2.2.遇到的问题
在执行java微服务包构建时,遇到了下方的错误The calling method's class, org.apache.catalina.authenticator.AuthenticatorBase, was loaded from the following location:jar:file:/export/system-server-1.0.0.jar!/BOOT-INF/lib/tomcat-embed-core-9.0.60.jar!/org/apache/catalina/authenticator/AuthenticatorBase.classThe called method's class, javax.servlet.ServletContext, is available from the following locations:jar:file:/export/system-server-1.0.0.jar!/BOOT-INF/lib/servlet-api-6.0.53.jar!/javax/servlet/ServletContext.classjar:file:/export/system-server-1.0.0.jar!/BOOT-INF/lib/tomcat-embed-core-9.0.60.jar!/javax/servlet/ServletContext.classThe called method's class hierarchy was loaded from the following locations:javax.servlet.ServletContext: jar:file:/export/system-server-1.0.0.jar!/BOOT-INF/lib/servlet-api-6.0.53.jar!/
解决方法:将BOOT-INF/lib/目录下的servlet-api-6.0.53.jar包替换为一个高版本的包,例如servlet-api-7.0.0.jar,然后重新执行上述构建即可解决。https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-servlet-api/7.0.0 在上述地址可以下载这个包,重命名为对应的名字即可使用
至此,fastjson低版本漏洞修复的两种方法介绍完毕