import datetime
import os

from typing import List

import requests
from bs4 import BeautifulSoup
from malwaretl_stoq_transformer import transformer

from malware_collector import MalwareCollector


class VXVaultCollector(MalwareCollector):
    # collect malware from http://vxvault.net/ViriList.php .

    base_url = "http://vxvault.net/"
    list_url = base_url + "ViriList.php"

    def __init__(self):
        super(VXVaultCollector, self).__init__()
        self.base_path = self.configfile.get("VXVault", "path")

    def get(self) -> List[str]:
        base_list = requests.get(self.list_url)
        soup = BeautifulSoup(base_list.content, features='html.parser')
        table = soup.find("table")
        downloaded_files = list()
        for row in table.find_all('tr'):
            th = row.find("th")
            if th is not None:
                continue
            anchors = row.find_all("a")
            for a in anchors:
                if "href" not in a.attrs:
                    continue
                href = a.attrs['href']
                if not href.endswith(".zip"):
                    continue
                filename = href.split("/")[-1]
                download_url = self.base_url + href
                full_saved_path = os.path.join(self.base_path, filename)
                if not os.path.exists(full_saved_path):
                    response = requests.get(download_url, stream=True)
                    with open(full_saved_path, "wb") as fileHandle:
                        for chunk in response.iter_content(chunk_size=8192):
                            fileHandle.write(chunk)
                    downloaded_files.append(full_saved_path)
                    print(f"downloaded {filename}")
        return downloaded_files


if __name__ == "__main__":
    print("starting")
    vx = VXVaultCollector()
    downloaded_files = vx.get()
    print(f"finished download, grabbed {len(downloaded_files)} files")
    if downloaded_files:
        stoq, metadata = transformer.init_vxvault(scan_mode=True)
        metadata.extra_data['collection_time'] = datetime.datetime.utcnow().isoformat()
        vx.scan_downloaded_files(stoq, metadata, downloaded_files)
    print("done")
import datetime
import os

from typing import List

import requests
from bs4 import BeautifulSoup
from malwaretl_stoq_transformer import transformer

from malware_collector import MalwareCollector


class VXVaultCollector(MalwareCollector):
    # collect malware from http://vxvault.net/ViriList.php .

    base_url = "http://vxvault.net/"
    list_url = base_url + "ViriList.php"

    def __init__(self):
        super(VXVaultCollector, self).__init__()
        self.base_path = self.configfile.get("VXVault", "path")

    def get(self) -> List[str]:
        base_list = requests.get(self.list_url)
        soup = BeautifulSoup(base_list.content, features='html.parser')
        table = soup.find("table")
        downloaded_files = list()
        for row in table.find_all('tr'):
            th = row.find("th")
            if th is not None:
                continue
            anchors = row.find_all("a")
            for a in anchors:
                if "href" not in a.attrs:
                    continue
                href = a.attrs['href']
                if not href.endswith(".zip"):
                    continue
                filename = href.split("/")[-1]
                download_url = self.base_url + href
                full_saved_path = os.path.join(self.base_path, filename)
                if not os.path.exists(full_saved_path):
                    response = requests.get(download_url, stream=True)
                    with open(full_saved_path, "wb") as fileHandle:
                        for chunk in response.iter_content(chunk_size=8192):
                            fileHandle.write(chunk)
                    downloaded_files.append(full_saved_path)
                    print(f"downloaded {filename}")
        return downloaded_files


if __name__ == "__main__":
    print("starting")
    vx = VXVaultCollector()
    downloaded_files = vx.get()
    print(f"finished download, grabbed {len(downloaded_files)} files")
    if downloaded_files:
        stoq, metadata = transformer.init_vxvault(scan_mode=True)
        metadata.extra_data['collection_time'] = datetime.datetime.utcnow().isoformat()
        vx.scan_downloaded_files(stoq, metadata, downloaded_files)
    print("done")
# collect malware from http://vxvault.net/ViriList.php .