前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >总结几个常用的系统安全设置(含DenyHosts)

总结几个常用的系统安全设置(含DenyHosts)

作者头像
洗尽了浮华
发布2022-03-29 12:24:31
2.8K0
发布2022-03-29 12:24:31
举报
文章被收录于专栏:散尽浮华散尽浮华

1)禁止系统响应任何从外部/内部来的ping请求攻击者一般首先通过ping命令检测此主机或者IP是否处于活动状态 如果能够ping通 某个主机或者IP,那么攻击者就认为此系统处于活动状态,继而进行攻击或破坏。如果没有人能ping通机器并收到响应,那么就可以大大增强服务器的安全性, linux下可以执行如下设置,禁止ping请求: [root@localhost ~]# echo "1"> /proc/sys/net/ipv4/icmp_echo_ignore_all

默认情况下"icmp_echo_ignore_all"的值为"0",表示响应ping操作。 可以加上面的一行命令到/etc/rc.d/rc.local文件中,以使每次系统重启后自动运行。

2)禁止Control-Alt-Delete组合键重启系统 在linux的默认设置下,同时按下Control-Alt-Delete键,系统将自动重启,这是很不安全的,因此要禁止Control-Alt-Delete组合键重启系统,查看/etc/inittab文件: [root@localhost ~]# cat /etc/inittab ....... # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf .......

[root@localhost ~]# cat /etc/init/control-alt-delete.conf # control-alt-delete - emergency keypress handling # # This task is run whenever the Control-Alt-Delete key combination is # pressed. Usually used to shut down the machine. # # Do not edit this file directly. If you want to change the behaviour, # please create a file control-alt-delete.override and put your changes there.

start on control-alt-delete

exec /sbin/shutdown -r now "Control-Alt-Delete pressed"

可以将/etc/init/control-alt-delete.conf文件删除或移走或更名。 [root@localhost ~]# mv /etc/init/control-alt-delete.conf /etc/init/control-alt-delete.conf.bak

或者将/etc/init/control-alt-delete.conf文件里的内容注释掉!

3)限制Shell记录历史命令大小 默认情况下,bash shell会在文件$HOME/.bash_history中存放多达1000条命令记录(根据系统不同,默认记录条数不同)。系统中每个用户的主目录下都有一个这样的文件。 这么多的历史命令记录,肯定是不安全的,因此必须限制该文件的大小。 可以编辑/etc/profile文件,修改其中的选项如下: [root@localhost ~]# vim /etc/profile ....... HISTSIZE=1000 .......

默认Shell记录历史命令的条数是1000,可以修改为100条,即HISTSIZE=1000,表示在文件$HOME/.bash_history中记录最近的30条历史命令。 如果将"HISTSIZE"设置为0,则表示不记录历史命令,那么也就不能用键盘的上下键查找历史命令了。

4)设定tcp_wrappers防火墙 Tcp_Wrappers是一个用来分析TCP/IP封包的软件,类似的IP封包软件还有iptables,linux默认都安装了此软件,作为一个安全的系统,Linux本身有两层安全防火墙,通过IP 过滤机制的iptables实现第一层防护,iptables防火墙通过直观地监视系统的运行状况,阻挡网络中的一些恶意攻击,保护整个系统正常运行,免遭攻击和破坏。 如果通过了iptables的第一层防护,那么下一层防护就是tcp_wrappers了,通过Tcp_Wrappers可以实现对系统中提供的某些服务的开放与关闭、允许和禁止,从而更有效地保 证系统安全运行。 4.1)Tcp_Wrappers的使用很简单,仅仅两个配置文件:/etc/hosts.allow和/etc/hosts.deny查看系统是否安装了Tcp_Wrappers: [root@localhost ~]# rpm -q tcp_wrappers tcp_wrappers-7.6-58.el6.x86_64 如果有上面的类似输出,表示系统已经安装了tcp_wrappers模块。如果没有显示,可能是没有安装,可以从linux系统安装盘找到对应RPM包进行安装。

