secOpsReport.sh 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/bash
  2. # Created By : cybergavin
  3. # Created On : 17-MAR-2020
  4. # Description : This script sends an email with the TOP 5 most frequently banned IPs to the IT Security Operations
  5. # team for further analysis and action (e.g. submit a request to Infrastructure Services for the
  6. # permanent ban of "bad" IPs on the frontend firewalls.
  7. #
  8. ######################################################################################################
  9. #
  10. # Variables
  11. #
  12. sender_name="cybergavin"
  13. sender_email="mail@cybergav.in"
  14. rec_email="" # Multiple email adddresses may be used and separated with commas
  15. #
  16. # Determine Script Location
  17. #
  18. if [ -n "`dirname $0 | grep '^/'`" ]; then
  19. SCRIPT_LOCATION=`dirname $0`
  20. elif [ -n "`dirname $0 | grep '^..'`" ]; then
  21. cd `dirname $0`
  22. SCRIPT_LOCATION=$PWD
  23. cd - > /dev/null
  24. else
  25. SCRIPT_LOCATION=`echo ${PWD}/\`dirname $0\` | sed 's#\/\.$##g'`
  26. fi
  27. SCRIPT_NAME=`basename $0`
  28. #
  29. # Log stderr and stdout
  30. #
  31. exec 1> ${SCRIPT_LOCATION}/${SCRIPT_NAME%%.*}.stdout
  32. exec 2> ${SCRIPT_LOCATION}/${SCRIPT_NAME%%.*}.stderr
  33. #
  34. # Generate report and send email
  35. #
  36. if [ ! -d ${SCRIPT_LOCATION}/data ]; then
  37. mkdir ${SCRIPT_LOCATION}/data
  38. fi
  39. my_report=${SCRIPT_LOCATION}/data/${SCRIPT_NAME%%.*}_`date '+%b%Y'`.txt
  40. my_report_html=${my_report%%.*}.html
  41. cat <<EOF > $my_report_html
  42. <html>
  43. <head>
  44. <style>
  45. .datagrid1 table { border: 1px solid black; border-collapse: collapse; text-align: justify; width: 30%; font: normal 12px/150% Verdana, Arial, Helvetica, sans-serif; }
  46. .datagrid1 td,th {border: 1px solid black;}
  47. </style>
  48. </head>
  49. <body>
  50. Security Operations <br /><br />
  51. Here are the <b>TOP 5 MOST FREQUENTLY BANNED IPs</b> in $(date '+%B %Y' --date="last month") on $HOSTNAME :<br /><br />
  52. <div class="datagrid1">
  53. <table>
  54. <tr>
  55. <th width="20%">IP ADDRESS</th>
  56. <th width="10%">#BANS</th>
  57. </tr>
  58. EOF
  59. awk -F, '$3 != "BANNED IP" {print $3}' ${SCRIPT_LOCATION}/data/banaction*.csv | sort | uniq -c | sort -rk 1 | head -5 | sed 's/^ *//g;s/ /,/g' > $my_report
  60. for f2b in `cat $my_report`
  61. do
  62. my_ip=`echo $f2b | cut -d, -f2`
  63. my_bc=`echo $f2b | cut -d, -f1`
  64. cat <<EOF >> $my_report_html
  65. <tr>
  66. <td width="20%">$my_ip</td>
  67. <td width="10%" align="center">$my_bc</td>
  68. </tr>
  69. EOF
  70. done
  71. cat <<EOF >> $my_report_html
  72. </table>
  73. </div>
  74. <br /><br />
  75. <p align="justify">Based on your analysis of the above IP addresses, you may opt to request the Network Admins to implement a permanent ban of one or more of the above IP addresses on the PAN firewalls.</p>
  76. </body>
  77. </html>
  78. EOF
  79. cat <<EOF | /usr/sbin/sendmail -f $sender_email $rec_email
  80. Subject: $sender_name : $(date '+%B %Y' --date="last month") BANNED IP Report
  81. Date: `LC_TIME=C date -u +"%%a, %%d %%h %%Y %%T +0000"`
  82. From: $sender_name <$sender_email>
  83. To: $rec_email
  84. Content-Type: text/html
  85. `cat $my_report_html`
  86. EOF
  87. #
  88. # Housekeep
  89. #
  90. if [ -d ${SCRIPT_LOCATION}/data ]; then
  91. find ${SCRIPT_LOCATION}/data -type f -name "${SCRIPT_NAME%%.*}*.txt" -mtime +180 | xargs rm -f
  92. fi