OPEnsSH 셸 스 크 립 트 자동 업그레이드

200 여 대의 Linux 서버 를 관리 하고 있 기 때문에 한 사람 이 이렇게 많은 기계 의 안전 보강 을 하 는 것 이 비교적 힘 들 기 때문에 셸 스 크 립 트 를 배 운 후에 과감하게 자주 사용 하 는 스 크 립 트 를 써 서 시스템 의 일상적인 유 지 를 했다. 본 고 는 OPENSSH 의 업 그 레이 드 는 블 로 거들 본인 이 거의 두 세 달 에 한 번 씩 업 그 레이 드 를 해 야 한다.어 쩔 수 없 이 녹 맹 안전 스 캔 시스템 은 항상 관련 된 고위 험 구멍 을 스 캔 하고 OPENSH 버 전 업데이트 도 빈번 하기 때문에 게 으 름 을 피 우지 않 으 면 거의 살 수 없다.쓸데없는 소리 하지 마 세 요. 여기에 스 크 립 트 를 붙 이 고 온라인 서버 에서 수백 번 이나 실 행 했 으 니 문제 가 없 을 겁 니 다.
#!/bin/bash
#################################################################
######    update openssl openssh scirpt                 #########
#####             Author:kl                                 #####
######           Date:2014/07/13                            #####
######        LastModified:2016/06/02                     #######
####  Warning:start telnet service before use the script    #####
#################################################################


####################################################################################
#			update openssh and openssl
				#########
				  #####
				    ##
####################################################################################
#Determine whether the current system installed gcc compiler tools		

zlib_version="zlib-1.2.8"
openssl_version="openssl-1.0.2g"
openssh_version="openssh-7.2p2"

gcc_path=`which gcc`
#gcc_name=`basename $gcc_path`

DATE=$(date +%Y%m%d)

# OS TYPE
#Distributor_ID=$(lsb_release -i)
Distributor=`lsb_release -i|cut -c 17-`
# Determine whether the root user
userid=`id -u`
if [ "$userid" -ne 0 ]; then
	echo "sorry,only root can execute the script. "
	exit
fi


# SET SELINUX=disabled
if [ "$Distributor" != "SUSE LINUX" ]; then

	sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
	setenforce 0
fi

# pam-devel,tcp_wrappers-devel need be installed, Otherwise, the software will install failure
# Support for tcpwrappers/libwrap has been removed in openssh6.7 

if ! rpm -qa|grep pam-devel &>/dev/null; then
	echo "pam-devel is not installed" && exit
fi

#if ! rpm -qa|grep tcp_wrappers-devel &>/dev/null; then
#	echo "tcp_wrappers-devel not installed" && exit
#fi

#  Check whether to open the telnet service

netstat -tnlp | grep -w 23

RETVAL3=$?
if [ $RETVAL3 -eq 0 ]; then
	echo "telnet service is running------------[yes]"
else
	echo "telnet service is not running--------[no]"
	exit
fi
# Determine whether to install gcc package
if [ -e "$gcc_path" ]; then
	echo "gcc is installed----------------[yes]"
else	
	echo "gcc is not installed------------[no]"
	exit 
fi

# stop sshd service 
netstat -tnlp | grep -w 22
RETVAL4=$?
if [ $RETVAL4 -eq 0 ]; then
	service sshd stop
	echo "stop sshd service --------------[yes]"
fi

if [ -e /etc/init.d/sshd ]; then
cp /etc/init.d/sshd /root
fi


# remove openssh*.rpm if exists
if rpm -qa | grep openssh &> /dev/null;	then
rpm -qa | grep openssh > openssh_list.txt

while read line
do
	rpm -e $line --nodeps
	echo "remove $line success------------[yes]"
done  /dev/null
cd $zlib_version
./configure

RETVAL5=$?

if [ $RETVAL5 -ne 0 ]; then
	echo "Configure zlib has encountered an error"
	exit
fi

make

RETVAL6=$?


if [ $RETVAL6 -ne 0 ]; then
	echo "make zlib has encountered an error"
	exit
fi

make install
cd ..
echo "#########################################################"
echo "################                        #################"
echo "################  zlib install success   #################"
echo "################                        #################"
echo "#########################################################"
sleep 2
########## install openssl #############
tar -zxvf "${openssl_version}.tar.gz" > /dev/null
cd $openssl_version
./config shared zlib

RETVAL7=$?


if [ $RETVAL7 -ne 0 ]; then
	echo "Configure openssl has encountered an error"
	exit
fi

make

RETVAL8=$?

if [ $RETVAL8 -ne 0 ]; then
	echo "make openssl has encountered an error"
	exit
