Repository: cowrie/cowrie

Issue:
free -m command results in stack trace and cowrie freezes
From within cowrie (docker current 7 Dec 2018), when the command "free -m" is run it results a stack trace errors shown below.  Cowrie freezes., and since many bots use free as info gathering, cowry may miss everything subsequent to this command.

Environment:
Ubuntu 16.04
Python 3.5.2
docker pull cowrie/cowrie
sudo iptables -t nat -A PREROUTING -p tcp --dport 22 -j REDIRECT --to-port 2222
docker run -it -p 2222:2222 -p 2223:2223 cowrie/cowrie

Console Error:
2018-12-07T04:53:52+0000 [SSHChannel session (0) on SSHService b'ssh-connection' on HoneyPotSSHTransport,1,172.17.0
.1] CMD: free -m
2018-12-07T04:53:52+0000 [SSHChannel session (0) on SSHService b'ssh-connection' on HoneyPotSSHTransport,1,172.17.0
.1] Command found: free -m
2018-12-07T04:53:52+0000 [SSHChannel session (0) on SSHService b'ssh-connection' on HoneyPotSSHTransport,1,172.17.0
.1] Unhandled Error
        Traceback (most recent call last):
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/context.py", line 122, in callWithCon
text
            return self.currentContext().callWithContext(ctx, func, *args, **kw)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/context.py", line 85, in callWithCont
ext
            return func(*args,**kw)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/ssh/service.py", line 45, in packetRec
eived
            return f(packet)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/ssh/connection.py", line 249, in ssh_C
HANNEL_DATA
            log.callWithLogger(channel, channel.dataReceived, data)
        --- <exception caught here> ---
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/log.py", line 103, in callWithLogger
            return callWithContext({"system": lp}, func, *args, **kw)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/log.py", line 86, in callWithContext
            return context.call({ILogContext: newCtx}, func, *args, **kw)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/context.py", line 122, in callWithCon
text
            return self.currentContext().callWithContext(ctx, func, *args, **kw)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/context.py", line 85, in callWithCont
ext
            return func(*args,**kw)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/ssh/session.py", line 112, in dataRece
ived
            self.client.transport.write(data)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/ssh/session.py", line 163, in write
            self.proto.dataReceived(data)
          File "/cowrie/cowrie-git/src/cowrie/insults/insults.py", line 104, in dataReceived
            insults.ServerProtocol.dataReceived(self, data)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/insults/insults.py", line 537, in data
Received
            self.terminalProtocol.keystrokeReceived(ch, None)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/recvline.py", line 225, in keystrokeRe
ceived
            m()
          File "/cowrie/cowrie-git/src/cowrie/shell/protocol.py", line 325, in handle_RETURN
            return recvline.RecvLine.handle_RETURN(self)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/recvline.py", line 292, in handle_RETU
RN
            self.lineReceived(line)
          File "/cowrie/cowrie-git/src/cowrie/shell/protocol.py", line 185, in lineReceived
            self.cmdstack[-1].lineReceived(line)
          File "/cowrie/cowrie-git/src/cowrie/shell/honeypot.py", line 106, in lineReceived
            self.runCommand()
          File "/cowrie/cowrie-git/src/cowrie/shell/honeypot.py", line 215, in runCommand
            self.protocol.call_command(pp, cmdclass, *cmd_array[0]['rargs'])
          File "/cowrie/cowrie-git/src/cowrie/shell/protocol.py", line 306, in call_command
            HoneyPotBaseProtocol.call_command(self, pp, cmd, *args)
          File "/cowrie/cowrie-git/src/cowrie/shell/protocol.py", line 194, in call_command
            obj.start()
          File "/cowrie/cowrie-git/src/cowrie/shell/command.py", line 128, in start
            self.call()
          File "/cowrie/cowrie-git/src/cowrie/commands/free.py", line 41, in call
            self.do_free(fmt='megabytes')
          File "/cowrie/cowrie-git/src/cowrie/commands/free.py", line 60, in do_free
            for key, value in raw_mem_stats.iteritems():
        builtins.AttributeError: 'dict' object has no attribute 'iteritems'



Discussion:

Resolving patch:
diff --git a/src/cowrie/commands/free.py b/src/cowrie/commands/free.py
--- a/src/cowrie/commands/free.py
+++ b/src/cowrie/commands/free.py
@@ -57,7 +57,7 @@ def do_free(self, fmt='kilobytes'):
 
         if fmt == 'megabytes':
             # Transform KB to MB
-            for key, value in raw_mem_stats.iteritems():
+            for key, value in raw_mem_stats.items():
                 raw_mem_stats[key] = int(value / 1000)
         elif fmt == 'human':
             magnitude = ["B", "M", "G", "T", "Z"]


Test patch: