<s> """<STR_LIT>""" <EOL> import re <EOL> import time <EOL> import uuid <EOL> import zlib <EOL> import twisted <EOL> from twisted . conch . ssh import factory <EOL> from twisted . conch . ssh import keys <EOL> from twisted . conch . ssh import transport <EOL> from twisted . python import log <EOL> from twisted . conch . openssh_compat import primes <EOL> from twisted . conch . ssh . common import getNS <EOL> from twisted . protocols . policies import TimeoutMixin <EOL> from cowrie . ssh import connection <EOL> from cowrie . ssh import userauth <EOL> from cowrie . core import keys as cowriekeys <EOL> class HoneyPotSSHFactory ( factory . SSHFactory ) : <EOL> """<STR_LIT>""" <EOL> services = { <EOL> '<STR_LIT>' : userauth . HoneyPotSSHUserAuthServer , <EOL> '<STR_LIT>' : connection . CowrieSSHConnection , <EOL> } <EOL> def __init__ ( self , cfg ) : <EOL> self . cfg = cfg <EOL> def logDispatch ( self , * msg , ** args ) : <EOL> """<STR_LIT>""" <EOL> for dblog in self . dbloggers : <EOL> dblog . logDispatch ( * msg , ** args ) <EOL> for output in self . output_plugins : <EOL> output . logDispatch ( * msg , ** args ) <EOL> def startFactory ( self ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . sessions = { } <EOL> self . starttime = time . time ( ) <EOL> rsaPubKeyString , rsaPrivKeyString = cowriekeys . getRSAKeys ( self . cfg ) <EOL> dsaPubKeyString , dsaPrivKeyString = cowriekeys . getDSAKeys ( self . cfg ) <EOL> self . publicKeys = { <EOL> '<STR_LIT>' : keys . Key . fromString ( data = rsaPubKeyString ) , <EOL> '<STR_LIT>' : keys . Key . fromString ( data = dsaPubKeyString ) } <EOL> self . privateKeys = { <EOL> '<STR_LIT>' : keys . Key . fromString ( data = rsaPrivKeyString ) , <EOL> '<STR_LIT>' : keys . Key . fromString ( data = dsaPrivKeyString ) } <EOL> self . dbloggers = [ ] <EOL> for x in self . cfg . sections ( ) : <EOL> if not x . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> engine = x . split ( '<STR_LIT:_>' ) [ <NUM_LIT:1> ] <EOL> try : <EOL> dblogger = __import__ ( '<STR_LIT>' . format ( engine ) , <EOL> globals ( ) , locals ( ) , [ '<STR_LIT>' ] ) . DBLogger ( self . cfg ) <EOL> log . addObserver ( dblogger . emit ) <EOL> self . dbloggers . append ( dblogger ) <EOL> log . msg ( "<STR_LIT>" . format ( engine ) ) <EOL> except : <EOL> log . err ( ) <EOL> log . msg ( "<STR_LIT>" . format ( engine ) ) <EOL> self . output_plugins = [ ] <EOL> for x in self . cfg . sections ( ) : <EOL> if not x . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> engine = x . split ( '<STR_LIT:_>' ) [ <NUM_LIT:1> ] <EOL> try : <EOL> output = __import__ ( '<STR_LIT>' . format ( engine ) , <EOL> globals ( ) , locals ( ) , [ '<STR_LIT>' ] ) . Output ( self . cfg ) <EOL> log . addObserver ( output . emit ) <EOL> self . output_plugins . append ( output ) <EOL> log . msg ( "<STR_LIT>" . format ( engine ) ) <EOL> except : <EOL> log . err ( ) <EOL> log . msg ( "<STR_LIT>" . format ( engine ) ) <EOL> factory . SSHFactory . startFactory ( self ) <EOL> def stopFactory ( self ) : <EOL> """<STR_LIT:U+0020>""" <EOL> factory . SSHFactory . stopFactory ( self ) <EOL> for output in self . output_plugins : <EOL> output . stop ( ) <EOL> def buildProtocol ( self , addr ) : <EOL> """<STR_LIT>""" <EOL> _modulis = '<STR_LIT>' , '<STR_LIT>' <EOL> t = HoneyPotTransport ( ) <EOL> try : <EOL> t . ourVersionString = self . cfg . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> except : <EOL> t . ourVersionString = "<STR_LIT>" <EOL> t . supportedPublicKeys = list ( self . privateKeys . keys ( ) ) <EOL> for _moduli in _modulis : <EOL> try : <EOL> self . primes = primes . parseModuliFile ( _moduli ) <EOL> break <EOL> except IOError as err : <EOL> pass <EOL> if not self . primes : <EOL> ske = t . supportedKeyExchanges [ : ] <EOL> ske . remove ( '<STR_LIT>' ) <EOL> t . supportedKeyExchanges = ske <EOL> log . msg ( "<STR_LIT>" ) <EOL> t . supportedCiphers = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> t . supportedPublicKeys = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> t . supportedMACs = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> t . supportedCompressions = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:none>' ] <EOL> t . factory = self <EOL> return t <EOL> class HoneyPotTransport ( transport . SSHServerTransport , TimeoutMixin ) : <EOL> """<STR_LIT:U+0020>""" <EOL> def connectionMade ( self ) : <EOL> """<STR_LIT>""" <EOL> self . transportId = uuid . uuid4 ( ) . hex [ : <NUM_LIT:8> ] <EOL> src_ip = self . transport . getPeer ( ) . host <EOL> ipv4rex = re . compile ( '<STR_LIT>' ) <EOL> ipv4_search = ipv4rex . search ( src_ip ) <EOL> if ipv4_search != None : <EOL> src_ip = ipv4_search . group ( <NUM_LIT:1> ) <EOL> log . msg ( eventid = '<STR_LIT>' , <EOL> format = '<STR_LIT>' , <EOL> src_ip = src_ip , src_port = self . transport . getPeer ( ) . port , <EOL> dst_ip = self . transport . getHost ( ) . host , dst_port = self . transport . getHost ( ) . port , <EOL> session = self . transportId , sessionno = self . transport . sessionno ) <EOL> self . transport . write ( '<STR_LIT>' . format ( self . ourVersionString ) ) <EOL> self . currentEncryptions = transport . SSHCiphers ( '<STR_LIT:none>' , '<STR_LIT:none>' , '<STR_LIT:none>' , '<STR_LIT:none>' ) <EOL> self . currentEncryptions . setKeys ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . setTimeout ( <NUM_LIT> ) <EOL> self . logintime = time . time ( ) <EOL> def sendKexInit ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . gotVersion : <EOL> return <EOL> transport . SSHServerTransport . sendKexInit ( self ) <EOL> def dataReceived ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . buf = self . buf + data <EOL> if not self . gotVersion : <EOL> if not '<STR_LIT:\n>' in self . buf : <EOL> return <EOL> self . otherVersionString = self . buf . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:0> ] . strip ( ) <EOL> if self . buf . startswith ( '<STR_LIT>' ) : <EOL> self . gotVersion = True <EOL> remoteVersion = self . buf . split ( '<STR_LIT:->' ) [ <NUM_LIT:1> ] <EOL> if remoteVersion not in self . supportedVersions : <EOL> self . _unsupportedVersionReceived ( remoteVersion ) <EOL> return <EOL> i = self . buf . index ( '<STR_LIT:\n>' ) <EOL> self . buf = self . buf [ i + <NUM_LIT:1> : ] <EOL> self . sendKexInit ( ) <EOL> else : <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> log . msg ( '<STR_LIT>' % ( self . otherVersionString , ) ) <EOL> self . transport . loseConnection ( ) <EOL> return <EOL> packet = self . getPacket ( ) <EOL> while packet : <EOL> messageNum = ord ( packet [ <NUM_LIT:0> ] ) <EOL> self . dispatchMessage ( messageNum , packet [ <NUM_LIT:1> : ] ) <EOL> packet = self . getPacket ( ) <EOL> if twisted . version . major < <NUM_LIT:11> and not self . _hadVersion and self . gotVersion : <EOL> self . sendKexInit ( ) <EOL> self . _hadVersion = True <EOL> def ssh_KEXINIT ( self , packet ) : <EOL> """<STR_LIT:U+0020>""" <EOL> k = getNS ( packet [ <NUM_LIT:16> : ] , <NUM_LIT:10> ) <EOL> strings , rest = k [ : - <NUM_LIT:1> ] , k [ - <NUM_LIT:1> ] <EOL> ( kexAlgs , keyAlgs , encCS , encSC , macCS , macSC , compCS , compSC , langCS , <EOL> langSC ) = [ s . split ( '<STR_LIT:U+002C>' ) for s in strings ] <EOL> log . msg ( eventid = '<STR_LIT>' , version = self . otherVersionString , <EOL> kexAlgs = kexAlgs , keyAlgs = keyAlgs , encCS = encCS , macCS = macCS , <EOL> compCS = compCS , format = '<STR_LIT>' ) <EOL> return transport . SSHServerTransport . ssh_KEXINIT ( self , packet ) <EOL> def timeoutConnection ( self ) : <EOL> """<STR_LIT:U+0020>""" <EOL> log . msg ( "<STR_LIT>" ) <EOL> self . transport . loseConnection ( ) <EOL> def setService ( self , service ) : <EOL> """<STR_LIT>""" <EOL> if service . name == "<STR_LIT>" : <EOL> self . setTimeout ( None ) <EOL> if service . name == "<STR_LIT>" : <EOL> if self . outgoingCompressionType == '<STR_LIT>' : <EOL> self . outgoingCompression = zlib . compressobj ( <NUM_LIT:6> ) <EOL> if self . incomingCompressionType == '<STR_LIT>' : <EOL> self . incomingCompression = zlib . decompressobj ( ) <EOL> transport . SSHServerTransport . setService ( self , service ) <EOL> def connectionLost ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> self . setTimeout ( None ) <EOL> transport . SSHServerTransport . connectionLost ( self , reason ) <EOL> self . transport . connectionLost ( reason ) <EOL> self . transport = None <EOL> duration = time . time ( ) - self . logintime <EOL> log . msg ( eventid = '<STR_LIT>' , <EOL> format = '<STR_LIT>' , <EOL> duration = duration ) <EOL> def sendDisconnect ( self , reason , desc ) : <EOL> """<STR_LIT>""" <EOL> if not '<STR_LIT>' in desc : <EOL> transport . SSHServerTransport . sendDisconnect ( self , reason , desc ) <EOL> else : <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> log . msg ( '<STR_LIT>' <EOL> % ( reason , desc ) ) <EOL> self . transport . loseConnection ( ) </s>
