Skip to content

Commit

Permalink
PEP8, Cortex 2 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
3c7 committed May 29, 2018
1 parent 53ce7d2 commit 1654f9b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 56 deletions.
11 changes: 10 additions & 1 deletion analyzers/Malwares/Malwares_GetReport.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@
"max_tlp": 3,
"service": "get"
},
"command": "Malwares/malwares.py"
"command": "Malwares/malwares.py",
"configurationItems": [
{
"name": "key",
"description": "Malwares.com API Key",
"type": "string",
"multi": false,
"required": true
}
]
}
11 changes: 10 additions & 1 deletion analyzers/Malwares/Malwares_Scan.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@
"service": "scan",
"max_tlp": 1
},
"command": "Malwares/malwares.py"
"command": "Malwares/malwares.py",
"configurationItems": [
{
"name": "key",
"description": "Malwares.com API Key",
"type": "string",
"multi": false,
"required": true
}
]
}
45 changes: 19 additions & 26 deletions analyzers/Malwares/malwares.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#!/usr/bin/env python3
# encoding: utf-8
import sys
import os
import json
import codecs
import time
import hashlib

Expand All @@ -15,13 +11,15 @@
except ImportError:
from io import StringIO


class MalwaresAnalyzer(Analyzer):

def __init__(self):
Analyzer.__init__(self)
self.service = self.getParam('config.service', None, 'Service parameter is missing')
self.key = self.getParam('config.key', None, 'Missing Malware API key')
self.polling_interval = self.getParam('config.polling_interval', 60)
self.service = self.get_param('config.service', None, 'Service parameter is missing')
self.key = self.get_param('config.key', None, 'Missing Malware API key')
self.polling_interval = self.get_param('config.polling_interval', 60)
self.m_api = Api(self.key)

def wait_file_report(self, id):
results = self.check_response(self.m_api.get_file_report(id))
Expand Down Expand Up @@ -61,9 +59,9 @@ def read_scan_response(self, response, func):
code = results.get('result_code', None)
md5 = results.get('md5', None)
url = results.get('url', None)
if code in (1,2) and md5 is not None:
if code in (1, 2) and md5 is not None:
func(md5)
elif code in (1,2) and url is not None:
elif code in (1, 2) and url is not None:
func(url)
else:
self.error('%d %s %s - Scan not found' % (code, md5, url))
Expand Down Expand Up @@ -106,57 +104,54 @@ def summary(self, raw):
score = raw.get("virustotal", {}).get("positives", 0)
total = raw.get("virustotal", {}).get("total", 0)
value = "\"{}/{} positives\"".format(score, total)

if score == 0:
level = "safe"
elif 0 < score <= 5:
level = "suspicious"
elif score > 5:
level = "malicious"
level = "malicious"

taxonomies.append(self.build_taxonomy(
level, namespace, predicate, value))
return {"taxonomies": taxonomies}

def run(self):
Analyzer.run(self)
self.m_api = Api(self.key)

if self.service == 'scan':
if self.data_type == 'file':
filename = self.getParam('filename', 'noname.ext')
filepath = self.getParam('file', None, 'File is missing')
filename = self.get_param('filename', 'noname.ext')
filepath = self.get_param('file', None, 'File is missing')
self.read_scan_response(self.m_api.scan_file(
open(filepath, 'rb'), filename), self.wait_file_report)
elif self.data_type == 'url':
data = self.getParam('data', None, 'Data is missing')
data = self.get_param('data', None, 'Data is missing')
self.read_scan_response(
self.m_api.scan_url(data), self.wait_url_report)
else:
self.error('Invalid data type')
elif self.service == 'get':
if self.data_type == 'domain':
data = self.getParam('data', None, 'Data is missing')
data = self.get_param('data', None, 'Data is missing')
self.report(self.check_response(
self.m_api.get_domain_report(data)))
elif self.data_type == 'ip':
data = self.getParam('data', None, 'Data is missing')
data = self.get_param('data', None, 'Data is missing')
self.report(self.check_response(self.m_api.get_ip_report(data)))
elif self.data_type == 'file':

hashes = self.getParam('attachment.hashes',
None)
hashes = self.get_param('attachment.hashes',
None)
if hashes is None:
filepath = self.getParam('file', None, 'File is missing')
filepath = self.get_param('file', None, 'File is missing')
hash = hashlib.sha256(open(filepath, 'r').read()).hexdigest();
else:
# find SHA256 hash
# find SHA256 hash
hash = next(h for h in hashes if len(h) == 64)

