From e2cf044e3f0955a5294143e4010ad92d3248da13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Werner?= Date: Sat, 23 May 2020 15:30:55 +0200 Subject: [PATCH 1/3] first shot at an MQTT module --- proxymodules/mqtt.py | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 proxymodules/mqtt.py diff --git a/proxymodules/mqtt.py b/proxymodules/mqtt.py new file mode 100644 index 0000000..ea8cee2 --- /dev/null +++ b/proxymodules/mqtt.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 +import os.path as path +import paho.mqtt.client as mqtt + + +class Module: + def __init__(self, incoming=False, verbose=False, options=None): + # extract the file name from __file__. __file__ is proxymodules/name.py + self.name = path.splitext(path.basename(__file__))[0] + self.description = 'Publish the data to an MQTT server' + self.incoming = incoming # incoming means module is on -im chain + self.client_id = '' + self.username = None + self.password = None + self.server = None + self.port = 1883 + self.topic = "" + if options is not None: + if 'clientid' in options.keys(): + self.client_id = options['clientid'] + if 'server' in options.keys(): + self.server = options['server'] + if 'username' in options.keys(): + self.username = options['username'] + if 'password' in options.keys(): + self.server = options['password'] + if 'port' in options.keys(): + try: + self.port = int(options['port']) + except ValueError: + print('invalid port, using default 1883') + if 'topic' in options.keys(): + self.topic = options['topic'] + + if self.server is not None: + self.mqtt = mqtt.Client(self.client_id) + if self.username is not None or self.password is not None: + self.mqtt.username_pw_set(self.username, self.password) + self.mqtt.connect(self.server, self.port) + else: + self.mqtt = None + + def execute(self, data): + if self.mqtt is not None: + if not self.mqtt.is_connected(): + self.mqtt.reconnect() + self.mqtt.publish(self.topic, data.hex()) + return data + + def help(self): + h = '\tserver: server to connect to, required\n' + h += ('\tclientid: what to use as client_id, default is empty\n' + '\tusername: username\n' + '\tpassword: password\n' + '\tport: port to connect to, default 1883\n' + '\ttopic: topic to publish to, default is empty') + return h + + +if __name__ == '__main__': + print('This module is not supposed to be executed alone!') From 84bbf3465f3e96943465697c921d55a698425b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Werner?= Date: Sat, 23 May 2020 20:29:19 +0200 Subject: [PATCH 2/3] fixed copy+paste error --- proxymodules/mqtt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxymodules/mqtt.py b/proxymodules/mqtt.py index ea8cee2..d13fc56 100644 --- a/proxymodules/mqtt.py +++ b/proxymodules/mqtt.py @@ -23,7 +23,7 @@ def __init__(self, incoming=False, verbose=False, options=None): if 'username' in options.keys(): self.username = options['username'] if 'password' in options.keys(): - self.server = options['password'] + self.password = options['password'] if 'port' in options.keys(): try: self.port = int(options['port']) From cc540f82c79fcec1c055df9c75bcaca646753d01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Werner?= Date: Sat, 23 May 2020 20:56:25 +0200 Subject: [PATCH 3/3] hex option for mqtt module --- proxymodules/mqtt.py | 21 +++++++++++++++++---- tcpproxy.py | 4 ++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/proxymodules/mqtt.py b/proxymodules/mqtt.py index d13fc56..4ceb725 100644 --- a/proxymodules/mqtt.py +++ b/proxymodules/mqtt.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import os.path as path import paho.mqtt.client as mqtt +from distutils.util import strtobool class Module: @@ -14,7 +15,8 @@ def __init__(self, incoming=False, verbose=False, options=None): self.password = None self.server = None self.port = 1883 - self.topic = "" + self.topic = '' + self.hex = False if options is not None: if 'clientid' in options.keys(): self.client_id = options['clientid'] @@ -27,10 +29,17 @@ def __init__(self, incoming=False, verbose=False, options=None): if 'port' in options.keys(): try: self.port = int(options['port']) + if self.port not in range(1, 65536): + raise ValueError except ValueError: - print('invalid port, using default 1883') + print(f'port: invalid port {options["port"]}, using default {self.port}') if 'topic' in options.keys(): self.topic = options['topic'] + if 'hex' in options.keys(): + try: + self.hex = bool(strtobool(options['hex'])) + except ValueError: + print(f'hex: {options["hex"]} is not a bool value, falling back to default value {self.hex}.') if self.server is not None: self.mqtt = mqtt.Client(self.client_id) @@ -44,7 +53,10 @@ def execute(self, data): if self.mqtt is not None: if not self.mqtt.is_connected(): self.mqtt.reconnect() - self.mqtt.publish(self.topic, data.hex()) + if self.hex is True: + self.mqtt.publish(self.topic, data.hex()) + else: + self.mqtt.publish(self.topic, data) return data def help(self): @@ -53,7 +65,8 @@ def help(self): '\tusername: username\n' '\tpassword: password\n' '\tport: port to connect to, default 1883\n' - '\ttopic: topic to publish to, default is empty') + '\ttopic: topic to publish to, default is empty\n' + '\thex: encode data as hex before sending it. AAAA becomes 41414141.') return h diff --git a/tcpproxy.py b/tcpproxy.py index 59e1566..6955255 100755 --- a/tcpproxy.py +++ b/tcpproxy.py @@ -131,7 +131,7 @@ def list_modules(): for _, module, _ in pkgutil.iter_modules([module_path]): __import__('proxymodules.' + module) m = sys.modules['proxymodules.' + module].Module() - print('%s - %s' % (m.name, m.description)) + print(f'{m.name} - {m.description}') def print_module_help(modlist): @@ -139,7 +139,7 @@ def print_module_help(modlist): modules = generate_module_list(modlist) for m in modules: try: - print(m.name) + print(f'{m.name} - {m.description}') print(m.help()) except AttributeError: print('\tNo options or missing help() function.')