Centos/rehl 6 SSH Log Filter script to prevent brute force

Source: Internet
Author: User

Script introduction, the script will filter the SSH log file (/var/log/secure*), and then through the Grep/awk/sort/uniq command to filter out the log, and log statistics.

Then by judging, when there is an excessive number of failures in an IP, send mail and log logs to the administrator for failure. In a production environment you may need to change the email address to the admin email address, provided that your server can send mail through the local mail server.

Script Source:

--------------------------------------------------------------------------------------------------------------- ---------------

[email protected] ~]# cat chk_ssh_login.sh
#!/bin/bash

serverip= ' Ifconfig eth0 | grep "inet addr" | awk ' {print $} ' | Awk-f ":" ' {print $} '

# Get SSH Failure login infos.
Grep-r failed/var/log/secure* >/root/ssh_failure_data.log

# Filter Failure Login Infos.
Cat/root/ssh_failure_data.log | awk ' {print $ (NF-3), $ (NF-5)} ' | Sort | uniq-c | awk ' {print $ ' = ' $ $ ' = ' $ $} ' >/root/ssh_data.log

For i in ' Cat/root/ssh_data.log '
Do
Count= ' echo $i |awk-f "=" ' {print '} '
Ip= ' echo $i |awk-f "=" ' {print $} '
User= ' echo $i |awk-f "=" ' {print $} '
If [$Count-gt 10]; Then
grep $IP/root/ssh_failure_data.log >/tmp/ssh_error.logs
Mail-s "SSH Abnormal login occurred on server $ServerIP." [Email protected] </tmp/ssh_error.logs
Rm-rf/tmp/ssh_error.logs
Fi
Done
--------------------------------------------------------------------------------------------------------------- ---------------

Execution process:

--------------------------------------------------------------------------------------------------------------- ---------------
[Email protected] ~]# sh chk_ssh_login.sh
You have mail in/var/spool/mail/root
[Email protected] ~]#
[Email protected] ~]#

--------------------------------------------------------------------------------------------------------------- ---------------

To view the execution results:

--------------------------------------------------------------------------------------------------------------- ---------------
[[Email protected] ~]# mail
Heirloom Mail Version 12.4 7/29/08. Type? For help.
"/var/spool/mail/root": 5 Messages 1 new 3 unread
1 cron Daemon Fri June 23:53 22/880 "cron <[email protected]>/usr/lib64/sa/sa2-a"
U 2 [email protected] Wed Feb 17:50 47/2260 "[ABRT] full crash report"
U 3 mail Delivery System Sun 11:31 74/2797 "undelivered Mail returned to Sender"
4 root Thu Apr 11:03 29/2108 "SSH Abnormal login occurred on server 10.9.249.173."
>n 5 root Thu Apr 11:04 28/2097 "SSH Abnormal login occurred on server 10.9.249.173."
& 5
Message 5:
From [email protected] Thu APR 23 11:04:02 2015
Return-path: <[email protected]>
x-original-to: [Email protected]
delivered-to: [Email protected]
Date:thu, APR 2015 11:04:02 +0800
To: [Email protected]
SUBJECT:SSH abnormal login occurred on server 10.9.249.173.
User-agent:heirloom MAILX 12.4 7/29/08
Content-type:text/plain; Charset=us-ascii
From: [Email protected] (root)
Status:r

/VAR/LOG/SECURE:APR 15:10:55 youserver sshd[21486]: Failed password for user1 from 10.9.150.68 Port 33275 ssh2
/VAR/LOG/SECURE:APR 15:10:59 youserver sshd[21486]: Failed password for user1 from 10.9.150.68 Port 33275 ssh2
/VAR/LOG/SECURE:APR 15:11:03 youserver sshd[21486]: Failed password for user1 from 10.9.150.68 Port 33275 ssh2
/VAR/LOG/SECURE:APR 15:11:39 youserver sshd[21489]: Failed password for user1 from 10.9.150.68 Port 33276 ssh2
/VAR/LOG/SECURE:APR 15:13:27 youserver sshd[21498]: Failed password for user1 from 10.9.150.68 Port 33277 ssh2
/VAR/LOG/SECURE:APR 15:15:08 youserver sshd[21498]: Failed password for user1 from 10.9.150.68 Port 33277 ssh2
/VAR/LOG/SECURE:APR 15:15:19 youserver sshd[21506]: Failed password for user1 from 10.9.150.68 Port 33278 ssh2
/VAR/LOG/SECURE:APR 15:16:21 youserver sshd[21536]: Failed password for user1 from 10.9.150.68 Port 33280 ssh2
/VAR/LOG/SECURE:APR 15:16:25 youserver sshd[21536]: Failed password for user1 from 10.9.150.68 Port 33280 ssh2
/VAR/LOG/SECURE:APR 15:16:30 youserver sshd[21536]: Failed password for user1 from 10.9.150.68 Port 33280 ssh2
/VAR/LOG/SECURE:APR 15:19:07 youserver sshd[21571]: Failed password for user1 from 10.9.150.68 Port 33281 ssh2

& Q
Held 5 Messages In/var/spool/mail/root

--------------------------------------------------------------------------------------------------------------- ---------------

Centos/rehl 6 SSH Log Filter script to prevent brute force

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.