Skip to content

Commit

Permalink
Adding MQTT basic structures and example
Browse files Browse the repository at this point in the history
* Minimalistic MQTT implementation, just focused on connecting, subscribing and publishing
  basic messages on topics.
* Simple MQTT example aimed at playing with different login options. Can be converted into
  a account/password brute forcer quite easily.
  • Loading branch information
asolino committed Oct 18, 2016
1 parent f35117e commit 8cef002
Show file tree
Hide file tree
Showing 2 changed files with 515 additions and 0 deletions.
95 changes: 95 additions & 0 deletions examples/mqtt_check.py
@@ -0,0 +1,95 @@
#!/usr/bin/python
# Copyright (c) 2003-2016 CORE Security Technologies
#
# This software is provided under under a slightly modified version
# of the Apache Software License. See the accompanying LICENSE file
# for more information.
#
# Author: Alberto Solino (@agsolino)
#
# Description:
# Simple MQTT example aimed at playing with different login options. Can be converted into a account/password
# brute forcer quite easily.
#
# Reference for:
# MQTT and Structure
#
#

import argparse
import logging
import re
import sys

from impacket import version
from impacket.examples import logger
from impacket.mqtt import CONNECT_ACK_ERROR_MSGS, MQTTConnection

try:
import OpenSSL
from OpenSSL import SSL, crypto
except:
logging.critical("pyOpenSSL is not installed, can't continue")
raise


class MQTT_LOGIN:
def __init__(self, username, password, target, options):
self._options = options
self._username = username
self._password = password
self._target = target

def run(self):
mqtt = MQTTConnection(self._target, int(self._options.port), self._options.ssl)

if self._options.client_id is None:
clientId = ' '
else:
clientId = self._options.client_id

mqtt.connect(clientId, self._username, self._password)

logging.info(CONNECT_ACK_ERROR_MSGS[0])

if __name__ == '__main__':
# Init the example's logger theme
logger.init()
print version.BANNER
parser = argparse.ArgumentParser(add_help=False,
description="MQTT login check")
parser.add_argument("--help", action="help", help='show this help message and exit')
parser.add_argument('target', action='store', help='[[domain/]username[:password]@]<targetName>')
parser.add_argument('-client-id', action='store', help='Client ID used when authenticating (default random)')
parser.add_argument('-ssl', action='store_true', help='turn SSL on')
parser.add_argument('-port', action='store', default='1883', help='port to connect to (default 1883)')
parser.add_argument('-debug', action='store_true', help='Turn DEBUG output ON')

try:
options = parser.parse_args()
except Exception, e:
logging.error(str(e))
sys.exit(1)

if options.debug is True:
logging.getLogger().setLevel(logging.DEBUG)
else:
logging.getLogger().setLevel(logging.INFO)

domain, username, password, address = re.compile('(?:(?:([^/@:]*)/)?([^@:]*)(?::([^@]*))?@)?(.*)').match(
options.target).groups('')

#In case the password contains '@'
if '@' in address:
password = password + '@' + address.rpartition('@')[0]
address = address.rpartition('@')[2]

check_mqtt = MQTT_LOGIN(username, password, address, options)
try:
check_mqtt.run()
except Exception, e:
#import traceback
#traceback.print_exc()
logging.error(e)


0 comments on commit 8cef002

Please sign in to comment.