/
Program.cs
721 lines (621 loc) · 37.2 KB
/
Program.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
namespace D2ROffline
{
class Program : NativeMethods
{
static void Main(string[] args)
{
string version = "v2.0.4-beta";
string d2rPath = "Game.exe";
// NOTE: if you are going to copy & modify this then please atleast write my name correct!
PrintASCIIArt(); // 'colored' logo
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($" _____ _______ _______ _______ _ _ ____" +
$"___ ______ \n {version.PadRight(16)} |_____] |_____| | | |_" +
$"____| |______ |_____/ \n______________________________________________________________| " +
$" | | | |_____ | | |______ | \\_ \n");
Console.ForegroundColor = ConsoleColor.Gray;
// overwrite path if args are set
if (args.Length > 0)
d2rPath = args[0];
if (!File.Exists(d2rPath))
{
ConsolePrint($"Error, {d2rPath} does not exist!", ConsoleColor.Red);
ConsolePrint("Usage: D2ROffline.exe PATH_TO_GAMEDOTEXE", ConsoleColor.White);
ConsolePrint("Press any key to exit...", ConsoleColor.Yellow);
Console.ReadKey();
return;
}
ConsolePrint("Launching game...");
var pInfo = new ProcessStartInfo(d2rPath);
//if (args.Length != 1)
// pInfo.Arguments = args[1];
//else
// ConsolePrint("Extra parameters not found. Proceeding...", ConsoleColor.DarkYellow);
var d2r = Process.Start(pInfo);
// wait for proc to properly enter userland to bypass first few anti-cheating checks
ConsolePrint("Process started...");
IntPtr hProcess = OpenProcess(ProcessAccessFlags.PROCESS_ALL_ACCESS, false, d2r.Id);
ConsolePrint("Opening process...");
if (hProcess == IntPtr.Zero)
{
ConsolePrint("Failed on OpenProcess. Handle is invalid.", ConsoleColor.Red);
ConsolePrint("Press any key to exit...", ConsoleColor.Yellow);
Console.ReadKey();
return;
}
// pre setup
WaitForData(hProcess, d2r.MainModule.BaseAddress, 0x22D8858);
Thread.Sleep(20); // NOTE: patch before this to get language working, may causes crash?
// suspend process
ConsolePrint("Suspending process...");
NtSuspendProcess(hProcess);
ConsolePrint("Process suspended");
if (VirtualQueryEx(hProcess, d2r.MainModule.BaseAddress, out MEMORY_BASIC_INFORMATION basicInformation, Marshal.SizeOf(typeof(MEMORY_BASIC_INFORMATION))) == 0)
{
ConsolePrint("Failed on VirtualQueryEx. Return is 0 bytes.", ConsoleColor.Red);
ConsolePrint("Press any key to exit...", ConsoleColor.Yellow);
Console.ReadKey();
return;
}
IntPtr regionBase = basicInformation.baseAddress;
IntPtr regionSize = basicInformation.regionSize;
ConsolePrint("Remapping process..");
IntPtr addr = RemapMemoryRegion(hProcess, regionBase, regionSize.ToInt32(), MemoryProtectionConstraints.PAGE_EXECUTE_READWRITE);
ConsolePrint("Resuming process..");
NtResumeProcess(hProcess);
CloseHandle(hProcess);
ConsolePrint("Done!", ConsoleColor.Green);
ConsolePrint("Press any key to exit...", ConsoleColor.Yellow);
Console.ReadKey();
}
private static void PrintASCIIArt()
{
// print chars individual to set color
// NOTE: thanks @pairofdocs for ASCII art (https://github.com/ferib/D2R-Offline/pull/4)
string Diablo2ASCII = " ..\n .,. .*,\n ,. ./ (((#(* ,\n *(*, */#%#/*, */. ..\n .* *##(#%((//. .. ,. ./#*. .,. *((/.\n , *(%%#%(/*. .,. .**,.**. . ,(%%%#(%(. *%#*\n ... ./ &%%%/*,./(* ,/(///((/*. ,. .*#%#%%%(#%&#*\n ,,,. .*%.%%%%#%(/**#%##%#* /. ,/(. *,.,*****(#%%%%%&,%/. .,*,\n ,/(*,. ,/& /&%%%%%%%%%%#((((#/ ,%#((##%##%%%%%%%&, #* ,*((*,.\n **%%#/**,,. .*% .%((* ./(% *%/ ..,/#%#/,,.\n ,(%%%%#((#%((/(*. ., */#& .%(/ ,*(* #, .,*(%&&&%%#(,,.\n ., (& / (/* .,/*. ,*## .,(#(* .*/# %* .., ,*#( ##/,\n .*#, &(**(%& &/, /(#(**,. *(% ,,**#(*/. .,*(%*(,,, /, ,*/#, ./# .%(**/% &(/\n , ((, &/,,/##& &* .*& .*(,. ,/% .,///% .%// ,(* ,,,**#// (/ ,((###%, *# ,%#/,. ,(% &(,\n *((, &/,, *#(% #( ,/& *(*, /& ,./( /%&. &* .,(. %# .(, %* .(# &(/ *% @#(#*.. ,#. #,\n ,/#, &/,.,(#& #(. .,(% */*. .*& .*( &##& %/, *(. %%, &,. # *(# &%*(,, /% /#(#/, ,#( #,\n , (%, &/, ..*#, %* .,(& *#*, ./& ,#. &(/# /(, ./#. @& %* (. /(# @&&#,#. ,(, &##*. ./%. //.\n ./#&. &%#%%@ &*. ,(((((#/, *(& */**/**/% &* ***,,/,,*. /. /((((((%(. *(. &%/*/#%, *(.\n.*#& @(, */& #. %, *% /* */%. ,#*\n, (* @%#####((/*, ., ,*#& # &*.. ./ %, ,/(#&%%#/,\n,#(, .(///(%# ((,/.*#. ,,( %*\n .((%#%%, /%%(/* .(& &##/,\n ./#&&&&&&&&&&&&&&&&&&%%*, *&%%%%%%%%%%%%%%%%%%%&( ~ Ferib Hellscream";
Dictionary<char, ConsoleColor> colorMapping = new Dictionary<char, ConsoleColor>()
{
{ '.', ConsoleColor.Yellow },
{ ',', ConsoleColor.Yellow },
{ '*', ConsoleColor.Yellow },
{ '%', ConsoleColor.DarkYellow },
{ '#', ConsoleColor.DarkYellow },
{ '@', ConsoleColor.DarkYellow },
{ '/', ConsoleColor.DarkYellow },
{ '(', ConsoleColor.DarkYellow },
{ '&', ConsoleColor.DarkYellow },
};
ConsoleColor oldf = Console.ForegroundColor;
ConsoleColor oldb = Console.BackgroundColor;
ConsoleColor lastColor = oldf;
string buffer = "";
for (int i = 0; i < Diablo2ASCII.Length; i++)
{
ConsoleColor currentColor;
if (colorMapping.TryGetValue(Diablo2ASCII[i], out currentColor))
{
if (currentColor != lastColor)
{
lastColor = colorMapping[Diablo2ASCII[i]];
Console.Write(buffer);
buffer = "";
Console.ForegroundColor = lastColor;
}
}
buffer += Diablo2ASCII[i];
}
Console.WriteLine(buffer);
Console.ForegroundColor = ConsoleColor.Black;
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.ForegroundColor = oldf;
Console.BackgroundColor = oldb;
}
private static void ConsolePrint(string str, ConsoleColor color = ConsoleColor.Gray)
{
ConsoleColor old = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss.fffff")}]: {str}");
Console.ForegroundColor = old;
}
//public static IntPtr RemapMemoryRegion(IntPtr processHandle, IntPtr baseAddress, int regionSize, MemoryProtectionConstraints mapProtection, uint mainThreadId)
public static IntPtr RemapMemoryRegion(IntPtr processHandle, IntPtr baseAddress, int regionSize, MemoryProtectionConstraints mapProtection)
{
IntPtr addr = VirtualAllocEx(processHandle, IntPtr.Zero, regionSize, MemoryAllocationType.MEM_COMMIT | MemoryAllocationType.MEM_RESERVE, mapProtection);
if (addr == IntPtr.Zero)
return IntPtr.Zero;
IntPtr copyBuf = VirtualAlloc(IntPtr.Zero, regionSize, MemoryAllocationType.MEM_COMMIT | MemoryAllocationType.MEM_RESERVE, mapProtection);
IntPtr copyBufEx = VirtualAllocEx(processHandle, IntPtr.Zero, regionSize, MemoryAllocationType.MEM_COMMIT | MemoryAllocationType.MEM_RESERVE, MemoryProtectionConstraints.PAGE_EXECUTE_READWRITE);
byte[] copyBuf2 = new byte[regionSize];
if (!ReadProcessMemory(processHandle, baseAddress, copyBuf, regionSize, out IntPtr bytes))
return IntPtr.Zero;
if (!ReadProcessMemory(processHandle, baseAddress, copyBuf2, regionSize, out bytes))
return IntPtr.Zero;
IntPtr sectionHandle = default;
long sectionMaxSize = regionSize;
Ntstatus status = NtCreateSection(ref sectionHandle, AccessMask.SECTION_ALL_ACCESS, IntPtr.Zero, ref sectionMaxSize, MemoryProtectionConstraints.PAGE_EXECUTE_READWRITE, SectionProtectionConstraints.SEC_COMMIT, IntPtr.Zero);
if (status != Ntstatus.STATUS_SUCCESS)
return IntPtr.Zero;
status = NtUnmapViewOfSection(processHandle, baseAddress);
if (status != Ntstatus.STATUS_SUCCESS)
return IntPtr.Zero;
IntPtr viewBase = baseAddress;
long sectionOffset = default;
uint viewSize = 0;
status = NtMapViewOfSection(sectionHandle,
processHandle,
ref viewBase,
UIntPtr.Zero,
regionSize,
ref sectionOffset,
ref viewSize,
2,
0,
MemoryProtectionConstraints.PAGE_EXECUTE_READWRITE);
if (status != Ntstatus.STATUS_SUCCESS)
return IntPtr.Zero;
if (!WriteProcessMemory(processHandle, viewBase, copyBuf, (int)viewSize, out bytes))
return IntPtr.Zero;
if (!WriteProcessMemory(processHandle, copyBufEx, copyBuf, (int)viewSize, out bytes))
return IntPtr.Zero;
// apply all request patches
ApplyAllPatches(processHandle, baseAddress);
//crc32 bypass
//search for F2 ?? 0F 38 F1 - F2 REX.W 0F 38 F1 /r CRC32 r64, r/m64 RM Valid N.E. Accumulate CRC32 on r/m64.
byte[] AoBpattern = { 0xF2, 0x42, 0x0F, 0x38, 0xF1 };
for (long i = 0; i < regionSize; i++)
{
bool isMatch = true;
for (long j = 0; j < AoBpattern.Length; j++)
{
if (!(copyBuf2[i + j] == AoBpattern[j] || j == 1))
{
isMatch = false;
break;
}
}
if (isMatch)
detourCRC(processHandle, (long)baseAddress + i, (long)baseAddress, (long)copyBufEx);
}
// NOTE: uncomment if you want to snitch a hook inside the .text before it remaps back from RWX to RX
#if DEBUG
ConsolePrint("Patching complete..");
ConsolePrint("[!] Press any key to remap and resume proces...", ConsoleColor.Yellow);
Console.ReadKey();
#endif
// remap
status = NtUnmapViewOfSection(processHandle, baseAddress);
if (status != Ntstatus.STATUS_SUCCESS)
return IntPtr.Zero;
status = NtMapViewOfSection(sectionHandle,
processHandle,
ref viewBase,
UIntPtr.Zero,
regionSize,
ref sectionOffset,
ref viewSize,
2,
0,
MemoryProtectionConstraints.PAGE_EXECUTE_READ);
if (status != Ntstatus.STATUS_SUCCESS)
return IntPtr.Zero;
if (!VirtualFree(copyBuf, 0, MemFree.MEM_RELEASE))
return IntPtr.Zero;
return addr;
}
private static void WaitForData(IntPtr processHandle, IntPtr baseAddress, int offset)
{
// now waiting for game to lock in inf loop
ConsolePrint($"Waiting for data at 0x{(baseAddress + offset).ToString("X8")}...");
int count = 0;
while (count < 500) // 5000ms timeout
{
byte[] buff = new byte[3];
if (!ReadProcessMemory(processHandle, baseAddress + offset, buff, buff.Length, out _)) // pre
{
ConsolePrint("Failed reading initial process memory", ConsoleColor.Red);
continue; // dont break?
}
if (buff[0] != 0x00 || buff[2] != 0x00)
break;
Thread.Sleep(10); // continue execution
count++;
}
}
private static void ApplyAllPatches(IntPtr processHandle, IntPtr baseAddress)
{
// NOTE: you can make a 'patches.txt' file, using the format '0x1234:9090' where 0x1234 indicates the offset (game.exe+0x1234) and 9090 indicates the patch value (nop nop)
string patchesContent = "";
if (File.Exists("patches.txt"))
patchesContent = File.ReadAllText("patches.txt");
if (patchesContent == "")
{
ConsolePrint("WARNING: Not patches are beeing loaded. (If this is unexpected, double check your 'patches.txt' file!)", ConsoleColor.Yellow);
return;
}
string[] split = patchesContent.Split('\n');
int[] addr = new int[split.Length];
byte[][] patch = new byte[split.Length][];
// init arrays
for (int i = 0; i < split.Length; i++)
{
string[] data = split[i].Split(':');
if (data.Length < 2)
continue; // probs empty line
addr[i] = Convert.ToInt32(data[0], 0x10);
if (addr[i] == 0)
continue;
if (data[1][0] == '+')
{
// offset patch
string offset = data[1].Substring(1);
//byte[] buf = new byte[offset.Length / 2]; // amount of bytes in patch len?
byte[] buf = new byte[8]; // qword
if(!ReadProcessMemory(processHandle, IntPtr.Add(baseAddress, addr[i]), buf, buf.Length, out _))
{
ConsolePrint("Error, failed read patch location!", ConsoleColor.Yellow);
continue; // non critical, just skip
}
patch[i] = BitConverter.GetBytes(BitConverter.ToInt64(buf, 0) + Convert.ToInt64(offset, 0x10));
}
else
{
// normal patch
patch[i] = new byte[data[1].Length / 2];
for (int j = 0; j < patch[i].Length; j++)
patch[i][j] = Convert.ToByte(data[1].Substring(j * 2, 2), 0x10);
}
}
// patch arrays
for (int i = 0; i < split.Length; i++)
{
if (addr[i] == 0)
continue;
if(patch[i] == null)
{
ConsolePrint($"Invalid patch at line {i+1}!", ConsoleColor.Yellow);
continue;
}
ConsolePrint($"Patching 0x{(baseAddress+addr[i]).ToString("X8")}");
if (!WriteProcessMemory(processHandle, IntPtr.Add(baseAddress, addr[i]), patch[i], patch[i].Length, out IntPtr bWritten1) || (int)bWritten1 != patch[i].Length)
ConsolePrint($"Patch {i} failed!!", ConsoleColor.Red);
}
}
public static bool detourCRC(IntPtr processHandle, long crcLocation, long wowBase, long wowCopyBase)
{
#region asmCave
//stuff that goes in the .text section
byte[] crcDetour =
{
0x50, //push rax
0x48, 0xB8, 0xEF, 0xEE, 0xEE, 0xEE, 0xEE, 0xBE, 0xAD, 0xDE, //mov rax, CaveAddr (0x03)
0xFF, 0xD0, //call rax
0x58, //pop rax
0x90 //nop
};
byte[] crcDetourRegOffsets = { 0x00, 0x02, 0x0C, 0x0D }; //regiser offsets (may need to change when register is used in code)
//stuff that goes in new allocated section
byte[] crcCave =
{
0x51, //push rcx
0x48, 0xB9, 0xEF, 0xEE, 0xEE, 0xEE, 0xEE, 0xBE, 0xAD, 0xDE, //mov rcx, imgBase (0x03)
0x48, 0x39, 0xCF, //cmp r2, rcx - 0x0B
0x7C, 0x38, //jl crc
0x50, //push rax
0x48, 0x8B, 0xC1, //mov rax, rcx
0x8B, 0x89, 0x58, 0x02, 0x00, 0x00, //mov ecx, [r1+0x258] // .text Raw Size
0x90,
0x48, 0x01, 0xC1, //add rcx,rax
0x8B, 0x80, 0x54, 0x02, 0x00, 0x00, //mov eax,[rax+0x254] // .text Virtual Address
0x90,
0x48, 0x01, 0xC1, //add rcx,rax
0x58, //pop rax
0x48, 0x39, 0xCF, //cmp r2, rcx - 0x29
0x7F, 0x1A, //jg crc
// TODO: update codecave with assembly below (and offset crcCaveRegInstructOffsets offsets)
// psuh rax
// mov eax, rcx
// mov ecx, [r1+0x280] // .rdata Raw Size
// nop
// add rcx, rax
// mov eax, [rax+0x27C] // .rdata Virtual Address
// nop
// add rcx, rax
// pop rax
// cmp r2, rcx
0x48, 0xB9, 0xEF, 0xEE, 0xEE, 0xEE, 0xEE, 0xBE, 0xAD, 0xDE, //mov rcx, imgBase (0x30)
0x48, 0x29, 0xCF, //sub r2, rcx - 0x38
0x48, 0xB9, 0xEF, 0xEE, 0xEE, 0xEE, 0xEE, 0xBE, 0xAD, 0xDE, //mov rcx, imgCopyBase (0x3D)
0x48, 0x01, 0xCF, //add r2, rcx - 0x45
0x59, //pop rcx
//crc: //crc location start
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, //+ 0x47
0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, // NOP's as placeholder for the 15-19 bytes
0x90, 0x90, 0x90,
//crc //crc location end
0xC3 //ret
};
byte[] crcCaveRegInstructOffsets = { 0x0B, 0x29, 0x38, 0x45 }; //register offsets (may need to change when register is used in code)
#endregion asmCave
IntPtr CaveAddr = VirtualAllocEx(processHandle, IntPtr.Zero, crcCave.Length, MemoryAllocationType.MEM_COMMIT, MemoryProtectionConstraints.PAGE_EXECUTE_READWRITE);
if (CaveAddr == IntPtr.Zero)
{
ConsolePrint("VirtualAlloxEx error", ConsoleColor.Red);
return false;
}
byte[] splitCaveAddr = BitConverter.GetBytes(CaveAddr.ToInt64()); //write CaveAddr to crcDetour buffer
byte[] splitWowBase = BitConverter.GetBytes(wowBase); //write imgBase to crcCave buffer
byte[] splitWowCopyBase = BitConverter.GetBytes(wowCopyBase); //write imgCopyBase to crcCave buffer
//replace the beef (placeholders)
for (int i = 0; i < 8; i++)
{
crcDetour[0x03 + i] = splitCaveAddr[i]; //CaveAdr
crcCave[0x03 + i] = splitWowBase[i]; //imgBase
crcCave[0x30 + i] = splitWowBase[i]; //imgBase
crcCave[0x3D + i] = splitWowCopyBase[i]; //imgCopyBase (aka Game_2.exe)
}
//obtain crc instructions
byte[] crcBuffer = new byte[88];
if (!ReadProcessMemory(processHandle, (IntPtr)crcLocation, crcBuffer, crcBuffer.Length, out IntPtr bRead))
{
ConsolePrint("Reading CRC location failed", ConsoleColor.Red);
return false;
}
bool isJmpFound = false;
int origCrcInstructionLength = -1;
for (int i = 0; i < crcCave.Length - 0x49; i++)
{
//jb is the last instruction and starts with 0x72 (2 bytes long)
crcCave[0x49 + i] = crcBuffer[i]; //write byte to codecave
if (crcBuffer[i] == 0x72)
{
crcCave[0x49 + i + 1] = crcBuffer[i + 1]; //include last byte of JB instruction before breaking
origCrcInstructionLength = i + 2; //Keep track of bytes used to NOP later
isJmpFound = true;
break;
}
}
if (!isJmpFound)
{
ConsolePrint("NOPE", ConsoleColor.Red);
return false;
}
//list used registers rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi
bool[] usedRegs = { false, false, false, false, false, false, false, false }; //rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi
//check byte code to find used stuff
usedRegs[(crcBuffer[0x05] - 0x04) / 8] = true; //x,[reg+reg*8]
usedRegs[(crcBuffer[0x09] - 0xC0)] = true; //inc x
if (crcBuffer[0x0C] >= 0xC0 && crcBuffer[0x0C] < 0xC8)
usedRegs[(crcBuffer[0x0C] - 0xC0)] = true; // cmp ?, x
byte selectReg = 0;
for (byte r = 0; r < usedRegs.Length; r++)
{
if (usedRegs[r] == false)
{
selectReg = r;
break;
}
}
//change Detour register to non-used register
for (int i = 0; i < crcDetourRegOffsets.Length; i++)
{
crcDetour[crcDetourRegOffsets[i]] += selectReg; //increase byte to set selected register
}
//Change the register(r2) used to calc crc32
for (int i = 0; i < crcCaveRegInstructOffsets.Length; i++)
{
crcCave[crcCaveRegInstructOffsets[i] + 0] = crcBuffer[0x01]; //copy
crcCave[crcCaveRegInstructOffsets[i] + 2] = crcBuffer[0x06]; //copy
if (crcCave[crcCaveRegInstructOffsets[i] + 0] != 0x48) //check if register is extra register (r8 - r15)
{
crcCave[crcCaveRegInstructOffsets[i] + 0] = 0x49; //set to extra register type
crcCave[crcCaveRegInstructOffsets[i] + 2] = (byte)(0xC8 + (crcBuffer[0x06] - 0xC0) % 8); //set second reg to rcx and fix first reg
}
else
crcCave[crcCaveRegInstructOffsets[i] + 2] += 8; //inc to fix basic registers
}
//add nops to end of the detour buffer
byte[] crcDetourFixed = new byte[origCrcInstructionLength];
for (int i = 0; i < origCrcInstructionLength; i++)
{
if (i < crcDetour.Length)
{
//Copy byte from crcDetour to fixed crcDetour
crcDetourFixed[i] = crcDetour[i];
}
else
{
//add NOPs
crcDetourFixed[i] = 0x90;
}
}
if (!WriteProcessMemory(processHandle, (IntPtr)(crcLocation), crcDetourFixed, crcDetourFixed.Length, out IntPtr bWrite))
{
ConsolePrint("Writing CRC detour failed", ConsoleColor.Red);
return false;
}
if (!WriteProcessMemory(processHandle, CaveAddr, crcCave, crcCave.Length, out bWrite))
{
ConsolePrint("Writing CRC CodeCave failed", ConsoleColor.Red);
return false;
}
ConsolePrint($"Bypassed CRC at {crcLocation.ToString("X")}"); // to {CaveAddr.ToString("X")}");
return true;
}
public static bool detourCRC_Experimental(IntPtr processHandle, long crcLocation, long wowBase, long wowCopyBase)
{
#region asmCave
//stuff that goes in the .text section
byte[] crcDetour =
{
0x50, //push rax
0x48, 0xB8, 0xEF, 0xEE, 0xEE, 0xEE, 0xEE, 0xBE, 0xAD, 0xDE, //mov rax, CaveAddr (0x03)
0xFF, 0xD0, //call rax
0x58, //pop rax
0x90 //nop
};
byte[] crcDetourRegOffsets = { 0x00, 0x02, 0x0C, 0x0D }; //regiser offsets (may need to change when register is used in code)
//stuff that goes in new allocated section
byte[] crcCave =
{
0x51, // push rcx
//if_text:
0x48, 0xB9, 0xEF, 0xEE, 0xEE, 0xEE, 0xEE, 0xBE, 0xAD, 0xDE, // mov rcx, imgBase (0x03)
0x48, 0x39, 0xCF, // cmp r2, rcx - 0x0B
0x7C, 0x1E, // jl if_rdata
0x50, // push rax
0x48, 0x8B, 0xC1, // mov rax, rcx
0x8B, 0x89, 0x58, 0x02, 0x00, 0x00, // mov ecx, [r1+0x258] // .text Raw Size
0x90,
0x48, 0x01, 0xC1, // add rcx,rax
0x8B, 0x80, 0x54, 0x02, 0x00, 0x00, // mov eax,[rax+0x254] // .text Virtual Address
0x90,
0x48, 0x01, 0xC1, // add rcx,rax
0x58, // pop rax
0x48, 0x39, 0xCF, // cmp r2, rcx - 0x29
0x76, 0x2B, // jbe swap_crc
// TODO: update codecave with assembly below (and offset crcCaveRegInstructOffsets offsets)
//if_rdata:
0x48, 0xB9, 0xEF, 0xEE, 0xEE, 0xEE, 0xEE, 0xBE, 0xAD, 0xDE, // mov rcx, imgBase (0x30)
0x48, 0x39, 0xCF, // cmp r2, rcx - 0x3A
0x50, // push rax
0x48, 0x8B, 0xC1, // mov eax, rcx
0x8B, 0x89, 0x80, 0x02, 0x00, 0x00, // mov ecx, [r1+0x280] // .rdata Raw Size
0x90, // nop
0x48, 0x01, 0xC1, // add rcx, rax
0x8B, 0x80, 0x7C, 0x02, 0x00, 0x00, // mov eax, [rax+0x27C] // .rdata Virtual Address
0x90, // nop
0x48, 0x01, 0xC1, // add rcx, rax
0x58, // pop rax
0x48, 0x39, 0xC8, // cmp r2, rcx - 0x
0x7F, 0x1A, // jg normal_crc
//swap_crc:
0x48, 0xB9, 0xEF, 0xEE, 0xEE, 0xEE, 0xEE, 0xBE, 0xAD, 0xDE, // mov rcx, imgBase (0x5B)
0x48, 0x29, 0xCF, // sub r2, rcx - 0x65
0x48, 0xB9, 0xEF, 0xEE, 0xEE, 0xEE, 0xEE, 0xBE, 0xAD, 0xDE, // mov rcx, imgCopyBase (0x68)
0x48, 0x01, 0xCF, // add r2, rcx - 0x45
0x59, // pop rcx
//normal_crc: // crc location start
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, //+ 0x76
0x90, 0x90, 0x90,
0x90, 0x90, 0x90, 0x90, 0x90, // NOP's as placeholder for the 15-19 bytes
0x90, 0x90, 0x90,
//crc // crc location end
0xC3 // ret
};
byte[] crcCaveRegInstructOffsets = { 0x0B, 0x29, 0x3A, 0x65, 0x72 }; //register offsets (may need to change when register is used in code)
#endregion asmCave
IntPtr CaveAddr = VirtualAllocEx(processHandle, IntPtr.Zero, crcCave.Length, MemoryAllocationType.MEM_COMMIT, MemoryProtectionConstraints.PAGE_EXECUTE_READWRITE);
if (CaveAddr == IntPtr.Zero)
{
ConsolePrint("VirtualAlloxEx error", ConsoleColor.Red);
return false;
}
byte[] splitCaveAddr = BitConverter.GetBytes(CaveAddr.ToInt64()); //write CaveAddr to crcDetour buffer
byte[] splitImgBase = BitConverter.GetBytes(wowBase); //write imgBase to crcCave buffer
byte[] splitImgCopyBase = BitConverter.GetBytes(wowCopyBase); //write imgCopyBase to crcCave buffer
// replace the beef(placeholders)
for (int i = 0; i < 8; i++)
{
crcDetour[0x03 + i] = splitCaveAddr[i]; //CaveAdr
crcCave[0x03 + i] = splitImgBase[i]; //imgBase
crcCave[0x30 + i] = splitImgBase[i]; //imgBase
crcCave[0x5B + i] = splitImgBase[i]; //imgBase
crcCave[0x68 + i] = splitImgCopyBase[i]; //imgCopyBase (aka Game_2.exe)
}
//obtain crc instructions
byte[] crcBuffer = new byte[88];
if (!ReadProcessMemory(processHandle, (IntPtr)crcLocation, crcBuffer, crcBuffer.Length, out IntPtr bRead))
{
ConsolePrint("Reading CRC location failed", ConsoleColor.Red);
return false;
}
bool isJmpFound = false;
int origCrcInstructionLength = -1;
for (int i = 0; i < crcCave.Length - (0x76+2); i++)
{
//jb is the last instruction and starts with 0x72 (2 bytes long)
crcCave[0x78 + i] = crcBuffer[i]; //write byte to codecave
if (crcBuffer[i] == 0x72)
{
crcCave[0x78 + i + 1] = crcBuffer[i + 1]; //include last byte of JB instruction before breaking
origCrcInstructionLength = i + 2; //Keep track of bytes used to NOP later
isJmpFound = true;
break;
}
}
if (!isJmpFound)
{
ConsolePrint("NOPE", ConsoleColor.Red);
return false;
}
//list used registers rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi
bool[] usedRegs = { false, false, false, false, false, false, false, false }; //rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi
//check byte code to find used stuff
usedRegs[(crcBuffer[0x05] - 0x04) / 8] = true; //x,[reg+reg*8]
usedRegs[(crcBuffer[0x09] - 0xC0)] = true; //inc x
if (crcBuffer[0x0C] >= 0xC0 && crcBuffer[0x0C] < 0xC8)
usedRegs[(crcBuffer[0x0C] - 0xC0)] = true; // cmp ?, x
byte selectReg = 0;
for (byte r = 0; r < usedRegs.Length; r++)
{
if (usedRegs[r] == false)
{
selectReg = r;
break;
}
}
//change Detour register to non-used register
for (int i = 0; i < crcDetourRegOffsets.Length; i++)
{
crcDetour[crcDetourRegOffsets[i]] += selectReg; //increase byte to set selected register
}
////Change the register(r2) used to calc crc32
//for (int i = 0; i < crcCaveRegInstructOffsets.Length; i++)
//{
// crcCave[crcCaveRegInstructOffsets[i] + 0] = crcBuffer[0x01]; //copy
// crcCave[crcCaveRegInstructOffsets[i] + 2] = crcBuffer[0x06]; //copy
// if (crcCave[crcCaveRegInstructOffsets[i] + 0] != 0x48) //check if register is extra register (r8 - r15)
// {
// crcCave[crcCaveRegInstructOffsets[i] + 0] = 0x49; //set to extra register type
// crcCave[crcCaveRegInstructOffsets[i] + 2] = (byte)(0xC8 + (crcBuffer[0x06] - 0xC0) % 8); //set second reg to rcx and fix first reg
// }
// else
// crcCave[crcCaveRegInstructOffsets[i] + 2] += 8; //inc to fix basic registers
//}
//add nops to end of the detour buffer
byte[] crcDetourFixed = new byte[origCrcInstructionLength];
for (int i = 0; i < origCrcInstructionLength; i++)
{
if (i < crcDetour.Length)
{
//Copy byte from crcDetour to fixed crcDetour
crcDetourFixed[i] = crcDetour[i];
}
else
{
//add NOPs
crcDetourFixed[i] = 0x90;
}
}
if (!WriteProcessMemory(processHandle, (IntPtr)(crcLocation), crcDetourFixed, crcDetourFixed.Length, out IntPtr bWrite))
{
ConsolePrint("Writing CRC detour failed", ConsoleColor.Red);
return false;
}
if (!WriteProcessMemory(processHandle, CaveAddr, crcCave, crcCave.Length, out bWrite))
{
ConsolePrint("Writing CRC CodeCave failed", ConsoleColor.Red);
return false;
}
ConsolePrint($"Bypassed CRC at {crcLocation.ToString("X")}"); // to {CaveAddr.ToString("X")}");
return true;
}
}
}