|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import requests |
| 4 | +import time |
| 5 | +import os |
| 6 | + |
| 7 | +from cortexutils.analyzer import Analyzer |
| 8 | + |
| 9 | + |
| 10 | +class IntezerCommunityAnalyzer(Analyzer): |
| 11 | + """ |
| 12 | + Intezer Community APIs: https://analyze.intezer.com/api/docs/documentation |
| 13 | + """ |
| 14 | + |
| 15 | + def run(self): |
| 16 | + |
| 17 | + try: |
| 18 | + |
| 19 | + if self.data_type == 'file': |
| 20 | + api_key = self.get_param('config.key', None, 'Missing Intezer API key') |
| 21 | + filepath = self.get_param('file', None, 'File is missing') |
| 22 | + filename = self.get_param('filename', os.path.basename(filepath)) |
| 23 | + |
| 24 | + base_url = 'https://analyze.intezer.com/api/v2-0' |
| 25 | + # this should be done just once in a day, but we cannot do that with Cortex Analyzers |
| 26 | + response = requests.post(base_url + '/get-access-token', json={'api_key': api_key}) |
| 27 | + response.raise_for_status() |
| 28 | + session = requests.session() |
| 29 | + session.headers['Authorization'] = session.headers['Authorization'] = 'Bearer %s' % response.json()[ |
| 30 | + 'result'] |
| 31 | + |
| 32 | + with open(filepath, 'rb') as file_to_upload: |
| 33 | + files = {'file': (filename, file_to_upload)} |
| 34 | + response = session.post(base_url + '/analyze', files=files) |
| 35 | + if response.status_code != 201: |
| 36 | + self.error('Error sending file to Intezer Analyzer\n{}'.format(response.text)) |
| 37 | + |
| 38 | + while response.status_code != 200: |
| 39 | + time.sleep(3) |
| 40 | + result_url = response.json()['result_url'] |
| 41 | + response = session.get(base_url + result_url) |
| 42 | + response.raise_for_status() |
| 43 | + |
| 44 | + report = response.json() |
| 45 | + self.report(report) |
| 46 | + |
| 47 | + else: |
| 48 | + self.notSupported() |
| 49 | + |
| 50 | + except requests.HTTPError as e: |
| 51 | + self.error(e) |
| 52 | + except Exception as e: |
| 53 | + self.unexpectedError(e) |
| 54 | + |
| 55 | + def summary(self, raw): |
| 56 | + taxonomies = [] |
| 57 | + namespace = 'IntezerCommunity' |
| 58 | + |
| 59 | + if 'status' in raw and raw['status'] == 'succeeded': |
| 60 | + predicate = 'Analysis succeeded' |
| 61 | + else: |
| 62 | + predicate = 'Analysis failed' |
| 63 | + |
| 64 | + level = 'info' |
| 65 | + value = 'no family' |
| 66 | + if 'result' in raw: |
| 67 | + if 'verdict' in raw['result']: |
| 68 | + level = raw['result']['verdict'] |
| 69 | + if level == 'trusted': |
| 70 | + level = 'safe' |
| 71 | + if level not in ['info', 'safe', 'suspicious', 'malicious']: |
| 72 | + level = 'info' |
| 73 | + if 'family_name' in raw['result']: |
| 74 | + value = raw['result']['family_name'] |
| 75 | + |
| 76 | + taxonomies.append(self.build_taxonomy(level, namespace, predicate, value)) |
| 77 | + |
| 78 | + return {'taxonomies': taxonomies} |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == '__main__': |
| 82 | + IntezerCommunityAnalyzer().run() |
0 commit comments