Learn about Centmin Mod LEMP Stack today
Register Now

CentOS 7.x Security OpenSSH Terrapin Attack and Mitigation for CentOS 7 OpenSSH 7.4p1

Discussion in 'CentOS, Redhat & Oracle Linux News' started by eva2000, Apr 24, 2024.

  1. eva2000

    eva2000 Administrator Staff Member

    51,987
    11,976
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,473
    Local Time:
    12:44 AM
    Nginx 1.25.x
    MariaDB 10.x

    OpenSSH Terrapin Attack Background




    OpenSSH is a widely used secure communication protocol that allows remote access to servers and other network devices. It encrypts the data transmitted between the client and the server to ensure confidentiality and integrity. However, a security vulnerability called the Terrapin attack has been discovered in the OpenSSH protocol, which could potentially allow attackers to gain unauthorized access to encrypted communication.

    The Terrapin attack exploits weaknesses in certain encryption algorithms and message authentication codes (MACs) used by OpenSSH. By intercepting and altering the initial handshake between the client and the server, an attacker can gradually leak information about the encrypted session. Over time, this leaked information can be used to decrypt the communication and gain access to sensitive data.

    CentOS 7, a popular Linux distribution, uses an older version of OpenSSH 7.4p1 by default. While newer versions of OpenSSH have built-in mitigations against the Terrapin attack, the OpenSSH version in CentOS 7 remains vulnerable. To address this issue, the CentOS/Redhat community has provided a set of recommended configuration changes that can help mitigate the risk of the Terrapin attack. EL8/EL9 operating systems like AlmaLinux/Rocky Linux 8 and 9 have backported OpenSSH 9.6 Terrapin attack mitigations to EL8 OpenSSH 8.0p1 and EL9 OpenSSH 8.7p1. But Redhat upstream decided the security risk is low so did not backport fix to EL7 - Redhat Enterprise Linux 7 or CentOS 7 and instead listed a mitigation workaround at cve-details and 2254210 – (CVE-2023-48795) CVE-2023-48795 ssh: Prefix truncation attack on Binary Packet Protocol (BPP)

    The mitigation involves updating the OpenSSH configuration files (/etc/ssh/ssh_config and /etc/ssh/sshd_config) with a stricter set of encryption algorithms and MACs. These recommended settings prioritize the use of secure algorithms and disable the vulnerable ones, making it much harder for attackers to exploit the Terrapin vulnerability.

    To make the mitigation process easier, I wrote a shell script named terrapin-openssh-el7-fix.sh for CentOS 7 EL7 users tested on Centmin Mod LEMP stacks. Centmin Mod 130.00beta01 users can automatically apply and run this script by running cmupdate command and running centmin.sh menu once and exit. The script is at /usr/local/src/centminmod/tools/terrapin-openssh-el7-fix.sh.

    This script automates the process of updating the OpenSSH configuration files with the recommended settings. It creates a backup of the original configuration files, removes any existing vulnerable settings, and appends the secure settings to the files. The script also provides both a silent and a verbose output mode to keep users informed about the changes being made and restarts the OpenSSH service to apply the new configuration.

    By running the terrapin-openssh-el7-fix.sh, CentOS 7 users can quickly and easily implement the recommended mitigations against the Terrapin attack. The script ensures that the OpenSSH configuration is updated with the secure encryption algorithms and MACs, reducing the risk of unauthorized access through this vulnerability.

    It's important to note that while the script helps mitigate the Terrapin attack, it's not a complete solution. The effectiveness of the mitigation depends on both the client and the server supporting the secure configuration. Therefore, it's crucial to ensure that all devices communicating via OpenSSH are updated with the recommended settings.

    In summary, the Terrapin attack is a vulnerability in the OpenSSH protocol that affects CentOS 7 system's OpenSSH 7.4 but does not affect EL8/EL9 systems OpenSSH 8.0p1/8.7p1 which have backported fixes. To mitigate the risk, users can run the terrapin-openssh-el7-fix.sh script, which automatically updates the OpenSSH configuration with secure settings. By applying these mitigations, CentOS 7 users can enhance the security of their OpenSSH communication and protect against potential unauthorized access through the Terrapin vulnerability.

    The shell script used to update CentOS 7 EL7 OpenSSH 7.4 with mitigations outlined at cve-details. Centmin Mod 130.00beta01 users can automatically apply and run this script by running cmupdate command and running centmin.sh menu once and exit. The script is at /usr/local/src/centminmod/tools/terrapin-openssh-el7-fix.sh.

    Code (Text):
    #!/bin/bash
    
    DT=$(date +"%d%m%y-%H%M%S")
    CENTMINLOGDIR='/root/centminlogs'
    LOGFILE="${CENTMINLOGDIR}/openssh_terrapin_mitigation_${DT}.log"
    
    # Define the configuration lines to be added
    CIPHERS_LINE="Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com"
    MACS_LINE="MACs umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512"
    
    CENTOSVER=$(awk '{ print $3 }' /etc/redhat-release)
    
    if [ "$CENTOSVER" == 'release' ]; then
        CENTOSVER=$(awk '{ print $4 }' /etc/redhat-release | cut -d . -f1,2)
        if [[ "$(cat /etc/redhat-release | awk '{ print $4 }' | cut -d . -f1)" = '7' ]]; then
            CENTOS_SEVEN='7'
        elif [[ "$(cat /etc/redhat-release | awk '{ print $4 }' | cut -d . -f1)" = '8' ]]; then
            CENTOS_EIGHT='8'
        elif [[ "$(cat /etc/redhat-release | awk '{ print $4 }' | cut -d . -f1)" = '9' ]]; then
            CENTOS_NINE='9'
        fi
    fi
    
    if [[ "$(cat /etc/redhat-release | awk '{ print $3 }' | cut -d . -f1)" = '6' ]]; then
        CENTOS_SIX='6'
    fi
    
    # Check for Redhat Enterprise Linux 7.x
    if [ "$CENTOSVER" == 'Enterprise' ]; then
        CENTOSVER=$(awk '{ print $7 }' /etc/redhat-release)
        if [[ "$(awk '{ print $1,$2 }' /etc/redhat-release)" = 'Red Hat' && "$(awk '{ print $7 }' /etc/redhat-release | cut -d . -f1)" = '7' ]]; then
            CENTOS_SEVEN='7'
            REDHAT_SEVEN='y'
        fi
    fi
    
    if [[ -f /etc/system-release && "$(awk '{print $1,$2,$3}' /etc/system-release)" = 'Amazon Linux AMI' ]]; then
        CENTOS_SIX='6'
    fi
    
    # ensure only el8+ OS versions are being looked at for alma linux, rocky linux
    # oracle linux, vzlinux, circle linux, navy linux, euro linux
    EL_VERID=$(awk -F '=' '/VERSION_ID/ {print $2}' /etc/os-release | sed -e 's|"||g' | cut -d . -f1)
    if [ -f /etc/almalinux-release ] && [[ "$EL_VERID" -eq 8 || "$EL_VERID" -eq 9 ]]; then
      CENTOSVER=$(awk '{ print $3 }' /etc/almalinux-release | cut -d . -f1,2)
      if [[ "$(echo $CENTOSVER | cut -d . -f1)" -eq '8' ]]; then
        CENTOS_EIGHT='8'
        ALMALINUX_EIGHT='8'
      elif [[ "$(echo $CENTOSVER | cut -d . -f1)" -eq '9' ]]; then
        CENTOS_NINE='9'
        ALMALINUX_NINE='9'
      fi
    elif [ -f /etc/rocky-release ] && [[ "$EL_VERID" -eq 8 || "$EL_VERID" -eq 9 ]]; then
      CENTOSVER=$(awk '{ print $4 }' /etc/rocky-release | cut -d . -f1,2)
      if [[ "$(echo $CENTOSVER | cut -d . -f1)" -eq '8' ]]; then
        CENTOS_EIGHT='8'
        ROCKYLINUX_EIGHT='8'
      elif [[ "$(echo $CENTOSVER | cut -d . -f1)" -eq '9' ]]; then
        CENTOS_NINE='9'
        ROCKYLINUX_NINE='9'
      fi
    elif [ -f /etc/oracle-release ] && [[ "$EL_VERID" -eq 8 || "$EL_VERID" -eq 9 ]]; then
      CENTOSVER=$(awk '{ print $5 }' /etc/oracle-release | cut -d . -f1,2)
      if [[ "$(echo $CENTOSVER | cut -d . -f1)" -eq '8' ]]; then
        CENTOS_EIGHT='8'
        ORACLELINUX_EIGHT='8'
      elif [[ "$(echo $CENTOSVER | cut -d . -f1)" -eq '9' ]]; then
        CENTOS_NINE='9'
        ORACLELINUX_NINE='9'
      fi
    elif [ -f /etc/vzlinux-release ] && [[ "$EL_VERID" -eq 8 || "$EL_VERID" -eq 9 ]]; then
      CENTOSVER=$(awk '{ print $4 }' /etc/vzlinux-release | cut -d . -f1,2)
      if [[ "$(echo $CENTOSVER | cut -d . -f1)" -eq '8' ]]; then
        CENTOS_EIGHT='8'
        VZLINUX_EIGHT='8'
      elif [[ "$(echo $CENTOSVER | cut -d . -f1)" -eq '9' ]]; then
        CENTOS_NINE='9'
        VZLINUX_NINE='9'
      fi
    elif [ -f /etc/circle-release ] && [[ "$EL_VERID" -eq 8 || "$EL_VERID" -eq 9 ]]; then
      CENTOSVER=$(awk '{ print $4 }' /etc/circle-release | cut -d . -f1,2)
      if [[ "$(echo $CENTOSVER | cut -d . -f1)" -eq '8' ]]; then
        CENTOS_EIGHT='8'
        CIRCLELINUX_EIGHT='8'
      elif [[ "$(echo $CENTOSVER | cut -d . -f1)" -eq '9' ]]; then
        CENTOS_NINE='9'
        CIRCLELINUX_NINE='9'
      fi
    elif [ -f /etc/navylinux-release ] && [[ "$EL_VERID" -eq 8 || "$EL_VERID" -eq 9 ]]; then
      CENTOSVER=$(awk '{ print $5 }' /etc/navylinux-release | cut -d . -f1,2)
      if [[ "$(echo $CENTOSVER | cut -d . -f1)" -eq '8' ]]; then
        CENTOS_EIGHT='8'
        NAVYLINUX_EIGHT='8'
      elif [[ "$(echo $CENTOSVER | cut -d . -f1)" -eq '9' ]]; then
        CENTOS_NINE='9'
        NAVYLINUX_NINE='9'
      fi
    elif [ -f /etc/el-release ] && [[ "$EL_VERID" -eq 8 || "$EL_VERID" -eq 9 ]]; then
      CENTOSVER=$(awk '{ print $3 }' /etc/el-release | cut -d . -f1,2)
      if [[ "$(echo $CENTOSVER | cut -d . -f1)" -eq '8' ]]; then
        CENTOS_EIGHT='8'
        EUROLINUX_EIGHT='8'
      elif [[ "$(echo $CENTOSVER | cut -d . -f1)" -eq '9' ]]; then
        CENTOS_NINE='9'
        EUROLINUX_NINE='9'
      fi
    fi
    
    if [[ "$CENTOS_SEVEN" -ne '7' ]]; then
        echo "script is intended for CentOS 7 (EL7) systems only"
        exit 1
    fi
    
    # Function to handle logging based on verbose mode
    log() {
        local message=$1
        if [ "$VERBOSE" = true ]; then
            echo "$message"
        fi
        echo "$message" >> "$LOGFILE"
    }
    
    # Function to handle backup and output based on verbose mode
    backup() {
        log "Backup files"
        log "cp -a /etc/ssh/sshd_config \"/etc/ssh/sshd_config-b4-terrapin-fix-$DT\""
        log "cp -a /etc/ssh/ssh_config \"/etc/ssh/ssh_config-b4-terrapin-fix-$DT\""
        cp -a /etc/ssh/sshd_config "/etc/ssh/sshd_config-b4-terrapin-fix-$DT"
        cp -a /etc/ssh/ssh_config "/etc/ssh/ssh_config-b4-terrapin-fix-$DT"
    }
    
    # Function to remove existing configuration lines and provide output based on verbose mode
    remove_existing_config() {
        local file=$1
        local setting=$2
        if grep -q "^$setting" "$file"; then
            log "Removing existing $setting from $file:"
            log "$(grep "^$setting" "$file")"
            log "Removed $setting from $file"
            sed -i "/^$setting/d" "$file"
        fi
    }
    
    # Function to append configuration if not already present and provide output based on verbose mode
    append_config() {
        local file=$1
        local setting=$2
        local line=$3
        if ! grep -q "^$setting" "$file"; then
            echo "$line" >> "$file"
            log "Added $setting to $file"
        else
            log "$setting already set in $file"
        fi
    }
    
    # Function to display the Ciphers and MACs settings from the file based on verbose mode
    display_config() {
        local file=$1
        log "Current Ciphers and MACs in $file:"
        log "$(grep -E '^(Ciphers|MACs)' "$file")"
    }
    
    # Parse command line arguments
    VERBOSE=true
    while getopts ":vs" opt; do
        case $opt in
            v)
                VERBOSE=true
                ;;
            s)
                VERBOSE=false
                ;;
            \?)
                echo "Invalid option: -$OPTARG" >&2
                exit 1
                ;;
        esac
    done
    
    # Create the log directory if it doesn't exist
    mkdir -p "$CENTMINLOGDIR"
    
    # Files to update
    SSH_CONFIG="/etc/ssh/ssh_config"
    SSHD_CONFIG="/etc/ssh/sshd_config"
    
    # Backup configs
    backup
    
    # Remove existing Ciphers and MACs from ssh_config
    remove_existing_config $SSH_CONFIG "Ciphers"
    remove_existing_config $SSH_CONFIG "MACs"
    
    # Remove existing Ciphers and MACs from sshd_config
    remove_existing_config $SSHD_CONFIG "Ciphers"
    remove_existing_config $SSHD_CONFIG "MACs"
    
    # Append Ciphers and MACs to ssh_config
    append_config $SSH_CONFIG "Ciphers" "$CIPHERS_LINE"
    append_config $SSH_CONFIG "MACs" "$MACS_LINE"
    
    # Append Ciphers and MACs to sshd_config
    append_config $SSHD_CONFIG "Ciphers" "$CIPHERS_LINE"
    append_config $SSHD_CONFIG "MACs" "$MACS_LINE"
    
    # Display the updated configurations based on verbose mode
    display_config $SSH_CONFIG
    display_config $SSHD_CONFIG
    
    # Restart SSHD to apply changes
    log "Restarting SSHD service..."
    sudo systemctl restart sshd
    
    if [ "$VERBOSE" = false ]; then
        echo "OpenSSH Terrapin Mitigation Applied"
        echo "https://community.centminmod.com/threads/25045/"
        echo "log file saved: $LOGFILE"
    else
        echo "SSHD service restarted."
    fi
    


    Running terrapin-openssh-el7-fix.sh script

    Code (Text):
    ./terrapin-openssh-el7-fix.sh
    Backup files
    cp -a /etc/ssh/sshd_config "/etc/ssh/sshd_config-b4-terrapin-fix-230424-215934"
    cp -a /etc/ssh/ssh_config "/etc/ssh/ssh_config-b4-terrapin-fix-230424-215934"
    Removing existing Ciphers from /etc/ssh/ssh_config:
    Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
    Removed Ciphers from /etc/ssh/ssh_config
    Removing existing MACs from /etc/ssh/ssh_config:
    MACs umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512
    Removed MACs from /etc/ssh/ssh_config
    Removing existing Ciphers from /etc/ssh/sshd_config:
    Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
    Removed Ciphers from /etc/ssh/sshd_config
    Removing existing MACs from /etc/ssh/sshd_config:
    MACs umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512
    Removed MACs from /etc/ssh/sshd_config
    Added Ciphers to /etc/ssh/ssh_config
    Added MACs to /etc/ssh/ssh_config
    Added Ciphers to /etc/ssh/sshd_config
    Added MACs to /etc/ssh/sshd_config
    Current Ciphers and MACs in /etc/ssh/ssh_config:
    Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
    MACs umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512
    Current Ciphers and MACs in /etc/ssh/sshd_config:
    Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
    MACs umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512
    Restarting SSHD service...
    SSHD service restarted.


    or silent mode which is what Centmin Mod 130.00beta01 users will get when they run cmupdate and run and exit centmin.sh menu once.

    Code (Text):
    ./terrapin-openssh-el7-fix.sh -s
    
    OpenSSH Terrapin Mitigation Applied
    https://community.centminmod.com/threads/25045/
    log file saved: /root/centminlogs/openssh_terrapin_mitigation_230424-233336.log
    


    Once properly tested in Centmin Mod 130.00beta01, will backport this script and routine to Centmin Mod 124.00stable.
     
  2. eva2000

    eva2000 Administrator Staff Member

    51,987
    11,976
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,473
    Local Time:
    12:44 AM
    Nginx 1.25.x
    MariaDB 10.x
    Terrapin scanner GitHub - RUB-NDS/Terrapin-Scanner test for CentOS 7's OpenSSH 7.4p1

    Code (Text):
    wget https://github.com/RUB-NDS/Terrapin-Scanner/releases/download/v1.1.3/Terrapin_Scanner_Linux_amd64 -O Terrapin-Scanner
    chmod +x Terrapin-Scanner
    


    After fix test shows NOT VULNERABLE as chacha20 cipher is disabled

    Code (Text):
    ./Terrapin-Scanner --connect localhost:22
    ================================================================================
    ==================================== Report ====================================
    ================================================================================
    
    Remote Banner: SSH-2.0-OpenSSH_7.4
    
    ChaCha20-Poly1305 support:   false
    CBC-EtM support:             false
    
    Strict key exchange support: false
    
    The scanned peer supports Terrapin mitigations and can establish
    connections that are NOT VULNERABLE to Terrapin. Glad to see this.
    For strict key exchange to take effect, both peers must support it.
    
    Note: This tool is provided as is, with no warranty whatsoever. It determines
          the vulnerability of a peer by checking the supported algorithms and
          support for strict key exchange. It may falsely claim a peer to be
          vulnerable if the vendor supports countermeasures other than strict key
          exchange.
    
    For more details visit our website available at https://terrapin-attack.com
     
  3. eva2000

    eva2000 Administrator Staff Member

    51,987
    11,976
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,473
    Local Time:
    12:44 AM
    Nginx 1.25.x
    MariaDB 10.x

    Man In The Middle Requirements



    Note that Terrapin attack requires the Man In The Middle (MitM) position to carry out the attack, so less likely to occur. Explained below

    Imagine you're writing a letter to a friend. You put the letter in an envelope, seal it, and give it to a messenger to deliver to your friend. Normally, the messenger would take the sealed envelope and deliver it directly to your friend without opening it.

    Now, imagine that the messenger is not trustworthy. Instead of delivering the letter directly to your friend, the messenger takes the letter to a secret location, carefully opens the envelope, and reads the contents of your letter. The messenger then writes a new letter, pretending to be you, and seals it in a new envelope. The messenger delivers this new letter to your friend, who believes it came directly from you.

    In this analogy, the messenger is like an attacker performing a man-in-the-middle (MitM) attack. Just as the messenger intercepts and alters the communication between you and your friend, an attacker in a MitM position can intercept and manipulate the communication between two parties without them realizing it.

    In the context of the Terrapin vulnerability (CVE-2023-48795), the attacker needs to be in a position to perform a MitM attack, such as being on the same local network as the targeted systems. By intercepting the communication between the two parties, the attacker can exploit weaknesses in the encryption and authentication mechanisms used by the SSH protocol.

    The attacker can then steal information, impersonate one of the communicating parties, or even inject malicious commands or data into the communication channel. This allows the attacker to compromise the confidentiality, integrity, and authenticity of the communication.

    To protect against MitM attacks, it's important to use secure communication protocols, strong encryption, and proper authentication mechanisms. In the case of the Terrapin vulnerability, applying the recommended mitigations, such as using strict key exchange and secure encryption algorithms, helps prevent attackers from successfully exploiting the vulnerability even if they are in a MitM position.
     
  4. Jon Snow

    Jon Snow Active Member

    738
    150
    43
    Jun 30, 2017
    Ratings:
    +215
    Local Time:
    11:44 AM
    Nginx 1.13.9
    MariaDB 10.1.31
    Does updating centmin code, opening the centmin menu and closing it patch this like CVE-2024-2961?

    I saw this when updating centmin:
    Code (Text):
    OpenSSH Terrapin Mitigation Applied

    I'm still on Centmin Mod 124.00stable
     
  5. eva2000

    eva2000 Administrator Staff Member

    51,987
    11,976
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,473
    Local Time:
    12:44 AM
    Nginx 1.25.x
    MariaDB 10.x
    Yes it does just silent for that one