forked from smicallef/spiderfoot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfp_bgpview.py
249 lines (185 loc) · 7.29 KB
/
sfp_bgpview.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# -*- coding: utf-8 -*-
# -------------------------------------------------------------------------------
# Name: sfp_bgpview
# Purpose: Query BGPView API - https://bgpview.docs.apiary.io/
#
# Author: <[email protected]>
#
# Created: 2019-09-03
# Copyright: (c) bcoles 2019
# Licence: MIT
# -------------------------------------------------------------------------------
import json
import time
from spiderfoot import SpiderFootEvent, SpiderFootPlugin
class sfp_bgpview(SpiderFootPlugin):
meta = {
'name': "BGPView",
'summary': "Obtain network information from BGPView API.",
'flags': [],
'useCases': ["Investigate", "Footprint", "Passive"],
'categories': ["Search Engines"],
'dataSource': {
'website': "https://bgpview.io/",
'model': "FREE_NOAUTH_UNLIMITED",
'references': [
"https://bgpview.docs.apiary.io/#",
"https://bgpview.docs.apiary.io/api-description-document"
],
'favIcon': "https://bgpview.io/favicon-32x32.png",
'logo': "https://bgpview.io/assets/logo.png",
'description': "BGPView is a simple API allowing consumers to view all sort of analytics data about the current state and structure of the internet.",
}
}
opts = {
}
optdescs = {
}
results = None
errorState = False
def setup(self, sfc, userOpts=dict()):
self.sf = sfc
self.results = self.tempStorage()
for opt in list(userOpts.keys()):
self.opts[opt] = userOpts[opt]
def watchedEvents(self):
return [
'IP_ADDRESS',
'IPV6_ADDRESS',
'BGP_AS_MEMBER',
'NETBLOCK_MEMBER',
'NETBLOCKV6_MEMBER'
]
def producedEvents(self):
return [
'BGP_AS_MEMBER',
'NETBLOCK_MEMBER',
'NETBLOCKV6_MEMBER',
'PHYSICAL_ADDRESS',
'RAW_RIR_DATA'
]
def queryAsn(self, qry):
res = self.sf.fetchUrl("https://api.bgpview.io/asn/" + qry.replace('AS', ''),
useragent=self.opts['_useragent'],
timeout=self.opts['_fetchtimeout'])
time.sleep(1)
if res['content'] is None:
return None
try:
json_data = json.loads(res['content'])
except Exception as e:
self.debug(f"Error processing JSON response from BGPView: {e}")
return None
if json_data.get('status') != 'ok':
self.debug("No results found for ASN " + qry)
return None
data = json_data.get('data')
if not data:
self.debug("No results found for ASN " + qry)
return None
return data
def queryIp(self, qry):
res = self.sf.fetchUrl("https://api.bgpview.io/ip/" + qry,
useragent=self.opts['_useragent'],
timeout=self.opts['_fetchtimeout'])
time.sleep(1)
if res['content'] is None:
return None
try:
json_data = json.loads(res['content'])
except Exception as e:
self.debug(f"Error processing JSON response from BGPView: {e}")
return None
if json_data.get('status') != 'ok':
self.debug("No results found for IP address " + qry)
return None
data = json_data.get('data')
if not data:
self.debug("No results found for IP address " + qry)
return None
return data
def queryNetblock(self, qry):
res = self.sf.fetchUrl("https://api.bgpview.io/prefix/" + qry,
useragent=self.opts['_useragent'],
timeout=self.opts['_fetchtimeout'])
time.sleep(1)
if res['content'] is None:
return None
try:
json_data = json.loads(res['content'])
except Exception as e:
self.debug(f"Error processing JSON response from BGPView: {e}")
return None
if json_data.get('status') != 'ok':
self.debug("No results found for netblock " + qry)
return None
data = json_data.get('data')
if not data:
self.debug("No results found for netblock " + qry)
return None
return data
def handleEvent(self, event):
eventName = event.eventType
srcModuleName = event.module
eventData = event.data
if self.errorState:
return
self.debug(f"Received event, {eventName}, from {srcModuleName}")
if eventData in self.results:
self.debug(f"Skipping {eventData}, already checked.")
return
self.results[eventData] = True
if eventName == 'BGP_AS_MEMBER':
data = self.queryAsn(eventData)
if not data:
self.info("No results found for ASN " + eventData)
return
e = SpiderFootEvent('RAW_RIR_DATA', str(data), self.__name__, event)
self.notifyListeners(e)
address = data.get('owner_address')
if not address:
return
evt = SpiderFootEvent('PHYSICAL_ADDRESS', ', '.join([_f for _f in address if _f]), self.__name__, event)
self.notifyListeners(evt)
if eventName in ['NETBLOCK_MEMBER', 'NETBLOCKV6_MEMBER']:
data = self.queryNetblock(eventData)
if not data:
self.info("No results found for netblock " + eventData)
return
e = SpiderFootEvent('RAW_RIR_DATA', str(data), self.__name__, event)
self.notifyListeners(e)
address = data.get('owner_address')
if not address:
return
evt = SpiderFootEvent('PHYSICAL_ADDRESS', ', '.join([_f for _f in address if _f]), self.__name__, event)
self.notifyListeners(evt)
if eventName in ['IP_ADDRESS', 'IPV6_ADDRESS']:
data = self.queryIp(eventData)
if not data:
self.info("No results found for IP address " + eventData)
return
e = SpiderFootEvent('RAW_RIR_DATA', str(data), self.__name__, event)
self.notifyListeners(e)
prefixes = data.get('prefixes')
if not prefixes:
self.info("No prefixes found for IP address " + eventData)
return
for prefix in prefixes:
p = prefix.get('prefix')
if not p:
continue
if not prefix.get('asn'):
continue
asn = prefix.get('asn').get('asn')
if not asn:
continue
self.info(f"Netblock found: {p} ({asn})")
evt = SpiderFootEvent("BGP_AS_MEMBER", str(asn), self.__name__, event)
self.notifyListeners(evt)
if self.sf.validIpNetwork(p):
if ":" in p:
evt = SpiderFootEvent("NETBLOCKV6_MEMBER", p, self.__name__, event)
else:
evt = SpiderFootEvent("NETBLOCK_MEMBER", p, self.__name__, event)
self.notifyListeners(evt)
# End of sfp_bgpview class