forked from cheat-engine/cheat-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCEDebugger.pas
305 lines (232 loc) · 9.5 KB
/
CEDebugger.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
298
299
300
301
302
303
304
305
unit CEDebugger;
//OLD debugger. Obsolete , just here till all old references have been removed
{$MODE Delphi}
interface
uses windows, Classes,LCLIntf,sysutils,CEFuncProc,Messages,forms,SyncObjs,
dialogs,controls,Graphics,NewKernelHandler,symbolhandler,StrUtils,
ComCtrls ,Assemblerunit,addressparser, debughelper;
type TReadonly = record
pagebase: uint_ptr;
pagesize: uint_ptr;
Address: uint_ptr;
size: integer;
originalprotection: dword;
end;
type tThreadEntry=record
threadHandle: thandle;
address: uint_ptr;
end;
type Process=record
ProcessID: dword;
running: boolean;
end;
type TDbgUIDebugActiveProcess = function(processhandle:THandle):boolean; stdcall;
type TDebugBreakProcess = function(processhandle:THandle):boolean; stdcall;
type TDebugActiveProcessStop= function(pid: dword):boolean; stdcall;
type TDebugSetProcessKillOnExit=function(KillOnExit: boolean):boolean; stdcall;
type TIsDebuggerPresent=function:boolean; stdcall;
type TntSuspendProcess=function(ProcessID:Dword):DWORD; stdcall;
type TntResumeProcess=function(ProcessID:Dword):DWORD; stdcall;
type
SYSTEM_HANDLE_TABLE_ENTRY_INFO=record
ProcessId: DWORD; //USHORT
ObjectTypeIndex: UCHAR;
HandleAttributes: UCHAR;
HandleValue: USHORT;
obj: pointer;
GrantedAccess: ACCESS_MASK;
end;
SYSTEM_HANDLE_INFORMATION=record
HandleCount: DWORD;
list: array [0..0] of SYSTEM_HANDLE_TABLE_ENTRY_INFO;
end;
PSYSTEM_HANDLE_INFORMATION=^SYSTEM_HANDLE_INFORMATION;
TProcessBasicInformation = record
ExitStatus : Longint;
PebBaseAddress : Pointer;
AffinityMask : DWORD;
BasePriority : Longint;
UniqueProcessId : DWORD;
InheritedFromUniqueProcessId : DWORD
end;
NTSTATUS = LONG;
_CLIENT_ID = record
UniqueProcess: HANDLE;
UniqueThread: HANDLE;
end;
CLIENT_ID = _CLIENT_ID;
PCLIENT_ID = ^CLIENT_ID;
TClientID = CLIENT_ID;
PClientID = ^TClientID;
KPRIORITY = LONG;
KAFFINITY = ULONG_PTR;
_THREAD_BASIC_INFORMATION = record // Information Class 0
ExitStatus: NTSTATUS;
TebBaseAddress: pointer;
ClientId: CLIENT_ID;
AffinityMask: KAFFINITY;
Priority: KPRIORITY;
BasePriority: KPRIORITY;
end;
THREAD_BASIC_INFORMATION = _THREAD_BASIC_INFORMATION;
PTHREAD_BASIC_INFORMATION = ^THREAD_BASIC_INFORMATION;
TThreadBasicInformation = THREAD_BASIC_INFORMATION;
PThreadBasicInformation = ^TThreadBasicInformation;
TProcessInfoClass=(
ProcessBasicInformation,ProcessQuotaLimits,ProcessIoCounters,ProcessVmCounters,ProcessTimes,
ProcessBasePriority,ProcessRaisePriority,ProcessDebugPort,ProcessExceptionPort,ProcessAccessToken,
ProcessLdtInformation,ProcessLdtSize,ProcessDefaultHardErrorMode,ProcessIoPortHandlers,
ProcessPooledUsageAndLimits,ProcessWorkingSetWatch,ProcessUserModeIOPL,ProcessEnableAlignmentFaultFixup,
ProcessPriorityClass,ProcessWx86Information,ProcessHandleCount,ProcessAffinityMask,ProcessPriorityBoost,
ProcessDeviceMap,ProcessSessionInformation,ProcessForegroundInformation,ProcessWow64Information,
MaxProcessInfoClass);
_THREADINFOCLASS = (
ThreadBasicInformation,
ThreadTimes,
ThreadPriority,
ThreadBasePriority,
ThreadAffinityMask,
ThreadImpersonationToken,
ThreadDescriptorTableEntry,
ThreadEnableAlignmentFaultFixup,
ThreadEventPair_Reusable,
ThreadQuerySetWin32StartAddress,
ThreadZeroTlsCell,
ThreadPerformanceCount,
ThreadAmILastThread,
ThreadIdealProcessor,
ThreadPriorityBoost,
ThreadSetTlsArrayAddress,
ThreadIsIoPending,
ThreadHideFromDebugger,
ThreadBreakOnTermination, // was added in XP - used by RtlSetThreadIsCritical()
MaxThreadInfoClass);
THREADINFOCLASS = _THREADINFOCLASS;
{.$ENDIF JWA_INCLUDEMODE}
THREAD_INFORMATION_CLASS = THREADINFOCLASS;
TThreadInfoClass = THREADINFOCLASS;
const SystemHandleInformation=16;
type TNtQuerySystemInformation=function(infoClass : dword; systemInformation : Pointer; SystemInformationLength : ULONG; returnLength : PULONG) : DWORD; stdcall;
type TNtQueryInformationProcess=function(Handle : THandle; infoClass : TProcessInfoClass; processInformation : Pointer; processInformationLength : ULONG; returnLength : PULONG) : DWORD; stdcall;
type TNtQueryInformationThread=function(Handle : THandle; infoClass : TThreadinfoClass; ThreadInformation: pointer; processInformationLength : ULONG; returnLength : PULONG) : DWORD; stdcall;
var //DebuggerThread: TDebugger;
DbgUIDebugActiveProcess:TDbgUIDebugActiveProcess;
DebugBreakProcess:TDebugBreakProcess;
DebugActiveProcessStop:TDebugActiveProcessStop;
DebugSetProcessKillOnExit:TDebugSetProcessKillOnExit;
IsDebuggerPresent:TIsDebuggerPresent;
ntSuspendProcess: TntSuspendProcess;
ntResumeProcess: tntResumeProcess;
NtQuerySystemInformation: TNtQuerySystemInformation;
NtQueryInformationProcess: TNtQueryInformationProcess;
NtQueryInformationThread: TNtQueryInformationThread;
DbgBreakPointLocation:ptrUint;
krn: thandle;
ntdlllib: thandle;
CRDebugging: TCriticalSection;
function startdebuggerifneeded: boolean; overload;
function startdebuggerifneeded(ask:boolean): boolean; overload;
function DebugActiveProcessStopProstitute(x: dword): boolean;
implementation
uses debuggertypedefinitions, debugeventhandler, MainUnit,frmFloatingPointPanelUnit,
Memorybrowserformunit,disassembler,frmTracerUnit,foundcodeunit,kerneldebugger,
advancedoptionsunit,formChangedAddresses,frmstacktraceunit,frmThreadlistunit,
formdebugstringsunit,formsettingsunit,processwindowunit,plugin,processhandlerunit(*,frmCreatedProcessListUnit*);
resourcestring
rsPleaseTargetAnotherProcess = 'Please target another process';
rsYouMustFirstOpenAProcess = 'You must first open a process';
rsThisWillAttachTheDebuggerOfCheatEngineToTheCurrent = 'This will attach the debugger of Cheat Engine to the current process.';
rsDoNotCloseCE = 'If you close Cheat Engine while the game is running, the game will close too. Are you sure you want to do this?';
rsContinue = 'Continue?';
rsDebugError = 'I couldn''t attach the debugger to this process! You could try to open the process using the processpicker and try that! If that also doesn''t work check if '
+'you have debugging rights.';
function DebugBreakProstitute(x: Thandle):boolean;
begin
result:=false;
end;
function DebugSetProcessKillOnExitProtitute(x: boolean):boolean;
begin
result:=false;
end;
function DebugActiveProcessStopProstitute(x: dword): boolean;
begin
result:=false;
end;
function startdebuggerifneeded(ask:boolean): boolean; overload;
var mes: string;
reS:boolean;
i: integer;
begin
result:=false;
if processid=GetCurrentProcessId then raise exception.create(rsPleaseTargetAnotherProcess);
if processhandle=0 then raise exception.create(rsYouMustFirstOpenAProcess);
if (debuggerthread=nil) then
begin
if @DebugActiveProcessStop=@DebugActiveProcessStopProstitute then
mes:=rsThisWillAttachTheDebuggerOfCheatEngineToTheCurrent+' '+rsDoNotCloseCE
else
mes:=rsThisWillAttachTheDebuggerOfCheatEngineToTheCurrent+' '+rsContinue;
if ask then
res:=Messagedlg(mes,mtConfirmation,[mbYes, mbNo],0)=mrYes
else
res:=true;
if res then
begin
//start the debugger on the current process
//check for a debugger
try
Debuggerthread:=TDebuggerThread.MyCreate2(processid);
except
raise exception.Create(rsDebugError);
end;
result:=true;
exit;
end
else
begin
result:=false;
exit;
end;
end;
result:=true;
end;
function startdebuggerifneeded: boolean; overload;
begin
result:=startdebuggerifneeded(true);
end;
initialization
CRDebugging:=TCriticalSection.Create;
krn := LoadLibrary('Kernel32.dll');
if krn <> 0 then
begin
@DebugBreakProcess := GetProcAddress(krn, 'DebugBreakProcess');
@DebugSetProcessKillOnExit :=GetProcAddress(krn,'DebugSetProcessKillOnExit');
@DebugActiveProcessStop :=GetProcAddress(krn,'DebugActiveProcessStop');
@IsDebuggerPresent:=GetProcAddress(krn,'IsDebuggerPresent');
if @DebugBreakProcess=nil then
@DebugBreakProcess:=@DebugBreakProstitute;
if @DebugSetProcessKillOnExit=nil then
@DebugSetProcessKillOnExit:=@DebugSetProcessKillOnExitProtitute;
if @DebugActiveProcessStop=nil then
@DebugActiveProcessStop:=@DebugActiveProcessStopProstitute;
end;
ntdlllib:=LoadLibrary('ntdll.dll');
if ntdlllib<>0 then
begin
DbgUIDebugActiveProcess:=getprocaddress(ntdlllib,'DbgUiDebugActiveProcess');
DbgBreakPointLocation:=ptrUint(getprocaddress(ntdlllib,'DbgBreakPoint'));
NtQueryInformationProcess:=nil;
NtQueryInformationProcess:=GetProcAddress(ntdlllib,'NtQueryInformationProcess');
NtQueryInformationThread:=nil;
NtQueryInformationThread:=GetProcAddress(ntdlllib,'NtQueryInformationThread');
NtQuerySystemInformation:=nil;
NtQuerySystemInformation:=GetProcAddress(ntdlllib,'NtQuerySystemInformation');
ntsuspendprocess:=nil;
ntsuspendprocess:=GetProcAddress(ntdlllib,'NtSuspendProcess');
ntresumeprocess:=GetProcAddress(ntdlllib,'NtResumeProcess');
freelibrary(ntdlllib);
end;
finalization
if CRDebugging<>nil then
CRDebugging.Free;
end.