Skip to content

Commit c2c9937

Browse files
Merge pull request #766 from hariomenkel/master
Add new responder VirustotalDownloader #765
2 parents c8e3923 + 7c00750 commit c2c9937

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "Virustotal Downloader",
3+
"version": "0.1",
4+
"author": "Mario Henkel @hariomenkel",
5+
"url": "https://github.com/TheHive-Project/Cortex-Analyzers",
6+
"license": "AGPL-V3",
7+
"description": "Download a file from Virustotal by its hash",
8+
"dataTypeList": ["thehive:case_artifact"],
9+
"command": "VirustotalDownloader/VirustotalDownloader.py",
10+
"baseConfig": "VirustotalDownloader",
11+
"configurationItems": [
12+
{
13+
"name": "virustotal_apikey",
14+
"description": "Virustotal API key which should be used to download files",
15+
"type": "string",
16+
"multi": false,
17+
"required": true
18+
},
19+
{
20+
"name": "thehive_url",
21+
"description": "URL pointing to your TheHive installation, e.g. 'http://127.0.0.1:9000'",
22+
"type": "string",
23+
"multi": false,
24+
"required": true
25+
},
26+
{
27+
"name": "thehive_apikey",
28+
"description": "TheHive API key which is used to add the downloaded file back to the alert/case",
29+
"type": "string",
30+
"multi": false,
31+
"required": true
32+
}
33+
]
34+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cortexutils
2+
datetime
3+
requests
4+
thehive4py
5+
python-magic
6+
filetype

0 commit comments

Comments
 (0)