mirror of
https://github.com/aljazceru/cowrie.git
synced 2026-02-03 05:24:43 +01:00
commit 79fff5332ba74bab40d0059e1051ca62614488ff Merge: 645f25f 39c9fa4 Author: Michel Oosterhof <michel@oosterhof.net> Date: Sun Sep 18 14:46:09 2016 +0000 Merge branch 'configparser' of https://github.com/micheloosterhof/cowrie into configparser commit 645f25f6f22dbe374b544f08d69b7de2ef0dcb45 Author: Michel Oosterhof <michel@oosterhof.net> Date: Sun Sep 18 14:37:46 2016 +0000 rewrite code for prompt. this had issues with unicode commit 99adf95f27ef03b229e592f1fa393cf66be3faf0 Author: Michel Oosterhof <michel@oosterhof.net> Date: Sun Sep 18 14:15:10 2016 +0000 can't use "mode" commit bee2b6693fe009d361a0b117c67d11f37ed21c41 Author: Michel Oosterhof <michel@oosterhof.net> Date: Sun Sep 18 14:09:57 2016 +0000 configparser changes. not working well yet on py2 commit 39c9fa406cca9ede51af8329280af81fff870e31 Author: Michel Oosterhof <michel@oosterhof.net> Date: Sun Sep 18 14:37:46 2016 +0000 rewrite code for prompt. this had issues with unicode commit 3dd08206fcb4c7dd0a66d20b914e3279947c77de Author: Michel Oosterhof <michel@oosterhof.net> Date: Sun Sep 18 14:15:10 2016 +0000 can't use "mode" commit f54272b239409b65ef0292ac328b0a2efccbe6cd Author: Michel Oosterhof <michel@oosterhof.net> Date: Sun Sep 18 14:09:57 2016 +0000 configparser changes. not working well yet on py2
170 lines
6.4 KiB
Python
170 lines
6.4 KiB
Python
# Copyright (c) 2015 Michel Oosterhof <michel@oosterhof.net>
|
|
# All rights reserved.
|
|
#
|
|
# 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. The names of the author(s) may not be used to endorse or promote
|
|
# products derived from this software without specific prior written
|
|
# permission.
|
|
#
|
|
# THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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.
|
|
|
|
"""
|
|
FIXME: This module contains ...
|
|
"""
|
|
|
|
from __future__ import print_function
|
|
|
|
from zope.interface import implementer
|
|
|
|
import os
|
|
import sys
|
|
|
|
from twisted.python import usage
|
|
from twisted.plugin import IPlugin
|
|
from twisted.application.service import IServiceMaker
|
|
from twisted.application import internet, service
|
|
from twisted.cred import portal
|
|
|
|
from cowrie.core.config import readConfigFile
|
|
from cowrie import core
|
|
import cowrie.core.realm
|
|
import cowrie.core.checkers
|
|
|
|
import cowrie.telnet.transport
|
|
import cowrie.ssh.factory
|
|
|
|
class Options(usage.Options):
|
|
"""
|
|
FIXME: Docstring
|
|
"""
|
|
optParameters = [
|
|
["port", "p", 0, "The port number to listen on.", int],
|
|
["config", "c", 'cowrie.cfg', "The configuration file to use."]
|
|
]
|
|
|
|
|
|
|
|
@implementer(IServiceMaker, IPlugin)
|
|
class CowrieServiceMaker(object):
|
|
"""
|
|
FIXME: Docstring
|
|
"""
|
|
tapname = "cowrie"
|
|
description = "She sells sea shells by the sea shore."
|
|
options = Options
|
|
|
|
def makeService(self, options):
|
|
"""
|
|
Construct a TCPServer from a factory defined in Cowrie.
|
|
"""
|
|
|
|
if os.name == 'posix' and os.getuid() == 0:
|
|
print('ERROR: You must not run cowrie as root!')
|
|
sys.exit(1)
|
|
|
|
cfg = readConfigFile(options["config"])
|
|
|
|
topService = service.MultiService()
|
|
application = service.Application('cowrie')
|
|
topService.setServiceParent(application)
|
|
|
|
factory = cowrie.ssh.factory.CowrieSSHFactory(cfg)
|
|
|
|
factory.portal = portal.Portal(core.realm.HoneyPotRealm(cfg))
|
|
factory.portal.registerChecker(
|
|
core.checkers.HoneypotPublicKeyChecker())
|
|
factory.portal.registerChecker(
|
|
core.checkers.HoneypotPasswordChecker(cfg))
|
|
|
|
if cfg.has_option('honeypot', 'auth_none_enabled') and \
|
|
cfg.get('honeypot', 'auth_none_enabled').lower() in \
|
|
('yes', 'true', 'on'):
|
|
factory.portal.registerChecker(
|
|
core.checkers.HoneypotNoneChecker())
|
|
|
|
if cfg.has_option('ssh', 'listen_addr'):
|
|
listen_ssh_addr = cfg.get('ssh', 'listen_addr')
|
|
elif cfg.has_option('honeypot', 'listen_addr'):
|
|
listen_ssh_addr = cfg.get('honeypot', 'listen_addr')
|
|
else:
|
|
listen_ssh_addr = '0.0.0.0'
|
|
|
|
# Preference: 1, option, 2, config, 3, default of 2222
|
|
if options['port'] != 0:
|
|
listen_ssh_port = int(options["port"])
|
|
elif cfg.has_option('ssh', 'listen_port'):
|
|
listen_ssh_port = cfg.getint('ssh', 'listen_port')
|
|
elif cfg.has_option('honeypot', 'listen_port'):
|
|
listen_ssh_port = cfg.getint('honeypot', 'listen_port')
|
|
else:
|
|
listen_ssh_port = 2222
|
|
|
|
for i in listen_ssh_addr.split():
|
|
svc = internet.TCPServer(listen_ssh_port, factory, interface=i)
|
|
# FIXME: Use addService on topService ?
|
|
svc.setServiceParent(topService)
|
|
|
|
if cfg.has_option('telnet', 'enabled') and \
|
|
cfg.get('telnet', 'enabled').lower() in \
|
|
('yes', 'true', 'on'):
|
|
|
|
if cfg.has_option('telnet', 'listen_addr'):
|
|
listen_telnet_addr = cfg.get('telnet', 'listen_addr')
|
|
else:
|
|
listen_telnet_addr = '0.0.0.0'
|
|
|
|
# Preference: 1, config, 2, default of 2223
|
|
if cfg.has_option('telnet', 'listen_port'):
|
|
listen_telnet_port = cfg.getint('telnet', 'listen_port')
|
|
else:
|
|
listen_telnet_port = 2223
|
|
|
|
f = cowrie.telnet.transport.HoneyPotTelnetFactory(cfg)
|
|
f.portal = portal.Portal(core.realm.HoneyPotRealm(cfg))
|
|
f.portal.registerChecker(core.checkers.HoneypotPasswordChecker(cfg))
|
|
if cfg.has_option('honeypot', 'auth_none_enabled') and \
|
|
cfg.get('honeypot', 'auth_none_enabled').lower() in \
|
|
('yes', 'true', 'on'):
|
|
f.portal.registerChecker(core.checkers.HoneypotNoneChecker())
|
|
for i in listen_telnet_addr.split():
|
|
tsvc = internet.TCPServer(listen_telnet_port, f, interface=i)
|
|
# FIXME: Use addService on topService ?
|
|
tsvc.setServiceParent(topService)
|
|
|
|
if cfg.has_option('honeypot', 'interact_enabled') and \
|
|
cfg.get('honeypot', 'interact_enabled').lower() in \
|
|
('yes', 'true', 'on'):
|
|
iport = int(cfg.get('honeypot', 'interact_port'))
|
|
# FIXME this doesn't support checking both Telnet and SSH sessions
|
|
from cowrie.core import interact
|
|
svc = internet.TCPServer(iport,
|
|
interact.makeInteractFactory(factory), interface='127.0.0.1')
|
|
# FIXME: Use addService on topService ?
|
|
svc.setServiceParent(topService)
|
|
|
|
return topService
|
|
|
|
# Now construct an object which *provides* the relevant interfaces
|
|
# The name of this variable is irrelevant, as long as there is *some*
|
|
# name bound to a provider of IPlugin and IServiceMaker.
|
|
|
|
serviceMaker = CowrieServiceMaker()
|