summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomeo Papa <romeopapa@caramail.com>2015-08-12 22:08:36 +0200
committerRomeo Papa <romeopapa@caramail.com>2015-08-20 14:32:01 +0200
commit95ac85f81013b26335d435146a63ee7d49fc77e0 (patch)
tree7ddd44a3ad58b729dcfbaf51821cb1c443402856
parentccc60e4fe6722a569a1023266162f2c2472bef06 (diff)
Validates hosts and ports (SOCKS proxy and SMTP server)feature/9412-add-socks-support-to-whisperback
-rw-r--r--whisperBack/utils.py40
-rw-r--r--whisperBack/whisperback.py11
2 files changed, 50 insertions, 1 deletions
diff --git a/whisperBack/utils.py b/whisperBack/utils.py
index 2e1284a..e84c624 100644
--- a/whisperBack/utils.py
+++ b/whisperBack/utils.py
@@ -135,6 +135,46 @@ def is_valid_email(candidate):
else:
return False
+def is_valid_port(candidate):
+ """Check if candidate is a valid port number (integer between 1 and 65535)
+
+ @param candidate the port number to be checked
+ """
+ try:
+ int(candidate)
+ except ValueError:
+ return False
+
+ if candidate >= 1 and candidate < 65535:
+ return True
+ else:
+ return False
+
+def is_valid_hostname_or_ipv4(candidate):
+ """Check if candidate is a valid hostname or IPv4 address
+
+ pySocks is not compatible with IPv6
+ hostname specs follow RFC 1123
+
+ @param candidate the hostname or IPv4 address to validate
+ """
+
+ # XXX: must be updated once IPv6 is enabled
+
+ if not isinstance(candidate, str):
+ return False
+ if len(candidate) > 255:
+ return False
+
+ # regex from http://stackoverflow.com/a/106223
+ ip_address_regex = re.compile("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
+ hostname_regex = re.compile("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$");
+
+ if ip_address_regex.match(candidate) or hostname_regex.match(candidate):
+ return True
+ else:
+ return False
+
def sanitize_hardware_info(log_string):
"""Sanitize hardware-identifying info from a string
diff --git a/whisperBack/whisperback.py b/whisperBack/whisperback.py
index d94adb1..63a8e2c 100644
--- a/whisperBack/whisperback.py
+++ b/whisperBack/whisperback.py
@@ -170,7 +170,16 @@ class WhisperBack(object):
if not self.socks_port:
raise whisperBack.exceptions.MisconfigurationException('socks_port')
- def execute_threaded(self, func, args, progress_callback=None,
+ if not whisperBack.utils.is_valid_hostname_or_ipv4(self.smtp_host):
+ raise ValueError("Invalid value for 'smtp_host'.")
+ if not whisperBack.utils.is_valid_port(self.smtp_port):
+ raise ValueError("Invalid value for 'smtp_port'.")
+ if not whisperBack.utils.is_valid_hostname_or_ipv4(self.socks_host):
+ raise ValueError("Invalid value for 'socks_host'.")
+ if not whisperBack.utils.is_valid_port(self.socks_port):
+ raise ValueError("Invalid value for 'socks_port'.")
+
+ def execute_threaded(self, func, args, progress_callback=None,
finished_callback=None, polling_freq=100):
"""Execute a function in another thread and handle it.