Centos下防止ssh暴力破解密码的方法

参考网址:

方法一,

收集 /var/log/secure 里面的信息,若是某个IP 链接次数超过一定次数 ,则把此ip记录到/etc/hosts.deny里面。

通过crontab来执行,每分钟执行一次。

*/1 * * * /bin/bash /root/Denyhosts/Denyhosts.sh

 

#!/bin/bash
#Denyhosts SHELL SCRIPT

cat /var/log/secure|awk ‘/Failed/{print $(NF-3)}’|sort|uniq -c|awk ‘{print $2″=” $1;}’ >/root/Denyhosts/Denyhosts.txt
DEFINE=”5″
for i in `cat /root/Denyhosts/Denyhosts.txt`
do
IP=`echo $i|awk -F= ‘{print $1}’`
NUM=`echo $i|awk -F= ‘{print $2}’`
if [ $NUM -gt $DEFINE ]
then
ipExists=`grep $IP /etc/hosts.deny |grep -v grep |wc -l`
if [ $ipExists -lt 1 ]
then
echo “sshd:$IP” >> /etc/hosts.deny
fi
fi
done

方法二,

DenyHosts官方网站为:http://denyhosts.sourceforge.net

1. 安装

# tar -zxvf DenyHosts-2.6.tar.gz
# cd DenyHosts-2.6
# python setup.py install
默认是安装到/usr/share/denyhosts目录的。

2. 配置

# cd /usr/share/denyhosts/
# cp denyhosts.cfg-dist denyhosts.cfg
# vi denyhosts.cfg
PURGE_DENY = 50m #过多久后清除已阻止IP
HOSTS_DENY = /etc/hosts.deny #将阻止IP写入到hosts.deny
BLOCK_SERVICE = sshd #阻止服务名
DENY_THRESHOLD_INVALID = 1 #允许无效用户登录失败的次数
DENY_THRESHOLD_VALID = 10 #允许普通用户登录失败的次数
DENY_THRESHOLD_ROOT = 5 #允许root登录失败的次数
WORK_DIR = /usr/local/share/denyhosts/data #将deny的host或ip纪录到Work_dir中
DENY_THRESHOLD_RESTRICTED = 1 #设定 deny host 写入到该资料夹
LOCK_FILE = /var/lock/subsys/denyhosts #将DenyHOts启动的pid纪录到LOCK_FILE中,已确保服务正确启动,防止同时启动多个服务。
HOSTNAME_LOOKUP=NO #是否做域名反解
ADMIN_EMAIL = #设置管理员邮件地址
DAEMON_LOG = /var/log/denyhosts #自己的日志文件
DAEMON_PURGE = 10m #该项与PURGE_DENY 设置成一样,也是清除hosts.deniedssh 用户的时间。

3. 设置启动脚本

# cp daemon-control-dist daemon-control
# chown root daemon-control
# chmod 700 daemon-control

完了之后执行daemon-contron start就可以了。

# ./daemon-control start

如果要使DenyHosts每次重起后自动启动还需做如下设置:

# ln -s /usr/share/denyhosts/daemon-control /etc/init.d/denyhosts
# chkconfig –add denyhosts
# chkconfig denyhosts on

然后就可以启动了:

# service denyhosts start
可以看看/etc/hosts.deny内是否有禁止的IP,有的话说明已经成功了。

提高ssh安全性的方法

1. 修改sshd服务器的配置文件/etc/ssh/sshd_config,将部分参数参照如下修改,增强安全性。

