예제 #1
0
def check_username_exists(check_username_exists_json):
    try:
        check_username_exists_dict = json_decoder.decode(
            check_username_exists_json)

        # if new installation, create new user table

        if not (user_table_exists()):
            create_new_user_table()

        # check user table is username exists

        fileObject = open("USERTABLE", "rb")
        table = pickle.load(fileObject)

        if any(obj['username'] == check_username_exists_dict['username']
               for obj in table):
            return json_encoder.encode({
                "message": "Success",
                "comment": "Username exists"
            })

        return json_encoder.encode({
            "message": "Success",
            "comment": "Username available"
        })

    except:
        return json_encoder.encode({
            "message": "Failure",
            "comment": "Other error"
        })
def check_username_exists(check_username_exists_json):
    check_username_exists_dict = json_decoder.decode(
        check_username_exists_json)

    print("\n", check_username_exists_dict, "\n")
    # if new installation, create new user table
    if not (user_table_exists()):
        create_new_user_table()

    # check user table is username exists
    fileObject = open("USERTABLE", "rb")
    table = pickle.load(fileObject)
    print("\n", table, "\n")
    if any(obj['username'] == check_username_exists_dict['username']
           for obj in table):
        print("exists")
        return json_encoder.encode({
            "message": "Success",
            "comment": "Username exists"
        })

    return json_encoder.encode({
        "message": "Success",
        "comment": "Username available"
    })
예제 #3
0
def check_notebook_name_exists(check_notebook_name_exists_json):
    check_notebook_name_exists_dict = json_decoder.decode(
        check_notebook_name_exists_json)

    # if new installation, create global notebooks table

    if not (notebook_global_table_exist()):
        create_notebook_global_table()

    # opens the global notebooks table, and checks whether notebook exists

    fileObject = open("NOTEBOOKS_DATA", "rb")
    table = pickle.load(fileObject)

    if any(obj['notebook_name'] ==
           check_notebook_name_exists_dict['notebook_name'] for obj in table):
        return json_encoder.encode({
            "message": "Success",
            "comment": "Notebook name exists"
        })

    return json_encoder.encode({
        "message": "Success",
        "comment": "Notebook name available"
    })
예제 #4
0
def export_pdf(notebook_name_json):
    notebook_name_dict = json_decoder.decode(notebook_name_json)

    url = [
        "/upload-data", "/build-model", "/train-model", "/results",
        "/investigate-model"
    ]
    merger = PdfFileMerger()

    # go over the 5 pages of the notebook and save them as pdfs using google-chrome invoked through shell.
    # will not work on windows
    for i in range(5):
        os.system("google-chrome --headless --disable-gpu --print-to-pdf=\"" +
                  notebook_name_dict['notebook_name'] + 'out' + str(i) +
                  '.pdf' + "\" http://localhost:8080/notebook/" +
                  notebook_name_dict['notebook_name'] + url[i])

    # merge the pdfs while deleting them
    for i in range(5):
        merger.append(
            open(notebook_name_dict['notebook_name'] + 'out' + str(i) + '.pdf',
                 'rb'))
        remove(notebook_name_dict['notebook_name'] + 'out' + str(i) + '.pdf')

    # save the merged pdf
    with open(notebook_name_dict['notebook_name'] + '_report.pdf', 'wb') as fp:
        merger.write(fp)

    # return PDF type data, to open on the browser
    return Response(open(notebook_name_dict['notebook_name'] + '_report.pdf',
                         'rb').read(),
                    mimetype="application/pdf")
def load_existing_notebook_details(notebook_details):
    # Send data to UI to load back existing notebook
    print(notebook_details)
    data = json_decoder.decode(notebook_details)
    notebook = get_notebook_data(data['notebook_name'])
    dct = {}
    for key in notebook:
        dct[key] = notebook[key]

    return json_encoder.encode({"message": "Success", "notebook_data": dct})
