github.com/Coalfire-Research/Slackor@v0.0.0-20191010164036-aa32a7f9250b/impacket/examples/mqtt_check.py (about)

     1  #!/usr/bin/env python
     2  # SECUREAUTH LABS. Copyright 2018 SecureAuth Corporation. All rights reserved.
     3  #
     4  # This software is provided under under a slightly modified version
     5  # of the Apache Software License. See the accompanying LICENSE file
     6  # for more information.
     7  #
     8  # Author: Alberto Solino (@agsolino)
     9  #
    10  # Description:
    11  #     Simple MQTT example aimed at playing with different login options. Can be converted into a account/password
    12  #     brute forcer quite easily.
    13  #
    14  # Reference for:
    15  #  MQTT and Structure
    16  #
    17  #
    18  
    19  from __future__ import print_function
    20  
    21  import argparse
    22  import logging
    23  import re
    24  import sys
    25  
    26  from impacket import version
    27  from impacket.examples import logger
    28  from impacket.mqtt import CONNECT_ACK_ERROR_MSGS, MQTTConnection
    29  
    30  class MQTT_LOGIN:
    31      def __init__(self, username, password, target, options):
    32          self._options = options
    33          self._username = username
    34          self._password = password
    35          self._target = target
    36  
    37          if self._username == '':
    38              self._username = None
    39  
    40      def run(self):
    41          mqtt = MQTTConnection(self._target, int(self._options.port), self._options.ssl)
    42  
    43          if self._options.client_id is None:
    44              clientId = ' '
    45          else:
    46              clientId = self._options.client_id
    47  
    48          mqtt.connect(clientId, self._username, self._password)
    49  
    50          logging.info(CONNECT_ACK_ERROR_MSGS[0])
    51  
    52  if __name__ == '__main__':
    53      # Init the example's logger theme
    54      logger.init()
    55      print(version.BANNER)
    56      parser = argparse.ArgumentParser(add_help=False,
    57                                       description="MQTT login check")
    58      parser.add_argument("--help", action="help", help='show this help message and exit')
    59      parser.add_argument('target', action='store', help='[[domain/]username[:password]@]<targetName>')
    60      parser.add_argument('-client-id', action='store', help='Client ID used when authenticating (default random)')
    61      parser.add_argument('-ssl', action='store_true', help='turn SSL on')
    62      parser.add_argument('-port', action='store', default='1883', help='port to connect to (default 1883)')
    63      parser.add_argument('-debug', action='store_true', help='Turn DEBUG output ON')
    64  
    65      try:
    66          options = parser.parse_args()
    67      except Exception as e:
    68          logging.error(str(e))
    69          sys.exit(1)
    70  
    71      if options.debug is True:
    72          logging.getLogger().setLevel(logging.DEBUG)
    73      else:
    74          logging.getLogger().setLevel(logging.INFO)
    75  
    76      domain, username, password, address = re.compile('(?:(?:([^/@:]*)/)?([^@:]*)(?::([^@]*))?@)?(.*)').match(
    77          options.target).groups('')
    78  
    79      #In case the password contains '@'
    80      if '@' in address:
    81          password = password + '@' + address.rpartition('@')[0]
    82          address = address.rpartition('@')[2]
    83  
    84      check_mqtt = MQTT_LOGIN(username, password, address, options)
    85      try:
    86          check_mqtt.run()
    87      except Exception as e:
    88          if logging.getLogger().level == logging.DEBUG:
    89              import traceback
    90              traceback.print_exc()
    91          logging.error(e)