Skip to content

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

sflib: Add validEmail function

Brendan Colesbb3ada0ea500
diff --git a/modules/sfp_builtwith.py b/modules/sfp_builtwith.py
index 8e5aa258..7fbab591 100644
--- a/modules/sfp_builtwith.py
+++ b/modules/sfp_builtwith.py
@@ -107,22 +107,20 @@ class sfp_builtwith(SpiderFootPlugin):
return None
if "Meta" in data:
- # Verify any email addresses as we sometimes get junk from BuiltWith
- pat = re.compile("([\%a-zA-Z\.0-9_\-\+]+@[a-zA-Z\.0-9\-]+\.[a-zA-Z\.0-9\-]+)")
if data['Meta'].get("Names", []):
for nb in data['Meta']['Names']:
e = SpiderFootEvent("RAW_RIR_DATA", "Possible full name: " + nb['Name'],
self.__name__, event)
self.notifyListeners(e)
if nb.get('Email', None):
- if (re.match(pat, nb['Email'])):
+ if self.sf.validEmail(nb['Email']):
e = SpiderFootEvent("EMAILADDR", nb['Email'],
self.__name__, event)
self.notifyListeners(e)
if data['Meta'].get("Emails", []):
for email in data['Meta']['Emails']:
- if (re.match(pat, email)):
+ if self.sf.validEmail(email):
e = SpiderFootEvent("EMAILADDR", email,
self.__name__, event)
self.notifyListeners(e)
diff --git a/modules/sfp_gravatar.py b/modules/sfp_gravatar.py
index 4b4d1a94..f61c1650 100644
--- a/modules/sfp_gravatar.py
+++ b/modules/sfp_gravatar.py
@@ -137,7 +137,7 @@ class sfp_gravatar(SpiderFootPlugin):
if data.get('emails') is not None:
for email in data.get('emails'):
- if email.get('value') is not None and email.get('value') != eventData:
+ if self.sf.validEmail(email.get('value')) and email.get('value') != eventData:
evt = SpiderFootEvent("EMAILADDR", email.get('value'), self.__name__, event)
self.notifyListeners(evt)
diff --git a/modules/sfp_jsonwhoiscom.py b/modules/sfp_jsonwhoiscom.py
index 7c92dc50..2872ce3d 100644
--- a/modules/sfp_jsonwhoiscom.py
+++ b/modules/sfp_jsonwhoiscom.py
@@ -182,7 +182,8 @@ class sfp_jsonwhoiscom(SpiderFootPlugin):
for contact in contacts:
email = contact.get('email')
if email:
- emails.append(email)
+ if self.sf.validEmail(email):
+ emails.append(email)
name = contact.get("name")
if name:
diff --git a/modules/sfp_scylla.py b/modules/sfp_scylla.py
index 166ae90a..7c84a0af 100644
--- a/modules/sfp_scylla.py
+++ b/modules/sfp_scylla.py
@@ -143,12 +143,12 @@ class sfp_scylla(SpiderFootPlugin):
if not email:
continue
- try:
- mailDom = email.lower().split('@')[1]
- except BaseException as e:
- self.sf.debug("Encountered strange result: " + email)
+ if not self.sf.validEmail(email):
+ self.sf.debug("Skipping invalid email address: " + email)
continue
+ mailDom = email.lower().split('@')[1]
+
# Skip unrelated emails
# Scylla sometimes returns broader results than the searched data
if not self.getTarget().matches(mailDom):
diff --git a/modules/sfp_snov.py b/modules/sfp_snov.py
index e93f3337..e9bf3d6a 100644
--- a/modules/sfp_snov.py
+++ b/modules/sfp_snov.py
@@ -179,6 +179,8 @@ class sfp_snov(SpiderFootPlugin):
if email:
if email in self.results:
continue
+ if not self.sf.validEmail(email):
+ continue
self.results[email] = True
evt = SpiderFootEvent("EMAILADDR", str(email), self.__name__, event)
diff --git a/sflib.py b/sflib.py
index 9273895c..7db705fe 100644
--- a/sflib.py
+++ b/sflib.py
@@ -1190,6 +1190,40 @@ class SpiderFoot:
ret.append(addr)
return ret
+ def validEmail(self, email):
+ """Check if the provided string is a valid email address.
+
+ Args:
+ email (str): The email address to check.
+
+ Returns:
+ bool
+ """
+
+ if not isinstance(email, str):
+ return False
+
+ if "@" not in email:
+ return False
+
+ # Basic regex check
+ if not re.match(r'^([\%a-zA-Z\.0-9_\-\+]+@[a-zA-Z\.0-9\-]+\.[a-zA-Z\.0-9\-]+)$', email):
+ return False
+
+ # Handle false positive matches
+ if len(email) < 5:
+ return False
+
+ # Handle messed up encodings
+ if "%" in email:
+ return False
+
+ # Handle truncated emails
+ if "..." in email:
+ return False
+
+ return True
+
def sanitiseInput(self, cmd):
"""Verify input command is safe to execute
@@ -1561,24 +1595,8 @@ class SpiderFoot:
matches = re.findall(r'([\%a-zA-Z\.0-9_\-\+]+@[a-zA-Z\.0-9\-]+\.[a-zA-Z\.0-9\-]+)', data)
for match in matches:
- self.debug("Found possible email: " + match)
-
- # Handle false positive matches
- if len(match) < 5:
- self.debug("Skipped likely invalid email address.")
- continue
-
- # Handle messed up encodings
- if "%" in match:
- self.debug("Skipped invalid email address: " + match)
- continue
-
- # Handle truncated emails
- if "..." in match:
- self.debug("Skipped incomplete e-mail address: " + match)
- continue
-
- emails.add(match)
+ if self.validEmail(match):
+ emails.add(match)
return list(emails)
diff --git a/test/unit/test_spiderfoot.py b/test/unit/test_spiderfoot.py
index a9baaca6..8fa35177 100644
--- a/test/unit/test_spiderfoot.py
+++ b/test/unit/test_spiderfoot.py
@@ -593,6 +593,28 @@ class TestSpiderFoot(unittest.TestCase):
valid_ip_network = sf.validIpNetwork('0.0.0.0/0')
self.assertIsInstance(valid_ip_network, bool)
+ def test_valid_email_should_return_a_boolean(self):
+ """
+ Test validEmail(self, email)
+ """
+ sf = SpiderFoot(dict())
+
+ valid_email = sf.validEmail(None)
+ self.assertIsInstance(valid_email, bool)
+ self.assertFalse(valid_email)
+
+ valid_email = sf.validEmail([])
+ self.assertIsInstance(valid_email, bool)
+ self.assertFalse(valid_email)
+
+ valid_email = sf.validEmail('root@localhost')
+ self.assertIsInstance(valid_email, bool)
+ self.assertFalse(valid_email)
+
+ valid_email = sf.validEmail('[email protected]')
+ self.assertIsInstance(valid_email, bool)
+ self.assertTrue(valid_email)
+
def test_normalize_dns(self):
"""
Test normalizeDNS(self, res)