是否可以将字节连接到 str ?
>>> b = b'this is bytes'
>>> s = 'this is string'
>>> b + s
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't concat str to bytes
>>>
Run Code Online (Sandbox Code Playgroud)
基于上面的简单代码是不可能的。
我之所以问这个问题,是因为我看到了字节已连接到 str 的代码?这是代码的片段。
buf = ""
buf += "\xdb\xd1\xd9\x74\x24\xf4\x5a\x2b\xc9\xbd\x0e\x55\xbd"
buffer = "TRUN /.:/" + "A" * 2003 + "\xcd\x73\xa3\x77" + "\x90" * 16 + buf + "C" * (5060 - 2003 - 4 - 16 - len(buf))
Run Code Online (Sandbox Code Playgroud)
您可以在此处查看完整代码。
http://sh3llc0d3r.com/vulnserver-trun-command-buffer-overflow-exploit/
将字符串编码为字节以获得以字节为单位的结果:
print(b'byte' + 'string'.encode())
# b'bytestring'
Run Code Online (Sandbox Code Playgroud)
或者将字节解码为字符串以获得 str 形式的结果:
print(b'byte'.decode() + 'string')
# bytestring
Run Code Online (Sandbox Code Playgroud)