zl程序教程

您现在的位置是:首页 >  系统

当前栏目

Linux服务器企业级安全加固

2023-06-13 09:13:13 时间

Linux服务器企业级安全加固

前言

Linux 是一个开放式系统,可以在网络上找到许多现成的程序和工具,这既方便了用户,也方便了黑.客,因为他们也能很容易地找到程序和工具来潜入 Linux 系统,或者盗取 Linux 系统上的重要信息。不过,只要我们仔细地设定 Linux 的各种系统功能,并且加上必要的安全措施,就能让黑.客们无机可乘。一般来说,对 Linux 系统的安全设定包括取消不必要的服务、限制远程存取、隐藏重要资料、修补安全漏.洞、采用安全工具以及经常性的安全检查等。 说明:以下安全级别表示建议的可操作的级别,星星越多,可操作性越高,建议级别越高

账-号、密-码安全

1 锁定不必要的用户

安全级别:★★★★★ 使用passwd -l锁定不必要的账号,这里是把除了root以外所有的账号都锁定

#!/bin/bash
for temp in cut -d ":" -f 1 /etc/passwd | grep -v "root"
do
passwd -l $temp
done

2 修改密.码过期时间

安全级别:★★★★

[root@localhost ~]# vim /etc/login.defs
PASS_MAX_DAYS 90 # 新建用户密.码最长使用天数
PASS_MIN_DAYS 0 # 新建用户密.码最短使用天数
PASS_MIN_LEN 7 # 新建用户密.码到期提示天数
PASS_WARN_AGE 10 # 最小密.码长度

3 设置密.码复杂度

安全级别:★★★★★ 复杂程度至少一个大写字母一个小写字母一个数字一个特殊符号,且长度至少为10位

[root@localhost ~]# vim /etc/pam.d/system-auth
password required pam_cracklib.so try_first_pass retry=3 dcredit=-1 lcredit=-1 ucredit=-1 ocredit=-1 minlen=10

4 限制登陆超时

安全级别:★★★★ 超时时间为5分钟,5分钟没有动作自动注销登陆

[root@localhost ~]# vim /etc/profile

TMOUT=300
export TMOUT

5 限制错误登陆次数

安全级别:★★★★★ 限制错误登陆次数防止暴力破.解

[root@localhost ~]# vim /etc/pam.d/login

#%PAM-1.0
auth required pam_tally2.so deny=3 lock_time=300 even_deny_root root_unlock_time=10
查询远程登录次数
pam_tally2 --user lyshark

6 禁止root用户远程登陆

安全级别:★★★★★

6.1 创建普通用户,并且授予sudo权限

[root@localhost ~]# useradd lyshark
[root@localhost ~]# passwd lyshark

[root@localhost ~]# vim /etc/sudoers

##The COMMANDS section may have other options added to it.
##Allow root to run any commands anywhere
root ALL=(ALL) ALL
lyshark ALL=(ALL) ALL
#--------------------------------------------------------------------------------------------

6.2 禁止ROOT远程登录系统

[root@localhost ~]# vim /etc/ssh/sshd_config
PermitRootLogin no

[root@localhost ~]# systemctl restart sshd

修改默认ssh连接端口

安全级别:★★★★★

[root@localhost ~]# vim /etc/ssh/sshd_config

Port 65534 # 登录端口改为65534
MaxAuthTries=3 # 密.码最大尝试次数3

[root@localhost ~]# systemctl restart sshd
[C:\Users]$ ssh  root@192.168.1.30 6553

限制允许ssh远程连接的Ip

安全级别:★★★★

vi /etc/hosts.allow
sshd:192.168.220.1 #允许单个IP

sshd:192.168.220. #允许地址段
vi /etc/hosts.deny
sshd:ALL #除了上面允许的其他都拒绝

使用密钥登陆

安全级别:★★★★★ 使用xshell等工具自带的公钥向导生成公钥,然后将公钥内容复制到服务器的/root/.ssh/ authorized_keys

锁定系统文件

将二进制文件进行锁定可以防止被修改 安全级别:★★★

[root@localhost sbin]# chattr +i /sbin/
[root@localhost sbin]# chattr +i /usr/sbin/
[root@localhost sbin]# chattr +i /bin/
[root@localhost sbin]# chattr +i /sbin/
[root@localhost sbin]# chattr +i /usr/lib
[root@localhost sbin]# chattr +i /usr/lib64
[root@localhost sbin]# chattr +i /usr/libexec

修改默认Umask值

安全级别:★★★ 系统默认的umask值为0022,将umask值改为0777的时候,用户默认创建的文件不具有可执行权限,防止被上传木.马

