Request the window title with terminal control codes, and parse the result.

The title, as well as screen height & width are now saved to sql->sessions



git-svn-id: https://kippo.googlecode.com/svn/trunk@144 951d7100-d841-11de-b865-b3884708a8e2
This commit is contained in:
desaster
2010-06-28 13:17:46 +00:00
parent 3779ba2471
commit 00664d251a
5 changed files with 81 additions and 2 deletions

View File

@@ -31,6 +31,8 @@ CREATE TABLE IF NOT EXISTS `sessions` (
`endtime` datetime default NULL,
`sensor` int(4) NOT NULL,
`ip` varchar(15) NOT NULL default '',
`termsize` varchar(7) default NULL,
`termtitle` varchar(255) default NULL,
PRIMARY KEY (`id`),
KEY `starttime` (`starttime`,`sensor`)
) ;

2
doc/sql/update3.sql Normal file
View File

@@ -0,0 +1,2 @@
ALTER TABLE `sessions` ADD `termsize` VARCHAR( 7 ) NULL DEFAULT NULL ,
ADD `termtitle` VARCHAR( 255 ) NULL DEFAULT NULL ;

View File

@@ -27,6 +27,10 @@ class DBLogger(object):
self.handleUnknownCommand),
('^INPUT \((?P<realm>[a-zA-Z0-9]+)\): (?P<input>.*)$',
self.handleInput),
('^Terminal size: (?P<height>[0-9]+) (?P<width>[0-9]+)$',
self.handleTerminalSize),
('^Terminal title: (?P<title>.*)$',
self.handleTerminalTitle),
)]
self.start(cfg)
@@ -113,4 +117,12 @@ class DBLogger(object):
def handleInput(self, session, args):
pass
# args has: width, height
def handleTerminalSize(self, session, args):
pass
# args has: title
def handleTerminalTitle(self, session, args):
pass
# vim: set sw=4 et:

View File

@@ -146,6 +146,7 @@ class HoneyPotProtocol(recvline.HistoricRecvLine):
def connectionMade(self):
recvline.HistoricRecvLine.connectionMade(self)
self.terminal.write('\x1b[21t', noLog = True) # terminal title
self.cmdstack = [HoneyPotShell(self)]
# You are in a maze of twisty little passages, all alike
@@ -273,10 +274,11 @@ class LoggingServerProtocol(insults.ServerProtocol):
print 'Opening TTY log: %s' % self.ttylog_file
ttylog.ttylog_open(self.ttylog_file, time.time())
self.ttylog_open = True
self.terminal_title = None
insults.ServerProtocol.connectionMade(self)
def write(self, bytes):
if self.ttylog_open:
def write(self, bytes, noLog = False):
if self.ttylog_open and not noLog:
ttylog.ttylog_write(self.ttylog_file, len(bytes),
ttylog.DIR_WRITE, time.time(), bytes)
insults.ServerProtocol.write(self, bytes)
@@ -287,6 +289,56 @@ class LoggingServerProtocol(insults.ServerProtocol):
self.ttylog_open = False
insults.ServerProtocol.connectionLost(self, reason)
# extended from the standard to read \x1b]lXXXX\x1b\\
def dataReceived(self, data):
for ch in data:
if self.state == 'data':
if ch == '\x1b':
self.state = 'escaped'
else:
self.terminalProtocol.keystrokeReceived(ch, None)
elif self.state == 'escaped':
if ch == '[':
self.state = 'bracket-escaped'
self.escBuf = []
elif ch == 'O':
self.state = 'low-function-escaped'
elif ch == ']':
self.state = 'reverse-bracket-escaped'
else:
self.state = 'data'
self._handleShortControlSequence(ch)
elif self.state == 'bracket-escaped':
if ch == 'O':
self.state = 'low-function-escaped'
elif ch.isalpha() or ch == '~':
self._handleControlSequence(''.join(self.escBuf) + ch)
del self.escBuf
self.state = 'data'
else:
self.escBuf.append(ch)
elif self.state == 'low-function-escaped':
self._handleLowFunctionControlSequence(ch)
self.state = 'data'
elif self.state == 'reverse-bracket-escaped':
if ch == 'l':
self.titleBuf = []
self.state = 'title-escaped'
self.title_escaped = False
elif self.state == 'title-escaped':
if ch == '\x1b':
self.title_escaped = True
elif self.title_escaped and ch == '\\':
self.terminal_title = ''.join(self.titleBuf)
print 'Terminal title: %s' % (self.terminal_title,)
self.state = 'data'
del self.titleBuf
else:
self.titleBuf.append(ch)
else:
raise ValueError("Illegal state")
# insults.ServerProtocol.dataReceived(self, data)
class HoneyPotAvatar(avatar.ConchUser):
implements(conchinterfaces.ISession)
@@ -302,6 +354,7 @@ class HoneyPotAvatar(avatar.ConchUser):
protocol.makeConnection(session.wrapProtocol(serverProtocol))
def getPty(self, terminal, windowSize, attrs):
print 'Terminal size: %s %s' % windowSize[0:2]
self.windowSize = windowSize
return None

View File

@@ -79,4 +79,14 @@ class DBLogger(dblog.DBLogger):
' VALUES (%s, FROM_UNIXTIME(%s), %s, %s)',
(session, self.nowUnix(), args['realm'], args['input']))
def handleTerminalSize(self, session, args):
self.query('UPDATE `sessions` SET `termsize` = %s' + \
' WHERE `id` = %s',
('%sx%s' % (args['width'], args['height']), session))
def handleTerminalTitle(self, session, args):
self.query('UPDATE `sessions` SET `termtitle` = %s' + \
' WHERE `id` = %s',
(args['title'], session))
# vim: set sw=4 et: