Skip to content

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

sflib: move options parsing to init

Brendan Coles02032e8ffe42
diff --git a/sflib.py b/sflib.py
index 059aa11f..8243f3b3 100644
--- a/sflib.py
+++ b/sflib.py
@@ -2669,11 +2669,14 @@ class SpiderFootTarget(object):
Raises:
TypeError: targetValue type was invalid
+ ValueError: targetValue value was empty
ValueError: typeName value was an invalid target type
"""
if not isinstance(targetValue, str):
raise TypeError("Invalid target value %s; expected %s" % type(targetValue))
+ if not targetValue:
+ raise ValueError("Specified target value is blank.")
if typeName not in self._validTypes:
raise ValueError("Invalid target type %s; expected %s" % (typeName, self._validTypes))
diff --git a/sfscan.py b/sfscan.py
index bbcb06c3..a4abc9ad 100644
--- a/sfscan.py
+++ b/sfscan.py
@@ -20,7 +20,6 @@ from sfdb import SpiderFootDb
from sflib import SpiderFoot, SpiderFootEvent, SpiderFootTarget, \
SpiderFootPlugin
-# Eventually change this to be able to control multiple scan instances
class SpiderFootScanner():
def __init__(self, scanName, scanId, scanTarget, targetType, moduleList, globalOpts, start=True):
"""Initialize SpiderFootScanner object.
@@ -36,6 +35,9 @@ class SpiderFootScanner():
Returns:
None
+
+ Todo:
+ Eventually change this to be able to control multiple scan instances
"""
if not isinstance(scanName, str):
@@ -85,6 +87,59 @@ class SpiderFootScanner():
self.config['_modulesenabled'] = self.moduleList
self.dbh.scanConfigSet(self.scanId, self.sf.configSerialize(deepcopy(self.config)))
+ # Process global options that point to other places for data
+
+ # If a SOCKS server was specified, set it up
+ if self.config['_socks1type']:
+ socksDns = self.config['_socks6dns']
+ socksAddr = self.config['_socks2addr']
+ socksPort = int(self.config['_socks3port'])
+ socksUsername = self.config['_socks4user'] or ''
+ socksPassword = self.config['_socks5pwd'] or ''
+
+ proxy = "%s:%s" % (socksAddr, socksPort)
+
+ if socksUsername or socksPassword:
+ proxy = "%s:%s@%s" % (socksUsername, socksPassword, proxy)
+
+ if self.config['_socks1type'] == '4':
+ proxy = 'socks4://' + proxy
+ elif self.config['_socks1type'] == '5':
+ proxy = 'socks5://' + proxy
+ elif self.config['_socks1type'] == 'HTTP':
+ proxy = 'http://' + proxy
+ elif self.config['_socks1type'] == 'TOR':
+ proxy = 'socks5h://' + proxy
+ else:
+ raise ValueError("Invalid SOCKS proxy type: %s" % self.config["_socks1ttype"])
+
+ self.sf.debug("SOCKS: %s:%s (%s:%s)" % (socksAddr, socksPort, socksUsername, socksPassword))
+
+ self.sf.updateSocket(proxy)
+ else:
+ self.sf.revertSocket()
+
+ # Override the default DNS server
+ if self.config['_dnsserver']:
+ res = dns.resolver.Resolver()
+ res.nameservers = [self.config['_dnsserver']]
+ dns.resolver.override_system_resolver(res)
+ else:
+ dns.resolver.restore_system_resolver()
+
+ # Set the user agent
+ self.config['_useragent'] = self.sf.optValueToData(self.config['_useragent'])
+
+ # Get internet TLDs
+ tlddata = self.sf.cacheGet("internet_tlds", self.config['_internettlds_cache'])
+
+ # If it wasn't loadable from cache, load it from scratch
+ if tlddata is None:
+ self.config['_internettlds'] = self.sf.optValueToData(self.config['_internettlds'])
+ self.sf.cachePut("internet_tlds", self.config['_internettlds'])
+ else:
+ self.config["_internettlds"] = tlddata.splitlines()
+
self.setStatus("INITIALIZING", time.time() * 1000, None)
if start:
@@ -128,59 +183,6 @@ class SpiderFootScanner():
self.sf.status("Scan [" + self.scanId + "] initiated.")
try:
- # Process global options that point to other places for data
-
- # If a SOCKS server was specified, set it up
- if self.config['_socks1type'] != '':
- socksDns = self.config['_socks6dns']
- socksAddr = self.config['_socks2addr']
- socksPort = int(self.config['_socks3port'])
- socksUsername = self.config['_socks4user'] or ''
- socksPassword = self.config['_socks5pwd'] or ''
- creds = ""
- if socksUsername and socksPassword:
- creds = socksUsername + ":" + socksPassword + "@"
- proxy = creds + socksAddr + ":" + str(socksPort)
-
- if self.config['_socks1type'] == '4':
- proxy = 'socks4://' + proxy
- elif self.config['_socks1type'] == '5':
- proxy = 'socks5://' + proxy
- elif self.config['_socks1type'] == 'HTTP':
- proxy = 'http://' + proxy
- elif self.config['_socks1type'] == 'TOR':
- proxy = 'socks5h://' + proxy
-
- self.sf.debug("SOCKS: " + socksAddr + ":" + str(socksPort) + \
- "(" + socksUsername + ":" + socksPassword + ")")
-
- self.sf.updateSocket(proxy)
- else:
- self.sf.revertSocket()
-
- # Override the default DNS server
- if self.config['_dnsserver'] != "":
- res = dns.resolver.Resolver()
- res.nameservers = [self.config['_dnsserver']]
- dns.resolver.override_system_resolver(res)
- else:
- dns.resolver.restore_system_resolver()
-
- # Set the user agent
- self.config['_useragent'] = self.sf.optValueToData(
- self.config['_useragent'])
-
- # Get internet TLDs
- tlddata = self.sf.cacheGet("internet_tlds",
- self.config['_internettlds_cache'])
- # If it wasn't loadable from cache, load it from scratch
- if tlddata is None:
- self.config['_internettlds'] = self.sf.optValueToData(
- self.config['_internettlds'])
- self.sf.cachePut("internet_tlds", self.config['_internettlds'])
- else:
- self.config["_internettlds"] = tlddata.splitlines()
-
# moduleList = list of modules the user wants to run
for modName in self.moduleList:
if modName == '':
diff --git a/test/unit/test_spiderfootscanner.py b/test/unit/test_spiderfootscanner.py
index 351fb62d..cf953c94 100644
--- a/test/unit/test_spiderfootscanner.py
+++ b/test/unit/test_spiderfootscanner.py
@@ -37,17 +37,17 @@ class TestSpiderFootScanner(unittest.TestCase):
def test_init(self):
"""
- Test __init__(self, scanName, scanTarget, targetType, moduleList, globalOpts, start=True)
+ Test __init__(self, scanName, scanId, scanTarget, targetType, moduleList, globalOpts, start=True)
"""
opts = self.default_options
opts['__modules__'] = dict()
scan_id = str(uuid.uuid4())
- sfscan = SpiderFootScanner("", scan_id, "", "IP_ADDRESS", list(), opts, start=False)
+ sfscan = SpiderFootScanner("", scan_id, "spiderfoot.net", "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, start=True)
+ Test __init__(self, scanName, scanId, scanTarget, targetType, moduleList, globalOpts, start=True)
"""
scan_id = str(uuid.uuid4())
with self.assertRaises(TypeError) as cm:
@@ -55,43 +55,71 @@ class TestSpiderFootScanner(unittest.TestCase):
def test_init_invalid_scan_id_should_raise(self):
"""
- Test __init__(self, scanName, scanTarget, targetType, moduleList, globalOpts, start=True)
+ Test __init__(self, scanName, scanId, 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)
+ sfscan = SpiderFootScanner("", scan_id, "", "IP_ADDRESS", list(), self.default_options, start=False)
- def test_init_invalid_scan_target_should_raise(self):
+ def test_init_invalid_targetvalue_type_should_raise(self):
"""
- Test __init__(self, scanName, scanTarget, targetType, moduleList, globalOpts, start=True)
+ Test __init__(self, scanName, scanId, scanTarget, targetType, moduleList, globalOpts, start=True)
+ """
+ scan_id = str(uuid.uuid4())
+ invalid_types = [None, list(), dict()]
+ for invalid_type in invalid_types:
+ with self.subTest(invalid_type=invalid_type):
+ with self.assertRaises(TypeError) as cm:
+ sfscan = SpiderFootScanner("", scan_id, invalid_type, "IP_ADDRESS", list(), self.default_options, start=False)
+
+ def test_init_blank_targetvalue_value_should_raise(self):
+ """
+ Test __init__(self, scanName, scanId, scanTarget, targetType, moduleList, globalOpts, start=True)
"""
scan_id = str(uuid.uuid4())
with self.assertRaises(TypeError) as cm:
sfscan = SpiderFootScanner("", scan_id, None, "IP_ADDRESS", list(), self.default_options, start=False)
- def test_init_invalid_scan_target_type_should_raise(self):
+ def test_init_invalid_targettype_type_should_raise(self):
"""
- Test __init__(self, scanName, scanTarget, targetType, moduleList, globalOpts)
+ Test __init__(self, scanName, scanId, scanTarget, targetType, moduleList, globalOpts)
"""
scan_id = str(uuid.uuid4())
- with self.assertRaises(TypeError) as cm:
- sfscan = SpiderFootScanner("", scan_id, "", None, list(), self.default_options, start=False)
+ invalid_types = [None, list(), dict()]
+ for invalid_type in invalid_types:
+ with self.subTest(invalid_type=invalid_type):
+ with self.assertRaises(TypeError) as cm:
+ sfscan = SpiderFootScanner("", scan_id, "spiderfoot.net", invalid_type, list(), self.default_options, start=False)
+
+ def test_init_blank_targettype_value_should_raise(self):
+ """
+ Test __init__(self, scanName, scanId, scanTarget, targetType, moduleList, globalOpts)
+ """
+ scan_id = str(uuid.uuid4())
+ with self.assertRaises(ValueError) as cm:
+ sfscan = SpiderFootScanner("", scan_id, "spiderfoot.net", "invalid target type", list(), self.default_options, start=False)
def test_init_invalid_module_list_should_raise(self):
"""
- Test __init__(self, scanName, scanTarget, targetType, moduleList, globalOpts)
+ Test __init__(self, scanName, scanId, scanTarget, targetType, moduleList, globalOpts)
"""
scan_id = str(uuid.uuid4())
- with self.assertRaises(TypeError) as cm:
- sfscan = SpiderFootScanner("", scan_id, "", "IP_ADDRESS", None, self.default_options, start=False)
+ invalid_types = [None, "", dict()]
+ for invalid_type in invalid_types:
+ with self.subTest(invalid_type=invalid_type):
+ with self.assertRaises(TypeError) as cm:
+ sfscan = SpiderFootScanner("", scan_id, "spiderfoot.net", "IP_ADDRESS", invalid_type, self.default_options, start=False)
def test_init_invalid_options_should_raise(self):
"""
- Test __init__(self, scanName, scanTarget, targetType, moduleList, globalOpts)
+ Test __init__(self, scanName, scanId, scanTarget, targetType, moduleList, globalOpts)
"""
scan_id = str(uuid.uuid4())
- with self.assertRaises(TypeError) as cm:
- sfscan = SpiderFootScanner("", scan_id, "", "IP_ADDRESS", list(), None, start=False)
+ invalid_types = [None, "", list()]
+ for invalid_type in invalid_types:
+ with self.subTest(invalid_type=invalid_type):
+ with self.assertRaises(TypeError) as cm:
+ sfscan = SpiderFootScanner("", scan_id, "spiderfoot.net", "IP_ADDRESS", list(), invalid_type, start=False)
scan_id = str(uuid.uuid4())
with self.assertRaises(ValueError) as cm:
@@ -104,7 +132,7 @@ class TestSpiderFootScanner(unittest.TestCase):
opts = self.default_options
opts['__modules__'] = dict()
scan_id = str(uuid.uuid4())
- sfscan = SpiderFootScanner("", scan_id, "", "IP_ADDRESS", list(), opts, start=False)
+ sfscan = SpiderFootScanner("", scan_id, "spiderfoot.net", "IP_ADDRESS", list(), opts, start=False)
self.assertIsInstance(sfscan, SpiderFootScanner)
with self.assertRaises(ValueError) as cm:
@@ -117,7 +145,7 @@ class TestSpiderFootScanner(unittest.TestCase):
opts = self.default_options
opts['__modules__'] = dict()
scan_id = str(uuid.uuid4())
- sfscan = SpiderFootScanner("", scan_id, "", "IP_ADDRESS", list(), opts, start=False)
+ sfscan = SpiderFootScanner("", scan_id, "spiderfoot.net", "IP_ADDRESS", list(), opts, start=False)
self.assertIsInstance(sfscan, SpiderFootScanner)
get_id = sfscan.getId()