def pick_tool(vulnerabilities_json):
    print("here")
    status_code = -1
    status_message = 'Error'
    return_data = ""
    print(vulnerabilities_json)
    # vulnerabilities_dict = json_encoder.encode(vulnerabilities_json)
    vulnerabilities_dict = json_decoder.decode(vulnerabilities_json)
    vulnerabilities = vulnerabilities_dict["vulnerabilities"]

    username = vulnerabilities_dict["username"]
    print("\nusername: "******"\n")

    url = vulnerabilities_dict["url"]
    url = url.strip('\"')
    print("\n " + vulnerabilities_dict["url"] + "\n")

    if "ssl" in vulnerabilities:
        url = url.split('//')
        if url[0] == 'https:' or url[0] == 'http:':
            url.pop(0)
        url_modified = ''.join(map(str, url))
        url_modified = url_modified.split('/')
        url_modified_stripped = url_modified[0]
        print(url_modified_stripped)
        ss = VulnerabilityScanTools()
        return_data += ss.sslyzer_scan("www.pes.edu:443", "full")
    elif "xsser" in vulnerabilities:
        ss = VulnerabilityScanTools()
        return_data += ss.xsser_scan("https://hack.me/")
    elif "nikto" in vulnerabilities:
        ss = VulnerabilityScanTools()
        return_data += ss.nikto_scan("www.isanalytics.com")

    print("Notebook name", vulnerabilities_dict["notebook_name"])

    #setting notebook data
    fileObject = open("NOTEBOOK_" + vulnerabilities_dict["notebook_name"],
                      "rb")
    table = pickle.load(fileObject)
    print(type(table))
    fileObject.close()

    table["url"] = vulnerabilities_dict["url"]
    table["vulnerabilities"] = vulnerabilities_dict["vulnerabilities"]
    table["report"] = return_data

    fileObject = open("NOTEBOOK_" + vulnerabilities_dict["notebook_name"],
                      "wb")
    pickle.dump(table, fileObject)
    fileObject.close()

    print("return data", return_data)
    return json_encoder.encode({"message": "Success", "report": return_data})
예제 #7
0
def get_roc_curve(notebook_name_json):
    notebook_name_dict = json_decoder.decode(notebook_name_json)

    notebook = get_notebook_data(notebook_name_dict['notebook_name'])

    # does predict on testing data

    # to invoke keras predict
    if notebook['model_type'] == "NEURAL NETWORK":
        model = keras.models.load_model("NOTEBOOK_" +
                                        notebook_name_dict['notebook_name'] +
                                        "_neural_network_model.hdf5")
        probs = model.predict(notebook['x_test'])

    # to invoke sklearn predict
    else:
        probs = notebook['model'].predict_proba(notebook['x_test'])

    preds = probs[:, 1]
    fpr, tpr, threshold = roc_curve(notebook['y_test'], preds)
    roc_auc = auc(fpr, tpr)

    # Create ROC plot
    plt.title('Receiver Operating Characteristic')
    plt.plot(fpr, tpr, 'b', label='AUC = %0.2f' % roc_auc)
    plt.legend(loc='lower right')
    plt.plot([0, 1], [0, 1], 'r--')
    plt.xlim([0, 1])
    plt.ylim([0, 1])
    plt.ylabel('True Positive Rate')
    plt.xlabel('False Positive Rate')

    # Save plot to be used by vue.js
    filename = "NOTEBOOK_" + notebook['notebook_name'] + "_roc_curve.jpg"
    plt.savefig("../UI/src/assets/" + filename)

    plt.clf()

    # Save file name in notebook
    notebook['roc_curve'] = filename

    set_notebook_data(notebook_name_dict['notebook_name'])

    try:
        keras.backend.clear_session()
    except:
        pass

    return json_encoder.encode({"message": "Success", "roc_curve": filename})
def get_user_notebooks(get_user_notebooks_json):
    get_user_notebooks_dict = json_decoder.decode(get_user_notebooks_json)

    # opens user table and returns list of notebooks opened by that user
    table = pickle.load(open("USERTABLE", "rb"))

    print("table", table, get_user_notebooks_dict)

    for obj in table:
        if (obj['username'] == get_user_notebooks_dict['username']):
            print("username match")
            return json_encoder.encode({
                "message": "Success",
                "notebook_names": obj['notebooks']
            })

    return json_encoder.encode({"message": "Failure"})
예제 #9
0
def get_confusion_matrix(notebook_name_json):
    notebook_name_dict = json_decoder.decode(notebook_name_json)

    notebook = get_notebook_data(notebook_name_dict['notebook_name'])

    # to invoke keras predict
    if notebook['model_type'] == "NEURAL NETWORK":
        model = keras.models.load_model("NOTEBOOK_" +
                                        notebook_name_dict['notebook_name'] +
                                        "_neural_network_model.hdf5")
        prediction = model.predict(notebook['x_test'])
        prediction = numpy.array(list(map(numpy.argmax, prediction)))

    # to invoke sklearn predict
    else:
        model = notebook['model']
        prediction = model.predict(notebook['x_test'])

    # de-"one hot"
    y_test = notebook['y_test']
    if len(y_test.shape) > 1:
        y_test = numpy.array(list(map(numpy.argmax, y_test)))

    matrix = confusion_matrix(notebook['y_test'], prediction).ravel()

    # save confusion matrix in notebook
    # did not consider for multiclass labels while displaying
    notebook['confusion_matrix'] = matrix
    notebook['true_negative'] = int(matrix[0])
    notebook['false_positive'] = int(matrix[1])
    notebook['false_negative'] = int(matrix[2])
    notebook['true_positive'] = int(matrix[3])

    set_notebook_data(notebook_name_dict['notebook_name'])

    try:
        keras.backend.clear_session()
    except:
        pass

    return json_encoder.encode({
        "message": "Success",
        "confusion_matrix": matrix.tolist()
    })
