|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# encoding: utf-8 |
| 3 | + |
| 4 | +from cortexutils.responder import Responder |
| 5 | +import requests |
| 6 | +import os |
| 7 | +import magic |
| 8 | +import tempfile |
| 9 | +import mimetypes |
| 10 | +import filetype |
| 11 | +from thehive4py.api import TheHiveApi |
| 12 | +from thehive4py.models import Case, CaseObservable |
| 13 | + |
| 14 | +class VirustotalDownloader(Responder): |
| 15 | + def __init__(self): |
| 16 | + Responder.__init__(self) |
| 17 | + self.virustotal_apikey = self.get_param('config.virustotal_apikey', None, "Virustotal API key missing!") |
| 18 | + self.thehive_url = self.get_param('config.thehive_url', None, "TheHive URL missing!") |
| 19 | + self.thehive_apikey = self.get_param('config.thehive_apikey', None, "TheHive API key missing!") |
| 20 | + |
| 21 | + def run(self): |
| 22 | + Responder.run(self) |
| 23 | + |
| 24 | + data_type = self.get_param('data.dataType') |
| 25 | + case_id = self.get_param('data._parent') |
| 26 | + ioc_types = ["hash"] |
| 27 | + |
| 28 | + if data_type in ioc_types: |
| 29 | + url = 'https://www.virustotal.com/vtapi/v2/file/download' |
| 30 | + params = {'apikey': self.virustotal_apikey, 'hash': self.get_param('data.data')} |
| 31 | + |
| 32 | + response = requests.get(url, params=params) |
| 33 | + |
| 34 | + if response.status_code == 200: |
| 35 | + filename = "" |
| 36 | + downloaded_file = response.content |
| 37 | + |
| 38 | + tempdir = tempfile.gettempdir() |
| 39 | + f = open(tempdir + "/" + self.get_param('data.data'), 'wb') |
| 40 | + f.write(downloaded_file) |
| 41 | + f.close() |
| 42 | + filename = f.name |
| 43 | + |
| 44 | + kind = filetype.guess(f.name) |
| 45 | + |
| 46 | + if kind.extension != None: |
| 47 | + os.rename(f.name, f.name + "." + kind.extension) |
| 48 | + filename = f.name + "." + kind.extension |
| 49 | + |
| 50 | + api = TheHiveApi(self.thehive_url, self.thehive_apikey) |
| 51 | + |
| 52 | + file_observable = CaseObservable(dataType='file', |
| 53 | + data=[filename], |
| 54 | + tlp=self.get_param('data.tlp'), |
| 55 | + ioc=True, |
| 56 | + tags=['src:VirusTotal', str(kind.mime), str(kind.extension), 'parent:' + self.get_param('data.data')], |
| 57 | + message='' |
| 58 | + ) |
| 59 | + |
| 60 | + response = api.create_case_observable(case_id, file_observable) |
| 61 | + |
| 62 | + self.report({'message': str(response.status_code) + " " + response.text}) |
| 63 | + else: |
| 64 | + self.report({'message': 'Virustotal returned the following error code: ' + str(response.status_code) + ". If you receive 403 this means that you are using a public API key but this responder needs a private Virustotal API key!"}) |
| 65 | + else: |
| 66 | + self.error('Incorrect dataType. "Hash" expected.') |
| 67 | + |
| 68 | + def operations(self, raw): |
| 69 | + return [self.build_operation('AddTagToArtifact', tag='Virustotal:Downloaded')] |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == '__main__': |
| 73 | + VirustotalDownloader().run() |
0 commit comments