Skip to content

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

sfscan: move startScan() functionality back into init()

Brendan Colesd9dcb9376cdf
diff --git a/sf.py b/sf.py
index 3f174521..96d4d31a 100755
--- a/sf.py
+++ b/sf.py
@@ -332,15 +332,11 @@ if __name__ == '__main__':
if args.x and args.t:
cfg['__outputfilter'] = args.t.split(",")
+ # Start running a new scan
+ scanName = target
+ scanId = sf.genScanInstanceGUID()
try:
- s = SpiderFootScanner(target, target, targetType, modlist, cfg)
- scanId = s.getId()
- except BaseException as e:
- print("[-] Failed to initialize scan: %s" % e)
- sys.exit(-1)
-
- try:
- p = mp.Process(target=s.startScan())
+ p = mp.Process(target=SpiderFootScanner, args=(scanName, scanId, target, targetType, modlist, cfg))
p.daemon = True
p.start()
except BaseException as e:
diff --git a/sfscan.py b/sfscan.py
index 1a63634e..bbcb06c3 100644
--- a/sfscan.py
+++ b/sfscan.py
@@ -22,15 +22,17 @@ from sflib import SpiderFoot, SpiderFootEvent, SpiderFootTarget, \
# Eventually change this to be able to control multiple scan instances
class SpiderFootScanner():
- def __init__(self, scanName, scanTarget, targetType, moduleList, globalOpts):
+ def __init__(self, scanName, scanId, scanTarget, targetType, moduleList, globalOpts, start=True):
"""Initialize SpiderFootScanner object.
Args:
scanName (str): name of the scan
+ scanId (str): unique ID of the scan
scanTarget (str): scan target
targetType (str): scan target type
moduleList (list): list of modules to run
globalOpts (dict): scan options
+ start (bool): start the scan immediately
Returns:
None
@@ -38,6 +40,8 @@ class SpiderFootScanner():
if not isinstance(scanName, str):
raise TypeError("scanName is %s; expected str()" % type(scanName))
+ if not isinstance(scanId, str):
+ raise TypeError("scanId is %s; expected str()" % type(scanId))
if not isinstance(scanTarget, str):
raise TypeError("scanTarget is %s; expected str()" % type(scanTarget))
if not isinstance(targetType, str):
@@ -61,7 +65,7 @@ class SpiderFootScanner():
self.sf.setDbh(self.dbh)
# Create a unique ID for this scan and create it in the back-end DB.
- self.scanId = self.sf.genScanInstanceGUID()
+ self.scanId = scanId
self.sf.setGUID(self.scanId)
self.dbh.scanInstanceCreate(self.scanId, self.scanName, self.targetValue)
@@ -83,6 +87,9 @@ class SpiderFootScanner():
self.setStatus("INITIALIZING", time.time() * 1000, None)
+ if start:
+ self.startScan()
+
def getId(self):
"""Retrieve the unique scan identifier.
diff --git a/sfwebui.py b/sfwebui.py
index 6df8f042..262f6cb6 100644
--- a/sfwebui.py
+++ b/sfwebui.py
@@ -28,7 +28,6 @@ from sfscan import SpiderFootScanner
from io import StringIO
mp.set_start_method("spawn", force=True)
-
class SpiderFootWebUi:
lookup = TemplateLookup(directories=[''])
defaultConfig = dict()
@@ -408,15 +407,9 @@ class SpiderFootWebUi:
scantarget = scantarget.lower()
# Start running a new scan
+ scanId = sf.genScanInstanceGUID()
try:
- s = SpiderFootScanner(scanname, scantarget, targetType, modlist, cfg)
- scanId = s.getId()
- except BaseException as e:
- print("[-] Failed to initialize scan: %s" % e)
- return self.error("Failed to initialize scan: %s" % e)
-
- try:
- p = mp.Process(target=s.startScan())
+ p = mp.Process(target=SpiderFootScanner, args=(scanname, scanId, scantarget, targetType, modlist, cfg))
p.daemon = True
p.start()
except BaseException as e:
@@ -462,15 +455,9 @@ class SpiderFootWebUi:
"a human name, IP address, IP subnet, ASN, domain name or host name.")
# Start running a new scan
+ scanId = sf.genScanInstanceGUID()
try:
- s = SpiderFootScanner(scanname, scantarget, targetType, modlist, cfg)
- scanId = s.getId()
- except BaseException as e:
- print("[-] Failed to initialize scan: %s" % e)
- return self.error("Failed to initialize scan: %s" % e)
-
- try:
- p = mp.Process(target=s.startScan())
+ p = mp.Process(target=SpiderFootScanner, args=(scanname, scanId, scantarget, targetType, modlist, cfg))
p.daemon = True
p.start()
except BaseException as e:
@@ -905,15 +892,10 @@ class SpiderFootWebUi:
else:
scantarget = scantarget.lower()
+ # Start running a new scan
+ scanId = sf.genScanInstanceGUID()
try:
- s = SpiderFootScanner(scanname, scantarget, targetType, modlist, cfg)
- scanId = s.getId()
- except BaseException as e:
- print("[-] Failed to initialize scan: %s" % e)
- return self.error("Failed to initialize scan: %s" % e)
-
- try:
- p = mp.Process(target=s.startScan())
+ p = mp.Process(target=SpiderFootScanner, args=(scanname, scanId, scantarget, targetType, modlist, cfg))
p.daemon = True
p.start()
except BaseException as e:
diff --git a/test/unit/test_spiderfootscanner.py b/test/unit/test_spiderfootscanner.py
index 79c5f836..351fb62d 100644
--- a/test/unit/test_spiderfootscanner.py
+++ b/test/unit/test_spiderfootscanner.py
@@ -1,6 +1,7 @@
# test_spiderfootscanner.py
from sfscan import SpiderFootScanner
import unittest
+import uuid
class TestSpiderFootScanner(unittest.TestCase):
"""
@@ -36,50 +37,65 @@ class TestSpiderFootScanner(unittest.TestCase):
def test_init(self):
"""
- Test __init__(self, scanName, scanTarget, targetType, moduleList, globalOpts)
+ Test __init__(self, scanName, scanTarget, targetType, moduleList, globalOpts, start=True)
"""
opts = self.default_options
opts['__modules__'] = dict()
- sfscan = SpiderFootScanner("", "", "IP_ADDRESS", list(), opts)
+ scan_id = str(uuid.uuid4())
+ sfscan = SpiderFootScanner("", scan_id, "", "IP_ADDRESS", list(), opts, start=False)
self.assertIsInstance(sfscan, SpiderFootScanner)
def test_init_invalid_scan_name_should_raise(self):
"""
- Test __init__(self, scanName, scanTarget, targetType, moduleList, globalOpts)
+ Test __init__(self, scanName, scanTarget, targetType, moduleList, globalOpts, start=True)
"""
+ scan_id = str(uuid.uuid4())
with self.assertRaises(TypeError) as cm:
- sfscan = SpiderFootScanner(None, "", "IP_ADDRESS", list(), self.default_options)
+ sfscan = SpiderFootScanner(None, scan_id, "", "IP_ADDRESS", list(), self.default_options, start=False)
+
+ def test_init_invalid_scan_id_should_raise(self):
+ """
+ Test __init__(self, scanName, scanTarget, targetType, moduleList, globalOpts, start=True)
+ """
+ scan_id = None
+ with self.assertRaises(TypeError) as cm:
+ sfscan = SpiderFootScanner(None, scan_id, "", "IP_ADDRESS", list(), self.default_options, start=False)
def test_init_invalid_scan_target_should_raise(self):
"""
- Test __init__(self, scanName, scanTarget, targetType, moduleList, globalOpts)
+ Test __init__(self, scanName, scanTarget, targetType, moduleList, globalOpts, start=True)
"""
+ scan_id = str(uuid.uuid4())
with self.assertRaises(TypeError) as cm:
- sfscan = SpiderFootScanner("", None, "IP_ADDRESS", list(), self.default_options)
+ sfscan = SpiderFootScanner("", scan_id, None, "IP_ADDRESS", list(), self.default_options, start=False)
def test_init_invalid_scan_target_type_should_raise(self):
"""
Test __init__(self, scanName, scanTarget, targetType, moduleList, globalOpts)
"""
+ scan_id = str(uuid.uuid4())
with self.assertRaises(TypeError) as cm:
- sfscan = SpiderFootScanner("", "", None, list(), self.default_options)
+ sfscan = SpiderFootScanner("", scan_id, "", None, list(), self.default_options, start=False)
def test_init_invalid_module_list_should_raise(self):
"""
Test __init__(self, scanName, scanTarget, targetType, moduleList, globalOpts)
"""
+ scan_id = str(uuid.uuid4())
with self.assertRaises(TypeError) as cm:
- sfscan = SpiderFootScanner("", "", "IP_ADDRESS", None, self.default_options)
+ sfscan = SpiderFootScanner("", scan_id, "", "IP_ADDRESS", None, self.default_options, start=False)
def test_init_invalid_options_should_raise(self):
"""
Test __init__(self, scanName, scanTarget, targetType, moduleList, globalOpts)
"""
+ scan_id = str(uuid.uuid4())
with self.assertRaises(TypeError) as cm:
- sfscan = SpiderFootScanner("", "", "IP_ADDRESS", list(), None)
+ sfscan = SpiderFootScanner("", scan_id, "", "IP_ADDRESS", list(), None, start=False)
+ scan_id = str(uuid.uuid4())
with self.assertRaises(ValueError) as cm:
- sfscan = SpiderFootScanner("", "", "IP_ADDRESS", list(), dict())
+ sfscan = SpiderFootScanner("", scan_id, "", "IP_ADDRESS", list(), dict(), start=False)
def test_set_status_invalid_status_should_raise(self):
"""
@@ -87,7 +103,8 @@ class TestSpiderFootScanner(unittest.TestCase):
"""
opts = self.default_options
opts['__modules__'] = dict()
- sfscan = SpiderFootScanner("", "", "IP_ADDRESS", list(), opts)
+ scan_id = str(uuid.uuid4())
+ sfscan = SpiderFootScanner("", scan_id, "", "IP_ADDRESS", list(), opts, start=False)
self.assertIsInstance(sfscan, SpiderFootScanner)
with self.assertRaises(ValueError) as cm:
@@ -99,11 +116,13 @@ class TestSpiderFootScanner(unittest.TestCase):
"""
opts = self.default_options
opts['__modules__'] = dict()
- sfscan = SpiderFootScanner("", "", "IP_ADDRESS", list(), opts)
+ scan_id = str(uuid.uuid4())
+ sfscan = SpiderFootScanner("", scan_id, "", "IP_ADDRESS", list(), opts, start=False)
self.assertIsInstance(sfscan, SpiderFootScanner)
- scan_id = sfscan.getId()
- self.assertIsInstance(scan_id, str)
+ get_id = sfscan.getId()
+ self.assertIsInstance(get_id, str)
+ self.assertEqual(scan_id, get_id)
@unittest.skip("todo")
def test_start_scan_should_start_a_scan(self):