I'm trying to convert a hexadecimal number,
like the stack address 0x7ffd6fa90940,
into its corresponding Byte representation b'\x40\x09\xa9\x6f\xfd\x7f\x00\x00'.
Just like how it is represented in gdb:
pwndbg> hexdump $rsp \32
+#### 0x7fffffffdc## 0 1 2 3 4 5 6 7 8 9 A B C D E F │ │
+0000 0x7fffffffdc30 e0 af 4b 00 15 00 00 00 [40 dc ff ff ff 7f 00 00] │..K.....│........│
+0010 0x7fffffffdc40 25 39 24 73 00 00 00 00 [50 dc ff ff ff 7f 00 00] │%9$s....│P.......│
I found three functions, but they do not convert the hex number as expected:
import pwnlib.util.packing
import binascii
addr = '0000' + '0x7ffd6fa90940'[2:]
addr = binascii.unhexlify(addr)
print("[DEBUG] addr: {}".format(addr))
# Prints big endian: b'\x00\x00\x7f\xfdo\xa9\t@'
# != b'\x7f\xfd\x6f\xa9\x09\x40'
addr = 0x7ffd6fa90940
addr = pwnlib.util.packing.p64(addr, endian='little')
print("[DEBUG] addr: {}".format(addr))
# Prints lit endian: b'@\t\xa9o\xfd\x7f\x00\x00'
# != b'\x7f\xfd\x6f\xa9\x09\x40'
addr = 0x7ffd6fa90940
addr = pwnlib.util.packing.pack(addr, word_size=64, endianness='little')
print("[DEBUG] addr: {}".format(addr))
# Prints lit endian: b'@\t\xa9o\xfd\x7f\x00\x00'
# != b'\x7f\xfd\x6f\xa9\x09\x40'
# Custom implementation:
addr = '0000' + '0x7ffd6fa90940'[2:]
addr = ''.join(reversed(['\\x'+addr[i:i+2] for i in range(0, len(addr), 2)]))
print("[DEBUG] addr: {}".format(addr))
# Prints lit endian notation as a string: \x40\x09\xa9\x6f\xfd\x7f\x00\x00
# But how to convert to actual Bytes?: b'\x40\x09\xa9\x6f\xfd\x7f\x00\x00'
#
addr = addr.encode('utf-8').replace(b'\\\\',b'\\')
print("[DEBUG] addr: {}".format(addr))
# Results in: b'\\x40\\x09\\xa9\\x6f\\xfd\\x7f\\x00\\x00'
Why is that and how can it be converted as expected?
Thanks in advance for any hints, links, and answers!
\x00representation but it is the same value.structmodule was created just for tasks like this.