Guest User

Untitled

a guest
Dec 16th, 2021
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.78 KB | None | 0 0
  1. #!/bin/bash
  2. # Find and identify vulnerable log4j2 jar files
  3. # /u/whetu, 2021-12-16
  4.  
  5. service_name="log4shell"
  6.  
  7. # This must be run as root or with sudo
  8. if (( "${EUID:$(id -u)}" != 0 )); then
  9.   printf -- '%s\n' "1 ${service_name} - This must be run as root"
  10.   # checkmk local checks need to exit 0, folks
  11.   exit 0
  12. fi
  13.  
  14. # It might be feasible to check if java is actually present and short circuit here
  15.  
  16. # From https://raw.githubusercontent.com/mubix/CVE-2021-44228-Log4Shell-Hashes/main/md5sum.txt
  17. # With the addition of an md5sum for 2.15.0 (i.e. CVE-2021-45046, CVSSv3 3.7)
  18. affected_md5sums=(
  19.     2addabe2ceca2145955c02a6182f7fc5
  20.     5b1d4e4eea828a724c8b0237326829b3
  21.     ce9e9a27c2a5caa47754999eb9c549b8
  22.     1538d8c342e3e2a31cd16e01e3865276
  23.     9cb138881a317a7f49c74c3e462f35f4
  24.     578ffc5bcccb29f6be2d23176c0425e0
  25.     5b73a0ad257c57e7441778edee4620a7
  26.     e32489039dab38637557882cca0653d7
  27.     db025370dbe801ac623382edb2336ede
  28.     152ecb3ce094ac5bc9ea39d6122e2814
  29.     cd70a1888ecdd311c1990e784867ce1e
  30.     088df113ad249ab72bf19b7f00b863d5
  31.     de8d01cc15fd0c74fea8bbb668e289f5
  32.     fbfa5f33ab4b29a6fdd52473ee7b834d
  33.     8c0cf3eb047154a4f8e16daf5a209319
  34.     8d331544b2e7b20ad166debca2550d73
  35.     5e4bca5ed20b94ab19bb65836da93f96
  36.     110ab3e3e4f3780921e8ee5dde3373ad
  37.     0079c907230659968f0fc0e41a6abcf9
  38.     f0c43adaca2afc71c6cc80f851b38818
  39.     dd0e3e0b404083ec69618aabb50b8ac0
  40.     5523f144faef2bfca08a3ca8b2becd6a
  41.     48f7f3cda53030a87e8c387d8d1e4265
  42.     472c8e1fbaa0e61520e025c255b5d168
  43.     2b63e0e5063fdaccf669a1e26384f3fd
  44.     c6d233bc8e9cfe5da690059d27d9f88f
  45.     547bb3ed2deb856d0e3bbd77c27b9625
  46.     4a5177a172764bda6f4472b94ba17ccb
  47.     a27e67868b69b7223576d6e8511659dd
  48.     a3a6bc23ffc5615efcb637e9fd8be7ec
  49.     0042e7de635dc1c6c0c5a1ebd2c1c416
  50.     90c12763ac2a49966dbb9a6d98be361d
  51.     71d3394226547d81d1bf6373a5b0e53a
  52.     8da9b75725fb3357cb9872adf7711f9f
  53.     7943c49b634b404144557181f550a59c
  54.     df949e7d73479ab717e5770814de0ae9
  55.     2803991d51c98421be35d2db4ed3c2ac
  56.     5ff1dab00c278ab8c7d46aadc60b4074
  57.     b8e0d2779abbf38586b869f8b8e2eb46
  58.     46e660d79456e6f751c22b94976f6ad5
  59.     62ad26fbfb783183663ba5bfdbfb5ace
  60.     3570d00d9ceb3ca645d6927f15c03a62
  61.     f5e2d2a9543ee3c4339b6f90b6cb01fc
  62.     5985349db390cb5b11cb5f9a5c398a8d
  63. )
  64.  
  65. # Lifted from https://stackoverflow.com/a/57536163
  66. array::join() {
  67.     (($#)) || return 1 # At least delimiter required
  68.     local -- delim="$1" str IFS=
  69.     shift
  70.     str="${*/#/$delim}" # Expand arguments with prefixed delimiter (Empty IFS)
  71.     echo "${str:${#delim}}" # Echo without first delimiter
  72. }
  73.  
  74. # Usage: in_array "string" "arrayname"
  75. in_array() {
  76.     local string element
  77.     string="${1}"
  78.     shift
  79.     # Iterate through the array elements
  80.     for element; do
  81.         [[ "${element}" = "${string}" ]] && return 0
  82.     done
  83.     # If we get to this point, nothing matched, so we return 1
  84.     return 1
  85. }
  86.  
  87. locate_log4j_files() {
  88.     if command -v locate >/dev/null 2>&1; then
  89.         locate -r log4j.*.jar
  90.     else
  91.         find /var /etc /usr /opt /lib* /home -type f -name "log4j.*.jar" 2>&1 |
  92.             grep -Ev '^find:.* Permission denied$|^find:.* No such file or directory$'
  93.     fi
  94. }
  95.  
  96. locate_jar_files() {
  97.     if command -v locate >/dev/null 2>&1; then
  98.         locate -r '.jar$|.war$|.ear$'
  99.     else
  100.         find /var /etc /usr /opt /lib* /home -type f -name "*.jar" -o -name "*.war" -o -name "*.ear" 2>&1 |
  101.             grep -Ev '^find:.* Permission denied$|^find:.* No such file or directory$'
  102.     fi
  103. }
  104.  
  105. # First we look for files with obvious names and compare their md5 hashes
  106. mapfile -t log4j_files < <(locate_log4j_files)
  107. vulnerable_count=0
  108. for file_path in "${log4j_files[@]}"; do
  109.     md5_string="$(md5sum "${file_path}" | awk '{print $1}')"
  110.     found_paths+=( "${file_path}" )
  111.     in_array "${md5_string}" "${affected_md5sums[@]}" && (( ++vulnerable_count ))
  112. done
  113.  
  114. # Next we audit all jar/war/ear files
  115. # We filter our already found files from above in order to de-duplicate
  116. mapfile -t jar_files < <(locate_jar_files | grep -v -f <(printf -- '%s\n' "${log4j_files[@]}"))
  117.  
  118. class_found_count=0
  119. for file_path in "${jar_files[@]}"; do
  120.     if grep -q JndiLookup.class "${file_path}"; then
  121.         found_paths+=( "${file_path}" )
  122.         (( ++class_found_count ))
  123.     fi
  124. done
  125.  
  126. # TO-DO: Recursively unzip jar files
  127.  
  128. # Build our output
  129. out_message="P ${service_name} vulnerable_file_count=${vulnerable_count};1;1"
  130. out_message="${out_message}|vulnerable_class_count=${class_found_count};1;1"
  131. if (( "${#found_paths[@]}" > 0 )); then
  132.     out_message="${out_message} Potential vulnerabilities found in the following files\n"
  133.     out_message="${out_message}$(array::join '\n' "${found_paths[@]}")"
  134. else
  135.     out_message="${out_message} No vulnerable files or classes found by this check."
  136. fi
  137.  
  138. printf -- '%s\n' "${out_message}"
  139.  
Advertisement
Add Comment
Please, Sign In to add comment