<commit_before>#
# Copyright (c) 2013-2014 QuarksLab.
# This file is part of IRMA project.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License in the top-level directory
# of this distribution and at:
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# No part of the project, including this file, may be copied,
# modified, propagated, or distributed except according to the
# terms contained in the LICENSE file.

import os
import sys
import hashlib

from ConfigParser import SafeConfigParser
from datetime import datetime

from lib.common.utils import timestamp
from lib.plugins import PluginBase
from lib.plugins import ModuleDependency, FileDependency
from lib.plugin_result import PluginResult


class VirusTotalPlugin(PluginBase):

    class VirusTotalResult:
        ERROR = -1
        FOUND = 1
        NOT_FOUND = 0

    # =================
    #  plugin metadata
    # =================

    _plugin_name_ = "VirusTotal"
    _plugin_author_ = "IRMA (c) Quarkslab"
    _plugin_version_ = "1.0.0"
    _plugin_category_ = "external"
    _plugin_description_ = "Plugin to query VirusTotal API"
    _plugin_dependencies_ = [
        ModuleDependency(
            'virus_total_apis',
            help='See requirements.txt for needed dependencies'
        ),
        FileDependency(
            os.path.join(os.path.dirname(__file__), 'config.ini')
        )
    ]

    # =============
    #  constructor
    # =============

    def __init__(self, apikey=None, private=None):
        # load default configuration file
        config = SafeConfigParser()
        config.read(os.path.join(os.path.dirname(__file__), 'config.ini'))

        # override default values if specified
        if apikey is None:
            self.apikey = config.get('VirusTotal', 'apikey')
        else:
            self.apikey = apikey

        if private is None:
            self.private = bool(config.get('VirusTotal', 'private'))
        else:
            self.private = private

        # choose either public or private API for requests
        if private:
            module = sys.modules['virus_total_apis'].PrivateApi
        else:
            module = sys.modules['virus_total_apis'].PublicApi
        self.module = module(self.apikey)

    def get_file_report(self, filename):
        with open(filename, 'rb') as filedesc:
            digest = hashlib.md5(filedesc.read()).hexdigest()
        return self.module.get_file_report(digest)

    # ==================
    #  probe interfaces
    # ==================

    def run(self, paths):
        results = PluginResult(name=type(self).plugin_name,
                               type=type(self).plugin_category,
                               version=None)
        try:
            # get the report, automatically append results
            started = timestamp(datetime.utcnow())
            response = self.get_file_report(paths)
            stopped = timestamp(datetime.utcnow())
            results.duration = stopped - started
            # check eventually for errors
            if (response['response_code'] == 204) or \
               (response['response_code'] == 403):
                results.status = self.VirusTotalResult.ERROR
                results.error = response['results']['verbose_msg']
            elif (response['response_code'] == 200) and \
                 (response['results']['response_code'] != 1):
                results.status = self.VirusTotalResult.NOT_FOUND
            else:
                results.status = self.VirusTotalResult.FOUND
            results.results = response
        except Exception as e:
            results.status = self.VirusTotalResult.ERROR
            results.results = str(e)
        return results<commit_msg>fix bug on vt probe when no connection available<commit_after>#
# Copyright (c) 2013-2014 QuarksLab.
# This file is part of IRMA project.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License in the top-level directory
# of this distribution and at:
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# No part of the project, including this file, may be copied,
# modified, propagated, or distributed except according to the
# terms contained in the LICENSE file.

import os
import sys
import hashlib

from ConfigParser import SafeConfigParser
from datetime import datetime

from lib.common.utils import timestamp
from lib.common.utils import to_unicode
from lib.plugins import PluginBase
from lib.plugins import ModuleDependency, FileDependency
from lib.plugin_result import PluginResult


class VirusTotalPlugin(PluginBase):

    class VirusTotalResult:
        ERROR = -1
        FOUND = 1
        NOT_FOUND = 0

    # =================
    #  plugin metadata
    # =================

    _plugin_name_ = "VirusTotal"
    _plugin_author_ = "IRMA (c) Quarkslab"
    _plugin_version_ = "1.0.0"
    _plugin_category_ = "external"
    _plugin_description_ = "Plugin to query VirusTotal API"
    _plugin_dependencies_ = [
        ModuleDependency(
            'virus_total_apis',
            help='See requirements.txt for needed dependencies'
        ),
        FileDependency(
            os.path.join(os.path.dirname(__file__), 'config.ini')
        )
    ]

    # =============
    #  constructor
    # =============

    def __init__(self, apikey=None, private=None):
        # load default configuration file
        config = SafeConfigParser()
        config.read(os.path.join(os.path.dirname(__file__), 'config.ini'))

        # override default values if specified
        if apikey is None:
            self.apikey = config.get('VirusTotal', 'apikey')
        else:
            self.apikey = apikey

        if private is None:
            self.private = bool(config.get('VirusTotal', 'private'))
        else:
            self.private = private

        # choose either public or private API for requests
        if private:
            module = sys.modules['virus_total_apis'].PrivateApi
        else:
            module = sys.modules['virus_total_apis'].PublicApi
        self.module = module(self.apikey)

    def get_file_report(self, filename):
        with open(filename, 'rb') as filedesc:
            digest = hashlib.md5(filedesc.read()).hexdigest()
        return self.module.get_file_report(digest)

    # ==================
    #  probe interfaces
    # ==================

    def run(self, paths):
        results = PluginResult(name=type(self).plugin_name,
                               type=type(self).plugin_category,
                               version=None)
        try:
            # get the report, automatically append results
            started = timestamp(datetime.utcnow())
            response = self.get_file_report(paths)
            stopped = timestamp(datetime.utcnow())
            results.duration = stopped - started
            # check eventually for errors
            if 'error' in response:
                results.status = self.VirusTotalResult.ERROR
                results.error = "Network probably unreachable"
            elif (response['response_code'] == 204) or \
               (response['response_code'] == 403):
                results.status = self.VirusTotalResult.ERROR
                results.error = response['results']['verbose_msg']
            elif (response['response_code'] == 200) and \
                 (response['results']['response_code'] != 1):
                results.status = self.VirusTotalResult.NOT_FOUND
            else:
                results.status = self.VirusTotalResult.FOUND
            results.results = response if 'error' not in response else None
        except Exception as e:
            results.status = self.VirusTotalResult.ERROR
            results.results = str(e)
        return to_unicode(results)
{'fraction_non_alphanumeric': 0.11017587939698492, 'fraction_numerical': 0.00728643216080402, 'mean_word_length': 2.347771236333053, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5985291', 'n_tokens_mistral': 2157, 'n_tokens_neox': 2044, 'n_words': 578}