-
Notifications
You must be signed in to change notification settings - Fork 385
/
Copy pathgreynoise.py
executable file
·142 lines (111 loc) · 4.51 KB
/
greynoise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
from collections import defaultdict, OrderedDict
from cortexutils.analyzer import Analyzer
class GreyNoiseAnalyzer(Analyzer):
"""
GreyNoise API docs: https://github.com/GreyNoise-Intelligence/api.greynoise.io
"""
@staticmethod
def _get_level(current_level, new_classification):
"""
Map GreyNoise classifications to Cortex maliciousness levels.
Accept a Cortex level and a GreyNoise classification, the return the more malicious of the two.
:param current_level: A Cortex maliciousness level
https://github.com/TheHive-Project/CortexDocs/blob/master/api/how-to-create-an-analyzer.md#output
:param new_classification: A classification field value from a GreyNoise record
https://github.com/GreyNoise-Intelligence/api.greynoise.io#v1queryip
:return: The more malicious of the 2 submitted values as a Cortex maliciousness level
"""
classification_level_map = OrderedDict([
('info', 'info'),
('benign', 'safe'),
('suspicious', 'suspicious'),
('malicious', 'malicious')
])
levels = classification_level_map.values()
new_level = classification_level_map.get(new_classification, 'info')
new_index = levels.index(new_level)
try:
current_index = levels.index(current_level)
except ValueError: # There is no existing level
current_index = -1
return new_level if new_index > current_index else current_level
def run(self):
if self.data_type == "ip":
api_key = self.get_param('config.key', None)
url = 'https://api.greynoise.io/v2/experimental/gnql?query=ip:%s' % self.get_data()
if api_key:
headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Key': '%s' % api_key }
else:
headers = {'Content-Type': 'application/x-www-form-urlencoded' }
response = requests.get(url, headers=headers)
if not (200 <= response.status_code < 300):
self.error('Unable to query GreyNoise API\n{}'.format(response.text))
self.report(response.json())
else:
self.notSupported()
def summary(self, raw):
"""
Return one taxonomy summarizing the reported tags
If there is only one tag, use it as the predicate
If there are multiple tags, use "entries" as the predicate
Use the total count as the value
Use the most malicious level found
Examples:
Input
{
"actor": SCANNER1,
"classification": ""
}
Output
GreyNoise:SCANNER1 = 1 (info)
Input
{
"actor": SCANNER1,
"classification": "malicious"
},
{
"classification": "benign"
}
Output
GreyNoise:SCANNER1 = 2 (malicious)
Input
{
"actor": SCANNER1,
"classification": ""
},
{
"actor": SCANNER1,
"classification": "safe"
},
{
"actor": SCANNER2,
"classification": ""
}
Output
GreyNoise:entries = 3 (safe)
"""
try:
taxonomies = []
if raw.get('data'):
final_level = None
taxonomy_data = defaultdict(int)
for record in raw.get('data', []):
actor = record.get('actor', 'unknown')
classification = record.get('classification', 'unknown')
taxonomy_data[actor] += 1
final_level = self._get_level(final_level, classification)
if len(taxonomy_data) > 1: # Multiple tags have been found
taxonomies.append(self.build_taxonomy(final_level, 'GreyNoise', 'entries', len(taxonomy_data)))
else: # There is only one tag found, possibly multiple times
for actor, count in taxonomy_data.iteritems():
taxonomies.append(self.build_taxonomy(final_level, 'GreyNoise', actor, count))
else:
taxonomies.append(self.build_taxonomy('info', 'GreyNoise', 'Records', 'None'))
return {"taxonomies": taxonomies}
except Exception as e:
self.error('Summary failed\n{}'.format(e.message))
if __name__ == '__main__':
GreyNoiseAnalyzer().run()