Prevent ssh Brute-force cracking of shell scripts in Centos

Source: Internet
Author: User
Tags ssh centos

Script

#! /Bin/bash
 
Cat/var/log/secure | awk '/Failed/{print $(NF-3 )} '| sort | uniq-c | awk' {print $2 "=" $1;} '>/root/black.txt # filter the ip addresses and times in the Failed status
 
Cat/var/log/secure | awk '/Invalid user/{print $ NF}' | sort | uniq-c | awk '{print $2 "=" $1 ;} '>/root/black.txt # filter the ip addresses and times of Invalid users
 
DEFINE = "20" # The number of allowed times is 20
 
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
 
Fi
 
Fi
 
Done
It is required that the ip address be added to hosts. deny when the logon fails or the number of illegal ip addresses reaches 20.
Because the secure log files are automatically archived every Saturday, we can write a schedule to run this script.
Run at every day

0 1 ***/usr/local/bin/ssh_black.sh 2> & 1>/dev/null

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.