add: new bruterforcer module in alfa

This commit is contained in:
cr0hn
2016-03-07 02:39:10 +01:00
parent 7d5e88b23d
commit ed52313b96
13 changed files with 111766 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
#
# Enteletaor - https://github.com/cr0hn/enteletaor
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
# following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
# following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
import logging
from .. import IModule
from ...libs.core.structs import CommonData
from ...libs.core.models import StringField, BoolField, IntegerField, FloatField
from .main import action_scan_main
log = logging.getLogger()
# ----------------------------------------------------------------------
class ModuleModel(CommonData):
port = StringField(default="6379")
target = StringField(required=True)
wordlist = StringField(required=True)
user = StringField()
concurrency = IntegerField(label="maximum parallels scans", default=10)
timeout = FloatField(label="timeout for socket connections", default=0.2)
# ----------------------------------------------------------------------
class BruteProcessModule(IModule):
"""
Try to extract information from remote processes
"""
__model__ = ModuleModel
__submodules__ = {
'default': dict(
action=action_scan_main
)
}
name = "brute"
description = "do a scans trying to find open brokers / MQ"

View File

@@ -0,0 +1,92 @@
# -*- coding: utf-8 -*-
import zmq
import redis
import socket
import redis.exceptions
import logging
import amqp.connection
from .exceptions import AuthRequired
log = logging.getLogger()
# --------------------------------------------------------------------------
# These 3 functions determinate if server has listen one of these services:
# - Redis server
# - RabbitMQ server
# - ZeroMQ PUB/SUB pattern
#
# Each function try to connect or do some action and determinate if service
# is on or not.
# --------------------------------------------------------------------------
def brute_redis(host, port=6379, user=None, password=None, db=0):
# log.debug(" * Connection to Redis: %s : %s" % (host, port))
try:
redis.StrictRedis(host=host,
port=port,
socket_connect_timeout=1,
socket_timeout=1,
password=password,
db=db).ping()
return True
except redis.exceptions.ResponseError as e:
if str(e).startswith("NOAUTH"):
raise AuthRequired()
else:
return False
except Exception:
return False
# ----------------------------------------------------------------------
def brute_amqp(host, port=5672, user=None, password=None, db=0):
host_and_port = "%s:%s" % (host, port)
user_name = "guest" if user is None else user
user_password = "guest" if password is None else password
try:
amqp.connection.Connection(host=host_and_port,
userid=user_name,
password=user_password,
connect_timeout=1,
read_timeout=1,
socket_timeout=1)
return True
except socket.timeout:
raise AuthRequired()
except Exception:
return False
# ----------------------------------------------------------------------
def brute_zmq(host, port=5555, user=None, password=None, db=0):
context = zmq.Context()
# Configure
socket = context.socket(zmq.SUB)
socket.setsockopt(zmq.SUBSCRIBE, b"") # All topics
socket.setsockopt(zmq.LINGER, 0) # All topics
socket.RCVTIMEO = 1000 # timeout: 1 sec
# Connect
socket.connect("tcp://%s:%s" % (host, port))
# Try to receive
try:
socket.recv()
return True
except Exception:
return False
finally:
socket.close()

View File