4.2)tcp_wrappers防火墙的局限性 系统中的某个服务是否可以使用tcp_wrappers防火墙,取决于该服务是否应用了libwrapped库文件,如果应用了就可以使用tcp_wrappers防火墙,系统中默认的一些服务如: sshd、portmap、sendmail、xinetd、vsftpd、tcpd等都可以使用tcp_wrappers防火墙。

4.3)tcp_wrappers设定的规则(使用例子可以参考:http://www.cnblogs.com/kevingrace/p/6245859.html) tcp_wrappers防火墙的实现是通过/etc/hosts.allow和/etc/hosts.deny两个文件来完成的,首先看一下设定的格式: service:host(s) [:action]

service:代表服务名,例如sshd、vsftpd、sendmail等。 host(s):主机名或者IP地址,可以有多个,例如192.168.60.0、www.ixdba.netl action:动作, 符合条件后所采取的动作。 几个关键字: ALL:所有服务或者所有IP。 ALL EXCEPT:所有的服务或者所有IP除去指定的。

例如:ALL:ALL EXCEPT 192.168.60.132 表示除了192.168.60.132这台机器,任何机器执行所有服务时或被允许或被拒绝。 了解了设定语法后,下面就可以对服务进行访问限定。

例如:互联网上一台linux服务器,实现的目标是:仅仅允许222.90.66.4、61.185.224.66以及域名softpark.com通过SSH服务远程登录到系统,设置如下: 首先设定允许登录的计算机,即配置/etc/hosts.allow文件,设置很简单,只要修改/etc/hosts.allow(如果没有此文件,请自行建立)这个文件即可。 只需将下面规则加入/etc/hosts.allow即可。 sshd: 222.90.66.4 61.185.224.66 softpark.com 接着设置不允许登录的机器,也就是配置/etc/hosts.deny文件了。 一般情况下,linux会首先判断/etc/hosts.allow这个文件,如果远程登录的计算机满足文件/etc/hosts.allow设定的话,就不会去使用/etc/hosts.deny文件了,相反,如果 不满足hosts.allow文件设定的规则的话,就会去使用hosts.deny文件了,如果满足hosts.deny的规则,此主机就被限制为不可访问linux服务器,如果也不满足hosts.deny的 设定,此主机默认是可以访问linux服务器的,因此,当设定好/etc/hosts.allow文件访问规则之后,只需设置/etc/hosts.deny为"所有计算机都不能登录状态"即可。 sshd:ALL

这样,一个简单的tcp_wrappers防火墙就设置完毕了。

5)网络安全选项的设定 编辑 "/etc/sysctl.conf" 档案,并加入下面几行, [root@localhost ~]# vim /etc/sysctl.conf # Enable ignoring broadcasts request(让系统对广播没有反应) net.ipv4.icmp_echo_ignore_broadcasts = 1

# Disables IP source routing(取消 IP source routing) net.ipv4.conf.all.accept_source_route = 0

# Enable TCP SYN Cookie Protection(开启 TCP SYN Cookie 保护) net.ipv4.tcp_syncookies = 1

# Disable ICMP Redirect Acceptance(取消 ICMP 接受 Redirect) net.ipv4.conf.all.accept_redirects = 0

# Enable bad error message Protection(开启错误讯息保护) net.ipv4.icmp_ignore_bogus_error_responses = 1

# Enable IP spoofing protection, turn on Source Address Verification(开启 IP 欺骗保护) net.ipv4.conf.all.rp_filter = 1

# Log Spoofed Packets, Source Routed Packets, Redirect Packets(记录Spoofed Packets, Source Routed Packets, Redirect Packets) net.ipv4.conf.all.log_martians = 1

最后重新启动network [root@localhost ~]# /etc/rc.d/init.d/network restart

或者sysctl –p 生效 [root@localhost ~]# echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all

6)系统文件和日志的安全 更改各种服务启动脚本目录的访问权限: [root@localhost ~]# chmod -R 700 /etc/rc.d/

更改系统文件的访问权限: [root@localhost ~]# chmod 700 /etc/services [root@localhost ~]# chmod 700 /etc/xinetd.conf [root@localhost ~]# chmod 700 /etc/inittab

更改日志的访访权限: [root@localhost ~]# chmod 700 /var/log/secure* [root@localhost ~]# chmod 700 /var/log/messages*

7)系统登陆的设定 每次登陆退出时,清除所用过命令的历史纪: 在.bash_logout文档中增加一行: [root@localhost ~]# vim /root/.bashrc ...... history –c

设置系统全局环境变量文件/etc/profile 增加一行: [root@localhost ~]# vim /etc/profile ...... TMOUT=600

单位为秒,表示如果在600秒(10分钟)之内没有做任何的操作,登陆终端将自动注销。 3.资源限制 编辑/etc/security/limits.conf 加入: [root@localhost ~]# vim /etc/security/limits.conf ...... * hard core 0 * hard rss 5000 * hard nproc 20

同时必须编辑/etc/pam.d/login文件加/检查下面这一行的存在: [root@localhost ~]# vim /etc/pam.d/login ....... session required /lib/security/pam_limits.so

上面的命令分别是禁止创建core文件,其他用户(除root)最多使用5M内存,限制最多进程数为20

8)vsftp配置 vsftpd的配置文件有三个,分别是: /etc/vsftpd/vsftpd.conf /etc/ vsftpd/vsftpd.ftpusers /etc/ vsftpd/vsftpd.user_list

其中: /etc/vsftpd.conf是主配置文件 /etc/vsftpd.ftpusers中指定了哪些用户不能访问FTP服务器 /etc/vsftpd.user_list中指定的用户默认情况下(即在/etc/vsftpd.conf中设置了userlist_deny= YES)不能访问FTP服务器

当在/etc/vsftpd.conf中设置了userlist_deny=NO时,仅仅允许/etc/vsftpd.user_list中指定的用户访问FTP服务器。 一般要在/etc/vsftpd.conf中设置userlist_deny=NO,通过/etc/vsftpd.user_list严格控制FTP用户。

配置基本的性能和安全选项 //禁止匿名登陆 anonymous_enable=NO (默认为YES) //设置空闲的用户会话的中断时间 例如下面的配置: idle_session_timeout=600 将在用户会话空闲10分钟后被中断。

//设置空闲的数据连接的的中断时间 例如下面的配置: data_connection_timeout=120 将在数据连接空闲2分钟后被中断。

//设置客户端空闲时的自动中断和激活连接的时间 例如下面的配置: accept_timeout=60 connect_timeout=60 将使客户端空闲1分钟后自动中断连接,并在中断1分钟后自动激活连接

//设置最大传输速率限制 例如下面的配置:(传输速率可根据实际情况自行修改) local_max_rate=50000 anon_max_rate=30000 将使本地用户的最大传输速率为50kbytes / sec,匿名用户的 传输速率为30 kbytes / sec。 //设置客户端连接时的端口范围 例如下面的配置: pasv_min_port=50000 pasv_max_port=60000 将使客户端连接时的端口范围在50000和60000之间。这提高了系统的安全性

截短历史命令 "~/.bash_history"文件,这个文件中保存着以前使用的命令列表。截短这个文件可以使您以前执行过的命令暴露在别人眼光下的机会减小. (在您的命令中很可能包括象密码信息这样的保密信息)。通过编辑/etc/profile的下面两项可以做到这一点: HISTFILESIZE=20

通过下面的措施可以防止任何人都可以su为root: 编辑su文件(vi /etc/pam.d/su)在文件的头部加入下面两行: auth sufficient /lib/security/pam_rootok.so debug auth required /lib/security/pam_wheel.so group=wheel

9)使用DenyHosts阻止SSH暴力破解(同时可以保护FTP) 当你的Linux服务器暴露在互联网之中,该服务器将会遭到互联网上的扫描软件进行扫描,并试图猜测SSH登录口令,不可避免的有病毒或无聊的人会尝试攻击SSH。。你会发现,每天会有多条SSH登录失败纪录。那些扫描工具将对你的服务器构成威胁,你必须设置复杂登录口令,并将尝试多次登录失败的IP给阻止掉,让其在一段时间内不能访问该服务器。DenyHosts是Python语言写的一个程序,用DenyHosts可以阻止试图猜测SSH登录口令,它会分析/var/log/secure等日志文件,当发现有重复的IP在进行多次SSH密码尝试时就会记录IP到/etc/hosts.deny文件,从而达到自动屏蔽该IP的目的。

