From 07207612d62cf66374e88da883089b450267a60b Mon Sep 17 00:00:00 2001 From: Michel Oosterhof Date: Wed, 17 May 2017 16:57:56 +0400 Subject: [PATCH] initial version of dd command --- cowrie/commands/__init__.py | 1 + cowrie/commands/dd.py | 75 +++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 cowrie/commands/dd.py diff --git a/cowrie/commands/__init__.py b/cowrie/commands/__init__.py index 9f0f1a01..a85596f1 100644 --- a/cowrie/commands/__init__.py +++ b/cowrie/commands/__init__.py @@ -7,6 +7,7 @@ __all__ = [ 'base', 'busybox', 'curl', + 'dd', 'env', 'ethtool', 'free', diff --git a/cowrie/commands/dd.py b/cowrie/commands/dd.py new file mode 100644 index 00000000..490f75ed --- /dev/null +++ b/cowrie/commands/dd.py @@ -0,0 +1,75 @@ +# Copyright (c) 2016 Michel Oosterhof +# See the COPYRIGHT file for more information + +""" +dd commands +""" + +import getopt +import copy +from os import path +from cowrie.core.honeypot import HoneyPotCommand +from cowrie.core.fs import * + +commands = {} + +class command_dd(HoneyPotCommand): + """ + dd command + """ + + ddargs = {} + + def start(self): + if not self.args or self.args[0] == '>': + return + + for arg in self.args: + if not arg.index('='): + self.write('unknown operand: {}'.format(arg)) + HoneyPotCommand.exit() + operand, value = arg.split('=') + if operand not in ('if', 'bs', 'of'): + self.write('unknown operand: {}'.format(operand)) + self.exit(success=False) + self.ddargs[operand] = value + + if self.input_data: + self.write(self.input_data) + else: + for arg in self.ddargs.keys(): + value = self.ddargs[arg] + if arg == 'if': + pname = self.fs.resolve_path(value, self.protocol.cwd) + if self.fs.isdir(pname): + self.errorWrite('dd: {}: Is a directory\n'.format(value)) + continue + try: + self.write(self.fs.file_contents(pname)) + except: + self.errorWrite('dd: {}: No such file or directory\n'.format(value)) + HoneyPotCommand.exit(self) + self.exit() + + + def exit(self, success=True): + if success == True: + self.write('0+0 records in\n') + self.write('0+0 records out\n') + self.write('0 bytes transferred in 0.695821 secs (0 bytes/sec)\n') + HoneyPotCommand.exit(self) + + + def lineReceived(self, line): + log.msg(eventid='cowrie.session.input', + realm='dd', + input=line, + format='INPUT (%(realm)s): %(input)s') + + + def handle_CTRL_D(self): + self.exit() + + +commands['/bin/dd'] = command_dd +