How to Prevent ssh brute-force cracking on linux servers -- denyhosts Solution

Source: Internet
Author: User
Recently, I have been paying attention to the varlogsecure file of the Linux server and found that there are a large number of ssh failure attempts. The following shows the attempts and times of this IP address, from one o'clock P.M. a.m. to as many as 9288 scans, we can see that we are trying various user names to connect. It's really nothing to do, I don't know where to use the broken software. Recently I have been paying attention to the/var/log/secure file of the Linux server. I found that there are a large number of ssh failure attempts, as shown below: Check the attempts and time of the IP address from one o'clock P.M..As many as 9288 scans, we can see that we are trying various user names to connect. It's really nothing to do, and I don't know what software to use to get bored. Fortunately, my password is complicated enough, otherwise .......... there are multiple secure on my server, which are intercepted by time. I will collect statistics on the secure.1 file. Obtain the IP address and quantity :# Grep-O' [0-9] \ {1, 3 \}\. [0-9] \ {1, 3 \}\. [0-9] \ {1, 3 \}\. [0-9] \ {1, 3 \} '/var/log/secure.1 | Sort| Uniq-C is so much that I can't help but feel cold sweat. It's really scary. I don't know what kind of good things are on my server. So I like it !!!! Of course, if you have successfully connected through ssh, the record will also be shown here. To prevent such bored people from visiting again, you have to find a way to prevent them from scanning. I searched for information on the Internet and learned that Denyhosts can achieve this. DenyHosts is a program written in Python, it analyzes the sshd log file (/var/log/secure) and records IP addresses to/etc/hosts when repeated attacks are detected. deny file to achieve the automatic screen IP function. If you add them manually, you will not be able to get tired. DenyHosts Official Website: http://denyhosts.sourceforge.net
The attachment has been attached to this article, which is downloaded from this website and the version is newer than version 2.6. I. Check installation requirements
Preferred check whether Sshd supports T CpWrap. Denyhost can be installed only when Tcpwrap is supported.
# L Dd/Usr/sbin/sshd | grep wraplibwrap. so.0 =>/usr/lib/libwrap. so.0 (0x00864000) // if this information is displayed, the Python version can be checked again. Python or later versions can be directly installed.
# Python-V
Python 2.4.3 meets requirement 2: Install Denyhosts
Download the software first, and then ExtractInstall, decompress, and enter the source directory.
# Tar-xzvf DenyHosts-2.6.tar.gz
# CdDenyHosts-2.6
Execute the Python script for installation,
# Python SetUp. py install
The program script is automatically installed in/usr/share/denyhosts.
Library files are automatically installed in/usr/lib/python2.4/site-packages/DenyHosts
Install denyhosts. py to/usr/bin
3. Set the Startup Script
# Cd/usr/share/denyhosts/
Copy template files
# Cp daemon-con TrOl-dist daemon-control
Set the user and permission of the Startup Script
# ChownRoot daemon-control
# Chmod700 daemon-control
Generate the main configuration file of Denyhost (filter the header in the template file and import it to Denyhost. cfg)
# Grep-v "^ #" denyhosts. cfg-dist> denyhosts. cfg
Edit the Denyhost. cfg file and modify it as needed.
---------------- Denyhosts. cfg --------------------------------------
SECURE_LOG =/var/log/secure # ssh log file, which is determined based on this file. If there are other logs, you only need to change the name, for example, change secure to secure.1 and other HOSTS_DENY =/etc/hosts. deny
# Control the user login file and add the IP addresses that failed to connect multiple times to this file to shield the file
PURGE_DENY =
# How long will it take to clear the banned BLOCK_SERVICE = sshd if I leave it empty?
# Forbidden service name. If you want to add other services, you only need to add a comma to keep up with the corresponding service to DENY_THRESHOLD_INVALID = 1.
# Number of failed invalid Users Allowed
DENY_THRESHOLD_VALID = 2
# Number of Logon failures allowed for valid users
DENY_THRESHOLD_ROOT = 3
# Number of root logon failures allowed
HOSTNAME_LOOKUP = NO
# Whether to perform domain name reverse resolution. Here, ADMIN_EMAIL = is not used .... # Administrator email address, which will send an email to the Administrator
DAEMON_LOG =/var/log/denyhosts
# Your own log files Others:
AGE_RESET_VALID = 5d # (h indicates the hour, d indicates the day, m indicates the month, w indicates the week, y indicates the year)
AGE_RESET_ROOT = 25d
AGE_RESET_RESTRICTED = 25d
AGE_RESET_INVALID = 10d
# How long will the user's logon Failure count be reset to 0?
RESET_ON_SUCCESS = yes
# If an ip address is successfully logged on, check whether the logon count for failure is reset to 0.
DAEMON_SLEEP = 30 s # interval of each log file read when running in a later mode. DAEMON_PURGE = 1 h # The interval between the PURGE_DENY and termination of old entries in HOSTS_DENY when the server is running in later mode. This will affect the interval between PURGE_DENY. Add the Denyhost STARTUP script to Automatic startup
# Echo '/usr/share/denyhosts/daemon-control start'>/etc/rc. d/rc. local
Start the Denyhost Process
#/Usr/share/denyhosts/daemon-control start
You can see that Denyhost is running.
# Ps-ef | grep deny
When you use ssh to connect to another machine, when you enter the wrong password several times in a row, it will be automatically blocked, within a certain period of time, you cannot connect to the log files recorded by the ssh connection. Check my/etc/hosts. deny file and find that there are 135 records in it. # Cat/Etc/hosts. deny | Wc-L
135

This article is from the "Zong Jun" blog, please be sure to keep this source http://tech110.blog.51cto.com/438717/287490

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.