Advertisement
Guest User

AVG Auditor

a guest
Jul 27th, 2019
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.53 KB | None | 0 0
  1. #!/bin/bash
  2. function showhelp(){
  3.     cat <<-EOF
  4.         Usage:
  5.             -h    Show this help message
  6.             -a    Show all fields
  7.                   This is equal to -f name,packages,status,severity,type,affected,fixed,ticket,issues
  8.             -f    Custom format, e.g. -f packages,affected,severity
  9.             -v    Show all vulnerable packages, not just ones on the system
  10.             -c    Colorize output
  11.             -t    Test against all packages, including fixed ones
  12.             -l    Link to the full AVG URL
  13.             -n    Do not count vulnerable/listed packages at the end
  14.             -b    Alternative database location
  15.        
  16.         Fields:
  17.             name        Link to the Arch Vulnerability Group number
  18.             packages    List of the affected packages
  19.             status      Shows whether it is fixed or not
  20.             severity    From Critical, High, Medium, to Low
  21.             type        Short description on the type of attack
  22.             affected    Version number of the affected package
  23.             fixed       Version number of the fixed package
  24.             ticket      Ticket number for bugs.archlinux.org
  25.             issues      List of related CVEs
  26.     EOF
  27.     exit
  28. }
  29.  
  30. declare -A vfields=(
  31.     [name]=1 [packages]=1 [status]=1 [severity]=1 [type]=1
  32.     [affected]=1 [fixed]=1 [ticket]=1 [issues]=1
  33. )
  34.  
  35. vcount=true
  36. vsys=true
  37. coloroutput=false
  38. fullurl=false
  39. securl='https://security.archlinux.org/issues/vulnerable/json'
  40. dbpath='/var/lib/pacman'
  41. vformat=packages,affected,fixed,status,severity,name
  42.  
  43. while getopts 'haf:vctlnb:' opt; do
  44.     case "${opt}" in
  45.         h) showhelp ;;
  46.         a) vformat=name,packages,status,severity,type,affected,fixed,ticket,issues ;;
  47.         f) vformat="${OPTARG}" ;;
  48.         v) vsys=false ;;
  49.         c) coloroutput=true ;;
  50.         t) securl='https://security.archlinux.org/issues/all/json' ;;
  51.         l) fullurl=true ;;
  52.         n) vcount=false ;;
  53.         b)
  54.             if [[ -d "${OPTARG}" ]]; then
  55.                 dbpath="${OPTARG}"
  56.             else
  57.                 echo "${OPTARG} not a directory"
  58.                 exit
  59.             fi
  60.             ;;
  61.         *) showhelp ;;
  62.     esac
  63. done
  64.  
  65. jdata="$(curl -s "${securl}")"
  66. IFS=','
  67. for f in ${vformat}; do
  68.     if [[ -n "${vfields[$f]}" ]]; then
  69.         [[ -n "${vheaders}" ]] && vheaders+=','
  70.         vheaders+="${f^^}"
  71.         [[ -n "${jqcommand}" ]] && jqcommand+=' + "," + '
  72.         case "${f}" in
  73.             status|severity|type|affected|fixed|ticket)
  74.                 jqcommand+=".${f}"
  75.                 ;;
  76.             packages|issues)
  77.                 jqcommand+=".${f}[]"
  78.                 ;;
  79.             name)
  80.                 $fullurl && jqcommand+='"https://security.archlinux.org/" + '
  81.                 jqcommand+=".${f}"
  82.                 ;;
  83.         esac
  84.     fi
  85. done
  86. jqcommand+=' + "\n"'
  87.  
  88. if $vsys; then
  89.     packagelist="$(pacman -Qsb "${dbpath}")"
  90.     while read -r vpackage; do
  91.         if echo "${packagelist}" | grep -q "${vpackage}"; then
  92.             vaffected="$(echo "${vpackage}" | cut -d' ' -f2)"
  93.             vpackagename="$(echo "${vpackage}" | cut -d' ' -f1)"
  94.             vjqdata+="$(echo "${jdata}" | jq -r '.[] | select((.affected == "'"${vaffected}"'") and .packages[0] == "'"${vpackagename}"'")')"
  95.         fi
  96.     done < <(echo "${jdata}" | jq -jr '.[] | .packages[] + " " + .affected + "\n"')
  97.     vrows+="$(echo "${vjqdata}" | jq -jr "${jqcommand}")"
  98. else
  99.     vrows+="$(echo "${jdata}" | jq -jr ".[] | ${jqcommand}")"
  100. fi
  101.  
  102. if $coloroutput; then
  103.     printf '%s\n%s' "${vheaders}" "${vrows}" | column -s',' -t | \
  104.         sed 's/Critical/\x1b[91m\x1b[1mCritical\x1b[0m/g;
  105.         s/High/\x1b[91m\x1b[1mHigh\x1b[0m/g;
  106.         s/Vulnerable/\x1b[91m\x1b[1mVulnerable\x1b[0m/g;
  107.         s/Medium/\x1b[93m\x1b[1mMedium\x1b[0m/g;
  108.         s/Low/\x1b[92m\x1b[1mLow\x1b[0m/g'
  109. else
  110.     printf '%s\n%s' "${vheaders}" "${vrows}" | column -s',' -t
  111. fi
  112.  
  113. if $vcount; then
  114.     vc="$(echo "${vrows}" | wc -l)"
  115.     printf '\n%s vulnerable packages ' "${vc}"
  116.     $vsys && echo 'installed' || echo 'listed'
  117. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement