<s> """<STR_LIT>""" <EOL> import time <EOL> from twisted . internet import protocol <EOL> from twisted . conch import telnet , recvline <EOL> from cowrie . core import ttylog <EOL> class Interact ( telnet . Telnet ) : <EOL> """<STR_LIT:U+0020>""" <EOL> def connectionMade ( self ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . interacting = None <EOL> self . cmdbuf = '<STR_LIT>' <EOL> self . honeypotFactory = self . factory . honeypotFactory <EOL> d = self . do ( telnet . LINEMODE ) <EOL> self . requestNegotiation ( telnet . LINEMODE , telnet . LINEMODE_EDIT + '<STR_LIT>' ) <EOL> self . will ( telnet . ECHO ) <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> self . cmd_help ( ) <EOL> def connectionLost ( self , reason ) : <EOL> """<STR_LIT:U+0020>""" <EOL> if self . interacting != None : <EOL> self . interacting . terminal . delInteractor ( self ) <EOL> def enableRemote ( self , option ) : <EOL> """<STR_LIT:U+0020>""" <EOL> return option == telnet . LINEMODE <EOL> def disableRemote ( self , option ) : <EOL> """<STR_LIT:U+0020>""" <EOL> pass <EOL> def applicationDataReceived ( self , bytes ) : <EOL> """<STR_LIT:U+0020>""" <EOL> if not self . interacting : <EOL> self . transport . write ( bytes ) <EOL> if bytes in ( '<STR_LIT:\r>' , '<STR_LIT:\n>' ) : <EOL> self . transport . write ( '<STR_LIT:\n>' ) <EOL> pieces = self . cmdbuf . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> self . cmdbuf = '<STR_LIT>' <EOL> cmd , args = pieces [ <NUM_LIT:0> ] , '<STR_LIT>' <EOL> if len ( pieces ) > <NUM_LIT:1> : <EOL> args = pieces [ <NUM_LIT:1> ] <EOL> try : <EOL> func = getattr ( self , '<STR_LIT>' + cmd ) <EOL> except AttributeError : <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> return <EOL> func ( args ) <EOL> else : <EOL> self . cmdbuf += bytes <EOL> else : <EOL> for c in bytes : <EOL> if ord ( c ) == <NUM_LIT> : <EOL> self . interacting . terminal . delInteractor ( self ) <EOL> self . interacting = None <EOL> self . transport . write ( <EOL> '<STR_LIT>' ) <EOL> return <EOL> if not self . readonly : <EOL> if type ( bytes ) == type ( '<STR_LIT>' ) : <EOL> ttylog . ttylog_write ( <EOL> self . interacting . terminal . ttylog_file , <EOL> len ( bytes ) , ttylog . TYPE_INTERACT , time . time ( ) , bytes ) <EOL> for c in bytes : <EOL> recvline . HistoricRecvLine . keystrokeReceived ( <EOL> self . interacting , c , None ) <EOL> def sessionWrite ( self , data ) : <EOL> """<STR_LIT:U+0020>""" <EOL> buf , prev = '<STR_LIT>' , '<STR_LIT>' <EOL> for c in data : <EOL> if c == '<STR_LIT:\n>' and prev != '<STR_LIT:\r>' : <EOL> buf += '<STR_LIT:\r\n>' <EOL> else : <EOL> buf += c <EOL> prev = c <EOL> self . transport . write ( buf ) <EOL> def sessionClosed ( self ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . interacting . terminal . delInteractor ( self ) <EOL> self . interacting = None <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> def cmd_hijack ( self , args ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . cmd_view ( args ) <EOL> self . readonly = False <EOL> def cmd_view ( self , args ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . readonly = True <EOL> try : <EOL> sessionno = int ( args ) <EOL> except ValueError : <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> return <EOL> for s in self . honeypotFactory . sessions : <EOL> if sessionno == s : <EOL> self . view ( s ) <EOL> return <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> def view ( self , sessionno ) : <EOL> """<STR_LIT:U+0020>""" <EOL> session = self . honeypotFactory . sessions [ sessionno ] <EOL> self . transport . write ( <EOL> '<STR_LIT>' % ( sessionno , ) ) <EOL> session . terminal . addInteractor ( self ) <EOL> self . interacting = session <EOL> def cmd_list ( self , args ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> for s in self . honeypotFactory . sessions : <EOL> session = self . honeypotFactory . sessions [ s ] <EOL> self . transport . write ( '<STR_LIT>' % ( str ( s ) . ljust ( <NUM_LIT:4> ) , <EOL> session . realClientIP . ljust ( <NUM_LIT:15> ) , <EOL> session . clientVersion ) ) <EOL> def cmd_help ( self , args = '<STR_LIT>' ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> self . transport . write ( <EOL> '<STR_LIT>' ) <EOL> self . transport . write ( <EOL> '<STR_LIT>' ) <EOL> self . transport . write ( <EOL> '<STR_LIT>' ) <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> def cmd_disconnect ( self , args ) : <EOL> """<STR_LIT:U+0020>""" <EOL> try : <EOL> sessionno = int ( args ) <EOL> except ValueError : <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> return <EOL> for s in self . honeypotFactory . sessions : <EOL> if sessionno == s : <EOL> self . transport . write ( <EOL> '<STR_LIT>' % ( sessionno , ) ) <EOL> self . honeypotFactory . sessions [ s ] . terminal . loseConnection ( ) <EOL> return <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> def cmd_exit ( self , args = '<STR_LIT>' ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . transport . loseConnection ( ) <EOL> def makeInteractFactory ( honeypotFactory ) : <EOL> """<STR_LIT:U+0020>""" <EOL> ifactory = protocol . Factory ( ) <EOL> ifactory . protocol = Interact <EOL> ifactory . honeypotFactory = honeypotFactory <EOL> return ifactory </s>
