<commit_before># This file is part of Viper - https://github.com/viper-framework/viper
# See the file 'LICENSE' for copying permission.

import re
import json
import getpass

try:
    import requests
    HAVE_REQUESTS = True
except ImportError:
    HAVE_REQUESTS = False

try:
    from bs4 import BeautifulSoup
    HAVE_BS4 = True
except ImportError:
    HAVE_BS4 = False

from viper.common.utils import string_clean
from viper.common.abstracts import Module
from viper.core.session import __sessions__

MALWR_LOGIN = 'https://malwr.com/account/login/'
MALWR_USER = '' 
MALWR_PASS = '' 
MALWR_SEARCH = 'https://malwr.com/analysis/search/'
MALWR_PREFIX = 'https://malwr.com'

ANUBIS_LOGIN = 'https://anubis.iseclab.org/?action=login'
ANUBIS_USER = None
ANUBIS_PASS = None
ANUBIS_SEARCH = 'https://anubis.iseclab.org/?action=hashquery'
ANUBIS_PREFIX = 'https://anubis.iseclab.org/'


class Reports(Module):
    cmd = 'reports'
    description = 'Online Sandboxes Reports'
    authors = ['emdel', 'nex', 'deralexxx']

    def __init__(self):
        super(Reports, self).__init__()
        self.parser.add_argument('--malwr', action='store_true', help='Find reports on Malwr')
        self.parser.add_argument('--anubis', action='store_true', help='Find reports on Anubis')
        self.parser.add_argument('--threat', action='store_true', help='Find reports on ThreatExchange')
        self.parser.add_argument('--joe', action='store_true', help='Find reports on Joe Sandbox')
        self.parser.add_argument('--meta', action='store_true', help='Find reports on metascan')

    def authenticate(self):
        username = raw_input('Username: ')
        password = getpass.getpass('Password: ')

        return (username, password)

    def malwr_parse(self, page):
        reports = []
        soup = BeautifulSoup(page)
        tables = soup.findAll('table')
        if len(tables) > 1:
            table = tables[1]
            rows = table.findAll('tr')
            for row in rows:
                cols = row.findAll('td')
                if cols:
                    time = str(cols[0].string)
                    link = cols[1].find('a')
                    url = '{0}{1}'.format(MALWR_PREFIX, link.get("href"))
                    reports.append([time, url])

            return reports

    def malwr(self):
        if not MALWR_USER or not MALWR_PASS:
            choice = raw_input("You need to specify a valid username/password, login now? [y/N] ")
            if choice == 'y':
                username, password = self.authenticate()
            else:
                return
        else:
            username = MALWR_USER
            password = <PASSWORD>

        sess = requests.Session()
        sess.auth = (username, password)

        sess.get(MALWR_LOGIN, verify=False)
        csrf = sess.cookies['csrftoken']

        sess.post(
            MALWR_LOGIN,
            {'username': username, 'password': password, 'csrfmiddlewaretoken': csrf},
            headers=dict(Referer=MALWR_LOGIN),
            verify=False,
            timeout=60
        )

        payload = {'search': __sessions__.current.file.sha256, 'csrfmiddlewaretoken': csrf}
        headers = {"Referer": MALWR_SEARCH}

        p = sess.post(
            MALWR_SEARCH,
            payload,
            headers=headers,
            timeout=60,
            verify=False
        )

        reports = self.malwr_parse(p.text)
        if not reports:
            self.log('info', "No reports for opened file")
            return

        self.log('table', dict(header=['Time', 'URL'], rows=reports))

    def anubis_parse(self, page):
        reports = []
        soup = BeautifulSoup(page)<commit_msg>try block for connection to malwr<commit_after># This file is part of Viper - https://github.com/viper-framework/viper
# See the file 'LICENSE' for copying permission.

import re
import json
import getpass

try:
    import requests
    HAVE_REQUESTS = True
except ImportError:
    HAVE_REQUESTS = False

try:
    from bs4 import BeautifulSoup
    HAVE_BS4 = True
except ImportError:
    HAVE_BS4 = False

from viper.common.utils import string_clean
from viper.common.abstracts import Module
from viper.core.session import __sessions__

MALWR_LOGIN = 'https://malwr.com/account/login/'
MALWR_USER = '' 
MALWR_PASS = '' 
MALWR_SEARCH = 'https://malwr.com/analysis/search/'
MALWR_PREFIX = 'https://malwr.com'

ANUBIS_LOGIN = 'https://anubis.iseclab.org/?action=login'
ANUBIS_USER = None
ANUBIS_PASS = None
ANUBIS_SEARCH = 'https://anubis.iseclab.org/?action=hashquery'
ANUBIS_PREFIX = 'https://anubis.iseclab.org/'


class Reports(Module):
    cmd = 'reports'
    description = 'Online Sandboxes Reports'
    authors = ['emdel', 'nex', 'deralexxx']

    def __init__(self):
        super(Reports, self).__init__()
        self.parser.add_argument('--malwr', action='store_true', help='Find reports on Malwr')
        self.parser.add_argument('--anubis', action='store_true', help='Find reports on Anubis')
        self.parser.add_argument('--threat', action='store_true', help='Find reports on ThreatExchange')
        self.parser.add_argument('--joe', action='store_true', help='Find reports on Joe Sandbox')
        self.parser.add_argument('--meta', action='store_true', help='Find reports on metascan')

    def authenticate(self):
        username = raw_input('Username: ')
        password = getpass.getpass('Password: ')

        return (username, password)

    def malwr_parse(self, page):
        reports = []
        soup = BeautifulSoup(page)
        tables = soup.findAll('table')
        if len(tables) > 1:
            table = tables[1]
            rows = table.findAll('tr')
            for row in rows:
                cols = row.findAll('td')
                if cols:
                    time = str(cols[0].string)
                    link = cols[1].find('a')
                    url = '{0}{1}'.format(MALWR_PREFIX, link.get("href"))
                    reports.append([time, url])

            return reports

    def malwr(self):
        if not MALWR_USER or not MALWR_PASS:
            choice = raw_input("You need to specify a valid username/password, login now? [y/N] ")
            if choice == 'y':
                username, password = self.authenticate()
            else:
                return
        else:
            username = MALWR_USER
            password = <PASSWORD>
        try:
            sess = requests.Session()
            sess.auth = (username, password)

            sess.get(MALWR_LOGIN, verify=False)
            csrf = sess.cookies['csrftoken']

            sess.post(
                MALWR_LOGIN,
                {'username': username, 'password': password, 'csrfmiddlewaretoken': csrf},
                headers=dict(Referer=MALWR_LOGIN),
                verify=False,
                timeout=60
                )
        except:
            self.log('info', "Error while connecting to malwr")
            return
        payload = {'search': __sessions__.current.file.sha256, 'csrfmiddlewaretoken': csrf}
        headers = {"Referer": MALWR_SEARCH}
        p = sess.post(
            MALWR_SEARCH,
            payload,
            headers=headers,
            timeout=60,
            verify=False
        )

        reports = self.malwr_parse(p.text)
        if not reports:
            self.log('info', "No reports for opened file")
            return

        self.log('table', dict(header=['Time', 'URL'], rows=reports))

    def anubis_parse(self, page):
        reports = []
        soup = BeautifulSoup(page)
{'fraction_non_alphanumeric': 0.11954937044400266, 'fraction_numerical': 0.004241219350563287, 'mean_word_length': 2.2539887882708065, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 14, 'lorem ipsum': 0, 'www.': 0, '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': '3992573', 'n_tokens_mistral': 2345, 'n_tokens_neox': 2248, 'n_words': 513}