Linux主機安全配置的幾個指令碼(轉)
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
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
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的目的。
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
- #!/bin/bash
- cp /etc/sysctl.conf /etc/sysctl.conf.bak
- echo "####add by hx10###">> /etc/sysctl.conf
- echo "net.ipv4.ip_local_port_range = 1024 65536" >> /etc/sysctl.conf
- echo "net.core.rmem_max=16777216" >> /etc/sysctl.conf
- echo "net.core.wmem_max=16777216" >> /etc/sysctl.conf
- echo "net.ipv4.tcp_rmem=4096 87380 16777216" >> /etc/sysctl.conf
- echo "net.ipv4.tcp_wmem=4096 65536 16777216" >> /etc/sysctl.conf
- echo "net.ipv4.tcp_fin_timeout = 15" >> /etc/sysctl.conf
- echo "net.ipv4.tcp_keepalive_time = 600" >> /etc/sysctl.conf
- echo "net.ipv4.tcp_tw_recycle = 1" >> /etc/sysctl.conf
- echo "net.core.netdev_max_backlog = 30000" >> /etc/sysctl.conf
- echo "net.ipv4.tcp_no_metrics_save=1" >> /etc/sysctl.conf
- echo "net.core.somaxconn = 262144" >> /etc/sysctl.conf
- echo "net.ipv4.tcp_syncookies = 1" >> /etc/sysctl.conf
- echo "net.ipv4.tcp_max_orphans = 8000" >> /etc/sysctl.conf
- echo "net.ipv4.tcp_max_syn_backlog = 8000" >> /etc/sysctl.conf
- echo "net.ipv4.tcp_synack_retries = 2" >> /etc/sysctl.conf
- echo "net.ipv4.tcp_syn_retries = 2" >> /etc/sysctl.conf
- sysctl -p
- ###change max open files
- ulimit -SHn 102400
- echo "ulimit -SHn 102400" >>/etc/rc.local
- ulimit -a
- exit
init_system.sh
- #!/bin/bash
- #change password policy
- cp /etc/login.defs /etc/login.defs.bak
- sed -i 's/^PASS_MAX_DAYS\s*99999/PASS_MAX_DAYS 90/' /etc/login.defs
- sed -i 's/^PASS_MIN_DAYS\s*0/PASS_MIN_DAYS 10/' /etc/login.defs
- sed -i 's/^PASS_MIN_LEN\s*5/PASS_MIN_LEN 8/' /etc/login.defs
- sed -i 's/^PASS_WARN_AGE\s*7/PASS_WARN_AGE 5/' /etc/login.defs
- #stop Linux detection
- echo 'ONLY Authorized users only! All accesses logged' > /etc/motd
- cp /etc/issue /etc/issue.bak
- cp /etc/issue.net /etc/issue.net.bak
- rm -f /etc/issue
- rm -f /etc/issue.net
- touch /etc/issue
- touch /etc/issue.net
- #stop rhosts
- #find / -name *.rhosts
- touch /.rhosts && chmod 0 /.rhosts
- #stop ip spoof
- cp /etc/host.conf /etc/host.conf.bak
- echo "nospoof on" >>/etc/host.conf
- #stop service
- 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
- chkconfig ntpd off
- chkconfig snmpd on
- chkconfig portmap on
- chkconfig nfs on
- #service vncserver off
- /etc/init.d/vncserver stop
- 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
- service ntpd stop
- service snmpd restart
- service portmap restart
- service nfs restart
- #lock user unnecessary
- 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
- #add jboss user
- useradd jboss && echo "23adjfajskdfja" | passwd --stdin jboss
- usermod -g jboss -G wheel jboss
- #chown -R jboss.jboss /usr/local/jboss-4.2.3.GA
- #change system character
- cp /etc/sysconfig/i18n /etc/sysconfig/i18n.bak
- cat /dev/null> /etc/sysconfig/i18n
- echo 'LANG="en_US.UTF-8"' >/etc/sysconfig/i18n
- #echo 'LANG="zh_CN.GB18030"' >/etc/sysconfig/i18n
- #sed -i -e 's/^LANG=.*/LANG="en_US.UTF-8"/' /etc/sysconfig/i18n
- source /etc/sysconfig/i18n
- #add ssh user
- cp /etc/hosts.allow /etc/hosts.allow.bak
- cp /etc/hosts.deny /etc/hosts.deny.bak
- echo "sshd:192.168.0.199,192.168.0.201,192.168.0.121" >>/etc/hosts.allow
- echo "sshd:all" >>/etc/hosts.deny
- #Limit ROOT from Remote server
- cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
- echo "PermitRootLogin no" >> /etc/ssh/sshd_config
- echo "ClientAliveCountMax 2" >> /etc/ssh/sshd_config
- #clean history
- cp /etc/skel/.bash_logout /etc/skel/.bash_logout.bak
- cp /etc/profile /etc/profile.bak
- sed -i 's/^HISTSIZE=1000/HISTSIZE=20/' /etc/profile
- echo "rm -rf \$HOME/.bash_history " >>/etc/skel/.bash_logout
- #install DenyHosts
- cd /usr/local/src
- wget ftp://192.168.0.21/pub/DenyHosts-2.6.tar.gz
- 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
- cp denyhosts.cfg denyhosts.cfg.bak
- sed -i 's/^PURGE_DENY =/PURGE_DENY = 5m/' /usr/share/denyhosts/denyhosts.cfg
- sed -i 's/^DENY_THRESHOLD_INVALID = 5/DENY_THRESHOLD_INVALID = 1/' /usr/share/denyhosts/denyhosts.cfg
- sed -i 's/^DENY_THRESHOLD_VALID = 10/DENY_THRESHOLD_VALID = 5/' /usr/share/denyhosts/denyhosts.cfg
- sed -i 's/^DENY_THRESHOLD_ROOT = 1/DENY_THRESHOLD_ROOT = 5/' /usr/share/denyhosts/denyhosts.cfg
- chown root daemon-control
- chmod 700 daemon-control
- cp /etc/rc.local /etc/rc.local.bak
- echo "/usr/share/denyhosts/daemon-control start" >>/etc/rc.local
- /usr/share/denyhosts/daemon-control start
- #limit alt-ctl-delete
- cp /etc/inittab /etc/inittab.bak
- sed -i 's/^ca::ctrlaltdel:\/sbin\/shutdown -t3 -r now/\#ca::ctrlaltdel:\/sbin\/shutdown -t3 -r now/g' /etc/inittab
- #limit key file
- 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/*
- chattr +i /etc/passwd
- chattr +i /etc/shadow
- chattr +i /etc/group
- chattr +i /etc/services
- chmod 600 /etc/xinetd.conf
- #remove
- #chattr -i /etc/passwd
- #chattr -i /etc/shadow
- service sshd restart
- service xinetd restart
- ps -ef |grep vnc |grep '/usr/share/vnc/classes' |awk '{print $2}' |xargs kill -9
- echo "congratulations!"
- sleep 2
- exit
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70109/viewspace-689583/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【Linux】Linux安全加固指令碼Linux指令碼
- 幾個Linux命令及指令碼使用中的奇淫巧技Linux指令碼
- Nginx與安全有關的幾個配置Nginx
- Linux shell:執行shell指令碼的幾種方式Linux指令碼
- linux 主機mail 系統配置.LinuxAI
- Linux下pppd撥號指令碼配置Linux指令碼
- Linux中主機名的作用是什麼?如何配置主機名?Linux
- Linux常見的網路排查指令,你知道幾個?Linux
- lnmp虛擬主機安全配置研究LNMP
- Linux配置開機自啟動執行指令碼方法有哪些?Linux指令碼
- Linux中配置開機自啟動執行指令碼的方法是什麼?Linux指令碼
- [轉]寫好shell指令碼的13個技巧指令碼
- python編寫圖片主色轉換指令碼Python指令碼
- Linux編寫Bash指令碼的10個技巧Linux指令碼
- 物理裸機配置如何轉換為天翼云云主機配置
- Linux中配置網路地址,主機名Linux
- Linux雲主機安全入侵排查步驟Linux
- 配置簡單的linux 的幾個操作步驟(Cent OS)Linux
- Linux提權的四個指令碼釋出啦!Linux指令碼
- Linux 新增指令碼開機自啟動Linux指令碼
- 網站防黑客入侵的主機系統安全配置方法網站黑客
- linux常用的shell指令碼Linux指令碼
- 幾例實用的Shell指令碼指令碼
- Linux下批量ping某個網段ip的指令碼Linux指令碼
- Linux指令碼分享Linux指令碼
- Linux Shell指令碼Linux指令碼
- Linux配置開機自啟動執行指令碼方法有哪些?常見介紹!Linux指令碼
- 3個例項介紹shell指令碼中幾個特殊引數的用法指令碼
- 基於Linux的mysql主從配置LinuxMySql
- 網站有幾種常見的指令碼型別?網路安全學習網站指令碼型別
- linux shell指令碼中 =~ 的作用Linux指令碼
- linux設定開機自啟動指令碼的最佳方式Linux指令碼
- linux主機名的修改Linux
- 一個簡單的Linux啟動jar包的shell指令碼LinuxJAR指令碼
- 虛擬主機和VPS主機之間的幾點不同
- 我的無線轉有線linux伺服器用到的指令碼們Linux伺服器指令碼
- Apache 虛擬主機裡的 ServerName 指令ApacheServer
- 幾百行程式碼實現一個指令碼直譯器行程指令碼
- 給linux(centos)作業系統設定主機名的幾種方式LinuxCentOS作業系統