Skip to content

Instantly share code, notes, and snippets.

@kugelblitzz
Forked from TheGU/upgrade_openssh_openssl.sh
Last active March 8, 2023 21:41
Show Gist options
  • Save kugelblitzz/ffe90d471fcfc2e0583553c07ec37b32 to your computer and use it in GitHub Desktop.
Save kugelblitzz/ffe90d471fcfc2e0583553c07ec37b32 to your computer and use it in GitHub Desktop.
Upgrade openssl and openssh to version 9.1 on Amazon Linux 2
# $OpenBSD: sshd_config,v 1.104 2021/07/02 05:11:21 dtucker Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
#PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
PubkeyAuthentication yes
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys
#AuthorizedPrincipalsFile none
#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no
#PermitEmptyPasswords no
# Change to no to disable s/key passwords
#KbdInteractiveAuthentication yes
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the KbdInteractiveAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via KbdInteractiveAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and KbdInteractiveAuthentication to 'no'.
#UsePAM no
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
# no default banner path
#Banner none
# override default of no subsystems
Subsystem sftp /usr/libexec/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
#!/bin/bash
#Install Pre-Requisits
sudo yum install gcc -y
sudo yum install zlib-devel -y
sudo yum install mlocate -y
sudo yum install autoconf -y
# Prepare folder
mkdir -p /app/ssh_upgrade && cd /app/ssh_upgrade
# Get openssl source file : openssl-1.0.2q.tar.gz
wget https://www.openssl.org/source/openssl-1.0.2q.tar.gz
# Get openssh source file : openssh-9.1p1.tar.gz
wget https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-9.1p1.tar.gz
timestamp=$(date +%s)
# Upgrade OpenSSL ================
# Backup old file
cp /usr/lib64/libcrypto.so.10 /usr/lib64/libcrypto.so.10-${timestamp}
cp /usr/lib64/libssl.so.10 /usr/lib64/libssl.so.10-${timestamp}
mv /usr/bin/openssl /usr/bin/openssl-${timestamp}
mv /usr/include/openssl /usr/include/openssl-${timestamp}
mv /usr/lib64/openssl/engines /usr/lib64/openssl/engines-${timestamp}
mv /usr/lib64/openssl /usr/lib64/openssl-${timestamp}
# Remove OpenSSL rpm package
rpm -qa | grep openssl |xargs -i rpm -e --nodeps {}
# Compile and install new OpenSSL
cd /app/ssh_upgrade
tar zxvf openssl-1.0.2q.tar.gz && cd openssl-1.0.2q
./config --prefix=/usr/local/openssl --openssldir=/etc/ssl --shared zlib && make && make test && make install
# Link binary file
ln -sf /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -sf /usr/local/openssl/include/openssl /usr/include/openssl
# Set ld.sd to recognize openssl lib folder
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf.d/openssl.conf
chmod 655 /etc/ld.so.conf.d/openssl.conf
ldconfig
echo "Check if folder already in ld list"
ldconfig -v | grep /usr/local/openssl/lib
# if no result it may need to manually copy to /usr/lib64 folder
# /bin/cp /usr/local/openssl/lib/* /usr/lib64/
# mv /usr/lib64/libcrypto.so.10-* /usr/lib64/libcrypto.so.10
# mv /usr/lib64/libssl.so.10-* /usr/lib64/libssl.so.10
echo "OpenSSl version upgrades :" && openssl version -a
# Upgrade OpenSSH ========================
# Backup old file
cp -R /etc/ssh /etc/ssh-${timestamp}
cp /etc/init.d/sshd /etc/init.d/sshd-${timestamp}
# Remove OpenSSH rpm package
rpm -qa | grep openssh
rpm -e --nodeps `rpm -qa | grep openssh`
# Compile and install new OpenSSH
cd /app/ssh_upgrade
tar zxvf openssh-9.1p1.tar.gz && cd openssh-9.1p1
./configure --prefix=/usr/local/openssh --sysconfdir=/etc/ssh --with-ssl-dir=/usr/local/openssl && make && make install
# chmod -R 755 /usr/local/openssh
chmod -R 400 /etc/ssh/
# link binary file
ln -sf /usr/local/openssh/sbin/sshd /usr/sbin/sshd
ln -sf /usr/local/openssh/bin/scp /usr/bin/scp
ln -sf /usr/local/openssh/bin/sftp /usr/bin/sftp
ln -sf /usr/local/openssh/bin/ssh /usr/bin/ssh
ln -sf /usr/local/openssh/bin/ssh-add /usr/bin/ssh-add
ln -sf /usr/local/openssh/bin/ssh-agent /usr/bin/ssh-agent
ln -sf /usr/local/openssh/bin/ssh-keygen /usr/bin/ssh-keygen
ln -sf /usr/local/openssh/bin/ssh-keyscan /usr/bin/ssh-keyscan
ln -sf /usr/local/openssh/libexec/sftp-server /usr/libexec/sftp-server
ln -sf /usr/local/openssh/libexec/ssh-keysign /usr/libexec/ssh-keysign
ln -sf /usr/local/openssh/libexec/ssh-pkcs11-helper /usr/libexec/ssh-pkcs11-helper
# don't know how to copy man page in /usr/local/openssh/share folder
# Copy default configuration file
# Check the different from file in /etc/ssh-${timestamp} to see what need to hardenning
/bin/cp ssh_config /etc/ssh/
/bin/wget https://gist.githubusercontent.com/kugelblitzz/ffe90d471fcfc2e0583553c07ec37b32/raw/0f998bf00699ab22c84d2a6b95dd97eacdbf8799/sshd_config -P /tmp/
/bin/cp /tmp/sshd_config /etc/ssh/
#/bin/cp sshd_config /etc/ssh/
/bin/cp moduli /etc/ssh/
#Harden File Permissions
chmod -R 400 /etc/ssh/
# Test config, this will fail if sshd_config files is wrong or has unsupport setting from previous version
# Run after copy config from hardenning and delete line that show in error
/usr/sbin/sshd -t -f /etc/ssh/sshd_config
# Set startup script in /etc/init.d
/bin/cp contrib/redhat/sshd.init /etc/init.d/sshd
chmod +x /etc/init.d/sshd
# Set start service on boot
chkconfig --add sshd
chkconfig sshd on
chkconfig sshd --list
# Start service
service sshd start
echo "OpenSSH version upgrades :" && ssh -V
usermod -p '*' ec2-user
systemctl restart sshd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment