edumgui

Fail2ban_BanPermanently_ReportToAbuseIPDB

Jul 5th, 2017
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.29 KB | None | 0 0
  1. #!/bin/bash
  2. #Look for "already" banned IP
  3. if tail -n 1 /var/log/fail2ban.log | grep -q already
  4. then
  5.     IP=$(tail -n 1 /var/log/fail2ban.log | awk 'NF{print $(NF-2); exit}')
  6.     CAT=$(tail -n 1 /var/log/fail2ban.log | awk 'NF{print $(NF-3); exit}')
  7.     case $CAT in
  8.         ("[sshd]") CATEGORY='category=18,22'; COMMENT='comment=Brute-force'; BAN='ip='$IP''; ;;
  9.         ("[apache-fakegooglebot]") CATEGORY='category=19'; COMMENT='comment=FakeGoogleBot'; BAN='ip='$IP''; ;;
  10.         ("[apache-badbots]") CATEGORY='category=19'; COMMENT='comment=BadBot'; BAN='ip='$IP''; ;;
  11.         ("[apache-botsearch]") CATEGORY='category=19'; COMMENT='comment=CrawlerBot'; BAN='ip='$IP''; ;;
  12.     esac;
  13. else
  14.     exit
  15. fi
  16.  
  17. #Just for debugging purposes
  18. #echo $CAT
  19. #echo $CATEGORY
  20. #echo $COMMENT
  21. #echo $BAN
  22.  
  23. #Look if IP is already in hosts.deny
  24. if grep -q $IP /etc/hosts.deny
  25. then
  26.     exit
  27. else
  28.     #Ban IP permanently
  29.     echo ALL: $IP >> /etc/hosts.deny
  30.     #Report to AbuseIPDB
  31.     RESULT=$(curl --data 'key=YOUR_ABUSEIPDB_APIKEY' \
  32.     --data $CATEGORY --data $COMMENT --data $BAN 'https://www.abuseipdb.com/report/json')
  33.     echo -e "\n Check AbuseIPDB: \n https://www.abuseipdb.com/check/$IP \n Curl result: \n $RESULT" | \
  34.     mail -s "New banned IP" "[email protected]"
  35. exit
  36. fi
  37. #If you need help, feel free to contact me via Twitter - @edumgui
Add Comment
Please, Sign In to add comment