Shell automatically checks SSH brute-force IP and blocks attacks

Source: Internet
Author: User

#!/bin/bash

local_ip= "192.168.0.4" #定义本地IP不会被拒绝

tmp_log= ' Mktemp '

Cat/var/log/secure |grep "Failed password for root from" |awk ' {print $11} ' |uniq-c > $tmp _log

Cat $tmp _log |while Read line

Do

Attack_num= ' echo $line |awk ' {print $} '

Attact_ip= ' echo $line |awk ' {print $} '

If ["$attack _num"-GT "] && [" $local _ip "! =" $attack _ip "]

Then

echo sshd: "$attact _ip":d eny >>/etc/hosts.deny

Fi

Done

Sort-u/etc/hosts.deny > $tmp _log #过滤相同IP

Cp-f $tmp _log/etc/hosts.deny

Rm-f $tmp _log


Every 5 minutes to detect, in 5 minutes to try password login error more than 10 times deny this IP

Crontab-e

*/5 * * * */sh/sshtool.sh


View denied IPs

Cat/etc/hosts.deny

This article is from the "Shell" blog, be sure to keep this source http://zhizhimao.blog.51cto.com/3379994/1936556

Shell automatically checks SSH brute-force IP and blocks attacks

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.