#!/usr/bin/env bash
# == Magento Security Scanner - Vanilla false flag tester
#
# This script can be used to test for false flags against vanilla Magento code
#
# = How To
#
# 1. use the 'init' param to download and extract Magento packages
# 2. use the 'run' param to run the YARA tests against these packages
#
# = Expectation
#
# - There should be "no output" from the run command as these packages are considered to be clean code

# Defining fancy Colors
RED="\033[0;31m"
YELLOW="\033[0;33m"
GREEN="\033[0;32m"
NONE="\033[0m"

# Variables
CMD=$1
ARG=$2
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
PROJECTROOT=$(cd "${DIR}/.." && pwd)
vanilladir=$(dirname "$PROJECTROOT/tmp/vanilla/.")

# == Vanilla versions (https://github.com/OpenMage/magento-mirror/archive/$version)
# You can change these to add or remove versions, see the release archive
versions=( "1.9.3.8" "1.9.2.4" "1.7.0.2" )

command_found=0
argument_found=0

# === Functions ====

# == Prints help text
function _print_help() {
    echo "==============================================="
    echo -e "======----  ${GREEN}Magento Security Scanner${NONE}  ----====="
    echo "==============================================="
    echo ""
    echo "This script helps to setup and test your rules for false flags against vanilla Magento code."
    echo ""
    echo -e "${YELLOW}How To:"
    echo ""
    echo -e "${GREEN}1. ${NONE}Use the ${YELLOW}init ${NONE}command to download and extract vanilla versions."
    echo -e "${GREEN}2. ${NONE}Use the ${YELLOW}run ${NONE}command to check for false flags."
    echo ""
    echo -e "${YELLOW}Usage${NONE}"
    echo " command [arguments]"
    echo ""
    echo -e "${YELLOW}Commands:${NONE}"
    echo -e "${GREEN}help${NONE}            Print this help text."
    echo -e "${GREEN}init${NONE}            Downloads and extracts several vanilla Magento packages."
    echo -e "${GREEN}run${NONE}             runs YARA against vanilla Magento packages."
    echo -e "${YELLOW}-----------------------------------------------------------------------------------"
    echo -e "${GREEN}test${NONE}            Shortcut to run the malware fingerprint / sample Pyton tests."
    echo ""
}

# == Init function
# - Creates directories
# - Downloads and extracts magento packages
function _init(){
if [ ! -d "$vanilladir" ]; then
    echo -e "${GREEN}creating $YELLOW $vanilladir $NONE"
    mkdir -p $vanilladir
fi

echo -e "${GREEN}Vanilla packages will be extracted to: $YELLOW $vanilladir $NONE"

for version in "${versions[@]}"
do
    echo -e "${GREEN}Downloading Magento ${YELLOW}$version${NONE}"
    cd $vanilladir && curl -LOk  "https://github.com/OpenMage/magento-mirror/archive/${version}.zip"

    if [ 0 -eq $?  ]; then
        echo -e "${GREEN}Extracting Magento ${YELLOW}$version${NONE}"
        unzip -qq "${version}.zip"
        count=$(find "$vanilladir" -type f  | wc -l )
        echo -e "${GREEN}Extracted ${count} files"
    else
        echo -e "${RED}Download failed! $version"
    fi;
done

echo -e "${GREEN}Done fetching Magento packages, you can now check against this code using ${YELLOW}run."

}


# == Runs all YARA tests against vanilla Magento packages
_runyara(){
if [ ! -d "$vanilladir" ]; then
    echo -e "${RED}No vanilla packages found at $YELLOW $vanilladir $NONE"
    echo -e "${GREEN}Run the init command first, "
    exit
fi

echo -e "${GREEN}Running YARA tests on vanilla code, ${YELLOW}should return nothing. $RED"
cd ${PROJECTROOT} && yara -r ./build/mwscan.yar ./tmp/vanilla
}


# == Shortcut to run Python fingerprint / sample tests
_runtests(){
cd $DIR && python ./runtests.py
}


# === Processing Input ===

# Initialise input
if [ ! -z "$CMD" ]; then
command_found=1
    if [ ! -z "$ARG" ]; then
        argument_found=1
    fi
fi

# When there's no argument or the argument = help, output help screen and exit
if [ $command_found -eq 0 ] || [ $CMD == "help" ]; then
    _print_help
    exit 0
fi

# = Mapping arguments to functions

if [ $CMD == "init" ]; then
_init
fi

if [ $CMD == "run" ]; then
_runyara
fi

if [ $CMD == "runtests" ]; then
_runtests
fi
