# 70-aide
# Setup of AIDE - Advanced Intrusion Detection Environment

AIDE_setup() {
    SECFILE=/etc/slossec.conf
    [ -f "$SECFILE" ] && source ${SECFILE} 
    if [[ $aide -eq 1 ]] ; then
        log "SLOSSecurity: AIDE Setup"
        $DIALOG --backtitle "$BT" --title "AIDE" --infobox "Advanced Intrusion Detection ENV Setup \nRemediation Rules" $H1 $W1
        sleep $autoTO
    else
        log "SLOSSecurity: AIDE Setup not performed"
        return
    fi
    if ! rpm -q --quiet aide ; then
        yum -y install aide cronie || rc=1
        if [ $? -ne 0 ] ; then
            log "SLOSSecurity: Failed to install aide, cron pkg's"
            return
        fi
    fi 

    /usr/sbin/aide --init
    /usr/bin/cp -p /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz
    
    CRONTAB=/etc/crontab
    CRONDIRS='/etc/cron.d /etc/cron.daily /etc/cron.weekly /etc/cron.monthly'

    if [ -f /var/spool/cron/root ]; then
    	VARSPOOL=/var/spool/cron/root
    fi

    if ! /usr/bin/grep -qR '^.*\/usr\/sbin\/aide\s*\-\-check.*' $CRONTAB $VARSPOOL $CRONDIRS; then
        crontab -l > /tmp/mycron
        /usr/bin/echo '0 5 * * * /usr/sbin/aide --check | /bin/mail -s "$(hostname) - AIDE Integrity Check" root@localhost' >> /tmp/mycron
        crontab /tmp/mycron
    fi
    log "Remediating rule 224/243: 'xccdf_org.ssgproject.content_rule_aide_verify_acls'"
    
    aide_conf="/etc/aide.conf"
    
    groups=$(/usr/bin/grep "^[A-Z]\+" $aide_conf | /usr/bin/grep -v "^ALLXTRAHASHES" | /usr/bin/cut -f1 -d '=' | /usr/bin/tr -d ' ' | /usr/bin/sort -u)
    
    for group in $groups
    do
        config=$(/usr/bin/grep "^$group\s*=" $aide_conf | /usr/bin/cut -f2 -d '=' | /usr/bin/tr -d ' ')
    
        if ! [[ $config = *acl* ]]
        then
            if [[ -z $config ]]
            then
                config="acl"
            else
                config=$config"+acl"
            fi
        fi
        /usr/bin/sed -i "s/^$group\s*=.*/$group = $config/g" $aide_conf
    done
    log "Remediating rule 223/243: 'xccdf_org.ssgproject.content_rule_aide_verify_ext_attributes'"
    
    aide_conf="/etc/aide.conf"
    
    groups=$(/usr/bin/grep "^[A-Z]\+" $aide_conf | /usr/bin/grep -v "^ALLXTRAHASHES" | /usr/bin/cut -f1 -d '=' | /usr/bin/tr -d ' ' | /usr/bin/sort -u)
    
    for group in $groups
    do
        config=$(/usr/bin/grep "^$group\s*=" $aide_conf | /usr/bin/cut -f2 -d '=' | /usr/bin/tr -d ' ')
    
        if ! [[ $config = *xattrs* ]]
        then
            if [[ -z $config ]]
            then
                config="xattrs"
            else
                config=$config"+xattrs"
            fi
        fi
        /usr/bin/sed -i "s/^$group\s*=.*/$group = $config/g" $aide_conf
    done
    
    ###############################################################################

(>&2 echo "Remediating rule 322/357: 'xccdf_org.ssgproject.content_rule_aide_use_fips_hashes'")


aide_conf="/etc/aide.conf"
forbidden_hashes=(sha1 rmd160 sha256 whirlpool tiger haval gost crc32)

groups=$(grep "^[A-Z]\+" $aide_conf | cut -f1 -d ' ' | tr -d ' ' | sort -u)

for group in $groups
do
    config=$(grep "^$group\s*=" $aide_conf | cut -f2 -d '=' | tr -d ' ')

    if ! [[ $config = *sha512* ]]
    then
        config=$config"+sha512"
    fi

    for hash in ${forbidden_hashes[@]}
    do
        config=$(echo $config | sed "s/$hash//")
    done

    config=$(echo $config | sed "s/^\+*//")
    config=$(echo $config | sed "s/\+\++/+/")
    config=$(echo $config | sed "s/\+$//")

    sed -i "s/^$group\s*=.*/$group = $config/g" $aide_conf
done

sed -i "s/aide=1/aide=0/g" $SECFILE 2>/dev/null
show "SLOSSecurity: AIDE Setup Completed"
}

AIDE_setup
