SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.
Python attacksurfacecticybersecurityfootprintinghackinginformation-gatheringinformation-securityinfosecintelligence-gatheringosintosint-frameworkosint-reconnaissanceosint-toolpentestingpythonreconsecurity-toolsthreat-intelligencethreatintel
Add GreyNoise Community Module (#1720)
Brad Chiappettae1db49ddec54
diff --git a/modules/sfp_greynoise.py b/modules/sfp_greynoise.pyindex 785f5970..fa06ee28 100644--- a/modules/sfp_greynoise.py+++ b/modules/sfp_greynoise.py@@ -7,7 +7,7 @@# Updated By: Brad Chiappetta, GreyNoise## Created: 20/11/2018-# Updated: 15-Nov-2021+# Updated: 31-Aug-2022# Copyright: (c) Steve Micallef# Licence: MIT# -------------------------------------------------------------------------------@@ -31,15 +31,15 @@ class sfp_greynoise(SpiderFootPlugin):"dataSource": {"website": "https://greynoise.io/","model": "FREE_AUTH_LIMITED",- "references": ["https://docs.greynoise.io/", "https://www.greynoise.io/viz/signup"],+ "references": ["https://docs.greynoise.io/", "https://viz.greynoise.io/signup"],"apiKeyInstructions": [- "Visit https://www.greynoise.io/viz/signup",+ "Visit https://viz.greynoise.io/signup","Sign up for a free account",- "Navigate to https://www.greynoise.io/viz/account/",+ "Navigate to https://viz.greynoise.io/account","The API key is listed under 'API Key'",],- "favIcon": "https://www.greynoise.io/favicon.ico",- "logo": "https://www.greynoise.io/_nuxt/img/greynoise-logo.dccd59d.png",+ "favIcon": "https://viz.greynoise.io/favicon.ico",+ "logo": "https://viz.greynoise.io/_nuxt/img/greynoise-logo.dccd59d.png","description": "At GreyNoise, we collect and analyze untargeted, widespread, ""and opportunistic scan and attack activity that reaches every server directly connected to the Internet. ""Mass scanners (such as Shodan and Censys), search engines, bots, worms, "@@ -119,7 +119,7 @@ class sfp_greynoise(SpiderFootPlugin):ip_response = self.sf.fetchUrl(gn_context_url + qry,timeout=self.opts["_fetchtimeout"],- useragent="greynoise-spiderfoot-v1.1.0",+ useragent="greynoise-spiderfoot-v1.2.0",headers=headers,)if ip_response["code"] == "200":@@ -127,7 +127,7 @@ class sfp_greynoise(SpiderFootPlugin):riot_response = self.sf.fetchUrl(gn_riot_url + qry,timeout=self.opts["_fetchtimeout"],- useragent="greynoise-spiderfoot-v1.1.0",+ useragent="greynoise-spiderfoot-v1.2.0",headers=headers,)if riot_response["code"] in ["200", "404"]:@@ -272,7 +272,7 @@ class sfp_greynoise(SpiderFootPlugin):descr += "\n - " + "Scans For CVEs: " + ", ".join(rec.get("cve"))if rec.get("raw_data") and not (rec.get("tags") or ret.get("cve")):descr += "\n - " + "Raw data: " + str(rec.get("raw_data"))- descr += "\n<SFURL>https://www.greynoise.io/viz/ip/" + rec.get("ip") + "</SFURL>"+ descr += "\n<SFURL>https://viz.greynoise.io/ip/" + rec.get("ip") + "</SFURL>"e = SpiderFootEvent(evtType, descr, self.__name__, event)self.notifyListeners(e)@@ -322,7 +322,7 @@ class sfp_greynoise(SpiderFootPlugin):descr += "\n - " + "Scans For CVEs: " + ", ".join(ret.get("cve"))if ret.get("raw_data") and not (ret.get("tags") or ret.get("cve")):descr += "\n - " + "Raw data: " + str(ret.get("raw_data"))- descr += "\n<SFURL>https://www.greynoise.io/viz/ip/" + ret.get("ip") + "</SFURL>"+ descr += "\n<SFURL>https://viz.greynoise.io/ip/" + ret.get("ip") + "</SFURL>"e = SpiderFootEvent(evtType, descr, self.__name__, event)self.notifyListeners(e)@@ -348,7 +348,7 @@ class sfp_greynoise(SpiderFootPlugin):descr += "\n - " + "Provider Name: " + ret.get("name")if ret.get("category"):descr += "\n - " + "Provider Category: " + ret.get("category")- descr += "\n<SFURL>https://www.greynoise.io/viz/ip/" + ret.get("ip") + "</SFURL>"+ descr += "\n<SFURL>https://viz.greynoise.io/ip/" + ret.get("ip") + "</SFURL>"e = SpiderFootEvent(evtType, descr, self.__name__, event)self.notifyListeners(e)diff --git a/modules/sfp_greynoise_community.py b/modules/sfp_greynoise_community.pynew file mode 100644index 00000000..e91e31c0--- /dev/null+++ b/modules/sfp_greynoise_community.py@@ -0,0 +1,204 @@+# -*- coding: utf-8 -*-+# -------------------------------------------------------------------------------+# Name: sfp_greynoise_community+# Purpose: Query GreyNoise's Community API+#+# Author: Brad Chiappetta, GreyNoise+# Updated By: Brad Chiappetta, GreyNoise+#+# Created: 31-Aug-2022+# Updated: 31-Aug-2022+# Copyright: (c) Steve Micallef+# Licence: MIT+# -------------------------------------------------------------------------------++import json+import time+from datetime import datetime++from spiderfoot import SpiderFootEvent, SpiderFootPlugin+++class sfp_greynoise_community(SpiderFootPlugin):++ meta = {+ "name": "GreyNoise Community",+ "summary": "Obtain IP enrichment data from GreyNoise Community API",+ "flags": ["apikey"],+ "useCases": ["Investigate", "Passive"],+ "categories": ["Reputation Systems"],+ "dataSource": {+ "website": "https://greynoise.io/",+ "model": "FREE_AUTH_LIMITED",+ "references": ["https://docs.greynoise.io/reference/get_v3-community-ip", "https://viz.greynoise.io/signup"],+ "apiKeyInstructions": [+ "Visit https://viz.greynoise.io/signup",+ "Sign up for a free account",+ "Navigate to https://viz.greynoise.io/account/",+ "The API key is listed under 'API Key'",+ ],+ "favIcon": "https://viz.greynoise.io/favicon.ico",+ "logo": "https://viz.greynoise.io/_nuxt/img/greynoise-logo.dccd59d.png",+ "description": "At GreyNoise, we collect and analyze untargeted, widespread, "+ "and opportunistic scan and attack activity that reaches every server directly connected to the Internet. "+ "Mass scanners (such as Shodan and Censys), search engines, bots, worms, "+ "and crawlers generate logs and events omnidirectionally on every IP address in the IPv4 space. "+ "GreyNoise gives you the ability to filter this useless noise out.",+ },+ }++ # Default options+ opts = {+ "api_key": "",+ "age_limit_days": 30,+ }++ # Option descriptions+ optdescs = {+ "api_key": "GreyNoise API Key.",+ "age_limit_days": "Ignore any records older than this many days. 0 = unlimited.",+ }++ # Be sure to completely clear any class variables in setup()+ # or you run the risk of data persisting between scan runs.++ results = None+ errorState = False++ def setup(self, sfc, userOpts=dict()):+ self.sf = sfc+ self.results = self.tempStorage()++ # Clear / reset any other class member variables here+ # or you risk them persisting between threads.++ for opt in list(userOpts.keys()):+ self.opts[opt] = userOpts[opt]++ # What events is this module interested in for input+ def watchedEvents(self):+ return ["IP_ADDRESS", "AFFILIATE_IPADDR", "NETBLOCK_MEMBER", "NETBLOCK_OWNER"]++ # What events this module produces+ def producedEvents(self):+ return [+ "MALICIOUS_IPADDR",+ "COMPANY_NAME",+ "RAW_RIR_DATA",+ ]++ def queryIP(self, qry, qry_type):+ gn_community_url = "https://api.greynoise.io/v3/community/"++ headers = {"key": self.opts["api_key"]}+ res = {}+ if qry_type == "ip":+ self.debug(f"Querying GreyNoise Community API for IP: {qry}")+ ip_res = {}+ ip_response = self.sf.fetchUrl(+ gn_community_url + qry,+ timeout=self.opts["_fetchtimeout"],+ useragent="greynoise-spiderfoot-community-v1.2.0",+ headers=headers,+ )+ if ip_response["code"] == "200":+ ip_res = json.loads(ip_response["content"])+ res = ip_res++ if not res:+ self.error("Greynoise API key seems to have been rejected or you have exceeded usage limits.")+ self.errorState = True+ return None++ return res++ # Handle events sent to this module+ 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 self.opts["api_key"] == "":+ self.error("You enabled sfp_greynoise_community but did not set an API key!")+ self.errorState = True+ return++ if eventData in self.results:+ self.debug(f"Skipping {eventData}, already checked.")+ return++ self.results[eventData] = True++ if eventName == "IP_ADDRESS":+ evtType = "MALICIOUS_IPADDR"+ qryType = "ip"+ if eventName == "AFFILIATE_IPADDR":+ evtType = "MALICIOUS_AFFILIATE_IPADDR"+ qryType = "ip"++ ret = self.queryIP(eventData, qryType)++ if not ret:+ return++ if "data" not in ret and "noise" not in ret and "riot" not in ret:+ return++ if "noise" in ret:+ if ret.get("noise", None):+ lastseen = ret.get("last_seen", "1970-01-01")+ lastseen_dt = datetime.strptime(lastseen, "%Y-%m-%d")+ lastseen_ts = int(time.mktime(lastseen_dt.timetuple()))+ age_limit_ts = int(time.time()) - (86400 * self.opts["age_limit_days"])+ if self.opts["age_limit_days"] > 0 and lastseen_ts < age_limit_ts:+ self.debug("Record found but too old, skipping.")+ return+ e = SpiderFootEvent("RAW_RIR_DATA", str(ret), self.__name__, event)+ self.notifyListeners(e)++ # Only report meta data about the target, not affiliates+ if ret.get("name", "unknown") != "unknown":+ e = SpiderFootEvent("COMPANY_NAME", ret.get("name"), self.__name__, event)+ self.notifyListeners(e)++ if ret.get("classification"):+ descr = (+ "GreyNoise - Mass-Scanning IP Detected ["+ + eventData+ + "]\n - Classification: "+ + ret.get("classification")+ )+ descr += "\n<SFURL>https://viz.greynoise.io/ip/" + ret.get("ip") + "</SFURL>"+ e = SpiderFootEvent(evtType, descr, self.__name__, event)+ self.notifyListeners(e)++ if "riot" in ret:+ if ret.get("riot", None):+ lastseen = ret.get("last_seen", "1970-01-01")+ lastseen = lastseen.split("T")[0]+ lastseen_dt = datetime.strptime(lastseen, "%Y-%m-%d")+ lastseen_ts = int(time.mktime(lastseen_dt.timetuple()))+ age_limit_ts = int(time.time()) - (86400 * self.opts["age_limit_days"])+ if self.opts["age_limit_days"] > 0 and lastseen_ts < age_limit_ts:+ self.debug("Record found but too old, skipping.")+ return+ e = SpiderFootEvent("RAW_RIR_DATA", str(ret), self.__name__, event)+ self.notifyListeners(e)++ if ret.get("name"):+ descr = (+ "GreyNoise - Common-Business Service IP Detected ["+ + eventData+ )+ descr += "\n - " + "Provider Name: " + ret.get("name")+ descr += "\n<SFURL>https://viz.greynoise.io/ip/" + ret.get("ip") + "</SFURL>"+ e = SpiderFootEvent(evtType, descr, self.__name__, event)+ self.notifyListeners(e)+++# End of sfp_greynoise_community class