From f388b769374dc1c68f7f00d9d0fbc52901ec3fdf Mon Sep 17 00:00:00 2001 From: Michel Oosterhof Date: Sun, 18 Sep 2016 14:51:02 +0000 Subject: [PATCH] Squashed commit of the following: commit 79fff5332ba74bab40d0059e1051ca62614488ff Merge: 645f25f 39c9fa4 Author: Michel Oosterhof Date: Sun Sep 18 14:46:09 2016 +0000 Merge branch 'configparser' of https://github.com/micheloosterhof/cowrie into configparser commit 645f25f6f22dbe374b544f08d69b7de2ef0dcb45 Author: Michel Oosterhof Date: Sun Sep 18 14:37:46 2016 +0000 rewrite code for prompt. this had issues with unicode commit 99adf95f27ef03b229e592f1fa393cf66be3faf0 Author: Michel Oosterhof Date: Sun Sep 18 14:15:10 2016 +0000 can't use "mode" commit bee2b6693fe009d361a0b117c67d11f37ed21c41 Author: Michel Oosterhof Date: Sun Sep 18 14:09:57 2016 +0000 configparser changes. not working well yet on py2 commit 39c9fa406cca9ede51af8329280af81fff870e31 Author: Michel Oosterhof Date: Sun Sep 18 14:37:46 2016 +0000 rewrite code for prompt. this had issues with unicode commit 3dd08206fcb4c7dd0a66d20b914e3279947c77de Author: Michel Oosterhof Date: Sun Sep 18 14:15:10 2016 +0000 can't use "mode" commit f54272b239409b65ef0292ac328b0a2efccbe6cd Author: Michel Oosterhof Date: Sun Sep 18 14:09:57 2016 +0000 configparser changes. not working well yet on py2 --- cowrie/core/config.py | 8 +++---- cowrie/core/honeypot.py | 40 ++++++++++++++------------------ cowrie/ssh/filetransfer.py | 6 ++--- twisted/plugins/cowrie_plugin.py | 6 ++--- 4 files changed, 28 insertions(+), 32 deletions(-) diff --git a/cowrie/core/config.py b/cowrie/core/config.py index 466e4c63..616c592b 100644 --- a/cowrie/core/config.py +++ b/cowrie/core/config.py @@ -5,10 +5,10 @@ This module contains ... """ -import ConfigParser +import configparser def readConfigFile(cfgfile): - cfg = ConfigParser.ConfigParser() - cfg.readfp(open(cfgfile)) - return cfg + config = configparser.ConfigParser() + config.read(cfgfile) + return config diff --git a/cowrie/core/honeypot.py b/cowrie/core/honeypot.py index a33085a0..ba799b5d 100644 --- a/cowrie/core/honeypot.py +++ b/cowrie/core/honeypot.py @@ -333,33 +333,29 @@ class HoneyPotShell(object): """ if not self.interactive: return - # Example: srv03:~# - #prompt = '%s:%%(path)s' % self.protocol.hostname - # Example: root@svr03:~# (More of a "Debianu" feel) - prompt = '%s@%s:%%(path)s' % (self.protocol.user.username, self.protocol.hostname) + + cwd = self.protocol.cwd + homelen = len(self.protocol.user.avatar.home) + if cwd == self.protocol.user.avatar.home: + cwd = '~' + elif len(cwd) > (homelen+1) and \ + cwd[:(homelen+1)] == self.protocol.user.avatar.home + '/': + cwd = '~' + cwd[homelen:] + # Uncomment the three lines below for a 'better' CentOS look. + # Rather than '[root@svr03 /var/log]#' is shows '[root@svr03 log]#'. + #cwd = cwd.rsplit('/', 1)[-1] + #if not cwd: + # cwd = '/' + # Example: [root@svr03 ~]# (More of a "CentOS" feel) - #prompt = '[%s@%s %%(path)s]' % (self.protocol.user.username, self.protocol.hostname,) + # Example: root@svr03:~# (More of a "Debian" feel) + prompt = '{}@{}:{}'.format(self.protocol.user.username, self.protocol.hostname, cwd) if not self.protocol.user.uid: prompt += '# ' # "Root" user else: prompt += '$ ' # "Non-Root" user - - path = self.protocol.cwd - homelen = len(self.protocol.user.avatar.home) - if path == self.protocol.user.avatar.home: - path = '~' - elif len(path) > (homelen+1) and \ - path[:(homelen+1)] == self.protocol.user.avatar.home + '/': - path = '~' + path[homelen:] - # Uncomment the three lines below for a 'better' CentOS look. - # Rather than '[root@svr03 /var/log]#' is shows '[root@svr03 log]#'. - #path = path.rsplit('/', 1)[-1] - #if not path: - # path = '/' - - attrs = {'path': path} - self.protocol.terminal.write(prompt % attrs) - self.protocol.ps = (prompt % attrs , '> ') + self.protocol.terminal.write(prompt) + self.protocol.ps = (prompt , '> ') def eofReceived(self): diff --git a/cowrie/ssh/filetransfer.py b/cowrie/ssh/filetransfer.py index 9d0b7775..5f81204a 100644 --- a/cowrie/ssh/filetransfer.py +++ b/cowrie/ssh/filetransfer.py @@ -53,11 +53,11 @@ class CowrieSFTPFile(object): if flags & FXF_EXCL == FXF_EXCL: openFlags |= os.O_EXCL if "permissions" in attrs: - mode = attrs["permissions"] + filemode = attrs["permissions"] del attrs["permissions"] else: - mode = 0777 - fd = sftpserver.fs.open(filename, openFlags, mode) + filemode = 0777 + fd = sftpserver.fs.open(filename, openFlags, filemode) if attrs: self.sftpserver.setAttrs(filename, attrs) self.fd = fd diff --git a/twisted/plugins/cowrie_plugin.py b/twisted/plugins/cowrie_plugin.py index 795fa4d1..eced9524 100644 --- a/twisted/plugins/cowrie_plugin.py +++ b/twisted/plugins/cowrie_plugin.py @@ -111,9 +111,9 @@ class CowrieServiceMaker(object): if options['port'] != 0: listen_ssh_port = int(options["port"]) elif cfg.has_option('ssh', 'listen_port'): - listen_ssh_port = int(cfg.get('ssh', 'listen_port')) + listen_ssh_port = cfg.getint('ssh', 'listen_port') elif cfg.has_option('honeypot', 'listen_port'): - listen_ssh_port = int(cfg.get('honeypot', 'listen_port')) + listen_ssh_port = cfg.getint('honeypot', 'listen_port') else: listen_ssh_port = 2222 @@ -133,7 +133,7 @@ class CowrieServiceMaker(object): # Preference: 1, config, 2, default of 2223 if cfg.has_option('telnet', 'listen_port'): - listen_telnet_port = int(cfg.get('telnet', 'listen_port')) + listen_telnet_port = cfg.getint('telnet', 'listen_port') else: listen_telnet_port = 2223