2017-09-04 44 views
0

我正在試圖製作一個簡單的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' 

任何想法如何解決呢?

+0

請將完整的確切錯誤添加到您的問題(複製/粘貼)。 –

+0

嗯,但我做到了。確切的錯誤信息是在控制檯上打印的「Deferred:Unhandled error in Deferred:」。顯然不是一個嚴重的錯誤,因爲腳本沒有中斷,也沒有回溯,但它有一些錯誤或警告,因爲它不是我的腳本顯示它。 – bontchev

+0

如果這些都出現了,那麼我認爲你正在使用一個Twisted版本,它有這個日誌記錄錯誤:。如果是這樣,那麼有關正在悄悄丟棄的故障的有用細節。這將有助於能夠看到它們 - 也許可以通過更改日誌記錄配置以避免該問題,降級到足夠舊的Twisted版本,或修復錯誤並進行升級。 –

回答

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)) 

還要注意的是,你實際上並沒有在應用程序中的任何迴音邏輯,所以你可能想補充一點,如果你會提供做echo'ing。您可能想要在Password提示符附近執行ECHO協商,而不是提示Username。 ECHO協商的意願通常是抑制密碼的回聲。

+0

好的,我已經完成了你所建議的修改,但是我正在解決這個錯誤。該日誌現在包含'Failure:twisted.internet.error.ConnectionLost:與其他人的連接以非乾淨的方式丟失:連接丟失.'和'Failure:twisted.conch.telnet.AlreadyDisabled:twisted.conch.telnet。 AlreadyDisabled:'\ x01''。我如何捕獲多個錯誤?列出他們在一個逗號分隔的列表或東西? – bontchev

+0

https://twistedmatrix.com/documents/17.5.0/api/twisted.python.failure.Failure.html#trap –

+0

謝謝。不得不添加更多的列表('AlreadyNegotiating'和'ConnectionDone'),但最終設法抑制錯誤消息。 – bontchev