예제 #10
0
def get_epoch_details(notebook_name_json):

    notebook_name_dict = json_decoder.decode(notebook_name_json)
    notebook = get_notebook_data(notebook_name_dict['notebook_name'])

    # Yield the events of epoch ending
    def epoch_done_streamer():

        # Busy wait - can be improved
        while True:
            if notebook['_epoch_done']:

                # reset the epoch done notifier; continue to wait busily
                notebook['_epoch_done'] = False

                # send just a signal to reload the image created, therefore data = 0
                yield 'event: EPOCH_END\ndata: 0\n\n'

    # return Server sent events
    return Response(epoch_done_streamer(), mimetype="text/event-stream")
예제 #11
0
def get_accuracy(notebook_name_json):
    notebook_name_dict = json_decoder.decode(notebook_name_json)

    notebook = get_notebook_data(notebook_name_dict['notebook_name'])

    # to invoke keras predict
    if notebook['model_type'] == "NEURAL NETWORK":
        model = keras.models.load_model("NOTEBOOK_" +
                                        notebook_name_dict['notebook_name'] +
                                        "_neural_network_model.hdf5")
        prediction = model.predict(notebook['x_test'])
        prediction = numpy.array(list(map(numpy.argmax, prediction)))

    # to invoke sklearn predict
    else:
        model = notebook['model']
        prediction = model.predict(notebook['x_test'])

    # de-"one hot"
    y_test = notebook['y_test']
    if len(y_test.shape) > 1:
        y_test = numpy.array(list(map(numpy.argmax, y_test)))

    accuracy = accuracy_score(notebook['y_test'], prediction)

    # save accuracy in notebook
    notebook['accuracy'] = accuracy

    set_notebook_data(notebook_name_dict['notebook_name'])

    try:
        keras.backend.clear_session()
    except:
        pass

    return json_encoder.encode({
        "message": "Success",
        "accuracy": str(accuracy)
    })
예제 #12
0
def investigate_model(notebook_name_json):

    notebook_name_dict = json_decoder.decode(notebook_name_json)
    notebook = get_notebook_data(notebook_name_dict['notebook_name'])

    def predict_fn(instance):
        if notebook['model_type'] == "NEURAL NETWORK":
            return keras.models.model_from_json(notebook['model']).predict(
                [instance])
        try:
            return notebook['model'].predict_proba(instance)
        except:
            return notebook['model'].predict(instance)

    def explain_instance_image_data(instance):

        newshape = numpy.prod(instance.shape)

        if notebook['model_type'] == "NEURAL NETWORK":
            model = keras.models.load_model(
                "NOTEBOOK_" + notebook_name_dict['notebook_name'] +
                "_neural_network_model.hdf5")
            target = list(
                map(
                    numpy.argmax,
                    model.predict(
                        numpy.reshape(instance,
                                      newshape=(1, *instance.shape)))[0]))[0]
        else:
            target = notebook['model'].predict(
                numpy.reshape(instance, newshape=newshape))

        plt.savefig("../UI/src/assets/" + "NOTEBOOK_" +
                    notebook['notebook_name'] +
                    "_investigate_model_instance0.jpg")
        plt.clf()

        explainer = lt.LimeTabularExplainer(
            training_data=notebook['x_train'],
            feature_names=[str(i) for i in range(len(instance))]
            if 'column_names' not in notebook else notebook['column_names'])
        exp = explainer.explain_instance(instance,
                                         predict_fn,
                                         num_features=len(instance),
                                         num_samples=newshape,
                                         labels=(target, ))
        exp.as_pyplot_figure(label=target).savefig(
            "../UI/src/assets/" + "NOTEBOOK_" + notebook['notebook_name'] +
            "_investigate_model_instance1.jpg",
            figsize=(50, 50))
        exp.save_to_file(file_path="../UI/src/assets/" + "NOTEBOOK_" +
                         notebook['notebook_name'] +
                         "_investigate_model_instance.html")

        explaination_list = exp.as_map()[target]
        constructed_image = numpy.zeros(shape=len(explaination_list))

        for i, j in explaination_list:
            constructed_image[i] = 0.5 + j / 2

        constructed_image = numpy.reshape(constructed_image,
                                          newshape=instance.shape)
        plt.savefig("../UI/src/assets/" + "NOTEBOOK_" +
                    notebook['notebook_name'] +
                    "_investigate_model_instance2.jpg")
        plt.clf()
        notebook['explanation'] = "NOTEBOOK_" + notebook[
            'notebook_name'] + "_investigate_model_instance.html"
        set_notebook_data(notebook_name_dict['notebook_name'])

        try:
            keras.backend.clear_session()
        except:
            pass

        return json_encoder.encode({
            'instance':
            "NOTEBOOK_" + notebook['notebook_name'] +
            "_investigate_model_instance0.jpg",
            'explanation':
            "NOTEBOOK_" + notebook['notebook_name'] +
            "_investigate_model_instance.html",
            'constructed':
            "NOTEBOOK_" + notebook['notebook_name'] +
            "_investigate_model_instance2.jpg"
        })

    def explain_instance_tabular_data(instance):
        newshape = numpy.prod(instance.shape)

        if notebook['model_type'] == "NEURAL NETWORK":
            model = keras.models.load_model(
                "NOTEBOOK_" + notebook_name_dict['notebook_name'] +
                "_neural_network_model.hdf5")
            target = list(
                map(
                    numpy.argmax,
                    model.predict(
                        numpy.reshape(instance,
                                      newshape=(1, *instance.shape)))[0]))[0]
        else:
            target = notebook['model'].predict([instance])[0]

        explainer = lt.LimeTabularExplainer(
            training_data=notebook['x_train'],
            feature_names=[str(i) for i in range(len(instance))])
        exp = explainer.explain_instance(instance,
                                         predict_fn,
                                         num_features=len(instance),
                                         num_samples=min(
                                             len(notebook['x_train']), 100),
                                         labels=(target, ))
        exp.as_pyplot_figure(label=target).savefig(
            "../UI/src/assets/" + "NOTEBOOK_" + notebook['notebook_name'] +
            "_investigate_model_instance1.jpg",
            figsize=(50, 50))
        exp.save_to_file(file_path="../UI/src/assets/" + "NOTEBOOK_" +
                         notebook['notebook_name'] +
                         "_investigate_model_instance.html")
        notebook['explanation'] = "NOTEBOOK_" + notebook[
            'notebook_name'] + "_investigate_model_instance.html"

        set_notebook_data(notebook_name_dict['notebook_name'])

        try:
            keras.backend.clear_session()
        except:
            pass

        return json_encoder.encode({
            'explanation':
            "NOTEBOOK_" + notebook['notebook_name'] +
            "_investigate_model_instance.html"
        })

    is_image = len(notebook['x_test'][0].shape) >= 2

    if is_image:
        return explain_instance_image_data(
            notebook['x_train'][numpy.random.randint(
                0, len(notebook['x_test'] - 1))])
    else:
        return explain_instance_tabular_data(
            notebook['x_train'][numpy.random.randint(
                0, len(notebook['x_test'] - 1))])
