-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMainScript.cpp
More file actions
377 lines (310 loc) · 10 KB
/
MainScript.cpp
File metadata and controls
377 lines (310 loc) · 10 KB
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
//---------------------------------------------------------------------------
#include <vcl.h>
#include <psapi.h>
#include <stdio.h>
#pragma hdrstop
#include "MainScript.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFPSPatcher *FPSPatcher;
//---------------------------------------------------------------------------
void __fastcall TFPSPatcher::OnMessagePathInfo(TMessage &M)
{
HWND hwnd = 0;
if (lb_client_list->ItemIndex != -1)
hwnd = m_client_list[lb_client_list->ItemIndex];
if ((HWND)M.LParam == hwnd)
{
if (M.WParam)
{
l_status->Font->Color = clGreen;
l_status->Caption = "Patched";
bt_patch->Caption = "Unset patch";
}
else
{
l_status->Font->Color = clRed;
l_status->Caption = "NOT Patched";
bt_patch->Caption = "Set patch";
}
}
else
M.Result=DefWindowProc(Handle,M.Msg,M.WParam,M.LParam);
}
//---------------------------------------------------------------------------
bool __stdcall EnumProc(HWND hWnd,long)
{
PDWORD pPid; //LPDWORD
DWORD result; //DWORD
PVOID hg; //HGLOBAL
if (!hWnd)
return false;
hg = GlobalAlloc(GMEM_SHARE, sizeof(DWORD));
pPid = (PDWORD)GlobalLock(hg);
result = GetWindowThreadProcessId(hWnd, pPid);
if (result)
{
char title[150] = {0};
char className[30] = {0};
GetClassName(hWnd, className, 30);
GetWindowText(hWnd, title, 150);
if (strstr(className, "Ultima Online"))
{
FPSPatcher->lb_client_list->Items->Add(title);
FPSPatcher->m_client_list.push_back(hWnd);
}
}
else
{
GlobalUnlock(hg);
GlobalFree(hg);
return false;
}
GlobalUnlock(hg);
GlobalFree(hg);
return true;
}
//---------------------------------------------------------------------------
__fastcall TFPSPatcher::TFPSPatcher(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
BOOL SetPrivilege(HANDLE hToken,LPCTSTR lpszPrivilege,BOOL bEnablePrivilege)
{
TOKEN_PRIVILEGES tp;
LUID luid;
if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid))
return FALSE;
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (bEnablePrivilege)
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[0].Attributes = 0;
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL))
return FALSE;
return TRUE;
}
//---------------------------------------------------------------------------
BOOL EnableDebugPrivilages()
{
HANDLE hToken;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE;
return SetPrivilege(hToken, SE_DEBUG_NAME, TRUE);
}
//---------------------------------------------------------------------------
void __fastcall TFPSPatcher::bt_refreshClick(TObject *Sender)
{
l_status->Font->Color = clWindowText;
l_status->Caption = "Unknown";
lb_client_list->Items->Clear();
m_client_list.clear();
long lp = 0;
EnumWindows((WNDENUMPROC)EnumProc, lp);
}
//---------------------------------------------------------------------------
void __fastcall TFPSPatcher::FormCreate(TObject *Sender)
{
EnableDebugPrivilages();
bt_refreshClick(bt_refresh);
}
//---------------------------------------------------------------------------
void __fastcall TFPSPatcher::lb_client_listClick(TObject *Sender)
{
if (lb_client_list->ItemIndex == -1)
{
l_status->Font->Color = clWindowText;
l_status->Caption = "Unknown";
return;
}
HWND hWnd = m_client_list[lb_client_list->ItemIndex];
HWND dllWindow = FindWindow(("FPSModWindow_" + IntToHex((int)hWnd, 8)).t_str(), NULL);
if (dllWindow == NULL)
{
DWORD processID = 0;
GetWindowThreadProcessId(hWnd, &processID);
HANDLE hp = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, TRUE, processID);
if (hp != NULL)
{
String path = ExtractFilePath(Application->ExeName) + "\\FPSMod.dll";
HANDLE hProcess = NULL, hThread = NULL;
PWSTR pszLibFileRemote = NULL;
__try
{
// Get a handle for the target process.
hProcess = OpenProcess(
PROCESS_QUERY_INFORMATION | // Required by Alpha
PROCESS_CREATE_THREAD | // For CreateRemoteThread
PROCESS_VM_OPERATION | // For VirtualAllocEx/VirtualFreeEx
PROCESS_VM_WRITE | // For WriteProcessMemory
PROCESS_VM_READ,
FALSE, processID);
if (hProcess == NULL)
{
MessageBox(Handle, "Open process failed.", "Error", MB_OK);
return;
}
// Calculate the number of bytes needed for the DLL's pathname
int cch = 1 + lstrlenW(path.c_str());
int cb = cch * sizeof(WCHAR);
// Allocate space in the remote process for the pathname
pszLibFileRemote=(PWSTR)VirtualAllocEx(hProcess, NULL, cb, MEM_COMMIT, PAGE_READWRITE);
if (pszLibFileRemote == NULL)
{
if (hProcess!=NULL)
CloseHandle(hProcess);
MessageBox(Handle, "Memory can't be selected.", "Error", MB_OK);
return;
}
// Copy the DLL's pathname to the remote process's address space
if (!WriteProcessMemory(hProcess, pszLibFileRemote, (PVOID) path.c_str(), cb, NULL))
{
if (pszLibFileRemote != NULL)
VirtualFreeEx(hProcess, pszLibFileRemote, 0, MEM_RELEASE);
if (hProcess != NULL)
CloseHandle(hProcess);
MessageBox(Handle, "Failed to write string in to the memory.", "Error", MB_OK);
return;
}
// Get the real address of LoadLibraryW in Kernel32.dll
PTHREAD_START_ROUTINE pfnThreadRtn = (PTHREAD_START_ROUTINE)
GetProcAddress(GetModuleHandle(TEXT("Kernel32")), "LoadLibraryW");
if (pfnThreadRtn == NULL)
{
if (pszLibFileRemote != NULL)
VirtualFreeEx(hProcess, pszLibFileRemote, 0, MEM_RELEASE);
if (hProcess != NULL)
CloseHandle(hProcess);
MessageBox(Handle, "LoadLibraryW not found in Kernel32.dll", "Error", MB_OK);
return;
}
bool created = false;
/*HMODULE modNtDll = GetModuleHandle("ntdll.dll");
if (modNtDll != NULL)
{
typedef NTSTATUS (WINAPI *LPFUN_NtCreateThreadEx)
(
OUT PHANDLE hThread,
IN ACCESS_MASK DesiredAccess,
IN LPVOID ObjectAttributes,
IN HANDLE ProcessHandle,
IN LPTHREAD_START_ROUTINE lpStartAddress,
IN LPVOID lpParameter,
IN BOOL CreateSuspended,
IN ULONG StackZeroBits,
IN ULONG SizeOfStackCommit,
IN ULONG SizeOfStackReserve,
OUT LPVOID lpBytesBuffer
);
LPFUN_NtCreateThreadEx funNtCreateThreadEx = (LPFUN_NtCreateThreadEx)GetProcAddress(modNtDll, "NtCreateThreadEx");
ShowMessage("funNtCreateThreadEx=0x"+IntToHex((int)funNtCreateThreadEx, 8)+"; 0x"+IntToHex((int)GetProcAddress(modNtDll, "NtCreateThread"),8 ));
if (funNtCreateThreadEx != NULL)
{
typedef struct NtCreateThreadExBuffer
{
ULONG Size;
ULONG Unknown1;
ULONG Unknown2;
PULONG Unknown3;
ULONG Unknown4;
ULONG Unknown5;
ULONG Unknown6;
PULONG Unknown7;
ULONG Unknown8;
} NtCreateThreadExBuffer;
NtCreateThreadExBuffer ntbuffer;
memset (&ntbuffer,0,sizeof(NtCreateThreadExBuffer));
DWORD temp1 = 0;
DWORD temp2 = 0;
ntbuffer.Size = sizeof(NtCreateThreadExBuffer);
ntbuffer.Unknown1 = 0x10003;
ntbuffer.Unknown2 = 0x8;
ntbuffer.Unknown3 = &temp2;
ntbuffer.Unknown4 = 0;
ntbuffer.Unknown5 = 0x10004;
ntbuffer.Unknown6 = 4;
ntbuffer.Unknown7 = &temp1;
ntbuffer.Unknown8 = 0;
NTSTATUS status = funNtCreateThreadEx(
&hThread,
0x1FFFFF,
NULL,
hProcess,
(LPTHREAD_START_ROUTINE)pfnThreadRtn,
pszLibFileRemote,
FALSE, //start instantly
NULL,
NULL,
NULL,
&ntbuffer
);
created = (hThread != NULL);
ShowMessage("Inj from ntdll: " + IntToStr((int)created));
}
}*/
if (!created) // Create a remote thread that calls LoadLibraryW(DLLPathname)
hThread = CreateRemoteThread(hProcess, NULL, 0, pfnThreadRtn, pszLibFileRemote, 0, NULL);
if (hThread == NULL)
{
if (pszLibFileRemote != NULL)
VirtualFreeEx(hProcess, pszLibFileRemote, 0, MEM_RELEASE);
if (hProcess != NULL)
CloseHandle(hProcess);
//ShowMessage("Thrd error: " + IntToStr((int)GetLastError()));
MessageBox(Handle, "Remote thread can't be created.", "Error", MB_OK);
return;
}
// Wait for the remote thread to terminate
WaitForSingleObject(hThread, INFINITE);
}
__finally
{ // Now, we can clean everthing up
// Free the remote memory that contained the DLL's pathname
if (pszLibFileRemote != NULL)
VirtualFreeEx(hProcess, pszLibFileRemote, 0, MEM_RELEASE);
if (hThread != NULL)
CloseHandle(hThread);
if (hProcess != NULL)
CloseHandle(hProcess);
}
CloseHandle(hp);
}
else
{
bt_refreshClick(bt_refresh);
//MessageBox(Handle, "UO process not found!", "Error", MB_OK);
return;
}
Sleep(500);
dllWindow = FindWindow(("FPSModWindow_" + IntToHex((int)hWnd, 8)).t_str(), NULL);
}
l_status->Font->Color = clPurple;
l_status->Caption = "Unknown patch";
bt_patch->Caption = "Set patch";
if (dllWindow != 0)
SendMessage(dllWindow, FPSPATCH_INFO_REQUEST, (DWORD)Handle, 0);
}
//---------------------------------------------------------------------------
void __fastcall TFPSPatcher::bt_patchClick(TObject *Sender)
{
if (lb_client_list->ItemIndex == -1)
{
ShowMessage("No selected client.");
return;
}
HWND hWnd = m_client_list[lb_client_list->ItemIndex];
HWND dllWindow = FindWindow(("FPSModWindow_" + IntToHex((int)hWnd, 8)).t_str(), NULL);
if (dllWindow == 0)
{
ShowMessage("FPS patch window is not found!");
return;
}
if (bt_patch->Caption == "Set patch")
SendMessage(dllWindow, FPSPATCH_ENABLE, (DWORD)Handle, 0);
else
SendMessage(dllWindow, FPSPATCH_DISABLE, (DWORD)Handle, 0);
}
//---------------------------------------------------------------------------