目录

Linux与等保那些事

目录

摘要

修改limits.conf文件

1
vi /etc/security/limits.conf

根据实际需求对文件中的各个变量参数进行合理设置

  • fsize 用户创建的文件大小限制;
  • core 生成的core文件大小的限制;
  • cpu 用户进程可用cpu的限定值;
  • data 进程数据段大小的限定值;
  • stack 进程堆栈段大小的限定值;
  • rss 进程常驻内存段的限定值;
  • nofiles 进程中打开文件的最大数量。

检查以下内容:

  • 检查是否存在空密码账户;
  • 身份标识(UID)具有唯一性;
  • 设置密码复杂度要求;
  • 定期更换密码;
  • 设置密码最短修改时间,防止非法用户短期更改多次;
  • 限制密码重用;
  • 确保root是唯一的UID为0的帐户

1、执行cat /etc/shadow | awk -F: '($2 == "" ) { print $1}', 查看空密码账户并处理;

2、查看/etc/passwd,检查是否有重复UID的用户并清理;

3、编辑/etc/security/pwquality.conf ,密码最小长度minlen设置为8-32之间,minclass设置3或4,如 minlen=10, minclass=3,Ubuntu为/etc/pam.d/common-password

直接vi编辑器 ,或者用sed命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# retry= N:定义登录/修改密码失败时,可以重试的次数;
# difok= N:定义新密码中必须有几个字符要与旧密码不同。但是如果新密码中有1/2以上的字符与旧密码不同时,该新密码将被接受;
# minlen = N:定义用户密码的最小长度;
# dcredit = N:定义用户密码中必须包含多少个数字;
# ucredit = N:定义用户密码中必须包含多少个大写字母;
# lcredit = N:定义用户密码中必须包含多少个小些字母;
# ocredit = N:定义用户密码中必须包含多少个特殊字符(除数字、字母之外);
# minclass = N:定义用户密码所需的最少字符类数
其中 =-1表示,至少有一个

sed -i 's/# minlen = 9/minlen = 8/' /etc/security/pwquality.conf
sed -i 's/# dcredit = 1/dcredit = -1/' /etc/security/pwquality.conf
sed -i 's/# ucredit = 1/ucredit = -1/' /etc/security/pwquality.conf
sed -i 's/# lcredit = 1/lcredit = -1/' /etc/security/pwquality.conf
sed -i 's/# ocredit = 1/ocredit = -1/' /etc/security/pwquality.conf

4、定期更换密码,在 /etc/login.defs 中将PASS_MAX_DAYS参数设置为 30-90之间,如 PASS_MAX_DAYS 90。需同时执行命令设置root密码失效时间:chage --maxdays 90 root

1
2
sed -i '/PASS_MAX_DAYS/s/99999/90/' /etc/login.defs
chage --maxdays 90 root

5、设置密码最短修改时间,在 /etc/login.defs 中将PASS_MIN_DAYS参数设置为 7-14之间,如 PASS_MIN_DAYS 7。需时执行命令为root用户设置:chage --mindays 7 root

1
2
sed -i '/PASS_MIN_DAYS/s/0/7/' /etc/login.defs
chage --mindays 7 root

6、在/etc/pam.d/password-auth/etc/pam.d/system-authpassword sufficient pam_unix.so 这行的末尾配置remember参数为5-24之间,建议设为5,即行末尾加remember=5

7、除root以外其他UID为0的用户,都应该删除或者修改其UID

检测是否配置登陆失败锁定策略,是否设置空闲会话断开时间和启用登陆时间过期后断开与客户端的连接设置

1、配置登陆失败锁定

备份要修改的文件

1
2
cp /etc/pam.d/password-auth /etc/pam.d/password-auth.bak
cp /etc/pam.d/system-auth /etc/pam.d/system-auth.bak

检查是否有pam_tally2.so模块

1
find /lib*/ -iname "pam_tally2.so"

编辑/etc/pam.d/password-auth/etc/pam.d/system-auth文件,在非注释行的第一行添加以下行(deny为连续失败次数,配置为3-8次,unlock_time为解锁时间,配置为600-1800秒)

1
auth required pam_tally2.so onerr=fail audit silent deny=5 unlock_time=900
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
vi /etc/pam.d/system-auth

