# Buggy code
def call(self):
    """ """
    escape_fn = lambda s: s
    newline = True

    try:
        optlist, args = getopt.getopt(self.args, "eEn")
        for opt in optlist:
            if opt[0] == "-e":
                escape_fn = functools.partial(str.decode, encoding="string_escape")
            elif opt[0] == "-E":
                escape_fn = lambda s: s
            elif opt[0] == "-n":
                newline = False
    except:
        args = self.args

    self.write(escape_fn(" ".join(args)))
    if newline is True:
        self.write("\n")


# Traceback (exceptions.ValueError)
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/local/lib/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/local/lib/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/local/lib/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/local/lib/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
why = selectable.doRead()
File "/usr/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 209, in doRead
return self._dataReceived(data)
File "/usr/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 215, in _dataReceived
rval = self.protocol.dataReceived(data)
File "/usr/local/lib/python2.7/site-packages/twisted/conch/telnet.py", line 589, in dataReceived
self.applicationDataReceived(''.join(appDataBuffer))
File "/usr/local/lib/python2.7/site-packages/twisted/conch/telnet.py", line 898, in applicationDataReceived
self.protocol.dataReceived(bytes)
File "/usr/local/lib/python2.7/site-packages/twisted/conch/telnet.py", line 940, in dataReceived
self.protocol.dataReceived(data)
File "/home/cowrie/cowrie/cowrie/insults/insults.py", line 110, in dataReceived
insults.ServerProtocol.dataReceived(self, data)
File "/usr/local/lib/python2.7/site-packages/twisted/conch/insults/insults.py", line 435, in dataReceived
self.terminalProtocol.keystrokeReceived(ch, None)
File "/usr/local/lib/python2.7/site-packages/twisted/conch/recvline.py", line 201, in keystrokeReceived
m()
File "/home/cowrie/cowrie/cowrie/core/protocol.py", line 353, in handle_RETURN
return recvline.RecvLine.handle_RETURN(self)
File "/usr/local/lib/python2.7/site-packages/twisted/conch/recvline.py", line 259, in handle_RETURN
self.lineReceived(line)
File "/home/cowrie/cowrie/cowrie/core/protocol.py", line 182, in lineReceived
self.cmdstack[-1].lineReceived(line)
File "/home/cowrie/cowrie/cowrie/core/honeypot.py", line 213, in lineReceived
self.runCommand()
File "/home/cowrie/cowrie/cowrie/core/honeypot.py", line 320, in runCommand
self.protocol.call_command(pp, cmdclass, *cmd_array[0]['rargs'])
File "/home/cowrie/cowrie/cowrie/core/protocol.py", line 330, in call_command
why = selectable.doRead()
File "/usr/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 209, in doRead
return self._dataReceived(data)
File "/usr/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 215, in _dataReceived
rval = self.protocol.dataReceived(data)
File "/usr/local/lib/python2.7/site-packages/twisted/conch/telnet.py", line 589, in dataReceived
self.applicationDataReceived(''.join(appDataBuffer))
File "/usr/local/lib/python2.7/site-packages/twisted/conch/telnet.py", line 898, in applicationDataReceived
self.protocol.dataReceived(bytes)
File "/usr/local/lib/python2.7/site-packages/twisted/conch/telnet.py", line 940, in dataReceived
self.protocol.dataReceived(data)
File "/home/cowrie/cowrie/cowrie/insults/insults.py", line 110, in dataReceived
insults.ServerProtocol.dataReceived(self, data)
File "/usr/local/lib/python2.7/site-packages/twisted/conch/insults/insults.py", line 435, in dataReceived
self.terminalProtocol.keystrokeReceived(ch, None)
File "/usr/local/lib/python2.7/site-packages/twisted/conch/recvline.py", line 201, in keystrokeReceived
m()
File "/home/cowrie/cowrie/cowrie/core/protocol.py", line 353, in handle_RETURN
return recvline.RecvLine.handle_RETURN(self)
File "/usr/local/lib/python2.7/site-packages/twisted/conch/recvline.py", line 259, in handle_RETURN
self.lineReceived(line)
File "/home/cowrie/cowrie/cowrie/core/protocol.py", line 182, in lineReceived
self.cmdstack[-1].lineReceived(line)
File "/home/cowrie/cowrie/cowrie/core/honeypot.py", line 213, in lineReceived
self.runCommand()
File "/home/cowrie/cowrie/cowrie/core/honeypot.py", line 320, in runCommand
self.protocol.call_command(pp, cmdclass, *cmd_array[0]['rargs'])
File "/home/cowrie/cowrie/cowrie/core/protocol.py", line 330, in call_command
HoneyPotBaseProtocol.call_command(self, pp, cmd, *args)
File "/home/cowrie/cowrie/cowrie/core/protocol.py", line 192, in call_command
obj.start()
File "/home/cowrie/cowrie/cowrie/core/honeypot.py", line 84, in start
self.call()
File "/home/cowrie/cowrie/cowrie/commands/base.py", line 139, in call
self.write(escape_fn(' '.join(args)))
exceptions.ValueError: invalid \x escape

# Fixed code
def call(self):
    """ """
    escape_fn = lambda s: s
    newline = True

    try:
        optlist, args = getopt.getopt(self.args, "eEn")
        for opt in optlist:
            if opt[0] == "-e":
                escape_fn = functools.partial(str.decode, encoding="string_escape")
            elif opt[0] == "-E":
                escape_fn = lambda s: s
            elif opt[0] == "-n":
                newline = False
    except:
        args = self.args

    # FIXME: Wrap in exception, Python escape cannot handle single digit \x codes (e.g. \x1)
    try:
        self.write(escape_fn(" ".join(args)))
    except exceptions.ValueError as e:
        log.msg("echo command received Python incorrect hex escape")

    if newline is True:
        self.write("\n")