[root@localhost ~]# echo “umask 0777” >> /etc/bashrc
[root@localhost ~]# touch test1
[root@localhost ~]# mkdir test2
[root@localhost ~]#
[root@localhost ~]# ls -lh
total 0
----------. 1 root root 0 Aug 25 05:46 test1
d---------. 2 root root 6 Aug 25 05:46 test2

限制GCC编译

安全级别:★★★ 限制编译可以防止黑.客编译生成可执行文件用来提权

[root@localhost ~]# rpm -q --filesbypkg gcc | grep “bin”

[root@localhost ~]# chmod 000 /usr/bin/c89
[root@localhost ~]# chmod 000 /usr/bin/c99
[root@localhost ~]# chmod 000 /usr/bin/cc
[root@localhost ~]# chmod 000 /usr/bin/gcc
[root@localhost ~]# chmod 000 /usr/bin/gcc-*
[root@localhost ~]# chmod 000 /usr/bin/gcc-*

限制日志文件

安全级别:★★★★ 黑.客入侵后大都会对日志文件进行清空或删除,所以要将日志文件进行不能删除只能增加限制

[root@localhost ~]# cd /var/log/
[root@localhost log]# chattr +a dmesg cron lastlog messages secure wtmp

[root@localhost log]# lsattr secure

最小化防火墙规则

安全级别:★★★★★ 最小化防火墙规则只开放http和https等对外提供服务的端口和ssh远程连接服务

vim /etc/firewalld/zones/public.xml
<service name=“ssh”/>
<port protocol=“tcp” port=“80”/>
<port protocol=“tcp” port=“443”/>

firewall-cmd –reload

开启selinux

安全级别:★★★

1 配置selinux允许ssh服务

默认selinux也是开放的,但是经常会有一些未知的问题,如果没有出现未知问题可以继续开放,下面是selinux允许ssh服务

[root@localhost ~]# yum install -y policycoreutils-python-2.5-29.el7.x86_64

[root@localhost ~]# semanage port -l | grep ssh
ssh_port_t tcp 22

[root@localhost ~]# semanage port -a -t ssh_port_t -p tcp 6553

[root@localhost ~]# semanage port -l | grep ssh
ssh_port_t tcp 6553, 22

2 通过semanage命令设置目录权限

[root@localhost html]# semanage fcontext -a -t httpd_sys_content_t /var/www/html/index.html

[root@localhost html]# ls -Z
-rw-r–r–. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html

设置history命令显示时间

安全级别:★★★★

vi /etc/profile
在文件的末尾添加参数
export HISTTIMEFORMAT="%F %T whoami "

禁止Control-Alt-Delete 键盘重启系统命令

安全级别:★★★

1.备份配置文件

cp -a /usr/lib/systemd/system/ctrl-alt-del.target /usr/lib/systemd/system/ctrl-alt-del.target.default

2.移除该原源文件

rm -rf /usr/lib/systemd/system/ctrl-alt-del.target

设置grub密.码

安全级别:★★★

1.备份配置文件

cp -a /etc/grub.d/00_header /etc/grub.d/00_header.default

2.使用grub2-mkpasswd-pbkdf2 加密密.码

密.码自己定义,但是一定得保存好否则最后连自己都登录不进去

[root@localhost ~]# grub2-mkpasswd-pbkdf2
Enter password:
Reenter password:
PBKDF2 hash of your password is grub.pbkdf2.sha512.10000.B157F42E96462AB239C03000F113D32EB18FD48073F1FC7D8F87A8F3B3F89F662424ECCAB901F3A812A997E547FD520F3E99D0E080F4FE8B05E019757E34F75B.29C83F87B4B6C086FC9A81E046CC3623CC5CF2F82128EDC3A0364894E429D4993B28563F82D71BF346188108CBD4341FC4A71B90E543581646B4E7EAE920C54A

3.修/boot/grub2/grub.cfg

添加以下配置到该文件的最后面(特别需要注意 用户名root和密文之间是空格分隔,而不是换行)

cat <<EOF
set superusers=‘root’
password_pbkdf2 root grub.pbkdf2.sha512.10000.B157F42E96462AB239C03000F113D32EB18FD48073F1FC7D8F87A8F3B3F89F662424ECCAB901F3A812A997E547FD520F3E99D0E080F4FE8B05E019757E34F75B.29C83F87B4B6C086FC9A81E046CC3623CC5CF2F82128EDC3A0364894E429D4993B28563F82D71BF346188108CBD4341FC4A71B90E543581646B4E7EAE920C54A
E0F

4.执行命令grub2-mkconfig -o /boot/grub2/grub.cfg

重新编译生成grub.cfg文件

及时更新漏.洞补丁及使用稳定安全版的服务器应用软件

安全级别:★★★★★

有条件使用堡垒机登陆服务器

安全级别:★★★★

将生产服务器和办公网物理隔离

安全级别:★★★★★