#%PAM-1.0
auth        required      pam_faillock.so preauth audit silent deny=5 unlock_time=900
auth        [success=1 default=bad] pam_unix.so
auth        [default=die] pam_faillock.so authfail audit deny=5 unlock_time=900
auth        sufficient    pam_faillock.so authsucc audit deny=5 unlock_time=900
auth        required      pam_tally2.so deny=5 unlock_time=900 even_deny_root root_unlock_time=10

# deny 设置普通用户和root用户连续错误登陆的最大次数,超过最大次数,则锁定该用户
# unlock_time 设定普通用户锁定后,多少时间后解锁,单位是秒;
# root_unlock_time 设定root用户锁定后,多少时间后解锁,单位是秒;

2、设置系统登陆不活动连接超时退出

编辑/etc/profile,将TMOUT 设置为300到1800,即5-30分钟

1
TMOUT=900

3、在/etc/ssh/sshd_config中取消MaxAuthTries注释符号#,设置最大密码尝试失败次数3-6,建议为5:MaxAuthTries 5

1
sed -i 's/^#MaxAuthTries 5/MaxAuthTries 3/' /etc/ssh/sshd_config

检查sshd是否强制使用V2安全协议(Centos7无需配置);禁止Telnet等不安全的远程连接服务。

1、编辑 /etc/ssh/sshd_config 文件设置参数(Centos7无需配置):Protocol 2

2、执行命令service sshd restart重启sshd服务 3、执行以下命令停止Telnet服务:

1
2
systemctl stop telnet.socket
systemctl disable telnet.socket
  • Linux下root账号不应删除,检查是否禁止SSH直接登陆即可
  • root之外的系统默认帐户、数据库帐户禁止登陆(non-login)
  • 确保无弱密码存在,对应的弱密码基线检测通过

1、(注意:禁止root账户登陆前确保有其他账户可以正常使用)编辑配置文件/etc/ssh/sshd_config,将PermitRootLogin yes 改为PermitRootLogin no

1
sed -i 's/^#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config

2、执行systemctl restart sshd重启ssh

3、root之外的系统默认帐户、数据库帐户禁止登陆(non-login)

4、确保所有系统用户的密码都设置为复杂密码

  • 除系统管理用户之外,应该分配普通用户、审计员、安全员帐户;
  • 确保用户umask为027或更严格;
  • 确保每个用户的home目录权限设置为750或者更严格。

1、除系统管理用户之外,应该分配普通用户、审计员、安全员帐户;

2、检查/etc/bashrc/etc/profile文件中uamsk值是否设置为027或更严格,否则添加或编辑umask参数: umask 027 执行命令:source /etc/profile

3、确保每个用户的home目录权限设置为750或者更严格

  • root之外的系统默认帐户、数据库帐户禁止登陆(non-login)
  • 锁定或删除shutdown、halt帐户

1、root之外的系统默认帐户、数据库帐户禁止登陆(non-login)

2、锁定或删除shutdown、halt帐户:

1
2
usermod -L shutdown
usermod -L halt
  • 确保 su 命令的访问受限制
  • 检查/etc/sudoers配置 sudo 权限的用户,根据需要给root以外用户配置 sudo 权限,但除管理员外不能所有用户都配置(ALL)权限。

1、确保 su 命令的访问受限制,编辑 /etc/pam.d/su 文件添加:auth required pam_wheel.so use_uid/etc/group文件中创建一个逗号分隔的用户列表:wheel:x:10:root,<user list>

2、检查/etc/sudoers配置 sudo 权限的用户,不能所有用户都配置(ALL)权限

检查重要文件,如访问控制配置文件和用户权限配置文件的权限,是否达到用户级别的粒度

1、执行以下4条命令:

1
2
3
4
chown root:root /etc/hosts.allow
chown root:root /etc/hosts.deny
chmod 644 /etc/hosts.deny
chmod 644 /etc/hosts.allow

2、执行以下5条命令

1
2
3
4
5
chown root:root /etc/passwd /etc/shadow /etc/group /etc/gshadow 
chmod 0644 /etc/group 
chmod 0644 /etc/passwd 
chmod 0400 /etc/shadow 
chmod 0400 /etc/gshadow

3、设置/etc/ssh/sshd_config的权限:

1
2
chown root:root /etc/ssh/sshd_config 
chmod 600 /etc/ssh/sshd_config

4、配置/etc/profile文件权限:

