Linux主機安全配置的幾個指令碼(轉)

wdnmg發表於2011-03-16
 

vim /etc/login.defs
PASS_MAX_DAYS   90    //口令最大有效天數
PASS_MIN_DAYS   10    //口令最小有效天數
PASS_MIN_LEN    8     //口令最少字元數
PASS_WARN_AGE   5     //口令過期提前警告天數

 

echo 'ONLY Authorized users only! All accesses logged' > /etc/motd

rm -f /etc/issue
rm -f /etc/issue.net
touch /etc/issue
touch /etc/issue.net


find / -name *.rhosts
touch /.rhosts && chmod 0 /.rhosts


防止IP SPOOF
echo "nospoof on" >>/etc/host.conf     


chkconfig bluetooth off
chkconfig firstboot off
chkconfig cups off
chkconfig ip6tables off
chkconfig iptables off
chkconfig isdn off
chkconfig kudzu off
chkconfig sendmail off
chkconfig smartd off
chkconfig autofs off
service vncserver off
service bluetooth stop
service firstboot stop
service cups stop
service ip6tables stop
service iptables stop
service isdn stop
service kudzu stop
service sendmail stop
service smartd stop
service autofs stop
service vncserver stop

鎖定不需要的使用者
usermod -L adm
usermod -L lp
usermod -L sync
usermod -L shutdown
usermod -L halt
usermod -L news
usermod -L uucp
usermod -L operator
usermod -L ftp
usermod -L rpc
usermod -L rpcuser
usermod -L gopher
usermod -L nscd


刪除不需要的組
groupdel lp
groupdel news
groupdel games
groupdel gopher
groupdel uucp


本次必須要保留的使用者是:
root、bin、nobody、ntp、ssh、uucp
必須保留的組包括:
root、nobody、ntp、uucp


useradd jboss
passwd jboss
usermod -g jboss -G wheel jboss
vim /etc/pam.d/su
auth required pam_wheel.so use.uid   把這一行的註釋去掉,只有wheel組的使用者才能su到root
chown -R jboss.jboss /usr/local/jboss-4.2.3.GA

 

 

#!/bin/bash
cd /usr/local/jboss-4.2.3.GA/server/default/deploy/
rm -rf bsh-deployer.xml
rm -rf client-deployer-service.xml
rm -rf ear-deployer.xml
rm -rf ejb-deployer.xml
rm -rf http-invoker.sar
rm -rf jboss-bean.deployer
rm -rf jms
rm -rf jsr88-service.xml
rm -rf schedule-manager-service.xml
rm -rf scheduler-service.xml
rm -rf sqlexception-service.xml
rm -rf uuid-key-generator.sar
rm -rf jmx-console.war
rm -rf management
cd /usr/local/jboss-4.2.3.GA/server/default/deploy/jboss-web.deployer/ROOT.war
rm -rf *
exit

隱藏jboss版本資訊
vim /usr/local/jboss-4.2.3.GA/server/default/deploy/jboss-web.deployer/server.xml


             maxThreads="250" maxHttpHeaderSize="8192"
         emptySessionPath="true" protocol="HTTP/1.1"
         enableLookups="false" redirectPort="8443" acceptCount="100"
         connectionTimeout="20000" disableUploadTimeout="true"
         server="ESB" />

在這一段最後加上一個server="ESB"/>

vim /usr/local/jboss-4.2.3.GA/server/default/deploy/jboss-web.deployer/conf/web.xml


     
         X-Powered-By
         ESB2.0
     

vim /etc/ssh/sshd_config
把PermitRootLogin yes前面的"#"去掉,把"yes"改為"no"

#ClientAliveCountMax 3
把這個前面的"#"去掉,就是超過3分鐘閒置,就自動斷開session

service sshd restart

sed -i -e 's/^LANG=.*/LANG="en_US.UTF-8"/'   /etc/sysconfig/i18n
source /etc/sysconfig/i18n


vim /etc/hosts.allow
sshd:192.168.0.199,192.168.0.201,192.168.0.121

vim /etc/hosts.deny
sshd:all

service xinetd restart


截短~/.bash_history檔案
vim /etc/profile
HISTSIZE=1000改成20


