-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbgExt.cpp
More file actions
183 lines (152 loc) · 3.5 KB
/
Copy pathDbgExt.cpp
File metadata and controls
183 lines (152 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "framework.h"
// command !chunk
DECLARE_API(chunk)
{
if (!GetExpression(args))
{
dprintf("usage: !chunk <chunk header addr>\n");
return;
}
ULONG64 chunk_head = GetExpression(args);
ULONG64 heap_head = chunk_head & ~0xffff;
HEAP_ENTRY chunk = { 0 };
HEAP heap = { 0 };
ULONG bytesRead = 0;
if (!ReadMemory(heap_head, &heap, sizeof(HEAP), &bytesRead))
{
dprintf("[-] failed to read heap header\n");
return;
}
if (!ReadMemory(chunk_head, &chunk, sizeof(HEAP_ENTRY), &bytesRead))
{
dprintf("[-] failed to read chunk header\n");
return;
}
// get the xor cookie
ULONG64 cookie = *(((PULONG64)&heap.Encoding) + 1);
dprintf("[+] cookie: 0x%I64x\n", cookie);
dprintf("[+] chunk: 0x%I64x\n", chunk_head);
// decode the chunk header
*(((PULONG64)&chunk) + 1) ^= cookie;
dprintf(" [>] PreviousBlockPrivateData: 0x%I64x ", chunk.PreviousBlockPrivateData);
CHAR Data[8] = { 0 };
memcpy(Data, &chunk.PreviousBlockPrivateData, 8);
for (auto item : Data)
{
if (item < 0x10)
{
dprintf("0%x ", item);
}
else
{
dprintf("%x ", item);
}
}
dprintf(" ");
for (auto item : Data)
{
if (item >= 0x20 && item <= 0x7e)
{
dprintf("%c", item);
}
else
{
dprintf(".");
}
}
dprintf("\n [>] Size: 0x%x(%u)\n", chunk.Size << 4, chunk.Size << 4);
if (chunk.Flags & 0x1)
{
dprintf(" [>] Flags: 0x%x(busy)\n", chunk.Flags);
}
else
{
dprintf(" [>] Flags: 0x%x(free)\n", chunk.Flags);
}
dprintf(" [>] PreviousSize: 0x%x(%u)\n", chunk.PreviousSize << 4, chunk.PreviousSize << 4);
}
// command !freelist
DECLARE_API(freelist)
{
if (!GetExpression(args))
{
dprintf("usage: !freelist <heap header addr>\n");
return;
}
CONST ULONG64 list_offset = 0x150;
ULONG64 list_header = GetExpression(args) + list_offset;
dprintf("[+] list entry head: 0x%I64x\n", list_header);
LIST_ENTRY64 first_list_entry = { 0 };
ULONG bytesRead = 0;
// first time get the flink and blink
if (!ReadMemory(list_header, &first_list_entry, sizeof(LIST_ENTRY64), &bytesRead))
{
dprintf("[-] failed to read list entry\n");
return;
}
dprintf(" [>] Flink: 0x%I64x ", first_list_entry.Flink - 0x10);
LIST_ENTRY64 temp_list_entry = { 0 };
ULONG64 flink = first_list_entry.Flink;
int count = 2;
BOOL flag = FALSE;
while (ReadMemory(flink, &temp_list_entry, sizeof(LIST_ENTRY64), &bytesRead))
{
if (temp_list_entry.Flink == first_list_entry.Flink)
{
break;
}
dprintf("-> 0x%I64x ", temp_list_entry.Flink - 0x10);
flink = temp_list_entry.Flink;
// beautify output
if (count % 6 == 0)
{
dprintf("\n ");
flag = TRUE;
}
else
{
flag = FALSE;
}
++count;
}
if (!flag)
{
dprintf("\n");
}
dprintf("\n [>] Blink: 0x%I64x ", first_list_entry.Blink - 0x10);
temp_list_entry = { 0 };
ULONG64 blink = first_list_entry.Blink;
count = 2;
flag = FALSE;
while (ReadMemory(blink, &temp_list_entry, sizeof(LIST_ENTRY64), &bytesRead))
{
if (temp_list_entry.Blink == first_list_entry.Blink)
{
break;
}
dprintf("-> 0x%I64x ", temp_list_entry.Blink - 0x10);
blink = temp_list_entry.Blink;
if (count % 6 == 0)
{
dprintf("\n ");
flag = TRUE;
}
else
{
flag = FALSE;
}
++count;
}
if (!flag)
{
dprintf("\n");
}
}
// command !heaphelp
DECLARE_API(heaphelp)
{
dprintf("[+] help for the windbgdll.dll\n");
dprintf(" [>] !chunk <addr> - show the chunk header info\n");
dprintf(" [>] !freelist <addr> - show the free list info\n");
dprintf(" [>] !heaphelp - show the help message\n");
}