1
2
chown root:root /etc/profile
chmod 644 /etc/profile
  • 确保用户home目录权限设置为750或者更严格
  • 无主文件或文件夹的所有权,根据需要重置为系统上的某个活动用户
  • 设置ssh主机公钥文件的权限和所有权
  • 设置ssh主机私钥文件的权限和所有权

1、确保用户home目录权限设置为750或者更严格

2、运行以下命令以设置ssh主机公钥文件的权限和所有权:

1
find /etc/ssh -xdev -type f -name 'ssh_host_*_key.pub' -exec chmod 0644 {} \; find /etc/ssh -xdev -type f -name 'ssh_host_*_key.pub' -exec chown root:root {} \;

3、运行以下命令以设置ssh主机私钥文件的权限和所有权:

1
find /etc/ssh -xdev -type f -name 'ssh_host_*_key' -exec chmod 0600 {} \; find /etc/ssh -xdev -type f -name 'ssh_host_*_key' -exec chown root:root {} \;
  • 启用 auditd 服务
  • 启用 rsyslog 或 syslog-ng 服务
  • 确保收集用户的文件删除事件
  • 确保收集对系统管理范围(sudoers)的更改
  • 确保收集修改用户/组信息的事件 如使用了第三方日志收集服务,可自行举证并忽略此项。

1、执行命令启用 auditd 服务:systemctl start auditd

2、执行命令systemctl start rsyslog启用 rsyslog 服务;

3、将以下行添加到/etc/audit/rules.d/audit.rules/etc/audit/audit.rules 文件中:

1
2
-a always,exit -F arch=b64 -S unlink -S unlinkat -S rename -S renameat -F auid>=1000 -F auid!=4294967295 -k delete
-a always,exit -F arch=b32 -S unlink -S unlinkat -S rename -S renameat -F auid>=1000 -F auid!=4294967295 -k delete

4、将以下行添加到/etc/audit/rules.d/audit.rules/etc/audit/audit.rules 文件中:

1
2
3
4
5
-w /etc/group -p wa -k identity 
-w /etc/passwd -p wa -k identity 
-w /etc/gshadow -p wa -k identity 
-w /etc/shadow -p wa -k identity 
-w /etc/security/opasswd -p wa -k identity

5、将以下行添加到/etc/audit/rules.d/audit.rules/etc/audit/audit.rules 文件中:

1
2
-w /etc/sudoers -p wa -k scope
-w /etc/sudoers.d/ -p wa -k scope

重启:auditd: systemctl restart auditd

满足另外一个检查项:启用安全审计功能,即满足此项

满足另外一个检查项:启用安全审计功能,即满足此项。

检查 auditd 文件大小、日志拆分配置或者备份至日志服务器

可通过日志文件分隔备份或者远程服务器日志备份来实现,以下两种方式之一修复加固:

1、编辑/etc/audit/auditd.conf,添加或编辑以下配置内容:

log_format定义了log日志的储存方式

1
log_format = RAW

log file的文件数量,设为5-10之间

1
num_logs = 5

max_log_file定义单个日志文件最大size,单位MB,设为5-50

1
2
3
max_log_file = 8
max_log_file_action = ROTATE
disk_full_action = SUSPEND

2、编辑/etc/rsyslog.conf文件添加以下行( logfile.example.com 是日志主机的名称):

1
*.* @@loghost.example.com

执行以下命令重启 rsyslog :

1
pkill -HUP rsyslogd

auditd 是审计进程audit的守护进程,syslogd 是日志进程 syslog 的守护进程,查看系统进程是否启动

1、执行命令启用 auditd 服务:systemctl start auditd

2、执行命令启用 rsyslog 服务 :systemctl start rsyslog

应卸载NetworkManager、avahi-daemon、bluetooth、firstboot、kdump、wdaemon、wpa_supplicant、ypbind等软件

卸载不必要的组件和程序,如NetworkManager、bluetooth、firstboot、wpa_supplicant、ypbind、kdump、avahi-daemon

1
2
3
4
5
6
7
yum -y remove avahi*
yum -y remove kexec-tools
yum -y remove *bluetooth*
yum -y remove NetworkManager*
yum -y remove firstboot
yum -y remove wpa_supplicant
yum -y remove ypbind

