-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathNtUtils.Debug.pas
297 lines (238 loc) · 8.25 KB
/
NtUtils.Debug.pas
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
unit NtUtils.Debug;
{
This module includes functions for user-mode debugging via Native API.
}
interface
uses
Ntapi.WinNt, Ntapi.ntdef, Ntapi.ntstatus, Ntapi.ntdbg,
NtUtils, NtUtils.Objects;
const
THREAD_SET_TRAP = THREAD_GET_CONTEXT or THREAD_SET_CONTEXT;
type
TDbgxWaitState = Ntapi.ntdbg.TDbgUiWaitStateChange;
TDbgxHandles = record
hxThread: IHandle;
hxProcess: IHandle;
hxFile: IHandle;
end;
{ -------------------------- Debug objects ----------------------------------- }
// Create a debug object
function NtxCreateDebugObject(
out hxDebugObj: IHandle;
KillOnClose: Boolean = False;
[opt] const ObjectAttributes: IObjectAttributes = nil
): TNtxStatus;
// Open existing debug object of a process
function NtxOpenDebugObjectProcess(
out hxDebugObj: IHandle;
[Access(PROCESS_QUERY_INFORMATION)] const hxProcess: IHandle
): TNtxStatus;
{ ------------------------ Debugging options --------------------------------- }
// Set whether the debugged process should be terminated
// when the last handle to its debug port is closed
function NtxSetDebugKillOnExit(
[Access(DEBUG_SET_INFORMATION)] const hxDebugObject: IHandle;
KillOnExit: LongBool
): TNtxStatus;
// Query whether child processes should be debugged as well
function NtxQueryDebugInherit(
[Access(PROCESS_QUERY_INFORMATION)] const hxProcess: IHandle;
out InheritDebugging: LongBool
): TNtxStatus;
// Set whether child processes should be debugged as well
function NtxSetDebugInherit(
[Access(PROCESS_SET_INFORMATION)] const hxProcess: IHandle;
InheritDebugging: LongBool
): TNtxStatus;
{ --------------------------- Debugging -------------------------------------- }
// Assign a debug object to a process
function NtxDebugProcess(
[Access(PROCESS_SUSPEND_RESUME)] const hxProcess: IHandle;
[Access(DEBUG_PROCESS_ASSIGN)] const hxDebugObject: IHandle
): TNtxStatus;
// Remove a debug object from a process
function NtxDebugProcessStop(
[Access(PROCESS_SUSPEND_RESUME)] const hxProcess: IHandle;
[Access(DEBUG_PROCESS_ASSIGN)] const hxDebugObject: IHandle
): TNtxStatus;
// Wait for a debug event
function NtxDebugWait(
[Access(DEBUG_READ_EVENT)] const hxDebugObj: IHandle;
out WaitStateChange: TDbgUiWaitStateChange;
out Handles: TDbgxHandles;
const Timeout: Int64 = NT_INFINITE;
Alertable: Boolean = False
): TNtxStatus;
// Continue after a debug event
function NtxDebugContinue(
[Access(DEBUG_READ_EVENT)] const hxDebugObject: IHandle;
const ClientId: TClientId;
Status: NTSTATUS = DBG_CONTINUE
): TNtxStatus;
{ ----------------------------- Breakin -------------------------------------- }
// Enable single-step flag for a thread
// NOTE: make sure the thread is suspended before calling this function
function NtxSetTrapFlagThread(
[Access(THREAD_SET_TRAP or THREAD_SUSPEND_RESUME)] const hxThread: IHandle;
Enabled: Boolean;
AlreadySuspended: Boolean = False
): TNtxStatus;
// Perform a single step of a thread to start debugging it
function DbgxIssueThreadBreakin(
[Access(THREAD_SET_TRAP)] const hxThread: IHandle
): TNtxStatus;
// Create a thread with a breakpoint inside a process
function DbgxIssueProcessBreakin(
[Access(PROCESS_CREATE_THREAD)] const hxProcess: IHandle
): TNtxStatus;
implementation
uses
Ntapi.ntpsapi, NtUtils.Threads, NtUtils.Processes.Info,
DelphiUtils.AutoObjects;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function NtxCreateDebugObject;
const
FLAGS: array [Boolean] of TDebugCreateFlags = (0, DEBUG_KILL_ON_CLOSE);
var
ObjAttr: PObjectAttributes;
hDebugObj: THandle;
begin
Result := AttributesRefOrNil(ObjAttr, ObjectAttributes);
if not Result.IsSuccess then
Exit;
Result.Location := 'NtCreateDebugObject';
Result.Status := NtCreateDebugObject(
hDebugObj,
AccessMaskOverride(DEBUG_ALL_ACCESS, ObjectAttributes),
ObjAttr,
FLAGS[KillOnClose <> False]
);
if Result.IsSuccess then
hxDebugObj := Auto.CaptureHandle(hDebugObj);
end;
function NtxOpenDebugObjectProcess;
var
hDebugObj: THandle;
begin
Result := NtxProcess.Query(hxProcess, ProcessDebugObjectHandle, hDebugObj);
if Result.IsSuccess then
hxDebugObj := Auto.CaptureHandle(hDebugObj);
end;
function NtxSetDebugKillOnExit;
begin
Result.Location := 'NtSetInformationDebugObject';
Result.LastCall.UsesInfoClass(DebugObjectKillProcessOnExitInformation, icSet);
Result.LastCall.Expects<TDebugObjectAccessMask>(DEBUG_SET_INFORMATION);
Result.Status := NtSetInformationDebugObject(HandleOrDefault(hxDebugObject),
DebugObjectKillProcessOnExitInformation, @KillOnExit, SizeOf(KillOnExit),
nil);
end;
function NtxQueryDebugInherit;
begin
Result := NtxProcess.Query(hxProcess, ProcessDebugFlags, InheritDebugging);
end;
function NtxSetDebugInherit;
begin
Result := NtxProcess.Set(hxProcess, ProcessDebugFlags, InheritDebugging);
end;
function NtxDebugProcess;
begin
Result.Location := 'NtDebugActiveProcess';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_SUSPEND_RESUME);
Result.LastCall.Expects<TDebugObjectAccessMask>(DEBUG_PROCESS_ASSIGN);
Result.Status := NtDebugActiveProcess(HandleOrDefault(hxProcess),
HandleOrDefault(hxDebugObject));
end;
function NtxDebugProcessStop;
begin
Result.Location := 'NtRemoveProcessDebug';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_SUSPEND_RESUME);
Result.LastCall.Expects<TDebugObjectAccessMask>(DEBUG_PROCESS_ASSIGN);
Result.Status := NtRemoveProcessDebug(HandleOrDefault(hxProcess),
HandleOrDefault(hxDebugObject));
end;
function NtxDebugWait;
begin
Result.Location := 'NtWaitForDebugEvent';
Result.LastCall.Expects<TDebugObjectAccessMask>(DEBUG_READ_EVENT);
Result.Status := NtWaitForDebugEvent(HandleOrDefault(hxDebugObj), Alertable,
TimeoutToLargeInteger(Timeout), WaitStateChange);
if not Result.IsSuccess or (Result.Status = STATUS_TIMEOUT) then
Exit;
Handles := Default(TDbgxHandles);
// Capture opened handles
with WaitStateChange do
case NewState of
DbgCreateThreadStateChange:
Handles.hxThread := Auto.CaptureHandle(CreateThread.HandleToThread);
DbgLoadDllStateChange:
if LoadDll.FileHandle <> 0 then
Handles.hxFile := Auto.CaptureHandle(LoadDll.FileHandle);
DbgCreateProcessStateChange:
begin
Handles.hxProcess := Auto.CaptureHandle(
CreateProcessInfo.HandleToProcess);
Handles.hxThread := Auto.CaptureHandle(
CreateProcessInfo.HandleToThread);
if CreateProcessInfo.NewProcess.FileHandle <> 0 then
Handles.hxFile := Auto.CaptureHandle(
CreateProcessInfo.NewProcess.FileHandle);
end;
end;
end;
function NtxDebugContinue;
begin
Result.Location := 'NtDebugContinue';
Result.LastCall.Expects<TDebugObjectAccessMask>(DEBUG_READ_EVENT);
Result.Status := NtDebugContinue(HandleOrDefault(hxDebugObject), ClientId, Status);
end;
function NtxSetTrapFlagThread;
var
Context: IContext;
DelayedResumer: IAutoReleasable;
begin
// We are going to change the thread's context, so make sure it is suspended
if not AlreadySuspended then
begin
Result := NtxSuspendThread(hxThread);
if not Result.IsSuccess then
Exit;
// Resume on exit
DelayedResumer := NtxDelayedResumeThread(hxThread);
end;
// Get thread's control registers
Result := NtxGetContextThread(hxThread, CONTEXT_CONTROL, Context);
if not Result.IsSuccess then
Exit;
if Enabled then
begin
// Skip if already enabled
if BitTest(Context.Data.EFlags and EFLAGS_TF) then
Exit;
Context.Data.EFlags := Context.Data.EFlags or EFLAGS_TF;
end
else
begin
// Skip if already cleared
if not BitTest(Context.Data.EFlags and EFLAGS_TF) then
Exit;
Context.Data.EFlags := Context.Data.EFlags and not EFLAGS_TF;
end;
// Apply the changes
Result := NtxSetContextThread(hxThread, Context.Data);
end;
function DbgxIssueThreadBreakin;
begin
// Enable single stepping for the thread. The system will clear this flag and
// notify the debugger on the next instruction executed by the target thread.
Result := NtxSetTrapFlagThread(hxThread, True);
end;
function DbgxIssueProcessBreakin;
begin
Result.Location := 'DbgUiIssueRemoteBreakin';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_CREATE_THREAD);
Result.Status := DbgUiIssueRemoteBreakin(HandleOrDefault(hxProcess));
end;
end.