Shell script settings to prevent brute force ssh

Shell script settings to prevent brute force ssh

The shell script sets access control, and the IP is blocked after multiple failed logins to prevent brute force cracking of ssh

When a host is found to be performing SSH remote management to the server and the wrong password is entered 3 times within 10 minutes, firewalld will be used to respond and prohibit this IP from connecting again, and the connection will be allowed again after 6 hours.

1. System: Centos7.1 64-bit

2. Method: Read /var/log/secure and search for the keyword Failed

 Jan 4 16:29:01 centos7 sshd[1718]: Failed password for root from 192.168.120.1 port 2171 ssh2
Jan 4 16:29:02 centos7 sshd[1718]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Jan 4 16:29:04 centos7 sshd[1718]: Failed password for root from 192.168.120.1 port 2171 ssh2
Jan 4 16:29:06 centos7 sshd[1718]: error: Received disconnect from 192.168.120.1 port 2171:0: [preauth]
Jan 4 16:45:53 centos7 sshd[1758]: Failed password for root from 192.168.120.134 port 40026 ssh2
3. Shell code, create test.sh
#!bin/bash
#Intercept illegal IP addresses
month=$(LANG=C date +"%b")
day=$(LANG=C date +"%e")
now=$(LANG=C date +"%T")
ten=$(LANG=C date -d "10 minutes ago" +"%T")

cat /var/log/secure |awk '$1=="'$month'" && $2=='"$day"' && $3>="'$ten'" && $3<="'$now'" { print}' |grep 'Failed'|awk -F'from' '{ print $2}' |awk '{ print $1}'|sort |uniq -c > baduser.txt

#Number of calculations
times=`awk '{ print $1 }' baduser.txt`

#banned IP address
seq=1
for i in $times
do
   ip=`sed -n ''$seq'p' baduser.txt |awk '{ print $2}'`
   if [ $i -ge 3 ]
   then
   firewall-cmd --add-rich-rule='rule family=ipv4 source address='$ip' port port=22 protocol=tcp reject' --timeout=6h
   fi
seq=`expr $seq + 1`
done
4. Put the test.sh script into the cron scheduled task and execute it every 60 minutes.
# crontab -e
*/60 * * * * sh /root/test.sh

insert image description here

5. Testing

1. Open a terminal window, connect to the server with ssh, and connect to the server several times with the wrong password.
Soon, there were records in the blacklist baduser.txt file on the server:

insert image description here

The IP has been added to the server's baduser.txt file, and cannot connect to the server, and is rejected:

insert image description here

This is the end of this article about shell script settings to prevent brute force cracking of ssh. For more relevant shell script anti-brute force cracking content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • What is ssh port forwarding? What's the use?
  • What is ssh? How to use? What are the misunderstandings?
  • Detailed explanation of Linux remote management and sshd service verification knowledge points
  • How to modify the ssh port number in Centos8 environment
  • ssh remote management service

<<:  Six border transition effects implemented by CSS3

>>:  Specific steps to use vant framework in WeChat applet

Recommend

Super simple qps statistics method (recommended)

Statistics of QPS values ​​in the last N seconds ...

Implementation of MySQL scheduled database backup (full database backup)

Table of contents 1. MySQL data backup 1.1, mysql...

Detailed explanation of the concept, principle and usage of MySQL triggers

This article uses examples to explain the concept...

Detailed tutorial on compiling and installing python3.6 on linux

1. First go to the official website https://www.p...

Detailed explanation of the configuration method of Vue request interceptor

Follow the steps below 1. request.js content: htt...

Detailed explanation of the usage of MySQL data type DECIMAL

MySQL DECIMAL data type is used to store exact nu...

Nginx content cache and common parameter configuration details

Use scenarios: The project's pages need to lo...

In-depth analysis of MySQL index data structure

Table of contents Overview Index data structure B...

CSS margin overlap and how to prevent it

The vertically adjacent edges of two or more bloc...

How to quickly import data into MySQL

Preface: In daily study and work, we often encoun...

MySQL free installation version configuration tutorial

This article shares the MySQL free installation c...

JavaScript Advanced Closures Explained

Table of contents 1. The concept of closure Addit...

How MySQL handles implicit default values

Some students said that they encountered the prob...

Detailed explanation of MySQL/Java server support for emoji and problem solving

This article describes the support and problem so...