SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.
Python attacksurfacecticybersecurityfootprintinghackinginformation-gatheringinformation-securityinfosecintelligence-gatheringosintosint-frameworkosint-reconnaissanceosint-toolpentestingpythonreconsecurity-toolsthreat-intelligencethreatintel
sfp_bitcoin: Extract Bitcoin addresses in Bech32 format (#1572)
bcolese7a1e1b0f8b9
diff --git a/modules/sfp_bitcoin.py b/modules/sfp_bitcoin.pyindex 2cc5ac2a..57df5b9e 100644--- a/modules/sfp_bitcoin.py+++ b/modules/sfp_bitcoin.py@@ -28,7 +28,6 @@ class sfp_bitcoin(SpiderFootPlugin):'categories': ["Content Analysis"]}- # Default optionsopts = {}optdescs = {}@@ -41,13 +40,9 @@ class sfp_bitcoin(SpiderFootPlugin):for opt in list(userOpts.keys()):self.opts[opt] = userOpts[opt]- # What events is this module interested in for inputdef watchedEvents(self):return ["TARGET_WEB_CONTENT"]- # What events this module produces- # This is to support the end user in selecting modules based on events- # produced.def producedEvents(self):return ["BITCOIN_ADDRESS"]@@ -66,7 +61,6 @@ class sfp_bitcoin(SpiderFootPlugin):bcbytes = self.decode_base58(bc, 25)return bcbytes[-4:] == sha256(sha256(bcbytes[:-4]).digest()).digest()[:4]- # Handle events sent to this moduledef handleEvent(self, event):eventName = event.eventTypesrcModuleName = event.module@@ -80,12 +74,23 @@ class sfp_bitcoin(SpiderFootPlugin):self.debug(f"Received event, {eventName}, from {srcModuleName}")+ addrs = list()+# thanks to https://stackoverflow.com/questions/21683680/regex-to-match-bitcoin-addresses- matches = re.findall(r"[\s:=\>]([13][a-km-zA-HJ-NP-Z1-9]{25,34})", eventData)+ # Does not support keys or testnet addresses+ matches = re.findall(r"[\s:=\>](bc(0([ac-hj-np-z02-9]{39}|[ac-hj-np-z02-9]{59})|1[ac-hj-np-z02-9]{8,87})|[13][a-km-zA-HJ-NP-Z1-9]{25,35})", eventData)for m in matches:- self.debug(f"Bitcoin potential match: {m}")- if self.check_bc(m):- evt = SpiderFootEvent("BITCOIN_ADDRESS", m, self.__name__, event)- self.notifyListeners(evt)+ address = m[0]+ self.debug(f"Potential Bitcoin address match: {address}")++ if address.startswith('1') or address.startswith('3'):+ if self.check_bc(address):+ addrs.append(address)+ else:+ addrs.append(address)++ for address in set(addrs):+ evt = SpiderFootEvent("BITCOIN_ADDRESS", address, self.__name__, event)+ self.notifyListeners(evt)# End of sfp_bitcoin classdiff --git a/test/unit/modules/test_sfp_bitcoin.py b/test/unit/modules/test_sfp_bitcoin.pyindex 54fd654c..99aabc26 100644--- a/test/unit/modules/test_sfp_bitcoin.py+++ b/test/unit/modules/test_sfp_bitcoin.py@@ -34,7 +34,7 @@ class TestModuleBitcoin(unittest.TestCase):module = sfp_bitcoin()self.assertIsInstance(module.producedEvents(), list)- def test_handleEvent_event_data_containing_bitcoin_string_should_return_event(self):+ def test_handleEvent_event_data_containing_bitcoin_string_in_legacy_base58_format_should_return_event(self):sf = SpiderFoot(self.default_options)module = sfp_bitcoin()@@ -70,6 +70,42 @@ class TestModuleBitcoin(unittest.TestCase):self.assertEqual("OK", str(cm.exception))+ def test_handleEvent_event_data_containing_bitcoin_string_in_bech32_format_should_return_event(self):+ sf = SpiderFoot(self.default_options)++ module = sfp_bitcoin()+ module.setup(sf, dict())++ target_value = 'spiderfoot.net'+ target_type = 'INTERNET_NAME'+ target = SpiderFootTarget(target_value, target_type)+ module.setTarget(target)++ def new_notifyListeners(self, event):+ expected = 'BITCOIN_ADDRESS'+ if str(event.eventType) != expected:+ raise Exception(f"{event.eventType} != {expected}")++ expected = 'bc1q4r8h8vqk02gnvlus758qmpk8jmajpy2ld23xtr73a39ps0r9z82qq0qqye'+ if str(event.data) != expected:+ raise Exception(f"{event.data} != {expected}")++ raise Exception("OK")++ module.notifyListeners = new_notifyListeners.__get__(module, sfp_bitcoin)++ event_type = 'ROOT'+ event_data = 'example data bc1q4r8h8vqk02gnvlus758qmpk8jmajpy2ld23xtr73a39ps0r9z82qq0qqye example data'+ event_module = ''+ source_event = ''++ evt = SpiderFootEvent(event_type, event_data, event_module, source_event)++ with self.assertRaises(Exception) as cm:+ module.handleEvent(evt)++ self.assertEqual("OK", str(cm.exception))+def test_handleEvent_event_data_not_containing_bitcoin_string_should_not_return_event(self):sf = SpiderFoot(self.default_options)