下载DenyHosts-2.6.tar.gz安装包以下是安装记录: [root@localhost ~]# wget http://sourceforge.net/projects/denyhosts/files/denyhosts/2.6/DenyHosts-2.6.tar.gz [root@localhost ~]# tar -zxvf DenyHosts-2.6.tar.gz [root@localhost ~]# cd DenyHosts-2.6 [root@localhost DenyHosts-2.6]# python setup.py install

默认是安装到/usr/share/denyhosts/目录的。 [root@localhost DenyHosts-2.6]# cd /usr/share/denyhosts/ [root@localhost denyhosts]# cp denyhosts.cfg-dist denyhosts.cfg [root@localhost denyhosts]# cp daemon-control-dist daemon-control [root@localhost denyhosts]# vim daemon-control DENYHOSTS_BIN = "/usr/bin/denyhosts.py" DENYHOSTS_LOCK = "/var/lock/subsys/denyhosts" DENYHOSTS_CFG = "/usr/share/denyhosts/denyhosts.cfg"

[root@localhost denyhosts]# chown root daemon-control [root@localhost denyhosts]# chmod 700 daemon-control

完了之后执行daemon-contron start就可以了(重启是restart)。 [root@localhost denyhosts]# ./daemon-control start //或者执行"service denyhosts start"命令来启动

如果要使DenyHosts每次重起后自动启动还需做如下设置: [root@localhost denyhosts]# cd /etc/init.d [root@localhost denyhosts]# ln -s /usr/share/denyhosts/daemon-control denyhosts [root@localhost denyhosts]# chkconfig --add denyhosts [root@localhost denyhosts]# chkconfig --level 2345 denyhosts on

或者修改/etc/rc.local文件: [root@localhost denyhosts]# vim /etc/rc.local //加入下面这条命令 /usr/share/denyhosts/daemon-control start

DenyHosts配置文件说明: [root@localhost denyhosts]# vim denyhosts.cfg #这里根据自己需要进行相应的配置 SECURE_LOG = /var/log/secure #sshd日志文件,它是根据这个文件来判断的,不同的操作系统,文件名稍有不同。 HOSTS_DENY = /etc/hosts.deny #控制用户登陆的文件 PURGE_DENY = 5m #过多久后清除已经禁止的 BLOCK_SERVICE = sshd BLOCK_SERVICE = vsftpd #(如果启用FTP服务,务必加上这一行) ,禁止的服务名 DENY_THRESHOLD_INVALID = 1 #允许无效用户失败的次数

DENY_THRESHOLD_VALID = 10 #允许普通用户登陆失败的次数 DENY_THRESHOLD_ROOT = 5 #允许root登陆失败的次数 HOSTNAME_LOOKUP=NO #是否做域名反解 DAEMON_LOG = /var/log/denyhosts #DenyHosts的日志文件

DenyHosts防SSH暴力破解测试:

代码语言:javascript
复制
192.168.10.200服务器按照上面步骤安装并启动了DenyHosts
现在在另一台服务器192.168.10.205上用ssh工具连接192.168.10.200,用错误的密码尝试几次:
[root@host-205 ~]# ssh -p22 root@192.168.10.200
root@192.168.10.200's password: 
Permission denied, please try again.
root@192.168.10.200's password: 
Permission denied, please try again.
root@192.168.10.200's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

[root@host-205 ~]# ssh -p22 root@192.168.10.200
root@192.168.10.200's password: 
Permission denied, please try again.
root@192.168.10.200's password: 
Permission denied, please try again.
root@192.168.10.200's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

[root@host-205 ~]# ssh -p22 root@192.168.10.200
ssh_exchange_identification: Connection closed by remote host