fi

make install 

if [ -e /usr/bin/openssl ]; then
	mv /usr/bin/openssl /usr/bin/openssl.OFF && ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
else
	ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
fi

if [ -e /usr/include/openssl ]; then
	mv /usr/include/openssl /usr/include/openssl.OFF && ln -s /usr/local/ssl/include/openssl /usr/include/openssl
else
	ln -s /usr/local/ssl/include/openssl /usr/include/openssl
fi
## Add "/usr/local/ssl/lib" to /etc/ld.so.conf 
ssl_lib=`grep -w "/usr/local/ssl/lib" /etc/ld.so.conf` 
if [ ! -e "$ssl_lib" ]; then
	echo "/usr/local/ssl/lib" >> /etc/ld.so.conf
fi


ldconfig -v

cd ..

echo "#########################################################"
echo "################                        #################"
echo "################ openssl install sucess  ################"
echo "################                        #################"
echo "#########################################################"
sleep 2
############# install openssh ##############
if [ -e /etc/ssh ]; then
	mv /etc/ssh /etc/ssh_$DATE
fi

tar -zxvf "${openssh_version}.tar.gz" > /dev/null
cd $openssh_version
./configure --prefix=/usr --sysconfdir=/etc/ssh --with-zlib --with-pam --with-ssl-dir=/usr/local/ssl --with-md5-passwords

RETVAL9=$?


if [ $RETVAL9 -ne 0 ]; then
	echo "Configure openssh has encountered an error"
	exit
fi

make

RETVAL10=$?


if [ $RETVAL10 -ne 0 -a $RETVAL10 -ne 0 ]; then
        echo "make openssh has encountered an error"
        exit
fi

make install

if [ "$Distributor" == "SUSE LINUX" ]; then
	cd contrib/suse
	cp rc.sshd /etc/init.d/sshd
	chmod +x /etc/init.d/sshd
	chkconfig --add sshd
else

	cd contrib/redhat 
	cp sshd.init /etc/init.d/sshd
	chmod +x /etc/init.d/sshd
	chkconfig --add sshd

fi
#A generic PAM configuration is included as "contrib/sshd.pam.generic",
#you may need to edit it before using it on your system.

cd ..
cp sshd.pam.generic /etc/pam.d/sshd
sed -i 's/\/lib\/security\///g' /etc/pam.d/sshd

# Modify /etc/ssh/sshd_config
# Backup /etc/ssh/sshd_config
cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config_bak

# The default set of ciphers and MACs has been altered to
# remove unsafe algorithms. In particular, CBC ciphers and arcfour*
# are disabled by default. 
# Changes since OpenSSH 6.6
echo "KexAlgorithms diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group1-sha1,[email protected]" >> /etc/ssh/sshd_config
echo "Ciphers aes128-cbc,aes192-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr,3des-cbc,arcfour128,arcfour256,arcfour,blowfish-cbc,cast128-cbc" >> /etc/ssh/sshd_config
echo "MACs hmac-md5,hmac-sha1,[email protected],hmac-ripemd160,hmac-sha1-96,hmac-md5-96" >> /etc/ssh/sshd_config

# Disable root access via ssh to server
#* The default for the sshd_config(5) PermitRootLogin option has changed from "yes" to "prohibit-password".
#* PermitRootLogin=without-password/prohibit-password now bans all
#interactive authentication methods, allowing only public-key,
#hostbased and GSSAPI authentication (previously it permitted
#keyboard-interactive and password-less authentication if those
#were enabled).
#PermitRootLogin prohibit-password is the default since version 7.0p1

sed -i 's/^#PermitRootLogin/PermitRootLogin/' /etc/ssh/sshd_config
#sed -i '/PermitRootLogin/s/yes/no/' /etc/ssh/sshd_config
sed -i '/PermitRootLogin/s/prohibit-password/no/' /etc/ssh/sshd_config

# Set 'UsePAM no' to 'UsePAM yes' to enable PAM authentication, account processing, 
# and session processing
sed -i '/^#UsePAM no/a UsePAM yes' /etc/ssh/sshd_config

# Start sshd process
service sshd start

# Disable telnet service
if netstat -tnlp | grep -w 22 &> /dev/null; then

sed -i '/disable/s/no/yes/' /etc/xinetd.d/telnet

service xinetd restart

fi

echo "#########################################################"
echo "################                        #################"
echo "################ openssh install sucess  ################"
echo "################                        #################"
echo "#########################################################"

echo "###############   ssh version     ################################################# "
echo "################################################################################### "
sshd -v
echo "#################################################################################### "
echo "#################################################################################### "

좋은 웹페이지 즐겨찾기