예제 #1
0
 def __init__(self):
     """
     Constructor
     """
     if not self.is_init:
         MarvinExperiment.init()
     super(MarvinExperiment, self).__init__()
     self.model = liblinearutil.load_model(config[section]['model'])
     self.perm_cost, self.static_cost, self.dynamic_cost = 0.1, 1, 100
예제 #2
0
def init():
    global benign_pool, benign_pool_size, model
    print("Initializing...")
    random.seed(1)
    model = liblinearutil.load_model(model_name)

    # Load gene pool
    std_logger.debug("Loading gene pool...")
    with open(benign_pool_file, "r") as f:
        population_samples = f.readlines()
    for sample in population_samples:
        benign_pool.append(util.load_marvin(sample))
    benign_pool_size = len(benign_pool)
    std_logger.info("Loaded gene pool")

    print("Initialization complete")
예제 #3
0
def init():
    global benign_pool, benign_pool_size, model
    print("Initializing...")
    random.seed(1)
    model = liblinearutil.load_model(model_name)

    # Load gene pool
    std_logger.debug("Loading gene pool...")
    with open(benign_pool_file, "r") as f:
        population_samples = f.readlines()
    for sample in population_samples:
        benign_pool.append(util.load_record(sample))
    benign_pool_size = len(benign_pool)
    std_logger.info("Loaded gene pool")

    # Select intial generation
    # generation_indices = random.sample(benign_pool_size, sample_size)
    # for index in generation_indices:
    #     generation.append(benign_pool[int(index)])
    print("Initialization complete")
예제 #4
0
def mutate(model_file, malicious_file, evasive_output):
    (std_logger, feature_logger) = init()
    model = liblinearutil.load_model(model_file)
    
    with open("seeds/training_1.benign", "r") as f:
        benign = util.load_record(f.read())

    print("------------------------------")
    print("Total features: " + str(len(benign.features.keys())))
    print("------------------------------")
    benign_list = list(benign.features.keys())

    with open(malicious_file, "r") as f:
        malicious_samples = f.readlines()

    evasive_file = open(evasive_output, "w+")

    malicious_sample_size = len(malicious_samples)

    for sample_num, malicious_sample in enumerate(malicious_samples):

        malicious = util.load_seed(malicious_sample)
        malicious_orig = copy.deepcopy(malicious)

        best_probs = [1.0, 0.0]
        best_feat = -1

        std_logger.debug("------------------------------")
        # print("------------------------------")
        std_logger.info(malicious_orig.stringify())
        for i in range(15):

            mutation_list = []
            mutation_labels = []

            for feat in benign_list:
                malicious.features[feat] = 1

                mutation_list.append(malicious.features)
                mutation_labels.append(malicious.label)

                malicious = copy.deepcopy(malicious_orig)

            with nostdout():
                p_labs, p_acc, p_vals = liblinearutil.predict(mutation_labels, mutation_list, model, '-b 1')

            best_feat_index, best_probs = min(enumerate(p_vals), key=lambda vals: vals[1][1])
            best_feat = benign_list[best_feat_index]

            # print("Feat: " + str(best_feat) + ", prob: " + str(best_probs))
            std_logger.info(str(best_feat) + ": " + str(best_probs))
            feature_logger.info(str(best_feat))
            malicious_orig.features[best_feat] = 1
            malicious = copy.deepcopy(malicious_orig)

            if best_probs[1] < 0.5:
                std_logger.warning("Success | Final: " + str(best_probs[0]) + " | Mutations: " + str(i+1))
                evasive_file.write(malicious.sparse_arff())
                break
                # print("Success - final prob: " + str(best_probs[0]))

        if best_probs[1] > 0.5:
            std_logger.warning("Failure | Final prob: " + str(best_probs[0]))

        if malicious_sample_size > 20 and sample_num % int(malicious_sample_size/20) == 0:
            print("Progress: " + str(round(sample_num/malicious_sample_size*100)) + "%")

    evasive_file.close()
    print("------------------------------")
    subprocess.run(["./util/postprocess.sh", "assisted_mutation"])
    print("------------------------------")
import sys
import contextlib
import subprocess
import copy
import logging
import threading
import subprocess
from util import *
from lib import liblinearutil

# start_feat = int(sys.argv[1])
# end_feat = int(sys.argv[2])

base = "assisted_mutation/"
model = liblinearutil.load_model("Marvin/models/model_all_liblinear-L2")


def init_logger(thread_id):
    std_logger = logging.getLogger("standard_logger_" + str(thread_id))
    std_logger.setLevel(logging.DEBUG)
    std_fh = logging.FileHandler(base + "master_" + str(thread_id) + ".log")
    std_fh.setLevel(logging.DEBUG)
    std_logger.addHandler(std_fh)

    feature_logger = logging.getLogger("feature_logger_" + str(thread_id))
    feature_logger.setLevel(logging.DEBUG)
    feature_fh = logging.FileHandler(base + "features_" + str(thread_id) +
                                     ".log")
    feature_fh.setLevel(logging.DEBUG)
    feature_logger.addHandler(feature_fh)