11#!/usr/bin/env python
22# encoding: utf-8
33
4- import json
54import os
6- import stat
5+ import tempfile
6+ from shutil import copyfileobj
77
88from cortexutils .extractor import Extractor
99from cortexutils .worker import Worker
10- from shutil import copyfileobj
11- import tempfile
12- import ntpath
1310
1411
1512class Analyzer (Worker ):
@@ -21,21 +18,27 @@ def __init__(self, job_directory=None, secret_phrases=None):
2118 self .artifact = self ._input
2219
2320 # Check for auto extraction config
24- self .auto_extract = self .get_param ('config.auto_extract' , self .get_param ('config.auto_extract_artifacts' , True ))
21+ self .auto_extract = self .get_param (
22+ "config.auto_extract" , self .get_param ("config.auto_extract_artifacts" , True )
23+ )
2524
2625 def get_data (self ):
2726 """Wrapper for getting data from input dict.
2827
2928 :return: Data (observable value) given through Cortex"""
30- if self .data_type == ' file' :
31- return self .get_param (' filename' , None , ' Missing filename.' )
29+ if self .data_type == " file" :
30+ return self .get_param (" filename" , None , " Missing filename." )
3231 else :
33- return self .get_param (' data' , None , ' Missing data field' )
32+ return self .get_param (" data" , None , " Missing data field" )
3433
3534 def get_param (self , name , default = None , message = None ):
3635 data = super (Analyzer , self ).get_param (name , default , message )
37- if name == 'file' and self .data_type == 'file' and self .job_directory is not None :
38- path = '%s/input/%s' % (self .job_directory , data )
36+ if (
37+ name == "file"
38+ and self .data_type == "file"
39+ and self .job_directory is not None
40+ ):
41+ path = "%s/input/%s" % (self .job_directory , data )
3942 if os .path .isfile (path ):
4043 return path
4144 else :
@@ -50,17 +53,19 @@ def build_taxonomy(self, level, namespace, predicate, value):
5053 :return: dict
5154 """
5255 # Set info level if something not expected is set
53- if level not in [' info' , ' safe' , ' suspicious' , ' malicious' ]:
54- level = ' info'
56+ if level not in [" info" , " safe" , " suspicious" , " malicious" ]:
57+ level = " info"
5558 return {
56- ' level' : level ,
57- ' namespace' : namespace ,
58- ' predicate' : predicate ,
59- ' value' : value
59+ " level" : level ,
60+ " namespace" : namespace ,
61+ " predicate" : predicate ,
62+ " value" : value ,
6063 }
6164
6265 def summary (self , raw ):
63- """Returns a summary, needed for 'short.html' template. Overwrite it for your needs!
66+ """Returns a summary, needed for 'short.html' template.
67+
68+ Overwrite it for your needs!
6469
6570 :returns: by default return an empty dict"""
6671 return {}
@@ -75,20 +80,26 @@ def artifacts(self, raw):
7580 return []
7681
7782 def build_artifact (self , data_type , data , ** kwargs ):
78- if data_type == ' file' :
83+ if data_type == " file" :
7984 if os .path .isfile (data ):
8085 dst = tempfile .NamedTemporaryFile (
81- dir = os .path .join (self .job_directory , "output" ), delete = False )
82- with open (data , 'rb' ) as src :
86+ dir = os .path .join (self .job_directory , "output" ), delete = False
87+ )
88+ with open (data , "rb" ) as src :
8389 copyfileobj (src , dst )
8490 dstfname = dst .name
8591 dst .close ()
8692 os .chmod (dstfname , 0o444 )
87- kwargs .update ({'dataType' : data_type , 'file' : os .path .basename (dst .name ),
88- 'filename' : os .path .basename (data )})
93+ kwargs .update (
94+ {
95+ "dataType" : data_type ,
96+ "file" : os .path .basename (dst .name ),
97+ "filename" : os .path .basename (data ),
98+ }
99+ )
89100 return kwargs
90101 else :
91- kwargs .update ({' dataType' : data_type , ' data' : data })
102+ kwargs .update ({" dataType" : data_type , " data" : data })
92103 return kwargs
93104
94105 def report (self , full_report , ensure_ascii = False ):
@@ -101,40 +112,49 @@ def report(self, full_report, ensure_ascii=False):
101112 try :
102113 summary = self .summary (full_report )
103114 except Exception :
104- pass
115+ pass # nosec B110
105116 operation_list = []
106117 try :
107118 operation_list = self .operations (full_report )
108119 except Exception :
109- pass
110- super (Analyzer , self ).report ({
111- 'success' : True ,
112- 'summary' : summary ,
113- 'artifacts' : self .artifacts (full_report ),
114- 'operations' : operation_list ,
115- 'full' : full_report
116- }, ensure_ascii )
120+ pass # nosec B110
121+ super (Analyzer , self ).report (
122+ {
123+ "success" : True ,
124+ "summary" : summary ,
125+ "artifacts" : self .artifacts (full_report ),
126+ "operations" : operation_list ,
127+ "full" : full_report ,
128+ },
129+ ensure_ascii ,
130+ )
117131
118132 def run (self ):
119133 """Overwritten by analyzers"""
120134 pass
121135
122136 # Not breaking compatibility
123137 def notSupported (self ):
124- self .error (' This datatype is not supported by this analyzer.' )
138+ self .error (" This datatype is not supported by this analyzer." )
125139
126140 # Not breaking compatibility
127141 def unexpectedError (self , e ):
128- self .error (' Unexpected Error: ' + str (e ))
142+ self .error (" Unexpected Error: " + str (e ))
129143
130144 # Not breaking compatibility
131145 def getData (self ):
132- """For not breaking compatibility to cortexutils.analyzer, this wraps get_data()"""
146+ """Wrapper of get_data.
147+
148+ For not breaking compatibility to cortexutils.analyzer.
149+ """
133150 return self .get_data ()
134151
135152 # Not breaking compatibility
136153 def getParam (self , name , default = None , message = None ):
137- """For not breaking compatibility to cortexutils.analyzer, this wraps get_param()"""
154+ """Wrapper for get_param.
155+
156+ For not breaking compatibility to cortexutils.analyzer.
157+ """
138158 return self .get_param (name = name , default = default , message = message )
139159
140160 # Not breaking compatibility
0 commit comments