#!/bin/sh # KrakenShield Java4Shell check (CVE-2026-114938), https://krakenshield.eu/java4shell-check/ # # Read-only. Lists the Java runtimes of this machine, asks each one for its version and for the CPU # features it really uses, then reads the options of the running Java processes. Nothing is installed, # changed or sent anywhere. # # Usage: sh java4shell-check.sh [--deep] [--lang fr|en] [/path/to/bin/java ...] # --deep also search the whole local file system for bin/java (slower) # Exit codes: 0 nothing to fix, 1 exploitable runtime found, 2 update or manual check needed. # # Java4Shell is a heap overflow in the x86-64 Base64 decoding intrinsic of HotSpot. It needs all of: # - an affected version: 17.0.6 to before 17.0.21.1, 21 before 21.0.13.1, 25 before 25.0.5.1, # 27 before 27.0.1.1, and every release of the unsupported 18, 19, 20, 22, 23, 24 and 26 lines; # - an x86-64 CPU with AVX-512 F, BW, VBMI and BMI2 (Intel Ice Lake and later, AMD Zen 4 and later); # - the intrinsic enabled (UseAVX=3 and UseBASE64Intrinsics, the defaults on those CPUs). # Java 8 and 11 are not affected (except the Oracle Java 8 Enterprise Performance Pack), nor is ARM64. set -u KS_VERSION=1.0.0 L=en case "${KS_LANG:-${LC_ALL:-${LC_MESSAGES:-${LANG:-}}}}" in fr*|FR*) L=fr ;; esac DEEP=0 EXTRA='' usage() { if [ "$L" = fr ]; then echo "Usage : sh java4shell-check.sh [--deep] [--lang fr|en] [/chemin/vers/bin/java ...]" echo " --deep cherche aussi bin/java dans tout le système de fichiers local (plus lent)" echo "Codes de sortie : 0 rien à corriger, 1 Java exploitable trouvé, 2 mise à jour ou vérification à faire." else echo "Usage: sh java4shell-check.sh [--deep] [--lang fr|en] [/path/to/bin/java ...]" echo " --deep also search the whole local file system for bin/java (slower)" echo "Exit codes: 0 nothing to fix, 1 exploitable runtime found, 2 update or manual check needed." fi } while [ $# -gt 0 ]; do case "$1" in --deep) DEEP=1 ;; --lang) shift; L=${1:-en} ;; --lang=*) L=${1#--lang=} ;; -h|--help) usage; exit 0 ;; *) EXTRA="$EXTRA $1" ;; esac shift done [ "$L" = fr ] || L=en m() { if [ "$L" = fr ]; then printf '%s' "$1"; else printf '%s' "$2"; fi; } if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then RED=$(printf '\033[1;31m'); GRN=$(printf '\033[1;32m'); YEL=$(printf '\033[1;33m'); BLD=$(printf '\033[1m'); DIM=$(printf '\033[2m'); RST=$(printf '\033[0m') else RED=''; GRN=''; YEL=''; BLD=''; DIM=''; RST='' fi run_java() { if command -v timeout >/dev/null 2>&1; then timeout 30 "$@"; else "$@"; fi } lower() { printf '%s' "$1" | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ,' 'abcdefghijklmnopqrstuvwxyz '; } # CPU feature list (cpuinfo, sysctl or the JVM log) -> yes | no | unknown cpu_status() { [ -n "$1" ] || { echo unknown; return; } f=" $(lower "$1") " for want in avx512f avx512bw bmi2; do case "$f" in *" $want "*) ;; *) echo no; return ;; esac done case "$f" in *" avx512vbmi "*|*" avx512_vbmi "*) echo yes ;; *) echo no ;; esac } is_x86() { case "$1" in x86_64|amd64|x64|i386|i686|x86) return 0 ;; *) return 1 ;; esac; } OS=$(uname -s 2>/dev/null || echo unknown) ARCH=$(uname -m 2>/dev/null || echo unknown) CPU_MODEL='' CPU_FLAGS='' if [ -r /proc/cpuinfo ]; then CPU_FLAGS=$(grep -E '^flags' /proc/cpuinfo | head -n1 | cut -d: -f2) CPU_MODEL=$(grep -E '^model name' /proc/cpuinfo | head -n1 | cut -d: -f2 | sed 's/^ *//') elif [ "$OS" = Darwin ]; then CPU_FLAGS=$(sysctl -n machdep.cpu.features machdep.cpu.leaf7_features 2>/dev/null | tr '\n' ' ') CPU_MODEL=$(sysctl -n machdep.cpu.brand_string 2>/dev/null) [ "$(sysctl -n hw.optional.arm64 2>/dev/null)" = 1 ] && ARCH=arm64 fi OS_CPU=$(cpu_status "$CPU_FLAGS") is_x86 "$ARCH" || OS_CPU=no RT_LIST='' add_rt() { p=$1 [ -n "$p" ] && [ -f "$p" ] && [ -x "$p" ] || return 0 rp=$(readlink -f "$p" 2>/dev/null || printf '%s' "$p") [ -n "$rp" ] || rp=$p case " $RT_LIST " in *" $rp "*) return 0 ;; esac RT_LIST="$RT_LIST $rp" } add_rt "$(command -v java 2>/dev/null || true)" [ -n "${JAVA_HOME:-}" ] && add_rt "$JAVA_HOME/bin/java" for p in /usr/lib/jvm/*/bin/java /usr/lib/jvm/*/jre/bin/java /usr/lib64/jvm/*/bin/java /usr/java/*/bin/java \ /usr/local/*/bin/java /usr/local/lib/jvm/*/bin/java /opt/*/bin/java /opt/*/*/bin/java /opt/*/*/*/bin/java \ /opt/*/*/*/*/bin/java /srv/*/*/bin/java /usr/share/*/jre/bin/java /usr/share/*/jbr/bin/java \ /Library/Java/JavaVirtualMachines/*/Contents/Home/bin/java "${HOME:-/nonexistent}"/.sdkman/candidates/java/*/bin/java \ "${HOME:-/nonexistent}"/.jdks/*/bin/java "${HOME:-/nonexistent}"/.gradle/jdks/*/bin/java; do add_rt "$p" done if [ -d /proc ]; then for e in /proc/[0-9]*/exe; do x=$(readlink "$e" 2>/dev/null) || continue case "$x" in */java) add_rt "$x" ;; esac done fi if [ "$DEEP" = 1 ]; then for p in $(find / -xdev -type f -path '*/bin/java' 2>/dev/null); do add_rt "$p"; done fi OLDIFS=$IFS IFS=' ' for p in $EXTRA; do add_rt "$p"; done IFS=$OLDIFS echo "${BLD}KrakenShield · $(m 'Test Java4Shell' 'Java4Shell check') (CVE-2026-114938) v$KS_VERSION${RST}" echo "Machine : $OS $ARCH${CPU_MODEL:+, $CPU_MODEL}" case "$OS_CPU" in yes) echo "$(m Processeur CPU) : ${YEL}$(m 'AVX-512 VBMI présent, le chemin vulnérable peut être utilisé' 'AVX-512 VBMI present, the vulnerable path can be used')${RST}" ;; no) if is_x86 "$ARCH"; then echo "$(m Processeur CPU) : ${GRN}$(m 'sans AVX-512 VBMI, faille non exploitable sur cette machine' 'no AVX-512 VBMI, flaw not exploitable on this machine')${RST}" else echo "$(m Processeur CPU) : ${GRN}$(m "architecture $ARCH, non concernée (seul x86-64 l'est)" "$ARCH architecture, not affected (only x86-64 is)")${RST}" fi ;; *) echo "$(m Processeur CPU) : $(m 'jeu d’instructions illisible ici, chaque JVM va le dire' 'feature list not readable here, each JVM will tell')" ;; esac echo N_VULN=0 N_TODO=0 N_OK=0 RT_VERDICTS='' say_verdict() { case "$1" in VULN) printf '%s[%s]%s %s\n' "$RED" "$(m 'VULNÉRABLE' 'VULNERABLE')" "$RST" "$2" ;; TODO) printf '%s[%s]%s %s\n' "$YEL" "$(m 'À METTRE À JOUR' 'UPDATE')" "$RST" "$2" ;; CHECK) printf '%s[%s]%s %s\n' "$YEL" "$(m 'À VÉRIFIER' 'CHECK')" "$RST" "$2" ;; OK) printf '%s[OK]%s %s\n' "$GRN" "$RST" "$2" ;; esac } detail() { printf ' %s\n' "$1"; } fix_for() { case "$1" in 17) echo 17.0.21.1 ;; 21) echo 21.0.13.1 ;; 25) echo 25.0.5.1 ;; 27) echo 27.0.1.1 ;; 8) echo 8u513-perf ;; *) echo '21.0.13.1 / 25.0.5.1' ;; esac } # MAJ MIN UPD PAT from a java.runtime.version such as 17.0.21.1+2-LTS, 21.0.11+10-LTS or 1.8.0_511-b33 parse_version() { v=$(printf '%s' "$1" | sed 's/^\([0-9][0-9._]*\).*/\1/') case "$v" in 1.*) MAJ=$(printf '%s' "$v" | cut -d. -f2); MIN=0; UPD=$(printf '%s' "$v" | sed -n 's/.*_\([0-9][0-9]*\).*/\1/p'); PAT=0 ;; *) MAJ=$(printf '%s' "$v" | cut -d. -f1); MIN=$(printf '%s' "$v" | cut -s -d. -f2) UPD=$(printf '%s' "$v" | cut -s -d. -f3 | sed 's/_.*//'); PAT=$(printf '%s' "$v" | cut -s -d. -f4 | sed 's/_.*//') ;; esac for n in MAJ MIN UPD PAT; do eval "val=\${$n}" case "$val" in ''|*[!0-9]*) eval "$n=0" ;; esac done } lt2() { [ "$1" -lt "$3" ] || { [ "$1" -eq "$3" ] && [ "$2" -lt "$4" ]; }; } version_status() { case "$MAJ" in 0) echo unknown ;; 17) if [ "$UPD" -lt 6 ]; then echo unaffected; elif lt2 "$UPD" "$PAT" 21 1; then echo affected; else echo fixed; fi ;; 21) if lt2 "$UPD" "$PAT" 13 1; then echo affected; else echo fixed; fi ;; 25) if lt2 "$UPD" "$PAT" 5 1; then echo affected; else echo fixed; fi ;; 27) if lt2 "$UPD" "$PAT" 1 1; then echo affected; else echo fixed; fi ;; 18|19|20|22|23|24|26) echo eol ;; 8) case "$1" in *perf*) if [ "$UPD" -lt 513 ]; then echo affected; else echo fixed; fi ;; *) echo unaffected ;; esac ;; *) if [ "$MAJ" -lt 17 ]; then echo unaffected; else echo newer; fi ;; esac } prop() { printf '%s\n' "$1" | sed -n "s/^ *$2 = //p" | head -n1; } jflag() { printf '%s\n' "$1" | awk -v n="$2" '$2 == n { print $4; exit }'; } OLDIFS=$IFS IFS=' ' set -f COUNT=0 for j in $RT_LIST; do [ -n "$j" ] || continue COUNT=$((COUNT + 1)) done echo "${BLD}$(m "Java trouvés : $COUNT" "Java runtimes found: $COUNT")${RST}" [ "$COUNT" -gt 0 ] || detail "$(m 'Aucun Java dans les emplacements habituels. Relancez avec --deep, ou donnez le chemin : sh java4shell-check.sh /chemin/bin/java' 'No Java in the usual places. Run again with --deep, or give the path: sh java4shell-check.sh /path/bin/java')" for j in $RT_LIST; do [ -n "$j" ] || continue IFS=$OLDIFS OUT=$(unset JAVA_TOOL_OPTIONS _JAVA_OPTIONS JDK_JAVA_OPTIONS; run_java "$j" -XshowSettings:properties -version 2>&1) RTV=$(prop "$OUT" java.runtime.version) [ -n "$RTV" ] || RTV=$(prop "$OUT" java.version) VENDOR=$(prop "$OUT" java.vendor.version) [ -n "$VENDOR" ] || VENDOR=$(prop "$OUT" java.vendor) JARCH=$(prop "$OUT" os.arch) parse_version "$RTV" VS=$(version_status "$RTV $VENDOR") FOUT='' JCPU=unknown UAVX='' UB64='' if [ "$VS" = affected ] || [ "$VS" = eol ]; then if [ "$MAJ" -ge 9 ]; then FOUT=$(unset JAVA_TOOL_OPTIONS _JAVA_OPTIONS JDK_JAVA_OPTIONS; run_java "$j" -Xlog:os+cpu -XX:+PrintFlagsFinal -version 2>&1) JCPU=$(cpu_status "$(printf '%s\n' "$FOUT" | grep 'CPU: total' | head -n1)") UAVX=$(jflag "$FOUT" UseAVX) UB64=$(jflag "$FOUT" UseBASE64Intrinsics) fi fi [ -n "$JARCH" ] || JARCH=$ARCH label="$j" info="$(m 'Version' 'Version') ${RTV:-?}${VENDOR:+ ($VENDOR)}" FIX=$(fix_for "$MAJ") case "$VS" in unknown) V=CHECK say_verdict CHECK "$label" detail "$(m 'Version illisible : lancez ce Java avec -version et comparez-la aux versions corrigées.' 'Version not readable: run this Java with -version and compare it with the fixed releases.')" ;; unaffected) V=OK; say_verdict OK "$label"; detail "$info · $(m "Java $MAJ n'est pas concerné." "Java $MAJ is not affected.")" ;; newer) V=OK; say_verdict OK "$label"; detail "$info · $(m 'version postérieure aux branches concernées.' 'newer than the affected lines.')" ;; fixed) V=OK; say_verdict OK "$label"; detail "$info · $(m 'contient le correctif Java4Shell.' 'includes the Java4Shell fix.')" ;; *) if ! is_x86 "$JARCH"; then V=TODO; EXPO=$(m "architecture $JARCH non concernée" "$JARCH architecture not affected") elif [ "$JCPU" = yes ] && [ "${UAVX:-0}" -ge 3 ] 2>/dev/null && [ "$UB64" = true ]; then V=VULN; EXPO=$(m "la JVM utilise AVX-512 VBMI (UseAVX=$UAVX, UseBASE64Intrinsics=true)" "the JVM uses AVX-512 VBMI (UseAVX=$UAVX, UseBASE64Intrinsics=true)") elif [ "$JCPU" = yes ]; then V=TODO; EXPO=$(m "intrinsèque désactivé par défaut ici (UseAVX=${UAVX:-?}, UseBASE64Intrinsics=${UB64:-?})" "intrinsic off by default here (UseAVX=${UAVX:-?}, UseBASE64Intrinsics=${UB64:-?})") elif [ "$JCPU" = no ]; then V=TODO; EXPO=$(m 'la JVM ne voit pas AVX-512 VBMI sur ce processeur' 'the JVM sees no AVX-512 VBMI on this CPU') elif [ "$OS_CPU" = yes ]; then V=VULN; EXPO=$(m 'processeur avec AVX-512 VBMI' 'CPU with AVX-512 VBMI') elif [ "$OS_CPU" = no ]; then V=TODO; EXPO=$(m 'processeur sans AVX-512 VBMI' 'CPU without AVX-512 VBMI') else V=CHECK; EXPO=$(m 'jeu d’instructions du processeur inconnu' 'CPU features unknown') fi say_verdict "$V" "$label" detail "$info · $(m 'version concernée' 'affected version'), $EXPO." if [ "$VS" = eol ]; then detail "$(m "Java $MAJ n'est plus maintenu et ne recevra pas de correctif : passez à 21.0.13.1 ou 25.0.5.1." "Java $MAJ is no longer maintained and gets no fix: move to 21.0.13.1 or 25.0.5.1.")" elif [ "$V" = VULN ]; then detail "$(m "→ Mettez à jour vers $FIX ou plus, puis redémarrez les applications." "→ Update to $FIX or later, then restart the applications.")" else detail "$(m "→ Mettez à jour vers $FIX quand même : une VM ou un conteneur peut être déplacé sur un processeur récent." "→ Update to $FIX anyway: a VM or container can be moved to a newer CPU.")" fi case "$VENDOR $RTV" in *[Rr]ed\ [Hh]at*|*[Uu]buntu*|*[Dd]ebian*|*[Ss][Uu][Ss][Ee]*) detail "$(m 'Paquet de distribution : un correctif reporté peut garder un ancien numéro, vérifiez le bulletin de sécurité de la distribution.' 'Distribution package: a backported fix may keep an older number, check the distribution security advisory.')" ;; esac ;; esac case "$V" in VULN) N_VULN=$((N_VULN + 1)) ;; TODO|CHECK) N_TODO=$((N_TODO + 1)) ;; *) N_OK=$((N_OK + 1)) ;; esac RT_VERDICTS="$RT_VERDICTS $j|$V" IFS=' ' done set +f IFS=$OLDIFS echo MITIG='-XX:-UseBASE64Intrinsics|-XX:UseAVX=[012]([^0-9]|$)' app_hint() { h=$(printf '%s' "$1" | grep -oiE 'jira|confluence|bitbucket|bamboo|jenkins|keycloak|artifactory|pingfederate|gerrit|spinnaker|kafka|zookeeper|elasticsearch|opensearch|solr|tomcat|catalina|jetty|wildfly|jboss|spring|gradle|maven|sonar|nexus|minecraft' | head -n1 | tr 'A-Z' 'a-z') printf '%s' "${h:-java}" } P_TOTAL=0 P_MIT=0 P_LINES='' if [ -d /proc ] && [ -r /proc/self/cmdline ]; then for d in /proc/[0-9]*; do x=$(readlink "$d/exe" 2>/dev/null) || continue case "$x" in */java|*/java\ \(deleted\)) ;; *) continue ;; esac pid=${d#/proc/} cmd=$(tr '\000' ' ' < "$d/cmdline" 2>/dev/null) envv=$(tr '\000' '\n' < "$d/environ" 2>/dev/null | grep -E '^(JAVA_TOOL_OPTIONS|_JAVA_OPTIONS|JDK_JAVA_OPTIONS)=' | tr '\n' ' ') owner=$(ls -ld "$d" 2>/dev/null | awk '{print $3}') P_TOTAL=$((P_TOTAL + 1)) hint=$(app_hint "$cmd") rv=$(printf '%s\n' "$RT_VERDICTS" | grep -F "${x%% (deleted)}|" | head -n1 | cut -d'|' -f2) if printf '%s %s' "$cmd" "$envv" | grep -qE -- "$MITIG"; then P_MIT=$((P_MIT + 1)) st="${GRN}$(m 'option de protection présente' 'mitigation option present')${RST}" else case "$x" in *"(deleted)") st="${YEL}$(m 'Java remplacé sur le disque : redémarrez ce processus' 'Java replaced on disk: restart this process')${RST}"; N_TODO=$((N_TODO + 1)) ;; *) case "$rv" in VULN) st="${RED}$(m 'VULNÉRABLE, aucune option de protection' 'VULNERABLE, no mitigation option')${RST}" ;; OK) st="${GRN}$(m 'Java non concerné ou corrigé' 'Java not affected or fixed')${RST}" ;; *) st="$(m 'voir le Java ci-dessus' 'see the runtime above')" ;; esac ;; esac fi P_LINES="$P_LINES PID $pid ($hint${owner:+, $owner}) : $st ${DIM}$x${RST}" done if [ "$(id -u 2>/dev/null)" != 0 ]; then P_NOTE=$(m 'Sans root, les processus Java des autres utilisateurs (services) ne sont pas visibles : relancez avec sudo.' 'Without root, the Java processes of other users (services) are not visible: run again with sudo.') else P_NOTE='' fi else P_NOTE='' PS_OUT=$(ps -axww -o pid= -o args= 2>/dev/null | grep -E '[/ ]java( |$)' | grep -v java4shell-check) while IFS= read -r l; do l=$(printf '%s' "$l" | sed 's/^ *//') [ -n "$l" ] || continue pid=${l%% *} P_TOTAL=$((P_TOTAL + 1)) if printf '%s' "$l" | grep -qE -- "$MITIG"; then P_MIT=$((P_MIT + 1)); st="${GRN}$(m 'option de protection présente' 'mitigation option present')${RST}" else st="$(m 'aucune option de protection sur la ligne de commande' 'no mitigation option on the command line')" fi P_LINES="$P_LINES PID $pid ($(app_hint "$l")) : $st" done <