<s> import twisted <EOL> from copy import deepcopy , copy <EOL> import os <EOL> import shlex <EOL> from kippo . core import fs <EOL> from kippo . core . config import config <EOL> import kippo . core . exceptions <EOL> from kippo import core <EOL> import pickle <EOL> class HoneyPotCommand ( object ) : <EOL> def __init__ ( self , honeypot , * args ) : <EOL> self . honeypot = honeypot <EOL> self . args = args <EOL> self . writeln = self . honeypot . writeln <EOL> self . write = self . honeypot . terminal . write <EOL> self . nextLine = self . honeypot . terminal . nextLine <EOL> self . fs = self . honeypot . fs <EOL> def start ( self ) : <EOL> self . call ( ) <EOL> self . exit ( ) <EOL> def call ( self ) : <EOL> self . honeypot . writeln ( '<STR_LIT>' % repr ( self . args ) ) <EOL> def exit ( self ) : <EOL> self . honeypot . cmdstack . pop ( ) <EOL> self . honeypot . cmdstack [ - <NUM_LIT:1> ] . resume ( ) <EOL> def ctrl_c ( self ) : <EOL> print '<STR_LIT>' <EOL> self . writeln ( '<STR_LIT>' ) <EOL> self . exit ( ) <EOL> def lineReceived ( self , line ) : <EOL> print '<STR_LIT>' % line <EOL> def resume ( self ) : <EOL> pass <EOL> def handle_TAB ( self ) : <EOL> pass <EOL> class HoneyPotShell ( object ) : <EOL> def __init__ ( self , honeypot , interactive = True ) : <EOL> self . honeypot = honeypot <EOL> self . interactive = interactive <EOL> self . showPrompt ( ) <EOL> self . cmdpending = [ ] <EOL> self . envvars = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def lineReceived ( self , line ) : <EOL> print '<STR_LIT>' % line <EOL> line = line [ : <NUM_LIT> ] <EOL> for i in [ x . strip ( ) for x in line . strip ( ) . split ( '<STR_LIT:;>' ) [ : <NUM_LIT:10> ] ] : <EOL> if not len ( i ) : <EOL> continue <EOL> self . cmdpending . append ( i ) <EOL> if len ( self . cmdpending ) : <EOL> self . runCommand ( ) <EOL> else : <EOL> self . showPrompt ( ) <EOL> def runCommand ( self ) : <EOL> def runOrPrompt ( ) : <EOL> if len ( self . cmdpending ) : <EOL> self . runCommand ( ) <EOL> else : <EOL> self . showPrompt ( ) <EOL> if not len ( self . cmdpending ) : <EOL> if self . interactive : <EOL> self . showPrompt ( ) <EOL> else : <EOL> self . honeypot . terminal . transport . loseConnection ( ) <EOL> return <EOL> line = self . cmdpending . pop ( <NUM_LIT:0> ) <EOL> try : <EOL> cmdAndArgs = shlex . split ( line ) <EOL> except : <EOL> self . honeypot . writeln ( <EOL> '<STR_LIT>' ) <EOL> self . cmdpending = [ ] <EOL> self . showPrompt ( ) <EOL> return <EOL> envvars = copy ( self . envvars ) <EOL> cmd = None <EOL> while len ( cmdAndArgs ) : <EOL> piece = cmdAndArgs . pop ( <NUM_LIT:0> ) <EOL> if piece . count ( '<STR_LIT:=>' ) : <EOL> key , value = piece . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> envvars [ key ] = value <EOL> continue <EOL> cmd = piece <EOL> break <EOL> args = cmdAndArgs <EOL> if not cmd : <EOL> runOrPrompt ( ) <EOL> return <EOL> rargs = [ ] <EOL> for arg in args : <EOL> matches = self . honeypot . fs . resolve_path_wc ( arg , self . honeypot . cwd ) <EOL> if matches : <EOL> rargs . extend ( matches ) <EOL> else : <EOL> rargs . append ( arg ) <EOL> cmdclass = self . honeypot . getCommand ( cmd , envvars [ '<STR_LIT>' ] . split ( '<STR_LIT::>' ) ) <EOL> if cmdclass : <EOL> print '<STR_LIT>' % ( line , ) <EOL> self . honeypot . logDispatch ( '<STR_LIT>' % ( line , ) ) <EOL> self . honeypot . call_command ( cmdclass , * rargs ) <EOL> else : <EOL> self . honeypot . logDispatch ( '<STR_LIT>' % ( line , ) ) <EOL> print '<STR_LIT>' % ( line , ) <EOL> if len ( line ) : <EOL> self . honeypot . writeln ( '<STR_LIT>' % cmd ) <EOL> runOrPrompt ( ) <EOL> def resume ( self ) : <EOL> if self . interactive : <EOL> self . honeypot . setInsertMode ( ) <EOL> self . runCommand ( ) <EOL> def showPrompt ( self ) : <EOL> if not self . interactive : <EOL> return <EOL> prompt = '<STR_LIT>' % ( self . honeypot . user . username , self . honeypot . hostname , ) <EOL> if not self . honeypot . user . uid : <EOL> prompt += '<STR_LIT>' <EOL> else : <EOL> prompt += '<STR_LIT>' <EOL> path = self . honeypot . cwd <EOL> homelen = len ( self . honeypot . user . home ) <EOL> if path == self . honeypot . user . home : <EOL> path = '<STR_LIT>' <EOL> elif len ( path ) > ( homelen + <NUM_LIT:1> ) and path [ : ( homelen + <NUM_LIT:1> ) ] == self . honeypot . user . home + '<STR_LIT:/>' : <EOL> path = '<STR_LIT>' + path [ homelen : ] <EOL> attrs = { '<STR_LIT:path>' : path } <EOL> self . honeypot . terminal . write ( prompt % attrs ) <EOL> def ctrl_c ( self ) : <EOL> self . honeypot . lineBuffer = [ ] <EOL> self . honeypot . lineBufferIndex = <NUM_LIT:0> <EOL> self . honeypot . terminal . nextLine ( ) <EOL> self . showPrompt ( ) <EOL> def handle_TAB ( self ) : <EOL> if not len ( self . honeypot . lineBuffer ) : <EOL> return <EOL> l = '<STR_LIT>' . join ( self . honeypot . lineBuffer ) <EOL> if l [ - <NUM_LIT:1> ] == '<STR_LIT:U+0020>' : <EOL> clue = '<STR_LIT>' <EOL> else : <EOL> clue = '<STR_LIT>' . join ( self . honeypot . lineBuffer ) . split ( ) [ - <NUM_LIT:1> ] <EOL> try : <EOL> basedir = os . path . dirname ( clue ) <EOL> except : <EOL> pass <EOL> if len ( basedir ) and basedir [ - <NUM_LIT:1> ] != '<STR_LIT:/>' : <EOL> basedir += '<STR_LIT:/>' <EOL> files = [ ] <EOL> tmppath = basedir <EOL> if not len ( basedir ) : <EOL> tmppath = self . honeypot . cwd <EOL> try : <EOL> r = self . honeypot . fs . resolve_path ( tmppath , self . honeypot . cwd ) <EOL> except : <EOL> return <EOL> for x in self . honeypot . fs . get_path ( r ) : <EOL> if clue == '<STR_LIT>' : <EOL> files . append ( x ) <EOL> continue <EOL> if not x [ fs . A_NAME ] . startswith ( os . path . basename ( clue ) ) : <EOL> continue <EOL> files . append ( x ) <EOL> if len ( files ) == <NUM_LIT:0> : <EOL> return <EOL> for i in range ( self . honeypot . lineBufferIndex ) : <EOL> self . honeypot . terminal . cursorBackward ( ) <EOL> self . honeypot . terminal . deleteCharacter ( ) <EOL> newbuf = '<STR_LIT>' <EOL> if len ( files ) == <NUM_LIT:1> : <EOL> newbuf = '<STR_LIT:U+0020>' . join ( l . split ( ) [ : - <NUM_LIT:1> ] + [ '<STR_LIT>' % ( basedir , files [ <NUM_LIT:0> ] [ fs . A_NAME ] ) ] ) <EOL> if files [ <NUM_LIT:0> ] [ fs . A_TYPE ] == fs . T_DIR : <EOL> newbuf += '<STR_LIT:/>' <EOL> else : <EOL> newbuf += '<STR_LIT:U+0020>' <EOL> else : <EOL> if len ( os . path . basename ( clue ) ) : <EOL> prefix = os . path . commonprefix ( [ x [ fs . A_NAME ] for x in files ] ) <EOL> else : <EOL> prefix = '<STR_LIT>' <EOL> first = l . split ( '<STR_LIT:U+0020>' ) [ : - <NUM_LIT:1> ] <EOL> newbuf = '<STR_LIT:U+0020>' . join ( first + [ '<STR_LIT>' % ( basedir , prefix ) ] ) <EOL> if newbuf == '<STR_LIT>' . join ( self . honeypot . lineBuffer ) : <EOL> self . honeypot . terminal . nextLine ( ) <EOL> maxlen = max ( [ len ( x [ fs . A_NAME ] ) for x in files ] ) + <NUM_LIT:1> <EOL> perline = int ( self . honeypot . user . windowSize [ <NUM_LIT:1> ] / ( maxlen + <NUM_LIT:1> ) ) <EOL> count = <NUM_LIT:0> <EOL> for file in files : <EOL> if count == perline : <EOL> count = <NUM_LIT:0> <EOL> self . honeypot . terminal . nextLine ( ) <EOL> self . honeypot . terminal . write ( file [ fs . A_NAME ] . ljust ( maxlen ) ) <EOL> count += <NUM_LIT:1> <EOL> self . honeypot . terminal . nextLine ( ) <EOL> self . showPrompt ( ) <EOL> self . honeypot . lineBuffer = list ( newbuf ) <EOL> self . honeypot . lineBufferIndex = len ( self . honeypot . lineBuffer ) <EOL> self . honeypot . terminal . write ( newbuf ) <EOL> class HoneyPotEnvironment ( object ) : <EOL> def __init__ ( self ) : <EOL> self . cfg = config ( ) <EOL> self . commands = { } <EOL> import kippo . commands <EOL> for c in kippo . commands . __all__ : <EOL> module = __import__ ( '<STR_LIT>' % c , <EOL> globals ( ) , locals ( ) , [ '<STR_LIT>' ] ) <EOL> self . commands . update ( module . commands ) <EOL> self . fs = pickle . load ( file ( <EOL> self . cfg . get ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT:rb>' ) ) </s>
