Top 20 OpenSSH Server Best Security Practices
2012-03-31 03:25
489 查看
OpenSSH(Open Secure Shell)是使用SSH透过计算机网络加密通讯的实现。它是取代由SSH Communications Security所提供的商用版本的开放源代码方案。OpenSSH被广泛建议用于远程登录,远程文件备份,通过scp或sftp传输。。。SSH很好的保持了两个网络和系统之间传输数据的机密性,数据完整性。不过,最主要的优势是服务器身份验证,通过公钥进加密。我们也常常听闻一些关于OpenSSH的0day爆出。要提高OpenSSH服务器的安全性,你应该了解下下面这些东东:默认配置文件和SSH端口/etc/ssh/sshd_config :OpenSSH服务器配置文件/etc/ssh/ssh_config :OpenSSH客户端配置文件~/.ssh/ :SSH用户配置目录。~/.ssh/authorized_keys or ~/.ssh/authorized_keys 公钥密钥列表,(RSA或DSA),可用于登录到该用户的帐户/etc/nologin :如果这个文件存在,sshd会拒绝除了root以外的用户登录。/etc/hosts.allow and /etc/hosts.deny : 两个文件是控制远程访问的配置,通过它可以允许或者拒绝某个ip或者ip段的客户访问系统的某项服务。如果请求访问的主机名或IP包含在/etc/hosts.allow中,那么访问验证会通过。如果请求访问的主机名或IP不包含在/etc/hosts.allow中,那么tcpd进程就查/etc/hosts.deny。看请求访问的主机名或IP有没有包含在hosts.deny文件中。如果包含,那么访问就被拒绝;如果既不包含在/etc/hosts.allow中,又不包含在 /etc/hosts.deny中,那么此访问也不会被允许。SSH default port : TCP 22#1:禁用OpenSSH服务器如果您的服务器或者工作站不需要SSH远程登录或者文件传输。那么您可以禁用或者删除SSH服务。CentOS/RHEL/Fedora Linux用户禁用SSH服务的命令:# chkconfig sshd off 删除SSH服务的yum命令:# yum erase-server Debian/ Ubuntu Linux用户禁用或删除SSH服务命令:# apt-get remove openssh-server (禁用、删除都用该命令)
对了,你或许更新你的iptables脚本去删除SSH的筛选规则。CentOS/RHEL/Fedora下编辑这两个文件/etc/sysconfig/iptables 和 /etc/sysconfig/ip6tables 。然后重启iptables服务即可。重启命令:# service iptables restart# service ip6tables restart
#2:只使用SSH Protocol2SSH protocol version 1(SSH-1)存在中间人攻击的问漏洞,是已经过时了的版本,所以最好别再使用。打开sshd_config文件,并确保存在 Protocol 2 。
#3限制用户的SSH访问默认情况下,所有系统用户可以使用自己的密码或公钥通过SSH登录。有时候你可能创建的UNIX/Linux用户帐户,或者FTP账户。然而,这些用户是可以通过ssh登录到系统。并且他们也
有使用像编译器之类的一些系统工具的权限。脚本语言,如Perl,Python中可以打开的端口或者干其他事儿。如果
你限制用户的SSH访问,即使攻击者通过一个PHP脚本在系统上创建一个帐户。但是,他也不能通过SSH访问系统,因
为他创建的账户不在AllowUsers中。是不是系统安全系数又提高了呢?如果你只容许root、systemerror和systembrother这三个用户使用SSH登陆系统,那么,你只需要在sshd_config配
置文件中写入:AllowUsers root systemerror systembrother或者,您可能允许所有用户通过SSH登录登陆,只有限制很少的用户通过SSH登陆(比如你要限制hacker跟thief这俩人),那么,你就可以再配置文件中写入:DenyUsers hacker thief您也可以限制用户组通过SSH访问系统,方法大同小异,你只需要AllowUsers改成AllowGroups即可。配置Linux PAM限制用户登陆,可参照:http://coldstone.blog.51cto.com/2314243/822021
不翻译鸟,麻烦死鸟...原文,有兴趣的童鞋自己看:
Output:
Explains how to setup DenyHosts under RHEL / Fedora and CentOS Linux.
Fail2ban is a similar program that prevents brute force attacks against SSH.
security/sshguard-pf protect hosts from brute force attacks against ssh and other services using pf.
security/sshguard-ipfw protect hosts from brute force attacks against ssh and other services using ipfw.
security/sshguard-ipfilter protect hosts from brute force attacks against ssh and other services using ipfilter.
security/sshblock block abusive SSH login attempts.
security/sshit checks for SSH/FTP bruteforce and blocks given IPs.
BlockHosts Automatic blocking of abusive IP hosts.
Blacklist Get rid of those bruteforce attempts.
Brute Force Detection A modular shell script for parsing application logs and checking for authentication failures. It does this using a rules system where application specific options are stored including regular expressions for each unique auth format.
IPQ BDB filter May be considered as a fail2ban lite.
Multiple-port knocking Netfilter/IPtables only implementation.
对了,你或许更新你的iptables脚本去删除SSH的筛选规则。CentOS/RHEL/Fedora下编辑这两个文件/etc/sysconfig/iptables 和 /etc/sysconfig/ip6tables 。然后重启iptables服务即可。重启命令:# service iptables restart# service ip6tables restart
#2:只使用SSH Protocol2SSH protocol version 1(SSH-1)存在中间人攻击的问漏洞,是已经过时了的版本,所以最好别再使用。打开sshd_config文件,并确保存在 Protocol 2 。
#3限制用户的SSH访问默认情况下,所有系统用户可以使用自己的密码或公钥通过SSH登录。有时候你可能创建的UNIX/Linux用户帐户,或者FTP账户。然而,这些用户是可以通过ssh登录到系统。并且他们也
有使用像编译器之类的一些系统工具的权限。脚本语言,如Perl,Python中可以打开的端口或者干其他事儿。如果
你限制用户的SSH访问,即使攻击者通过一个PHP脚本在系统上创建一个帐户。但是,他也不能通过SSH访问系统,因
为他创建的账户不在AllowUsers中。是不是系统安全系数又提高了呢?如果你只容许root、systemerror和systembrother这三个用户使用SSH登陆系统,那么,你只需要在sshd_config配
置文件中写入:AllowUsers root systemerror systembrother或者,您可能允许所有用户通过SSH登录登陆,只有限制很少的用户通过SSH登陆(比如你要限制hacker跟thief这俩人),那么,你就可以再配置文件中写入:DenyUsers hacker thief您也可以限制用户组通过SSH访问系统,方法大同小异,你只需要AllowUsers改成AllowGroups即可。配置Linux PAM限制用户登陆,可参照:http://coldstone.blog.51cto.com/2314243/822021
不翻译鸟,麻烦死鸟...原文,有兴趣的童鞋自己看:
#4: Configure Idle Log Out Timeout Interval
User can login to server via ssh and you can set an idel timeout interval to avoid unattended ssh session. Open sshd_config and make sure following values are configured:ClientAliveInterval 300 ClientAliveCountMax 0You are setting an idle timeout interval in seconds (300 secs = 5 minutes). After this interval has passed, the idle user will be automatically kicked out (read as logged out). See how to automatically log BASH / TCSH / SSH usersout after a period of inactivity for more details.
#5: Disable .rhosts Files
Don't read the user's ~/.rhosts and ~/.shosts files. Update sshd_config with the following settings:IgnoreRhosts yesSSH can emulate the behavior of the obsolete rsh command, just disable insecure access via RSH.
#6: Disable Host-Based Authentication
To disable host-based authentication, update sshd_config with the following option:HostbasedAuthentication no
#7: Disable root Login via SSH
There is no need to login as root via ssh over a network. Normal users can use su or sudo (recommended) to gain root level access. This also make sure you get full auditing information about who ran privileged commands on the system via sudo. To disable root login via SSH, update sshd_config with the following line:PermitRootLogin noHowever, bob made excellent point:Saying "don't login as root" is h******t. It stems from the days when people sniffed the first packets of sessions so logging in as yourself and su-ing decreased the chance an attacker would see the root pw, and decreast the chance you got spoofed as to your telnet host target, You'd get your password spoofed but not root's pw. Gimme a break. this is 2005 - We have ssh, used properly it's secure. used improperly none of this 1989 will make a damn bit of difference. -Bob
#8: Enable a Warning Banner
Set a warning banner by updating sshd_config with the following line:Banner /etc/issueSample /etc/issue file:
---------------------------------------------------------------------------------------------- You are accessing a XYZ Government (XYZG) Information System (IS) that is provided for authorized use only. By using this IS (which includes any device attached to this IS), you consent to the following conditions: + The XYZG routinely intercepts and monitors communications on this IS for purposes including, but not limited to, penetration testing, COMSEC monitoring, network operations and defense, personnel misconduct (PM), law enforcement (LE), and counterintelligence (CI) investigations. + At any time, the XYZG may inspect and seize data stored on this IS. + Communications using, or data stored on, this IS are not private, are subject to routine monitoring, interception, and search, and may be disclosed or used for any XYZG authorized purpose. + This IS includes security measures (e.g., authentication and access controls) to protect XYZG interests--not for your personal benefit or privacy. + Notwithstanding the above, using this IS does not constitute consent to PM, LE or CI investigative searching or monitoring of the content of privileged communications, or work product, related to personal representation or services by attorneys, psychotherapists, or clergy, and their assistants. Such communications and work product are private and confidential. See User Agreement for details. ----------------------------------------------------------------------------------------------Above is standard sample, consult your legal team for exact user agreement and legal notice details.
#8: Firewall SSH Port # 22
You need to firewall ssh port # 22 by updating iptables or pf firewall configurations. Usually, OpenSSH server must only accept connections from your LAN or other remote WAN sites only.Netfilter (Iptables) Configuration
Update /etc/sysconfig/iptables (Redhat and friends specific file) to accept connection only from 192.168.1.0/24 and 202.54.1.5/29, enter:-A RH-Firewall-1-INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 22 -j ACCEPT -A RH-Firewall-1-INPUT -s 202.54.1.5/29 -m state --state NEW -p tcp --dport 22 -j ACCEPTIf you've dual stacked sshd with IPv6, edit /etc/sysconfig/ip6tables (Redhat and friends specific file), enter:
-A RH-Firewall-1-INPUT -s ipv6network::/ipv6mask -m tcp -p tcp --dport 22 -j ACCEPTReplace ipv6network::/ipv6mask with actual IPv6 ranges.
*BSD PF Firewall Configuration
If you are using PF firewall update /etc/pf.conf as follows:pass in on $ext_if inet proto tcp from {192.168.1.0/24, 202.54.1.5/29} to $ssh_server_ip port ssh flags S/SA synproxy state
#9: Change SSH Port and Limit IP Binding
By default SSH listen to all available interfaces and IP address on the system. Limit ssh port binding and change ssh port (by default brute forcing scripts only try to connects to port # 22). To bind to 192.168.1.5 and 202.54.1.5 IPs and to port 300, add or correct the following line:Port 300 ListenAddress 192.168.1.5 ListenAddress 202.54.1.5A better approach to use proactive approaches scripts such as fail2ban or denyhosts (see below).
#10: Use Strong SSH Passwords and Passphrase
It cannot be stressed enough how important it is to use strong user passwords and passphrase for your keys. Brute force attack works because you use dictionary based passwords. You can force users to avoid passwords against a dictionary attack and use john the ripper tool to find out existing weak passwords. Here is a sample random password generator (put in your ~/.bashrc):genpasswd() { local l=$1 [ "$l" == "" ] && l=20 tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs }Run it:
genpasswd 16
Output:
uw8CnDVMwC6vOKgW
#11: Use Public Key Based Authentication
Use public/private key pair with password protection for the private key. See how to use RSA and DSA key based authentication. Never ever use passphrase free key (passphrase key less) login.#12: Use Keychain Based Authentication
keychain is a special bash script designed to make key-based authentication incredibly convenient and flexible. It offers various security benefits over passphrase-free keys. See how to setup and use keychain software.#13: Chroot SSHD (Lock Down Users To Their Home Directories)
By default users are allowed to browse the server directories such as /etc/, /bin and so on. You can protect ssh, using os based chroot or use special tools such as rssh. With the release of OpenSSH 4.8p1 or 4.9p1, you no longer have to rely on third-party hacks such as rssh or complicated chroot(1) setups to lock users to their home directories. See this blog post about new ChrootDirectory directive to lock down users to their home directories.#14: Use TCP Wrappers
TCP Wrapper is a host-based Networking ACL system, used to filter network access to Internet. OpenSSH does supports TCP wrappers. Just update your /etc/hosts.allow file as follows to allow SSH only from 192.168.1.2 172.16.23.12 :sshd : 192.168.1.2 172.16.23.12See this FAQ about setting and using TCP wrappers under Linux / Mac OS X and UNIX like operating systems.
#15: Disable Empty Passwords
You need to explicitly disallow remote login from accounts with empty passwords, update sshd_config with the following line:PermitEmptyPasswords no
#16: Thwart SSH Crackers (Brute Force Attack)
Brute force is a method of defeating a cryptographic scheme by trying a large number of possibilities using a single or distributed computer network. To prevents brute force attacks against SSH, use the following softwares:DenyHosts is a Python based security tool for SSH servers. It is intended to prevent brute force attacks on SSH servers by monitoring invalid login attempts in the authentication log and blocking the originating IP addresses.Explains how to setup DenyHosts under RHEL / Fedora and CentOS Linux.
Fail2ban is a similar program that prevents brute force attacks against SSH.
security/sshguard-pf protect hosts from brute force attacks against ssh and other services using pf.
security/sshguard-ipfw protect hosts from brute force attacks against ssh and other services using ipfw.
security/sshguard-ipfilter protect hosts from brute force attacks against ssh and other services using ipfilter.
security/sshblock block abusive SSH login attempts.
security/sshit checks for SSH/FTP bruteforce and blocks given IPs.
BlockHosts Automatic blocking of abusive IP hosts.
Blacklist Get rid of those bruteforce attempts.
Brute Force Detection A modular shell script for parsing application logs and checking for authentication failures. It does this using a rules system where application specific options are stored including regular expressions for each unique auth format.
IPQ BDB filter May be considered as a fail2ban lite.
#17: Rate-limit Incoming Port # 22 Connections
Both netfilter and pf provides rate-limit option to perform simple throttling on incoming connections on port # 22.Iptables Example
The following example will drop incoming connections which make more than 5 connection attempts upon port 22 within 60 seconds:#!/bin/bash inet_if=eth1 ssh_port=22 $IPT -I INPUT -p tcp --dport ${ssh_port} -i ${inet_if} -m state --state NEW -m recent --set $IPT -I INPUT -p tcp --dport ${ssh_port} -i ${inet_if} -m state --state NEW -m recent --update --seconds 60 --hitcount 5 -j DROPCall above script from your iptables scripts. Another config option:
$IPT -A INPUT -i ${inet_if} -p tcp --dport ${ssh_port} -m state --state NEW -m limit --limit 3/min --limit-burst 3 -j ACCEPT $IPT -A INPUT -i ${inet_if} -p tcp --dport ${ssh_port} -m state --state ESTABLISHED -j ACCEPT $IPT -A OUTPUT -o ${inet_if} -p tcp --sport ${ssh_port} -m state --state ESTABLISHED -j ACCEPT # another one line example # $IPT -A INPUT -i ${inet_if} -m state --state NEW,ESTABLISHED,RELATED -p tcp --dport 22 -m limit --limit 5/minute --limit-burst 5-j ACCEPTSee iptables man page for more details.
*BSD PF Example
The following will limits the maximum number of connections per source to 20 and rate limit the number of connections to 15 in a 5 second span. If anyone breaks our rules add them to our abusive_ips table and block them for making any further connections. Finally, flush keyword kills all states created by the matching rule which originate from the host which exceeds these limits.sshd_server_ip="202.54.1.5" table <abusive_ips> persist block in quick from <abusive_ips> pass in on $ext_if proto tcp to $sshd_server_ip port ssh flags S/SA keep state (max-src-conn 20, max-src-conn-rate 15/5, overload <abusive_ips> flush)
#18: Use Port Knocking
Port knocking is a method of externally opening ports on a firewall by generating a connection attempt on a set of prespecified closed ports. Once a correct sequence of connection attempts is received, the firewall rules are dynamically modified to allow the host which sent the connection attempts to connect over specific port(s). A sample port Knocking example for ssh using iptables:$IPT -N stage1 $IPT -A stage1 -m recent --remove --name knock $IPT -A stage1 -p tcp --dport 3456 -m recent --set --name knock2 $IPT -N stage2 $IPT -A stage2 -m recent --remove --name knock2 $IPT -A stage2 -p tcp --dport 2345 -m recent --set --name heaven $IPT -N door $IPT -A door -m recent --rcheck --seconds 5 --name knock2 -j stage2 $IPT -A door -m recent --rcheck --seconds 5 --name knock -j stage1 $IPT -A door -p tcp --dport 1234 -m recent --set --name knock $IPT -A INPUT -m --state ESTABLISHED,RELATED -j ACCEPT $IPT -A INPUT -p tcp --dport 22 -m recent --rcheck --seconds 5 --name heaven -j ACCEPT $IPT -A INPUT -p tcp --syn -j doofwknop is an implementation that combines port knocking and passive OS fingerprinting.
Multiple-port knocking Netfilter/IPtables only implementation.
#19: Use Log Analyzer
Read your logs using logwatch or logcheck. These tools make your log reading life easier. It will go through your logs for a given period of time and make a report in the areas that you wish with the detail that you wish. Make sure LogLevel is set to INFO or DEBUG in sshd_config:LogLevel INFO
#20: Patch OpenSSH and Operating Systems
It is recommended that you use tools such as yum, apt-get, freebsd-update and others to keep systems up to date with the latest security patches.Other Options
To hide openssh version, you need to update source code and compile openssh again. Make sure following options are enabled in sshd_config:# Turn on privilege separation UsePrivilegeSeparation yes # Prevent the use of insecure home directory and key file permissions StrictModes yes # Turn on reverse name checking VerifyReverseMapping yes # Do you need port forwarding? AllowTcpForwarding no X11Forwarding no # Specifies whether password authentication is allowed. The default is yes. PasswordAuthentication noVerify your sshd_config file before restarting / reloading changes:
# /usr/sbin/sshd -t
相关文章推荐
- Top 20 OpenSSH Server Best Security Practices
- Top 20 OpenSSH Server Best Security Practices
- Top 10 steps to optimize data access in SQL Server: Part II (Re-factor TSQL and apply best practices)
- Web Server Security Best Practices
- VDS Best Practices – Rack Server Deployment with Two 10 Gigabit adapters (Part 4 of 6)
- Top 20 Best Agile Development Books, Ever
- 5 Best Practices to Secure and Protect SSH Server
- Top 11 Best Practices in Software Testing
- Best Practices for Enterprise Security
- 微软推出Microsoft Exchange Server Best Practices Analyzer Tool
- Top 5 vSphere Best Practices missed by the Enterprise
- BizTalk Server 2006 Best Practices Analyzer
- SQL Server administration best practices
- http://docs.aliyun.com/#/rds/best-practices/collocation&security
- Disk Partition Alignment Best Practices for SQL Server
- Top 10 Backup and Recovery best practices
- Core Security Patterns : Best Practices and Strategies for J2EE(TM), Web Services, and Identity Mana
- Top 15 Ant Best Practices
- Top 10 Backup and Recovery Best Practices (文档 ID 388422.1)
- Best Practices for AWS Security Using CloudCheckr