Twisted中的Telenet服务器获取" Deferred中的未处理错误"错误

时间:2017-09-04 10:31:26

标签: python twisted telnet

我试图创建一个简单的Telnet服务器,记录那些机器人使用的用户名/密码对,试图暴力破解弱Telnet凭证(Mirai,Gafgyt等)。我试图将Twisted用于此目的,因为它似乎是用于此类目的的最先进技术。

这是我到目前为止所做的:

#!/usr/bin/env python

from twisted.conch.telnet import TelnetTransport, TelnetProtocol, ECHO
from twisted.internet.protocol import ServerFactory
from twisted.application.internet import TCPServer
from twisted.application.service import Application
from twisted.internet import reactor

import logging

class TelnetEcho(TelnetProtocol):

    ip = ''
    user = ''
    state = ''
    line = ''

    def connectionMade(self):
        self.ip = self.transport.getPeer().host
        self.transport.write('Username: ')
        self.transport.will(ECHO)
        self.state = 'User'

    def dataReceived(self, data):
        if self.state != 'Password':
            self.transport.write(data)
        self.line += data
        if data == '\n':
            self.processLine()
            self.line = ''
        return

    def processLine(self):
        if self.state == 'User':
            self.user = self.line.strip()
            self.transport.write('Password: ')
            self.state = 'Password'
        elif self.state == 'Password':
            print 'IP: ' + self.ip + ', user:' + self.user + ', pass:' + self.line.strip()
            logging.info(self.ip + ',' + self.user + ',' + self.line.strip())
            self.transport.write('\r\nIncorrect password or username.\r\n')
            self.transport.write('Username: ')
            self.state = 'User'

def CreateMyFactory():
    factory = ServerFactory()
    factory.protocol = lambda: TelnetTransport(TelnetEcho)
    return factory

if __name__ == "__main__":
    logging.basicConfig(filename='telnet.log', format='%(message)s', level=logging.DEBUG)
    logging.info('Tmestamp,IP,Username,Password')
    for handler in logging.root.handlers[:]:
        logging.root.removeHandler(handler)
    logging.basicConfig(filename='telnet.log', format='%(asctime)s,%(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.DEBUG)
    MyFactory = CreateMyFactory()
    reactor.listenTCP(23, MyFactory)
    reactor.run()

它工作得很好 - 我的意思是,机器人尝试登录并记录他们使用的凭据 - 但我不断收到Unhandled error in Deferred错误让我完全神秘化。我没有使用任何延期,至少不是故意的。是什么导致错误以及如何解决问题?

有趣的是,如果我手动telnet到服务器并尝试自己输入用户名/密码,则不会出现错误;它们只在机器人试图登录时出现。我猜机器人正试图做一些我的服务器不能解决的事情,但我无法弄清楚我应该做什么。

编辑:

我已将上述脚本更改为使用Twisted记录器而不是Python记录器。现在我在日志中获得了一些额外的信息。首先,我收到以下警告:

2017-09-06 16:17:01+0300 [-] Warning: primary log target selected twice at <c:\python\lib\site-packages\twisted\application\app.py:212> - previously selected at <c:\python\lib\site-packages\twisted\python\log.py:214>.  Remove one of the calls to beginLoggingTo.

我想这是Twisted中的一些错误。

接下来,当&#34; Deferred中的未处理错误&#34;发生错误,我得到这个日志:

2017-09-06 16:33:33+0300 [-] Unhandled error in Deferred:
2017-09-06 16:33:33+0300 [-] Unhandled Error
    Traceback (most recent call last):
    Failure: twisted.conch.telnet.OptionRefused: twisted.conch.telnet.OptionRefused:'\x01'

任何想法如何解决?

1 个答案:

答案 0 :(得分:0)

以下是您对Deferred

的使用
    self.transport.will(ECHO)

以下是will的API文档:

def will(option):
    """
    Indicate our willingness to begin performing this option locally.

    Returns a Deferred that fires with True when the peer agrees to allow us
    to begin performing this option, or fails with L{OptionRefused} if the
    peer refuses to allow us to begin performing it.  If the option is
    already enabled locally, the Deferred will fail with L{AlreadyEnabled}.
    If negotiation regarding this option is already in progress, the
    Deferred will fail with L{AlreadyNegotiating}.

    Note: It is currently possible that this Deferred will never fire,
    if the peer never responds, or if the peer believes the option to
    already be enabled.
    """

在您的情况下,对等方拒绝您提供执行ECHO功能。如果要取消拒绝报告,请添加errback以吞下该异常类型:

    d = self.transport.will(ECHO)
    d.addErrback(lambda reason: reason.trap(OptionRefused))

另请注意,您在应用程序中实际上没有任何回显逻辑,因此如果您打算提供回声,则可能需要添加它。您可能希望围绕Password提示而不是Username提示进行意愿/不进行ECHO协商。意志/不进行ECHO协商的要点通常是抑制密码的回声。