#!/bin/bash

# manage_library.sh by Tommie Giles (tommie.giles@layeredtech.com)
# this library (and the applicable script) will perform a number of standard tasks for IDS server deployments

restart_snort(){
    log_something "restarting snort"
    /etc/init.d/snort r
}

download_rules(){
    log_something "INFO downloading updated rules"

    rm -rf $HOME_DIR/scripts/new/rules.tar.gz
    wget https://phonehome.gsihosting.com/downloads/snort/rules.tar.gz -O $HOME_DIR/scripts/new/rules.tar.gz --no-check-certificate &> /dev/null

    if [ $? = 1 ]
    then
        log_something "CRIT download failed. quitting here."
        exit
    else
        download_hash=`md5sum $HOME_DIR/scripts/new/rules.tar.gz | awk ' { print $1 } '`
        log_something "INFO download successful. md5sum of download is $download_hash"

    fi
}

extract_rules(){
    log_something "INFO extracting downloaded rules"

    cd $HOME_DIR/scripts/new
    tar -zxvf rules.tar.gz &> /dev/null

    if [ $? = 2 ]
    then
        log_something "CRIT extraction failed. quitting here."
        exit
    else
        log_something "INFO extraction successful."
    fi

}

backup_rules(){
    zip -r $HOME_DIR/scripts/current/snort.`date +%N`.zip $HOME_DIR/rules/ &> /dev/null

    if [ $? != 0 ]
    then
        log_something "CRIT snort rule backup failed. quitting here."
        exit
    else
        log_something "INFO snort rule backup successful."
    fi

}

