Skip to content

SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.

sfp_ripe: Cleanup (#1373)

bcoles61af49668470
diff --git a/modules/sfp_ripe.py b/modules/sfp_ripe.py
index c77a4ea2..f9cf4574 100644
--- a/modules/sfp_ripe.py
+++ b/modules/sfp_ripe.py
@@ -72,8 +72,6 @@ class sfp_ripe(SpiderFootPlugin):
'BGP_AS_OWNER', 'BGP_AS_MEMBER']
# What events this module produces
- # This is to support the end user in selecting modules based on events
- # produced.
def producedEvents(self):
return ["NETBLOCK_MEMBER", "NETBLOCK_OWNER", "BGP_AS_MEMBER",
"RAW_RIR_DATA", "BGP_AS_OWNER"]
@@ -81,22 +79,23 @@ class sfp_ripe(SpiderFootPlugin):
# Fetch content and notify of the raw data
def fetchRir(self, url):
if url in self.memCache:
- res = self.memCache[url]
- else:
- res = self.sf.fetchUrl(url, timeout=self.opts['_fetchtimeout'],
- useragent=self.opts['_useragent'])
- if res['content'] is not None:
- self.memCache[url] = res
- self.lastContent = res['content']
+ return self.memCache[url]
+
+ res = self.sf.fetchUrl(url, timeout=self.opts['_fetchtimeout'],
+ useragent=self.opts['_useragent'])
+ if res['content'] is not None:
+ self.memCache[url] = res
+ self.lastContent = res['content']
+
return res
# Get the netblock the IP resides in
def ipNetblock(self, ipaddr):
prefix = None
- res = self.fetchRir("https://stat.ripe.net/data/network-info/data.json?resource=" + ipaddr)
+ res = self.fetchRir(f"https://stat.ripe.net/data/network-info/data.json?resource={ipaddr}")
if res['content'] is None:
- self.sf.debug("No Netblock info found/available for " + ipaddr + " at RIPE.")
+ self.sf.debug(f"No netblock info found/available for {ipaddr} at RIPE.")
return None
try:
@@ -112,71 +111,87 @@ class sfp_ripe(SpiderFootPlugin):
return prefix
+ # Query WHOIS data
+ def queryWhois(self, qry):
+ res = self.fetchRir(f"https://stat.ripe.net/data/whois/data.json?resource={qry}")
+ if res['content'] is None:
+ self.sf.debug(f"No results for {qry} at RIPE.")
+ return None
+
+ try:
+ data = json.loads(res['content'])
+ return data.get("data")
+ except Exception as e:
+ self.sf.debug(f"Error processing JSON response: {e}")
+
+ return None
+
# Get the AS owning the netblock
def netblockAs(self, prefix):
- asn = None
+ whois = self.queryWhois(prefix)
- res = self.fetchRir("https://stat.ripe.net/data/whois/data.json?resource=" + prefix)
- if res['content'] is None:
- self.sf.debug("No AS info found/available for prefix: " + prefix + " at RIPE.")
+ if not whois:
return None
try:
- j = json.loads(res['content'])
- if len(j["data"]["irr_records"]) > 0:
- data = j["data"]["irr_records"][0]
+ if len(whois["irr_records"]) > 0:
+ data = whois["irr_records"][0]
else:
- data = j["data"]["records"][0]
+ data = whois["records"][0]
except Exception as e:
self.sf.debug(f"Error processing JSON response: {e}")
return None
+ asn = None
for rec in data:
if rec["key"] == "origin":
asn = rec["value"]
break
- if asn is None:
+ if not asn:
return None
return str(asn)
# Owner information about an AS
def asOwnerInfo(self, asn):
- ownerinfo = dict()
- # Which keys to look for ownership information in (prefix)
- ownerkeys = ["as", "value", "auth", "desc", "org", "mnt", "admin", "tech"]
+ whois = self.queryWhois(asn)
- res = self.fetchRir("https://stat.ripe.net/data/whois/data.json?resource=" + asn)
- if res['content'] is None:
- self.sf.debug("No info found/available for ASN: " + asn + " at RIPE.")
+ if not whois:
return None
try:
- j = json.loads(res['content'])
- data = j["data"]["records"]
+ data = whois["records"]
except Exception as e:
self.sf.debug(f"Error processing JSON response: {e}")
return None
+ ownerinfo = dict()
+ # Which keys to look for ownership information in (prefix)
+ ownerkeys = ["as", "value", "auth", "desc", "org", "mnt", "admin", "tech"]
+
for rec in data:
for d in rec:
for k in ownerkeys:
- if d['key'].lower().startswith(k):
- if d["value"].lower() not in ["null", "none", "none specified"]:
- if d["key"] in ownerinfo:
- ownerinfo[d["key"]].append(d["value"])
- else:
- ownerinfo[d["key"]] = [d["value"]]
-
- self.sf.debug("Returning ownerinfo: " + str(ownerinfo))
+ key = d['key']
+ value = d['value']
+ if not key.lower().startswith(k):
+ continue
+
+ if value.lower() in ["null", "none", "none specified"]:
+ continue
+
+ if key in ownerinfo:
+ ownerinfo[key].append(value)
+ else:
+ ownerinfo[key] = [value]
+
+ self.sf.debug(f"Found AS owner info: {ownerinfo}")
return ownerinfo
# Netblocks owned by an AS
def asNetblocks(self, asn):
- netblocks = list()
-
- res = self.fetchRir("https://stat.ripe.net/data/announced-prefixes/data.json?resource=AS" + asn)
+ res = self.fetchRir(f"https://stat.ripe.net/data/announced-prefixes/data.json?resource=AS{asn}")
if res['content'] is None:
self.sf.debug(f"No netblocks info found/available for AS{asn} at RIPE.")
return None
@@ -188,17 +203,17 @@ class sfp_ripe(SpiderFootPlugin):
self.sf.debug(f"Error processing JSON response: {e}")
return None
+ netblocks = list()
for rec in data:
+ prefix = rec["prefix"]
netblocks.append(rec["prefix"])
- self.sf.info("Additional netblock found from same AS: " + rec["prefix"])
+ self.sf.info(f"Additional netblock found from same AS: {prefix}")
return netblocks
# Neighbours to an AS
def asNeighbours(self, asn):
- neighbours = list()
-
- res = self.fetchRir("https://stat.ripe.net/data/asn-neighbours/data.json?resource=AS" + asn)
+ res = self.fetchRir(f"https://stat.ripe.net/data/asn-neighbours/data.json?resource=AS{asn}")
if res['content'] is None:
self.sf.debug(f"No neighbour info found/available for AS{asn} at RIPE.")
return None
@@ -210,6 +225,7 @@ class sfp_ripe(SpiderFootPlugin):
self.sf.debug(f"Error processing JSON response: {e}")
return None
+ neighbours = list()
for rec in data:
neighbours.append(str(rec['asn']))
@@ -288,26 +304,30 @@ class sfp_ripe(SpiderFootPlugin):
if eventName == "BGP_AS_OWNER":
# Don't report additional netblocks from this AS if we've
# already found this AS before.
- if eventData not in self.nbreported:
- # Find all the netblocks owned by this AS
- self.nbreported[eventData] = True
- netblocks = self.asNetblocks(eventData)
- if netblocks is not None:
- for netblock in netblocks:
- if netblock in self.results:
- continue
-
- # Technically this netblock was identified via the AS, not
- # the original IP event, so link it to asevt, not event.
- # Skip IPv6 for now
- if ":" in netblock:
- continue
- evt = SpiderFootEvent("NETBLOCK_OWNER", netblock,
- self.__name__, event)
- self.notifyListeners(evt)
- evt = SpiderFootEvent("RAW_RIR_DATA", self.lastContent, self.__name__,
- event)
- self.notifyListeners(evt)
+ if eventData in self.nbreported:
+ return
+
+ # Find all the netblocks owned by this AS
+ self.nbreported[eventData] = True
+ netblocks = self.asNetblocks(eventData)
+ if not netblocks:
+ return
+
+ for netblock in netblocks:
+ if netblock in self.results:
+ continue
+
+ # TODO: Support IPv6
+ if ":" in netblock:
+ continue
+
+ # Technically this netblock was identified via the AS, not
+ # the original IP event, so link it to asevt, not event.
+ evt = SpiderFootEvent("NETBLOCK_OWNER", netblock, self.__name__, event)
+ self.notifyListeners(evt)
+
+ evt = SpiderFootEvent("RAW_RIR_DATA", self.lastContent, self.__name__, event)
+ self.notifyListeners(evt)
return
@@ -316,14 +336,13 @@ class sfp_ripe(SpiderFootPlugin):
# Get the BGP AS the netblock is a part of
asn = self.netblockAs(eventData)
if asn is None:
- self.sf.debug("Could not identify BGP AS for " + eventData)
+ self.sf.debug(f"Could not identify BGP AS for {eventData}")
return
if eventName == "NETBLOCK_OWNER" and self.ownsAs(asn):
asevt = SpiderFootEvent("BGP_AS_OWNER", asn, self.__name__, event)
self.notifyListeners(asevt)
- evt = SpiderFootEvent("RAW_RIR_DATA", self.lastContent, self.__name__,
- event)
+ evt = SpiderFootEvent("RAW_RIR_DATA", self.lastContent, self.__name__, event)
self.notifyListeners(evt)
else:
asevt = SpiderFootEvent("BGP_AS_MEMBER", asn, self.__name__, event)
@@ -336,17 +355,17 @@ class sfp_ripe(SpiderFootPlugin):
# Get the Netblock the IP is a part of
prefix = self.ipNetblock(eventData)
if prefix is None:
- self.sf.debug("Could not identify network prefix for " + eventData)
+ self.sf.debug(f"Could not identify network prefix for {eventData}")
return
# Get the BGP AS the netblock is a part of
asn = self.netblockAs(prefix)
if asn is None:
- self.sf.debug("Could not identify BGP AS for " + prefix)
+ self.sf.debug(f"Could not identify BGP AS for {prefix}")
return
if self.sf.validIpNetwork(prefix):
- self.sf.info("Netblock found: " + prefix + "(" + asn + ")")
+ self.sf.info(f"Netblock found: {prefix} ({asn})")
evt = SpiderFootEvent("NETBLOCK_MEMBER", prefix, self.__name__, event)
self.notifyListeners(evt)