<s> """<STR_LIT:U+0020>""" <EOL> import time <EOL> import re <EOL> import hashlib <EOL> import getopt <EOL> import socket <EOL> from twisted . python import log <EOL> from twisted . internet import reactor <EOL> from cowrie . core . honeypot import HoneyPotCommand <EOL> commands = { } <EOL> class command_ssh ( HoneyPotCommand ) : <EOL> """<STR_LIT:U+0020>""" <EOL> def valid_ip ( self , address ) : <EOL> """<STR_LIT:U+0020>""" <EOL> try : <EOL> socket . inet_aton ( address ) <EOL> return True <EOL> except : <EOL> return False <EOL> def start ( self ) : <EOL> """<STR_LIT:U+0020>""" <EOL> try : <EOL> optlist , args = getopt . getopt ( self . args , <EOL> '<STR_LIT>' ) <EOL> except getopt . GetoptError as err : <EOL> self . write ( '<STR_LIT>' ) <EOL> self . exit ( ) <EOL> for opt in optlist : <EOL> if opt [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> self . write ( '<STR_LIT>' ) <EOL> self . exit ( ) <EOL> return <EOL> if not len ( args ) : <EOL> for l in ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) : <EOL> self . write ( l + '<STR_LIT:\n>' ) <EOL> self . exit ( ) <EOL> return <EOL> user , host = '<STR_LIT:root>' , args [ <NUM_LIT:0> ] <EOL> for opt in optlist : <EOL> if opt [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> user = opt [ <NUM_LIT:1> ] <EOL> if args [ <NUM_LIT:0> ] . count ( '<STR_LIT:@>' ) : <EOL> user , host = args [ <NUM_LIT:0> ] . split ( '<STR_LIT:@>' , <NUM_LIT:1> ) <EOL> if re . match ( '<STR_LIT>' , host ) : <EOL> if self . valid_ip ( host ) : <EOL> self . ip = host <EOL> else : <EOL> self . write ( '<STR_LIT>' % ( host , ) ) <EOL> self . exit ( ) <EOL> else : <EOL> s = hashlib . md5 ( host ) . hexdigest ( ) <EOL> self . ip = '<STR_LIT:.>' . join ( [ str ( int ( x , <NUM_LIT:16> ) ) for x in <EOL> ( s [ <NUM_LIT:0> : <NUM_LIT:2> ] , s [ <NUM_LIT:2> : <NUM_LIT:4> ] , s [ <NUM_LIT:4> : <NUM_LIT:6> ] , s [ <NUM_LIT:6> : <NUM_LIT:8> ] ) ] ) <EOL> self . host = host <EOL> self . user = user <EOL> self . write ( '<STR_LIT>' % ( self . host , self . ip ) ) <EOL> self . write ( '<STR_LIT>' ) <EOL> self . write ( '<STR_LIT>' ) <EOL> self . callbacks = [ self . yesno , self . wait ] <EOL> def yesno ( self , line ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . write ( <EOL> '<STR_LIT>' % self . host ) <EOL> self . write ( '<STR_LIT>' % ( self . user , self . host ) ) <EOL> self . protocol . password_input = True <EOL> def wait ( self , line ) : <EOL> """<STR_LIT:U+0020>""" <EOL> reactor . callLater ( <NUM_LIT:2> , self . finish , line ) <EOL> def finish ( self , line ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . pause = False <EOL> rest , host = self . host , '<STR_LIT:localhost>' <EOL> rest = self . host . strip ( ) . split ( '<STR_LIT:.>' ) <EOL> if len ( rest ) and rest [ <NUM_LIT:0> ] . isalpha ( ) : <EOL> host = rest [ <NUM_LIT:0> ] <EOL> self . protocol . hostname = host <EOL> self . protocol . cwd = '<STR_LIT>' <EOL> if not self . fs . exists ( self . protocol . cwd ) : <EOL> self . protocol . cwd = '<STR_LIT:/>' <EOL> self . protocol . password_input = False <EOL> self . write ( <EOL> '<STR_LIT>' % ( self . protocol . hostname , ) ) <EOL> self . write ( '<STR_LIT>' % ( time . ctime ( time . time ( ) - <NUM_LIT> ) , ) ) <EOL> self . exit ( ) <EOL> def lineReceived ( self , line ) : <EOL> """<STR_LIT:U+0020>""" <EOL> log . msg ( '<STR_LIT>' , line ) <EOL> if len ( self . callbacks ) : <EOL> self . callbacks . pop ( <NUM_LIT:0> ) ( line ) <EOL> commands [ '<STR_LIT>' ] = command_ssh </s>