[root@host-205 ~]# ssh -p22 root@192.168.10.200
ssh_exchange_identification: Connection closed by remote host

发现在192.168.10.205上使用错误密码去ssh连接192.168.10.200机器失败多数后,就是禁止205这台机器去ssh连接200机器了。

查看192.168.10.200机器的/var/log/denyhosts,可以发现如下信息:
[root@host-200 ~]# tail -f /var/log/denyhosts
......
2018-02-12 00:59:59,320 - denyhosts   : INFO     monitoring log: /var/log/secure
2018-02-12 00:59:59,320 - denyhosts   : INFO     sync_time: 3600
2018-02-12 00:59:59,320 - denyhosts   : INFO     purging of /etc/hosts.deny is disabled
2018-02-12 00:59:59,320 - denyhosts   : INFO     denyhosts synchronization disabled
2018-02-12 01:16:00,203 - denyhosts   : INFO     new denied hosts: ['192.168.10.205']

同时,/etc/hosts.deny中也会加入192.168.10.205,即禁止这台机器使用ssh来连接:
[root@host-200 ~]# cat  /etc/hosts.deny
#
# hosts.deny  This file contains access rules which are used to
#   deny connections to network services that either use
#   the tcp_wrappers library or that have been
#   started through a tcp_wrappers-enabled xinetd.
#
#   The rules in this file can also be set up in
#   /etc/hosts.allow with a 'deny' option instead.
#
#   See 'man 5 hosts_options' and 'man 5 hosts_access'
#   for information on rule syntax.
#   See 'man tcpd' for information on tcp_wrappers
#
sshd: 192.168.10.205

若要手动允许该IP通过ssh访问,请把对应的IP从/etc/hosts.deny中删掉,然后,重启denyhosts服务即可。

除了上面源码方式安装,还可以采用yum安装DenyHosts

代码语言:javascript
复制
1)yum 安装 denyhosts
[root@denyhosts ~]# yum search denyhosts
[root@denyhosts ~]# yum install denyhosts
[root@denyhosts ~]# rpm -ql denyhosts

2)denyhosts几个比较重要的目录
该目录中主要存放计划任务,日志压缩 以及 chkconfig 和 service 启动的文档
/etc/cron.d/denyhosts
/etc/denyhosts.conf
/etc/logrotate.d/denyhosts
/etc/rc.d/init.d/denyhosts
/etc/sysconfig/denyhosts

该目录中主要存放 denyhosts 所拒绝及允许的一些主机信息
/var/lib/denyhosts/allowed-hosts
/var/lib/denyhosts/allowed-warned-hosts
/var/lib/denyhosts/hosts
/var/lib/denyhosts/hosts-restricted
/var/lib/denyhosts/hosts-root
/var/lib/denyhosts/hosts-valid
/var/lib/denyhosts/offset
/var/lib/denyhosts/suspicious-logins
/var/lib/denyhosts/sync-hosts
/var/lib/denyhosts/users-hosts
/var/lib/denyhosts/users-invalid
/var/lib/denyhosts/users-valid
/var/log/denyhosts

3)来看看 /etc/denyhosts.conf 中的配置参数
[root@denyhosts ~]# egrep -v "(^$|^#)" /etc/denyhosts.conf 
############ THESE SETTINGS ARE REQUIRED ############
# 系统安全日志文件,主要获取ssh信息
SECURE_LOG = /var/log/secure

# 拒绝写入IP文件 hosts.deny
HOSTS_DENY = /etc/hosts.deny

# 过多久后清除已经禁止的,其中w代表周,d代表天,h代表小时,s代表秒,m代表分钟
PURGE_DENY = 4w

# denyhosts所要阻止的服务名称
BLOCK_SERVICE  = sshd

# 允许无效用户登录失败的次数
DENY_THRESHOLD_INVALID = 3

# 允许普通用户登录失败的次数
DENY_THRESHOLD_VALID = 10

# 允许ROOT用户登录失败的次数
DENY_THRESHOLD_ROOT = 6

# 设定 deny host 写入到该资料夹
DENY_THRESHOLD_RESTRICTED = 1

