Skip to content

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

Support Stopping Jobs in multiprocess. Modify Sflib, sfweb and sfscan to remove the global scanstatus enviromental variable, and replace it with calls to the database. This is the missing component in fullly migrating this into multprocess. This allows for stable simutanous multi-job use.

fallingcubes91e60fe8f0d4
diff --git a/sflib.py b/sflib.py
index 852c7fd6..b9e57e5d 100644
--- a/sflib.py
+++ b/sflib.py
@@ -39,6 +39,7 @@ from datetime import datetime
from bs4 import BeautifulSoup, SoupStrainer
from copy import deepcopy
+
# For hiding the SSL warnings coming from the requests lib
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
@@ -1801,6 +1802,7 @@ class SpiderFootPlugin(object):
_priority = 1
# Error state of the module
errorState = False
+
# Not really needed in most cases.
def __init__(self):
@@ -1944,10 +1946,12 @@ class SpiderFootPlugin(object):
# For modules to use to check for when they should give back control
def checkForStop(self):
- global globalScanStatus
+ scanstatus = self.__sfdb__.scanInstanceGet(self.__scanId__)
+ #Ensure that scanStatus is not None
+ if scanstatus != None:
+ if scanstatus[5] == "ABORT-REQUESTED":
- if globalScanStatus.getStatus(self.__scanId__) == "ABORT-REQUESTED":
- return True
+ return True
return False
# Return a list of the default configuration options for the module.
diff --git a/sfscan.py b/sfscan.py
index 26fdff64..72115173 100644
--- a/sfscan.py
+++ b/sfscan.py
@@ -14,17 +14,14 @@ import time
import sys
import socket
import dns.resolver
-import threading
import random
from copy import deepcopy
from sfdb import SpiderFootDb
from sflib import SpiderFoot, SpiderFootEvent, SpiderFootTarget, \
- SpiderFootPlugin, globalScanStatus
+ SpiderFootPlugin
# Eventually change this to be able to control multiple scan instances
-class SpiderFootScanner(): #threading.Thread):
- # Thread-safe storage
- ts = None
+class SpiderFootScanner():
# Temporary storage
temp = None
@@ -32,11 +29,7 @@ class SpiderFootScanner(): #threading.Thread):
def __init__(self, scanName, scanTarget, targetType, scanId, moduleList,
globalOpts, moduleOpts):
- # Initialize the thread
- #threading.Thread.__init__(self, name="SF_" + scanId + \
- # str(random.SystemRandom().randint(100000, 999999)))
-
- # Temporary data to be used in startScan
+
self.temp = dict()
self.temp['config'] = deepcopy(globalOpts)
self.temp['targetValue'] = scanTarget
@@ -55,7 +48,6 @@ class SpiderFootScanner(): #threading.Thread):
self.status = status
self.dbh.scanInstanceSet(self.scanId, started, ended, status)
- #globalScanStatus.setStatus(self.scanId, status)
return None
def run(self):
@@ -68,9 +60,7 @@ class SpiderFootScanner(): #threading.Thread):
# Start running a scan
def startScan(self):
- #global globalScanStatus
-
- #self = threading.local()
+
self.moduleInstances = dict()
self.sf = SpiderFoot(self.temp['config'])
self.config = deepcopy(self.temp['config'])
@@ -222,6 +212,7 @@ class SpiderFootScanner(): #threading.Thread):
psMod = SpiderFootPlugin()
psMod.__name__ = "SpiderFoot UI"
psMod.setTarget(target)
+ psMod.setDbh(self.dbh)
psMod.clearListeners()
for mod in list(self.moduleInstances.values()):
if mod.watchedEvents() is not None:
diff --git a/sfwebui.py b/sfwebui.py
index 8916af9f..b346b210 100644
--- a/sfwebui.py
+++ b/sfwebui.py
@@ -23,7 +23,7 @@ from copy import deepcopy
from mako.lookup import TemplateLookup
from mako.template import Template
from sfdb import SpiderFootDb
-from sflib import SpiderFoot, globalScanStatus
+from sflib import SpiderFoot
from sfscan import SpiderFootScanner
from io import StringIO
mp.set_start_method("spawn", force=True)
@@ -358,20 +358,19 @@ class SpiderFootWebUi:
# Start running a new scan
newId = sf.genScanInstanceGUID(scanname)
- p = mp.Process(target=SpiderFootScanner, args=(scanname, scantarget, targetType, newId
- modlist, cfg, modopts)
- )
+ p = mp.Process(target=SpiderFootScanner, args=(scanname, scantarget, targetType, newId,
+ modlist, cfg, modopts))
p.start()
# Wait until the scan has initialized
- while globalScanStatus.getStatus(newId) == None:
+ while dbh.scanInstanceGet(newId) == None:
print("[info] Waiting for the scan to initialize...")
time.sleep(1)
templ = Template(filename='dyn/scaninfo.tmpl', lookup=self.lookup)
return templ.render(id=newId, name=str(scanname), docroot=self.docroot,
- status=globalScanStatus.getStatus(newId), pageid="SCANLIST")
+ status=dbh.scanInstanceGet(newId), pageid="SCANLIST")
rerunscan.exposed = True
@@ -410,7 +409,7 @@ class SpiderFootWebUi:
p.start()
# Wait until the scan has initialized
- while globalScanStatus.getStatus(newId) == None:
+ while dbh.scanInstanceGet(newId) == None:
print("[info] Waiting for the scan to initialize...")
time.sleep(1)
@@ -759,7 +758,8 @@ class SpiderFootWebUi:
# Initiate a scan
def startscan(self, scanname, scantarget, modulelist, typelist, usecase, cli=None):
- global globalScanStatus
+ #Swap the globalscantable for the database handler
+ dbh = SpiderFootDb(self.config)
# Snapshot the current configuration to be used by the scan
cfg = deepcopy(self.config)
@@ -836,17 +836,19 @@ class SpiderFootWebUi:
scantarget = scantarget.lower()
p = mp.Process(target=SpiderFootScanner, args=(scanname, scantarget, targetType, scanId,
modlist, cfg, modopts))
+ p.daemon = True
p.start()
# Wait until the scan has initialized
- # while globalScanStatus.getStatus(scanId) is None:
- # print("[info] Waiting for the scan to initialize...")
- # time.sleep(1)
+ # Check the database for the scan status results
+ while dbh.scanInstanceGet(scanId) is None:
+ print("[info] Waiting for the scan to initialize...")
+ time.sleep(1)
if not cli:
templ = Template(filename='dyn/scaninfo.tmpl', lookup=self.lookup)
return templ.render(id=scanId, name=scanname, docroot=self.docroot,
- status=globalScanStatus.getStatus(scanId), pageid="SCANLIST")
+ status=dbh.scanInstanceGet(scanId), pageid="SCANLIST")
else:
return json.dumps(["SUCCESS", scanId])
@@ -856,7 +858,7 @@ class SpiderFootWebUi:
# Stop a scan (id variable is unnecessary for now given that only one simultaneous
# scan is permitted.)
def stopscanmulti(self, ids):
- global globalScanStatus # running scans
+ #Removing All globalScanStatus Refs
dbh = SpiderFootDb(self.config)
error = list()
@@ -868,24 +870,19 @@ class SpiderFootWebUi:
return self.error("Invalid scan ID specified.")
scanname = str(scaninfo[0])
-
- if globalScanStatus.getStatus(id) == "FINISHED" or scaninfo[5] == "FINISHED":
+ scanstatus = scaninfo[5]
+ if scanstatus == "FINISHED":
error.append("Scan '" + scanname + "' is in a finished state. <a href='/scandelete?id=" + \
id + "&confirm=1'>Maybe you want to delete it instead?</a>")
errState = True
- if not errState and (globalScanStatus.getStatus(id) == "ABORTED" or scaninfo[5] == "ABORTED"):
+ if not errState and scanstatus == "ABORTED":
error.append("Scan '" + scanname + "' is already aborted.")
errState = True
- if not errState and globalScanStatus.getStatus(id) is None:
- error.append("Scan '" + scanname + "' is not actually running. A data consistency " + \
- "error for this scan probably exists. <a href='/scandelete?id=" + \
- id + "&confirm=1'>Click here to delete it.</a>")
- errState = True
-
if not errState:
- globalScanStatus.setStatus(id, "ABORT-REQUESTED")
+ #set the scanstatus in the db to "ABORT-REQUESTED"
+ dbh.scanInstanceSet(id, status="ABORT-REQUESTED")
templ = Template(filename='dyn/scanlist.tmpl', lookup=self.lookup)
return templ.render(pageid='SCANLIST', stoppedscan=True,
@@ -896,40 +893,32 @@ class SpiderFootWebUi:
# Stop a scan.
def stopscan(self, id, cli=None):
- global globalScanStatus
-
dbh = SpiderFootDb(self.config)
scaninfo = dbh.scanInstanceGet(id)
+ scanstatus = scaninfo[5]
+ print(scanstatus)
if scaninfo is None:
if not cli:
return self.error("Invalid scan ID.")
else:
return json.dumps(["ERROR", "Invalid scan ID."])
- if globalScanStatus.getStatus(id) is None:
- if not cli:
- return self.error("That scan is not actually running. A data consistency " + \
- "error for this scan probably exists. <a href='/scandelete?id=" + \
- id + "&confirm=1'>Click here to delete it.</a>")
- else:
- return json.dumps(["ERROR", "Scan doesn't appear to be running."])
-
- if globalScanStatus.getStatus(id) == "ABORTED":
+ if scanstatus == "ABORTED":
if not cli:
return self.error("The scan is already aborted.")
else:
return json.dumps(["ERROR", "Scan already aborted."])
- if not globalScanStatus.getStatus(id) == "RUNNING":
+ if not scanstatus == "RUNNING":
if not cli:
return self.error("The running scan is currently in the state '" + \
- globalScanStatus.getStatus(id) + "', please try again later or restart " + \
+ scanstatus + "', please try again later or restart " + \
" SpiderFoot.")
else:
return json.dumps(["ERROR", "Scan in an invalid state for stopping."])
- globalScanStatus.setStatus(id, "ABORT-REQUESTED")
+ dbh.scanInstanceSet(id, status="ABORT-REQUESTED")
if not cli:
templ = Template(filename='dyn/scanlist.tmpl', lookup=self.lookup)
return templ.render(pageid='SCANLIST', stoppedscan=True, docroot=self.docroot, errors=list())