系统缺省使用22号端口,将监听端口更改为其他数值(最好是1024以上的高端口,以免和其他常规服务端口冲突),这样可以增加入侵者探测系统是否运行了 sshd守护进程的难度。
Port 5555
对于在服务器上安装了多个网卡或配置多个IP地址的情况,设定sshd只在其中一个指定的接口地址监听,这样可以减少sshd的入口,降低入侵的可能性。
ListenAddress 192.168.0.1
如果允许用户使用root用户登录,那么黑客们可以针对root用户尝试暴力破解密码,给系统安全带来风险。
PermitRootLogin no
允许使用空密码系统就像不设防的堡垒,任何安全措施都是一句空话。
PermitEmptyPasswords no
只允许指定的某些用户通过ssh访问服务器,将ssh使用权限限定在最小的范围内。
AllowUsers sshuser1 sshuser2
同上面的AllowUsers类似,限定指定的用户组通过ssh访问服务器,二者对于限定访问服务器有相同的效果。
AllowGroups sshgroup
禁止使用版本1协议,因为其存在设计缺陷,很容易使密码被黑掉。
Protocol 2
关闭X11Forwarding,防止会话被劫持。
X11Forwarding no
sshd服务运行时每一个连接都要使用一大块可观的内存,这也是ssh存在拒绝服务攻击的原因。一台服务器除非存在许多管理员同时管理服务器,否则上面这 个连接数设置是够用了。
MaxStartups 5
注意:以上参数设置仅仅是一个示例,用户具体使用时应根据各自的环境做相应的更改。

2. 修改sshd服务器的配置文件/etc/ssh/sshd_config的读写权限,对所有非root用户设置只读权限,防止非授权用户修改sshd 服务的安全设置。

chmod 644 /etc/ssh/sshd_config

3. 设置TCP Wrappers。服务器默认接受所有的请求连接,这是非常危险的。使用TCP Wrappers可以阻止或允许应用服务仅对某些主机开放,给系统在增加一道安全屏障。这部分设置共涉计到两个文件:hosts.allow和 hosts.deny。
将那些明确允许的请求添加到/etc/hosts.allow中。如系统仅允许IP地址为192.168.0.15和10.0.0.11的主机使用 sshd服务,则添加如下内容:

sshd:192.168.0.15 10.0.0.11

将需要禁止使用的信息添加到/etc/hosts.deny中。如对除了在hosts.allow列表中明确允许使用sshd的用户外,所有其他用户都禁止使用sshd服务,则添加如下内容到hosts.deny文件中:
sshd:All
注意:系统对上述两个文件的判断顺序是先检查hosts.allow文件再查看hosts.deny文件,因此一个用户在hosts.allow允许使用网络资源,而同时在hosts.deny中禁止使用该网络资源,在这种情况下系统优先选择使用hosts.allow配置,允许用户使用该网络资源。

 

注:

更改port值后需要如下命令设置

semanage port -a -t ssh_port_t -p tcp 1998

firewall-cmd –zone=public –add-port=1998/tcp –permanent

firewall-cmd –reload

 

参考文章:https://blog.51cto.com/lorysun/1663567

Linux系统安装好后,默认会开启SSH服务以便远程配置。但使用默认端口22不安全,一般不建议使用默认端口,那就需要修改SSH默认端口。在RHEL7.0上修改和7.0以下类似,但要注意SELinux的修改。

SSH 为 Secure Shell,由IETF的网络工作小组(Network Working Group)所制定;SSH 是建立在应用层和传输层基础上的一种安全协议。SSH传输数据是加密的,可以有效防止传输过程被截取数据保障安全。SSH的数据是经过压缩的,所以可以加快传输的速度。

修改步骤:

1、查看是否已安装SSH软件包,#rpm -qa|grep ssh

2、检查服务是否开启,# systemctl status sshd.service

3、检查进程运行状态,# ps -ef |grep sshd

4、检查程序运行端口,# netstat -anpl |grep sshd

5、修改sshd配置文件,# vi /etc/ssh/sshd_config

取消“#Port 22”前面的#号,另起一行新增Port 6022(自定义端口),wq保存退出。

6、重启SSH服务,# systemctl restart sshd.service

7、开放防火墙端口:

添加端口,# firewall-cmd –zone=public –add-port=6022/tcp –permanent

重新加载,# firewall-cmd –reload

重启服务,# systemctl restart firewalld.service

