Centos远程主机加固
目前互联网环境比较复杂,随着僵尸网络的不断扩大,ssh暴力破解相当严重,每个人的ssh密码都有可能存在被破解的可能,笔者刚刚安装好一台Linux远程主机,不一会就发现有人开始暴力破解密码,于是将自己的加固过程记录下来,已被以后查询。
0x01 添加非root用户并授权
添加用户
1
[root@VM_cenots ~]# adduser cnwill
给用户配置密码
1
2
3
4[root@VM_centos ~]# passwd cnwill
New password:
Retype new password:
passwd: all authentication tokens updated successfully.对创建的新用户进行sudo操作的授权(/etc/sudoers )
1
2[root@VM_centos ~]# whereis sudoers
sudoers: /etc/sudoers /etc/sudoers.d /usr/share/man/man5/sudoers.5.gz查看文件权限
1
2[root@VM_centos ~]# ls -l /etc/sudoers
-r--r----- 1 root root 3927 Jul 15 09:04 /etc/sudoers加入可写的权限
1 | [root@VM_centos ~]# chmod -v u+w /etc/sudoers |
将cnwill用户添加至sudoers文件中
使用vi编辑/etc/sudoers 文件,找到其中root ALL=(ALL) ALL这一行,之后在这行下面加入cnwill ALL=(ALL) ALL即可,:wq推出编辑1
2
3
4[root@VM_centos ~]# vi /etc/sudoers
## Allow root to run any commands anywher
root ALL=(ALL) ALL
cnwill ALL=(ALL) ALL将sudoers文件的写权限收回
1
[root@VM_centos ~]# chmod -v u-w /etc/sudoers
使用cnwill进行登陆,并测测试sudo 权限:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18[root@VM_centos ~]# su cnwill
[cnwill@VM_centos ~]$ sudo ifconfig
[sudo] password for cnwill:
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.24.35.55 netmask 255.255.240.0 broadcast 172.24.47.255
ether 00:16:3e:00:c8:36 txqueuelen 1000 (Ethernet)
RX packets 23509 bytes 25781988 (24.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6821 bytes 1134380 (1.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
0x02 修改ssh端口
默认在root用户下ssh服务已经安装完成,未安装的可以使用yum install openssh-server安装。
进入ssh配置文件的目录(一般情况再
/etc/ssh),并编辑配置文件(sshd_config),使用vi编辑配置文件,找到如下位置,添加10011和 22(默认);1
2
3
4
5
6[root@VM_centos ~] vi /etc/ssh/sshd_config
#Port 22
#ListenAddress 0.0.0.0
#ListenAddress ::
port 10011
Port 22向防火墙(本地主机)或者安全组(云主机)添加端口规则
** 本地主机,或者是非阿里云这样的运营商的主机,向防火墙添加端口1
2[root@VM_centos ~] # firewall-cmd --zone=public --add-port=10011/tcp --permanent
[root@VM_centos ~] # firewall-cmd --reload** 如果你使用的是云主机,需要在云安全策略中添加1011号端口入栈任意地址。
向SELinux中添加修改的SSH端口
安装semanage1
[root@VM_centos ~] # yum provides semanage
查询ssh端口
1
2[root@VM_centos ~] # semanage port -l | grep ssh
ssh_port_t tcp 22向 SELinux 中添加 ssh 端口
1
[root@VM_centos ~] # semanage port -a -t ssh_port_t -p tcp 10011
查询ssh端口
1
2[root@VM_centos ~] # semanage port -l | grep ssh
ssh_port_t tcp 10011, 22重启ssh服务
1
[root@VM_centos ~] # systemctl restart sshd.service
0x03 加固ssh
使用xshell生成rsa密钥对
打开xshell ,选择‘工具-》新建用户密码生成向导-》下一步-》下一步-》设置一个密钥用于加密私钥-》下一步-》文件另存为-》选择路径-》确定’。tips:这里保存的是公钥,私钥默认已经加入到你的xshell里面了。*
配置ssh使用证书登录
将刚才生成的公钥文件上传到服务器/home/cnwill(这是我之前新建的用户)目录下,(我使用的方式是xftp,如果你安装的是x系列全家桶,可以使用同样的方式,),并新建.ssh文件夹,之后将上传的公钥文件rsa2048.pub移动到.ssh文件夹下,并重命名为authorized_keys.pub。之后配置sshd_config 文件。tips:这里面有个”.”*
1
2
3
4
5
6
7
8[root@VM_centos ~] #mv rsa2048.pub .ssh/authorized_keys.pub
[root@VM_centos ~] #vi /etc/ssh/sshd_config
#允许密钥认证
RSAAuthentication yes
PubkeyAuthentication yes
StrictModes no
#公钥保存文件
AuthorizedKeysFile .ssh/authorized_keys禁用密码登录
1
PasswordAuthentication no
禁止root用户远程登录
1
PermitRootLogin no
完成之后
:wq保存推出并重启ssh服务1
[root@VM_centos ~] # systemctl restart sshd.service