我正在試圖製作一個簡單的Telnet服務器,用於記錄那些試圖暴力破解弱Telnet證書的機器人使用的用戶名/密碼對(Mirai,Gafgyt等)。我試圖用Twisted來達到這個目的,因爲它似乎是用於此目的的最新技術。Twisted中的Telenet服務器獲取「未處理的延遲錯誤」錯誤
這是我到目前爲止做出:
#!/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到服務器並嘗試自己輸入用戶名/密碼,則不會出現錯誤;它們只在殭屍程序試圖登錄時出現。我猜殭屍正試圖做一些我的服務器沒有考慮到的事情,但我無法弄清楚我應該做什麼。
編輯:
我已經改變了上面的腳本使用雙絞線代替記錄了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中的一些錯誤。
接下來,當錯誤「遞延未處理的錯誤」的發生,我得到這個我的日誌:
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:Unhandled error in Deferred:」。顯然不是一個嚴重的錯誤,因爲腳本沒有中斷,也沒有回溯,但它有一些錯誤或警告,因爲它不是我的腳本顯示它。 – bontchev
如果這些都出現了,那麼我認爲你正在使用一個Twisted版本,它有這個日誌記錄錯誤:。如果是這樣,那麼有關正在悄悄丟棄的故障的有用細節。這將有助於能夠看到它們 - 也許可以通過更改日誌記錄配置以避免該問題,降級到足夠舊的Twisted版本,或修復錯誤並進行升級。 –