2010-06-08 17:37:16 +00:00
|
|
|
# Copyright (c) 2009 Upi Tamminen <desaster@gmail.com>
|
|
|
|
# See the COPYRIGHT file for more information
|
|
|
|
|
2010-06-17 09:09:37 +00:00
|
|
|
import re, time, socket
|
2010-06-08 17:37:16 +00:00
|
|
|
|
|
|
|
class DBLogger(object):
|
|
|
|
def __init__(self, cfg):
|
2010-06-16 14:51:26 +00:00
|
|
|
self.cfg = cfg
|
2010-06-08 17:37:16 +00:00
|
|
|
self.sessions = {}
|
|
|
|
self.ttylogs = {}
|
2010-06-16 14:51:26 +00:00
|
|
|
self.re_connected = re.compile(
|
|
|
|
'^New connection: ([0-9.]+):([0-9]+) \(([0-9.]+):([0-9]+)\) ' + \
|
|
|
|
'\[session: ([0-9]+)\]$')
|
|
|
|
self.re_sessionlog = re.compile('.*HoneyPotTransport,([0-9]+),[0-9.]+$')
|
2010-06-08 17:37:16 +00:00
|
|
|
self.re_map = [(re.compile(x[0]), x[1]) for x in (
|
|
|
|
('^connection lost$',
|
|
|
|
self._connectionLost),
|
|
|
|
('^login attempt \[(?P<username>.*)/(?P<password>.*)\] failed',
|
|
|
|
self.handleLoginFailed),
|
|
|
|
('^login attempt \[(?P<username>.*)/(?P<password>.*)\] succeeded',
|
|
|
|
self.handleLoginSucceeded),
|
|
|
|
('^Opening TTY log: (?P<logfile>.*)$',
|
|
|
|
self.handleTTYLogOpened),
|
|
|
|
('^Command found: (?P<input>.*)$',
|
|
|
|
self.handleCommand),
|
|
|
|
('^Command not found: (?P<input>.*)$',
|
|
|
|
self.handleUnknownCommand),
|
|
|
|
('^INPUT \((?P<realm>[a-zA-Z0-9]+)\): (?P<input>.*)$',
|
|
|
|
self.handleInput),
|
|
|
|
)]
|
2010-06-16 10:41:37 +00:00
|
|
|
self.start(cfg)
|
|
|
|
|
2010-06-08 17:37:16 +00:00
|
|
|
def start():
|
|
|
|
pass
|
|
|
|
|
2010-06-16 14:51:26 +00:00
|
|
|
def getSensor(self):
|
|
|
|
if self.cfg.has_option('honeypot', 'sensor_name'):
|
|
|
|
return self.cfg.get('honeypot', 'sensor_name')
|
|
|
|
return None
|
|
|
|
|
2010-06-08 17:37:16 +00:00
|
|
|
def nowUnix(self):
|
|
|
|
"""return the current UTC time as an UNIX timestamp"""
|
2010-06-17 09:09:37 +00:00
|
|
|
return int(time.mktime(time.gmtime()[:-1] + (-1,)))
|
2010-06-08 17:37:16 +00:00
|
|
|
|
|
|
|
def emit(self, ev):
|
2010-06-16 14:51:26 +00:00
|
|
|
if not len(ev['message']):
|
|
|
|
return
|
|
|
|
match = self.re_connected.match(ev['message'][0])
|
|
|
|
if match:
|
|
|
|
sessionid = int(match.groups()[4])
|
|
|
|
self.sessions[sessionid] = \
|
|
|
|
self.createSession(
|
|
|
|
match.groups()[0], int(match.groups()[1]),
|
|
|
|
match.groups()[2], int(match.groups()[3]))
|
2010-06-08 17:37:16 +00:00
|
|
|
return
|
2010-06-16 14:51:26 +00:00
|
|
|
match = self.re_sessionlog.match(ev['system'])
|
2010-06-09 09:23:05 +00:00
|
|
|
if not match:
|
|
|
|
return
|
2010-06-16 14:51:26 +00:00
|
|
|
sessionid = int(match.groups()[0])
|
|
|
|
if sessionid not in self.sessions.keys():
|
2010-06-16 10:41:37 +00:00
|
|
|
return
|
2010-06-08 17:37:16 +00:00
|
|
|
message = ev['message'][0]
|
|
|
|
for regex, func in self.re_map:
|
|
|
|
match = regex.match(message)
|
|
|
|
if match:
|
2010-06-16 14:51:26 +00:00
|
|
|
func(self.sessions[sessionid], match.groupdict())
|
2010-06-08 17:37:16 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
def _connectionLost(self, session, args):
|
|
|
|
self.handleConnectionLost(session, args)
|
|
|
|
if session in self.ttylogs:
|
|
|
|
del self.ttylogs[session]
|
|
|
|
for i in [x for x in self.sessions if self.sessions[x] == session]:
|
|
|
|
del self.sessions[i]
|
|
|
|
|
2010-06-11 04:59:52 +00:00
|
|
|
def ttylog(self, session):
|
|
|
|
ttylog = None
|
|
|
|
if session in self.ttylogs:
|
|
|
|
f = file(self.ttylogs[session])
|
|
|
|
ttylog = f.read(10485760)
|
|
|
|
f.close()
|
|
|
|
return ttylog
|
|
|
|
|
2010-06-08 17:37:16 +00:00
|
|
|
# We have to return an unique ID
|
2010-06-16 14:51:26 +00:00
|
|
|
def createSession(self, peerIP, peerPort, hostIP, hostPort):
|
2010-06-08 17:37:16 +00:00
|
|
|
return 0
|
|
|
|
|
|
|
|
# args has: logfile
|
|
|
|
def handleTTYLogOpened(self, session, args):
|
|
|
|
self.ttylogs[session] = args['logfile']
|
|
|
|
|
|
|
|
# args is empty
|
|
|
|
def handleConnectionLost(self, session, args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# args has: username, password
|
|
|
|
def handleLoginFailed(self, session, args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# args has: username, password
|
|
|
|
def handleLoginSucceeded(self, session, args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# args has: input
|
|
|
|
def handleCommand(self, session, args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# args has: input
|
|
|
|
def handleUnknownCommand(self, session, args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# args has: realm, input
|
|
|
|
def handleInput(self, session, args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# vim: set sw=4 et:
|