查看端口,# firewall-cmd –zone=public –list-all

8、修改SELinux端口:

检查SELinux是否启用,# sestatus -v |grep SELinux

SELinux status:  enabled    #表示启用

检查semanage是否安装,# rpm -qa |grep policycoreutils-python

若未安装,请先安装工具包# yum install policycoreutils-python

查看当前selinux允许的端口,# semanage port -l |grep ssh

添加新端口,# semanage port -a -t ssh_port_t -p tcp 6022

检查是否添加成功,# semanage port -l |grep ssh

重启SSH服务,# systemctl restart sshd.service

注意:

SSH端口默认是22,如果要修改端口,可先增加一个端口,方法如上,使用新端口登录后再把22端口注释掉(注意同时修改防火墙# firewall-cmd –zone=public –remove-port=22/tcp –permanent,再重新加载# firewall-cmd –reload)。如果是增加端口号,需要先取消22端口的#号,再另起一行新增一个port端口。

若想删除已停用的端口可使用-d删除命令,# semanage port -d -t ssh_port_t -p tcp 6022

若想添加其他已定义的端口(如443)到SSH,可使用-m修改命令,# semanage port -m -t ssh_port_t -p tcp 443

直接使用-a命令添加443端口会报错:ValueError: Port tcp/443 already defined

一般停用默认端口后我们会想到从selinux中删除22端口,但在执行# semanage port -d -t ssh_port_t -p tcp 22命令后会报以下错误:ValueError: Port tcp/22 is defined in policy, cannot be deleted而无法删除。暂时无法实现。

使用badblocks命令检测、修复坏道

参考网址:

https://linux.cn/blog-6515-1012.html

badblocks是Linux下常用的坏道修复工具。当你觉得硬盘上可能有坏道,或者是SMART数据显示有坏道的时候,都可以用badblocks来检查一下。

假设我们要检查的硬盘是/dev/sdb

$sudo badblocks -s -v -o sdbbadblocks.log /dev/sdb

这样就可以对硬盘进行只读扫描,自动获取硬盘块数目并扫描全部块,将扫描日志输出到屏幕同时记录在sdbbadblocks.log文件中。

由于扫描速度比较低,一次不一定能扫完,可以分多次进行。

sudo badblocks -s -v -o sdbbadblocks.log /dev/sdb END START

将END和START换成结束和开始的块的编号就可以了。

如果找到了坏道,可以进行写入扫描进行修复。写入扫描遇到坏道的时候会自动重映射。写入扫描会覆盖原有数据,所以请先备份。写入扫描速度很低,所以应该只扫描只读扫描时候发现错误的部分。

$sudo badblocks -w -s  /dev/sdb END START

想了解各选项的意思及更多用法,请man badblocks。

Vim &xxd 编辑查看二进制文件

1.使用二进制方式打开文件

vim -b filename

:%!xxd

00000000: 4740 1110 0042 f025 0001 c100 00ff 01ff  G@...B.%........
00000010: 0001 fc80 1448 1201 0646 466d 7065 6709  .....H...FFmpeg.
00000020: 5365 7276 6963 6530 3177 7c43 caff ffff  Service01w|C....
00000030: ffff ffff ffff ffff ffff ffff ffff ffff  ................
00000040: ffff ffff ffff ffff ffff ffff ffff ffff  ................
00000050: ffff ffff ffff ffff ffff ffff ffff ffff  ................

 

dd 命令分割文件

dd if=base.apk of=./testmidi.wav bs=1 skip=232820652 count=398

bs=BYTES read and write up to BYTES bytes at a time (default: 512);
overrides ibs and obs
cbs=BYTES convert BYTES bytes at a time
conv=CONVS convert the file as per the comma separated symbol list
count=N copy only N input blocks
ibs=BYTES read up to BYTES bytes at a time (default: 512)
if=FILE read from FILE instead of stdin
iflag=FLAGS read as per the comma separated symbol list
obs=BYTES write BYTES bytes at a time (default: 512)
of=FILE write to FILE instead of stdout
oflag=FLAGS write as per the comma separated symbol list
seek=N skip N obs-sized blocks at start of output
skip=N skip N ibs-sized blocks at start of input
status=LEVEL The LEVEL of information to print to stderr;
‘none’ suppresses everything but error messages,
‘noxfer’ suppresses the final transfer statistics,
‘progress’ shows periodic transfer statistics

stream 对象创建时机

 
对于所有的文件格式,stream对象都是在probe函数探测到文件格式后调用相应的 AVInputFormat 的int (*read_header)(struct AVFormatContext *); 函数。

537 int avformat_open_input(AVFormatContext **ps, const char *filename,
 538                         AVInputFormat *fmt, AVDictionary **options)
 629 
 630     if (!(s->flags&AVFMT_FLAG_PRIV_OPT) && s->iformat->read_header)
 631         if ((ret = s->iformat->read_header(s)) < 0)
 632             goto fail;
For MKV
#0  avformat_new_stream (s=0x7fffe00008c0, c=0x0) at libavformat/utils.c:4456
#1  0x00000000007a1e89 in matroska_parse_tracks (s=0x7fffe00008c0) at libavformat/matroskadec.c:2199
#2  0x00000000007a4077 in matroska_read_header (s=0x7fffe00008c0) at libavformat/matroskadec.c:2657
#3  0x00000000008a1939 in avformat_open_input (ps=0x7fffe83b4d00, filename=0x423a060 "test.mkv", fmt=0x0, options=0x25e1410 <format_opts>) at libavformat/utils.c:631
For AVI 
#0  avformat_new_stream (s=0x7fffe00008c0, c=0x0) at libavformat/utils.c:4456
#1  0x000000000072970d in avi_read_header (s=0x7fffe00008c0) at libavformat/avidec.c:571
#2  0x00000000008a1939 in avformat_open_input (ps=0x7fffe83b4d00, filename=0x425a460 "/home/xw/Documents/videotest/mpeg1.avi", fmt=0x0, options=0x25e1410 <format_opts>) at libavformat/utils.c:631
For ts 
#0  avformat_new_stream (s=0x7fffe00008c0, c=0x0) at libavformat/utils.c:4456
#1  0x00000000007fd3b1 in pmt_cb (filter=0x7fffe0001040, section=0x7fffe0009500 "\002\260\027", section_len=26) at libavformat/mpegts.c:2075
#2  0x00000000007f8531 in write_section_data (ts=0x7fffe00174c0, tss1=0x7fffe0001040, buf=0x7fffe000f60d "\002\260\027", buf_size=183, is_start=1) at libavformat/mpegts.c:441
#3  0x00000000007fe2f1 in handle_packet (ts=0x7fffe00174c0, packet=0x7fffe000f608 "GO\377\020") at libavformat/mpegts.c:2394
#4  0x00000000007feb6e in handle_packets (ts=0x7fffe00174c0, nb_packets=26595) at libavformat/mpegts.c:2583
#5  0x00000000007ff1c3 in mpegts_read_header (s=0x7fffe00008c0) at libavformat/mpegts.c:2704
#6  0x00000000008a1939 in avformat_open_input (ps=0x7fffe83b4d00, filename=0x425d0f0 "/home/xw/Documents/videotest/tt.ts", fmt=0x0, options=0x25e1410 <format_opts>) at libavformat/utils.c:631
For mp3
#0  avformat_new_stream (s=0x7fffe00008c0, c=0x0) at libavformat/utils.c:4456
#1  0x00000000007ecf28 in mp3_read_header (s=0x7fffe00008c0) at libavformat/mp3dec.c:355
#2  0x00000000008a1939 in avformat_open_input (ps=0x7fffe83b4d00, filename=0x425d0f0 "/home/xw/Documents/videotest/tesss.mp3", fmt=0x0, options=0x25e1410 <format_opts>) at libavformat/utils.c:631