Skip to content

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

Initial Py2->Py3 efforts.

smicallef9b1d1a7ca1bc
diff --git a/modules/sfp_dnsresolve.py b/modules/sfp_dnsresolve.py
index cee213a5..3908239d 100644
--- a/modules/sfp_dnsresolve.py
+++ b/modules/sfp_dnsresolve.py
@@ -16,7 +16,7 @@ try:
except ImportError as e:
import re
-import urllib2
+import urllib.request, urllib.error, urllib.parse
from netaddr import IPNetwork
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -142,7 +142,7 @@ class sfp_dnsresolve(SpiderFootPlugin):
if eventName not in [ "CO_HOSTED_SITE", "AFFILIATE_INTERNET_NAME",
"NETBLOCK_OWNER", "IP_ADDRESS",
"INTERNET_NAME", "AFFILIATE_IPADDR"]:
- data = urllib2.unquote(eventData)
+ data = urllib.parse.unquote(eventData)
for name in self.getTarget().getNames():
if self.checkForStop():
return None
diff --git a/modules/sfp_duckduckgo.py b/modules/sfp_duckduckgo.py
index 94b17352..40402ea2 100644
--- a/modules/sfp_duckduckgo.py
+++ b/modules/sfp_duckduckgo.py
@@ -63,7 +63,7 @@ class sfp_duckduckgo(SpiderFootPlugin):
else:
self.results[eventData] = True
- url = "https://api.duckduckgo.com/?q=" + eventData + "&format=json&pretty=1"
+ url = "https://api.duckduckgo.com/?q=" + eventData + "&format=json&pretty=1"
res = self.sf.fetchUrl(url, timeout=self.opts['_fetchtimeout'],
useragent="SpiderFoot")
diff --git a/modules/sfp_filemeta.py b/modules/sfp_filemeta.py
index 3fbccfc3..b9d0cdba 100644
--- a/modules/sfp_filemeta.py
+++ b/modules/sfp_filemeta.py
@@ -17,7 +17,7 @@ import docx
import pptx
import exifread
import lxml
-from StringIO import StringIO
+import io
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -93,7 +93,7 @@ class sfp_filemeta(SpiderFootPlugin):
# Based on the file extension, handle it
if fileExt.lower() == "pdf":
try:
- raw = StringIO(ret['content'])
+ raw = io.BytesIO(ret['content'])
#data = metapdf.MetaPdfReader().read_metadata(raw)
pdf = PyPDF2.PdfFileReader(raw, strict=False)
data = pdf.getDocumentInfo()
@@ -106,7 +106,7 @@ class sfp_filemeta(SpiderFootPlugin):
if fileExt.lower() in ["docx"]:
try:
- c = StringIO(ret['content'])
+ c = io.BytesIO(ret['content'])
doc = docx.Document(c)
mtype = mimetypes.guess_type(eventData)[0]
self.sf.debug("Office type: " + str(mtype))
@@ -121,7 +121,7 @@ class sfp_filemeta(SpiderFootPlugin):
if fileExt.lower() in ["pptx"]:
try:
- c = StringIO(ret['content'])
+ c = io.BytesIO(ret['content'])
doc = pptx.Presentation(c)
mtype = mimetypes.guess_type(eventData)[0]
self.sf.debug("Office type: " + str(mtype))
@@ -136,7 +136,7 @@ class sfp_filemeta(SpiderFootPlugin):
if fileExt.lower() in ["jpg", "jpeg", "tiff"]:
try:
- raw = StringIO(ret['content'])
+ raw = io.BytesIO(ret['content'])
data = exifread.process_file(raw)
if data is None or len(data) == 0:
continue
diff --git a/modules/sfp_github.py b/modules/sfp_github.py
index 11088274..90cb87c8 100644
--- a/modules/sfp_github.py
+++ b/modules/sfp_github.py
@@ -213,7 +213,7 @@ class sfp_github(SpiderFootPlugin):
res = self.sf.fetchUrl(url, timeout=self.opts['_fetchtimeout'],
useragent=self.opts['_useragent'])
- if res['content'] == None:
+ if res['content'] == None:
self.sf.error("Unable to fetch " + url, False)
continue
diff --git a/modules/sfp_socialprofiles.py b/modules/sfp_socialprofiles.py
index 9c622c60..3f4c2c49 100644
--- a/modules/sfp_socialprofiles.py
+++ b/modules/sfp_socialprofiles.py
@@ -15,7 +15,7 @@ try:
except ImportError as e:
import re
-import urllib2
+import urllib.request, urllib.error, urllib.parse
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
sites = {
@@ -197,7 +197,7 @@ class sfp_socialprofiles(SpiderFootPlugin):
# for a firm relationship.
# Keywords might be empty if the target was an IP, subnet or name.
if self.opts["tighten"] and self.keywords:
- match = urllib2.unquote(match)
+ match = urllib.parse.unquote(match)
self.sf.debug(
"Tightening results to look for " + str(self.keywords)
)
@@ -222,7 +222,7 @@ class sfp_socialprofiles(SpiderFootPlugin):
continue
self.sf.info("Social Media Profile found at " + site + ": " + match)
- match = urllib2.unquote(match)
+ match = urllib.parse.unquote(match)
evt = SpiderFootEvent(
"SOCIAL_MEDIA", site + ": <SFURL>" + match + "</SFURL>", self.__name__, event
)
diff --git a/modules/sfp_sslcert.py b/modules/sfp_sslcert.py
index 5eeb0b6c..269a0372 100644
--- a/modules/sfp_sslcert.py
+++ b/modules/sfp_sslcert.py
@@ -11,7 +11,7 @@
# -------------------------------------------------------------------------------
import time
-import urlparse
+from urllib.parse import urlparse
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -78,7 +78,7 @@ class sfp_sslcert(SpiderFootPlugin):
return None
# Handle URLs containing port numbers
- u = urlparse.urlparse(eventData)
+ u = urlparse(eventData)
port = 443
if u.port:
port = u.port
diff --git a/modules/sfp_wikipediaedits.py b/modules/sfp_wikipediaedits.py
index 0ceea9dd..a03abea0 100644
--- a/modules/sfp_wikipediaedits.py
+++ b/modules/sfp_wikipediaedits.py
@@ -16,7 +16,7 @@ try:
except ImportError as e:
import re
-from HTMLParser import HTMLParser
+from html.parser import HTMLParser
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
class sfp_wikipediaedits(SpiderFootPlugin):
diff --git a/requirements.txt b/requirements.txt
index f27a2e11..cef165be 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -18,3 +18,4 @@ future
pyOpenSSL>=16.2.0
python-docx>=0.8.10
python-pptx>=0.6.18
+networkx>=2.4
diff --git a/sf.py b/sf.py
index 5704af96..cd505190 100755
--- a/sf.py
+++ b/sf.py
@@ -25,8 +25,8 @@ if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)
deps = ['netaddr', 'dns', 'cherrypy', 'mako', 'socks', 'whois', 'OpenSSL',
- 'PyPDF2', 'openxmllib', 'stem', 'bs4', 'gexf', 'phonenumbers', 'ipaddr',
- 'ipwhois']
+ 'PyPDF2', 'stem', 'bs4', 'phonenumbers', 'ipaddr', 'ipwhois',
+ 'networkx']
for mod in deps:
try:
if mod.startswith("lib."):
@@ -36,7 +36,7 @@ for mod in deps:
__import__(mod)
except ImportError as e:
print("")
- print("Critical Start-up Failure: " + str(e))
+ print(("Critical Start-up Failure: " + str(e)))
print("=================================")
print("It appears you are missing a module required for SpiderFoot")
print("to function. Please refer to the documentation for the list")
@@ -44,7 +44,7 @@ for mod in deps:
print("")
print("Python modules required are: ")
for mod in deps:
- print(" - " + mod)
+ print((" - " + mod))
print("")
print("****************************************************************")
print("Please note that if you are seeing this after doing a git pull")
@@ -99,7 +99,7 @@ sfOptdescs = {
'_debug': "Enable debugging?",
'_internettlds': "List of Internet TLDs.",
'_internettlds_cache': "Hours to cache the Internet TLD list. This can safely be quite a long time given that the list doesn't change too often.",
- '_useragent': "User-Agent string to use for HTTP requests. Prefix with an '@' to randomly select the User Agent from a file containing user agent strings for each request, e.g. @C:\useragents.txt or @/home/bob/useragents.txt. Or supply a URL to load the list from there.",
+ '_useragent': "User-Agent string to use for HTTP requests. Prefix with an '@' to randomly select the User Agent from a file containing user agent strings for each request, e.g. @C:\\useragents.txt or @/home/bob/useragents.txt. Or supply a URL to load the list from there.",
'_dnsserver': "Override the default resolver with another DNS server. For example, 8.8.8.8 is Google's open DNS server.",
'_fetchtimeout': "Number of seconds before giving up on a HTTP request.",
'_socks1type': "SOCKS Server Type. Can be '4', '5', 'HTTP' or 'TOR'",
@@ -187,7 +187,7 @@ if __name__ == '__main__':
if hasattr(sfModules[modName]['object'], 'optdescs'):
sfModules[modName]['optdescs'] = sfModules[modName]['object'].optdescs
- if len(sfModules.keys()) < 1:
+ if len(list(sfModules.keys())) < 1:
print("No modules found in the modules directory.")
sys.exit(-1)
@@ -205,7 +205,7 @@ if __name__ == '__main__':
for m in sorted(sfModules.keys()):
if "__" in m:
continue
- print('{0:25} {1}'.format(m, sfModules[m]['descr']))
+ print(('{0:25} {1}'.format(m, sfModules[m]['descr'])))
sys.exit(0)
if args.types:
@@ -216,7 +216,7 @@ if __name__ == '__main__':
types[r[1]] = r[0]
for t in sorted(types.keys()):
- print('{0:45} {1}'.format(t, types[t]))
+ print(('{0:45} {1}'.format(t, types[t])))
sys.exit(0)
if not args.s:
@@ -245,7 +245,7 @@ if __name__ == '__main__':
modlist = list()
if not args.t and not args.m:
print("WARNING: You didn't specify any modules or types, so all will be enabled.")
- for m in sfModules.keys():
+ for m in list(sfModules.keys()):
if "__" in m:
continue
modlist.append(m)
@@ -327,7 +327,7 @@ if __name__ == '__main__':
# Run the scan
if not args.q:
- print("[*] Modules enabled (" + str(len(modlist)) + "): " + ",".join(modlist))
+ print(("[*] Modules enabled (" + str(len(modlist)) + "): " + ",".join(modlist)))
cfg = sf.configUnserialize(dbh.configGet(), sfConfig)
scanId = sf.genScanInstanceGUID(target)
@@ -353,7 +353,7 @@ if __name__ == '__main__':
continue
if info[5] in [ "ERROR-FAILED", "ABORT-REQUESTED", "ABORTED", "FINISHED" ]:
if not args.q:
- print("[*] Scan completed with status " + info[5])
+ print(("[*] Scan completed with status " + info[5]))
sys.exit(0)
time.sleep(1)
@@ -361,7 +361,7 @@ if __name__ == '__main__':
# Start the web server so you can start looking at results
url = 'http://' + sfConfig['__webaddr'] + ":" + str(sfConfig['__webport']) + sfConfig['__docroot']
- print('Starting web server at %s ...' % url)
+ print(('Starting web server at %s ...' % url))
cherrypy.config.update({
'server.socket_host': sfConfig['__webaddr'],
diff --git a/sflib.py b/sflib.py
index e2ec3efa..edd96b46 100644
--- a/sflib.py
+++ b/sflib.py
@@ -15,10 +15,8 @@ from stem import Signal
from stem.control import Controller
import inspect
import hashlib
-import urllib
+import urllib.request, urllib.parse, urllib.error
import binascii
-import gzip
-import gexf
import json
import re
import os
@@ -29,20 +27,21 @@ import ssl
import sys
import time
import netaddr
-import urllib2
-import StringIO
+import urllib.request, urllib.error, urllib.parse
+import io
import threading
import traceback
import OpenSSL
import cryptography
import dns.resolver
+from networkx import nx
+from networkx.readwrite.gexf import GEXFWriter
from datetime import datetime
from bs4 import BeautifulSoup, SoupStrainer
from copy import deepcopy, copy
# For hiding the SSL warnings coming from the requests lib
import urllib3
-
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
@@ -50,7 +49,7 @@ class SpiderFoot:
dbh = None
GUID = None
savedsock = socket
- urllib2.savedsock = urllib2.socket
+ socksProxy = None
# 'options' is a dictionary of options which changes the behaviour
# of how certain things are done in this module
@@ -72,13 +71,11 @@ class SpiderFoot:
# Bit of a hack to support SOCKS because of the loading order of
# modules. sfscan will call this to update the socket reference
# to the SOCKS one.
- def updateSocket(self, sock):
- socket = sock
- urllib2.socket = sock
+ def updateSocket(self, socksProxy):
+ self.socksProxy = socksProxy
def revertSocket(self):
- socket = self.savedsock
- urllib2.socket = urllib2.savedsock
+ self.socksProxy = None
# Tell TOR to re-circuit
def refreshTorIdent(self):
@@ -126,12 +123,12 @@ class SpiderFoot:
if val.lower().startswith('http://') or val.lower().startswith('https://'):
try:
self.info("Downloading configuration data from: " + val)
- res = urllib2.urlopen(val)
- data = res.read()
+ session = self.getSession()
+ res = session.get(val)
if splitLines:
- return data.splitlines()
+ return res.content.splitlines()
else:
- return data
+ return res.content
except BaseException as e:
if fatal:
self.error("Unable to open option URL, " + val + ": " + str(e))
@@ -197,8 +194,7 @@ class SpiderFoot:
# included.
def buildGraphGexf(self, root, title, data, flt=[]):
mapping = self.buildGraphData(data, flt)
- g = gexf.Gexf(title, title)
- graph = g.addGraph("undirected", "static", "SpiderFoot Export")
+ graph = nx.Graph()
nodelist = dict()
ecounter = 0
@@ -210,28 +206,28 @@ class SpiderFoot:
# Leave out this special case
if dst == "ROOT" or src == "ROOT":
continue
+
if dst not in nodelist:
ncounter = ncounter + 1
if dst in root:
col = ["255", "0", "0"]
- graph.addNode(str(ncounter), unicode(dst, errors="replace"),
- r=col[0], g=col[1], b=col[2])
+ node = graph.add_node(dst)
+ graph.node[dst]['viz'] = {'color': { 'r': col[0], 'g': col[1], 'b': col[2] } }
nodelist[dst] = ncounter
if src not in nodelist:
ncounter = ncounter + 1
if src in root:
col = ["255", "0", "0"]
- graph.addNode(str(ncounter), unicode(src, errors="replace"),
- r=col[0], g=col[1], b=col[2])
+ graph.add_node(src)
+ graph.node[src]['viz'] = {'color': { 'r': col[0], 'g': col[1], 'b': col[2] } }
nodelist[src] = ncounter
ecounter = ecounter + 1
graph.addEdge(str(ecounter), str(nodelist[src]), str(nodelist[dst]))
- output = StringIO.StringIO()
- g.write(output)
- return output.getvalue()
+ gexf = GEXFWriter(graph=graph)
+ return str(gexf).encode('utf-8')
# Convert supplied raw data into JSON format for SigmaJS
def buildGraphJson(self, root, data, flt=list()):
@@ -255,7 +251,7 @@ class SpiderFoot:
if dst in root:
col = "#f00"
ret['nodes'].append({'id': str(ncounter),
- 'label': unicode(dst, errors="replace"),
+ 'label': str(dst, errors="replace"),
'x': random.SystemRandom().randint(1, 1000),
'y': random.SystemRandom().randint(1, 1000),
'size': "1",
@@ -268,7 +264,7 @@ class SpiderFoot:
col = "#f00"
ncounter = ncounter + 1
ret['nodes'].append({'id': str(ncounter),
- 'label': unicode(src, errors="replace"),
+ 'label': str(src, errors="replace"),
'x': random.SystemRandom().randint(1, 1000),
'y': random.SystemRandom().randint(1, 1000),
'size': "1",
@@ -315,11 +311,11 @@ class SpiderFoot:
return None
if self.dbh is None:
- print('[Error] ' + error)
+ print(('[Error] ' + error))
else:
self._dblog("ERROR", error)
if self.opts.get('__logstdout'):
- print("[Error] " + error)
+ print(("[Error] " + error))
if exception:
raise BaseException("Internal Error Encountered: " + error)
@@ -328,7 +324,7 @@ class SpiderFoot:
print(('[Fatal] %s' % error))
else:
self._dblog("FATAL", error)
- print(str(inspect.stack()))
+ print((str(inspect.stack())))
sys.exit(-1)
def status(self, message):
@@ -404,16 +400,16 @@ class SpiderFoot:
# Determine whether we've been compiled by py2exe
if hasattr(sys, "frozen"):
- return os.path.dirname(unicode(sys.executable, sys.getfilesystemencoding()))
+ return os.path.dirname(sys.executable)
- return os.path.dirname(unicode(__file__, sys.getfilesystemencoding()))
+ return os.path.dirname(__file__)
def hashstring(self, string):
s = string
if type(string) in [list, dict]:
s = str(string)
if type(s) == str:
- s = unicode(s, 'utf-8', errors='replace')
+ s = str(s, 'utf-8', errors='replace')
return hashlib.sha256(s.encode('raw_unicode_escape')).hexdigest()
#
@@ -470,7 +466,7 @@ class SpiderFoot:
def configSerialize(self, opts, filterSystem=True):
storeopts = dict()
- for opt in opts.keys():
+ for opt in list(opts.keys()):
# Filter out system temporary variables like GUID and others
if opt.startswith('__') and filterSystem:
continue
@@ -517,7 +513,7 @@ class SpiderFoot:
returnOpts = referencePoint
# Global options
- for opt in referencePoint.keys():
+ for opt in list(referencePoint.keys()):
if opt.startswith('__') and filterSystem:
# Leave out system variables
continue
@@ -594,9 +590,9 @@ class SpiderFoot:
# Parse the target and set the targetType
for rxpair in regexToType:
- rx = rxpair.keys()[0]
+ rx = list(rxpair.keys())[0]
if re.match(rx, target, re.IGNORECASE|re.UNICODE):
- targetType = rxpair.values()[0]
+ targetType = list(rxpair.values())[0]
break
return targetType
@@ -604,7 +600,7 @@ class SpiderFoot:
# types specified.
def modulesProducing(self, events):
modlist = list()
- for mod in self.opts['__modules__'].keys():
+ for mod in list(self.opts['__modules__'].keys()):
if self.opts['__modules__'][mod]['provides'] is None:
continue
@@ -620,7 +616,7 @@ class SpiderFoot:
# specified.
def modulesConsuming(self, events):
modlist = list()
- for mod in self.opts['__modules__'].keys():
+ for mod in list(self.opts['__modules__'].keys()):
if self.opts['__modules__'][mod]['consumes'] is None:
continue
@@ -638,7 +634,7 @@ class SpiderFoot:
def eventsFromModules(self, modules):
evtlist = list()
for mod in modules:
- if mod in self.opts['__modules__'].keys():
+ if mod in list(self.opts['__modules__'].keys()):
if self.opts['__modules__'][mod]['provides'] is not None:
for evt in self.opts['__modules__'][mod]['provides']:
evtlist.append(evt)
@@ -650,7 +646,7 @@ class SpiderFoot:
def eventsToModules(self, modules):
evtlist = list()
for mod in modules:
- if mod in self.opts['__modules__'].keys():
+ if mod in list(self.opts['__modules__'].keys()):
if self.opts['__modules__'][mod]['consumes'] is not None:
for evt in self.opts['__modules__'][mod]['consumes']:
evtlist.append(evt)
@@ -803,9 +799,9 @@ class SpiderFoot:
for addr in res:
if type(addr) == list:
for host in addr:
- ret.append(unicode(host, 'utf-8', errors='replace'))
+ ret.append(str(host, 'utf-8', errors='replace'))
else:
- ret.append(unicode(addr, 'utf-8', errors='replace'))
+ ret.append(str(addr, 'utf-8', errors='replace'))
return list(set(ret))
# Verify input is OK to execute
@@ -846,7 +842,7 @@ class SpiderFoot:
w = w.strip().lower()
wd[w.split('/')[0]] = True
- return wd.keys()
+ return list(wd.keys())
# Return dictionary names
def dictnames(self):
@@ -866,7 +862,7 @@ class SpiderFoot:
w = w.strip().lower()
wd[w.split('/')[0]] = True
- return wd.keys()
+ return list(wd.keys())
# Converts a dictionary of k -> array to a nested
@@ -876,7 +872,7 @@ class SpiderFoot:
#print("called")
ret = list()
- if needle not in haystack.keys():
+ if needle not in list(haystack.keys()):
return None
if haystack[needle] is None:
@@ -889,12 +885,12 @@ class SpiderFoot:
# Find the element with no parents, that's our root.
root = None
- for k in data.keys():
+ for k in list(data.keys()):
if data[k] is None:
continue
contender = True
- for ck in data.keys():
+ for ck in list(data.keys()):
if data[ck] is None:
continue
@@ -921,8 +917,8 @@ class SpiderFoot:
def resolveHost(self, host):
try:
# IDNA-encode the hostname in case it contains unicode
- if type(host) != unicode:
- host = unicode(host, "utf-8", errors='replace').encode("idna")
+ if type(host) != str:
+ host = str(host, "utf-8", errors='replace').encode("idna")
else:
host = host.encode("idna")
@@ -1203,7 +1199,7 @@ class SpiderFoot:
return None
try:
- for t in tags.keys():
+ for t in list(tags.keys()):
for lnk in BeautifulSoup(data, "lxml",
parse_only=SoupStrainer(t)).find_all(t):
if lnk.has_attr(tags[t]):
@@ -1217,8 +1213,8 @@ class SpiderFoot:
# Remember the regex will return two vars (two groups captured)
junk = linkTuple[0]
link = linkTuple[1]
- if type(link) != unicode:
- link = unicode(link, 'utf-8', errors='replace')
+ if type(link) != str:
+ link = str(link, 'utf-8', errors='replace')
linkl = link.lower()
absLink = None
@@ -1243,7 +1239,7 @@ class SpiderFoot:
# URL decode links
if '%2f' in linkl:
- link = urllib2.unquote(link)
+ link = urllib.parse.unquote(link)
# Capture the absolute link:
# If the link contains ://, it is already an absolute link
@@ -1276,6 +1272,15 @@ class SpiderFoot:
def urlEncodeUnicode(self, url):
return re.sub('[\x80-\xFF]', lambda c: '%%%02x' % ord(c.group(0)), url)
+ def getSession(self):
+ session = requests.session()
+ if self.socksProxy:
+ session.proxies = {
+ 'http': self.socksProxy,
+ 'https': self.socksProxy,
+ }
+ return session
+
# Fetch a URL, return the response object
def fetchUrl(self, url, fatal=False, cookies=None, timeout=30,
useragent="SpiderFoot", headers=None, noLog=False,
@@ -1337,9 +1342,9 @@ class SpiderFoot:
# Add custom headers
if headers is not None:
- for k in headers.keys():
- if type(headers[k]) != unicode:
- header[k] = unicode(headers[k], 'utf-8', errors='replace')
+ for k in list(headers.keys()):
+ if type(headers[k]) != str:
+ header[k] = str(headers[k], 'utf-8', errors='replace')
else:
header[k] = headers[k]
@@ -1349,8 +1354,8 @@ class SpiderFoot:
" [user-agent: " + header['User-Agent'] + "] [timeout: " + \
str(timeout) + "]")
- hdr = requests.head(url, headers=header, proxies=proxies,
- verify=False, timeout=timeout)
+ hdr = self.getSession().head(url, headers=header, proxies=proxies,
+ verify=verify, timeout=timeout)
size = int(hdr.headers.get('content-length', 0))
result['realurl'] = hdr.headers.get('location', url)
result['code'] = str(hdr.status_code)
@@ -1367,8 +1372,8 @@ class SpiderFoot:
" [user-agent: " + header['User-Agent'] + "] [timeout: " + \
str(timeout) + "]")
- hdr = requests.head(result['realurl'], headers=header, proxies=proxies,
- verify=False, timeout=timeout)
+ hdr = self.getSession().head(result['realurl'], headers=header, proxies=proxies,
+ verify=verify, timeout=timeout)
size = int(hdr.headers.get('content-length', 0))
result['realurl'] = hdr.headers.get('location', result['realurl'])
result['code'] = str(hdr.status_code)
@@ -1390,21 +1395,22 @@ class SpiderFoot:
# MAKE THE REQUEST
#
if postData:
- res = requests.post(url, data=postData, headers=header, proxies=proxies,
- cookies=cookies, timeout=timeout, verify=False)
+ res = self.getSession().post(url, data=postData, headers=header, proxies=proxies,
+ allow_redirects=True, cookies=cookies,
+ timeout=timeout, verify=verify)
else:
- res = requests.get(url, headers=header, proxies=proxies,
- cookies=cookies, timeout=timeout, verify=False)
+ res = self.getSession().get(url, headers=header, proxies=proxies, allow_redirects=True,
+ cookies=cookies, timeout=timeout, verify=verify)
result['headers'] = dict()
for h in res.headers:
- if type(h) != unicode:
- hu = unicode(h, 'utf-8', errors='replace')
+ if type(h) != str:
+ hu = str(h, 'utf-8', errors='replace')
else:
hu = h
v = res.headers.get(h)
- if type(v) != unicode:
- vu = unicode(v, 'utf-8', errors='replace')
+ if type(v) != str:
+ vu = str(v, 'utf-8', errors='replace')
else:
vu = v
result['headers'][hu.lower()] = vu
@@ -1434,7 +1440,7 @@ class SpiderFoot:
if dontMangle:
result['content'] = res.content
else:
- result['content'] = unicode(res.content, 'utf-8', errors='replace')
+ result['content'] = str(res.content, 'utf-8', errors='replace')
if fatal:
res.raise_for_status()
except requests.exceptions.HTTPError as h:
@@ -1491,7 +1497,7 @@ class SpiderFoot:
}
response = self.fetchUrl(
- endpoint + urllib.urlencode(params),
+ endpoint + urllib.parse.urlencode(params),
timeout=opts["timeout"],
)
@@ -1514,9 +1520,9 @@ class SpiderFoot:
"rls": "org.mozilla:en-US:official",
"client": "firefox-a",
}
- search_url = u"https://www.google.com/search?q={search_string}&{params}".format(
+ search_url = "https://www.google.com/search?q={search_string}&{params}".format(
search_string=searchString.replace(" ", "%20"),
- params=urllib.urlencode(params)
+ params=urllib.parse.urlencode(params)
)
results = {
"urls": [str(k['link']) for k in response_json['items']],
@@ -1548,7 +1554,7 @@ class SpiderFoot:
}
response = self.fetchUrl(
- endpoint + urllib.urlencode(params),
+ endpoint + urllib.parse.urlencode(params),
timeout=opts["timeout"],
useragent=opts["useragent"],
headers={"Ocp-Apim-Subscription-Key": opts["api_key"]},
@@ -1608,9 +1614,8 @@ class SpiderFootPlugin(object):
# Hack to override module's use of socket, replacing it with
# one that uses the supplied SOCKS server
- def _updateSocket(self, sock):
- socket = sock
- urllib2.socket = sock
+ def _updateSocket(self, socksProxy):
+ self.socksProxy = socksProxy
# Used to clear any listener relationships, etc. This is needed because
# Python seems to cache local variables even between threads.
@@ -1678,11 +1683,11 @@ class SpiderFootPlugin(object):
# Convert strings to unicode
if type(sfEvent.data) == str:
- sfEvent.data = unicode(sfEvent.data, 'utf-8', errors='replace')
+ sfEvent.data = str(sfEvent.data, 'utf-8', errors='replace')
storeOnly = False # Under some conditions, only store and don't notify
- if sfEvent.data is None or (type(sfEvent.data) is unicode and len(sfEvent.data) == 0):
+ if sfEvent.data is None or (type(sfEvent.data) is str and len(sfEvent.data) == 0):
#print("No data to send for " + eventName + " to " + listener.__module__)
return None
@@ -1793,8 +1798,8 @@ class SpiderFootTarget(object):
def __init__(self, targetValue, typeName):
if typeName in self._validTypes:
self.targetType = typeName
- if type(targetValue) != unicode:
- self.targetValue = unicode(targetValue, 'utf-8', errors='replace').lower()
+ if type(targetValue) != str:
+ self.targetValue = str(targetValue, 'utf-8', errors='replace').lower()
else:
self.targetValue = targetValue
self.targetAliases = list()
@@ -1925,14 +1930,14 @@ class SpiderFootEvent(object):
self.module = module
self.sourceEvent = sourceEvent
- if type(data) != str and type(data) != unicode:
+ if type(data) != str and type(data) != str:
print(("FATAL: Only string events are accepted, not '%s'." % type(data)))
print(("FATAL: Offending module: %s" % module))
print(("FATAL: Offending type: %s" % eventType))
sys.exit(-1)
- if type(data) != unicode and data != None:
- self.data = unicode(data, 'utf-8', errors='replace')
+ if type(data) != str and data != None:
+ self.data = str(data, 'utf-8', errors='replace')
else:
self.data = data
@@ -2046,7 +2051,7 @@ class PublicSuffixList(object):
if len(node) == 1:
return node[0]
- return (node[0], dict((k, self._simplify(v)) for (k, v) in node[1].items()))
+ return (node[0], dict((k, self._simplify(v)) for (k, v) in list(node[1].items())))
def _build_structure(self, fp):
root = [0]
diff --git a/sfscan.py b/sfscan.py
index 1a52b77d..780c95de 100644
--- a/sfscan.py
+++ b/sfscan.py
@@ -49,8 +49,8 @@ class SpiderFootScanner(threading.Thread):
# Set the status of the currently running scan (if any)
def setStatus(self, status, started=None, ended=None):
if self.ts is None:
- print("Internal Error: Status set attempted before " + \
- "SpiderFootScanner was ready.")
+ print(("Internal Error: Status set attempted before " + \
+ "SpiderFootScanner was ready."))
exit(-1)
self.ts.status = status
@@ -102,69 +102,32 @@ class SpiderFootScanner(threading.Thread):
try:
# Process global options that point to other places for data
- # Save default socket methods that will be overridden
- if not hasattr(socket, 'savedsocket'):
- socket.savedsocket = socket.socket
- socket.savedcreate_connection = socket.create_connection
- #socket.savedgetaddrinfo = socket.getaddrinfo
-
# If a SOCKS server was specified, set it up
if self.ts.config['_socks1type'] != '':
- socksType = socks.PROXY_TYPE_SOCKS4
socksDns = self.ts.config['_socks6dns']
socksAddr = self.ts.config['_socks2addr']
socksPort = int(self.ts.config['_socks3port'])
- socksUsername = ''
- socksPassword = ''
+ socksUsername = self.ts.config['_socks4user'] or ''
+ socksPassword = self.ts.config['_socks5pwd'] or ''
+ creds = ""
+ if socksUsername and socksPassword:
+ creds = socksUsername + ":" + socksPassword + "@"
+ proxy = creds + socksAddr + ":" + socksPort
if self.ts.config['_socks1type'] == '4':
- socksType = socks.PROXY_TYPE_SOCKS4
- if self.ts.config['_socks1type'] == '5':
- socksType = socks.PROXY_TYPE_SOCKS5
- socksUsername = self.ts.config['_socks4user']
- socksPassword = self.ts.config['_socks5pwd']
-
- if self.ts.config['_socks1type'] == 'HTTP':
- socksType = socks.PROXY_TYPE_HTTP
-
- if self.ts.config['_socks1type'] == 'TOR':
- socksType = socks.PROXY_TYPE_SOCKS5
+ proxy = 'socks4://' + proxy
+ elif self.ts.config['_socks1type'] == '5':
+ proxy = 'socks5://' + proxy
+ elif self.ts.config['_socks1type'] == 'HTTP':
+ proxy = 'http://' + proxy
+ elif self.ts.config['_socks1type'] == 'TOR':
+ proxy = 'socks5h://' + proxy
self.ts.sf.debug("SOCKS: " + socksAddr + ":" + str(socksPort) + \
"(" + socksUsername + ":" + socksPassword + ")")
- socks.setdefaultproxy(socksType, socksAddr, socksPort,
- socksDns, socksUsername, socksPassword)
-
- # Override the default socket and getaddrinfo calls with the
- # SOCKS ones. Just ensure we don't also try and SOCKS-proxy
- # connectivity to the TOR control port.
- def _create_connection(address, timeout=None, source_address=None):
- if socksAddr not in address:
- sock = socks.socksocket()
- sock.setproxy(socks.PROXY_TYPE_SOCKS5, socksAddr, socksPort)
- sock.settimeout(self.ts.config['_fetchtimeout'])
- sock.connect(address)
- return sock
- else:
- sock = socket.socket
- sock.settimeout(self.ts.config['_fetchtimeout'])
- sock.connect(address)
- return sock
-
- socket.socket = socks.socksocket
- socket.setdefaulttimeout(self.ts.config['_fetchtimeout'])
- socket.create_connection = _create_connection
- #socket.getaddrinfo = socks.getaddrinfo
- self.ts.sf.updateSocket(socket)
+
+ self.ts.sf.updateSocket(proxy)
else:
- # BUG: If the user had a SOCKS proxy set
- # and then decided to unset it, the original socket class
- # is not reverted to its default state - we still have
- # the SOCKS version of socket.
- socket.socket = socket.savedsocket
- socket.setdefaulttimeout(self.ts.config['_fetchtimeout'])
- socket.create_connection = socket.savedcreate_connection
- #socket.getaddrinfo = socket.savedgetaddrinfo
self.ts.sf.revertSocket()
# Override the default DNS server
@@ -211,7 +174,7 @@ class SpiderFootScanner(threading.Thread):
# Set up the module
# Configuration is a combined global config with module-specific options
self.ts.modconfig[modName] = deepcopy(self.ts.config['__modules__'][modName]['opts'])
- for opt in self.ts.config.keys():
+ for opt in list(self.ts.config.keys()):
self.ts.modconfig[modName][opt] = deepcopy(self.ts.config[opt])
mod.clearListeners() # clear any listener relationships from the past
@@ -238,11 +201,11 @@ class SpiderFootScanner(threading.Thread):
self.ts.sf.status(modName + " module loaded.")
# Register listener modules and then start all modules sequentially
- for module in self.ts.moduleInstances.values():
+ for module in list(self.ts.moduleInstances.values()):
# Register the target with the module
module.setTarget(target)
- for listenerModule in self.ts.moduleInstances.values():
+ for listenerModule in list(self.ts.moduleInstances.values()):
# Careful not to register twice or you will get duplicate events
if listenerModule in module._listenerModules:
continue
@@ -260,7 +223,7 @@ class SpiderFootScanner(threading.Thread):
psMod.__name__ = "SpiderFoot UI"
psMod.setTarget(target)
psMod.clearListeners()
- for mod in self.ts.moduleInstances.values():
+ for mod in list(self.ts.moduleInstances.values()):
if mod.watchedEvents() is not None:
psMod.registerListener(mod)
@@ -284,7 +247,7 @@ class SpiderFootScanner(threading.Thread):
# Check in case the user requested to stop the scan between modules
# initializing
- for module in self.ts.moduleInstances.values():
+ for module in list(self.ts.moduleInstances.values()):
if module.checkForStop():
self.setStatus('ABORTING')
aborted = True