Matteo Dal Grande
Matteo Dal Grande

Reputation: 120

Python 3 - TypeError: can only concatenate str (not "bytes") to str

I have this wrong code and can't find the fix.

49         os.system('powershell -enc 
    '+base64.b64encode(addpermissions.encode('utf_16_le')))   
    . 
    .
    .
     137 HexRegHash, HexRegSysk, jd, skew1, gbg, data = getRegistryValues(HexRID)

I have this error:

    Traceback (most recent call last):
      File "hash.py", line 137, in <module>
        HexRegHash, HexRegSysk, jd, skew1, gbg, data = 
    getRegistryValues(HexRID)
      File "hash.py", line 49, in getRegistryValues
        os.system('powershell -enc 
    '+base64.b64encode(addpermissions.encode('utf_16_le')))
    TypeError: can only concatenate str (not "bytes") to str

Upvotes: 4

Views: 21440

Answers (1)

Saritus
Saritus

Reputation: 978

base64.b64encode produces a byte-stream, not a string. So for your concatenation to work, you have to convert it into a string first with str(base64.b64encode(addpermissions.encode('utf_16_le')))

b64cmd = base64.b64encode(cmd.encode('utf_16_le')).decode('utf-8')
os.system('powershell -enc ' + b64cmd)

EDIT: Normal string conversion didn't work with os.system, used decode('utf-8') instead

Upvotes: 8

Related Questions