vim /etc/skel/.bash_logout 
在/etc/skel/.bash_logout在檔案中新增這行
rm -rf $HOME/.bash_history 
這樣當使用者登出時,.bash_history被刪除


DenyHosts是用Python2.3寫的一個程式,它會分析/var/log/secure(redhat,Fedora Core)等日誌檔案,當發現同一IP在進行多次SSH密
碼嘗試時就會記錄IP到/etc/hosts.deny檔案,從而達到自動遮蔽該IP的目的。

wget http://downloads.sourceforge.net/project/denyhosts/denyhosts/2.6/DenyHosts-2.6.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fdenyhosts%2Ffiles%2F&ts=1281580068&mirror=ncu

tar -zxvf DenyHosts-2.6.tar.gz
cd DenyHosts-2.6
python setup.py install


cd /usr/share/denyhosts/
grep -v "^#" denyhosts.cfg-dist > denyhosts.cfg
cp daemon-control-dist daemon-control

vim denyhosts.cfg

PURGE_DENY = 5m                //5分鐘清除

DENY_THRESHOLD_INVALID = 1    //允許無效使用者失敗的次數

DENY_THRESHOLD_VALID = 5       //允許普通使用者登陸失敗的次數

DENY_THRESHOLD_ROOT = 5        //允許root登陸失敗的次數

 


chown root daemon-control
chmod 700 daemon-control


vim /etc/rc.local
/usr/share/denyhosts/daemon-control start

vim /etc/audit/audit.rules  最後增加一行,對該目錄審計
-w /usr/local/jboss-4.2.3.GA/server/default/deploy -pwa
service auditd restart

檢視審計規則 auditctl -l
檢視audit執行狀態 auditctl -s

審計工具
wget http://www.rootkit.nl/files/lynis-1.2.9.tar.gz

