Reimplement running commands in a bit more sane way - should allow for easier

interactive commands (eg. passwd).

Fix history (uparrow)



git-svn-id: https://kippo.googlecode.com/svn/trunk@20 951d7100-d841-11de-b865-b3884708a8e2
This commit is contained in:
desaster
2009-11-17 21:49:37 +00:00
parent 29569cb639
commit 12f83ccb6f
3 changed files with 99 additions and 98 deletions

View File

@@ -99,7 +99,7 @@ class command_quit(HoneyPotCommand):
def call(self, args): def call(self, args):
self.honeypot.terminal.reset() self.honeypot.terminal.reset()
self.honeypot.writeln('Connection to server closed.') self.honeypot.writeln('Connection to server closed.')
self.honeypot.prompt = 'localhost:%(path)s# ' self.honeypot.hostname = 'localhost'
class command_clear(HoneyPotCommand): class command_clear(HoneyPotCommand):
def call(self, args): def call(self, args):
@@ -140,23 +140,25 @@ class command_pwd(HoneyPotCommand):
self.honeypot.writeln(self.honeypot.cwd) self.honeypot.writeln(self.honeypot.cwd)
class command_passwd(HoneyPotCommand): class command_passwd(HoneyPotCommand):
def call(self, args): def start(self):
self.honeypot.terminal.write('Enter new UNIX password: ') self.honeypot.terminal.write('Enter new UNIX password: ')
self.callback = callback_passwd1
self.honeypot.password_input = True self.honeypot.password_input = True
self.callbacks = [self.ask_again, self.finish]
class callback_passwd1(HoneyPotCommand): def ask_again(self):
def call(self, args):
self.honeypot.terminal.write('Retype new UNIX password: ') self.honeypot.terminal.write('Retype new UNIX password: ')
self.callback = callback_passwd2
class callback_passwd2(HoneyPotCommand): def finish(self):
def call(self, args):
self.honeypot.password_input = False self.honeypot.password_input = False
self.honeypot.writeln('Sorry, passwords do not match') self.honeypot.writeln('Sorry, passwords do not match')
self.honeypot.writeln( self.honeypot.writeln(
'passwd: Authentication information cannot be recovered') 'passwd: Authentication information cannot be recovered')
self.honeypot.writeln('passwd: password unchanged') self.honeypot.writeln('passwd: password unchanged')
self.exit()
def lineReceived(self, line):
print 'passwd input:', line
self.callbacks.pop(0)()
class command_nop(HoneyPotCommand): class command_nop(HoneyPotCommand):
def call(self, args): def call(self, args):

View File

