【问题标题】:Python concatenate bytes to strPython将字节连接到str
【发布时间】:2020-09-30 14:41:21
【问题描述】:

是否可以将字节连接到 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
>>> 

根据上面的简单代码是不可能的。

我之所以问这个问题是因为我看到了一个将字节连接到 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))

您可以在此处查看完整代码。

http://sh3llc0d3r.com/vulnserver-trun-command-buffer-overflow-exploit/

【问题讨论】:

  • 请注意,第二个代码只使用字符串...
  • 正如错误消息所暗示的那样,这是不可能的。您所展示的不是将字节连接到 str,而是连接两个 str。检查type("\xdb\xd1\xd9\x74\x24\xf4\x5a\x2b\xc9\xbd\x0e\x55\xbd")
  • @juanpa.arrivillaga 他看到的代码完全有可能是为 Python 2.7 编写的,其中bytes 只是str 的同义词。也就是说,他发布的代码 sn-p 中没有字节。
  • Gosh .. b'' 字符串,其中 str 在 python2 中输入。 Op 的链接,其中他显示的代码来自 2015 年,很可能打算在 python2 上运行,这非常好..

标签: python shellcode


【解决方案1】:

要么将字符串编码为字节以得到以字节为单位的结果:

print(b'byte' + 'string'.encode())
# b'bytestring'

或者将字节解码成字符串,得到str的结果:

print(b'byte'.decode() + 'string')
# bytestring

【讨论】:

    【解决方案2】:

    第二个代码 sn-p 显示正在连接的字符串。您需要将字节转换为字符串(如问题Convert bytes to a string 所示)。试试这个:b.decode("utf-8") + s。它应该为您提供所需的输出。

    【讨论】:

      猜你喜欢
      • 2021-06-22
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 2014-03-21
      • 2021-11-07
      • 1970-01-01
      相关资源
      最近更新 更多