self.report(self.check_response(self.m_api.get_file_report(hash)))

elif self.data_type == 'hash':
data = self.getParam('data', None, 'Data is missing')
data = self.get_param('data', None, 'Data is missing')
self.report(self.check_response(self.m_api.get_file_report(data)))
else:
self.error('Invalid data type')
Expand All @@ -166,5 +161,3 @@ def run(self):

if __name__ == '__main__':
MalwaresAnalyzer().run()


56 changes: 28 additions & 28 deletions analyzers/Malwares/malwares_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# -*- coding: utf-8 -*-

import os
#import StringIO
import requests


class Api():

def __init__(self, api_key=None):
Expand Down Expand Up @@ -55,13 +55,13 @@ def get_file_report(self, this_hash):
ri = _return_response_and_status_code(response_info)
ra = _return_response_and_status_code(response_additional)

if ri['response_code'] == '1' and ra['response_code'] == '1': # both ok
if ri['response_code'] == '1' and ra['response_code'] == '1': # both ok
response = dict(results={**ri['results'], **ra['results']}, response_code=1)
elif ri['response_code'] == '1' and ra['response_code'] == '0': # advance non exists but standard ok
elif ri['response_code'] == '1' and ra['response_code'] == '0': # advance non exists but standard ok
response = ri
elif ri['response_code'] == '2': # main is still loading
elif ri['response_code'] == '2': # main is still loading
response = dict(results={}, response_code=2)
else: # error generic
else: # error generic
response = ri
return response

Expand Down Expand Up @@ -126,6 +126,7 @@ def get_domain_report(self, this_domain):

return _return_response_and_status_code(response)


class ApiError(Exception):
pass

Expand All @@ -139,32 +140,31 @@ def _return_response_and_status_code(response):
"""

result_codes = {
"-11" : "No matching data to API Key API Key error",
"-12" : "No authority to use No authority to use",
"-13" : "Expired API Key API Key expired",
"-14" : "Over the daily request limit Request limit per daily exceeded",
"-15" : "Over the hourly request limit Request limit per hour exceeded",
"-1" : "Invalid Parameters / Invalid Request",
"-25" : "File Upload Quota Limit Error in file size to upload",
"-2" : "Invalid URL Error in URL type",
"-31" : "Invalid type of hash error in Hash type",
"-400" : "No file attached No file attached",
"-404" : "No result No result",
"-415" : "Ectype of upload form is not multipart/form-data Error in upload form type",
"-41" : "Invalid type of url Error in URL type",
"-500" : "Internal Server Error System error",
"-51" : "Invalid type of ip Error in IP type",
"-61" : "Invalid type of hostname Error in Hostname type",
"0" : "Data is not exist No information found in DB.",
"1" : "Data exists / Analysis request succeeded /Successful upload (new)",
"2" : "Analysis in progress / Successful upload (duplicated)",
"-11": "No matching data to API Key API Key error",
"-12": "No authority to use No authority to use",
"-13": "Expired API Key API Key expired",
"-14": "Over the daily request limit Request limit per daily exceeded",
"-15": "Over the hourly request limit Request limit per hour exceeded",
"-1": "Invalid Parameters / Invalid Request",
"-25": "File Upload Quota Limit Error in file size to upload",
"-2": "Invalid URL Error in URL type",
"-31": "Invalid type of hash error in Hash type",
"-400": "No file attached No file attached",
"-404": "No result No result",
"-415": "Ectype of upload form is not multipart/form-data Error in upload form type",
"-41": "Invalid type of url Error in URL type",
"-500": "Internal Server Error System error",
"-51": "Invalid type of ip Error in IP type",
"-61": "Invalid type of hostname Error in Hostname type",
"0": "Data is not exist No information found in DB.",
"1": "Data exists / Analysis request succeeded /Successful upload (new)",
"2": "Analysis in progress / Successful upload (duplicated)",
"-999": "Error"

}

results = response.json()

result_code = str(response.json().get('result_code', '-999'))
result_message = result_codes[result_code]
return dict(results=results, response_code=result_code, result_message=result_message)

0 comments on commit 1654f9b

Please sign in to comment.