# 将deny的host或ip纪录到Work_dir中 
WORK_DIR = /var/lib/denyhosts

SUSPICIOUS_LOGIN_REPORT_ALLOWED_HOSTS=YES

# 是否做域名反解
HOSTNAME_LOOKUP=YES

# 将DenyHOts启动的pid纪录到LOCK_FILE中,已确保服务正确启动,防止同时启动多个服务
LOCK_FILE = /var/lock/subsys/denyhosts

############ THESE SETTINGS ARE OPTIONAL ############
# 管理员Mail地址(可以不用设置)
ADMIN_EMAIL = root
SMTP_HOST = localhost
SMTP_PORT = 25
SMTP_FROM = DenyHosts <nobody@localhost>
SMTP_SUBJECT = DenyHosts Report from $[HOSTNAME]

# 有效用户登录失败计数归零的时间
AGE_RESET_VALID=5d

# ROOT用户登录失败计数归零的时间
AGE_RESET_ROOT=25d

# 用户的失败登录计数重置为0的时间(/usr/share/denyhosts/restricted-usernames)
AGE_RESET_RESTRICTED=25d

# 无效用户登录失败计数归零的时间
AGE_RESET_INVALID=10d

   ######### THESE SETTINGS ARE SPECIFIC TO DAEMON MODE  ##########
# denyhosts log文件
DAEMON_LOG = /var/log/denyhosts

DAEMON_SLEEP = 30s

# 该项与PURGE_DENY 设置成一样,也是清除hosts.deniedssh 用户的时间
DAEMON_PURGE = 1h

4)启动 denyhosts 服务并查看状态
[root@denyhosts ~]# service denyhosts status
denyhosts.py is stopped
[root@denyhosts ~]# service denyhosts start
Starting denyhosts:                                        [  OK  ]
[root@denyhosts ~]# service denyhosts status
denyhosts.py (pid  19784) is running...

5)通过测试 invalid、valid、root 等用户设置不同的ssh连接失败次数,来测试 denyhosts。

这里只测试使用系统中不存在的用户进行失败登录尝试~
这里允许 invalid 用户只能失败4次、ROOT 用户失败7次、valid用户失败10次
DENY_THRESHOLD_INVALID = 4 
DENY_THRESHOLD_VALID = 10 
DENY_THRESHOLD_ROOT = 7

测试:
在客户机(192.168.10.204)上使用一个没有创建的用户失败登录denyhosts机器(192.168.10.202)四次
[root@centos6-06 ~]# ssh -p22 haha@192.168.10.202
.......
多次尝试登陆失败后,就会发现客户机就不能使用ssh登陆denyhosts服务器了
[root@centos6-06 ~]# ssh -p22 haha@192.168.10.202   
ssh_exchange_identification: Connection closed by remote host

查看denyhosts服务器的/etc/hosts.deny
[root@denyhosts ~]# echo -n ""  > /var/log/secure
[root@denyhosts ~]# tail -f /var/log/secure
Feb 12 01:47:14 ceph-node3 sshd[19885]: refused connect from 192.168.10.204 (192.168.10.204)
[root@denyhosts ~]# tail -f /etc/hosts.deny
#
#   The rules in this file can also be set up in
#   /etc/hosts.allow with a 'deny' option instead.
#
#   See 'man 5 hosts_options' and 'man 5 hosts_access'
#   for information on rule syntax.
#   See 'man tcpd' for information on tcp_wrappers
#
# DenyHosts: Mon Feb 12 01:44:48 2018 | sshd: 192.168.10.204
sshd: 192.168.10.204

