Linux protects against SSH brute force scan IP

Source: Internet
Author: User
Tags ssh port

First modify the SSH port.


Create Script pb_ssh.sh


#! /bin/bash


#crontab execute every 1 minutes

#*/1 * * * */root/pb_ssh.sh


# Get Secure records in the first 1 minutes, Count SSH authentication failed IP and its number of failures


Scanner= ' grep ' $ (date-d -1min|awk ' {print substr ($0,10,7)} ') "/var/log/secure|awk '/failed/{print $ (NF-3)} ' |awk-f ':" ' { Print $NF} ' |grep-v From|sort|uniq-c|awk ' {print $ "=" $;} ' `



For I in $SCANNER


Do


# Number of authentication failures taken


Num= ' echo $i |awk-f= ' {print $} '


# take its IP address


Ip= ' echo $i |awk-f= ' {print $} '


# If it has failed more than 5 times and has not been blocked before, add a policy to block it and log


If [$NUM-gt 5] && [-Z "' Iptables-vnl input|grep $IP '"]


Then


/sbin/iptables-i input-s $IP-j DROP


echo "' Date ' $IP ($NUM)" >>/var/log/scanner.log


Fi


Done



Restore the blocked IP, log in from another server, restart the Firewall service


Iptables-l

IPTABLES-VNL INPUT



Configuration steps

Su-root

Chown Root.root pb_ssh.sh

chmod 755 pb_ssh.sh


#crontab prevent SSH scanning every 1 minutes

*/1 * * * */root/pb_ssh.sh


or use denyhosts software.

Linux protects against SSH brute force scan IP

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.