Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 655
Matteo Dal Grande Avatar asked Oct 19 '25 13:10

Matteo Dal Grande


1 Answers

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

like image 142
Saritus Avatar answered Oct 21 '25 03:10

Saritus