Linux安全基线Centos7-爱代码爱编程
安全基线检查
扫描用户和时间:
user_id=whoami
echo “当前扫描用户:${user_id}”
scanner_time=date '+%Y-%m-%d %H:%M:%S'
echo “当前扫描时间:${scanner_time}”
echo “***************************”
echo “账号策略检查中…”
echo “***************************”
#项目1:帐号与口令-用户口令设置
#合格:Y;不合格:N
#不合格地方
passmax=cat /etc/login.defs | grep PASS_MAX_DAYS | grep -v ^# | awk '{print $2}'
passmin=cat /etc/login.defs | grep PASS_MIN_DAYS | grep -v ^# | awk '{print $2}'
passlen=cat /etc/login.defs | grep PASS_MIN_LEN | grep -v ^# | awk '{print $2}'
passage=cat /etc/login.defs | grep PASS_WARN_AGE | grep -v ^# | awk '{print $2}'
if [ $passmax -le 90 -a
p
a
s
s
m
a
x
−
g
t
0
]
;
t
h
e
n
e
c
h
o
"
Y
:
口
令
生
存
周
期
为
passmax -gt 0 ];then echo "Y:口令生存周期为
passmax−gt0];thenecho"Y:口令生存周期为{passmax}天,符合要求"
else
echo “N:口令生存周期为${passmax}天,不符合要求,建议设置不大于90天”
fi
if [
p
a
s
s
m
i
n
−
g
e
6
]
;
t
h
e
n
e
c
h
o
"
Y
:
口
令
更
改
最
小
时
间
间
隔
为
passmin -ge 6 ];then echo "Y:口令更改最小时间间隔为
passmin−ge6];thenecho"Y:口令更改最小时间间隔为{passmin}天,符合要求"
else
echo “N:口令更改最小时间间隔为${passmin}天,不符合要求,建议设置大于等于6天”
fi
if [
p
a
s
s
l
e
n
−
g
e
8
]
;
t
h
e
n
e
c
h
o
"
Y
:
口
令
最
小
长
度
为
passlen -ge 8 ];then echo "Y:口令最小长度为
passlen−ge8];thenecho"Y:口令最小长度为{passlen},符合要求"
else
echo “N:口令最小长度为${passlen},不符合要求,建议设置最小长度大于等于8”
fi
if [ $passage -ge 30 -a $passage -lt
p
a
s
s
m
a
x
]
;
t
h
e
n
e
c
h
o
"
Y
:
口
令
过
期
警
告
时
间
天
数
为
passmax ];then echo "Y:口令过期警告时间天数为
passmax];thenecho"Y:口令过期警告时间天数为{passage},符合要求"
else
echo "N:口令过期警告时间天数为KaTeX parse error: Expected 'EOF', got '#' at position 41: …0并小于口令生存周期" fi #̲项目2:帐号与口令-root用…(cat /etc/ssh/sshd_config | grep -v ^# |grep “PermitRootLogin no”)
if [ $? -eq 0 ];then
echo “Y:已经设置远程root不能登陆,符合要求”
else
echo “N:已经设置远程root能登陆,不符合要求,建议/etc/ssh/sshd_config添加PermitRootLogin no”
fi
#编号:SBL-Linux-02-01-03
#项目:帐号与口令-检查是否存在除root之外UID为0的用户
#合格:Y;不合格:N
#不合格地方
#查找非root账号UID为0的账号
echo “SBL-Linux-02-01-03:”
UIDS=awk -F[:] 'NR!=1{print $3}' /etc/passwd
flag=0
for i in $UIDS
do
if [ $i = 0 ];then
echo “N:存在非root账号的账号UID为0,不符合要求”
else
flag=1
fi
done
if [ $flag = 1 ];then
echo “Y:不存在非root账号的账号UID为0,符合要求”
fi
more /etc/securetty
#检查是否有下列行:
#pts/x(x为一个十进制整数)
#more /etc/ssh/sshd_config
#检查下列行设置是否为no并且未被注释:
#PermitRootLogin
#不存在pts/x则禁止了telnet登录,PermitRootLogin no禁止了ssh登录,符合以上条件>则禁止了root远程登录,符合安全要求,否则低于安全要求。
##!/bin/bash
SSHSTATUS=ps -ef|grep -i sshd|grep -v grep|wc -l
if [ x"
S
S
H
S
T
A
T
U
S
"
!
=
"
x
0
"
]
;
t
h
e
n
i
f
[
[
‘
g
r
e
p
"
P
e
r
m
i
t
R
o
o
t
L
o
g
i
n
n
o
"
/
e
t
c
/
s
s
h
/
s
s
h
d
c
o
n
f
i
g
∣
w
c
−
l
‘
!
=
0
]
]
;
t
h
e
n
g
r
e
p
"
P
e
r
m
i
t
R
o
o
t
L
o
g
i
n
n
o
"
/
e
t
c
/
s
s
h
/
s
s
h
d
c
o
n
f
i
g
;
e
c
h
o
"
T
h
i
s
d
e
v
i
c
e
d
o
e
s
n
o
t
p
e
r
m
i
t
r
o
o
t
t
o
s
s
h
l
o
g
i
n
,
c
h
e
c
k
r
e
s
u
l
t
:
t
r
u
e
"
;
e
l
s
e
e
c
h
o
"
T
h
i
s
d
e
v
i
c
e
p
e
r
m
i
t
s
r
o
o
t
t
o
s
s
h
l
o
g
i
n
,
c
h
e
c
k
r
e
s
u
l
t
:
f
a
l
s
e
"
;
f
i
e
l
s
e
e
c
h
o
"
T
h
e
s
s
h
s
e
r
v
i
c
e
o
f
d
e
v
i
c
e
i
s
n
o
t
r
u
n
n
i
n
g
,
c
h
e
c
k
r
e
s
u
l
t
:
t
r
u
e
"
;
f
i
T
E
L
S
T
A
T
U
S
=
‘
n
e
t
s
t
a
t
−
a
n
t
∣
g
r
e
p
"
:
23
"
∣
w
c
−
l
‘
i
f
[
x
"
SSHSTATUS" != "x0" ]; then if [[ `grep "^PermitRootLogin no" /etc/ssh/sshd_config|wc -l` != 0 ]]; then grep "^PermitRootLogin no" /etc/ssh/sshd_config; echo "This device does not permit root to ssh login,check result:true"; else echo "This device permits root to ssh login,check result:false" ; fi else echo "The ssh service of device is not running,check result:true"; fi TELSTATUS=`netstat -ant|grep ":23\>"|wc -l` if [ x"
SSHSTATUS"!="x0"];thenif[[‘grep"PermitRootLoginno"/etc/ssh/sshdconfig∣wc−l‘!=0]];thengrep"PermitRootLoginno"/etc/ssh/sshdconfig;echo"Thisdevicedoesnotpermitroottosshlogin,checkresult:true";elseecho"Thisdevicepermitsroottosshlogin,checkresult:false";fielseecho"Thesshserviceofdeviceisnotrunning,checkresult:true";fiTELSTATUS=‘netstat−ant∣grep":23"∣wc−l‘if[x"TELSTATUS" != “x0” ];
then if ([ -f /etc/securetty ] && [ grep -i "^pts" /etc/securetty|wc -l
= 0 ]);
then echo “This device does not permit root to telnet login,check result:true”;
else echo “This device permits root to telnet login,check result:false”;
fi
else echo “The telnet service of device is not running,check result:true” ;
fi
#项目3:帐号与口令-root用户环境变量的安全性
#合格:Y;不合格:N
#不合格地方
#检查目录权限是否为777
echo “SBL-Linux-02-01-05:”
dirPri=$(find $(echo KaTeX parse error: Double superscript at position 15: PATH | tr ':' '̲ ') -type d \( …dirPri" ]
then
echo “Y:目录权限无777的,符合要求”
else
echo “N:文件KaTeX parse error: Expected 'EOF', got '#' at position 31: …7的,不符合要求。" fi #̲项目4:帐号与口令-远程连接的…(find / -xdev -mount -name .netrc -print)
if [ -z “
f
i
l
e
N
e
t
r
c
"
]
;
t
h
e
n
e
c
h
o
"
Y
:
不
存
在
.
n
e
t
r
c
文
件
,
符
合
要
求
"
e
l
s
e
e
c
h
o
"
N
:
存
在
.
n
e
t
r
c
文
件
,
不
符
合
要
求
"
f
i
f
i
l
e
R
h
o
s
t
s
=
‘
f
i
n
d
/
−
x
d
e
v
−
m
o
u
n
t
−
n
a
m
e
.
r
h
o
s
t
s
−
p
r
i
n
t
‘
i
f
[
−
z
"
{fileNetrc}" ];then echo "Y:不存在.netrc文件,符合要求" else echo "N:存在.netrc文件,不符合要求" fi fileRhosts=`find / -xdev -mount -name .rhosts -print` if [ -z "
fileNetrc"];thenecho"Y:不存在.netrc文件,符合要求"elseecho"N:存在.netrc文件,不符合要求"fifileRhosts=‘find/−xdev−mount−name.rhosts−print‘if[−z"fileRhosts” ];then
echo “Y:不存在.rhosts文件,符合要求”
else
echo “N:存在.rhosts文件,不符合要求”
fi
#项目5:帐号与口令-用户的umask安全配置
#合格:Y;不合格:N
#不合格地方
#检查umask设置
echo “SBL-Linux-02-01-07:”
umask1=cat /etc/profile | grep umask | grep -v ^# | awk '{print $2}'
umask2=cat /etc/csh.cshrc | grep umask | grep -v ^# | awk '{print $2}'
umask3=cat /etc/bashrc | grep umask | grep -v ^# | awk 'NR!=1{print $2}'
flags=0
for i in $umask1
do
if [
i
!
=
"
027
"
]
;
t
h
e
n
e
c
h
o
"
N
:
/
e
t
c
/
p
r
o
f
i
l
e
文
件
中
所
所
设
置
的
u
m
a
s
k
为
i != "027" ];then echo "N:/etc/profile文件中所所设置的umask为
i!="027"];thenecho"N:/etc/profile文件中所所设置的umask为{i},不符合要求,建议设置为027”
flags=1
break
fi
done
if [
f
l
a
g
s
=
=
0
]
;
t
h
e
n
e
c
h
o
"
Y
:
/
e
t
c
/
p
r
o
f
i
l
e
文
件
中
所
设
置
的
u
m
a
s
k
为
flags == 0 ];then echo "Y:/etc/profile文件中所设置的umask为
flags==0];thenecho"Y:/etc/profile文件中所设置的umask为{i},符合要求"
fi
flags=0
for i in $umask2
do
if [
i
!
=
"
027
"
]
;
t
h
e
n
e
c
h
o
"
N
:
/
e
t
c
/
c
s
h
.
c
s
h
r
c
文
件
中
所
所
设
置
的
u
m
a
s
k
为
i != "027" ];then echo "N:/etc/csh.cshrc文件中所所设置的umask为
i!="027"];thenecho"N:/etc/csh.cshrc文件中所所设置的umask为{i},不符合要求,建议设置为027"
flags=1
break
fi
done
if [
f
l
a
g
s
=
=
0
]
;
t
h
e
n
e
c
h
o
"
Y
:
/
e
t
c
/
c
s
h
.
c
s
h
r
c
文
件
中
所
设
置
的
u
m
a
s
k
为
flags == 0 ];then echo "Y:/etc/csh.cshrc文件中所设置的umask为
flags==0];thenecho"Y:/etc/csh.cshrc文件中所设置的umask为{i},符合要求"
fi
flags=0
for i in $umask3
do
if [
i
!
=
"
027
"
]
;
t
h
e
n
e
c
h
o
"
N
:
/
e
t
c
/
b
a
s
h
r
c
文
件
中
所
设
置
的
u
m
a
s
k
为
i != "027" ];then echo "N:/etc/bashrc文件中所设置的umask为
i!="027"];thenecho"N:/etc/bashrc文件中所设置的umask为{i},不符合要求,建议设置为027"
flags=1
break
fi
done
if [
f
l
a
g
s
=
=
0
]
;
t
h
e
n
e
c
h
o
"
Y
:
/
e
t
c
/
b
a
s
h
r
c
文
件
中
所
设
置
的
u
m
a
s
k
为
flags == 0 ];then echo "Y:/etc/bashrc文件中所设置的umask为
flags==0];thenecho"Y:/etc/bashrc文件中所设置的umask为{i},符合要求"
fi
#项目6:文件系统-重要目录和文件的权限设置
#合格:Y;不合格:N
#不合格地方
echo “SBL-Linux-02-02-01:” >> “/tmp/${ipadd}_checkResult.txt”
echo “***************************”
echo “检查重要文件权限中…”
echo “***********************"
file1=ls -l /etc/passwd | awk '{print $1}'
file2=ls -l /etc/shadow | awk '{print $1}'
file3=ls -l /etc/group | awk '{print $1}'
file4=ls -l /etc/securetty | awk '{print $1}'
file5=ls -l /etc/services | awk '{print $1}'
#检测文件权限为400的文件
if [ $file2 = “-r--------” ];then
echo “Y:/etc/shadow文件权限为400,符合要求”
else
echo “N:/etc/shadow文件权限不为400,不符合要求,建议设置权限为400”
fi
#检测文件权限为600的文件
if [ $file4 = “-rw-------” ];then
echo “Y:/etc/security文件权限为600,符合要求”
else
echo “N:/etc/security文件权限不为600,不符合要求,建议设置权限为600”
fi
#检测文件权限为644的文件
if [ $file1 = “-rw-r–r--” ];then
echo “Y:/etc/passwd文件权限为644,符合要求”
else
echo “N:/etc/passwd文件权限不为644,不符合要求,建议设置权限为644”
fi
if [ $file5 = “-rw-r–r--” ];then
echo “Y:/etc/services文件权限为644,符合要求”
else
echo “N:/etc/services文件权限不为644,不符合要求,建议设置权限为644”
fi
if [ KaTeX parse error: Can't use function '\(' in math mode at position 146: …edfile=`find / \̲(̲ -perm -04000 -…{unauthorizedfile}设置了SUID/SGID,请检查是否授权"
#项目7:文件系统-检查任何人都有写权限的目录
#合格:Y;不合格:N;检查:C
#不合格地方
echo “SBL-Linux-02-02-03:”
checkWriteDre=KaTeX parse error: Can't use function '\(' in math mode at position 30: …-mount -type d \̲(̲ -perm -0002 -a…{checkWriteDre}" ];then
echo “Y:不存在任何人都有写权限的目录,符合要求”
else
echo “N:KaTeX parse error: Expected 'EOF', got '#' at position 38: …以写,不符合要求" fi #̲项目8:文件系统-检查任何人都…(find / -xdev -mount -type f ( -perm -0002 -a ! -perm -1000 ))
if [ -z “
c
h
e
c
k
W
r
i
t
e
F
i
l
e
"
]
;
t
h
e
n
e
c
h
o
"
Y
:
不
存
在
任
何
人
都
有
写
权
限
的
目
录
,
符
合
要
求
"
e
l
s
e
e
c
h
o
"
N
:
{checkWriteFile}" ];then echo "Y:不存在任何人都有写权限的目录,符合要求" else echo "N:
checkWriteFile"];thenecho"Y:不存在任何人都有写权限的目录,符合要求"elseecho"N:{checkWriteFile}目录任何人都可以写,不符合要求”
fi
#项目9:文件系统-检查异常隐含文件
#合格:Y;不合格:N;检查:C
#不合格地方
echo “SBL-Linux-02-02-05:”
hideFile=KaTeX parse error: Can't use function '\(' in math mode at position 22: …/ -xdev -mount \̲(̲ -name "..*" -o…{hideFile}” ];then
echo “Y:不存在隐藏文件,符合要求”
else
echo “N:KaTeX parse error: Expected 'EOF', got '#' at position 29: …文件,建议审视" fi #̲项目10:日志审计-syslo…{recodeFile}” ];then
logFile=KaTeX parse error: Expected group after '^' at position 33: …conf | grep -V ^̲# | grep authpr…{logFile}" ];then
echo “Y:存在保存authpirv的日志文件”
else
echo “N:不存在保存authpirv的日志文件”
fi
else
echo “N:不存在/etc/syslog.conf文件,建议对所有登录事件都记录”
fi
#项目11:系统文件-检查日志审核功能是否开启
#合格:Y;不合格:N;检查:C
echo “SBL-Linux-03-01-02:”
auditdStatus=$(service auditd status )
if [ $? = 0 ];then
echo “Y:系统日志审核功能已开启,符合要求”
fi
if [ KaTeX parse error: Expected 'EOF', got '#' at position 71: …td start开启" fi #̲项目12:系统文件-系统cor…(cat /etc/security/limits.conf | grep -V ^# | grep core)
if [ $? -eq 0 ];then
soft=(cat /etc/security/limits.conf | grep -V ^# | grep core | awk {print $2})
for i in
s
o
f
t
d
o
i
f
[
"
soft do if [ "
softdoif["i"x = "soft"x ];then
echo "Y: soft core 0 已经设置"
fi
if [ "$i"x = "hard"x ]
then
echo "Y: hard core 0 已经设置"
fi
done
else
echo "N:没有设置core,建议在/etc/security/limits.conf中添加 soft core 0和 hard core 0”
fi
#项目13:系统文件-检查磁盘动态空间,是否大于等于80%
#合格:Y;不合格:N;检查:C
echo “SBL-Linux-04-01-02:”
space=$(df -h | awk -F “[ %]+” ‘NR!=1{print $5}’)
for i in $space
do
if [ $i -ge 80 ];then
echo “C:警告!磁盘存储容量大于80%,建议扩充磁盘容量或者删除垃圾文件”
fi
done
执行脚本过程:
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/weixin_51167520/article/details/111060514