@@ -1,8 +1,8 @@
from core.Kippo import HoneyPotCommand from core.Kippo import HoneyPotCommand
class command_ssh(HoneyPotCommand): class command_ssh(HoneyPotCommand):
def call(self, args): def start(self):
if not len(args.strip()): if not len(self.args.strip()):
for l in ( for l in (
'usage: ssh [-1246AaCfgKkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]', 'usage: ssh [-1246AaCfgKkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]',
' [-D [bind_address:]port] [-e escape_char] [-F configfile]', ' [-D [bind_address:]port] [-e escape_char] [-F configfile]',
@@ -12,33 +12,34 @@ class command_ssh(HoneyPotCommand):
' [-w local_tun[:remote_tun]] [user@]hostname [command]', ' [-w local_tun[:remote_tun]] [user@]hostname [command]',
): ):
self.honeypot.writeln(l) self.honeypot.writeln(l)
self.exit()
return return
self.honeypot.writeln('The authenticity of host \'127.0.0.4 (127.0.0.4)\' can\'t be established.') self.host = self.args.strip()
self.honeypot.writeln('RSA key fingerprint is 9c:30:97:8e:9e:f8:0d:de:04:8d:76:3a:7b:4b:30:f8.') self.honeypot.writeln('The authenticity of host \'187.42.2.9 (187.42.2.9)\' can\'t be established.')
self.honeypot.writeln('RSA key fingerprint is 9d:30:97:8a:9e:48:0d:de:04:8d:76:3a:7b:4b:30:f8.')
self.honeypot.terminal.write('Are you sure you want to continue connecting (yes/no)? ') self.honeypot.terminal.write('Are you sure you want to continue connecting (yes/no)? ')
self.callback = (callback_connect, args) self.callbacks = [self.yesno, self.finish]
class callback_connect(HoneyPotCommand): def yesno(self, args):
def call(self, line, args):
host = args.strip() host = args.strip()
self.honeypot.writeln( self.honeypot.writeln(
'Warning: Permanently added \'%s\' (RSA) to the list of known hosts.' % \ 'Warning: Permanently added \'%s\' (RSA) to the list of known hosts.' % \
host) host)
self.honeypot.terminal.write('%s\'s password: ' % args) self.honeypot.terminal.write('%s\'s password: ' % self.host)
self.honeypot.password_input = True self.honeypot.password_input = True
self.callback = (callback_done, args)
class callback_done(HoneyPotCommand): def finish(self, args):
def call(self, line, args): user, rest, host = 'root', self.host, 'localhost'
user = 'root' if self.host.count('@'):
if args.count('@'): user, rest = self.host.split('@', 1)
user, rest = args.split('@', 1) rest = rest.strip().split('.')
else: if len(rest) and rest[0].isalpha():
rest = args host = rest[0]
host = rest.strip().split('.')
if len(host) and host[0].isalpha(): self.honeypot.hostname = host
host = host[0]
else:
host = 'localhost'
self.honeypot.password_input = False self.honeypot.password_input = False
self.honeypot.prompt = '%s:%%(path)s# ' % host self.exit()
def lineReceived(self, line):
print 'ssh input:', line
self.callbacks.pop(0)(line)

View File

