Twisted 中的 Telenet 服务器出现 "Unhandled error in Deferred" 错误
Telenet server in Twisted gets "Unhandled error in Deferred" errors
我正在尝试制作一个简单的 Telnet 服务器,用于记录机器人使用的 username/password 对,这些机器人试图暴力破解弱 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到服务器并尝试自己输入username/password,则不会出现错误;它们仅在机器人尝试登录时出现。我猜机器人正在尝试做一些我的服务器没有考虑到的事情,但我不知道我应该做什么。
编辑:
我已将上述脚本更改为使用 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 中的一些错误。
接下来,当 "Unhandled error in Deferred" 错误发生时,我在日志中得到了这个:
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'
有什么解决办法吗?
这是您对 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
提示进行 will/wont ECHO 协商。 will/wont ECHO 协商的要点通常是抑制密码回显。
我正在尝试制作一个简单的 Telnet 服务器,用于记录机器人使用的 username/password 对,这些机器人试图暴力破解弱 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到服务器并尝试自己输入username/password,则不会出现错误;它们仅在机器人尝试登录时出现。我猜机器人正在尝试做一些我的服务器没有考虑到的事情,但我不知道我应该做什么。
编辑:
我已将上述脚本更改为使用 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 中的一些错误。
接下来,当 "Unhandled error in Deferred" 错误发生时,我在日志中得到了这个:
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'
有什么解决办法吗?
这是您对 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
提示进行 will/wont ECHO 协商。 will/wont ECHO 协商的要点通常是抑制密码回显。