-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcymon_analyzer.py
226 lines (171 loc) · 6.69 KB
/
cymon_analyzer.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
# encoding: utf-8
import os
import requests
import json
from cortexutils.analyzer import Analyzer
from urllib import quote_plus
requests.packages.urllib3.disable_warnings()
class CymonApi(object):
def __init__(self, auth_token=None,
endpoint='https://cymon.io:443/api/nexus/v1'):
self.endpoint = endpoint
self.session = requests.Session()
self.session.headers = {
'content-type': 'application/json',
'accept': 'application/json',
}
if auth_token:
self.session.headers.update(
{'Authorization': 'Token {0}'.format(auth_token)})
def get(self, method, params=None):
r = self.session.get(self.endpoint + method, params=params)
r.raise_for_status()
return r
def post(self, method, params, headers=None):
r = self.session.post(self.endpoint + method, data=json.dumps(params),
headers=headers)
r.raise_for_status()
return r
def ip_lookup(self, ip_addr):
r = self.get('/ip/' + ip_addr)
return r.json()
def ip_events(self, ip_addr):
r = self.get('/ip/' + ip_addr + '/events')
return r.json()
def ip_domains(self, ip_addr):
r = self.get('/ip/' + ip_addr + '/domains')
return r.json()
def ip_urls(self, ip_addr):
r = self.get('/ip/' + ip_addr + '/urls')
return r.json()
def domain_lookup(self, name):
r = self.get('/domain/' + name)
return r.json()
def url_lookup(self, location):
r = self.get('/url/' + quote_plus(location))
return r.json()
def ip_blacklist(self, tag, days=1, limit=10, offset=10):
# supported tags: malware, botnet, spam, phishing, dnsbl, blacklist
r = self.get('/blacklist/ip/' + tag + '/?days=%d' % (days) +
'&limit=%d' % (limit) +
'&offset=%d' % (offset))
return r.json()
def domain_blacklist(self, tag, days=1, limit=15, offset=10):
# supported tags: malware, botnet, spam, phishing, dnsbl, blacklist
r = self.get('/blacklist/domain/' + tag + '/?days=%d' % (days) +
'&limit=%d' % (limit) +
'&offset=%d' % (offset))
return r.json()
class CymonEngine():
def __init__(self):
default_file = 'config.json'
self.cymon_cat = ['malware',
'botnet',
'spam',
'phishing',
'malicious activity',
'blacklist',
'dnsbl']
if os.path.isfile(default_file):
conf = self.loadSetting(default_file)
c_token = conf['1']['cymon']['token']
self.api = CymonApi(c_token)
def loadSetting(self, filepath):
with open(filepath, 'rb') as f:
d = json.loads(f.read())
return d
def search(self, ipaddr):
d = {'Founds': {},
'stats': {}
}
total = 0
modo = 'ip_events'
func = getattr(self.api, modo)
req = func(ipaddr)
d['Clear'] = True
for item in self.cymon_cat:
tag_found = False
for elem in req['results']:
if item in elem['tag']:
tag_found = True
if tag_found:
d['Clear'] = False
s = self.api.ip_lookup(ipaddr)['sources']
if 'malicious' in item:
d['stats']['total_malicious'] = len(s)
d['malicious_activity'] = s
d['Founds']['malicious_activity'] = tag_found
else:
d['Founds'][item] = tag_found
d['stats']['total_' + item] = len(s)
d[item] = s
total = total + len(s)
else:
if 'malicious' in item:
d['stats']['total_malicious'] = 0
d['Founds']['malicious_activity'] = tag_found
else:
d['stats']['total_' + item] = 0
d['Founds'][item] = tag_found
d['permalink'] = 'https://cymon.io/' + ipaddr
d['Success'] = True
d['total'] = total
return d
class CymonAnalyzer(Analyzer):
def __init__(self):
Analyzer.__init__(self)
self.service = self.getParam('config.service', None,
'Cymon service is missing')
self.con = CymonEngine()
def summary(self, raw_report):
taxonomy = {"level": "malicious", "namespace": "Cymon.io", "predicate": "Analisis", "value": 0}
taxonomies = []
level = "malicious"
namespace = "Cymon.io"
predicate = "Report"
value = "\"\""
for elm in raw_report["Founds"]:
if raw_report["Founds"][elm]:
if elm in ["spam", "blacklist", "phishing", "dnsbl"]:
level = 'suspicious'
value = "{}".format(elm)
taxonomies.append(self.build_taxonomy(level, namespace, predicate, value))
if raw_report["Clear"]:
value = "{}".format("Clear")
taxonomies.append(self.build_taxonomy("info", namespace, predicate, value))
result = {
'Malware': raw_report['Founds']['malware'],
'Blacklist': raw_report['Founds']['blacklist'],
'MaliciousActivity': raw_report['Founds']['malicious_activity'],
'Dns_blacklist': raw_report['Founds']['dnsbl'],
'Spam': raw_report['Founds']['spam'],
'Phishing': raw_report['Founds']['phishing'],
'Botnet': raw_report['Founds']['botnet'],
'Source_link': raw_report['permalink'],
'count': raw_report['total'],
'stats': raw_report['stats'],
'taxonomies':taxonomies
}
return result
def artifacts(self, raw_report):
result = []
if self.service == 'Check_IP':
pass
return result
def run(self):
Analyzer.run(self)
data = self.getData()
try:
if self.service == 'Check_IP':
if self.data_type == 'ip':
result = self.con.search(data)
self.report(result)
else:
self.notSupported()
except ValueError as e:
self.error('Invalid IP address')
except Exception as e:
self.unexpectedError(type(e))
if __name__ == '__main__':
CymonAnalyzer().run()