SSH anti-brute shell script

Source: Internet
Author: User


Write an SSH anti-violence hack script.

The principle is to detect the SSH connection log, filter the login failed IP, more than the number of login to add it into the Hosts.deny file, limit its login.

The script is as follows:

#! /bin/bashcat /var/log/secure|awk  '/failed/{print $ (NF-3)} ' |sort|uniq -c|awk  ' {print $2 "=" $;} '  > /root/black.txtdefine= "Ten" for i in  ' Cat  /root/black.txt ' do         ip= ' echo  $i  |awk -F=  ' {print $1} '          num= ' echo  $i |awk -f=  ' {print $2} '          if [  $NUM  -gt  $DEFINE  ];         then         grep  $IP  /etc/ hosts.deny > /dev/null          if [  $? -gt 0 ];          then           echo  "sshd: $IP"  >> /etc/hosts.deny  & nbsp;        fi        fidone 


Note that the SSH log file path for Ubuntu and CentOS is different:

/var/log/auth.log Ubuntu is this file

/var/log/secure CentOS is this file


Then add the task schedule:

Centos:

echo "* */1 * * * * root sh/root/ssh_deny.sh" >>/var/spool/cron/root


Ubuntu:

echo "* */1 * * * * root sh/root/ssh_deny.sh >/dev/null 2>&1" >>/var/spool/cron/crontabs/rootcrontab/var /spool/cron/crontabs/root

Since Ubuntu's default shell is dash, which causes crontab not to run, we'll just change the default shell to bash.

Rm/bin/sh ln-s/bin/bash/bin/sh




SSH anti-brute shell script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.