Repository: cowrie/cowrie

Issue:
pinging a domain crashes
From within cowrie (docker current 7 Dec 2018), when the you ping a domain (not an IP) a stack trace errors shown below.  Cowrie freezes., and since many bots use ping as info gathering/sandbox detection, 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


```
2018-12-07T04:56:59+0000 [SSHChannel session (0) on SSHService b'ssh-connection' on HoneyPotSSHTransport,2,172.17.0.1] gett
ing shell
2018-12-07T04:57:07+0000 [SSHChannel session (0) on SSHService b'ssh-connection' on HoneyPotSSHTransport,2,172.17.0.1] CMD:
 **ping google.com**
2018-12-07T04:57:07+0000 [SSHChannel session (0) on SSHService b'ssh-connection' on HoneyPotSSHTransport,2,172.17.0.1] Comm
and found: ping google.com
2018-12-07T04:57:07+0000 [SSHChannel session (0) on SSHService b'ssh-connection' on HoneyPotSSHTransport,2,172.17.0.1] Unha
ndled Error
        Traceback (most recent call last):
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/context.py", line 122, in callWithContext
            return self.currentContext().callWithContext(ctx, func, *args, **kw)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/context.py", line 85, in callWithContext
            return func(*args,**kw)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/ssh/service.py", line 45, in packetReceived
            return f(packet)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/ssh/connection.py", line 249, in ssh_CHANNEL_D
ATA
            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 callWithContext
            return self.currentContext().callWithContext(ctx, func, *args, **kw)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/context.py", line 85, in callWithContext
            return func(*args,**kw)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/ssh/session.py", line 112, in dataReceived
            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 dataReceived
            self.terminalProtocol.keystrokeReceived(ch, None)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/recvline.py", line 225, in keystrokeReceived
            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_RETURN
            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/commands/ping.py", line 70, in start
            s = hashlib.md5(self.host).hexdigest()
        builtins.TypeError: Unicode-objects must be encoded before hashing
```



Discussion:
It seems to be a unicode issue on Py3.

Resolving patch:
diff --git a/src/cowrie/commands/ping.py b/src/cowrie/commands/ping.py
--- a/src/cowrie/commands/ping.py
+++ b/src/cowrie/commands/ping.py
@@ -67,7 +67,7 @@ def start(self):
                 self.write('ping: unknown host %s\n' % (self.host,))
                 self.exit()
         else:
-            s = hashlib.md5(self.host).hexdigest()
+            s = hashlib.md5((self.host).encode("utf-8")).hexdigest()
             self.ip = '.'.join([str(int(x, 16)) for x in (s[0:2], s[2:4], s[4:6], s[6:8])])
 
         self.running = True


Test patch: