-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdbgkfuncs.h
223 lines (184 loc) · 5.36 KB
/
dbgkfuncs.h
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
/*++ NDK Version: 0098
Copyright (c) Alex Ionescu. All rights reserved.
Header Name:
dbgkfuncs.h
Abstract:
Function definitions for the User Mode Debugging Facility.
Author:
Alex Ionescu ([email protected]) - Updated - 27-Feb-2006
--*/
#ifndef _DBGKFUNCS_H
#define _DBGKFUNCS_H
//
// Dependencies
//
#include <..\ndk\umtypes.h>
#include <..\ndk\dbgktypes.h>
#include <..\ndk\ntstrsafe.h>
//
// Native calls
//
NTSYSCALLAPI
NTSTATUS
NTAPI
NtDebugActiveProcess(
_In_ HANDLE Process,
_In_ HANDLE DebugObject
);
NTSYSCALLAPI
NTSTATUS
NTAPI
NtCreateDebugObject(
_Out_ PHANDLE DebugHandle,
_In_ ACCESS_MASK DesiredAccess,
_In_ POBJECT_ATTRIBUTES ObjectAttributes,
_In_ ULONG Flags
);
NTSYSCALLAPI
NTSTATUS
NTAPI
NtDebugContinue(
_In_ HANDLE DebugObject,
_In_ PCLIENT_ID AppClientId,
_In_ NTSTATUS ContinueStatus
);
NTSYSCALLAPI
NTSTATUS
NTAPI
NtWaitForDebugEvent(
_In_ HANDLE DebugObject,
_In_ BOOLEAN Alertable,
_In_opt_ PLARGE_INTEGER Timeout,
_Out_ PDBGUI_WAIT_STATE_CHANGE StateChange
);
NTSYSCALLAPI
NTSTATUS
NTAPI
NtRemoveProcessDebug(
_In_ HANDLE Process,
_In_ HANDLE DebugObject
);
NTSYSCALLAPI
NTSTATUS
NTAPI
NtSetInformationDebugObject(
_In_ HANDLE DebugObject,
_In_ DEBUGOBJECTINFOCLASS InformationClass,
_In_ PVOID Information,
_In_ ULONG InformationLength,
_Out_opt_ PULONG ReturnLength
);
NTSYSAPI
NTSTATUS
NTAPI
ZwDebugActiveProcess(
_In_ HANDLE Process,
_In_ HANDLE DebugObject
);
NTSYSAPI
NTSTATUS
NTAPI
ZwCreateDebugObject(
_Out_ PHANDLE DebugHandle,
_In_ ACCESS_MASK DesiredAccess,
_In_ POBJECT_ATTRIBUTES ObjectAttributes,
_In_ ULONG Flags
);
NTSYSAPI
NTSTATUS
NTAPI
ZwDebugContinue(
_In_ HANDLE DebugObject,
_In_ PCLIENT_ID AppClientId,
_In_ NTSTATUS ContinueStatus
);
NTSYSAPI
NTSTATUS
NTAPI
ZwRemoveProcessDebug(
_In_ HANDLE Process,
_In_ HANDLE DebugObject
);
NTSYSAPI
NTSTATUS
NTAPI
ZwWaitForDebugEvent(
_In_ HANDLE DebugObject,
_In_ BOOLEAN Alertable,
_In_opt_ PLARGE_INTEGER Timeout,
_Out_ PDBGUI_WAIT_STATE_CHANGE StateChange
);
NTSYSAPI
NTSTATUS
NTAPI
ZwSetInformationDebugObject(
_In_ HANDLE DebugObject,
_In_ DEBUGOBJECTINFOCLASS InformationClass,
_In_ PVOID Information,
_In_ ULONG InformationLength,
_Out_opt_ PULONG ReturnLength
);
typedef struct _DBGOUT_STATIC_STORAGE {
USHORT lineNum;
USHORT currentWchar;
} DBGOUT_STATIC_STORAGE, *PDBGOUT_STATIC_STORAGE;
#if defined(NTOS_MODE_USER)
#if !defined(BOOTSCR_OUTPUT)
#define ALPHABET_LETTER_COUNT 26
static void myWPrintf(const WCHAR* pFormatString, ...){
WCHAR szPrintBuf[512];
WCHAR szValueName[4];
va_list args;
///Get ourselves per-thread static storage for storing the current line number as well as the current
///output key. As the field is highly undocumented it might not work in the future
///if Microsoft decides to actually use the field for something other than a neither correctly working
///nor useful ProcessInstrumentationCallback implementation.
PDBGOUT_STATIC_STORAGE pStaticInfoStorage = (PDBGOUT_STATIC_STORAGE)NtCurrentTeb()->SpareBytes1;
WCHAR szVPrintfErrorString[] = { L'R', L't', L'l', L'S', L't', L'r', L'i', L'n', L'g', L'V', L'P', L'r', L'i', L'n', L't', L'f', L'W', L'o', L'r', L'k', L'e', L'r', L'W', L' ', L'e', L'r', L'r', L'o', L'r', L'!', 0x0};
WCHAR szParentOutputKey[] = { L'D', L'b', L'g', L'O', L'u', L't', 0x0 };
WCHAR szOutputKeyPath[] = { L'D', L'b', L'g', L'O', L'u', L't', L'\\', L'O', L'u', L't', L'p', L'u', L't', L' ', L'A', 0x0 };
NTSTATUS status = STATUS_UNSUCCESSFUL;
#if defined (BOOT_APP)
ULONG relativeTo = RTL_REGISTRY_CONTROL;
#else
ULONG relativeTo = RTL_REGISTRY_USER;
#endif
if (!pFormatString || !pStaticInfoStorage)
return;
///DANGEROUS! Not invoking the API calls by function pointers will lead to non PIC code.
///We must never forget about that if we want to write PIC code.
status = RtlCreateRegistryKey(relativeTo, szParentOutputKey);
if (!pStaticInfoStorage->lineNum){
for (USHORT i = 0; i < ALPHABET_LETTER_COUNT; i++){
szOutputKeyPath[sizeof(szOutputKeyPath) / sizeof(WCHAR) - 2] = L'A' + i;
status = RtlCheckRegistryKey(relativeTo, szOutputKeyPath);
if (STATUS_OBJECT_NAME_NOT_FOUND == status) {
pStaticInfoStorage->currentWchar = L'A' + i;
break;
}
}
}
else {
szOutputKeyPath[sizeof(szOutputKeyPath) / sizeof(WCHAR) - 2] = pStaticInfoStorage->currentWchar;
}
(pStaticInfoStorage->lineNum)++;
szValueName[2] = L'a' + (pStaticInfoStorage->lineNum) % ALPHABET_LETTER_COUNT;
szValueName[1] = L'a' + (pStaticInfoStorage->lineNum) / ALPHABET_LETTER_COUNT;
szValueName[0] = L'a' + (pStaticInfoStorage->lineNum) / (ALPHABET_LETTER_COUNT * ALPHABET_LETTER_COUNT);
szValueName[sizeof(szValueName) / sizeof(WCHAR) - 1] = 0x0;
RtlSecureZeroMemory(szPrintBuf, sizeof(szPrintBuf));
va_start(args, pFormatString);
#pragma warning(push)
#pragma warning(disable:4995) ///name was marked as #pragma deprecated
status = RtlStringVPrintfWorkerW(szPrintBuf, sizeof(szPrintBuf) / sizeof(WCHAR), NULL, pFormatString, args);
#pragma warning(pop)
va_end(args);
if (status){
RtlWriteRegistryValue(relativeTo, szOutputKeyPath, szValueName, REG_SZ, szVPrintfErrorString, sizeof(szVPrintfErrorString));
return;
}
RtlWriteRegistryValue(relativeTo, szOutputKeyPath, szValueName, REG_MULTI_SZ, szPrintBuf, sizeof(szPrintBuf));
}
#endif ///!BOOTSCR_OUTPUT
#endif ///UM
#endif ///DBGKFUNCS_H