/
DebugEvent.cs
402 lines (372 loc) · 13.4 KB
/
DebugEvent.cs
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright 2019 Google Inc. All Rights Reserved.
//
// Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
namespace NtApiDotNet
{
/// <summary>
/// The base class for a debug event.
/// </summary>
public abstract class DebugEvent : IDisposable
{
#region Private Members
private readonly NtDebug _debug;
#endregion
/// <summary>
/// Process ID for the event.
/// </summary>
public int ProcessId { get; }
/// <summary>
/// Thread ID for the event.
/// </summary>
public int ThreadId { get; }
/// <summary>
/// The event code.
/// </summary>
public DbgState State { get; }
/// <summary>
/// Constructor.
/// </summary>
/// <param name="debug_event">The current debug event.</param>
/// <param name="debug">The debug port associated with this event.</param>
protected DebugEvent(DbgUiWaitStatusChange debug_event, NtDebug debug)
{
ProcessId = debug_event.AppClientId.UniqueProcess.ToInt32();
ThreadId = debug_event.AppClientId.UniqueThread.ToInt32();
State = debug_event.NewState;
_debug = debug;
}
/// <summary>
/// Continue the debugged process.
/// </summary>
/// <param name="continue_status">The continue status code.</param>
/// <param name="throw_on_error">True to throw on error.</param>
/// <returns>The NT status code.</returns>
public NtStatus Continue(NtStatus continue_status, bool throw_on_error)
{
return _debug.Continue(ProcessId, ThreadId, continue_status, throw_on_error);
}
/// <summary>
/// Continue the debugged process.
/// </summary>
/// <param name="continue_status">The continue status code.</param>
public void Continue(NtStatus continue_status)
{
_debug.Continue(ProcessId, ThreadId, continue_status);
}
/// <summary>
/// Continue the debugged process with a success code.
/// </summary>
public void Continue()
{
Continue(NtStatus.DBG_CONTINUE);
}
/// <summary>
/// Dispose the event.
/// </summary>
public virtual void Dispose()
{
}
internal static DebugEvent FromDebugEvent(DbgUiWaitStatusChange debug_event, NtDebug debug)
{
switch (debug_event.NewState)
{
case DbgState.CreateProcessStateChange:
return new CreateProcessDebugEvent(debug_event, debug);
case DbgState.CreateThreadStateChange:
return new CreateThreadDebugEvent(debug_event, debug);
case DbgState.BreakpointStateChange:
case DbgState.ExceptionStateChange:
case DbgState.SingleStepStateChange:
return new ExceptionDebugEvent(debug_event, debug);
case DbgState.ExitProcessStateChange:
return new ExitProcessDebugEvent(debug_event, debug);
case DbgState.ExitThreadStateChange:
return new ExitThreadDebugEvent(debug_event, debug);
case DbgState.LoadDllStateChange:
return new LoadDllDebugEvent(debug_event, debug);
case DbgState.UnloadDllStateChange:
return new UnloadDllDebugEvent(debug_event, debug);
default:
return new UnknownDebugEvent(debug_event, debug);
}
}
}
/// <summary>
/// Debug event for the Create Process event.
/// </summary>
public sealed class CreateProcessDebugEvent : DebugEvent
{
/// <summary>
/// Subsystem key for the process.
/// </summary>
public int ProcessSubSystemKey { get; }
/// <summary>
/// Handle to the process file (if available).
/// </summary>
public NtFile File { get; }
/// <summary>
/// Base of image file.
/// </summary>
public long BaseOfImage { get; }
/// <summary>
/// Debug info file offset.
/// </summary>
public int DebugInfoFileOffset { get; }
/// <summary>
/// Debug info file size.
/// </summary>
public int DebugInfoSize { get; }
/// <summary>
/// Subsystem key for the thread.
/// </summary>
public int ThreadSubSystemKey { get; }
/// <summary>
/// Start address of the thread.
/// </summary>
public long ThreadStartAddress { get; }
/// <summary>
/// Handle to the process (if available).
/// </summary>
public NtProcess Process { get; }
/// <summary>
/// Handle to the thread (if available).
/// </summary>
public NtThread Thread { get; }
internal CreateProcessDebugEvent(DbgUiWaitStatusChange debug_event, NtDebug debug)
: base(debug_event, debug)
{
var info = debug_event.StateInfo.CreateProcess;
Process = info.HandleToProcess == IntPtr.Zero ? null : NtProcess.FromHandle(info.HandleToProcess);
Thread = info.HandleToThread == IntPtr.Zero ? null : NtThread.FromHandle(info.HandleToThread);
var new_proc = info.NewProcess;
ProcessSubSystemKey = new_proc.SubSystemKey;
File = new_proc.FileHandle == IntPtr.Zero ? null : NtFile.FromHandle(new_proc.FileHandle);
BaseOfImage = new_proc.BaseOfImage.ToInt64();
DebugInfoFileOffset = new_proc.DebugInfoFileOffset;
DebugInfoSize = new_proc.DebugInfoSize;
var thread = new_proc.InitialThread;
ThreadSubSystemKey = thread.SubSystemKey;
ThreadStartAddress = thread.StartAddress.ToInt64();
}
/// <summary>
/// Dispose the event.
/// </summary>
public override void Dispose()
{
Process?.Dispose();
Thread?.Dispose();
File?.Dispose();
}
}
/// <summary>
/// Debug event for the Create Thread event.
/// </summary>
public sealed class CreateThreadDebugEvent : DebugEvent
{
/// <summary>
/// Subsystem key for the thread.
/// </summary>
public int ThreadSubSystemKey { get; }
/// <summary>
/// Start address of the thread.
/// </summary>
public long ThreadStartAddress { get; }
/// <summary>
/// Handle to the thread (if available).
/// </summary>
public NtThread Thread { get; }
internal CreateThreadDebugEvent(DbgUiWaitStatusChange debug_event, NtDebug debug)
: base(debug_event, debug)
{
var info = debug_event.StateInfo.CreateThread;
Thread = info.HandleToThread == IntPtr.Zero ? null : NtThread.FromHandle(info.HandleToThread);
var thread = info.NewThread;
ThreadSubSystemKey = thread.SubSystemKey;
ThreadStartAddress = thread.StartAddress.ToInt64();
}
/// <summary>
/// Dispose the event.
/// </summary>
public override void Dispose()
{
Thread?.Dispose();
}
}
/// <summary>
/// Debug event for the Exit Thread event.
/// </summary>
public sealed class ExitThreadDebugEvent : DebugEvent
{
/// <summary>
/// Exit status code.
/// </summary>
public NtStatus ExitStatus { get; }
internal ExitThreadDebugEvent(DbgUiWaitStatusChange debug_event, NtDebug debug)
: base(debug_event, debug)
{
ExitStatus = debug_event.StateInfo.ExitThread.ExitStatus;
}
}
/// <summary>
/// Debug event for the Exit Process event.
/// </summary>
public sealed class ExitProcessDebugEvent : DebugEvent
{
/// <summary>
/// Exit status code.
/// </summary>
public NtStatus ExitStatus { get; }
internal ExitProcessDebugEvent(DbgUiWaitStatusChange debug_event, NtDebug debug)
: base(debug_event, debug)
{
ExitStatus = debug_event.StateInfo.ExitProcess.ExitStatus;
}
}
/// <summary>
/// Debug event for load DLL event.
/// </summary>
public sealed class LoadDllDebugEvent : DebugEvent
{
/// <summary>
/// DLL file handle.
/// </summary>
public NtFile File { get; }
/// <summary>
/// Base of loaded DLL.
/// </summary>
public long BaseOfDll { get; }
/// <summary>
/// Debug info offset.
/// </summary>
public int DebugInfoFileOffset { get; }
/// <summary>
/// Debug info size.
/// </summary>
public int DebugInfoSize { get; }
/// <summary>
/// Address of name.
/// </summary>
public long NamePointer { get; }
internal LoadDllDebugEvent(DbgUiWaitStatusChange debug_event, NtDebug debug)
: base(debug_event, debug)
{
var info = debug_event.StateInfo.LoadDll;
File = info.FileHandle == IntPtr.Zero ? null : NtFile.FromHandle(info.FileHandle);
BaseOfDll = info.BaseOfDll.ToInt64();
DebugInfoFileOffset = info.DebugInfoFileOffset;
DebugInfoSize = info.DebugInfoSize;
NamePointer = info.NamePointer.ToInt64();
}
/// <summary>
/// Dispose the event.
/// </summary>
public override void Dispose()
{
File?.Dispose();
}
}
/// <summary>
/// Debug event for unload DLL event.
/// </summary>
public sealed class UnloadDllDebugEvent : DebugEvent
{
/// <summary>
/// Base of loaded DLL.
/// </summary>
public long BaseAddress { get; }
internal UnloadDllDebugEvent(DbgUiWaitStatusChange debug_event, NtDebug debug)
: base(debug_event, debug)
{
var info = debug_event.StateInfo.UnloadDll;
BaseAddress = info.BaseAddress.ToInt64();
}
}
/// <summary>
/// Debug event for exception event.
/// </summary>
public sealed class ExceptionDebugEvent : DebugEvent
{
/// <summary>
/// Indicates if this is a first chance exception.
/// </summary>
public bool FirstChance { get; }
/// <summary>
/// Exception code.
/// </summary>
public NtStatus Code { get; }
/// <summary>
/// Exception flags.
/// </summary>
public NtStatus Flags { get; }
/// <summary>
/// Pointer to next exception in the chain.
/// </summary>
public long RecordChain { get; }
/// <summary>
/// Address of exception.
/// </summary>
public long Address { get; }
/// <summary>
/// Additional parameters for exception.
/// </summary>
public IList<long> Parameters { get; }
internal ExceptionDebugEvent(DbgUiWaitStatusChange debug_event, NtDebug debug)
: base(debug_event, debug)
{
var info = debug_event.StateInfo.Exception;
FirstChance = info.FirstChance != 0;
var exp = info.ExceptionRecord;
Code = exp.ExceptionCode;
Flags = exp.ExceptionFlags;
RecordChain = exp.ExceptionRecordChain.ToInt64();
Address = exp.ExceptionAddress.ToInt64();
List<long> ps = new List<long>
{
exp.ExceptionInformation0.ToInt64(),
exp.ExceptionInformation1.ToInt64(),
exp.ExceptionInformation2.ToInt64(),
exp.ExceptionInformation3.ToInt64(),
exp.ExceptionInformation4.ToInt64(),
exp.ExceptionInformation5.ToInt64(),
exp.ExceptionInformation6.ToInt64(),
exp.ExceptionInformation7.ToInt64(),
exp.ExceptionInformation8.ToInt64(),
exp.ExceptionInformation9.ToInt64(),
exp.ExceptionInformationA.ToInt64(),
exp.ExceptionInformationB.ToInt64(),
exp.ExceptionInformationC.ToInt64(),
exp.ExceptionInformationD.ToInt64(),
exp.ExceptionInformationE.ToInt64()
};
ps.RemoveRange(exp.NumberParameters, ps.Count - exp.NumberParameters);
Parameters = ps.AsReadOnly();
}
}
/// <summary>
/// Debug event when we don't handle the state.
/// </summary>
public sealed class UnknownDebugEvent : DebugEvent
{
/// <summary>
/// The raw debug event.
/// </summary>
public DbgUiWaitStatusChange DebugEvent { get; }
internal UnknownDebugEvent(DbgUiWaitStatusChange debug_event, NtDebug debug)
: base(debug_event, debug)
{
DebugEvent = debug_event;
}
}
}