Beispiel #1
0
 def load(self):
     filepath, _ = self.get_filepath_hash()
     call_specific_db_func(
         self.secuml_conf.db_type, 'load_idents',
         (self.cursor, filepath, self.dataset_conf.dataset_id))
     self.secuml_conf.logger.info(
         'Idents file for the dataset %s/%s '
         'loaded into the database (%s).' %
         (self.dataset_conf.project, self.dataset_conf.dataset, filepath))
Beispiel #2
0
 def load_partial_annotations(self, filename, annotations_id):
     filename = path.join(self.dataset_conf.input_dir(self.secuml_conf),
                          'annotations', filename)
     if not path.isfile(filename):
         raise SecuMLexpException(
             'The annotations file %s does not exist.' % filename)
     conn = self.session.connection().connection
     cursor = conn.cursor()
     call_specific_db_func(
         self.secuml_conf.db_type, 'load_partial_annotations',
         (cursor, filename, annotations_id, self.dataset_conf.dataset_id))
     self.session.flush()
Beispiel #3
0
 def load(self):
     filepath, _ = self.get_filepath_hash()
     self.exists = filepath is not None
     if not self.exists:
         return
     call_specific_db_func(
         self.secuml_conf.db_type, 'load_ground_truth',
         (self.cursor, filepath, self.dataset_conf.dataset_id))
     self.secuml_conf.logger.info(
         'Ground-truth file for the dataset %s/%s '
         'loaded into the database (%s).' %
         (self.dataset_conf.project, self.dataset_conf.dataset, filepath))
Beispiel #4
0
def getAlerts(exp_id, analysis_type):
    exp = update_curr_exp(exp_id)
    # With proba ? With scores ?
    query = session.query(DiademExpAlchemy)
    query = query.filter(DiademExpAlchemy.exp_id == exp_id)
    diadem_exp = query.one()
    with_proba, with_scores = diadem_exp.proba, diadem_exp.with_scoring
    # Get alerts
    query = session.query(PredictionsAlchemy)
    query = query.filter(PredictionsAlchemy.exp_id == exp_id)
    if with_proba:
        threshold = exp.exp_conf.core_conf.detection_threshold
        query = query.filter(PredictionsAlchemy.proba >= threshold)
    else:
        query = query.filter(PredictionsAlchemy.value == MALICIOUS)
    if analysis_type == 'topN' and (with_proba or with_scores):
        if with_proba:
            query = query.order_by(PredictionsAlchemy.proba.desc())
        else:
            query = query.order_by(PredictionsAlchemy.score.desc())
    elif analysis_type == 'random':
        query = call_specific_db_func(secuml_conf.db_type, 'random_order',
                                      (query, ))
    query = query.limit(NUM_MAX_DISPLAY)
    return jsonify(_predictions_results(query))
Beispiel #5
0
def getAlerts(exp_id, analysis_type):
    exp = update_curr_exp(exp_id)
    # With proba ?
    query = session.query(DiademExpAlchemy)
    query = query.filter(DiademExpAlchemy.exp_id == exp_id)
    with_proba = query.one().proba
    threshold = None
    if with_proba:
        threshold = exp.exp_conf.core_conf.detection_threshold
    # Get alerts
    query = session.query(PredictionsAlchemy)
    query = query.filter(PredictionsAlchemy.exp_id == exp_id)
    if with_proba:
        query = query.filter(PredictionsAlchemy.proba >= threshold)
    if analysis_type == 'topN' and with_proba:
        query = query.order_by(PredictionsAlchemy.proba.desc())
    elif analysis_type == 'random':
        query = call_specific_db_func(secuml_conf.db_type, 'random_order',
                                      (query,))
    query = query.limit(TOP_N_ALERTS)
    predictions = query.all()
    if predictions:
        ids, probas = zip(*[(r.instance_id, r.proba) for r in predictions])
    else:
        ids = []
        probas = []
    return jsonify({'instances': ids, 'proba': probas})
Beispiel #6
0
def getPredictionsScores(exp_id, range_, label):
    score_min, score_max = [float(x) for x in range_.split(' - ')]
    query = session.query(PredictionsAlchemy)
    query = query.filter(PredictionsAlchemy.exp_id == exp_id)
    query = query.filter(PredictionsAlchemy.score >= score_min)
    query = query.filter(PredictionsAlchemy.score <= score_max)
    query = query.order_by(PredictionsAlchemy.score.asc())
    if label != 'all':
        query = query.join(PredictionsAlchemy.instance)
        query = query.join(InstancesAlchemy.ground_truth)
        query = query.filter(GroundTruthAlchemy.label == label)
    query = call_specific_db_func(secuml_conf.db_type, 'random_order',
                                  (query, ))
    query = query.limit(NUM_MAX_DISPLAY)
    return jsonify(_predictions_results(query))
Beispiel #7
0
 def _get_errors(exp_id, fn_fp):
     if fn_fp == 'FN':
         predicted_value = 'benign'
         ground_truth = 'malicious'
     else:
         predicted_value = 'malicious'
         ground_truth = 'benign'
     query = session.query(PredictionsAlchemy)
     query = query.filter(PredictionsAlchemy.exp_id == exp_id)
     query = query.filter(PredictionsAlchemy.value == predicted_value)
     query = query.join(PredictionsAlchemy.instance)
     query = query.join(InstancesAlchemy.ground_truth)
     query = query.filter(GroundTruthAlchemy.label == ground_truth)
     query = call_specific_db_func(secuml_conf.db_type, 'random_order',
                                   (query, ))
     query = query.limit(NUM_MAX_DISPLAY)
     return _predictions_results(query)
Beispiel #8
0
def getPredictionsProbas(exp_id, index, label):
    index = int(index)
    proba_min = index * 0.1
    proba_max = (index + 1) * 0.1
    query = session.query(PredictionsAlchemy)
    query = query.filter(PredictionsAlchemy.exp_id == exp_id)
    query = query.filter(PredictionsAlchemy.proba >= proba_min)
    query = query.filter(PredictionsAlchemy.proba <= proba_max)
    query = query.order_by(PredictionsAlchemy.proba.asc())
    if label != 'all':
        query = query.join(PredictionsAlchemy.instance)
        query = query.join(InstancesAlchemy.ground_truth)
        query = query.filter(GroundTruthAlchemy.label == label)
    query = call_specific_db_func(secuml_conf.db_type, 'random_order',
                                  (query, ))
    query = query.limit(NUM_MAX_DISPLAY)
    return jsonify(_predictions_results(query))
Beispiel #9
0
def getPredictions(exp_id, predicted_value, right_wrong, multiclass):
    multiclass = multiclass == 'true'
    query = session.query(PredictionsAlchemy)
    query = query.filter(PredictionsAlchemy.exp_id == exp_id)
    query = query.filter(PredictionsAlchemy.value == predicted_value)
    if right_wrong != 'all':
        query = query.join(PredictionsAlchemy.instance)
        query = query.join(InstancesAlchemy.ground_truth)
        field = 'family' if multiclass else 'label'
        if right_wrong == 'right':
            query = query.filter(
                getattr(GroundTruthAlchemy, field) == predicted_value)
        elif right_wrong == 'wrong':
            query = query.filter(
                getattr(GroundTruthAlchemy, field) != predicted_value)
        else:
            assert (False)
    query = call_specific_db_func(secuml_conf.db_type, 'random_order',
                                  (query, ))
    query = query.limit(NUM_MAX_DISPLAY)
    return jsonify(_predictions_results(query))
Beispiel #10
0
 def get_engine(self):
     return call_specific_db_func(self.db_type, 'get_engine',
                                  (self.db_uri,))