install_rules(){
    cp -f $HOME_DIR/scripts/new/rules/* $HOME_DIR/rules/ &> /dev/null

    if [ $? != 0 ]
    then
        log_something "CRIT new rule file copy failed. please roll back changes manually."
        exit
    else
        log_something "INFO new rule copy successful."
    fi
}

disabled_rules(){
    wget https://phonehome.gsihosting.com/downloads/snort/disabled_rules/disabled.txt -O $HOME_DIR/disabled.rules --no-check-certificate &> /dev/null

    if [ $? != 0 ]
    then
        log_something "CRIT disabled rules download failed. please roll back changes manually."
    else
        log_something "INFO disabled rules download successful."
    fi

}

insert_default_rules(){
    log_something "INFO inserting default rules and changes in..."
    echo "alert icmp any any -> 206.55.126.215 any (msg:\"Snort ICMP Canary\"; classtype:not-suspicious; sid:10000001;)" >> $HOME_DIR/trons-rules/bad-unknown.rules
    cd $HOME_DIR/trons-rules; for i in `ls *.rules`; do sed -i /'WEB-CLIENT'/d $i; done
    cd $HOME_DIR/trons-rules; for i in `ls *.rules`; do sed -i /'WEB-MISC'/d $i; done
    cd $HOME_DIR/trons-rules; for i in `ls *.rules`; do sed -i /'Microsoft Windows 2000'/d $i; done
    cd $HOME_DIR/trons-rules; for i in `ls *.rules`; do sed -i /'WEB-ACTIVEX'/d $i; done
}

link_library(){
    log_something "INFO linking appropriate system libraries..."

    uname -a | grep 'x86_64'

if [ $? -eq 0 ]; then
    log_something "INFO I'm 64-bit. linking libraries."

    ln -s $HOME_DIR/lib/libdnet.1 /lib64/
    ln -s $HOME_DIR/lib/libsfbpf.so.0 /lib64/
    ln -s $HOME_DIR/lib/libpcap.so.1 /lib64/

else

    log_something "INFO I'm 32-bit. linking libraries"

    ln -s $HOME_DIR/lib/libdnet.1 /lib/
    ln -s $HOME_DIR/lib/libsfbpf.so.0 /lib/
    ln -s $HOME_DIR/lib/libpcap.so.1 /lib/
fi
}

rule_directory_cleanup(){
    log_something "INFO cleaning up temporary directories"
    rm -rf $HOME_DIR/tmp
    mkdir $HOME_DIR/tmp

    rm -rf $HOME_DIR/trons-rules
    mkdir $HOME_DIR/trons-rules

    rm -rf $HOME_DIR/tmp/ntest
}

rebuild_rules(){
    log_something "INFO compliling mother file..."
    # Set Internal Field Seperator
    IFS=$'\n'
    # Next, let's compile together one "mother file" from which we'll pull all the other items from. This will reside in /opt/snort/tmp/compiled
    cd $HOME_DIR/rules
    cat *.rules | uniq -u | grep -v "#" > $HOME_DIR/tmp/compiled

    ###
    log_something "INFO parsing whitelist..."
    # Now, let's parse the contents of the snort.whitelist file, search for those contents in the 'compiled file', and then remove those items from the files.
    IFS=$'\t\n'
    # I have no clue how this works. It's pretty, tho.
    cd $HOME_DIR
    IFS=$'\n'
    for i in `cat $HOME_DIR/disabled.rules | grep -v "#" | cut -d "\"" -f 2`; do
    IFS=$'\n'
    echo "sed -i /'$i'/d $HOME_DIR/tmp/compiled" >> $HOME_DIR/tmp/ntest
    done

    ###

    log_something "INFO running ntest..."
    bash $HOME_DIR/tmp/ntest

    ###

    log_something "INFO converting rules..."
    IFS=$'\t\n'

    # This bit does the following:
    # * pulls a list of all items in /etc/snort/classification.config and parses it until we just get a list of items to look for
    # * creates a bunch of blank files in the trons-rules/ directory (so I don't clobber anything while writing)
    # * takes the list of items generated and greps for them in the /etc/snort/compiled file.
    # * Performs a seek for any "any any -> any any" rules, and converts them into EXTERNAL -> HOME and copies them over.
    # * it excludes everything that is setup with a "HOME_NET.*EXTERNAL_NET" and dumps everything else into appropriate trons-rules rule
    # ** If it finds any items with *only* the "HOME_NET.*EXTERNAL_NET" variables swapped around, and then dumps them after fixing
    # * taadaa

    if [ ! -f $HOME_DIR/etc/classification.config ]; then
        log_something "CRIT $HOME_DIR/etc/classification.config does not exist. dying here"
        exit
    fi

    for i in `grep -v "#" $HOME_DIR/etc/classification.config | grep ":" | awk ' { print $3 }' | cut -d ',' -f1` ; do
        touch $HOME_DIR/trons-rules/$i.rules
        cd $HOME_DIR/rules; grep "any any -> any any" $HOME_DIR/tmp/compiled | sed -s 's/any any -> any any/$EXTERNAL_NET any -> $HOME_NET any/g' >> $HOME_DIR/trons-rules/$i.rules
        cd $HOME_DIR/rules; grep -h -w $i $HOME_DIR/tmp/compiled | grep -v HOME_NET.*EXTERNAL_NET >> $HOME_DIR/trons-rules/$i.rules
        cd $HOME_DIR/rules; grep -h -w $i $HOME_DIR/tmp/compiled | grep HOME_NET.*EXTERNAL_NET | sed 's/HOME_NET/GSI_HOME_NET/g' | sed 's/EXTERNAL_NET/GSI_EXTERNAL_NET/g'>> $HOME_DIR/trons-rules/$i.rules
        done

    ###

    log_something "INFO cleaning up duplicates..."
    cd $HOME_DIR/trons-rules
    for i in `ls *.rules`; do
        uniq $i > cooked.$i
        mv -f cooked.$i $i
    done

}

insert_flood_rules(){
    log_something "INFO inserting flood rules..."
    echo "alert icmp any any -> any any (msg:\"Snort ICMP Canary\"; classtype:not-suspicious; sid:10000002;)" >> $HOME_DIR/trons-rules/bad-unknown.rules
    echo "alert tcp any any -> any any (msg:\"Snort TCP Canary\"; classtype:not-suspicious; sid:10000003;)" >> $HOME_DIR/trons-rules/bad-unknown.rules
    echo "alert udp any any -> any any (msg:\"Snort UDP Canary\"; classtype:not-suspicious; sid:10000004;)" >> $HOME_DIR/trons-rules/bad-unknown.rules
    restart_snort

}

remove_flood_rules(){
    log_something "INFO removing flood rules..."
    sed -i '/10000002/d' $HOME_DIR/trons-rules/bad-unknown.rules
    sed -i '/10000003/d' $HOME_DIR/trons-rules/bad-unknown.rules
    sed -i '/10000004/d' $HOME_DIR/trons-rules/bad-unknown.rules
    restart_snort
}

enable_cc_monitor(){
    log_something "INFO enabling cc monitoring..."

    if [ ! -f $HOME_DIR/etc/.cc.snort.conf ]
    then
        echo "preprocessor sensitive_data: alert_threshold 1
        output alert_syslog: LOG_ALERT LOG_INFO" >> $HOME_DIR/etc/snort.conf
        touch $HOME_DIR/etc/.cc.snort.conf
        return
    else
        log_something "WARN cc monitoring already enabled in $HOME_DIR/etc/snort.conf. skipping..."
    fi

    ###
    log_something "INFO adding rulest in..."
    echo "alert tcp any any -> any any (msg:\"Unencrypted Credit Card number traffic detected\"; gid:138; sid:10000005; rev:1;  sd_pattern:2,credit_card;)" >> $HOME_DIR/trons-rules/bad-unknown.rules

    ###

    restart_snort
}

disable_cc_monitoring(){
    log_something "INFO disabling cc monitoring rule..."
    sed -i '/10000005/d' $HOME_DIR/trons-rules/bad-unknown.rules

}

#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#