@@ -0,0 +1,161 @@
# -*- coding: utf-8 -*-
#
# Enteletaor - https://github.com/cr0hn/enteletaor
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
# following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
# following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
import os
import signal
import logging
import threading
import eventlet
from eventlet import tpool
from .authers import brute_redis, brute_amqp, brute_zmq
from .exceptions import AuthRequired
FOUND = None
THREADS = []
log = logging.getLogger()
# Path thread library
eventlet.monkey_patch(socket=True, select=True, thread=True)
# ----------------------------------------------------------------------
class FoundPassword(Exception):
pass
# ----------------------------------------------------------------------
# Runners
# ----------------------------------------------------------------------
def find_password_sem(fn, sem, host, port, user, password, db):
global FOUND
try:
if fn(host, port, user, password, None) is True:
FOUND = "%s: %s%s" % (host, "", password)
except AuthRequired:
pass
sem.release()
# ----------------------------------------------------------------------
def find_password(fn, host, port, user, password, db):
global FOUND
try:
if fn(host, port, user, password, db) is True:
FOUND = "%s - %s%s" % (host, "%s/" % user, password)
except AuthRequired:
pass
# ----------------------------------------------------------------------
# Workers function
# ----------------------------------------------------------------------
def cracking_threads(fn, port, config):
global FOUND
global THREADS
th = []
sem = threading.BoundedSemaphore(config.concurrency)
with open(config.wordlist, "r") as f:
for i, password in enumerate(f.readlines()):
password = password.replace("\n", "")
log.debug(" -- Testing '%s'" % password)
if FOUND is not None:
break
# Launch password
t = threading.Thread(target=find_password_sem, args=(fn, sem, config.target, port, config.user, password, None, ))
th.append(t)
sem.acquire()
t.start()
if (i % 500) == 0:
log.info(" >> %s passwords tested" % i)
# Wait for ending
for x in th:
x.join()
if FOUND is not None:
log.error(" - Password found: %s" % FOUND)
# ----------------------------------------------------------------------
def cracking_evenlets(fn, port, config):
global FOUND
os.getenv("EVENTLET_THREADPOOL_SIZE", config.concurrency)
try:
with open(config.wordlist, "r") as f:
for i, password in enumerate(f.readlines()):
password = password.replace("\n", "")
log.debug(" >> Testing %s" % password)
if FOUND is not None:
break
tpool.execute(find_password, fn, config.target, port, config.user, password, None)
if (i % 500) == 0:
log.info(" >> %s passwords tested" % i)
except FoundPassword as e:
log.error(" - Credentials found: %s" % e)
# ----------------------------------------------------------------------
def cracking(server_type, port, config):
crackers = {
'redis': (brute_redis, cracking_evenlets),
'rabbitmq': (brute_amqp, cracking_threads),
'zeromq': brute_zmq
}
mode, fn = crackers[server_type.lower()]
# --------------------------------------------------------------------------
# Check requisites
# --------------------------------------------------------------------------
if server_type.lower() == "rabbitmq":
if config.user is None:
log.error(" - Username is required for this server.")
return
fn(mode, port, config)

View File

@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
#
# Enteletaor - https://github.com/cr0hn/enteletaor
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
# following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
# following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ----------------------------------------------------------------------
def cracking(server_type, port, config):
print(server_type)

View File

@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
#
# Enteletaor - https://github.com/cr0hn/enteletaor
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
# following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
# following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ----------------------------------------------------------------------
class AuthRequired(Exception):
pass

View File

@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
#
# Enteletaor - https://github.com/cr0hn/enteletaor
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
# following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
# following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
import six
import logging
from .utils import get_server_type
if six.PY2:
from .cracker import cracking
else:
# from .cracker3 import cracking
from .cracker import cracking
# Reconfigure AMQP LOGGER
logging.getLogger('amqp').setLevel(100)
log = logging.getLogger()
# ----------------------------------------------------------------------
def action_scan_main(config):
# --------------------------------------------------------------------------
# Preparing scan
# --------------------------------------------------------------------------
server_type, status, port = get_server_type(config)
log.error(" - Detected '%s' server '%s' " % (server_type, status))
# --------------------------------------------------------------------------
# Do brute
# --------------------------------------------------------------------------
if status == "auth":
cracking(server_type, port, config)
elif status == "open":
log.error(" - '%s' '%s' server is open. No password cracking need" % (server_type, config.target))
else:
log.error(" - Not detected brokers in '%s'." % config.target)

View File

