SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.
Python attacksurfacecticybersecurityfootprintinghackinginformation-gatheringinformation-securityinfosecintelligence-gatheringosintosint-frameworkosint-reconnaissanceosint-toolpentestingpythonreconsecurity-toolsthreat-intelligencethreatintel
Cleaned up sfp_ripe and renamed.
smicallefd55bdadfe95c
diff --git a/modules/sfp_ripe.py b/modules/sfp_ripe.pydeleted file mode 100644index 3455399c..00000000--- a/modules/sfp_ripe.py+++ /dev/null@@ -1,254 +0,0 @@-#--------------------------------------------------------------------------------# Name: sfp_ripe-# Purpose: Some RIPE (http://stat.ripe.net) queries to get netblocks owned-# and other bits of info.-#-# Author: Steve Micallef <[email protected]>-#-# Created: 11/03/2013-# Copyright: (c) Steve Micallef 2013-# Licence: GPL-#---------------------------------------------------------------------------------import sys-import re-import json-import socket-from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent--# SpiderFoot standard lib (must be initialized in setup)-sf = None--class sfp_ripe(SpiderFootPlugin):- """RIPE Query:Queries RIPE to identify netblocks and other info."""-- # Default options- opts = { }-- # Target- baseDomain = None- results = dict()-- def setup(self, sfc, target, userOpts=dict()):- global sf-- sf = sfc- self.baseDomain = target- self.results = dict()-- for opt in userOpts.keys():- self.opts[opt] = userOpts[opt]-- # What events is this module interested in for input- def watchedEvents(self):- return ['IP_ADDRESS', 'SUBDOMAIN']-- # What events this module produces- # This is to support the end user in selecting modules based on events- # produced.- def producedEvents(self):- return [ "AFFILIATE", "NETBLOCK", "RAW_RIPE_DATA",- "BGP_AS", "PROVIDER_INTERNET" ]-- # Handle events sent to this module- def handleEvent(self, event):- eventName = event.eventType- srcModuleName = event.module- eventData = event.data-- sf.debug("Received event, " + eventName + ", from " + srcModuleName)-- # Don't look up stuff twice- if self.results.has_key(eventData):- sf.debug("Skipping " + eventData + " as already mapped.")- return None- else:- self.results[eventData] = True-- if eventName == 'SUBDOMAIN':- res = sf.fetchUrl("http://stat.ripe.net/data/dns-chain/data.json?resource=" + \- eventData, timeout=self.opts['_fetchtimeout'], useragent=self.opts['_useragent'])- if res['content'] == None:- sf.info("No RIPE info found/available for " + eventData)- return None-- try:- j = json.loads(res['content'])- except Exception as e:- sf.debug("Error processing JSON response.")- return None-- nslist = j["data"]["authoritative_nameservers"]- for ns in nslist:- nsclean = ns.rstrip('.').lower()- if not nsclean.endswith(self.baseDomain):- evt = SpiderFootEvent("AFFILIATE", nsclean, self.__name__, event)- self.notifyListeners(evt)-- # Just send the content off for others to process- evt = SpiderFootEvent("RAW_RIPE_DATA", res['content'], self.__name__, event)- self.notifyListeners(evt)- return None-- # First get the netblock the IP resides on- res = sf.fetchUrl("http://stat.ripe.net/data/network-info/data.json?resource=" + \- eventData, timeout=self.opts['_fetchtimeout'], useragent=self.opts['_useragent'])- if res['content'] == None:- sf.info("No RIPE info found/available for " + eventData)- return None-- try:- j = json.loads(res['content'])- except Exception as e:- sf.debug("Error processing JSON response.")- return None-- prefix = j["data"]["prefix"]- if prefix == None:- sf.debug("Could not identify network prefix.")- return None-- # Now see who owns the prefix- res = sf.fetchUrl("http://stat.ripe.net/data/whois/data.json?resource=" + \- prefix, timeout=self.opts['_fetchtimeout'], useragent=self.opts['_useragent'])- if res['content'] == None:- sf.info("No RIPE info found/available for prefix: " + prefix)- return None-- keyword = sf.domainKeyword(self.baseDomain)- content = res['content'].lower()- #print keyword + " and " + self.baseDomain + " in: " + res['content']- # Crude and probably prone to a lot of false positives. Need to revisit.- if self.baseDomain in content or "\"" + keyword in content \- or keyword +"\"" in content or keyword +"-" in content \- or "-"+keyword in content:- sf.info("Owned netblock found: " + prefix)- evt = SpiderFootEvent("NETBLOCK", prefix, self.__name__, event)- self.notifyListeners(evt)- evt = SpiderFootEvent("RAW_RIPE_DATA", res['content'], self.__name__, event)- self.notifyListeners(evt)-- # We now have a netblock owned by our target, we want to find- # the AS hop before the AS owning this netblock to find the ISP.- # 1. Parse the AS out of res['content']- try:- j = json.loads(res['content'])- data = j["data"]["irr_records"][0]- except Exception as e:- sf.debug("Error processing JSON response.")- return None- asn = None- for rec in data:- if rec["key"] == "origin":- asn = rec["value"]- break-- # 2. Find all the netblocks owned by this AS- if asn != None:- res = sf.fetchUrl("http://stat.ripe.net/data/announced-prefixes/data.json?resource=" + \- "AS" + str(asn), timeout=self.opts['_fetchtimeout'], useragent=self.opts['_useragent'])- if res['content'] == None:- sf.info("No RIPE info found/available for AS" + asn)- else:- evt = SpiderFootEvent("BGP_AS", asn, self.__name__, event)- self.notifyListeners(evt)- try:- j = json.loads(res['content'])- data = j["data"]["prefixes"]- except Exception as e:- sf.debug("Error processing JSON response.")- return None- for rec in data:- pfx = rec["prefix"]- sf.info("Additional netblock found from same AS: " + pfx)- evt = SpiderFootEvent("NETBLOCK", pfx, self.__name__, event)- self.notifyListeners(evt)-- # 3. Find all the AS neighbours (the last hop before our target)- res = sf.fetchUrl("http://stat.ripe.net/data/asn-neighbours/data.json?resource=" + \- "AS" + str(asn), timeout=self.opts['_fetchtimeout'], useragent=self.opts['_useragent'])- if res['content'] == None:- sf.info("No RIPE info found/available for AS" + asn)- else:- try:- j = json.loads(res['content'])- data = j["data"]["neighbours"]- except Exception as e:- sf.debug("Error processing JSON response.")- return None- for rec in data:- nasn = rec['asn']- res = sf.fetchUrl("http://stat.ripe.net/data/whois/data.json?resource=" + \- str(nasn), timeout=self.opts['_fetchtimeout'], useragent=self.opts['_useragent'])- if res['content'] == None:- sf.info("No RIPE info found/available for prefix: " + prefix)- return None- try:- j = json.loads(res['content'])- data = j["data"]["records"]- except Exception as e:- sf.debug("Error processing JSON response.")- return None-- name = None- for rec in data:- for rrec in rec:- if rrec['key'] == "descr":- name = rrec['value']- break- if rrec['key'] == "OrgName":- name = rrec['value']- break-- if name != None:- break-- if name != None:- evt = SpiderFootEvent("PROVIDER_INTERNET", name,- self.__name__, event)- self.notifyListeners(evt)- else:- # If they don't own the netblock they are serving from, then- # the netblock owner is their Internet provider.- try:- j = json.loads(res['content'])- data = j["data"]["irr_records"][0]- except Exception as e:- sf.debug("Error processing JSON response.")- return None- for rec in data:- if rec['key'] == "descr":- evt = SpiderFootEvent("PROVIDER_INTERNET", rec['value'],- self.__name__, event)- self.notifyListeners(evt)- break-- return None-- def start(self):- res = sf.fetchUrl("http://stat.ripe.net/data/dns-chain/data.json?resource=" + \- self.baseDomain, timeout=self.opts['_fetchtimeout'],- useragent=self.opts['_useragent'])- if res['content'] == None:- sf.info("No RIPE info found/available for " + self.baseDomain)- return None-- try:- j = json.loads(res['content'])- except Exception as e:- sf.debug("Error processing JSON response.")- return None-- nslist = j["data"]["authoritative_nameservers"]- for ns in nslist:- nsclean = ns.rstrip('.').lower()- if not nsclean.endswith(self.baseDomain):- evt = SpiderFootEvent("AFFILIATE", nsclean, self.__name__)- self.notifyListeners(evt)-- # Just send the content off for others to process- evt = SpiderFootEvent("RAW_RIPE_DATA", res['content'], self.__name__)- self.notifyListeners(evt)- return None--# End of sfp_ripe classdiff --git a/modules/sfp_rir.py b/modules/sfp_rir.pynew file mode 100644index 00000000..73e97610--- /dev/null+++ b/modules/sfp_rir.py@@ -0,0 +1,241 @@+#-------------------------------------------------------------------------------+# Name: sfp_rir+# Purpose: Queries Internet registryes like RIPE and ARIN to get netblocks+# and other bits of info.+#+# Author: Steve Micallef <[email protected]>+#+# Created: 8/12/2013+# Copyright: (c) Steve Micallef 2013+# Licence: GPL+#-------------------------------------------------------------------------------++import sys+import re+import json+import socket+from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent++# SpiderFoot standard lib (must be initialized in setup)+sf = None++class sfp_rir(SpiderFootPlugin):+ """Internet Registries:Queries RIPE and ARIN to identify netblocks and other info."""++ # Default options+ opts = { }++ # Target+ baseDomain = None+ results = dict()+ currentEventSrc = None++ def setup(self, sfc, target, userOpts=dict()):+ global sf++ sf = sfc+ self.baseDomain = target+ self.results = dict()++ for opt in userOpts.keys():+ self.opts[opt] = userOpts[opt]++ # What events is this module interested in for input+ def watchedEvents(self):+ return ['IP_ADDRESS']++ # What events this module produces+ # This is to support the end user in selecting modules based on events+ # produced.+ def producedEvents(self):+ return [ "NETBLOCK", "RAW_RIR_DATA", "BGP_AS", "PROVIDER_INTERNET" ]++ # Fetch content and notify of the raw data+ def fetchRir(self, url):+ res = sf.fetchUrl(url, timeout=self.opts['_fetchtimeout'],+ useragent=self.opts['_useragent'])++ if res['content'] != None:+ evt = SpiderFootEvent("RAW_RIR_DATA", res['content'], self.__name__,+ self.currentEventSrc)+ self.notifyListeners(evt)++ 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)+ if res['content'] == None:+ sf.debug("No Netblock info found/available for " + ipaddr + " at RIPE.")+ return None++ try:+ j = json.loads(res['content'])+ except Exception as e:+ sf.debug("Error processing JSON response.")+ return None++ prefix = j["data"]["prefix"]+ if prefix == None:+ sf.debug("Could not identify network prefix.")+ return None++ return prefix++ # Get the AS owning the netblock+ def netblockAs(self, prefix):+ asn = None++ res = self.fetchRir("https://stat.ripe.net/data/whois/data.json?resource=" + prefix)+ if res['content'] == None:+ sf.debug("No AS info found/available for prefix: " + prefix + " at RIPE.")+ return None++ try:+ j = json.loads(res['content'])+ data = j["data"]["irr_records"][0]+ except Exception as e:+ sf.debug("Error processing JSON response.")+ return None++ for rec in data:+ if rec["key"] == "origin":+ asn = rec["value"]+ break++ return str(asn)++ # Owner information about an AS+ def asOwnerInfo(self, asn):+ ownerinfo = dict()++ res = self.fetchRir("https://stat.ripe.net/data/whois/data.json?resource=" + asn)+ if res['content'] == None:+ sf.debug("No info found/available for ASN: " + asn + " at RIPE.")+ return None++ try:+ j = json.loads(res['content'])+ data = j["data"]["records"]+ except Exception as e:+ sf.debug("Error processing JSON response.")+ return None++ for rec in data:+ for d in rec:+ if d["key"].startswith("Org") or d["key"].startswith("AS"):+ ownerinfo[d["key"]] = d["value"]++ 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)+ if res['content'] == None:+ sf.debug("No netblocks info found/available for AS" + asn + " at RIPE.")+ return None++ try:+ j = json.loads(res['content'])+ data = j["data"]["prefixes"]+ except Exception as e:+ sf.debug("Error processing JSON response.")+ return None++ for rec in data:+ netblocks.append(rec["prefix"])+ sf.info("Additional netblock found from same AS: " + rec["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)+ if res['content'] == None:+ sf.debug("No neighbour info found/available for AS" + asn + " at RIPE.")+ return None++ try:+ j = json.loads(res['content'])+ data = j["data"]["neighbours"]+ except Exception as e:+ sf.debug("Error processing JSON response.")+ return None++ for rec in data:+ neighbours.append(str(rec['asn']))++ return neighbours++ # Handle events sent to this module+ def handleEvent(self, event):+ eventName = event.eventType+ srcModuleName = event.module+ eventData = event.data+ self.currentEventSrc = event++ sf.debug("Received event, " + eventName + ", from " + srcModuleName)++ # Don't look up stuff twice+ if self.results.has_key(eventData):+ sf.debug("Skipping " + eventData + " as already mapped.")+ return None+ else:+ self.results[eventData] = True++ prefix = self.ipNetblock(eventData)+ if prefix == None:+ sf.debug("Could not identify network prefix.")+ return None++ asn = self.netblockAs(prefix)+ if asn == None:+ sf.debug("Could not identify netblock AS.")+ return None++ ownerinfo = self.asOwnerInfo(asn)+ keyword = sf.domainKeyword(self.baseDomain).lower()+ owned = False+ for k in ownerinfo.keys():+ item = ownerinfo[k]+ if self.baseDomain in item or "\"" + keyword in item \+ or keyword +"\"" in item or keyword +"-" in item \+ or "-"+keyword in item:+ owned = True++ if owned:+ sf.info("Owned netblock found: " + prefix + "(" + asn + ")")+ evt = SpiderFootEvent("NETBLOCK", prefix, self.__name__, event)+ self.notifyListeners(evt)+ evt = SpiderFootEvent("BGP_AS", asn, self.__name__, event)+ self.notifyListeners(evt)++ # 2. Find all the netblocks owned by this AS+ netblocks = self.asNetblocks(asn)+ for netblock in netblocks:+ evt = SpiderFootEvent("NETBLOCK", netblock, self.__name__, event)+ self.notifyListeners(evt)++ neighs = self.asNeighbours(asn)+ for nasn in neighs:+ ownerinfo = self.asOwnerInfo(nasn)+ if len(ownerinfo) > 0:+ evt = SpiderFootEvent("PROVIDER_INTERNET", str(ownerinfo),+ self.__name__, event)+ self.notifyListeners(evt)+ else:+ # If they don't own the netblock they are serving from, then+ # the netblock owner is their Internet provider.+ evt = SpiderFootEvent("PROVIDER_INTERNET", str(ownerinfo),+ self.__name__, event)+ self.notifyListeners(evt)++ return None++# End of sfp_rir class