Skip to content

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

Site check performance improvements.

Steve Bate69ce6638ce24
diff --git a/modules/sfp_accounts.py b/modules/sfp_accounts.py
index c3154000..3e1a813c 100644
--- a/modules/sfp_accounts.py
+++ b/modules/sfp_accounts.py
@@ -13,6 +13,7 @@
import time
import threading
+from queue import Queue, Empty as QueueEmpty
import json
import random
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -117,24 +118,12 @@ class sfp_accounts(SpiderFootPlugin):
self.siteResults[retname] = False
return
- try:
- found = False
- site['account_existence_code'] = str(site['account_existence_code'])
- if site['account_existence_code']:
- if site['account_existence_code'] == res['code']:
- found = True
- if site['account_missing_code']:
- if site['account_missing_code'] == res['code']:
- found = False
- if site['account_existence_string']:
- if site['account_existence_string'] in res['content']:
- found = True
- if site['account_missing_string']:
- if site['account_missing_string'] in res['content']:
- found = False
- except BaseException:
- self.sf.debug("Error parsing configuration: " + str(site))
- found = False
+ # If we see the existence code and string, then consider the
+ # site to be found. Otherwise, assume it's not found. Note that
+ # the account_missing_code might be the same as the account_existence_code
+ # (e.g. 200).
+ found = site.get('account_existence_code') == res['code'] \
+ and site.get('account_existence_string') in res['content']
if found and self.opts['musthavename']:
if name not in res['content']:
@@ -144,79 +133,55 @@ class sfp_accounts(SpiderFootPlugin):
# Some sites can't handle periods so treat bob.abc and bob as the same
if found and "." in name:
firstname = name.split(".")[0]
-
if firstname + "<" in res['content'] or firstname + '"' in res['content']:
found = False
with self.lock:
self.siteResults[retname] = found
- def threadSites(self, name, siteList):
- ret = list()
- self.siteResults = dict()
- running = True
- i = 0
- t = []
-
- for site in siteList:
- if self.checkForStop():
- return None
-
- self.sf.info("Spawning thread to check site: " + site['name'] + \
- " / " + site['check_uri'].format(account=name))
- t.append(threading.Thread(name='thread_sfp_accounts_' + site['name'],
- target=self.checkSite, args=(name, site)))
- t[i].start()
- i += 1
-
- # Block until all threads are finished
- while running:
- found = False
- for rt in threading.enumerate():
- if rt.name.startswith("thread_sfp_accounts_"):
- found = True
-
- if not found:
- running = False
-
- time.sleep(0.25)
+ def checkSites(self, username, sites=None):
+ def processSiteQueue(username, queue):
+ try:
+ while True:
+ site = queue.get(timeout=0.1)
+ try:
+ self.checkSite(username, site)
+ except Exception as ex:
+ self.sf.debug(f'thread {threading.current_thread().name} exception {ex}')
+ except QueueEmpty:
+ return
- # Return once the scanning has completed
- return self.siteResults
+ startTime = time.monotonic()
- def batchSites(self, name):
- i = 0
- res = list()
- siteList = list()
+ # results will be collected in siteResults
+ self.siteResults = {}
- for site in self.sites:
- if not site['valid'] or 'check_uri' not in site:
- self.sf.debug("Skipping " + site['name'])
- continue
- if i >= self.opts['_maxthreads']:
- data = self.threadSites(name, siteList)
- if data == None:
- return res
+ sites = self.sites if sites is None else sites
- for ret in list(data.keys()):
- if data[ret]:
- res.append(ret)
- i = 0
- siteList = list()
+ # load the queue
+ queue = Queue()
+ for site in sites:
+ queue.put(site)
- siteList.append(site)
- i += 1
+ # start the scan threads
+ threads = []
+ for i in range(min(len(sites), self.opts['_maxthreads'])):
+ thread = threading.Thread(
+ name=f'sfp_accounts_scan_{i}',
+ target=processSiteQueue,
+ args=(username, queue))
+ thread.start()
+ threads.append(thread)
- if i > 0 and i < self.opts['_maxthreads']:
- data = self.threadSites(name, siteList)
- if data == None:
- return res
+ # wait for all scan threads to finish
+ while threads:
+ threads.pop(0).join()
- for ret in list(data.keys()):
- if data[ret]:
- res.append(ret)
+ duration = time.monotonic() - startTime
+ scanRate = len(sites) / duration
+ self.sf.debug(f'Scan statistics: name={username}, count={len(self.siteResults)}, duration={duration:.2f}, rate={scanRate:.0f}')
- return res
+ return [site for site, found in self.siteResults.items() if found]
# Handle events sent to this module
def handleEvent(self, event):
@@ -254,7 +219,7 @@ class sfp_accounts(SpiderFootPlugin):
else:
randpool = 'abcdefghijklmnopqrstuvwxyz1234567890'
randuser = ''.join([random.SystemRandom().choice(randpool) for x in range(10)])
- res = self.batchSites(randuser)
+ res = self.checkSites(randuser)
if len(res) > 0:
delsites = list()
for site in res:
@@ -306,7 +271,7 @@ class sfp_accounts(SpiderFootPlugin):
# this module, and we don't want duplicates (one based on EMAILADDR and another
# based on USERNAME).
if eventName == "USERNAME":
- res = self.batchSites(user)
+ res = self.checkSites(user)
for site in res:
evt = SpiderFootEvent("ACCOUNT_EXTERNAL_OWNED", site,
self.__name__, event)