From 798b6b6a6562e2174e1b22279526be2a34a5d5de Mon Sep 17 00:00:00 2001 From: Michael Bailey Date: Tue, 15 Aug 2017 13:37:21 -0700 Subject: [PATCH] Banner generation (and date/time evaluation) upon use --- fakenet/listeners/BannerFactory.py | 41 +++++++++++++++++++++++++++--- fakenet/listeners/FTPListener.py | 4 +-- fakenet/listeners/IRCListener.py | 7 ++--- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/fakenet/listeners/BannerFactory.py b/fakenet/listeners/BannerFactory.py index 279cc26..34d6f7b 100644 --- a/fakenet/listeners/BannerFactory.py +++ b/fakenet/listeners/BannerFactory.py @@ -3,6 +3,38 @@ import string import datetime +class Banner(): + """Act like a string, but actually get date/time components on the fly.""" + + def __init__(self, banner, insertions): + self.banner = banner + self.insertions = insertions + # Indicate an error in the banner early-on as opposed to + # when a login or other event occurs. + test = self.failEarly() + + def failEarly(self): + """Raise exceptions upon construction rather than later.""" + return self.fmt() + + def __len__(self): + """Needed for pyftpdlib. + + If the length changes between the time when the caller obtains the + length and the time when they reference the string, then... *shrug*? + """ + return len(self.fmt()) + + def __repr__(self): + return self.fmt() + + def fmt(self): + banner = self.banner + banner = datetime.datetime.now().strftime(banner) + banner = banner % self.insertions + banner = banner.replace('\\n', '\n').replace('\\t', '\t') + return banner + class BannerFactory(): def genBanner(self, config, bannerdict, defaultbannerkey='!generic'): """Select and format a banner. @@ -55,11 +87,12 @@ def genBanner(self, config, bannerdict, defaultbannerkey='!generic'): insertions = {'servername': servername, 'tz': 'UTC'} - banner = datetime.datetime.now().strftime(banner) - banner = banner % insertions - banner = banner.replace('\\n', '\n').replace('\\t', '\t') + return Banner(banner, insertions) - return banner + # banner = datetime.datetime.now().strftime(banner) + # banner = banner % insertions + # banner = banner.replace('\\n', '\n').replace('\\t', '\t') + # return banner def randomizeHostname(self): valid_hostname_charset = (string.ascii_letters + string.digits + '-') diff --git a/fakenet/listeners/FTPListener.py b/fakenet/listeners/FTPListener.py index a177abc..334ec3c 100644 --- a/fakenet/listeners/FTPListener.py +++ b/fakenet/listeners/FTPListener.py @@ -255,7 +255,7 @@ def start(self): else: self.handler = FakeFTPHandler - self.handler.banner = self.banner() + self.handler.banner = self.genBanner() self.handler.ftproot_path = self.ftproot_path self.handler.abstracted_fs = FakeFS @@ -279,7 +279,7 @@ def stop(self): if self.server: self.server.close_all() - def banner(self): + def genBanner(self): bannerfactory = BannerFactory.BannerFactory() return bannerfactory.genBanner(self.config, BANNERS) diff --git a/fakenet/listeners/IRCListener.py b/fakenet/listeners/IRCListener.py index 96d2860..35b61d4 100644 --- a/fakenet/listeners/IRCListener.py +++ b/fakenet/listeners/IRCListener.py @@ -15,7 +15,7 @@ SRV_WELCOME = "Welcome to FakeNet." BANNERS = { - 'generic': 'Welcome to IRC - %%(servername)s - %a %b %d %H:%M:%S %%(tz)s %Y', + 'generic': 'Welcome to IRC - %(servername)s - %a %b %d %H:%M:%S %(tz)s %Y', 'debian-ircd-irc2': ( '17/10/2011 11:50\n' + ' [ Debian GNU/Linux ]\n' + @@ -49,6 +49,7 @@ def start(self): self.server = ThreadedTCPServer((self.local_ip, int(self.config['port'])), ThreadedTCPRequestHandler) + self.banner = self.genBanner() self.server.listener = self self.server.logger = self.logger self.server.config = self.config @@ -64,7 +65,7 @@ def stop(self): self.server.shutdown() self.server.server_close() - def banner(self): + def genBanner(self): bannerfactory = BannerFactory.BannerFactory() return bannerfactory.genBanner(self.config, BANNERS) @@ -117,7 +118,7 @@ def irc_NICK(self, cmd, params): self.nick = params - banner = self.server.listener.banner() + banner = self.server.listener.banner self.irc_send_server("001", "%s :%s" % (self.nick, banner)) self.irc_send_server("376", "%s :End of /MOTD command." % self.nick)