1
+ import traceback
2
+ import logging
3
+ import requests
4
+
5
+ from api_app .exceptions import AnalyzerRunException
6
+ from api_app .script_analyzers import general
7
+ from intel_owl import secrets
8
+
9
+ logger = logging .getLogger (__name__ )
10
+
11
+ base_url = 'https://www.censys.io/api/v1'
12
+
13
+
14
+ def run (analyzer_name , job_id , observable_name , observable_classification , additional_config_params ):
15
+ logger .info ("started analyzer {} job_id {} observable {}"
16
+ "" .format (analyzer_name , job_id , observable_name ))
17
+ report = general .get_basic_report_template (analyzer_name )
18
+ try :
19
+ api_id_name = additional_config_params .get ('api_id_name' , '' )
20
+ api_secret_name = additional_config_params .get ('api_secret_name' , '' )
21
+ if not api_id_name :
22
+ api_id_name = "CENSYS_API_ID"
23
+ api_secret_name = "CENSYS_API_SECRET"
24
+ api_id = secrets .get_secret (api_id_name )
25
+ api_secret = secrets .get_secret (api_secret_name )
26
+ if not (api_id and api_secret ):
27
+ raise AnalyzerRunException ("no api credentials retrieved" )
28
+
29
+ result = _censys_get_report ((api_id , api_secret ), observable_name , observable_classification ,
30
+ additional_config_params )
31
+
32
+ # pprint.pprint(result)
33
+ report ['report' ] = result
34
+ except AnalyzerRunException as e :
35
+ error_message = "job_id:{} analyzer:{} observable_name:{} Analyzer error {}" \
36
+ "" .format (job_id , analyzer_name , observable_name , e )
37
+ logger .error (error_message )
38
+ report ['errors' ].append (error_message )
39
+ report ['success' ] = False
40
+ except Exception as e :
41
+ traceback .print_exc ()
42
+ error_message = "job_id:{} analyzer:{} observable_name:{} Unexpected error {}" \
43
+ "" .format (job_id , analyzer_name , observable_name , e )
44
+ logger .exception (error_message )
45
+ report ['errors' ].append (str (e ))
46
+ report ['success' ] = False
47
+ else :
48
+ report ['success' ] = True
49
+
50
+ general .set_report_and_cleanup (job_id , report )
51
+
52
+ logger .info ("ended analyzer {} job_id {} observable {}"
53
+ "" .format (analyzer_name , job_id , observable_name ))
54
+
55
+ return report
56
+
57
+
58
+ def _censys_get_report (api_creds , observable_name , observable_classification , additional_config_params ):
59
+ censys_analysis = additional_config_params .get ('censys_analysis' , 'search' )
60
+ if censys_analysis == 'search' :
61
+ uri = '/view/ipv4/{}' .format (observable_name )
62
+ else :
63
+ raise AnalyzerRunException ("not supported observable type {}. Supported is IP"
64
+ "" .format (observable_classification ))
65
+ try :
66
+ response = requests .get (base_url + uri , auth = api_creds )
67
+ response .raise_for_status ()
68
+ except requests .RequestException as e :
69
+ raise AnalyzerRunException (e )
70
+ result = response .json ()
71
+ return result
0 commit comments