예제 #13
0
def get_precision_recall_curve(notebook_name_json):
    notebook_name_dict = json_decoder.decode(notebook_name_json)

    notebook = get_notebook_data(notebook_name_dict['notebook_name'])

    # to invoke sklearn predict
    if notebook['model_type'] == "NEURAL NETWORK":
        model = keras.models.load_model("NOTEBOOK_" +
                                        notebook_name_dict['notebook_name'] +
                                        "_neural_network_model.hdf5")
        prediction = model.predict(notebook['x_test'])
        prediction = numpy.array(list(map(numpy.argmax, prediction)))

    # to invoke sklearn predict
    else:
        model = notebook['model']
        prediction = model.predict(notebook['x_test'])

    y_score = prediction

    # de-"one hot"
    y_test = notebook['y_test']
    if len(y_test.shape) > 1:
        y_test = numpy.array(list(map(numpy.argmax, y_test)))

    # Average precision curve for only binary class problems
    # can iterate for multi class
    y_test[y_test == y_test.min()] = 0
    y_test[y_test != 0] = 1

    average_precision = average_precision_score(y_test, y_score)

    notebook['average_precision_score'] = average_precision
    precision, recall, _ = precision_recall_curve(y_test, y_score)

    # create precision recall curve
    plt.step(recall, precision, color='b', alpha=0.2, where='post')
    plt.fill_between(recall, precision, step='post', alpha=0.2, color='b')
    plt.xlabel('Recall')
    plt.ylabel('Precision')
    plt.ylim([0.0, 1.05])
    plt.xlim([0.0, 1.0])
    plt.title('2-class Precision-Recall curve: AP={0:0.2f}'.format(
        average_precision))

    # save data and filename in the notebook for reloading
    filename = "NOTEBOOK_" + notebook[
        'notebook_name'] + "_precision_recall_curve.jpg"
    plt.savefig("../UI/src/assets/" + filename)
    plt.clf()
    notebook['precision_recall_curve'] = filename
    notebook['average_precision_score'] = average_precision
    notebook['recall'] = recall.mean()

    set_notebook_data(notebook_name_dict['notebook_name'])

    try:
        keras.backend.clear_session()
    except:
        pass

    return json_encoder.encode({
        "message": "Success",
        "precision_recall_curve": filename,
        "average_precision_score": average_precision,
        "recall": recall.mean()
    })