給關鍵檔案加root許可權
chmod  744  /etc/passwd
chmod  740  /etc/shadow
chmod  744  /etc/rc3.d
chmod  744  /etc/profile
chmod  1777  /tmp
chmod 644 /etc/exports
chmod -R 700 /etc/rc.d/init.d/*

Control-Alt-Delete 鍵盤關機命令
vim /etc/inittab
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
這一行前面加#,改成
#ca::ctrlaltdel:/sbin/shutdown -t3 -r now


給檔案加鎖(暫時不實施)
chattr +i /etc/passwd
chattr +i /etc/shadow
chattr +i /etc/group
chattr +i /etc/services
chmod 600 /etc/xinetd.conf

當chattr +i時就是禁止對檔案進行修改,當我們要新增使用者時,就會有麻煩,因為passwd檔案禁止修改寫入.
所以我們還要該掉它的屬性.chattr -i.
 

指令碼如下:

init_sysctl.sh

 

  1. #!/bin/bash  
  2. cp /etc/sysctl.conf /etc/sysctl.conf.bak  
  3. echo "####add by hx10###">> /etc/sysctl.conf  
  4. echo "net.ipv4.ip_local_port_range = 1024 65536" >> /etc/sysctl.conf  
  5. echo "net.core.rmem_max=16777216>> /etc/sysctl.conf  
  6. echo "net.core.wmem_max=16777216>> /etc/sysctl.conf  
  7. echo "net.ipv4.tcp_rmem=4096 87380 16777216" >> /etc/sysctl.conf  
  8. echo "net.ipv4.tcp_wmem=4096 65536 16777216" >> /etc/sysctl.conf  
  9. echo "net.ipv4.tcp_fin_timeout = 15>> /etc/sysctl.conf  
  10. echo "net.ipv4.tcp_keepalive_time = 600>> /etc/sysctl.conf  
  11. echo "net.ipv4.tcp_tw_recycle = 1>> /etc/sysctl.conf  
  12. echo "net.core.netdev_max_backlog = 30000>> /etc/sysctl.conf  
  13. echo "net.ipv4.tcp_no_metrics_save=1>> /etc/sysctl.conf  
  14. echo "net.core.somaxconn = 262144>> /etc/sysctl.conf  
  15. echo "net.ipv4.tcp_syncookies = 1>> /etc/sysctl.conf  
  16. echo "net.ipv4.tcp_max_orphans = 8000>> /etc/sysctl.conf  
  17. echo "net.ipv4.tcp_max_syn_backlog = 8000>> /etc/sysctl.conf  
  18. echo "net.ipv4.tcp_synack_retries = 2>> /etc/sysctl.conf  
  19. echo "net.ipv4.tcp_syn_retries = 2>> /etc/sysctl.conf  
  20. sysctl -p  
  21. ###change max open files  
  22. ulimit -SHn 102400  
  23. echo "ulimit -SHn 102400" >>/etc/rc.local  
  24. ulimit -a  
  25. exit  


 

init_system.sh

 

  1. #!/bin/bash  
  2. #change password policy  
  3. cp /etc/login.defs  /etc/login.defs.bak  
  4. sed -i 's/^PASS_MAX_DAYS\s*99999/PASS_MAX_DAYS    90/' /etc/login.defs  
  5. sed -i 's/^PASS_MIN_DAYS\s*0/PASS_MIN_DAYS    10/' /etc/login.defs  
  6. sed -i 's/^PASS_MIN_LEN\s*5/PASS_MIN_LEN     8/' /etc/login.defs  
  7. sed -i 's/^PASS_WARN_AGE\s*7/PASS_WARN_AGE    5/' /etc/login.defs  
  8. #stop Linux detection  
  9. echo 'ONLY Authorized users only! All accesses logged' > /etc/motd  
  10. cp /etc/issue /etc/issue.bak  
  11. cp /etc/issue.net /etc/issue.net.bak  
  12. rm -f /etc/issue  
  13. rm -f /etc/issue.net  
  14. touch /etc/issue  
  15. touch /etc/issue.net  
  16. #stop rhosts  
  17. #find / -name *.rhosts   
  18. touch /.rhosts && chmod 0 /.rhosts  
  19. #stop ip spoof  
  20. cp /etc/host.conf /etc/host.conf.bak  
  21. echo "nospoof on" >>/etc/host.conf  
  22. #stop service  
  23. chkconfig bluetooth off  
  24. chkconfig firstboot off  
  25. chkconfig cups off  
  26. chkconfig ip6tables off  
  27. chkconfig iptables off  
  28. chkconfig isdn off  
  29. chkconfig kudzu off  
  30. chkconfig sendmail off  
  31. chkconfig smartd off  
  32. chkconfig autofs off  
  33. chkconfig ntpd off  
  34. chkconfig snmpd on  
  35. chkconfig portmap on  
  36. chkconfig nfs on  
  37. #service vncserver off  
  38. /etc/init.d/vncserver stop  
  39. service bluetooth stop  
  40. service firstboot stop  
  41. service cups stop  
  42. service ip6tables stop  
  43. service iptables stop  
  44. service isdn stop  
  45. service kudzu stop  
  46. service sendmail stop  
  47. service smartd stop  
  48. service autofs stop  
  49. service vncserver stop  
  50. service ntpd stop  
  51. service snmpd restart  
  52. service portmap restart  
  53. service nfs restart  
  54. #lock user unnecessary  
  55. usermod -L adm  
  56. usermod -L lp  
  57. usermod -L sync  
  58. usermod -L shutdown  
  59. usermod -L halt  
  60. usermod -L news  
  61. usermod -L uucp  
  62. usermod -L operator  
  63. usermod -L ftp  
  64. usermod -L rpc  
  65. usermod -L rpcuser  
  66. usermod -L gopher  
  67. usermod -L nscd  
  68. #add jboss user  
  69. useradd jboss && echo "23adjfajskdfja" | passwd --stdin jboss  
  70. usermod -g jboss -G wheel jboss   
  71. #chown -R jboss.jboss /usr/local/jboss-4.2.3.GA  
  72. #change system character  
  73. cp /etc/sysconfig/i18n /etc/sysconfig/i18n.bak  
  74. cat /dev/null> /etc/sysconfig/i18n  
  75. echo 'LANG="en_US.UTF-8">/etc/sysconfig/i18n  
  76. #echo 'LANG="zh_CN.GB18030">/etc/sysconfig/i18n  
  77. #sed -i -e 's/^LANG=.*/LANG="en_US.UTF-8"/' /etc/sysconfig/i18n  
  78. source /etc/sysconfig/i18n  
  79. #add ssh user  
  80. cp /etc/hosts.allow /etc/hosts.allow.bak  
  81. cp /etc/hosts.deny /etc/hosts.deny.bak  
  82. echo "sshd:192.168.0.199,192.168.0.201,192.168.0.121" >>/etc/hosts.allow  
  83. echo "sshd:all" >>/etc/hosts.deny  
  84. #Limit ROOT from Remote server  
  85. cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak  
  86. echo "PermitRootLogin no" >> /etc/ssh/sshd_config  
  87. echo "ClientAliveCountMax 2" >> /etc/ssh/sshd_config  
  88. #clean history  
  89. cp /etc/skel/.bash_logout /etc/skel/.bash_logout.bak  
  90. cp /etc/profile /etc/profile.bak  
  91. sed -i 's/^HISTSIZE=1000/HISTSIZE=20/' /etc/profile  
  92. echo "rm -rf \$HOME/.bash_history " >>/etc/skel/.bash_logout  
  93. #install DenyHosts  
  94. cd /usr/local/src  
  95. wget ftp://192.168.0.21/pub/DenyHosts-2.6.tar.gz  
  96. tar -zxvf DenyHosts-2.6.tar.gz  
  97. cd DenyHosts-2.6  
  98. python setup.py install  
  99. cd /usr/share/denyhosts/  
  100. grep -v "^#" denyhosts.cfg-dist > denyhosts.cfg  
  101. cp daemon-control-dist daemon-control  
  102. cp denyhosts.cfg denyhosts.cfg.bak  
  103. sed -i 's/^PURGE_DENY =/PURGE_DENY = 5m/' /usr/share/denyhosts/denyhosts.cfg  
  104. sed -i 's/^DENY_THRESHOLD_INVALID = 5/DENY_THRESHOLD_INVALID = 1/' /usr/share/denyhosts/denyhosts.cfg  
  105. sed -i 's/^DENY_THRESHOLD_VALID = 10/DENY_THRESHOLD_VALID = 5/' /usr/share/denyhosts/denyhosts.cfg  
  106. sed -i 's/^DENY_THRESHOLD_ROOT = 1/DENY_THRESHOLD_ROOT = 5/' /usr/share/denyhosts/denyhosts.cfg  
  107. chown root daemon-control  
  108. chmod 700 daemon-control  
  109. cp /etc/rc.local /etc/rc.local.bak  
  110. echo "/usr/share/denyhosts/daemon-control start" >>/etc/rc.local  
  111. /usr/share/denyhosts/daemon-control start  
  112. #limit alt-ctl-delete  
  113. cp /etc/inittab /etc/inittab.bak  
  114. sed -i 's/^ca::ctrlaltdel:\/sbin\/shutdown -t3 -r now/\#ca::ctrlaltdel:\/sbin\/shutdown -t3 -r now/g' /etc/inittab  
  115. #limit key file  
  116. chmod  744  /etc/passwd  
  117. chmod  740  /etc/shadow  
  118. chmod  744  /etc/rc3.d  
  119. chmod  744  /etc/profile  
  120. chmod  1777  /tmp  
  121. chmod 644 /etc/exports  
  122. chmod -R 700 /etc/rc.d/init.d/*  
  123. chattr +i /etc/passwd  
  124. chattr +i /etc/shadow  
  125. chattr +i /etc/group  
  126. chattr +i /etc/services  
  127. chmod 600 /etc/xinetd.conf  
  128. #remove   
  129. #chattr -i /etc/passwd  
  130. #chattr -i /etc/shadow  
  131. service sshd restart  
  132. service xinetd restart  
  133. ps -ef |grep vnc |grep '/usr/share/vnc/classes' |awk '{print $2}' |xargs kill -9  
  134. echo "congratulations!"  
  135. sleep 2  
  136. exit    
  137.  

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70109/viewspace-689583/,如需轉載,請註明出處,否則將追究法律責任。

相關文章