linux自动化安全基线检查脚本

1.基线

即安全基线配置,诸如操作系统、中间件和数据库的一个整体配置,这个版本中各项配置都符合安全方面的标准。比如在系统安装后需要按安全基线标准,将新机器中各项配置调整到一个安全、高效、合理的数值。

2.基线扫描

使用自动化工具、抓取系统和服务的配置项。将抓取到的实际值和标准值进行对比,将不符合的项显示出来,最终以报告的形式体现出扫描结果

有的工具将配置采集和配置对比分开,通过自动化脚本采集配置后再通过特别的软件转换为适合人类阅读的文档

3.自动化脚本

.sh文件为执行文件。而.pl文件则包含着检查命令。

下面给出两个文件的截图


4.Linux基线加固

下面对重要程度排名靠前的项进行脚本加固

基线加固脚本如下:

(*所有操作均在拷贝后执行)


  
  
  1. echo ---------------开始--------------------
  2. echo ---------------aboutkey----------------
  3. cd /etc
  4. if [ -f login.defs ];then
  5. cp /etc/login.defs /home/test1
  6. MINDAY=`cat -n /home/test1/login.defs | grep -v ".*#.*"| grep PASS_MIN_DAYS|awk '{print $1}'`
  7. sed -i ''$MINDAY's/.*PASS_MIN_DAYS.*/PASS_MIN_DAYS 6/' /home/test1/login.defs
  8. WARNAGE=`cat -n /home/test1/login.defs | grep -v ".*#.*"| grep PASS_WARN_AGE|awk '{print $1}'`
  9. sed -i ''$WARNAGE's/.*PASS_WARN.*/PASS_WARN_AGE 30/' /home/test1/login.defs
  10. MAXDAY=`cat -n /home/test1/login.defs | grep -v ".*#.*"| grep PASS_MAX_DAYS|awk '{print $1}'`
  11. sed -i ''$MAXDAY's/.*PASS_MAX.*/PASS_MAX_DAYS 90/' /home/test1/login.defs
  12. MINLEN=`cat -n /home/test1/login.defs | grep -v ".*#.*"| grep PASS_MIN_LEN|awk '{print $1}'`
  13. sed -i ''$MINDAY's/.*PASS_MIN_LEN.*/PASS_MIN_ LEN 6/' /home/test1/login.defs
  14. fi
  15. echo --------------------ok---------------------------
  16. echo -------------------stop the del------------------------
  17. cd /etc/init
  18. if [ -f control-alt-delete.conf ];then
  19. cp /etc/init/control-alt-delete.conf /home/test1
  20. #delete=`grep -n "/sbin/shutdown -r now" /home/test1/control-alt-delete.conf | cut -d ":" -f 1`
  21. #sed -i ''$delete' r s/^/#/' /home/test1/control-alt-delete.conf
  22. #cp /etc/init/control-alt-delete.conf /home/test1
  23. #num1=`grep -n "/sbin/shutdown" /home/test1/control-alt-delete.conf | cut -d "" -f 1`
  24. #sed -i ''$num' r s/^/#/' /home/test1/control-alt-delete.conf
  25. #a=`cat -n /home/test1/control-alt-delete.conf|grep -v "#" | grep "/sbin/shutdown" | awk '{print $1}'`
  26. #text=`sed -n "$a"p /home/test1/control-alt-delete.conf`
  27. #sed -i ''$a'c # '$text'' /home/test1/control-alt-delete.conf
  28. a=`cat -n /home/test1/control-alt-delete.conf|grep -v "#" | grep /sbin/shutdown | awk '{print $1}'`
  29. if [ -z $a ];then
  30. echo ok
  31. else
  32. sed -i ''$a' s/^/#/' /home/test1/control-alt-delete.conf
  33. fi
  34. fi
  35. echo ---------------------ok---------------------------------------
  36. echo ------------------------grub and lilo key------------------------
  37. grub="/home/test1/menu.lst"
  38. if [ ! -x "$grub" ];then
  39. touch "$grub"
  40. echo password=123456 >> "$grub"
  41. else
  42. echo password=123456 >> "$grub"
  43. fi
  44. lilo="/home/test1/lilo.conf"
  45. if [ ! -x "$lilo" ];then
  46. touch "$lilo"
  47. echo password=123456 >> "$lilo"
  48. else
  49. echo password=123456 >> "$lilo"
  50. fi
  51. echo ---------------------ok--------------------------------------
  52. echo ----------------------the history of mouthpasswd------------------
  53. cd /etc
  54. if [ -f profile ];then
  55. cp /etc/profile /home/test1
  56. #num=`sed -n /home/test1/profile | grep HISTFILESIZE | awk '{print $1}'`
  57. #/home/test1/profile | sed $num'c HISTFILESIZE=5'
  58. echo "HISTFILESIZE=5" >> /home/test1/profile
  59. echo "ulimit -S -c unlimited" >> /home/test1/profile
  60. fi
  61. echo -------------------------ok---------------------
  62. echo ------------------------issue-----------------
  63. #issu="/etc/issue.net"
  64. cd /etc
  65. if [ -f issue.net ];then
  66. cp issue.net /home/test1/issue.net.bak
  67. echo ok
  68. fi
  69. echo ok
  70. if [ -f issue ];then
  71. cp issue /home/test1/issue.bak
  72. echo ok
  73. fi
  74. echo -----------------------allow/deny ip-------------------
  75. cd /etc
  76. if [ -f hosts.allow ];then
  77. cp /etc/hosts.allow /home/test1
  78. echo "all:172.18.12.:all" >> /home/test1/hosts.allow
  79. echo "sshd:172.18.12.:all" >> /home/test1/hosts.allow
  80. fi
  81. cd /etc
  82. if [ -f hosts.deny ];then
  83. cp /etc/hosts.deny /home/test1
  84. echo "all:all" >> /home/test1/hosts.deny
  85. fi
  86. echo -----------------ok------------------------
  87. #/etc/init.d/xinetd restart
  88. echo -----------------------------core dump-------------------
  89. cd /etc/security
  90. if [ -f limits.conf ];then
  91. cp /etc/security/limits.conf /home/test1
  92. echo "*soft core 0" >> /home/test1/limits.conf
  93. echo "*hard core 0" >> /home/test1/limits.conf
  94. fi
  95. echo --------------ok-------------------------
  96. echo ----------------------------passwdrepeat---------------------
  97. cd /etc/pam.d
  98. if [ -f system-auth ];then
  99. cp /etc/pam.d/system-auth /home/test1
  100. #num=`grep -n "md5" /home/test1/system-auth | cut -d ":" -f 1`
  101. #sed -i ''$num' r s/$/ remember=5' /home/test1/system-auth
  102. kk=`cat -n /home/test1/system-auth | grep -v ".*#.*"| grep md5|awk '{print $1}'`
  103. echo $kk
  104. version="password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok remember=500"
  105. sed -i ""$kk"c $version" /home/test1/system-auth
  106. letter=`cat -n /home/test1/system-auth |grep password | grep requisite | awk '{print $1}'`
  107. sed -i ''$letter's/pam_cracklib.so/& ucredit=-1 lcredit=-1 dcredit=-1 /' /etc/pam.d/system-auth
  108. fi
  109. echo -----------------ok--------------------
  110. echo --------------------超出退出--------------
  111. cd /etc
  112. if [ -f profile ];then
  113. cp /etc/profile /home/test1
  114. echo "export TMOUT=600" >> /home/test1/profile
  115. fi
  116. echo ------------------ok-------------------
  117. echo ------------------权限-------------------
  118. chmod 644 /etc/passwd
  119. chmod 644 /etc/group
  120. chmod 400 /etc/shadow
  121. #chmod 600 /etc/xinetd.conf
  122. chmod 644 /etc/services
  123. chmod 600 /etc/security
  124. chmod 600 /etc/grub.conf
  125. chmod 600 /boot/grub/grub.conf
  126. chmod 600 /etc/lilo.conf
  127. echo ------------------unmask--------------------
  128. cp /etc/csh.cshrc /home/test1
  129. cp /etc/csh.login /home/test1
  130. cp /etc/bashrc /home/test1
  131. cp /etc/profile /home/test1
  132. sed -i '11 s/.*umask.*/umask 077/' /home/test1/csh.cshrc
  133. sed -i '58 s/.*umask.*/umask 077/' /home/test1/csh.login
  134. sed -i '66 s/.*UMASK.*/UMASK 077/' /home/test1/bashrc
  135. sed -i '62s/.*umask.*/umask 077/' /home/test1/profile
  136. echo --------------------before login banner-------------------
  137. cd /etc
  138. if [ -f ssh_banner ];then
  139. touch /etc/ssh_banner
  140. chown bin:bin /etc/ssh_banner
  141. chmod 644 /etc/ssh_banner
  142. echo "Authorized only.All activity will be monitored and reported" > /etc/ssh_banner
  143. fi
  144. echo -----------------------ok----------------------------
  145. echo -------------------stop root ssh login------------------
  146. cp /etc/pam.d/login /home/test1
  147. echo "auth required pam_securetty.so" >> /home/test1/login
  148. cp /etc/ssh/sshd_config /home/test1
  149. echo "Banner /etc/ssh_banner" >> /home/test1/sshd_config
  150. echo "PermitRootLogin no" >> /home/test1/sshd_config
  151. service sshd restart
  152. echo -------------------------ok-------------------
  153. echo --------------------openssh----------------------------
  154. openssh=`cat -n /home/test1/sshd_config | grep -v ".*#.*"| grep Protocol |awk '{print $1}'`
  155. sed -i ''$openssh's/.*Protocol.*/Protocol 2/' /home/test1/sshd_config
  156. echo -------------ok---------------------------



linux自动化安全基线脚本

发布了51 篇原创文章 · 获赞 7 · 访问量 5594

猜你喜欢

转载自blog.csdn.net/xxx0028/article/details/104953376