forked from xiaobfly/DisPg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.c
222 lines (197 loc) · 6.06 KB
/
Stack.c
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
*
* Copyright (c) 2015-2018 by blindtiger ( [email protected] )
*
* The contents of this file are subject to the Mozilla Public License Version
* 2.0 (the "License")); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. SEe the License
* for the specific language governing rights and limitations under the
* License.
*
* The Initial Developer of the Original e is blindtiger.
*
*/
#include <OsDefs.h>
#include "Ctx.h"
#include "Reload.h"
#include "Stack.h"
#include "Testis.h"
PSTR
NTAPI
FindSymbol(
__in PVOID Address,
__out_opt PKLDR_DATA_TABLE_ENTRY * DataTableEntry
)
{
NTSTATUS Status = STATUS_SUCCESS;
PKLDR_DATA_TABLE_ENTRY FoundDataTableEntry = NULL;
PIMAGE_EXPORT_DIRECTORY ExportDirectory = NULL;
ULONG Size = 0;
PULONG NameTable = NULL;
PUSHORT OrdinalTable = NULL;
PULONG AddressTable = NULL;
USHORT HintIndex = 0;
PVOID ProcedureAddress = NULL;
PVOID NearAddress = NULL;
PSTR ProcedureName = NULL;
Status = FindEntryForAllKernelAddress(
Address,
&FoundDataTableEntry);
if (Status >= 0) {
ExportDirectory = RtlImageDirectoryEntryToData(
FoundDataTableEntry->DllBase,
TRUE,
IMAGE_DIRECTORY_ENTRY_EXPORT,
&Size);
if (NULL != ExportDirectory) {
NameTable = (PCHAR)FoundDataTableEntry->DllBase + ExportDirectory->AddressOfNames;
OrdinalTable = (PCHAR)FoundDataTableEntry->DllBase + ExportDirectory->AddressOfNameOrdinals;
AddressTable = (PCHAR)FoundDataTableEntry->DllBase + ExportDirectory->AddressOfFunctions;
if (NULL != NameTable &&
NULL != OrdinalTable &&
NULL != AddressTable) {
for (HintIndex = 0;
HintIndex < ExportDirectory->NumberOfNames;
HintIndex++) {
ProcedureAddress = (PCHAR)FoundDataTableEntry->DllBase + AddressTable[OrdinalTable[HintIndex]];
if ((ULONG_PTR)ProcedureAddress <=
(ULONG_PTR)Address) {
if (NULL == NearAddress) {
NearAddress = ProcedureAddress;
ProcedureName = (PCHAR)FoundDataTableEntry->DllBase + NameTable[HintIndex];
}
else if ((ULONG_PTR)ProcedureAddress > (ULONG_PTR)NearAddress) {
NearAddress = ProcedureAddress;
ProcedureName = (PCHAR)FoundDataTableEntry->DllBase + NameTable[HintIndex];
}
}
}
}
}
if (NULL != DataTableEntry) {
*DataTableEntry = FoundDataTableEntry;
}
}
else {
if (NULL != DataTableEntry) {
*DataTableEntry = NULL;
}
}
return ProcedureName;
}
VOID
NTAPI
PrintSymbol(
__in PVOID Address
)
{
PKLDR_DATA_TABLE_ENTRY DataTableEnry = NULL;
PVOID ProcedureAddress = NULL;
PSTR ProcedureName = NULL;
ProcedureName = FindSymbol(
(PVOID)Address,
&DataTableEnry);
if (NULL != ProcedureName) {
ProcedureAddress = GetProcedureAddress(
DataTableEnry->DllBase,
ProcedureName,
0);
if (0 == (ULONG64)Address - (ULONG64)ProcedureAddress) {
#ifndef VMP
DbgPrint(
"Soul - Testis - < %p > < %wZ!%hs >\n",
Address,
&DataTableEnry->BaseDllName,
ProcedureName);
#endif // !VMP
}
else {
#ifndef VMP
DbgPrint(
"Soul - Testis - < %p > < %wZ!%hs + 0x%x >\n",
Address,
&DataTableEnry->BaseDllName,
ProcedureName,
(ULONG64)Address - (ULONG64)ProcedureAddress);
#endif // !VMP
}
}
else if (NULL != DataTableEnry) {
#ifndef VMP
DbgPrint(
"Soul - Testis - < %p > < %wZ!%s + 0x%x >\n",
Address,
&DataTableEnry->BaseDllName,
(ULONG64)Address - (ULONG64)DataTableEnry->DllBase);
#endif // !VMP
}
else {
#ifndef VMP
DbgPrint(
"Soul - Testis - < %p >\n",
Address);
#endif // !VMP
}
}
PKTRAP_FRAME
NTAPI
GetBaseTrapFrame(
__in PETHREAD Thread
)
{
#ifndef _WIN64
ULONG InitialStack = 0;
InitialStack = (ULONG)IoGetInitialStack();
if (OsBuildNumber < 9200) {
return (PKTRAP_FRAME)(InitialStack - \
PSPALIGN_UP(sizeof(KTRAP_FRAME), KTRAP_FRAME_ALIGN) - \
sizeof(FX_SAVE_AREA));
}
else {
return (PKTRAP_FRAME)(InitialStack - \
PSPALIGN_UP(sizeof(KTRAP_FRAME), KTRAP_FRAME_ALIGN));
}
#else
ULONG64 InitialStack = 0;
PKSTACK_CONTROL_SPECIAL StackControlSpecial = NULL;
PKSTACK_CONTROL StackControl = NULL;
InitialStack = (ULONG64)IoGetInitialStack();
if (OsBuildNumber < 9200) {
StackControlSpecial = (PKSTACK_CONTROL_SPECIAL)InitialStack;
while (StackControlSpecial->Previous.StackBase != 0) {
InitialStack = StackControlSpecial->Previous.InitialStack;
StackControlSpecial = (PKERNEL_STACK_CONTROL)InitialStack;
}
}
else {
StackControl = (PKSTACK_CONTROL)InitialStack;
while (StackControl->Previous.StackBase != 0) {
InitialStack = StackControl->Previous.InitialStack;
StackControl = (PKSTACK_CONTROL)InitialStack;
}
}
return (PKTRAP_FRAME)(InitialStack - KTRAP_FRAME_LENGTH);
#endif // !_WIN64
}
VOID
NTAPI
PrintFrameChain(
__in_opt ULONG FramesToSkip
)
{
ULONG Index = 0;
ULONG Count = 0;
CALLERS Callers[MAX_STACK_DEPTH] = { 0 };
Count = WalkFrameChain(
Callers,
MAX_STACK_DEPTH);
for (Index = FramesToSkip;
Index < Count;
Index++) {
PrintSymbol(Callers[Index].Establisher);
}
}