SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.
Python attacksurfacecticybersecurityfootprintinghackinginformation-gatheringinformation-securityinfosecintelligence-gatheringosintosint-frameworkosint-reconnaissanceosint-toolpentestingpythonreconsecurity-toolsthreat-intelligencethreatintel
sflib: Add useProxyForUrl function
Brendan Colesf29902f77889
diff --git a/sflib.py b/sflib.pyindex 3a6508bd..73084656 100644--- a/sflib.py+++ b/sflib.py@@ -2442,6 +2442,53 @@ class SpiderFoot:return ret+ def useProxyForUrl(self, url):+ """Check if the configured proxy should be used to connect to a specified URL.++ Args:+ url (str): The URL to check++ Returns:+ bool: should the configured proxy be used?+ """+ host = self.urlFQDN(url).lower()++ if not self.opts['_socks1type']:+ return False++ proxy_host = self.opts['_socks2addr']++ if not proxy_host:+ return False++ proxy_port = self.opts['_socks3port']++ if not proxy_port:+ return False++ # Never proxy requests to the proxy+ if host == proxy_host.lower():+ return False++ # Never proxy RFC1918 addresses on the LAN or the local network interface+ if self.validIP(host):+ if netaddr.IPAddress(host).is_private():+ return False+ if netaddr.IPAddress(host).is_loopback():+ return False++ # Never proxy local hostnames+ else:+ neverProxyNames = ['local', 'localhost']+ if host in neverProxyNames:+ return False++ for s in neverProxyNames:+ if host.endswith(s):+ return False++ return True+def fetchUrl(self, url, fatal=False, cookies=None, timeout=30,useragent="SpiderFoot", headers=None, noLog=False,postData=None, dontMangle=False, sizeLimit=None,@@ -2462,53 +2509,15 @@ class SpiderFoot:url = url.strip()proxies = dict()- if self.opts['_socks1type']:- neverProxyNames = [self.opts['_socks2addr']]- neverProxySubnets = [- "127."- "10.",- "192.168.",- "172.16.",- "172.17.",- "172.18.",- "172.19.",- "172.20.",- "172.21.",- "172.22.",- "172.23.",- "172.24.",- "172.25.",- "172.26.",- "172.27.",- "172.28.",- "172.29.",- "172.30.",- "172.31."- ]- host = self.urlFQDN(url)-- # Completely on or off?- proxy = bool(self.opts['_socks1type'])-- # Never proxy these system/internal locations- if host in neverProxyNames:- proxy = False- for s in neverProxyNames:- if host.endswith(s):- proxy = False- for sub in neverProxySubnets:- if host.startswith(sub):- proxy = False-- if proxy:- self.debug(f"Using proxy for {host}")- self.debug("Proxy set to " + self.opts['_socks2addr'] + ":" + str(self.opts['_socks3port']))- proxies = {- 'http': 'socks5h://' + self.opts['_socks2addr'] + ":" + str(self.opts['_socks3port']),- 'https': 'socks5h://' + self.opts['_socks2addr'] + ":" + str(self.opts['_socks3port'])- }- else:- self.debug("Not using proxy for " + host)+ if self.useProxyForUrl(url):+ proxy_url = f"socks5h://{self.opts['_socks2addr']}:{self.opts['_socks3port']}"+ self.debug(f"Using proxy {proxy_url} for {url}")+ proxies = {+ 'http': proxy_url,+ 'https': proxy_url+ }+ else:+ self.debug(f"Not using proxy for {url}")try:header = dict()diff --git a/test/unit/test_spiderfoot.py b/test/unit/test_spiderfoot.pyindex 19f3ec14..03876aae 100644--- a/test/unit/test_spiderfoot.py+++ b/test/unit/test_spiderfoot.py@@ -1416,6 +1416,27 @@ class TestSpiderFoot(unittest.TestCase):new_url = sf.removeUrlCreds(url)self.assertNotIn("secret", new_url)+ def test_useProxyForUrl_argument_url_should_return_a_bool(self):+ """+ Test useProxyForUrl(self, url)+ """+ opts = self.default_options++ proxy_host = 'spiderfoot.net'++ opts['_socks1type'] = '5'+ opts['_socks2addr'] = proxy_host+ opts['_socks3port'] = '8080'++ sf = SpiderFoot(opts)+ self.assertFalse(sf.useProxyForUrl('10.1.1.1'))+ self.assertFalse(sf.useProxyForUrl('172.16.1.1'))+ self.assertFalse(sf.useProxyForUrl('192.168.1.1'))+ self.assertFalse(sf.useProxyForUrl('127.0.0.1'))+ self.assertFalse(sf.useProxyForUrl('localhost'))+ self.assertFalse(sf.useProxyForUrl('test.local'))+ self.assertFalse(sf.useProxyForUrl(proxy_host))+def test_fetchUrl_argument_url_should_return_http_response_as_dict(self):"""Test fetchUrl(self, url, fatal=False, cookies=None, timeout=30,