From 0fd1dcd8908fb0841119be31dba4be27c206ba0c Mon Sep 17 00:00:00 2001 From: Michel Oosterhof Date: Thu, 12 Sep 2019 22:36:52 +0400 Subject: [PATCH] allow more --- src/cowrie/commands/dd.py | 40 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/cowrie/commands/dd.py b/src/cowrie/commands/dd.py index ce148e44..43a501e5 100644 --- a/src/cowrie/commands/dd.py +++ b/src/cowrie/commands/dd.py @@ -7,6 +7,8 @@ dd commands from __future__ import absolute_import, division +import re + from twisted.python import log from cowrie.shell.command import HoneyPotCommand @@ -51,7 +53,7 @@ class command_dd(HoneyPotCommand): if bSuccess: if 'bs' in self.ddargs: - block = int(self.ddargs['bs']) + block = parse_size(self.ddargs['bs']) if block <= 0: self.errorWrite('dd: invalid number \'{}\'\n'.format(block)) bSuccess = False @@ -98,5 +100,41 @@ class command_dd(HoneyPotCommand): self.exit() +def parse_size(param): + """ + Parse dd arguments that indicate block sizes + Return 0 in case of illegal input + """ + pattern = r'^(\d+)(c|w|b|kB|K|MB|M|xM|GB|G|T|TB|P|PB|E|EB|Z|ZB|Y|YB)$' + z = re.search(pattern, param) + if not z: + return 0 + digits = int(z.group(0)) + letters = z.group(1) + + if letters == 'c': + multiplier = 1 + elif letters == 'w': + multiplier = 2 + elif letters == 'b': + multiplier = 512 + elif letters == 'kB': + multiplier = 1000 + elif letters == 'K': + multiplier = 1024 + elif letters == 'MB': + multiplier = 1000*1000 + elif letters == 'M' or letters == 'xM': + multiplier = 1024*1024 + elif letters == 'GB': + multiplier = 1000*1000*1000 + elif letters == 'G': + multiplier = 1024*1024*1024 + else: + multiplier = 1 + + return digits * multiplier + + commands['/bin/dd'] = command_dd commands['dd'] = command_dd