Difference between Python3 and Python2 - socket.send data

前端 未结 2 668
忘掉有多难
忘掉有多难 2020-12-20 23:18

I\'m practicing some buffer-overflow techniques and I came across an odd issue with sending socked data.

I have this two almost identical codes, except the fact that

相关标签:
2条回答
  • 2020-12-20 23:29

    You can use six.b to make it compatible with both python2 and python3:

    import six
    
    ...
    sock.send(six.b(buffer))
    

    https://six.readthedocs.io/#six.b

    0 讨论(0)
  • 2020-12-20 23:38

    Your server and debugger are right - the buffer content is not the same.

    In both python 2 and 3, if you write buffer = "A"*268, the type of buffer is str. However, what str represents is completely different in the two versions.

    In python 2, a str is effectively an array of bytes. In python 3, it's a sequence of human readable characters, not bytes (what is called a "unicode string" in python 2)

    If you further .encode(), you'll translate the sequence of characters into a sequence of bytes, using utf-8. This "changes the content" of your string, so to speak

    What you probably wanted to do is buffer = b"A"*268, which will use bytes instead of str. You'll need to prefix all concatenated byte sequences by b, too

    0 讨论(0)
提交回复
热议问题