tendang.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/bash
  2. clear
  3. MAX=1
  4. if [ -e "/var/log/auth.log" ]; then
  5. OS=1;
  6. LOG="/var/log/auth.log";
  7. fi
  8. if [ -e "/var/log/secure" ]; then
  9. OS=2;
  10. LOG="/var/log/secure";
  11. fi
  12. if [ $OS -eq 1 ]; then
  13. service ssh restart > /dev/null 2>&1;
  14. fi
  15. if [ $OS -eq 2 ]; then
  16. service sshd restart > /dev/null 2>&1;
  17. fi
  18. service dropbear restart > /dev/null 2>&1;
  19. if [[ ${1+x} ]]; then
  20. MAX=$1;
  21. fi
  22. cat /etc/passwd | grep "/home/" | cut -d":" -f1 > /root/user.txt
  23. username1=( `cat "/root/user.txt" `);
  24. i="0";
  25. for user in "${username1[@]}"
  26. do
  27. username[$i]=`echo $user | sed 's/'\''//g'`;
  28. jumlah[$i]=0;
  29. i=$i+1;
  30. done
  31. cat $LOG | grep -i dropbear | grep -i "Password auth succeeded" > /tmp/log-db.txt
  32. proc=( `ps aux | grep -i dropbear | awk '{print $2}'`);
  33. for PID in "${proc[@]}"
  34. do
  35. cat /tmp/log-db.txt | grep "dropbear\[$PID\]" > /tmp/log-db-pid.txt
  36. NUM=`cat /tmp/log-db-pid.txt | wc -l`;
  37. USER=`cat /tmp/log-db-pid.txt | awk '{print $10}' | sed 's/'\''//g'`;
  38. IP=`cat /tmp/log-db-pid.txt | awk '{print $12}'`;
  39. if [ $NUM -eq 1 ]; then
  40. i=0;
  41. for user1 in "${username[@]}"
  42. do
  43. if [ "$USER" == "$user1" ]; then
  44. jumlah[$i]=`expr ${jumlah[$i]} + 1`;
  45. pid[$i]="${pid[$i]} $PID"
  46. fi
  47. i=$i+1;
  48. done
  49. fi
  50. done
  51. cat $LOG | grep -i sshd | grep -i "Accepted password for" > /tmp/log-db.txt
  52. data=( `ps aux | grep "\[priv\]" | sort -k 72 | awk '{print $2}'`);
  53. for PID in "${data[@]}"
  54. do
  55. cat /tmp/log-db.txt | grep "sshd\[$PID\]" > /tmp/log-db-pid.txt;
  56. NUM=`cat /tmp/log-db-pid.txt | wc -l`;
  57. USER=`cat /tmp/log-db-pid.txt | awk '{print $9}'`;
  58. IP=`cat /tmp/log-db-pid.txt | awk '{print $11}'`;
  59. if [ $NUM -eq 1 ]; then
  60. i=0;
  61. for user1 in "${username[@]}"
  62. do
  63. if [ "$USER" == "$user1" ]; then
  64. jumlah[$i]=`expr ${jumlah[$i]} + 1`;
  65. pid[$i]="${pid[$i]} $PID"
  66. fi
  67. i=$i+1;
  68. done
  69. fi
  70. done
  71. j="0";
  72. for i in ${!username[*]}
  73. do
  74. if [ ${jumlah[$i]} -gt $MAX ]; then
  75. date=`date +"%Y-%m-%d %X"`;
  76. echo "$date - ${username[$i]} - ${jumlah[$i]}";
  77. echo "$date - ${username[$i]} - ${jumlah[$i]}" >> /root/log-limit.txt;
  78. kill ${pid[$i]};
  79. pid[$i]="";
  80. j=`expr $j + 1`;
  81. fi
  82. done
  83. if [ $j -gt 0 ]; then
  84. if [ $OS -eq 1 ]; then
  85. service ssh restart > /dev/null 2>&1;
  86. fi
  87. if [ $OS -eq 2 ]; then
  88. service sshd restart > /dev/null 2>&1;
  89. fi
  90. service dropbear restart > /dev/null 2>&1;
  91. j=0;
  92. fi