Skip to content

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

Move host resolution to sflib.

smicallefed6f5d93475f
diff --git a/modules/sfp_binaryedge.py b/modules/sfp_binaryedge.py
index a5aa2a27..4a34a71e 100644
--- a/modules/sfp_binaryedge.py
+++ b/modules/sfp_binaryedge.py
@@ -12,7 +12,6 @@
import json
import time
-import socket
from netaddr import IPNetwork
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -85,21 +84,6 @@ class sfp_binaryedge(SpiderFootPlugin):
"UDP_PORT_OPEN", "UDP_PORT_OPEN_INFO",
"CO_HOSTED_SITE", "MALICIOUS_IPADDR"]
- # Verify a host resolves
- 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")
- else:
- host = host.encode("idna")
-
- addrs = socket.gethostbyname_ex(host)
- return True
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
def query(self, qry, querytype, page=1):
ret = None
retarr = list()
@@ -232,7 +216,7 @@ class sfp_binaryedge(SpiderFootPlugin):
continue
if self.getTarget().matches(host, includeParents=True):
if self.opts['verify']:
- if not self.resolveHost(host):
+ if not self.sf.resolveHost(host):
continue
evt = SpiderFootEvent("INTERNET_NAME", host, self.__name__, event)
self.notifyListeners(evt)
@@ -280,7 +264,7 @@ class sfp_binaryedge(SpiderFootPlugin):
else:
self.reportedhosts[rec] = True
if self.opts['verify']:
- if not self.resolveHost(rec):
+ if not self.sf.resolveHost(rec):
self.sf.debug("Couldn't resolve " + rec + ", so skipping.")
continue
e = SpiderFootEvent(evtType, rec, self.__name__, event)
diff --git a/modules/sfp_bingsharedip.py b/modules/sfp_bingsharedip.py
index 3f34e781..81f5058f 100644
--- a/modules/sfp_bingsharedip.py
+++ b/modules/sfp_bingsharedip.py
@@ -10,7 +10,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
from netaddr import IPNetwork
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -60,23 +59,6 @@ class sfp_bingsharedip(SpiderFootPlugin):
def producedEvents(self):
return ["CO_HOSTED_SITE", "IP_ADDRESS", "RAW_RIR_DATA"]
- def validateIP(self, host, ip):
- try:
- addrs = socket.gethostbyname_ex(host)
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
- for addr in addrs:
- if type(addr) == list:
- for a in addr:
- if str(a) == ip:
- return True
- else:
- if str(addr) == ip:
- return True
- return False
-
# Handle events sent to this module
def handleEvent(self, event):
eventName = event.eventType
@@ -151,7 +133,7 @@ class sfp_bingsharedip(SpiderFootPlugin):
+ " because it is on the same domain."
)
continue
- if self.opts["verify"] and not self.validateIP(site, ip):
+ if self.opts["verify"] and not self.sf.validateIP(site, ip):
self.sf.debug("Host " + site + " no longer resolves to " + ip)
continue
# Create an IP Address event stemming from the netblock as the
diff --git a/modules/sfp_circllu.py b/modules/sfp_circllu.py
index eb0acbe1..3f34e678 100644
--- a/modules/sfp_circllu.py
+++ b/modules/sfp_circllu.py
@@ -14,7 +14,6 @@ import json
import base64
import re
import time
-import socket
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
class sfp_circllu(SpiderFootPlugin):
@@ -58,24 +57,6 @@ class sfp_circllu(SpiderFootPlugin):
for opt in userOpts.keys():
self.opts[opt] = userOpts[opt]
- # Verify a host resolves to an IP
- def validateIP(self, host, ip):
- try:
- addrs = socket.gethostbyname_ex(host)
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
- for addr in addrs:
- if type(addr) == list:
- for a in addr:
- if str(a) == ip:
- return True
- else:
- if str(addr) == ip:
- return True
- return False
-
# What events is this module interested in for input
def watchedEvents(self):
return ["INTERNET_NAME", "NETBLOCK_OWNER", "IP_ADDRESS", "DOMAIN_NAME"]
@@ -217,7 +198,7 @@ class sfp_circllu(SpiderFootPlugin):
cohosts.append(rec['rrname'])
for co in cohosts:
- if eventName == "IP_ADDRESS" and (self.opts['verify'] and not self.validateIP(co, eventData)):
+ if eventName == "IP_ADDRESS" and (self.opts['verify'] and not self.sf.validateIP(co, eventData)):
self.sf.debug("Host no longer resolves to our IP.")
continue
diff --git a/modules/sfp_cleanbrowsing.py b/modules/sfp_cleanbrowsing.py
index 205703a3..c637acf9 100644
--- a/modules/sfp_cleanbrowsing.py
+++ b/modules/sfp_cleanbrowsing.py
@@ -11,7 +11,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
import dns.resolver
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -79,7 +78,7 @@ class sfp_cleanbrowsing(SpiderFootPlugin):
# Check that it resolves first, as it becomes a valid
# malicious host only if NOT resolved by Cleanbrowsing.org.
try:
- if self.sf.normalizeDNS(socket.gethostbyname_ex(eventData)):
+ if self.sf.resolveHost(eventData):
resolved = True
except BaseException as e:
return None
diff --git a/modules/sfp_comodo.py b/modules/sfp_comodo.py
index bd518a67..f61158eb 100644
--- a/modules/sfp_comodo.py
+++ b/modules/sfp_comodo.py
@@ -11,7 +11,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
import dns.resolver
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -79,7 +78,7 @@ class sfp_comodo(SpiderFootPlugin):
# Check that it resolves first, as it becomes a valid
# malicious host only if NOT resolved by Comodo.
try:
- if self.sf.normalizeDNS(socket.gethostbyname_ex(eventData)):
+ if self.sf.resolveHost(eventData):
resolved = True
except BaseException as e:
return None
diff --git a/modules/sfp_dnsneighbor.py b/modules/sfp_dnsneighbor.py
index 73aa23e0..e7b30e2a 100644
--- a/modules/sfp_dnsneighbor.py
+++ b/modules/sfp_dnsneighbor.py
@@ -11,7 +11,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
from netaddr import IPAddress
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -33,16 +32,12 @@ class sfp_dnsneighbor(SpiderFootPlugin):
events = dict()
domresults = dict()
hostresults = dict()
- resolveCache = dict()
- resolveCache6 = dict()
def setup(self, sfc, userOpts=dict()):
self.sf = sfc
self.events = dict()
self.domresults = dict()
self.hostresults = dict()
- self.resolveCache = dict()
- self.resolveCache6 = dict()
self.__dataSource__ = "DNS"
for opt in userOpts.keys():
@@ -97,8 +92,8 @@ class sfp_dnsneighbor(SpiderFootPlugin):
s += 1
continue
- addrs = self.resolveIP(sip)
- if len(addrs) == 0:
+ addrs = self.sf.resolveIP(sip)
+ if not addrs:
self.sf.debug("Look-aside resolve for " + sip + " failed.")
s += 1
continue
@@ -139,48 +134,6 @@ class sfp_dnsneighbor(SpiderFootPlugin):
self.processHost(addr, parent, True)
s += 1
- # Resolve an IP
- def resolveIP(self, ipaddr):
- ret = list()
- self.sf.debug("Performing reverse-resolve of " + ipaddr)
-
- if ipaddr in self.resolveCache:
- self.sf.debug("Returning cached result for " + ipaddr + " (" +
- str(self.resolveCache[ipaddr]) + ")")
- return self.resolveCache[ipaddr]
-
- try:
- addrs = self.sf.normalizeDNS(socket.gethostbyaddr(ipaddr))
- self.resolveCache[ipaddr] = addrs
- self.sf.debug("Resolved " + ipaddr + " to: " + str(addrs))
- return addrs
- except BaseException as e:
- self.sf.debug("Unable to resolve " + ipaddr + " (" + str(e) + ")")
- self.resolveCache[ipaddr] = list()
- return ret
-
- # Resolve a host
- def resolveHost(self, hostname):
- if hostname in self.resolveCache:
- self.sf.debug("Returning cached result for " + hostname + " (" +
- str(self.resolveCache[hostname]) + ")")
- return self.resolveCache[hostname]
-
- try:
- # IDNA-encode the hostname in case it contains unicode
- if type(hostname) != unicode:
- hostname = unicode(hostname, "utf-8", errors='replace').encode("idna")
- else:
- hostname = hostname.encode("idna")
-
- addrs = self.sf.normalizeDNS(socket.gethostbyname_ex(hostname))
- self.resolveCache[hostname] = addrs
- self.sf.debug("Resolved " + hostname + " to: " + str(addrs))
- return addrs
- except BaseException as e:
- self.sf.debug("Unable to resolve " + hostname + " (" + str(e) + ")")
- return list()
-
# Process a host/IP, parentEvent is the event that represents this entity
def processHost(self, host, parentEvent, affiliate=None):
parentHash = self.sf.hashstring(parentEvent.data)
@@ -203,9 +156,11 @@ class sfp_dnsneighbor(SpiderFootPlugin):
# If the IP the host resolves to is in our
# list of aliases,
if not self.sf.validIP(host):
- for hostip in self.resolveHost(host):
- if self.getTarget().matches(hostip):
- affil = False
+ hostips = self.sf.resolveHost(host)
+ if hostips:
+ for hostip in hostips:
+ if self.getTarget().matches(hostip):
+ affil = False
else:
affil = affiliate
diff --git a/modules/sfp_dnsraw.py b/modules/sfp_dnsraw.py
index 67ea20df..ca42bd6d 100644
--- a/modules/sfp_dnsraw.py
+++ b/modules/sfp_dnsraw.py
@@ -11,7 +11,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
import re
import dns
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -54,24 +53,6 @@ class sfp_dnsraw(SpiderFootPlugin):
'INTERNET_NAME', 'INTERNET_NAME_UNRESOLVED',
'AFFILIATE_DOMAIN', 'AFFILIATE_DOMAIN_UNRESOLVED']
- # Resolve a host
- 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")
- else:
- host = host.encode("idna")
-
- addrs = socket.gethostbyname_ex(host)
- if not addrs:
- return False
-
- return True
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
# Handle events sent to this module
def handleEvent(self, event):
eventName = event.eventType
@@ -148,7 +129,7 @@ class sfp_dnsraw(SpiderFootPlugin):
else:
evt_type = 'AFFILIATE_DOMAIN'
- if self.opts['verify'] and not self.resolveHost(domain):
+ if self.opts['verify'] and not self.sf.resolveHost(domain):
self.sf.debug("Host " + domain + " could not be resolved")
evt_type += '_UNRESOLVED'
diff --git a/modules/sfp_dnsresolve.py b/modules/sfp_dnsresolve.py
index 520ea56d..66662c9a 100644
--- a/modules/sfp_dnsresolve.py
+++ b/modules/sfp_dnsresolve.py
@@ -11,7 +11,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
import re
import urllib2
from netaddr import IPNetwork
@@ -39,16 +38,12 @@ class sfp_dnsresolve(SpiderFootPlugin):
events = dict()
domresults = dict()
hostresults = dict()
- resolveCache = dict()
- resolveCache6 = dict()
def setup(self, sfc, userOpts=dict()):
self.sf = sfc
self.events = dict()
self.domresults = dict()
self.hostresults = dict()
- self.resolveCache = dict()
- self.resolveCache6 = dict()
self.__dataSource__ = "DNS"
for opt in userOpts.keys():
@@ -58,9 +53,9 @@ class sfp_dnsresolve(SpiderFootPlugin):
ret = list()
# If it's an IP, get the hostname it reverse resolves to
if target.getType() == "IP_ADDRESS":
- ret = self.resolveIP(target.getValue())
+ ret = self.sf.resolveIP(target.getValue())
if target.getType() == "INTERNET_NAME":
- ret = self.resolveHost(target.getValue())
+ ret = self.sf.resolveHost(target.getValue())
if target.getType() == "NETBLOCK_OWNER":
ret = list()
for addr in IPNetwork(target.getValue()):
@@ -72,16 +67,19 @@ class sfp_dnsresolve(SpiderFootPlugin):
ret.append(ipaddr)
# Add the reverse-resolved hostnames as aliases too..
- names = self.resolveIP(ipaddr)
+ names = self.sf.resolveIP(ipaddr)
if self.opts['validatereverse']:
for host in names:
- chk = self.resolveHost(host)
+ chk = self.sf.resolveHost(host)
if ipaddr in chk:
ret.append(host)
else:
ret.extend(names)
+ if not ret:
+ return target
+
for host in ret:
if self.sf.validIP(host):
target.setAlias(host, "IP_ADDRESS")
@@ -201,7 +199,7 @@ class sfp_dnsresolve(SpiderFootPlugin):
if self.checkForStop():
return None
- addrs = self.resolveIP(ipaddr)
+ addrs = self.sf.resolveIP(ipaddr)
if len(addrs) > 0:
self.sf.debug("Found a reversed hostname from " + ipaddr +
@@ -220,9 +218,12 @@ class sfp_dnsresolve(SpiderFootPlugin):
"AFFILIATE_IPADDR", "AFFILIATE_INTERNET_NAME"]:
if "_NAME" in eventName:
- addrs = self.resolveHost(eventData)
+ addrs = self.sf.resolveHost(eventData)
else:
- addrs = self.resolveIP(eventData)
+ addrs = self.sf.resolveIP(eventData)
+
+ if not addrs:
+ return None
# We now have a set of hosts/IPs to do something with.
for addr in addrs:
@@ -240,72 +241,6 @@ class sfp_dnsresolve(SpiderFootPlugin):
else:
self.processHost(addr, parentEvent, True)
- # Resolve an IP
- def resolveIP(self, ipaddr):
- ret = list()
- self.sf.debug("Performing reverse-resolve of " + ipaddr)
-
- if ipaddr in self.resolveCache:
- self.sf.debug("Returning cached result for " + ipaddr + " (" +
- str(self.resolveCache[ipaddr]) + ")")
- return self.resolveCache[ipaddr]
-
- try:
- addrs = self.sf.normalizeDNS(socket.gethostbyaddr(ipaddr))
- self.resolveCache[ipaddr] = addrs
- self.sf.debug("Resolved " + ipaddr + " to: " + str(addrs))
- return addrs
- except BaseException as e:
- self.sf.debug("Unable to resolve " + ipaddr + " (" + str(e) + ")")
- self.resolveCache[ipaddr] = list()
- return ret
-
- # Resolve a host
- def resolveHost(self, hostname):
- if not hostname:
- return list()
-
- if hostname in self.resolveCache:
- self.sf.debug("Returning cached result for " + hostname + " (" +
- str(self.resolveCache[hostname]) + ")")
- return self.resolveCache[hostname]
-
- try:
- # IDNA-encode the hostname in case it contains unicode
- if type(hostname) != unicode:
- hostname = unicode(hostname, "utf-8", errors='replace').encode("idna")
- else:
- hostname = hostname.encode("idna")
- addrs = self.sf.normalizeDNS(socket.gethostbyname_ex(hostname))
- self.resolveCache[hostname] = addrs
- self.sf.debug("Resolved " + hostname + " to: " + str(addrs))
- return addrs
- except BaseException as e:
- self.sf.debug("Unable to resolve " + hostname + " (" + str(e) + ")")
- return list()
-
- # Resolve a host to IPv6
- def resolveHost6(self, hostname):
- if hostname in self.resolveCache6:
- self.sf.debug("Returning IPv6 cached result for " + hostname + " (" +
- str(self.resolveCache6[hostname]) + ")")
- return self.resolveCache6[hostname]
-
- try:
- addrs = list()
- res = socket.getaddrinfo(hostname, None, socket.AF_INET6)
- for addr in res:
- if addr[4][0] not in addrs:
- addrs.append(addr[4][0])
- if len(addrs) < 1:
- return None
- self.resolveCache6[hostname] = addrs
- self.sf.debug("Resolved " + hostname + " to IPv6: " + str(addrs))
- return addrs
- except BaseException as e:
- self.sf.debug("Unable to IPv6 resolve " + hostname + " (" + str(e) + ")")
- return list()
-
# Process a host/IP, parentEvent is the event that represents this entity
def processHost(self, host, parentEvent, affiliate=None):
parentHash = self.sf.hashstring(parentEvent.data)
@@ -328,9 +263,11 @@ class sfp_dnsresolve(SpiderFootPlugin):
# If the IP the host resolves to is in our
# list of aliases,
if not self.sf.validIP(host):
- for hostip in self.resolveHost(host):
- if self.getTarget().matches(hostip):
- affil = False
+ hostips = self.sf.resolveHost(host)
+ if hostips:
+ for hostip in hostips:
+ if self.getTarget().matches(hostip):
+ affil = False
else:
affil = affiliate
@@ -346,7 +283,7 @@ class sfp_dnsresolve(SpiderFootPlugin):
htype = "INTERNET_NAME"
if htype.endswith("INTERNET_NAME"):
- resolved = len(self.resolveHost(host)) > 0
+ resolved = self.sf.resolveHost(host)
if htype == "INTERNET_NAME" and not resolved:
evt = SpiderFootEvent("INTERNET_NAME_UNRESOLVED", host,
self.__name__, parentEvent)
@@ -368,7 +305,10 @@ class sfp_dnsresolve(SpiderFootPlugin):
self.processDomain(dom, evt)
# Try obtain the IPv6 address
- for ip6 in self.resolveHost6(host):
+ ip6s = self.sf.resolveHost6(host)
+ if not ip6s:
+ return None
+ for ip6 in ip6s:
evt6 = SpiderFootEvent("IPV6_ADDRESS", ip6, self.__name__, evt)
self.notifyListeners(evt6)
diff --git a/modules/sfp_dronebl.py b/modules/sfp_dronebl.py
index c402e1bf..620d31ab 100644
--- a/modules/sfp_dronebl.py
+++ b/modules/sfp_dronebl.py
@@ -12,7 +12,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
from netaddr import IPNetwork
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -94,7 +93,9 @@ class sfp_dronebl(SpiderFootPlugin):
try:
lookup = self.reverseAddr(qaddr) + "." + domain
self.sf.debug("Checking Blacklist: " + lookup)
- addrs = self.sf.normalizeDNS(socket.gethostbyname_ex(lookup))
+ addrs = self.sf.resolveHost(lookup)
+ if not addrs:
+ continue
self.sf.debug("Addresses returned: " + str(addrs))
text = None
diff --git a/modules/sfp_fsecure_riddler.py b/modules/sfp_fsecure_riddler.py
index cf7d79cb..adad4aaa 100644
--- a/modules/sfp_fsecure_riddler.py
+++ b/modules/sfp_fsecure_riddler.py
@@ -127,24 +127,6 @@ class sfp_fsecure_riddler(SpiderFootPlugin):
return data
- # Resolve a host
- 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")
- else:
- host = host.encode("idna")
-
- addrs = socket.gethostbyname_ex(host)
- if not addrs:
- return False
-
- return True
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
def handleEvent(self, event):
eventName = event.eventType
srcModuleName = event.module
@@ -218,7 +200,7 @@ class sfp_fsecure_riddler(SpiderFootPlugin):
else:
evt_type = 'AFFILIATE_DOMAIN'
- if self.opts['verify'] and not self.resolveHost(domain):
+ if self.opts['verify'] and not self.sf.resolveHost(domain):
self.sf.debug("Host " + domain + " could not be resolved")
evt_type += '_UNRESOLVED'
diff --git a/modules/sfp_hackertarget.py b/modules/sfp_hackertarget.py
index 9cc1ce9e..f7221f39 100644
--- a/modules/sfp_hackertarget.py
+++ b/modules/sfp_hackertarget.py
@@ -14,7 +14,6 @@
import json
import re
-import socket
from netaddr import IPNetwork
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -71,41 +70,6 @@ class sfp_hackertarget(SpiderFootPlugin):
'INTERNET_NAME', 'INTERNET_NAME_UNRESOLVED',
'AFFILIATE_DOMAIN', 'AFFILIATE_DOMAIN_UNRESOLVED']
- def validateIP(self, host, ip):
- try:
- addrs = socket.gethostbyname_ex(host)
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
- for addr in addrs:
- if type(addr) == list:
- for a in addr:
- if str(a) == ip:
- return True
- else:
- if str(addr) == ip:
- return True
- return False
-
- # Resolve a host
- 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")
- else:
- host = host.encode("idna")
-
- addrs = socket.gethostbyname_ex(host)
- if not addrs:
- return False
-
- return True
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
# Port scan for commonly open UDP ports
def portScanUDP(self, ip):
res = self.sf.fetchUrl("https://hackertarget.com/udp-port-scan/", timeout=self.opts['_fetchtimeout'],
@@ -280,7 +244,7 @@ class sfp_hackertarget(SpiderFootPlugin):
else:
evt_type = 'AFFILIATE_DOMAIN'
- if self.opts['verify'] and not self.resolveHost(domain):
+ if self.opts['verify'] and not self.sf.resolveHost(domain):
self.sf.debug("Host " + domain + " could not be resolved")
evt_type += '_UNRESOLVED'
@@ -319,7 +283,7 @@ class sfp_hackertarget(SpiderFootPlugin):
continue
if h not in myres and h != ip:
- if self.opts['verify'] and not self.validateIP(h, ip):
+ if self.opts['verify'] and not self.sf.validateIP(h, ip):
self.sf.debug("Host " + h + " no longer resolves to " + ip)
continue
if self.cohostcount < self.opts['maxcohost']:
diff --git a/modules/sfp_honeypot.py b/modules/sfp_honeypot.py
index 3e091ca7..9a7e56cf 100644
--- a/modules/sfp_honeypot.py
+++ b/modules/sfp_honeypot.py
@@ -11,7 +11,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
from netaddr import IPNetwork
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -111,7 +110,9 @@ class sfp_honeypot(SpiderFootPlugin):
self.reverseAddr(qaddr) + ".dnsbl.httpbl.org"
self.sf.debug("Checking Honeypot: " + lookup)
- addrs = self.sf.normalizeDNS(socket.gethostbyname_ex(lookup))
+ addrs = self.sf.resolveHost(lookup)
+ if not addrs:
+ return None
self.sf.debug("Addresses returned: " + str(addrs))
text = None
diff --git a/modules/sfp_mnemonic.py b/modules/sfp_mnemonic.py
index 9d2d3bb2..4e31d51d 100644
--- a/modules/sfp_mnemonic.py
+++ b/modules/sfp_mnemonic.py
@@ -13,7 +13,6 @@
import json
import time
-import socket
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
class sfp_mnemonic(SpiderFootPlugin):
@@ -51,24 +50,6 @@ class sfp_mnemonic(SpiderFootPlugin):
for opt in userOpts.keys():
self.opts[opt] = userOpts[opt]
- # Verify a host resolves to an IP
- def validateIP(self, host, ip):
- try:
- addrs = socket.gethostbyname_ex(host)
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
- for addr in addrs:
- if type(addr) == list:
- for a in addr:
- if str(a) == ip:
- return True
- else:
- if str(addr) == ip:
- return True
- return False
-
# What events is this module interested in for input
def watchedEvents(self):
return ['IP_ADDRESS', 'INTERNET_NAME', 'DOMAIN_NAME']
@@ -181,7 +162,7 @@ class sfp_mnemonic(SpiderFootPlugin):
cohosts.append(r['query'])
for co in cohosts:
- if eventName == "IP_ADDRESS" and (self.opts['verify'] and not self.validateIP(co, eventData)):
+ if eventName == "IP_ADDRESS" and (self.opts['verify'] and not self.sf.validateIP(co, eventData)):
self.sf.debug("Host " + co + " no longer resolves to " + eventData)
continue
diff --git a/modules/sfp_norton.py b/modules/sfp_norton.py
index 09fb7ac0..4f0c8017 100644
--- a/modules/sfp_norton.py
+++ b/modules/sfp_norton.py
@@ -11,7 +11,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
import dns.resolver
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -79,7 +78,7 @@ class sfp_norton(SpiderFootPlugin):
# Check that it resolves first, as it becomes a valid
# malicious host only if NOT resolved by Norton.
try:
- if self.sf.normalizeDNS(socket.gethostbyname_ex(eventData)):
+ if self.sf.resolveHost(eventData):
resolved = True
except BaseException as e:
return None
diff --git a/modules/sfp_opendns.py b/modules/sfp_opendns.py
index 015c9829..68673e78 100644
--- a/modules/sfp_opendns.py
+++ b/modules/sfp_opendns.py
@@ -11,7 +11,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
import dns.resolver
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -79,7 +78,7 @@ class sfp_opendns(SpiderFootPlugin):
# Check that it resolves first, as it becomes a valid
# malicious host only if NOT resolved by OpenDNS.
try:
- if self.sf.normalizeDNS(socket.gethostbyname_ex(eventData)):
+ if self.sf.resolveHost(eventData):
resolved = True
except BaseException as e:
return None
diff --git a/modules/sfp_portscan_tcp.py b/modules/sfp_portscan_tcp.py
index 15c95fec..59093df5 100644
--- a/modules/sfp_portscan_tcp.py
+++ b/modules/sfp_portscan_tcp.py
@@ -12,7 +12,6 @@
# -------------------------------------------------------------------------------
from netaddr import IPNetwork
-import socket
import random
import threading
import time
@@ -93,8 +92,7 @@ class sfp_portscan_tcp(SpiderFootPlugin):
def tryPort(self, ip, port):
try:
- sock = socket.create_connection((ip, port), self.opts['timeout'])
- sock.settimeout(self.opts['timeout'])
+ sock = self.sf.safeSocket(ip, port, self.opts['timeout'])
with self.lock:
self.portResults[ip + ":" + str(port)] = True
except Exception as e:
diff --git a/modules/sfp_quad9.py b/modules/sfp_quad9.py
index ff5d5793..519b8493 100644
--- a/modules/sfp_quad9.py
+++ b/modules/sfp_quad9.py
@@ -11,7 +11,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
import dns.resolver
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -79,7 +78,7 @@ class sfp_quad9(SpiderFootPlugin):
# Check that it resolves first, as it becomes a valid
# malicious host only if NOT resolved by Quad9.
try:
- if self.sf.normalizeDNS(socket.gethostbyname_ex(eventData)):
+ if self.sf.resolveHost(eventData):
resolved = True
except BaseException as e:
return None
diff --git a/modules/sfp_riskiq.py b/modules/sfp_riskiq.py
index 1a0f43af..f651bfdc 100644
--- a/modules/sfp_riskiq.py
+++ b/modules/sfp_riskiq.py
@@ -12,7 +12,6 @@
import json
import base64
-import socket
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
class sfp_riskiq(SpiderFootPlugin):
@@ -55,24 +54,6 @@ class sfp_riskiq(SpiderFootPlugin):
for opt in userOpts.keys():
self.opts[opt] = userOpts[opt]
- # Verify a host resolves to an IP
- def validateIP(self, host, ip):
- try:
- addrs = socket.gethostbyname_ex(host)
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
- for addr in addrs:
- if type(addr) == list:
- for a in addr:
- if str(a) == ip:
- return True
- else:
- if str(addr) == ip:
- return True
- return False
-
# What events is this module interested in for input
def watchedEvents(self):
return ["INTERNET_NAME", "IP_ADDRESS", "DOMAIN_NAME", "EMAILADDR"]
@@ -213,7 +194,7 @@ class sfp_riskiq(SpiderFootPlugin):
cohosts.append(r['focusPoint'])
for co in cohosts:
- if eventName == "IP_ADDRESS" and (self.opts['verify'] and not self.validateIP(co, eventData)):
+ if eventName == "IP_ADDRESS" and (self.opts['verify'] and not self.sf.validateIP(co, eventData)):
self.sf.debug("Host no longer resolves to our IP.")
continue
diff --git a/modules/sfp_robtex.py b/modules/sfp_robtex.py
index 0da9e7bf..384b0d76 100644
--- a/modules/sfp_robtex.py
+++ b/modules/sfp_robtex.py
@@ -10,7 +10,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
import json
import time
from netaddr import IPNetwork
@@ -60,23 +59,6 @@ class sfp_robtex(SpiderFootPlugin):
def producedEvents(self):
return ["CO_HOSTED_SITE", "IP_ADDRESS"]
- def validateIP(self, host, ip):
- try:
- addrs = socket.gethostbyname_ex(host)
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
- for addr in addrs:
- if type(addr) == list:
- for a in addr:
- if str(a) == ip:
- return True
- else:
- if str(addr) == ip:
- return True
- return False
-
# Handle events sent to this module
def handleEvent(self, event):
eventName = event.eventType
@@ -160,7 +142,7 @@ class sfp_robtex(SpiderFootPlugin):
self.sf.debug("Skipping " + r['o'] + " because it is on the same domain.")
continue
- if self.opts['verify'] and not self.validateIP(r['o'], ip):
+ if self.opts['verify'] and not self.sf.validateIP(r['o'], ip):
self.sf.debug("Host " + r['o'] + " no longer resolves to " + ip)
continue
if eventName == "NETBLOCK_OWNER":
diff --git a/modules/sfp_securitytrails.py b/modules/sfp_securitytrails.py
index c79b2547..b822b806 100644
--- a/modules/sfp_securitytrails.py
+++ b/modules/sfp_securitytrails.py
@@ -12,7 +12,6 @@
import json
import time
-import socket
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
class sfp_securitytrails(SpiderFootPlugin):
@@ -62,24 +61,6 @@ class sfp_securitytrails(SpiderFootPlugin):
return ["CO_HOSTED_SITE", "AFFILIATE_DOMAIN", "INTERNET_NAME",
"PROVIDER_HOSTING"]
- # Verify a host resolves to an IP
- def validateIP(self, host, ip):
- try:
- addrs = socket.gethostbyname_ex(host)
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
- for addr in addrs:
- if type(addr) == list:
- for a in addr:
- if str(a) == ip:
- return True
- else:
- if str(addr) == ip:
- return True
- return False
-
# Search SecurityTrails
def query(self, qry, querytype, page=1, accum=None):
info = None
@@ -181,7 +162,7 @@ class sfp_securitytrails(SpiderFootPlugin):
continue
if h not in myres and h != ip:
- if self.opts['verify'] and not self.validateIP(h, ip):
+ if self.opts['verify'] and not self.sf.validateIP(h, ip):
self.sf.debug("Host " + h + " no longer resolves to our IP.")
continue
myres.append(h.lower())
diff --git a/modules/sfp_similar.py b/modules/sfp_similar.py
index b3305e39..91b653d1 100644
--- a/modules/sfp_similar.py
+++ b/modules/sfp_similar.py
@@ -11,7 +11,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
nearchars = {
@@ -142,9 +141,9 @@ class sfp_similar(SpiderFootPlugin):
for d in domlist:
resolved = False
try:
- if len(self.sf.normalizeDNS(socket.gethostbyname_ex(d + tld))) > 0:
+ if self.sf.resolveHost(d + tld):
resolved = True
- if len(self.sf.normalizeDNS(socket.gethostbyname_ex("www." + d + tld))) > 0:
+ if self.sf.resolveHost("www." + d + tld):
resolved = True
if resolved:
diff --git a/modules/sfp_sorbs.py b/modules/sfp_sorbs.py
index 47679b65..caf32cd2 100644
--- a/modules/sfp_sorbs.py
+++ b/modules/sfp_sorbs.py
@@ -12,7 +12,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
from netaddr import IPNetwork
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -87,7 +86,10 @@ class sfp_sorbs(SpiderFootPlugin):
try:
lookup = self.reverseAddr(qaddr) + "." + domain
self.sf.debug("Checking Blacklist: " + lookup)
- addrs = self.sf.normalizeDNS(socket.gethostbyname_ex(lookup))
+ addrs = self.sf.resolveHost(lookup)
+ if not addrs:
+ continue
+
self.sf.debug("Addresses returned: " + str(addrs))
text = None
diff --git a/modules/sfp_spamcop.py b/modules/sfp_spamcop.py
index 643ceb36..f46bfa2b 100644
--- a/modules/sfp_spamcop.py
+++ b/modules/sfp_spamcop.py
@@ -12,7 +12,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
from netaddr import IPNetwork
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -81,9 +80,12 @@ class sfp_spamcop(SpiderFootPlugin):
try:
lookup = self.reverseAddr(qaddr) + "." + domain
self.sf.debug("Checking Blacklist: " + lookup)
- addrs = self.sf.normalizeDNS(socket.gethostbyname_ex(lookup))
+ addrs = self.sf.resolveHost(lookup)
self.sf.debug("Addresses returned: " + str(addrs))
+ if not addrs:
+ continue
+
text = None
for addr in addrs:
if type(self.checks[domain]) is str:
diff --git a/modules/sfp_spamhaus.py b/modules/sfp_spamhaus.py
index 0774ea7a..6aec8197 100644
--- a/modules/sfp_spamhaus.py
+++ b/modules/sfp_spamhaus.py
@@ -12,7 +12,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
from netaddr import IPNetwork
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -90,9 +89,12 @@ class sfp_spamhaus(SpiderFootPlugin):
try:
lookup = self.reverseAddr(qaddr) + "." + domain
self.sf.debug("Checking Blacklist: " + lookup)
- addrs = self.sf.normalizeDNS(socket.gethostbyname_ex(lookup))
+ addrs = self.sf.resolveHost(lookup)
self.sf.debug("Addresses returned: " + str(addrs))
+ if not addrs:
+ continue
+
text = None
for addr in addrs:
if type(self.checks[domain]) is str:
diff --git a/modules/sfp_spyonweb.py b/modules/sfp_spyonweb.py
index 9f141c48..5e329235 100644
--- a/modules/sfp_spyonweb.py
+++ b/modules/sfp_spyonweb.py
@@ -13,7 +13,6 @@
import datetime
import json
-import socket
import time
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -55,24 +54,6 @@ class sfp_spyonweb(SpiderFootPlugin):
for opt in userOpts.keys():
self.opts[opt] = userOpts[opt]
- # Verify a host resolves to an IP
- def validateIP(self, host, ip):
- try:
- addrs = socket.gethostbyname_ex(host)
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
- for addr in addrs:
- if type(addr) == list:
- for a in addr:
- if str(a) == ip:
- return True
- else:
- if str(addr) == ip:
- return True
- return False
-
# What events is this module interested in for input
def watchedEvents(self):
return ['IP_ADDRESS', 'INTERNET_NAME', 'DOMAIN_NAME', 'WEB_ANALYTICS_ID']
@@ -274,7 +255,7 @@ class sfp_spyonweb(SpiderFootPlugin):
self.sf.debug("Record found too old, skipping.")
continue
- if self.opts['verify'] and not self.validateIP(co, eventData):
+ if self.opts['verify'] and not self.sf.validateIP(co, eventData):
self.sf.debug("Host " + co + " no longer resolves to " + eventData)
continue
diff --git a/modules/sfp_sslcert.py b/modules/sfp_sslcert.py
index b6ae01d7..186f42ce 100644
--- a/modules/sfp_sslcert.py
+++ b/modules/sfp_sslcert.py
@@ -10,13 +10,11 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
import ssl
import time
import M2Crypto
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
-
class sfp_sslcert(SpiderFootPlugin):
"""SSL Certificates:Footprint,Investigate:Crawling and Scanning::Gather information about SSL certificates used by the target's HTTPS sites."""
@@ -67,24 +65,6 @@ class sfp_sslcert(SpiderFootPlugin):
"SSL_CERTIFICATE_MISMATCH", "SSL_CERTIFICATE_EXPIRED",
"SSL_CERTIFICATE_EXPIRING", "SSL_CERTIFICATE_RAW"]
- # Resolve a host
- 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")
- else:
- host = host.encode("idna")
-
- addrs = socket.gethostbyname_ex(host)
- if not addrs:
- return False
-
- return True
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
# Handle events sent to this module
def handleEvent(self, event):
eventName = event.eventType
@@ -110,11 +90,7 @@ class sfp_sslcert(SpiderFootPlugin):
self.sf.debug("Testing SSL for: " + eventData + ':' + str(port))
# Re-fetch the certificate from the site and process
try:
- s = socket.socket()
- s.settimeout(int(self.opts['ssltimeout']))
- s.connect((fqdn, port))
- sock = ssl.wrap_socket(s)
- sock.do_handshake()
+ sock = self.sf.safeSSLSocket(fqdn, port, int(self.opts['ssltimeout']))
rawcert = sock.getpeercert(True)
cert = ssl.DER_cert_to_PEM_cert(rawcert)
m2cert = M2Crypto.X509.load_cert_string(str(cert).replace('\r', ''))
@@ -159,7 +135,7 @@ class sfp_sslcert(SpiderFootPlugin):
else:
evt_type = 'AFFILIATE_DOMAIN'
- if self.opts['verify'] and not self.resolveHost(domain):
+ if self.opts['verify'] and not self.sf.resolveHost(domain):
self.sf.debug("Host " + san + " could not be resolved")
evt_type += '_UNRESOLVED'
diff --git a/modules/sfp_ssltools.py b/modules/sfp_ssltools.py
index 6719eecd..35fc7dab 100644
--- a/modules/sfp_ssltools.py
+++ b/modules/sfp_ssltools.py
@@ -12,7 +12,6 @@
import json
import M2Crypto
-import socket
import time
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -101,24 +100,6 @@ class sfp_ssltools(SpiderFootPlugin):
return data
- # Resolve a host
- 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")
- else:
- host = host.encode("idna")
-
- addrs = socket.gethostbyname_ex(host)
- if not addrs:
- return False
-
- return True
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
# Handle events sent to this module
def handleEvent(self, event):
eventName = event.eventType
@@ -205,7 +186,7 @@ class sfp_ssltools(SpiderFootPlugin):
else:
evt_type = 'AFFILIATE_DOMAIN'
- if self.opts['verify'] and not self.resolveHost(domain):
+ if self.opts['verify'] and not self.sf.resolveHost(domain):
self.sf.debug("Host " + san + " could not be resolved")
evt_type += '_UNRESOLVED'
diff --git a/modules/sfp_sublist3r.py b/modules/sfp_sublist3r.py
index 5691d8be..52e10b92 100644
--- a/modules/sfp_sublist3r.py
+++ b/modules/sfp_sublist3r.py
@@ -11,7 +11,6 @@
# -------------------------------------------------------------------------------
import json
-import socket
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
class sfp_sublist3r(SpiderFootPlugin):
@@ -44,22 +43,6 @@ class sfp_sublist3r(SpiderFootPlugin):
for opt in userOpts.keys():
self.opts[opt] = userOpts[opt]
- # Verify a host resolves
- 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")
- else:
- host = host.encode("idna")
-
- addrs = socket.gethostbyname_ex(host)
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
- return True
-
# What events is this module interested in for input
def watchedEvents(self):
return ["DOMAIN_NAME"]
@@ -108,7 +91,7 @@ class sfp_sublist3r(SpiderFootPlugin):
if ret:
for res in ret:
if self.opts['verify']:
- if not self.resolveHost(res):
+ if not self.sf.resolveHost(res):
e = SpiderFootEvent("INTERNET_NAME_UNRESOLVED", res, self.__name__, event)
self.notifyListeners(e)
continue
diff --git a/modules/sfp_threatminer.py b/modules/sfp_threatminer.py
index b4d5fb20..ff01abf0 100644
--- a/modules/sfp_threatminer.py
+++ b/modules/sfp_threatminer.py
@@ -13,7 +13,6 @@
import json
from datetime import datetime
import time
-import socket
from netaddr import IPNetwork
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -72,21 +71,6 @@ class sfp_threatminer(SpiderFootPlugin):
def producedEvents(self):
return ["INTERNET_NAME", "CO_HOSTED_SITE"]
- # Verify a host resolves
- 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")
- else:
- host = host.encode("idna")
-
- addrs = socket.gethostbyname_ex(host)
- return True
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
def query(self, qry, querytype):
if self.sf.validIP(qry):
tgttype = "host"
@@ -193,7 +177,7 @@ class sfp_threatminer(SpiderFootPlugin):
continue
if self.getTarget().matches(host, includeParents=True):
if self.opts['verify']:
- if not self.resolveHost(host):
+ if not self.sf.resolveHost(host):
continue
evt = SpiderFootEvent("INTERNET_NAME", host, self.__name__, event)
self.notifyListeners(evt)
@@ -223,7 +207,7 @@ class sfp_threatminer(SpiderFootPlugin):
else:
self.reportedhosts[rec] = True
if self.opts['verify']:
- if not self.resolveHost(rec):
+ if not self.sf.resolveHost(rec):
self.sf.debug("Couldn't resolve " + rec + ", so skipping.")
continue
e = SpiderFootEvent(evtType, rec, self.__name__, event)
diff --git a/modules/sfp_tldsearch.py b/modules/sfp_tldsearch.py
index acd13a79..987b5123 100644
--- a/modules/sfp_tldsearch.py
+++ b/modules/sfp_tldsearch.py
@@ -11,7 +11,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
import random
import threading
import time
@@ -64,13 +63,14 @@ class sfp_tldsearch(SpiderFootPlugin):
def tryTld(self, target, tld):
if self.opts['skipwildcards'] and self.sf.checkDnsWildcard(tld):
return None
- try:
- addrs = socket.gethostbyname_ex(target)
- with self.lock:
- self.tldResults[target] = True
- except BaseException as e:
+
+ addrs = self.sf.resolveHost(target)
+ if not addrs:
with self.lock:
self.tldResults[target] = False
+ else:
+ with self.lock:
+ self.tldResults[target] = True
def tryTldWrapper(self, tldList, sourceEvent):
self.tldResults = dict()
diff --git a/modules/sfp_uceprotect.py b/modules/sfp_uceprotect.py
index 8e514387..f969e4c9 100644
--- a/modules/sfp_uceprotect.py
+++ b/modules/sfp_uceprotect.py
@@ -12,7 +12,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
from netaddr import IPNetwork
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -82,9 +81,12 @@ class sfp_uceprotect(SpiderFootPlugin):
try:
lookup = self.reverseAddr(qaddr) + "." + domain
self.sf.debug("Checking Blacklist: " + lookup)
- addrs = self.sf.normalizeDNS(socket.gethostbyname_ex(lookup))
+ addrs = self.sf.resolveHost(lookup)
self.sf.debug("Addresses returned: " + str(addrs))
+ if not addrs:
+ continue
+
text = None
for addr in addrs:
if type(self.checks[domain]) is str:
diff --git a/modules/sfp_urlscan.py b/modules/sfp_urlscan.py
index 97f32fc6..8fac1b3c 100644
--- a/modules/sfp_urlscan.py
+++ b/modules/sfp_urlscan.py
@@ -11,7 +11,6 @@
# -------------------------------------------------------------------------------
import json
-import socket
import urllib
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -45,24 +44,6 @@ class sfp_urlscan(SpiderFootPlugin):
return ['GEOINFO', 'LINKED_URL_INTERNAL', 'RAW_RIR_DATA',
'INTERNET_NAME', 'INTERNET_NAME_UNRESOLVED', 'BGP_AS_MEMBER', 'WEBSERVER_BANNER']
- # Resolve a host
- 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")
- else:
- host = host.encode("idna")
-
- addrs = socket.gethostbyname_ex(host)
- if not addrs:
- return False
-
- return True
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
# https://urlscan.io/about-api/
def query(self, qry):
params = {
@@ -178,7 +159,7 @@ class sfp_urlscan(SpiderFootPlugin):
self.notifyListeners(evt)
for domain in set(domains):
- if self.opts['verify'] and not self.resolveHost(domain):
+ if self.opts['verify'] and not self.sf.resolveHost(domain):
evt = SpiderFootEvent('INTERNET_NAME_UNRESOLVED', domain, self.__name__, event)
self.notifyListeners(evt)
else:
diff --git a/modules/sfp_viewdns.py b/modules/sfp_viewdns.py
index 95d68fb4..9ec775ed 100644
--- a/modules/sfp_viewdns.py
+++ b/modules/sfp_viewdns.py
@@ -11,7 +11,6 @@
# -------------------------------------------------------------------------------
import json
-import socket
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
class sfp_viewdns(SpiderFootPlugin):
@@ -59,23 +58,6 @@ class sfp_viewdns(SpiderFootPlugin):
def producedEvents(self):
return ["AFFILIATE_DOMAIN", "CO_HOSTED_SITE"]
- def validateIP(self, host, ip):
- try:
- addrs = socket.gethostbyname_ex(host)
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
- for addr in addrs:
- if type(addr) == list:
- for a in addr:
- if str(a) == ip:
- return True
- else:
- if str(addr) == ip:
- return True
- return False
-
# Search ViewDNS.info
def query(self, qry, querytype, page=1):
info = None
@@ -191,7 +173,7 @@ class sfp_viewdns(SpiderFootPlugin):
continue
self.cohostcount += 1
if eventName == "IP_ADDRESS" and self.opts['verify']:
- if not self.validateIP(h, eventData):
+ if not self.sf.validateIP(h, eventData):
self.sf.debug("Host no longer resolves to our IP.")
continue
e = SpiderFootEvent("CO_HOSTED_SITE", h, self.__name__, event)
diff --git a/modules/sfp_virustotal.py b/modules/sfp_virustotal.py
index 58121488..37ba1c45 100644
--- a/modules/sfp_virustotal.py
+++ b/modules/sfp_virustotal.py
@@ -12,7 +12,6 @@
import json
import time
-import socket
from netaddr import IPNetwork
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -77,22 +76,6 @@ class sfp_virustotal(SpiderFootPlugin):
"MALICIOUS_SUBNET", "INTERNET_NAME", "AFFILIATE_INTERNET_NAME",
"INTERNET_NAME_UNRESOLVED"]
- # Verify a host resolves
- 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")
- else:
- host = host.encode("idna")
-
- addrs = socket.gethostbyname_ex(host)
- except BaseException as e:
- self.sf.debug("Unable to resolve " + host + ": " + str(e))
- return False
-
- return True
-
def query(self, qry):
ret = None
@@ -222,7 +205,7 @@ class sfp_virustotal(SpiderFootPlugin):
if self.getTarget().matches(s):
if s not in self.results:
if self.opts['verify']:
- if not self.resolveHost(s):
+ if not self.sf.resolveHost(s):
e = SpiderFootEvent("INTERNET_NAME_UNRESOLVED", s, self.__name__, event)
self.notifyListeners(e)
else:
@@ -237,7 +220,7 @@ class sfp_virustotal(SpiderFootPlugin):
for n in info['subdomains']:
if n not in self.results:
if self.opts['verify']:
- if not self.resolveHost(n):
+ if not self.sf.resolveHost(n):
e = SpiderFootEvent("INTERNET_NAME_UNRESOLVED", n, self.__name__, event)
self.notifyListeners(e)
else:
diff --git a/modules/sfp_yandexdns.py b/modules/sfp_yandexdns.py
index 130dd327..19dc10b7 100644
--- a/modules/sfp_yandexdns.py
+++ b/modules/sfp_yandexdns.py
@@ -11,7 +11,6 @@
# Licence: GPL
# -------------------------------------------------------------------------------
-import socket
import dns.resolver
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent
@@ -82,7 +81,7 @@ class sfp_yandexdns(SpiderFootPlugin):
# Check that it resolves first, as it becomes a valid
# malicious host only if NOT resolved by Yandex.
try:
- if self.sf.normalizeDNS(socket.gethostbyname_ex(eventData)):
+ if self.sf.resolveHost(eventData):
resolved = True
except BaseException as e:
self.sf.debug("Unable to resolve " + eventData)
diff --git a/sflib.py b/sflib.py
index d93444ce..7922c1d7 100644
--- a/sflib.py
+++ b/sflib.py
@@ -33,6 +33,7 @@ import urllib2
import StringIO
import threading
import traceback
+import ssl
from bs4 import BeautifulSoup, SoupStrainer
from copy import deepcopy, copy
@@ -873,6 +874,84 @@ class SpiderFoot:
# General helper functions to automate many common tasks between modules
#
+ # Return a normalised resolution or None if not resolved.
+ 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")
+ else:
+ host = host.encode("idna")
+
+ addrs = self.normalizeDNS(socket.gethostbyname_ex(host))
+ if len(addrs) > 0:
+ return addrs
+ return None
+ except BaseException as e:
+ self.debug("Unable to resolve " + host + ": " + str(e))
+ return None
+
+ # Return a normalised resolution of an IP or None if not resolved.
+ def resolveIP(self, ipaddr):
+ self.debug("Performing reverse-resolve of " + ipaddr)
+
+ try:
+ addrs = self.normalizeDNS(socket.gethostbyaddr(ipaddr))
+ if len(addrs) > 0:
+ return addrs
+ return None
+ except BaseException as e:
+ self.debug("Unable to resolve " + ipaddr + " (" + str(e) + ")")
+ return None
+
+ def resolveHost6(self, hostname):
+ try:
+ addrs = list()
+ res = socket.getaddrinfo(hostname, None, socket.AF_INET6)
+ for addr in res:
+ if addr[4][0] not in addrs:
+ addrs.append(addr[4][0])
+ if len(addrs) < 1:
+ return None
+ self.debug("Resolved " + hostname + " to IPv6: " + str(addrs))
+ return addrs
+ except BaseException as e:
+ self.debug("Unable to IPv6 resolve " + hostname + " (" + str(e) + ")")
+ return None
+
+ # Verify a host resolves to a given IP
+ def validateIP(self, host, ip):
+ try:
+ addrs = socket.gethostbyname_ex(host)
+ except BaseException as e:
+ self.debug("Unable to resolve " + host + ": " + str(e))
+ return False
+
+ for addr in addrs:
+ if type(addr) == list:
+ for a in addr:
+ if str(a) == ip:
+ return True
+ else:
+ if str(addr) == ip:
+ return True
+ return False
+
+ # Create a safe socket that's using SOCKS/TOR if it was enabled
+ def safeSocket(self, host, port, timeout):
+ sock = socket.create_connection((host, int(port)), int(timeout))
+ sock.settimeout(int(timeout))
+ return sock
+
+ # Create a safe SSL connection that's using SOCKs/TOR if it was enabled
+ def safeSSLSocket(self, host, port, timeout):
+ s = socket.socket()
+ s.settimeout(int(timeout))
+ s.connect((host, int(port)))
+ sock = ssl.wrap_socket(s)
+ sock.do_handshake()
+ return sock
+
# Parse the contents of robots.txt, returns a list of patterns
# which should not be followed
def parseRobotsTxt(self, robotsTxtData):