forked from MISP/misp-modules
-
Notifications
You must be signed in to change notification settings - Fork 3
/
iprep.py
executable file
·88 lines (71 loc) · 3.19 KB
/
iprep.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
# -*- coding: utf-8 -*-
import json
import requests
misperrors = {'error': 'Error'}
mispattributes = {'input': ['ip-src', 'ip-dst'], 'output': ['text']}
moduleinfo = {'version': '1.0', 'author': 'Keith Faber',
'description': 'Query IPRep Data for IP Address',
'module-type': ['expansion']}
moduleconfig = ['apikey']
def handler(q=False):
if q is False:
return False
request = json.loads(q)
if request.get('ip-src'):
toquery = request['ip-src']
elif request.get('ip-dst'):
toquery = request['ip-dst']
else:
misperrors['error'] = "Unsupported attributes type"
return misperrors
if not request.get('config') and not request['config'].get('apikey'):
misperrors['error'] = 'IPRep api key is missing'
return misperrors
err, rep = parse_iprep(toquery, request['config'].get('apikey'))
if len(err) > 0:
misperrors['error'] = ','.join(err)
return misperrors
return {'results': rep}
def parse_iprep(ip, api):
meta_fields = ['origin', 'Query_Time', 'created_on', 'IP_Lookup_History', 'IPs_in_collection', '_id', 'disclaimer',
'MaxMind_Free_GeoIP', 'Unique_Lookups', 'query_result']
rep = []
err = []
full_text = ''
url = 'https://www.packetmail.net/iprep.php/%s' % ip
try:
data = requests.get(url, params={'apikey': api}).json()
except Exception:
return ['Error pulling data'], rep
# print '%s' % data
for name, val in data.items():
if name not in meta_fields:
try:
context = val['context']
if type(context) is list:
if context[0].get('alert'):
context = ','.join([hit['alert']['signature'] for hit in context])
elif context[0].get('signature'):
context = ','.join([hit['signature'] for hit in context])
elif context[0].get('target_port') and context[0].get('protocol'):
context = ','.join(
['Port Attacked: %s %s' % (hit['target_port'], hit['protocol']) for hit in context])
elif context[0].get('phishing_kit') and context[0].get('url'):
context = ','.join(['%s (%s)' % (hit['phishing_kit'], hit['url']) for hit in context])
else:
context = ';'.join(['%s: %s' % (k, v) for k, v in context[0].items()])
if val.get('special_note'):
context += '; ' + val['special_note']
misp_val = context
full_text += '\n%s' % context
misp_comment = 'IPRep Source %s: %s' % (name, val['last_seen'])
rep.append({'types': mispattributes['output'], 'categories': ['External analysis'], 'values': misp_val, 'comment': misp_comment})
except Exception:
err.append('Error parsing source: %s' % name)
rep.append({'types': ['freetext'], 'values': full_text, 'comment': 'Free text import of IPRep'})
return err, rep
def introspection():
return mispattributes
def version():
moduleinfo['config'] = moduleconfig
return moduleinfo