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
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
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