pwntools' p32 function is weird

12.5k Views Asked by At

I'm testing on Intel x86_64, Ubuntu 64bit, Python3, Pwntools v4.3.1

$ python
Python 3.7.4 (default, Aug 13 2019, 20:35:49) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pwn import *
>>> addr = 0xbffffb78
>>> print(p32(addr))
b'x\xfb\xff\xbf'

In my opinion, the correct packing result for 0xbffffb78 should be\x78\xfb\xff\xbf.

But why did b'x\xfb\xff\xbf' happen? where is \x78 ?

And what is the correct way of packing, not using p32()?

1

There are 1 best solutions below

2
On

This is just how Python renders bytes objects. If a byte can be rendered as an ASCII character, it is displayed as one.

>>> b"\x78"
b'x'

To see the bytes rendered as hex you can use the hex method of the bytes object:

>>> b'x\xfb\xff\xbf'.hex()
'78fbffbf'