@@ -14,39 +14,83 @@ from core import ttylog
from core.fstypes import * from core.fstypes import *
import config import config
class HoneyPotCommand(object):
def __init__(self, honeypot, args):
self.honeypot = honeypot
self.args = args
def start(self):
self.call(self.args)
self.exit()
def call(self, *args):
self.honeypot.writeln('Hello World! [%s]' % repr(args))
def exit(self):
self.honeypot.cmdstack.pop()
self.honeypot.cmdstack[-1].resume()
def lineReceived(self, line):
print 'Unhandled input: %s' % line
def resume(self):
pass
class HoneyPotShell(object):
def __init__(self, honeypot):
self.honeypot = honeypot
self.showPrompt()
def lineReceived(self, line):
cmdAndArgs = line.split(' ', 1)
if len(cmdAndArgs) > 1:
cmd, args = cmdAndArgs
else:
cmd, args = cmdAndArgs[0], ''
cmdclass = self.honeypot.getCommand(cmd)
if cmdclass:
obj = cmdclass(self.honeypot, args)
self.honeypot.cmdstack.append(obj)
obj.start()
else:
self.honeypot.writeln('bash: %s: command not found' % cmd)
self.showPrompt()
def resume(self):
self.showPrompt()
def showPrompt(self):
prompt = '%s:%%(path)s# ' % self.honeypot.hostname
path = self.honeypot.cwd
if path == '/root':
path = '~'
attrs = {'path': path}
self.honeypot.terminal.write(prompt % attrs)
class HoneyPotProtocol(recvline.HistoricRecvLine): class HoneyPotProtocol(recvline.HistoricRecvLine):
def __init__(self, user, env): def __init__(self, user, env):
self.user = user self.user = user
self.env = env self.env = env
self.cwd = '/root' self.cwd = '/root'
self.hostname = config.fake_hostname
self.fs = HoneyPotFilesystem(deepcopy(self.env.fs)) self.fs = HoneyPotFilesystem(deepcopy(self.env.fs))
self.prompt = '%s:%%(path)s# ' % config.fake_hostname
self.next_callback = None
self.password_input = False self.password_input = False
self.cmdstack = []
def connectionMade(self): def connectionMade(self):
recvline.HistoricRecvLine.connectionMade(self) recvline.HistoricRecvLine.connectionMade(self)
self.showPrompt() self.cmdstack = [HoneyPotShell(self)]
# Overriding to prevent terminal.reset() # Overriding to prevent terminal.reset()
def initializeScreen(self): def initializeScreen(self):
self.setInsertMode() self.setInsertMode()
def showPrompt(self): def getCommand(self, cmd):
path = self.cwd if not len(cmd.strip()):
if path == '/root': return None
path = '~'
attrs = {
'path': path,
}
self.terminal.write(self.prompt % attrs)
def getCommand(self, cmd, args):
path = None path = None
if cmd in self.env.commands: if cmd in self.env.commands:
return self.env.commands[cmd](self) return self.env.commands[cmd]
if cmd[0] in ('.', '/'): if cmd[0] in ('.', '/'):
path = self.fs.resolve_path(cmd, self.cwd) path = self.fs.resolve_path(cmd, self.cwd)
if not self.fs.exists(path): if not self.fs.exists(path):
@@ -58,53 +102,15 @@ class HoneyPotProtocol(recvline.HistoricRecvLine):
path = i path = i
break break
if path in self.env.commands: if path in self.env.commands:
return self.env.commands[path](self) return self.env.commands[path]
return None return None
def lineReceived(self, line): def lineReceived(self, line):
line = line.strip() if len(self.cmdstack):
if line: self.cmdstack[-1].lineReceived(line)
# Hack to allow password prompts, etc
if self.next_callback:
print 'INPUT: %s' % line
cmd = self.next_callback
args = None
if type(()) == type(cmd):
cmd, args = cmd
obj = cmd(self)
try:
if args:
obj.call(line, args)
else:
obj.call(line)
self.next_callback = obj.callback
del obj
except Exception, e:
print e
self.writeln("Segmentation fault")
else:
print 'CMD: %s' % line
cmdAndArgs = line.split(' ', 1)
cmd = cmdAndArgs[0]
args = ''
if len(cmdAndArgs) > 1:
args = cmdAndArgs[1]
obj = self.getCommand(cmd, args)
if obj:
try:
obj.call(args)
self.next_callback = obj.callback
del obj
except Exception, e:
print e
self.writeln("Segmentation fault")
else:
self.writeln('bash: %s: command not found' % cmd)
if not self.next_callback:
self.showPrompt()
def keystrokeReceived(self, keyID, modifier): def keystrokeReceived(self, keyID, modifier):
if type(keyID) == type(''):
ttylog.ttylog_write(self.terminal.ttylog_file, len(keyID), ttylog.ttylog_write(self.terminal.ttylog_file, len(keyID),
ttylog.DIR_READ, time.time(), keyID) ttylog.DIR_READ, time.time(), keyID)
recvline.HistoricRecvLine.keystrokeReceived(self, keyID, modifier) recvline.HistoricRecvLine.keystrokeReceived(self, keyID, modifier)
@@ -123,14 +129,6 @@ class HoneyPotProtocol(recvline.HistoricRecvLine):
self.terminal.write(data) self.terminal.write(data)
self.terminal.nextLine() self.terminal.nextLine()
class HoneyPotCommand(object):
def __init__(self, honeypot):
self.honeypot = honeypot
self.callback = None
def call(self, *args):
self.honeypot.writeln('Hello World!')
class LoggingServerProtocol(insults.ServerProtocol): class LoggingServerProtocol(insults.ServerProtocol):
def connectionMade(self): def connectionMade(self):
self.ttylog_file = './log/tty/%s-%s.log' % \ self.ttylog_file = './log/tty/%s-%s.log' % \