1.jpg

    1. #!/bin/bash
    2. # ================ Global function for print result ===================
    3. dash_line="------------------------------------------------------------------"
    4. # 绿色字体输出检测通过
    5. pass=$(($pass+1))
    6. print_pass(){
    7. echo -e "\033[32m++> PASS \033[0m"
    8. echo "++> PASS" >> "$file"
    9. }
    10. # 红色字体输出检测失败FAIL
    11. fail=$(($fail+1))
    12. print_fail(){
    13. echo -e "\033[31m--> FAIL \033[0m"
    14. echo "--> FAIL" >> "$file"
    15. }
    16. # 黄色字体输出需手工再检查的项
    17. print_manual_check(){
    18. echo -e "\033[33m##> Manual \033[0m"
    19. echo "##> Manual" >> "$file"
    20. }
    21. # 蓝色字体输出补充
    22. print_info(){
    23. echo -e "\033[34m$1 \033[0m"
    24. }
    25. # 紫色字体输出检测项
    26. print_check_point(){
    27. echo ""
    28. echo -e "\033[35m[No."$1"] "$2" \033[0m"
    29. echo "[$1]"" $2" >> "$file"
    30. }
    31. print_dot_line(){
    32. echo "$dash_line"
    33. }
    34. print_summary(){
    35. # 输出显示
    36. print_info "---------------------------- Summary -----------------------------"
    37. echo -e "\033[35m全部检测项: $1 \033[0m"
    38. echo -e "\033[32m通过检测项: $2 \033[0m"
    39. echo -e "\033[31m失败检测项: $3 \033[0m"
    40. echo -e "\033[33m手工检测项: $4 \033[0m"
    41. print_info "检测结果将写入文件 $file中..."
    42. print_info "$dash_line"
    43. # 写入文件
    44. echo "---------------------------- Summary -----------------------------" >> "$file"
    45. echo "全部检测项: $1" >> "$file"
    46. echo "通过检测项: $2" >> "$file"
    47. echo "失败检测项: $3" >> "$file"
    48. echo "手工检测项: $4" >> "$file"
    49. echo "$dash_line" >> "$file"
    50. }
    51. # ====================================
    52. begin_msg="-------------------- 正在执行操作系统基线检查 --------------------"
    53. print_info "$begin_msg"
    54. index=0 # 检测项编号
    55. pass=0 # 通过的检测项数
    56. fail=0 # 未通过的检测项数
    57. manual=0 # 需手工复核的检测项数
    58. file="os_check_result.txt"
    59. echo "$begin_msg" > "$file"
    60. check_point="帐号管理-1:检查是否设置除root之外UID为0的用户"
    61. index=$(($index+1))
    62. print_check_point $index "$check_point"
    63. print_info "'任何UID为0的帐户都具有系统上的超级用户特权,只有root账号的uid才能为0'"
    64. print_dot_line
    65. result=`/bin/cat /etc/passwd | /bin/awk -F: '($3 == 0) { print $1 }'`
    66. print_info "UID为0的用户如下:"
    67. print_info "[ $result ]"
    68. if [ "root" = $result ]; then
    69. pass=$(($pass+1))
    70. print_pass
    71. else
    72. fail=$(($fail+1))
    73. print_fail
    74. fi
    75. check_point="帐号管理-2:检查是否按用户分配账号 "
    76. index=$(($index+1))
    77. print_check_point $index "$check_point"
    78. print_info "'应按照不同的用户分配不同的账号,避免不同用户间共享账号,避免用户账号和设备间通信使用的账号共享'"
    79. print_dot_line
    80. up_uidmin=`(grep -v ^# /etc/login.defs |grep "^UID_MIN"|awk '($1="UID_MIN"){print $2}')`
    81. up_uidmax=`(grep -v ^# /etc/login.defs |grep "^UID_MAX"|awk '($1="UID_MAX"){print $2}')`
    82. users=`/bin/cat /etc/passwd | /bin/awk -F: '{if( $3>='$up_uidmin' && $3<='$up_uidmax' ) {print $1":"$3}}'`
    83. print_info "系统中存在的用户如下:"
    84. print_info "[ $users ]"
    85. if [ "$users" ]; then
    86. pass=$(($pass+1))
    87. print_pass
    88. else
    89. fail=$(($fail+1))
    90. print_fail
    91. fi
    92. check_point="帐号管理-3:检查是否删除与设备运行、维护等工作无关的账号"
    93. index=$(($index+1))
    94. print_check_point $index "$check_point"
    95. print_info "'应删除或锁定与设备运行、维护等工作无关的账号'"
    96. print_dot_line
    97. print_info "系统中存在的账号如下:"
    98. account=`/bin/cat /etc/shadow | /usr/bin/sed '/^\s*#/d' | /bin/awk -F: '($2!~/^*/) && ($2!~/^!!/) {print $1}'`
    99. print_info "[ $account ]"
    100. manual=$(($manual+1))
    101. print_manual_check
    102. check_point="帐号管理-4:检查是否设置不同的用户组"
    103. index=$(($index+1))
    104. print_check_point $index "$check_point"
    105. print_info "'根据系统要求及用户的业务需求,建立多账户组,将用户账号分配到相应的账户组'"
    106. print_dot_line
    107. groups=`cat /etc/group | awk -F ':' '$3>1000{print $1}'`
    108. print_info "系统中存在的用户自定义用户组 gid >= 1000,如下"
    109. print_info "[ $groups ]"
    110. if [ -n "$groups" ];then
    111. pass=$(($pass+1))
    112. print_pass
    113. else
    114. fail=$(($fail+1))
    115. print_fail
    116. fi
    117. check_point="口令策略-1:检查是否设置口令生存周期 "
    118. index=$(($index+1))
    119. print_check_point $index "$check_point"
    120. passmax=`cat /etc/login.defs | grep PASS_MAX_DAYS | grep -v ^#`
    121. print_info "'PASS_MAX_DAYS 应介于1~90'"
    122. print_dot_line
    123. print_info "$passmax"
    124. if [ -n "$passmax" ]; then
    125. days=`echo $passmax | awk '{print $2}'`
    126. if [ "$days" -gt 90 ]; then
    127. fail=$(($fail+1))
    128. print_fail
    129. else
    130. pass=$(($pass+1))
    131. print_pass
    132. fi
    133. else
    134. fail=$(($fail+1))
    135. print_fail
    136. fi
    137. check_point="口令策略-2:检查是否设置口令更改最小间隔天数 "
    138. index=$(($index+1))
    139. print_check_point $index "$check_point"
    140. passmin=`cat /etc/login.defs | grep PASS_MIN_DAYS | grep -v ^#`
    141. print_info "'PASS_MIN_DAYS 应大于等于 7'"
    142. print_dot_line
    143. print_info "$passmin"
    144. if [ -n "$passmin" ]; then
    145. days=`echo $passmin | awk '{print $2}'`
    146. if [ "$days" -lt 7 ]; then
    147. fail=$(($fail+1))
    148. print_fail
    149. else
    150. pass=$(($pass+1))
    151. print_pass
    152. fi
    153. else
    154. fail=$(($fail+1))
    155. print_fail
    156. fi
    157. check_point="口令策略-3:检查是否设置口令过期前警告天数 "
    158. index=$(($index+1))
    159. print_check_point $index "$check_point"
    160. print_info "'口令过期前告警天数PASS_WARN_AGE应设置为 7'"
    161. print_dot_line
    162. pass_age=`cat /etc/login.defs | grep PASS_WARN_AGE | grep -v ^# `
    163. print_info "$pass_age"
    164. if [ -n "$pass_age" ]; then
    165. days=`echo $pass_age | awk '{print $2}'`
    166. if [ "$days" -eq 7 ]; then
    167. pass=$(($pass+1))
    168. print_pass
    169. else
    170. fail=$(($fail+1))
    171. print_fail
    172. fi
    173. else
    174. fail=$(($fail+1))
    175. print_fail
    176. fi
    177. check_point="口令策略-4:检查设备密码复杂度策略"
    178. index=$(($index+1))
    179. print_check_point $index "$check_point"
    180. print_info "'系统应设置密码复杂度策略,避免设置账号弱口令'"
    181. print_dot_line
    182. print_info "此部分要求可能不一致,请手工检查/etc/pam.d/system-auth或/etc/security/pwquality.conf文件配置"
    183. # 以下内容是以密码长度至少为8位,并且存在大写字母、小写字母、数字、特殊字符至少一个要求来检测的
    184. # ------------------------------------------------------------------------------
    185. # print_info "'密码长度>=8,且至少包含一个大写字母、小写字母、数字、特殊字符(可自定义密码复杂度策略)'"
    186. # print_info "可在/etc/pam.d/system-auth或/etc/security/pwquality.conf进行配置"
    187. # print_dot_line
    188. # print_info "检查/etc/pam.d/system-auth,如下:"
    189. #
    190. # flag=0
    191. #
    192. # # 以下检查/etc/pam.d/system-auth文件中的内容
    193. # #
    194. # info=`cat /etc/pam.d/system-auth | grep password | grep requisite`
    195. # print_info "$info"
    196. # line=`cat /etc/pam.d/system-auth | grep password | grep pam_cracklib.so | grep -v ^#`
    197. # if [ -n "$line" ]; then
    198. # check_min=`echo $line | grep minlen`
    199. # check_dcredit=`echo $line | grep dcredit`
    200. # check_ucredit=`echo $line | grep ucredit`
    201. # check_ocredit=`echo $line | grep ocredit`
    202. # check_lcredit=`echo $line | grep lcredit`
    203. # # minlen:密码字符串长度,dcredit数字字符个数,ucredit大写字符个数,ocredit特殊字符个数,lcredit小写字符个数
    204. # if [ -n "$check_min" ] && [ -n "$check_dcredit" ] && [ -n "$check_ucredit" ] && [ -n "$check_ocredit" ] && [ -n "$check_lcredit" ]; then
    205. # minlen=`echo $line | awk -F 'minlen=' '{print $2}' | awk -F ' ' '{print $1}'`
    206. # dcredit=`echo $line | awk -F 'dcredit=' '{print $2}' | awk -F ' ' '{print $1}'`
    207. # ucredit=`echo $line | awk -F 'ucredit=' '{print $2}' | awk -F ' ' '{print $1}'`
    208. # ocredit=`echo $line | awk -F 'ocredit=' '{print $2}' | awk -F ' ' '{print $1}'`
    209. # lcredit=`echo $line | awk -F 'lcredit=' '{print $2}' | awk -F ' ' '{print $1}'`
    210. #
    211. # if [ "$minlen" -ge 8 ] && [ ${dcredit#-} -ge 1 ] && [ ${ucredit#-} -ge 1 ] && [ ${ucredit#-} -ge 1 ] && \
    212. # [ ${ocredit#-} -ge 1 ] && [ ${lcredit#-} -ge 1 ]; then
    213. # print_info "minlen => ""[ $minlen ]"
    214. # print_info "dcredit => ""[ $dcredit ]"
    215. # print_info "ucredit => ""[ $ucredit ]"
    216. # print_info "ocredit => ""[ $ocredit ]"
    217. # print_info "lcredit => ""[ $lcredit ]"
    218. # flag=1
    219. # fi
    220. # fi
    221. # fi
    222. #
    223. # # 以下检查/etc/security/pwquality.conf文件中的内容
    224. # # minlen为密码字符串长度,minclass为字符类别
    225. # print_info "检查/etc/security/pwquality.conf,如下:"
    226. # line_minlen=`cat /etc/security/pwquality.conf | grep minlen | grep -v ^#`
    227. # line_minclass=`cat /etc/security/pwquality.conf | grep minclass | grep -v ^#`
    228. #
    229. # if [ -n "$line_minlen" ] && [ -n "$line_minclass" ]; then
    230. # minlen=`echo "$line_minlen" | awk -F "=" '{print $2}' | awk '{gsub(/^\s+|\s+$/, "");print}'`
    231. # minclass=`echo "$line_minclass" | awk -F "=" '{print $2}' | awk '{gsub(/^\s+|\s+$/, "");print}'`
    232. # if [ "$minlen" -ge 8 ] && [ "$minclass" -ge 4 ];then
    233. # print_info "minlen =>"" [ $minlen ]"
    234. # print_info "minclass =>"" [ $minclass ]"
    235. # flag=1
    236. # fi
    237. # fi
    238. #
    239. # if [ "$flag" -eq 1 ]; then
    240. # pass=$(($pass+1))
    241. # print_pass
    242. # else
    243. # fail=$(($fail+1))
    244. # print_fail
    245. # fi
    246. # ------------------------------------------------------------------------------
    247. manual=$(($manual+1))
    248. print_manual_check
    249. check_point="口令策略-5:检查是否存在空口令账号"
    250. index=$(($index+1))
    251. print_check_point $index "$check_point"
    252. print_info "'不允许存在空口令的账号'"
    253. print_dot_line
    254. tmp=`/bin/cat /etc/shadow | /bin/awk -F: '($2 == "" ) { print "user " $1 " does not have a password "}'`
    255. print_info '空口令账号:'"[ $tmp ]"
    256. if [ -z "$tmp" ]; then
    257. pass=$(($pass+1))
    258. print_pass
    259. else
    260. fail=$(($fail+1))
    261. print_fail
    262. fi
    263. check_point="口令策略-6:检查密码重复使用次数限制"
    264. index=$(($index+1))
    265. print_check_point $index "$check_point"
    266. line=`cat /etc/pam.d/system-auth | grep password | grep sufficient | grep pam_unix.so | grep remember | grep -v ^#`
    267. print_info "'口令重复使用限制次数 remember >=5'"
    268. print_dot_line
    269. print_info "[ $line ]"
    270. if [ -n "$line" ]; then
    271. times=`echo $line|awk -F "remember=" '{print $2}'`
    272. if [ $times -ge 5 ]; then
    273. pass=$(($pass+1))
    274. print_pass
    275. else
    276. fail=$(($fail+1))
    277. print_fail
    278. fi
    279. else
    280. fail=$(($fail+1))
    281. print_fail
    282. fi
    283. check_point="口令策略-7:检查账户认证失败次数限制"
    284. index=$(($index+1))
    285. print_check_point $index "$check_point"
    286. print_dot_line
    287. print_info "登录失败限制可以使用pam_tally或pam.d,请手工检测/etc/pam.d/system-auth"
    288. manual=$(($manual+1))
    289. print_manual_check
    290. check_point="认证授权-1:检查用户目录缺省访问权限设置 "
    291. index=$(($index+1))
    292. print_check_point $index "$check_point"
    293. tmp=`umask`
    294. print_info "'文件目录缺省访问权限应是 027'"
    295. print_dot_line
    296. print_info "实际检测值为:"
    297. print_info "[ $tmp ]"
    298. tt=`echo $tmp | grep 027`
    299. if [ -n "$tt" ];then
    300. pass=$(($pass+1))
    301. print_pass
    302. else
    303. fail=$(($fail+1))
    304. print_fail
    305. fi
    306. check_point="认证授权-2:检查是否设置SSH登录前警告Banner"
    307. index=$(($index+1))
    308. print_check_point $index "$check_point"
    309. banner1=`cat /etc/ssh/sshd_config | grep Banner`
    310. print_info "'检查SSH配置文件:/etc/ssh/sshd_config,未启用banner或合理设置banner的内容'"
    311. print_dot_line
    312. print_info "$banner1"
    313. # 如果banner为空或者为 None,则符合要求
    314. if [ -z "$banner1" ]; then
    315. print_info "不存在Banner配置项"
    316. pass=$(($pass+1))
    317. print_pass
    318. else
    319. banner2=`cat /etc/ssh/sshd_config | grep Banner | awk '{print $2}' | grep -v "none"`
    320. if [ -z "$banner2" ]; then
    321. print_info "未配置Banner路径文件"
    322. pass=$(($pass+1))
    323. print_pass
    324. else
    325. manual=$(($manual+1))
    326. path=`cat /etc/ssh/sshd_config | grep Banner | awk '{print $2}'`
    327. print_info "请手工检查文件 $path 是否符合要求"
    328. print_manual_check
    329. fi
    330. fi
    331. check_point="日志审计-1:检查是否对登录进行日志记录"
    332. index=$(($index+1))
    333. print_check_point $index "$check_point"
    334. print_info "'设备应配置日志功能,对用户登录进行记录,记录内容包括用户登录使用的账号,登录是否成功,登录时间,以及远程登录时,用户使用的IP地址'"
    335. tmp=`cat /etc/rsyslog.conf | grep /var/log/secure | egrep 'authpriv'.\('info|\*'\) | grep -v ^#`
    336. print_dot_line
    337. print_info "/etc/rsyslog.conf 文件中 authpriv 的配置如下所示:"
    338. print_info "$tmp"
    339. if [ -n "$tmp" ]; then
    340. pass=$(($pass+1))
    341. print_pass
    342. else
    343. fail=$(($fail+1))
    344. print_fail
    345. fi
    346. check_point="日志审计-2:检查是否启用cron行为日志功能"
    347. index=$(($index+1))
    348. print_check_point $index "$check_point"
    349. print_dot_line
    350. tmp=`cat /etc/rsyslog.conf | grep /var/log/cron | egrep 'cron.\*' | grep -v ^#`
    351. print_info "/etc/rsyslog.conf 文件中 cron 的配置如下所示:"
    352. print_info "$tmp"
    353. if [ -n "$tmp" ]; then
    354. pass=$(($pass+1))
    355. print_pass
    356. else
    357. fail=$(($fail+1))
    358. print_fail
    359. fi
    360. check_point="日志审计-3:检查是否配置远程日志功能"
    361. index=$(($index+1))
    362. print_check_point $index "$check_point"
    363. print_info "'在远程主机上存储日志数据保护日志完整性免受本地攻击'"
    364. print_dot_line
    365. msg="请检查/etc/rsyslog.conf文件,查看是否配置日志服务器"
    366. print_info "$msg"
    367. manual=$(($manual+1))
    368. print_manual_check
    369. check_point="日志审计-4:检查是否配置su命令使用情况记录"
    370. index=$(($index+1))
    371. print_check_point $index "$check_point"
    372. print_info "'启用syslog系统日志审计功能'"
    373. print_dot_line
    374. tmp=`cat /etc/rsyslog.conf | grep /var/log/secure | egrep 'authpriv'.\('info|\*'\) | grep -v ^#`
    375. print_info "/etc/rsyslog.conf 文件中 authpriv 的配置如下所示:"
    376. print_info "$tmp"
    377. if [ -n "$tmp" ]; then
    378. pass=$(($pass+1))
    379. print_pass
    380. else
    381. fail=$(($fail+1))
    382. print_fail
    383. fi
    384. check_point="日志审计-5:检查日志文件权限设置"
    385. index=$(($index+1))
    386. print_check_point $index "$check_point"
    387. messages=`stat -c %a /var/log/messages`
    388. dmesg=`stat -c %a /var/log/dmesg`
    389. maillog=`stat -c %a /var/log/maillog`
    390. secure=`stat -c %a /var/log/secure`
    391. wtmp=`stat -c %a /var/log/wtmp`
    392. cron=`stat -c %a /var/log/cron`
    393. print_info "'设备应配置权限,控制对日志文件读取、修改和删除等操作'"
    394. print_info "推荐的文件权限:(不大于左侧值)"
    395. print_info "600 /var/log/messages"
    396. print_info "600 /var/log/secure、"
    397. print_info "600 /var/log/maillog、"
    398. print_info "600 /var/log/cron"
    399. print_info "644 /var/log/dmesg"
    400. print_info "664 /var/log/wtmp"
    401. print_dot_line
    402. print_info "目前的文件权限如下:"
    403. print_info $messages' /var/log/messages'
    404. print_info $dmesg' /var/log/dmesg '
    405. print_info $maillog' /var/log/maillog '
    406. print_info $secure' /var/log/secure '
    407. print_info $wtmp' /var/log/wtmp '
    408. print_info $cron' /var/log/cron '
    409. if [ "$messages" -le 600 ] && [ "$secure" -le 600 ] && [ "$maillog" -le 600 ] && [ "$cron" -le 600 ] && [ "$dmesg" -le 644 ] && [ "$wtmp" -le 664 ]; then
    410. pass=$(($pass+1))
    411. print_pass
    412. else
    413. fail=$(($fail+1))
    414. print_fail
    415. fi
    416. check_point="日志审计-6:检查安全事件日志配置"
    417. index=$(($index+1))
    418. print_check_point $index "$check_point"
    419. print_info "'设备应配置日志功能,记录对与设备相关的安全事件'"
    420. print_dot_line
    421. tmp=`cat /etc/rsyslog.conf | grep /var/log/messages | egrep '\*.info;mail.none;authpriv.none;cron.none' | grep -v ^#`
    422. print_info "/etc/rsyslog.conf 文件中 /var/log/messages 的配置如下所示:"
    423. print_info "$tmp"
    424. if [ -n "$tmp" ]; then
    425. pass=$(($pass+1))
    426. print_pass
    427. else
    428. fail=$(($fail+1))
    429. print_fail
    430. fi
    431. check_point="文件权限-1:检查FTP用户上传的文件所具有的权限"
    432. index=$(($index+1))
    433. print_check_point $index "$check_point"
    434. print_info "'FTP服务未运行,或ftp用户和匿名用户上传文件的权限为022'"
    435. print_dot_line
    436. tmp=`netstat -lntp | grep ftp`
    437. print_info "$tmp"
    438. if [ -z "$tmp" ]; then
    439. print_info "No FTP Service"
    440. pass=$(($pass+1))
    441. print_pass
    442. else
    443. local_umask=`cat /etc/vsftpd/vsftpd.conf | grep local_umask | grep 022 | grep -v ^#`
    444. anon_umask=`cat /etc/vsftpd/vsftpd.conf | grep anon_umask | grep 022 | grep -v ^#`
    445. if [ -n "$local_umask" ] && [ -n "$anon_umask" ]; then
    446. pass=$(($pass+1))
    447. print_pass
    448. else
    449. print_info 'local_umask:'"[ $local_umask ]"
    450. print_info 'anon_umask:'"[ $anon_umask ]"
    451. fail=$(($fail+1))
    452. print_fail
    453. fi
    454. fi
    455. check_point="文件权限-2:检查重要目录或文件权限设置"
    456. index=$(($index+1))
    457. print_check_point $index "$check_point"
    458. passwd=`stat -c %a /etc/passwd`
    459. shadow=`stat -c %a /etc/shadow`
    460. group=`stat -c %a /etc/group`
    461. print_info "'在设备权限配置能力内,根据用户的业务需要,配置其所需的最小权限'"
    462. print_info "建议文件权限:(不大于左侧值)"
    463. print_info "644 /etc/passwd"
    464. print_info "400 /etc/shadow"
    465. print_info "644 /etc/group"
    466. print_dot_line
    467. print_info "实际检测值为:"
    468. print_info "$passwd"" /etc/passwd"
    469. print_info "$shadow"" /etc/shadow"
    470. print_info "$group"" /etc/group"
    471. if [ "$passwd" -le 644 ] && [ "$shadow" -le 400 ] && [ "$group" -le 644 ]; then
    472. pass=$(($pass+1))
    473. print_pass
    474. else
    475. fail=$(($fail+1))
    476. print_fail
    477. fi
    478. check_point="网络通信-1:检查是否禁止root用户远程登录"
    479. index=$(($index+1))
    480. print_check_point $index "$check_point"
    481. Protocol=`cat /etc/ssh/sshd_config | grep -i Protocol | egrep -v ^\# | awk '{print $2}'`
    482. PermitRootLogin=`cat /etc/ssh/sshd_config | grep -i PermitRootLogin | egrep -v ^\# | awk '{print $2}'`
    483. print_info "'PermitRootLogin 为no 且 Protocol 为2'"
    484. print_dot_line
    485. print_info "/etc/ssh/sshd_config 两项配置如下:"
    486. print_info 'PermitRootLogin ==> '"[ $PermitRootLogin ]"
    487. print_info 'Protocol ==> '"[ $Protocol ]"
    488. if [ "$PermitRootLogin" = "no" ] && [ "$Protocol" -eq 2 ]; then
    489. pass=$(($pass+1))
    490. print_pass
    491. else
    492. fail=$(($fail+1))
    493. print_fail
    494. fi
    495. check_point="网络通信-2:检查使用IP协议远程维护的设备是否配置SSH协议,禁用Telnet协议"
    496. index=$(($index+1))
    497. print_check_point $index "$check_point"
    498. print_dot_line
    499. telnet=`netstat -lntp |grep telnet`
    500. ssh=`netstat -lntp |grep ssh`
    501. print_info "==> telnet"
    502. print_info "$telnet"
    503. print_info "==> ssh"
    504. print_info "$ssh"
    505. if [ -z "$telnet" ] && [ -n "$ssh" ]; then
    506. pass=$(($pass+1))
    507. print_pass
    508. else
    509. fail=$(($fail+1))
    510. print_fail
    511. fi
    512. check_point="网络通信-3:检查是否修改SNMP默认团体字"
    513. index=$(($index+1))
    514. print_check_point $index "$check_point"
    515. print_info "'如果没有必要,需要停止SNMP服务,如果确实需要使用SNMP服务,需要修改SNMP默认团体字'"
    516. print_dot_line
    517. snmp=`ps -ef|grep "snmpd"|grep -v "grep"`
    518. if [ -z "$snmp" ]; then
    519. print_info "SNMP Server is not running..."
    520. pass=$(($pass+1))
    521. print_pass
    522. else
    523. string=`cat /etc/snmp/snmpd.conf | grep com2sec | grep public | grep -v ^# `
    524. if [ -n "$string" ]; then
    525. fail=$(($fail+1))
    526. print_fail
    527. else
    528. pass=$(($pass+1))
    529. print_pass
    530. fi
    531. fi
    532. check_point="网络通信-4:检查是否禁止root用户登录FTP"
    533. index=$(($index+1))
    534. print_check_point $index "$check_point"
    535. print_info "'FTP服务未运行 或 root被禁用'"
    536. print_dot_line
    537. tmp=`ps -ef | grep ftp | grep -v grep`
    538. if [ -z "$tmp" ]; then
    539. print_info "No FTP Service"
    540. pass=$(($pass+1))
    541. print_pass
    542. else
    543. print_info "1.FTP服务正在运行..."
    544. print_info "2.检查 /etc/vsftpd/ftpusers 配置文件中是否有root,以下是文件内容"
    545. print_info "`cat /etc/vsftpd/ftpusers`"
    546. root=`cat /etc/vsftpd/ftpusers | grep root | grep -v ^#`
    547. if [ -n "$root" ]; then
    548. pass=$(($pass+1))
    549. print_pass
    550. else
    551. fail=$(($fail+1))
    552. print_fail
    553. fi
    554. fi
    555. check_point="网络通信-5:检查是否使用PAM认证模块禁止wheel组之外的用户su为root"
    556. index=$(($index+1))
    557. print_check_point $index "$check_point"
    558. print_info "'使用PAM禁止任何人su为root'"
    559. print_info "检查/etc/pam.d/su文件中,是否存在如下配置:"
    560. print_info "auth sufficient pam_rootok.so"
    561. print_info "auth required pam_wheel.so group=wheel"
    562. print_dot_line
    563. pam_rootok=`cat /etc/pam.d/su | grep auth | grep sufficient | grep pam_rootok.so | grep -v ^#`
    564. pam_wheel=`cat /etc/pam.d/su | grep auth | grep pam_wheel.so | grep group=wheel | grep -v ^#`
    565. print_info "实际配置如下:"
    566. print_info "$pam_rootok"
    567. print_info "$pam_wheel"
    568. if [ -n "$pam_rootok" ] && [ -n "$pam_wheel" ]; then
    569. pass=$(($pass+1))
    570. print_pass
    571. else
    572. fail=$(($fail+1))
    573. print_fail
    574. fi
    575. check_point="其他配置-1:检查是否禁止匿名用户登录FTP"
    576. index=$(($index+1))
    577. print_check_point $index "$check_point"
    578. print_info "'FTP服务未启用或者限制了匿名账号登录ftp服务器则合规'"
    579. print_dot_line
    580. tmp=`ps -ef | grep ftp | grep -v grep`
    581. if [ -z "$tmp" ]; then
    582. print_info "No FTP Service"
    583. pass=$(($pass+1))
    584. print_pass
    585. else
    586. tmp=`cat /etc/vsftpd/vsftpd.conf | grep "anonymous_enable=NO" | grep -v ^#`
    587. if [ -z "$tmp" ]; then
    588. tmp=`cat /etc/vsftpd/vsftpd.conf | grep "anonymous_enable" | grep -v ^#`
    589. print_info "$tmp"
    590. fail=$(($fail+1))
    591. print_fail
    592. else
    593. pass=$(($pass+1))
    594. print_pass
    595. fi
    596. fi
    597. check_point="其他配置-2:检查是否删除了潜在危险文件"
    598. index=$(($index+1))
    599. print_check_point $index "$check_point"
    600. print_info "'系统不应该存在.rhost、.netrc、hosts.equiv这三个文件则合规'"
    601. print_dot_line
    602. rhost=`locate .rhost | egrep 'rhost$'`
    603. equiv=`locate .netrc | egrep 'netrc$'`
    604. equiv=`locate .equiv | egrep 'hosts.equiv$'`
    605. print_info "rhost ==> "" [ $rhost ]"
    606. print_info "netrc ==> "" [ $netrc ]"
    607. print_info "equiv ==> "" [ $equiv ]"
    608. if [ -z "$rhost" ] && [ -z "$netrc" ] && [ -z "$equiv" ]; then
    609. pass=$(($pass+1))
    610. print_pass
    611. else
    612. fail=$(($fail+1))
    613. print_fail
    614. fi
    615. check_point="其他配置-3:检查是否设置命令行界面超时退出"
    616. index=$(($index+1))
    617. print_check_point $index "$check_point"
    618. print_info "'命令行界面超时自动登出时间TMOUT应不大于300s'"
    619. print_dot_line
    620. TMOUT=`cat /etc/profile |grep -i TMOUT | grep -v ^#`
    621. if [ -z "$TMOUT" ]; then
    622. print_info "没有设置超时时间TMOUT"
    623. fail=$(($fail+1))
    624. print_fail
    625. else
    626. TMOUT=`cat /etc/profile |grep -i TMOUT | egrep -v ^\# | awk -F "=" '{print $2}'`
    627. if [ "$TMOUT" -gt 300 ]; then
    628. print_info "TMOUT值过大:""$TMOUT"
    629. fail=$(($fail+1))
    630. print_fail
    631. else
    632. print_info "TMOUT:""$TMOUT"
    633. pass=$(($pass+1))
    634. print_pass
    635. fi
    636. fi
    637. check_point="其他配置-4:检查系统是否禁用Ctrl+Alt+Delete组合键"
    638. index=$(($index+1))
    639. print_check_point $index "$check_point"
    640. tmp=`cat /usr/lib/systemd/system/ctrl-alt-del.target | grep "Alias=ctrl-alt-del.target" | grep -v ^#`
    641. print_info "'应禁用Ctrl+Alt+Delete组合键重启系统'"
    642. print_dot_line
    643. print_info "Ctrl+Alt+Delete的配置如下:"
    644. print_info $tmp
    645. if [ -n "$tmp" ]; then
    646. fail=$(($fail+1))
    647. print_fail
    648. else
    649. pass=$(($pass+1))
    650. print_pass
    651. fi
    652. check_point="其他配置-5:检查root用户的path环境变量"
    653. index=$(($index+1))
    654. print_check_point $index "$check_point"
    655. print_info "'PATH环境变量中不存在.或者..的路径(此处以不存在'..'为检查条件,因为'.'可能会存在于软件版本号中)'"
    656. print_dot_line
    657. print_info "PATH环境变量如下:"
    658. tmp=`echo $PATH | egrep '\.\.'`
    659. print_info "$PATH"
    660. if [ -z "$tmp" ]; then
    661. pass=$(($pass+1))
    662. print_pass
    663. else
    664. fail=$(($fail+1))
    665. print_fail
    666. fi
    667. check_point="其他配置-6:检查历史命令设置"
    668. index=$(($index+1))
    669. print_check_point $index "$check_point"
    670. print_info "'HISTFILESIZE和HISTSIZE的值应小于等于5'"
    671. print_dot_line
    672. print_info "实际检测值为:"
    673. HISTSIZE=`cat /etc/profile | grep ^HISTSIZE | egrep -v ^\#`
    674. HISTFILESIZE=`cat /etc/profile | grep ^HISTFILESIZE | egrep -v ^\#`
    675. if [ -n "$HISTSIZE" ] && [ -n "$HISTFILESIZE" ]; then
    676. HISTSIZE=`cat /etc/profile | grep HISTSIZE | egrep -v ^\# | awk -F "=" '{print $2}'`
    677. HISTFILESIZE=`cat /etc/profile | grep HISTFILESIZE | egrep -v ^\# | awk -F "=" '{print $2}'`
    678. print_info "HISTSIZE => "" [ $HISTSIZE ]"
    679. print_info "HISTFILESIZE => "" [ $HISTFILESIZE ]"
    680. if [ "$HISTSIZE" -lt 5 ] && [ "$HISTFILESIZE" -lt 5 ]; then
    681. pass=$(($pass+1))
    682. print_pass
    683. else
    684. fail=$(($fail+1))
    685. print_fail
    686. fi
    687. else
    688. fail=$(($fail+1))
    689. print_fail
    690. fi
    691. check_point="其他配置-7:检查是否设置SSH成功登录后Banner"
    692. index=$(($index+1))
    693. print_check_point $index "$check_point"
    694. print_info "'用户通过网络或者本地成功登录系统后,显示一些警告信息'"
    695. print_dot_line
    696. tmp=`systemctl status sshd | grep running`
    697. if [ -z "$tmp" ]; then
    698. print_info "==>SSHD is not running..."
    699. pass=$(($pass+1))
    700. print_pass
    701. else
    702. temp=`cat /etc/motd`
    703. if [ -n "$temp" ]; then
    704. print_info "请手工检查/etc/motd文件中的内容是否符合要求"
    705. print_info "$temp"
    706. manual=$(($manual+1))
    707. print_manual_check
    708. else
    709. print_info "/etc/motd文件中内容为空,不提示登录信息"
    710. pass=$(($pass+1))
    711. print_pass
    712. fi
    713. fi
    714. check_point="其他配置-8:检查是否限制FTP用户登录后能访问的目录"
    715. index=$(($index+1))
    716. print_check_point $index "$check_point"
    717. print_info "'FTP服务器应该限制FTP可以使用的目录范围'"
    718. print_dot_line
    719. tmp=`ps -ef | grep ftp | grep -v grep`
    720. if [ -z "$tmp" ]; then
    721. print_info "No FTP Service Running"
    722. pass=$(($pass+1))
    723. print_pass
    724. else
    725. chroot_local_user=`cat /etc/vsftpd/vsftpd.conf | grep ^chroot_local_user=NO`
    726. chroot_list_enable=`cat /etc/vsftpd/vsftpd.conf | grep ^chroot_list_enable=YES`
    727. chroot_list_file=`cat /etc/vsftpd/vsftpd.conf | grep ^chroot_list_file=/etc/vsftpd/chroot_list`
    728. if [ -n "$chroot_local_user" ] && [ -n "$chroot_list_enable" ] && [ -n "$chroot_list_file" ]; then
    729. pass=$(($pass+1))
    730. print_pass
    731. else
    732. fail=$(($fail+1))
    733. print_fail
    734. fi
    735. fi
    736. check_point="其他配置-9:检查是否关闭数据包转发功能"
    737. index=$(($index+1))
    738. print_check_point $index "$check_point"
    739. print_info "'对于不做路由功能的系统,应该关闭数据包转发功能'"
    740. print_dot_line
    741. ip_forward=`sysctl -n net.ipv4.ip_forward`
    742. print_info "实际值 ==> ip_forward:"" [ $ip_forward ] "
    743. if [ 0 -eq "$ip_forward" ]; then
    744. pass=$(($pass+1))
    745. print_pass
    746. else
    747. fail=$(($fail+1))
    748. print_fail
    749. fi
    750. check_point="其他配置-10:检查别名文件/etc/aliase"
    751. index=$(($index+1))
    752. print_check_point $index "$check_point"
    753. print_info "'检查是否配合配置了ls和rm命令别名'"
    754. print_dot_line
    755. aol=`cat ~/.bashrc | grep "^alias ls='ls -aol'"`
    756. rmi=`cat ~/.bashrc | grep "^alias rm='rm -i"`
    757. print_info "aol ==> "" [ $aol ]"
    758. print_info "rmi ==> "" [ $rmi ]"
    759. if [ -n "$aol" ] && [ -n "$rmi" ]; then
    760. pass=$(($pass+1))
    761. print_pass
    762. else
    763. fail=$(($fail+1))
    764. print_fail
    765. fi
    766. check_point="其他配置-11:检查是否使用NTP(网络时间协议)保持时间同步"
    767. index=$(($index+1))
    768. print_check_point $index "$check_point"
    769. print_info "'如果网络中存在信任的NTP服务器,应该配置系统使用NTP服务保持时间同步'"
    770. print_dot_line
    771. print_info "NTP服务运行状态信息:"
    772. ntpd=`ps -ef|egrep "ntp|ntpd"|grep -v grep | grep "/usr/sbin/ntpd"`
    773. print_info "$ntpd"
    774. if [ -n "$ntpd" ]; then
    775. server=`cat /etc/ntp.conf | grep ^server`
    776. print_info "==> servers <=="
    777. print_info "$server"
    778. if [ -n "$server" ]; then
    779. pass=$(($pass+1))
    780. print_pass
    781. else
    782. fail=$(($fail+1))
    783. print_fail
    784. fi
    785. else
    786. print_info "==> NTP Service is not running..."
    787. fail=$(($fail+1))
    788. print_fail
    789. fi
    790. check_point="其他配置-12:检查是否限制远程登录IP范围"
    791. index=$(($index+1))
    792. print_check_point $index "$check_point"
    793. print_info "'设备应支持对允许登录到该设备的IP地址范围进行设定'"
    794. print_dot_line
    795. print_info "请手工查看/etc/hosts.allow和/etc/hosts.deny两个文件"
    796. manual=$(($manual+1))
    797. print_manual_check
    798. check_point="其他配置-13:检查NFS(网络文件系统)服务配置"
    799. index=$(($index+1))
    800. print_check_point $index "$check_point"
    801. print_info "'如果没有必要,需要停止NFS服务;如果需要NFS服务,需要限制能够访问NFS服务的IP范围'"
    802. print_dot_line
    803. tmp=`netstat -lntp | grep nfs`
    804. if [ -z "$tmp" ]; then
    805. print_info "NFS 服务未启用..."
    806. pass=$(($pass+1))
    807. print_pass
    808. else
    809. allow=`cat /etc/hosts.allow | grep -v ^#`
    810. deny=`cat /etc/hosts.deny | grep -v ^#`
    811. if [ -n "$allow" ] && [ -n "$deny" ]; then
    812. print_info "hosts.allow 和 hosts.deny皆已配置"
    813. pass=$(($pass+1))
    814. print_pass
    815. else
    816. print_info "未配置hosts.allow 或 hosts.deny"
    817. fail=$(($fail+1))
    818. print_fail
    819. fi
    820. fi
    821. check_point="其他配置-14:检查是否配置定时自动屏幕锁定"
    822. index=$(($index+1))
    823. print_check_point $index "$check_point"
    824. print_info "'对于具备图形界面(含WEB界面)的设备,应配置定时自动屏幕锁定(没有界面可忽略此项)'"
    825. print_dot_line
    826. idle_activation_enabled=`gconftool-2 -g /apps/gnome-screensaver/idle_activation_enabled`
    827. lock_enabled=`gconftool-2 -g /apps/gnome-screensaver/lock_enabled`
    828. mode=`gconftool-2 -g /apps/gnome-screensaver/mode`
    829. idle_delay=`gconftool-2 -g /apps/gnome-screensaver/idle_delay`
    830. print_info "idle_activation_enabled ==> "" [ $idle_activation_enabled ]"
    831. print_info "lock_enabled ==> "" [ $lock_enabled ]"
    832. print_info "mode ==> "" [ $mode ]"
    833. print_info "idle_delay ==> "" [ $idle_delay ]"
    834. if [ "$idle_activation_enabled" == "true" ] && [ "$lock_enabled" == "true" ] \
    835. && [ "$mode" == "blank-only" ] && [ "$idle_delay" -le 15 ]; then
    836. pass=$(($pass+1))
    837. print_pass
    838. else
    839. fail=$(($fail+1))
    840. print_fail
    841. fi
    842. check_point="其他配置-15:检查是否安装chkrootkit进行系统监测"
    843. index=$(($index+1))
    844. print_check_point $index "$check_point"
    845. print_info "'安装入侵检测攻击检查Linux系统是否遭受攻击'"
    846. print_dot_line
    847. chkrootkit=`rpm -qa|grep -i "chkrootkit"`
    848. print_info "chkrootkit ==> "" [ $chkrootkit ]"
    849. if [ -n "$chkrootkit" ]; then
    850. pass=$(($pass+1))
    851. print_pass
    852. else
    853. fail=$(($fail+1))
    854. print_fail
    855. fi
    856. check_point="其他配置-16:检查是否安装OS补丁"
    857. index=$(($index+1))
    858. print_check_point $index "$check_point"
    859. print_info "'手工检查当前系统版本是否安装最新补丁'"
    860. print_dot_line
    861. os=`uname -a`
    862. print_info "==> please manual check os version ..."
    863. print_info "$os"
    864. manual=$(($manual+1))
    865. print_manual_check
    866. check_point="其他配置-17:检查FTP banner设置"
    867. index=$(($index+1))
    868. print_check_point $index "$check_point"
    869. print_dot_line
    870. tmp=`ps -ef | grep ftp | grep -v grep`
    871. if [ -z "$banner" ]; then
    872. print_info "FTP Service is not Running..."
    873. pass=$(($pass+1))
    874. print_pass
    875. else
    876. print_info "请手工检查/etc/vsftpd/vsftpd.conf文件中的banner是否符合要求"
    877. manual=$(($manual+1))
    878. print_manual_check
    879. fi
    880. check_point="其他配置-18:检查Telnet banner设置"
    881. index=$(($index+1))
    882. print_check_point $index "$check_point"
    883. print_info "'FTP登录时需要显示警告信息,隐藏操作系统和FTP服务器相关信息'"
    884. print_dot_line
    885. tmp=`systemctl status telnet.socket | grep active`
    886. if [ -z "$tmp" ]; then
    887. print_info "==>Telnet service is not installed or not running..."
    888. pass=$(($pass+1))
    889. print_pass
    890. else
    891. print_info "Please check /etc/issue、/etc/issue.net whether contains banner information"
    892. manual=$(($manual+1))
    893. print_manual_check
    894. fi
    895. check_point="其他配置-19:检查系统内核参数配置"
    896. index=$(($index+1))
    897. print_check_point $index "$check_point"
    898. print_info "'调整内核安全参数,增强系统安全性,tcp_syncookies的值应设为1'"
    899. print_dot_line
    900. tcp_syncookies=`cat /proc/sys/net/ipv4/tcp_syncookies`
    901. print_info "tcp_syncookies ==> "" [ $tcp_syncookies ]"
    902. if [ "$tcp_syncookies" -eq 1 ]; then
    903. pass=$(($pass+1))
    904. print_pass
    905. else
    906. fail=$(($fail+1))
    907. print_fail
    908. fi
    909. check_point="其他配置-20:检查系统openssh安全配置"
    910. index=$(($index+1))
    911. print_check_point $index "$check_point"
    912. print_info "'建议系统放弃旧的明文登录协议,使用SSH防止会话劫持和嗅探网络上的敏感数据'"
    913. print_dot_line
    914. X11Forwarding=`cat /etc/ssh/sshd_config | grep X11Forwarding | egrep -v ^\# | awk '{print $2}'`
    915. MaxAuthTries=`cat /etc/ssh/sshd_config | grep MaxAuthTries | egrep -v ^\# | awk '{print $2}'`
    916. IgnoreRhosts=`cat /etc/ssh/sshd_config | grep IgnoreRhosts | egrep -v ^\# | awk '{print $2}'`
    917. HostbasedAuthentication=`cat /etc/ssh/sshd_config | grep HostbasedAuthentication | egrep -v ^\# | awk '{print $2}'`
    918. PermitEmptyPasswords=`cat /etc/ssh/sshd_config | grep PermitEmptyPasswords | egrep -v ^\# | awk '{print $2}'`
    919. print_info "X11Forwarding => "" [ $X11Forwarding ]"
    920. print_info "MaxAuthTries => "" [ $MaxAuthTries ]"
    921. print_info "IgnoreRhosts => "" [ $IgnoreRhosts ]"
    922. print_info "HostbasedAuthentication => "" [ $HostbasedAuthentication ]"
    923. print_info "PermitEmptyPasswords => "" [ $PermitEmptyPasswords ]"
    924. if [ "$X11Forwarding" = "no" ] && [ "$MaxAuthTries" -le 4 ] && [ "$IgnoreRhosts" = "yes" ] && \
    925. [ "$HostbasedAuthentication" = "no" ] && [ "$PermitEmptyPasswords" = "no" ]; then
    926. pass=$(($pass+1))
    927. print_pass
    928. else
    929. fail=$(($fail+1))
    930. print_fail
    931. fi
    932. check_point="其他配置-21:检查系统coredump设置"
    933. index=$(($index+1))
    934. print_check_point $index "$check_point"
    935. print_info "'core dump中可能包括系统信息,易被入侵者利用,建议关闭'"
    936. print_info "设置* soft core、* hard core为0,且注释掉ulimit -S -c 0 > /dev/null 2>&1"
    937. print_dot_line
    938. soft=`cat /etc/security/limits.conf | grep soft | grep core | grep 0 | grep ^*`
    939. hard=`cat /etc/security/limits.conf | grep hard | grep core | grep 0 | grep ^*`
    940. if [ -n "$soft" ] && [ -n "$hard" ]; then
    941. tmp=`cat /etc/profile | grep "ulimit -S -c 0 > /dev/null 2>&1" | grep -v ^#`
    942. if [ -n $tmp ]; then
    943. fail=$(($fail+1))
    944. print_fail
    945. else
    946. pass=$(($pass+1))
    947. print_pass
    948. fi
    949. else
    950. fail=$(($fail+1))
    951. print_fail
    952. fi
    953. check_point="其他配置-22:检查是否关闭不必要的服务和端口"
    954. index=$(($index+1))
    955. print_check_point $index "$check_point"
    956. print_info "'结合实际业务需要人工判断是否存在不必要的未关闭的端口和服务,请通过以下命令,手工检查'"
    957. print_dot_line
    958. print_info "# chkconfig --list"
    959. manual=$(($manual+1))
    960. print_manual_check
    961. check_point="其他配置-23:检查磁盘空间占用率"
    962. index=$(($index+1))
    963. print_check_point $index "$check_point"
    964. print_info "'检查磁盘空间占用率,建议不超过80%'"
    965. print_dot_line
    966. print_info "`df -h`"
    967. space=$(df -h | awk -F "[ %]+" 'NR!=1''{print $5}')
    968. flag=0
    969. for i in $space
    970. do
    971. if [ $i -ge 80 ];then
    972. flag=1
    973. print_info "请使用命令手工检查磁盘空间占用率情况"
    974. fi
    975. done
    976. if [ "$flag" -eq 1 ];then
    977. manual=$(($manual+1))
    978. print_manual_check
    979. else
    980. pass=$(($pass+1))
    981. print_pass
    982. fi
    983. print_summary $index $pass $fail $manual

    参考
    https://github.com/starnightcyber/Miscellaneous/blob/master/%E5%9F%BA%E7%BA%BF%E6%A3%80%E6%9F%A5%26%E5%8A%A0%E5%9B%BA/baseline_os.sh