/var/log/secure 日志信息:
[root@denyhosts ~]# tail -f /var/log/secure
.......
Feb 12 01:48:15 ceph-node3 sshd[19892]: Invalid user haha from 192.168.10.204
Feb 12 01:48:15 ceph-node3 sshd[19893]: input_userauth_request: invalid user haha
Feb 12 01:48:17 ceph-node3 sshd[19892]: pam_unix(sshd:auth): check pass; user unknown
Feb 12 01:48:17 ceph-node3 sshd[19892]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.10.204 
Feb 12 01:48:17 ceph-node3 sshd[19892]: pam_succeed_if(sshd:auth): error retrieving information about user haha
Feb 12 01:48:18 ceph-node3 sshd[19892]: Failed password for invalid user haha from 192.168.10.204 port 60196 ssh2
Feb 12 01:48:19 ceph-node3 sshd[19892]: pam_unix(sshd:auth): check pass; user unknown
Feb 12 01:48:19 ceph-node3 sshd[19892]: pam_succeed_if(sshd:auth): error retrieving information about user haha
Feb 12 01:48:22 ceph-node3 sshd[19892]: Failed password for invalid user haha from 192.168.10.204 port 60196 ssh2
Feb 12 01:48:23 ceph-node3 sshd[19892]: pam_unix(sshd:auth): check pass; user unknown
Feb 12 01:48:23 ceph-node3 sshd[19892]: pam_succeed_if(sshd:auth): error retrieving information about user haha
Feb 12 01:48:25 ceph-node3 sshd[19892]: Failed password for invalid user haha from 192.168.10.204 port 60196 ssh2
Feb 12 01:48:25 ceph-node3 sshd[19893]: Connection closed by 192.168.10.204
Feb 12 01:48:25 ceph-node3 sshd[19892]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.10.204 
Feb 12 01:48:27 ceph-node3 sshd[19899]: refused connect from 192.168.10.204 (192.168.10.204)

关于DenyHosts中清除及添加可信主机记录

代码语言:javascript
复制
DenyHosts会读取多个记录确保没有漏网之鱼,如果想删除一个或多个已经禁止的主机IP,仅清空/var/log/secure和/etc/hosts.deny并不能完美解除已被锁定的IP, 
很快就会被DenyHosts重新锁定,这也是DenyHosts强大的地方!!需要进入 /var/lib/denyhosts 目录,进入以下操作:

针对源码方式安装DenyHosts后的解锁ip的措施:
1)停止DenyHosts服务:service denyhosts stop
2)在下列文件中找到想解锁的IP记录并删除(通常只修改前两个文件就OK了)。不推荐这种方法,因为用vim在下面的文件中找到解锁IP是非常费时费力的.
/var/log/secure              
/etc/hosts.deny               
/usr/share/denyhosts/data/hosts
/usr/share/denyhosts/data/hosts-restricted
/usr/share/denyhosts/data/hosts-root
/usr/share/denyhosts/data/hosts-valid

如果不在乎上面的记录文件, 推荐清空上面几个Linux系统日志然后重新开启DennyHosts. 清空这些Linux系统日志不会影响任何功能. 
如果你觉得这几个文件记录对你很重要(真的?), 请不要随意清空, 老老实实使用上面的方法.
清空上面几个Linux系统日志很简单, 在SSH中敲入下面的命令:
cat /dev/null > /var/log/secure
cat /dev/null > /etc/hosts.deny
cat /dev/null > /usr/share/denyhosts/data/hosts
cat /dev/null > /usr/share/denyhosts/data/hosts-restricted
cat /dev/null > /usr/share/denyhosts/data/hosts-root
cat /dev/null > /usr/share/denyhosts/data/hosts-valid
cat /dev/null > /usr/share/denyhosts/data/offset
cat /dev/null > /usr/share/denyhosts/data/suspicious-logins
cat /dev/null > /usr/share/denyhosts/data/users-hosts
cat /dev/null > /usr/share/denyhosts/data/users-invalid
cat /dev/null > /usr/share/denyhosts/data/users-valid

把日志文件全部清空!要注意的是,清空后一定要 
# service rsyslog restart 重启日志记数器!
因为如果你不重启服务,你会发现secure日志文件会停止记录一切活动了!那么denyhosts也就无效了

然后重新开启Deny Hosts
# service denyhosts start

最好重启ssh服务和iptables防火墙(如果打开的话)
#service sshd restart
#service iptables restart

