【问题标题】:Overwriting the stored return address on the stack doesn't work覆盖堆栈上存储的返回地址不起作用
【发布时间】:2021-12-02 22:58:37
【问题描述】:

我试图通过覆盖堆栈上存储的返回地址 (saved eip) 来控制 C 程序的执行:

(gdb) info frame
Stack level 0, frame at 0xbffff550:
 eip = 0x8048831 in main (6.c:52); saved eip = 0xbffffdef
 source language c.
 Arglist at 0xbffff538, args: argc=6, argv=0xbffff5e4
 Locals at 0xbffff538, Previous frame's sp is 0xbffff550
 Saved registers:
  ebx at 0xbffff534, ebp at 0xbffff538, eip at 0xbffff54c

0xbffffdef 的值是 "Hello, world!" shellcode 的地址,它已被组装、检查任何空字节并放入环境变量 SHELLCODE

(gdb) x/s *((char **)environ + 7)
0xbffffdef: "SHELLCODE=\353\023Y1\300\260\004\061\333C1Ҳ\017̀\260\001K̀\350\350\377\377\377Hello, world!\n\r"

很遗憾,程序未能打印出预期的问候语:

Program received signal SIGSEGV, Segmentation fault.
0xbffffdfd in ?? ()

为什么会崩溃以及如何解决问题?

注意事项:

gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
// program was compiled with the following flags:
-m32 -fno-stack-protector -z execstack -fno-PIE -no-pie -g

程序来源:link

【问题讨论】:

  • 请显示程序的源代码。反汇编 shellcode 也会有所帮助。
  • 等一下,你的shellcode在0xbffffdef,但你要跳转到0xffffddef?错字?此外,0xbffffdef 的内容是您不想执行的字母 SHELLCODE= 的字节,因此添加 11 以结束 0xbffffdfa
  • @Nate Eldredge 非常感谢,跳过SHELLCODE(但添加 10,而不是 11)有帮助!我应该发布完整的答案吗?
  • 当然,继续。 (是的,你没看错,10,我数错了。)

标签: c gdb shellcode


【解决方案1】:

感谢@Nate Eldredge

此特定崩溃背后的原因是不正确的返回地址0xbffffdef。环境变量SHELLCODE的位置其实指向了字符串"SHELLCODE=..."的开头

(gdb) x/s *((char **)environ + 7)
0xbffffdef: "SHELLCODE=\353\023Y1\300\260\004\061\333C1Ҳ\017̀\260\001K̀\350\350\377\377\377Hello, world!\n\r"

为了解决这个问题,我们必须通过跳过前 10 个字符来调整地址并指向"\353\023Y1\300..."

0xbffffdef + 0xa = 0xbffffdf9

当用这个值0xbffffdf9覆盖存储的返回地址时,程序被强制问候世界:

(gdb) info frame
Stack level 0, frame at 0xbffff550:
 eip = 0x8048831 in main (6.c:52); saved eip = 0xbffffdf9
 source language c.
 Arglist at 0xbffff538, args: argc=6, argv=0xbffff5e4
 Locals at 0xbffff538, Previous frame's sp is 0xbffff550
 Saved registers:
  ebx at 0xbffff534, ebp at 0xbffff538, eip at 0xbffff54c
(gdb) cont
Continuing.
Hello, world!
[Inferior 1 (process 28591) exited normally]

【讨论】:

    猜你喜欢
    • 2014-04-21
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 2017-08-21
    • 2010-12-14
    • 1970-01-01
    相关资源
    最近更新 更多