@@ -0,0 +1,136 @@
# -*- coding: utf-8 -*-
#
# Enteletaor - https://github.com/cr0hn/enteletaor
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
# following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
# following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
"""
This file contains monkey patches for
"""
from __future__ import absolute_import
def new_transport_init(self, host, connect_timeout):
import errno
import re
import socket
import ssl
# Jython does not have this attribute
try:
from socket import SOL_TCP
except ImportError: # pragma: no cover
from socket import IPPROTO_TCP as SOL_TCP # noqa
try:
from ssl import SSLError
except ImportError:
class SSLError(Exception): # noqa
pass
from struct import pack, unpack
from amqp.exceptions import UnexpectedFrame
from amqp.utils import get_errno, set_cloexec
_UNAVAIL = errno.EAGAIN, errno.EINTR, errno.ENOENT
AMQP_PORT = 5672
EMPTY_BUFFER = bytes()
# Yes, Advanced Message Queuing Protocol Protocol is redundant
AMQP_PROTOCOL_HEADER = 'AMQP\x01\x01\x00\x09'.encode('latin_1')
# Match things like: [fe80::1]:5432, from RFC 2732
IPV6_LITERAL = re.compile(r'\[([\.0-9a-f:]+)\](?::(\d+))?')
# --------------------------------------------------------------------------
# __init__ content:
# --------------------------------------------------------------------------
self.connected = True
msg = None
port = AMQP_PORT
m = IPV6_LITERAL.match(host)
if m:
host = m.group(1)
if m.group(2):
port = int(m.group(2))
else:
if ':' in host:
host, port = host.rsplit(':', 1)
port = int(port)
self.sock = None
last_err = None
for res in socket.getaddrinfo(host, port, 0,
socket.SOCK_STREAM, SOL_TCP):
af, socktype, proto, canonname, sa = res
try:
self.sock = socket.socket(af, socktype, proto)
try:
set_cloexec(self.sock, True)
except NotImplementedError:
pass
self.sock.settimeout(connect_timeout)
self.sock.connect(sa)
except socket.error as exc:
msg = exc
self.sock.close()
self.sock = None
last_err = msg
continue
break
if not self.sock:
# Didn't connect, return the most recent error message
raise socket.error(last_err)
try:
# self.sock.settimeout(None)
self.sock.setsockopt(SOL_TCP, socket.TCP_NODELAY, 1)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
self._setup_transport()
self._write(AMQP_PROTOCOL_HEADER)
except (OSError, IOError, socket.error) as exc:
if get_errno(exc) not in _UNAVAIL:
self.connected = False
raise
# --------------------------------------------------------------------------
# amqlib
# --------------------------------------------------------------------------
def patch_transport():
"""
This function path transport constructor to fix timeout in sockets
"""
from amqp.transport import _AbstractTransport
_AbstractTransport.__init__ = new_transport_init

View File

@@ -0,0 +1,95 @@
# -*- coding: utf-8 -*-
#
# Enteletaor - https://github.com/cr0hn/enteletaor
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
# following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
# following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
import six
import socket
import logging
from .patch import patch_transport
from .exceptions import AuthRequired
from .authers import brute_amqp, brute_redis, brute_zmq
# Monkey patch for AMQP lib
patch_transport()
log = logging.getLogger()
# ----------------------------------------------------------------------
def get_server_type(config):
"""
Get server type and if it's open or closed.
Returns server type and their status as format: (TYPE, STATUS, port), where:
- TYPE: redis/zeromq/amqp
- STATUS: open/closed/auth
:return: type of server as format: (type, status, port)
:rtype: (str, str, int)
"""
handlers = {
'Redis': brute_redis,
'RabbitMQ': brute_amqp,
'ZeroMQ': brute_zmq
}
host = config.target
port = config.port
user = config.user
password = None
result = -1
log.warning(" > Analyzing host '%s' with port '%s' " % (host, port))
try:
# Try to check if port is open
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(config.timeout)
result = s.connect_ex((host, int(port)))
except socket.gaierror as e:
log.debug("%s error: %s" % (port, e))
finally:
s.close()
# Is port open?
if result == 0:
log.info(" <i> Port '%s' is open in '%s'" % (port, host))
# Check each serve
for server_type, handle in six.iteritems(handlers):
try:
if handle(host, port, user, password, config) is True:
return server_type, "open", port
except AuthRequired:
return server_type, "auth", port
else:
return None, "closed", port