针对yum方式安装DenyHosts后的解锁ip的措施:
1)停止DenyHosts服务:service denyhosts stop
2)在 /etc/hosts.deny 中删除你想取消的主机IP
3)编辑 DenyHosts 工作目录的所有文件 /var/lib/denyhosts,并且删除已被添加的主机信息。
/var/lib/denyhosts/hosts 
/var/lib/denyhosts/hosts-restricted 
/var/lib/denyhosts/hosts-root 
/var/lib/denyhosts/hosts-valid 
/var/lib/denyhosts/users-hosts 
/var/lib/denyhosts/users-invalid 
/var/lib/denyhosts/users-valid
4)添加你想允许的主机IP地址到 
/var/lib/denyhosts/allowed-hosts
5)启动DenyHosts服务: service denyhosts start

通过邮件接收 denyhosts 所发送的信息

代码语言:javascript
复制
1)安装sendmail邮件服务,请参考:http://www.cnblogs.com/kevingrace/p/6143977.html

2)denyhosts机器的主机名配置
[root@ceph-node1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.202 ceph-node1.localdomain ceph-node1

[root@ceph-node1 ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=ceph-node1

2)修改 /etc/denyhosts.conf文件中的mail配置,并重denyhosts 服务
[root@ceph-node1 ~]# cat /etc/denyhosts.conf
......
# 管理员Mail地址
ADMIN_EMAIL = wangshibo@kevin.com                     //若有ip被禁用发邮件通知
SMTP_HOST = ceph-node1                                //这个是mail机器的主机名。这里我是在denyhosts本机部署的mail服务
SMTP_PORT = 25
SMTP_FROM = DenyHosts <nobody@localhost>              //邮件的发送发
SMTP_SUBJECT = DenyHosts Report from $[HOSTNAME]      //邮件主题

[root@ceph-node1 ~]# /etc/init.d/denyhosts restart
Stopping denyhosts:                                        [  OK  ]
Starting denyhosts:                                        [  OK  ]

3)在客户机(192.168.10.210)上通过ssh连接denyhosts部署机(192.168.10.202),ssh连接失败次数到达denyhosts设置的失败次数后,
192.168.10.210的ip就会被denyhosts机器自动屏幕,这时候就会触发邮件发信动作。

注意:这里仅仅是在denyhosts机器刚发现客户机的ip被禁用的时候发送邮件,当已经禁用后,再次ssh连接失败后就不会重复发送邮件了,及邮件只会发送一次!

[root@zabbix-server ~]# ssh -p22 root@192.168.10.202
The authenticity of host '192.168.10.202 (192.168.10.202)' can't be established.
RSA key fingerprint is fd:82:c3:2d:f1:3e:0f:69:39:bf:7e:f7:82:59:a3:cb.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.10.202' (RSA) to the list of known hosts.
root@192.168.10.202's password: 
Permission denied, please try again.
root@192.168.10.202's password: 
Permission denied, please try again.
root@192.168.10.202's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

[root@zabbix-server ~]# ssh -p22 root@192.168.10.202
root@192.168.10.202's password: 
Permission denied, please try again.
root@192.168.10.202's password: 
Permission denied, please try again.
root@192.168.10.202's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

[root@zabbix-server ~]# ssh -p22 root@192.168.10.202
root@192.168.10.202's password: 
Permission denied, please try again.
root@192.168.10.202's password: 
Permission denied, please try again.
root@192.168.10.202's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

[root@zabbix-server ~]# ssh -p22 root@192.168.10.202
root@192.168.10.202's password: 
Permission denied, please try again.
root@192.168.10.202's password: 
Permission denied, please try again.
root@192.168.10.202's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

[root@zabbix-server ~]# ssh -p22 root@192.168.10.202
ssh_exchange_identification: Connection closed by remote host

此时,客户机192.168.10.210的ip已经被denyhosts机器那边给自动屏蔽掉了,也是在这个时候,通知邮件发出去了。
[root@ceph-node1 ~]# cat /etc/hosts.deny
.......
# DenyHosts: Mon Feb 12 04:34:10 2018 | sshd: 192.168.10.210
sshd: 192.168.10.210
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-02-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档