1
0

hunter.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/bin/bash
  2. #########################################################################
  3. # Script for hunting malware #
  4. # Created by PM-DoIT #
  5. # ver 0.2 #
  6. #########################################################################
  7. #------------------------------- VARIABLE ------------------------------#
  8. clear
  9. score=0
  10. IGNORE="apache|apparmor|chrony|cron|mariadb|networking|fpm|postfix|snmpd|ssh|certbot|logrotate|rsyslog|clamav|php|redis|supervisor|getty|apt-daily|chkrootkit|monit|qemu-guest|systemd-journald|maldet|wazuh|filebea"
  11. echo -e "\e[1;37m-------------------------------------\e[0m"
  12. echo -e "\e[1;33mSTARTED host COMPROMISE assessment:\e[0m"
  13. echo -e "\e[1;37m-------------------------------------\e[0m"
  14. #------------------------- Suspicious UID 0 users ----------------------#
  15. echo -e "- \e[1;32mChecking suspicious UID 0 users\e[0m"
  16. uid0=$(awk -F: '$3 == 0 { print $1 }' /etc/passwd | grep -v root)
  17. if [ -n "$uid0" ]; then
  18. echo -e "\e[1;31m- ! UID 0 non-root users: $uid0\e[0m"
  19. score=$((score+30))
  20. fi
  21. #------------------------- SSH keys in root/home -----------------------#
  22. echo -e "- \e[1;32mChecking keys in root/home\e[0m"
  23. keys=$(find /root /home -name authorized_keys 2>/dev/null | wc -l)
  24. if [ "$keys" -gt 5 ]; then
  25. echo -e "\e[1;31m- ! Excess SSH keys detected: $keys\e[0m"
  26. score=$((score+15))
  27. fi
  28. #---------------------- Suspicious listening ports ---------------------#
  29. echo -e "- \e[1;32mChecking suspicious listening ports\e[0m"
  30. listeners=$(ss -tulpn | grep -E 'bash|sh|python|nc|perl' | grep -v "ssh" | wc -l)
  31. if [ "$listeners" -gt 0 ]; then
  32. echo -e "\e[1;31m- ! Suspicious listeners detected\e[0m"
  33. ss -tulpn | grep -E 'bash|sh|python|nc|perl' | grep -v "ssh"
  34. score=$((score+15))
  35. fi
  36. #--------------------------- Cron persistence --------------------------#
  37. echo -e "- \e[1;32mChecking cron persistence\e[0m"
  38. cron_count=$(find /etc/cron* /var/spool/cron -type f 2>/dev/null | wc -l)
  39. if [ "$cron_count" -gt 20 ]; then
  40. echo -e "\e[1;31m- ! High number of cron jobs: [$cron_count]\e[0m"
  41. find /etc/cron* /var/spool/cron -type f
  42. score=$((score+5))
  43. fi
  44. #--------------------------- Executable TMP ----------------------------#
  45. exec_count=$(find /tmp /var/tmp /dev/shm -type f -executable -ls | grep -v "hunter.sh" | wc -l)
  46. if [ "$exec_count" -ge 1 ]; then
  47. echo -e "\e[1;31m- ! High number of executable temporary detected: [$exec_count]\e[0m"
  48. find /tmp /var/tmp /dev/shm -type f -executable -ls | awk '{print $NF}' | grep -v "hunter.sh"
  49. score=$((score+10))
  50. fi
  51. #--------------------- SUID binaries outside baseline ------------------#
  52. echo -e "- \e[1;32mChecking SUID binaries outside baseline\e[0m"
  53. suid=$(find / -perm -4000 -type f 2>/dev/null | wc -l)
  54. if [ "$suid" -gt 100 ]; then
  55. echo -e "\e[1;31m- ! High SUID count: $suid\e[0m"
  56. score=$((score+10))
  57. fi
  58. #------------------------ Suspicious processes -------------------------#
  59. echo -e "- \e[1;32mChecking suspicious processes\e[0m"
  60. proc=$(ps aux | grep -E '/tmp|/dev/shm|nc -l|python -c|bash -i' | egrep -v "grep|hunter|wazuh" | wc -l)
  61. if [ "$proc" -gt 0 ]; then
  62. echo -e "\e[1;31m- ! Suspicious processes detected\e[0m"
  63. ps aux | grep -iE '/tmp|/dev/shm|nc -l|python -c|bash -i' | egrep -v "grep|hunter|wazuh"
  64. score=$((score+30))
  65. fi
  66. #--------------------- Recent auth failures spike ----------------------#
  67. echo -e "- \e[1;32mChecking recent auth failures spike\e[0m"
  68. auth_fail=$(grep -i "Failed password" /var/log/auth.log 2>/dev/null | wc -l)
  69. if [ "$auth_fail" -gt 50 ]; then
  70. echo -e "\e[1;31m- ! High auth failure rate: $auth_fail\e[0m"
  71. score=$((score+10))
  72. fi
  73. #------------------------------ Last logs ------------------------------#
  74. echo -e "- \e[1;32mChecking last logs\e[0m"
  75. last -a | head -10
  76. lastb -a | head -10
  77. #---------------------- Suspicious connections -------------------------#
  78. echo -e "- \e[1;32mChecking suspicious connections\e[0m"
  79. lsof -i -P -n | egrep -v "80|443|161|2812|323|3306|9200|6379|LISTEN"
  80. #----------------------- Suspicious services ---------------------------#
  81. echo -e "- \e[1;32mChecking suspicious services\e[0m"
  82. systemctl list-unit-files --state=enabled | egrep -v "$IGNORE"
  83. systemctl list-units --type=service --state=running | egrep -v "$IGNORE"
  84. #---------------------- Final score normalization ----------------------#
  85. if [ "$score" -gt 100 ]; then score=100; fi
  86. echo -e "\e[1;37m-------------------------------------\e[0m"
  87. echo -e "\e[1;33mCOMPROMISE SCORE:\e[0m \e[1;36m$score / 100\e[0m"
  88. echo -e "\e[1;37m-------------------------------------\e[0m"
  89. if [ "$score" -lt 30 ]; then
  90. echo -e "- Status: \e[1;32mLOW RISK\e[0m"
  91. elif [ "$score" -lt 60 ]; then
  92. echo -e "- Status: \e[1;35mMEDIUM RISK\e[0m"
  93. else
  94. echo -e "- Status: \e[1;31mHIGH RISK\e[0m"
  95. fi
  96. echo -e "\e[1;37m-------------------------------------\e[0m"