Search code examples
pythonpython-3.xctypesshellcode

python 'decode' has no attribute shellcode


I am trying to convert this script from python2 to python3.

I am running into a problem with this line:

# need to code the input into the right format through string escape
shellcode = shellcode.decode("string_escape")

This 'shellcode' gets converted into a bytearray in the next line(or should):

shellcode = bytearray(shellcode)

However, I run into the error of:

AttributeError: 'str' object has no attribute 'decode'.

"shellcode" equals:

shellcode = sys.argv[1]

shellcode should be equal to something like "\x31\xd2\xb2\x30\" and the rest of the script executes the shellcode.

Thanks(this script runs perfect in python2.7).


Solution

  • In python3 sys.argv[1] would be a str (unicode, in python2) so there's nothing to decode.

    A simple test - arg_type.py:

    import sys
    shellcode = sys.argv[1]
    print(type(shellcode))
    

    Run in python3 and python 2

    $ python3.4 arg_type.py 'é'
    <class 'str'>
    
    $ python2.7 arg_type.py 'é'
    <type 'str'>
    

    Note that in the python2.7 str is really a bytearray, which you'd need to decode into unicode.

    This is one of the ways in which python3 is so much more sensible by default.