/etc/hosts.allow文件指定允许连接到主机的 IP 地址,不应配为ALL:ALL/etc/hosts.deny文件指定禁止连接到主机到 IP,应该配置为ALL:ALL默认禁止所有连接。 两者需要配合使用,且必须先配置/etc/hosts.allow规则。若是已通过其他方式实现如网络安全组、防火墙等,可自行举证并忽略此项。

配置hosts.deny前请确保hosts.allow已经正确配置,否则会导致无法远程访问主机

1、执行录入规则命令(<net>/<mask>,代表需要访问该服务器的网段,例如:“192.168.1.0/255.255.255.0”):

1
echo "ALL: <net>/<mask>, <net>/<mask>, ..." >/etc/hosts.allow

2、执行录入规则命令:

1
echo "ALL: ALL" >> /etc/hosts.deny
  • 应关闭不需要的系统服务、文件共享服务
  • 关闭21 、23、25、111、427、631等高危端口 如有特殊需求必须严格配置访问控制策略,需自行举证和忽略。

查看已经启动的或者是手动的系统服务,停止一些不必要的服务:

1、如使用命令关闭Telnet、RSH、SMB、Talk服务服务:

1
2
3
4
5
6
7
8
systemctl disable telnet.socket
systemctl disable rsh.socket
systemctl disable smb
systemctl disable ntalk
systemctl stop telnet.socket
systemctl stop rsh.socket
systemctl stop smb
systemctl stop ntalk

2、禁用NFSRPC服务

1
2
3
4
5
6
7
8
systemctl disable rpcbind
systemctl disable rpcbind.socket
systemctl disable nfs
systemctl disable nfs-server
systemctl stop rpcbind
systemctl stop rpcbind.socket
systemctl stop nfs
systemctl stop nfs-server

3、禁用IMAPPOP3服务

1
2
systemctl disable dovecot
systemctl stop dovecot

4、关闭21 、23、25、111、427、631端口

默认情况下,telnet ip 22端口会显示openssh的版本信息

修改ssh banner信息

1
2
3
4
5
whereis sshd
cp /usr/sbin/sshd /usr/sbin/sshd.bak
strings  /usr/sbin/sshd | grep OpenSSH
sed -i 's/OpenSSH_7.4/OpenSSH_9.8/g' /usr/sbin/sshd #修改版本
systemctl restart sshd

telnet 22端口测试banner信息,版本最好往大修改,有些漏扫根据服务版本判断漏洞,版本小就会说你有漏洞。

禁止无密码访问,防范恶意占用ssh连接资源

1
2
3
4
PermitEmptyPasswords no  # 禁止无密码访问服务器
LoginGraceTime 2m    #在输入密码界面指定时间内没有成功连接断开连接,若无单位则默认时间为秒
sed -i 's/^#PermitEmptyPasswords no/PermitEmptyPasswords no/' /etc/ssh/sshd_config
sed -i 's/^#LoginGraceTime 2m/LoginGraceTime 2m/' /etc/ssh/sshd_config

长时间无操作断开ssh连接

1
2
ClientAliveInterval 600  # 小于等于600s
sed -i 's/^#ClientAliveInterval 0/ClientAliveInterval 300/' /etc/ssh/sshd_config

修改完配置文件后,重新启动sshd服务器(systemctl restart sshd)即可。

1
useradd -s /bin/rbash readonly

修改用户密码

1
passwd readonly

创建用户shell执行命令目录

1
mkdir /home/readonly/.bin

root修改用户的shell配置文件

1
2
chown root. /home/readonly/.bash_profile 
chmod 755 /home/readonly/.bash_profile

修改bash配置文件,主要是指定PATH的读取

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
vi /home/readonly/.bash_profile 
# .bash_profile
 
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi
 
# User specific environment and startup programs
PATH=$HOME/bin
export PATH

将允许执行的命令链接到$HOME/bin目录

1
2
3
4
5
6
7
8
9
ln -s /usr/bin/wc  /home/readonly/.bin/wc
ln -s /usr/bin/tail  /home/readonly/.bin/tail
ln -s /bin/more  /home/readonly/.bin/more
ln -s /bin/cat  /home/readonly/.bin/cat
ln -s /bin/grep  /home/readonly/.bin/grep
ln -s /bin/find  /home/readonly/.bin/find
ln -s /bin/pwd  /home/readonly/.bin/pwd
ln -s /bin/ls  /home/readonly/.bin/ls
ln -s /bin/ll  /home/readonly/.bin/ll

相关内容