forked from praydog/REFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegrityCheckBypass.cpp
More file actions
1854 lines (1490 loc) · 80.8 KB
/
IntegrityCheckBypass.cpp
File metadata and controls
1854 lines (1490 loc) · 80.8 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
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <unordered_set>
#include <iomanip>
#include <regex>
#include "utility/Module.hpp"
#include "utility/Scan.hpp"
#include "sdk/RETypeDB.hpp"
#include "Hooks.hpp"
#include "IntegrityCheckBypass.hpp"
struct IntegrityCheckPattern {
std::string pat{};
uint32_t offset{};
};
std::shared_ptr<IntegrityCheckBypass> s_integrity_check_bypass_instance{nullptr};
std::shared_ptr<IntegrityCheckBypass>& IntegrityCheckBypass::get_shared_instance() {
if (!s_integrity_check_bypass_instance) {
s_integrity_check_bypass_instance = std::make_unique<IntegrityCheckBypass>();
}
return s_integrity_check_bypass_instance;
}
std::optional<std::string> IntegrityCheckBypass::on_initialize() {
// Patterns for assigning or accessing of the integrity check boolean (RE3)
// and for jumping past the integrity checks (RE8)
// In RE8, the integrity checks cause a noticeable stutter as well.
std::vector<IntegrityCheckPattern> possible_patterns {
#ifdef RE3
/*
cmp qword ptr [rax+18h], 0
cmovz ecx, r15d
mov cs:bypass_integrity_checks, cl*/
// Referenced above "steam_api64.dll"
{"48 ? ? 18 00 41 ? ? ? 88 0D ? ? ? ?", 11},
{"48 ? ? 18 00 0F ? ? 88 0D ? ? ? ? 49 ? ? ? 48", 10},
#elif defined(RE8)
/*
These are partially obfuscated and are within protected sections.
The ja jumps past the checksum checks which cause very large stutters if they are ran.
We'll replace the ja to always jump past the checksum checks.
There are various patterns here because the code is obfuscated, there's an element of randomness per update.
Lots of random junk code. Some instructions are obfuscated into multiple instructions as well.
We're taking a shot in the dark here hoping that the obfuscated code
stays generally the same past a game update.
*/
/*
sub eax, ecx
ja NO_CHECKSUM_CHECKS1
mov eax, [rsp+whatever]
*/
// app.PlayerCore.onDamage, app.EnemyCore.onDie2 (onDie2 gets called from onDie)
{"29 c8 0f 87 ? ? ? ? 8b 84", 2},
/*
sub eax, ecx
ja NO_CHECKSUM_CHECKS2
. xor eax, eax
sub eax, [rsp+whatever]
*/
// app.PlayerCore.onDamage #2
{"29 c8 0f 87 ? ? ? ? 31 C0 2B", 2},
/*
mov eax, [rsp+whatever]
sub eax, ecx
ja NO_CHECKSUM_CHECKS3
xor eax, eax
*/
// app.PlayerCore.onDamage #3, app.EnemyCore.onDie2 #2
{"8b 84 ? ? ? ? ? 29 c8 0f 87 ? ? ? ?", 9},
/*
There is another one inside of app.GlobalService.msgSceneTransition_afterDeactivate
but didn't bother to patch it out. Reason being that it seems to only get called when loading is finished.
Maybe some more investigation is required here?
*/
// The above function names can be found within il2cpp_dump.json, which is dumped with REFramework's "Dump SDK" button in developer mode.
#endif
};
std::unordered_set<uintptr_t> already_patched{};
const auto module_size = *utility::get_module_size(g_framework->get_module().as<HMODULE>());
const auto module_end = g_framework->get_module() + module_size;
for (auto& possible_pattern : possible_patterns) {
spdlog::info("Scanning for {}", possible_pattern.pat);
auto integrity_check_ref = utility::scan(g_framework->get_module().as<HMODULE>(), possible_pattern.pat);
if (!integrity_check_ref) {
continue;
}
#if defined(RE3)
m_bypass_integrity_checks = (bool*)utility::calculate_absolute(*integrity_check_ref + possible_pattern.offset);
#elif defined(RE8)
ignore_application_entries();
while (integrity_check_ref) {
const auto ja_instruction = *integrity_check_ref + possible_pattern.offset;
if (already_patched.contains(ja_instruction)) {
spdlog::info("IntegrityCheckBypass: ja instruction at 0x{:X} already patched, continuing...", ja_instruction);
integrity_check_ref =
utility::scan(*integrity_check_ref + 1, module_end - (*integrity_check_ref + 1), possible_pattern.pat);
continue;
}
// Create a ja->jmp patch for bypassing the integrity check
std::vector<uint8_t> patch_bytes{0xE9, 0x00, 0x00, 0x00, 0x00, 0x90};
// Overwrite the target address with the original ja target. Add 1 byte because the new instruction is smaller.
*(uint32_t*)&patch_bytes[1] = *(uint32_t*)(ja_instruction + 2) + 1;
// Convert the uint8_t patch_bytes to int16_t vector
std::vector<int16_t> patch_int16_bytes{};
for (auto& patch_byte : patch_bytes) {
patch_int16_bytes.push_back(patch_byte);
}
// Log the patch address (ja_instruction) and bytes with spdlog
spdlog::info("Patch address: 0x{:X}", ja_instruction);
// Convert patch_bytes to hex string with stringstream and then log the string with spdlog
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (auto& patch_byte : patch_bytes) {
ss << std::setw(2) << (int)patch_byte << " ";
}
spdlog::info("Patch bytes: {}", ss.str());
// Patch the bytes
m_patches.emplace_back(Patch::create(ja_instruction, patch_int16_bytes));
already_patched.emplace(ja_instruction);
// Search for the next integrity check using the same pattern
integrity_check_ref = utility::scan(*integrity_check_ref + 1, module_end - (*integrity_check_ref + 1), possible_pattern.pat);
}
// If we didn't find any integrity checks
if (m_patches.empty()) {
spdlog::info("Could not find any integrity checks to bypass!");
}
#endif
}
// These may be removed, so don't fail altogether
/*if (m_bypass_integrity_checks == nullptr) {
return "Failed to find IntegrityCheckBypass pattern";
}*/
#ifdef RE3
spdlog::info("[{:s}]: bypass_integrity_checks: {:x}", get_name().data(), (uintptr_t)m_bypass_integrity_checks);
#endif
#ifdef MHRISE
// this is pretty much what it was like finding this, you just gotta look a little closer!
const auto very_cool_type = sdk::find_type_definition_by_fqn(0x83f09f47);
static std::vector<Patch::Ptr> very_cool_patches{};
auto find_method_by_hash = [](sdk::RETypeDefinition* t, size_t hash) -> sdk::REMethodDefinition* {
for (auto& method : t->get_methods()) {
if (utility::hash(method.get_name()) == hash) {
return &method;
}
}
return nullptr;
};
if (very_cool_type != nullptr) {
auto patch_very_cool_method = [&](size_t hash) {
const auto method = find_method_by_hash(very_cool_type, hash);
if (method == nullptr) {
spdlog::error("Could not find very cool method", hash);
return false;
}
if (method->get_function() == nullptr) {
spdlog::error("[{:s}]: Could not find very_cool_type::very_cool_method!", get_name().data());
return false;
}
spdlog::info("[{:s}]: Patching very cool method!", get_name().data());
very_cool_patches.emplace_back(Patch::create((uintptr_t)method->get_function(), { 0xB0, 0x00, 0xC3 }, true));
return true;
};
patch_very_cool_method(0x21c27632fa7ba29b);
patch_very_cool_method(0x49b943a462e8cf6a);
} else {
spdlog::error("[{:s}]: Could not find very_cool_type!", get_name().data());
}
const auto very_awesome_type = sdk::find_type_definition_by_fqn(0xce04a0c6);
if (very_awesome_type != nullptr) {
const auto very_awesome_method = find_method_by_hash(very_awesome_type, 0x9f79221341cfcb18);
if (very_awesome_method != nullptr) {
if (very_awesome_method->get_function() == nullptr) {
spdlog::error("[{:s}]: Could not find very_awesome_type::very_awesome_method!", get_name().data());
return Mod::on_initialize();
}
const auto very_awesome_call = utility::scan_opcode((uintptr_t)very_awesome_method->get_function(), 10, 0xE8);
if (!very_awesome_call) {
spdlog::error("[{:s}]: Could not find very_awesome_call!", get_name().data());
return Mod::on_initialize();
}
const auto real_awesome_function = utility::calculate_absolute(*very_awesome_call + 1);
if (real_awesome_function == 0) {
spdlog::error("[{:s}]: Could not find real_awesome_function!", get_name().data());
return Mod::on_initialize();
}
spdlog::info("[{:s}]: Patching very awesome method!", get_name().data());
very_cool_patches.emplace_back(Patch::create(real_awesome_function, { 0xB0, 0x00, 0xC3 }, true));
} else {
spdlog::error("[{:s}]: Could not find very_awesome_method!", get_name().data());
}
} else {
spdlog::error("[{:s}]: Could not find very_awesome_type!", get_name().data());
}
#endif
s_patch_count_checked = false;
spdlog::info("Done.");
return Mod::on_initialize();
}
void IntegrityCheckBypass::on_frame() {
#ifdef RE3
if (m_bypass_integrity_checks != nullptr) {
*m_bypass_integrity_checks = true;
}
#endif
#ifdef RE8
// These three are responsible for various stutters and
// gameplay altering effects e.g. not being able to interact with objects
disable_update_timers("app.InteractManager");
disable_update_timers("app.EnemyManager");
disable_update_timers("app.GUIManager");
disable_update_timers("app.HIDManager");
disable_update_timers("app.FadeManager");
#endif
}
#ifdef RE8
void IntegrityCheckBypass::disable_update_timers(std::string_view name) const {
// get the singleton correspdonding to the given name
auto manager = sdk::get_managed_singleton<::REManagedObject>(name);
// If the interact manager is null, we're probably not in the game
if (manager == nullptr || manager->info == nullptr || manager->info->classInfo == nullptr) {
return;
}
// Get the sdk::RETypeDefinition of the manager
auto t = utility::re_managed_object::get_type_definition(manager);
if (t == nullptr) {
return;
}
// Get the update timer fields, which are responsible for disabling interactions (for app.InteractManager)
// if the integrity checks are triggered
auto update_timer_enable_field = t->get_field("UpdateTimerEnable");
auto update_timer_late_enable_field = t->get_field("LateUpdateTimerEnable");
// Get the actual field data now within the manager
if (update_timer_enable_field != nullptr) {
auto& update_timer_enable = update_timer_enable_field->get_data<bool>(manager, true);
// Log that we are about to set these to false if they were true before
if (update_timer_enable) {
spdlog::info("[{:s}]: {:s}.UpdateTimerEnable was true, disabling it...", get_name().data(), name.data());
}
update_timer_enable = false;
}
if (update_timer_late_enable_field != nullptr) {
auto& update_timer_late_enable = update_timer_late_enable_field->get_data<bool>(manager, true);
if (update_timer_late_enable) {
spdlog::info("[{:s}]: {:s}.LateUpdateTimerEnable was true, disabling it...", get_name().data(), name.data());
}
update_timer_late_enable = false;
}
}
#endif
void IntegrityCheckBypass::ignore_application_entries() {
Hooks::get()->ignore_application_entry(0x76b8100bec7c12c3);
Hooks::get()->ignore_application_entry(0x9f63c0fc4eea6626);
#if TDB_VER >= 73
Hooks::get()->ignore_application_entry(0x00c0ab9309584734);
Hooks::get()->ignore_application_entry(0xa474f1d3a294e6a4);
#endif
#if TDB_VER >= 74
Hooks::get()->ignore_application_entry(0x00ec4793097cd833);
Hooks::get()->ignore_application_entry(0x00d85893096c4c0c);
#endif
}
void IntegrityCheckBypass::immediate_patch_re8() {
// Apparently patching this in SF6 causes some bugs like chat not showing up and being unable to view replays.
// Disabling it for now as the game still seems to work fine without it.
#ifdef SF6
if (true) {
return;
}
#endif
#if TDB_VER < 73
// We have to immediately patch this at startup in RE8 unlike MHRise
// because the game immediately starts checking the integrity of the executable
// on the first execution of this callback, unlike MHRise which was delayed.
// So we can't use the IL2CPP metadata yet, we have to resort to
// plain old pattern scanning.
// We're essentially patching the application entries we ignored above, but immediately.
spdlog::info("[IntegrityCheckBypass]: Scanning RE8...");
const auto game = utility::get_executable();
const auto game_size = utility::get_module_size(game).value_or(0);
const auto game_end = (uintptr_t)game + game_size;
// Present in MHRise and RE8.
// sub rax, 128E329h
const uint32_t sussy_constant = 0x128E329;
std::optional<uintptr_t> sussy_result{};
bool patched_sussy1 = false;
for (sussy_result = utility::scan_data(game, (const uint8_t*)&sussy_constant, sizeof(sussy_constant));
sussy_result.has_value();
sussy_result = utility::scan_data(*sussy_result + 1, (game_end - (*sussy_result + 1)) - 0x100, (const uint8_t*)&sussy_constant, sizeof(sussy_constant)))
{
// Find the start of the instruction, given the sussy_constant is in the middle of it.
const auto resolved_instruction = utility::resolve_instruction(*sussy_result);
// If this instruction didn't get resolved, go onto the next one. We probably ran into garbage data.
if (resolved_instruction) {
const auto sussy_function_start = utility::find_function_start(resolved_instruction->addr);
if (!sussy_function_start) {
spdlog::error("[IntegrityCheckBypass]: Could not find function start for sussy_constant @ 0x{:x}", *sussy_result);
continue;
}
// Create a patch that returns instantly.
static auto patch = Patch::create(sussy_function_start.value(), { 0xC3 }, true);
patched_sussy1 = true;
spdlog::info("[IntegrityCheckBypass]: Patched sussy_function 1");
break;
}
}
if (!patched_sussy1) {
spdlog::error("[IntegrityCheckBypass]: Could not find sussy_constant usage!");
}
// Now we need to patch the second callback.
// I hope this constant isn't randomly generated by the protection!!!!!!
/*
call ProtectionTripResult
cmp eax, 1F2h
jz ...
lea rcx, ProtectionGlobalContext
call ProtectionTripResult
*/
const auto sussy_result_2 = utility::scan(game, "E8 ? ? ? ? 3D F2 01 00 00 0F 84 ? ? ? ? 48 8D 0D ? ? ? ? E8");
if (sussy_result_2) {
const auto sussy_function_start = utility::find_function_start(sussy_result_2.value());
if (sussy_function_start) {
static auto patch = Patch::create(sussy_function_start.value(), { 0xC3 }, true);
spdlog::info("[IntegrityCheckBypass]: Patched sussy_function 2");
}
} else {
spdlog::error("[IntegrityCheckBypass]: Could not find sussy_result_2!");
}
// These are embedded checks that run during startup and sometimes during loading transitions
// they get passed different indices that make it perform different behavior
// if it returns 1, the original execution flow gets altered
// and stuff like DLC loading gets skipped so it needs to always return 0
// there are really obvious constants to go off of within these functions
// but they look like they might be auto generated so can't rely on them
const auto sussy_result_3 = utility::scan(game, "8D ? 02 E8 ? ? ? ? 0F B6 C8 48 ? ? 50 48 ? ? 18 0F");
if (sussy_result_3) {
const auto func = utility::calculate_absolute(*sussy_result_3 + 4);
static auto patch = Patch::create(func, { 0xB0, 0x00, 0xC3 }, true);
spdlog::info("[IntegrityCheckBypass]: Patched sussy_function 3");
} else {
const auto sussy_result_alternative = utility::scan(game, "8D ? 05 E8 ? ? ? ? 0F B6 C8 48 ? ? 50 48 ? ? 18 0F");
if (sussy_result_alternative) {
const auto func = utility::calculate_absolute(*sussy_result_alternative + 4);
static auto patch = Patch::create(func, { 0xB0, 0x00, 0xC3 }, true);
spdlog::info("[IntegrityCheckBypass]: Patched sussy_function 3");
} else {
spdlog::error("[IntegrityCheckBypass]: Could not find sussy_result_3!");
}
}
const auto sussy_result_4 = utility::scan(game, "72 ? 41 8B ? E8 ? ? ? ? 0F B6 C8 48 ? ? 50 48 ? ? 18 0F");
if (sussy_result_4) {
const auto func = utility::calculate_absolute(*sussy_result_4 + 6);
static auto patch = Patch::create(func, { 0xB0, 0x00, 0xC3 }, true);
spdlog::info("[IntegrityCheckBypass]: Patched sussy_function 4");
} else {
spdlog::error("[IntegrityCheckBypass]: Could not find sussy_result_4!");
}
#endif
}
void IntegrityCheckBypass::immediate_patch_re4() {
// This patch fixes the constant scans that are done every frame on the game's memory.
// The scans will still be performed, but the crash will be avoided.
// Ideally, the scans should be patched as well, because they are literally running every frame
// which may cause performance issues.
// As far as I can tell, this conditional jmp jumps into a via::clr::VM cleanup routine, corrupting memory.
// This will cause the game to crash in a random location that accesses VM memory.
// This is probably to make finding the code that causes the crash in the first place harder.
spdlog::info("[IntegrityCheckBypass]: Scanning RE4...");
const auto game = utility::get_executable();
const auto conditional_jmp_block = utility::scan(game, "48 8B 8D D0 03 00 00 48 29 C1 75 ?");
if (!conditional_jmp_block) {
spdlog::error("[IntegrityCheckBypass]: Could not find conditional_jmp, trying fallback.");
// mov [rbp+192h], al
// this is used shortly after the conditional jmp, only place that uses it.
const auto unique_instruction = utility::scan(game, "88 85 92 01 00 00");
if (!unique_instruction) {
spdlog::error("[IntegrityCheckBypass]: Could not find unique_instruction!");
return;
}
// Conditional jmp is very close to the instruction, before it.
// However, this specific block of instructions is used all over the place
// so we have to use the unique instruction is a reference point to scan from.
const auto short_jmp_before = utility::scan_reverse(*unique_instruction, 0x100, "75 ? 50 F7 D0");
if (short_jmp_before) {
static auto patch = Patch::create(*short_jmp_before, { 0xEB }, true);
spdlog::info("[IntegrityCheckBypass]: Patched conditional_jmp!");
return;
}
// If we've gotten to this point, we are trying the scorched earth method of trying to obtain the function "start"
// for this giant obfuscated blob. We will get the instructions behind the unique_instruction we found by doing that,
// and look for the nearest branch instruction to patch.
spdlog::error("[IntegrityCheckBypass]: Could not find short_jmp_before, trying fallback.");
// Get the preceding instructions. If this doesn't work we'll need to scan for a common instruction anchor to scan forward from...
auto previous_instructions = utility::get_disassembly_behind(*unique_instruction);
if (previous_instructions.empty()) {
spdlog::error("[IntegrityCheckBypass]: Could not find previous_instructions!");
return;
}
// Reverse the order of the instructions.
std::reverse(previous_instructions.begin(), previous_instructions.end());
spdlog::info("[IntegrityCheckBypass]: Found {} previous instructions.", previous_instructions.size());
spdlog::info("[IntegrityCheckBypass]: Walking previous instructions...");
for (auto& insn : previous_instructions) {
if (insn.instrux.BranchInfo.IsBranch) {
spdlog::info("[IntegrityCheckBypass]: Found branch instruction, patching...");
if (insn.instrux.BranchInfo.IsFar) {
static auto patch = Patch::create(insn.addr, { 0xE9 }, true);
} else {
static auto patch = Patch::create(insn.addr, { 0xEB }, true);
}
spdlog::info("[IntegrityCheckBypass]: Patched conditional_jmp");
return;
}
}
spdlog::error("[IntegrityCheckBypass]: Could not find branch instruction to patch!");
return;
}
const auto conditional_jmp = *conditional_jmp_block + 10;
// Create a patch that always jumps.
static auto patch = Patch::create(conditional_jmp, { 0xEB }, true);
spdlog::info("[IntegrityCheckBypass]: Patched conditional_jmp!");
}
void* IntegrityCheckBypass::renderer_create_blas_hook(void* a1, void* a2, void* a3, void* a4, void* a5) {
if (s_corruption_when_zero != nullptr) {
if (*s_corruption_when_zero == 0) {
*s_corruption_when_zero = s_last_non_zero_corruption;
spdlog::info("[IntegrityCheckBypass]: Fixed corruption_when_zero!");
}
s_last_non_zero_corruption = *s_corruption_when_zero;
}
return s_renderer_create_blas_hook->get_original<decltype(renderer_create_blas_hook)>()(a1, a2, a3, a4, a5);
}
// This is used to nuke the heap allocated code that causes crashes
// when debuggers are attached and other integrity checks.
// They happen to be in the same (heap allocated) executable section, so we can just
// replace every byte with a RET instruction.
void IntegrityCheckBypass::nuke_heap_allocated_code(uintptr_t addr) {
// Get the base of the memory region.
MEMORY_BASIC_INFORMATION mbi{};
if (VirtualQuery((LPCVOID)addr, &mbi, sizeof(mbi)) == 0) {
spdlog::error("[IntegrityCheckBypass]: VirtualQuery failed!");
return;
}
// Get the end of the memory region.
const auto start = (uintptr_t)mbi.BaseAddress;
const auto end = (uintptr_t)mbi.BaseAddress + mbi.RegionSize;
spdlog::info("[IntegrityCheckBypass]: Nuking heap allocated code at 0x{:X} - 0x{:X}", start, end);
// Fix the protection of the memory region.
ProtectionOverride _{(void*)start, mbi.RegionSize, PAGE_EXECUTE_READWRITE};
// Replace every single byte with a RET (C3) instruction.
std::memset((void*)start, 0xC3, mbi.RegionSize);
spdlog::info("[IntegrityCheckBypass]: Nuked heap allocated code at 0x{:X}", start);
}
void IntegrityCheckBypass::anti_debug_watcher() try {
static const auto ntdll = GetModuleHandleW(L"ntdll.dll");
static const auto dbg_ui_remote_breakin = ntdll != nullptr ? GetProcAddress(ntdll, "DbgUiRemoteBreakin") : nullptr;
static auto original_dbg_ui_remote_breakin_bytes = dbg_ui_remote_breakin != nullptr ? utility::get_original_bytes(dbg_ui_remote_breakin) : std::optional<std::vector<uint8_t>>{};
if (dbg_ui_remote_breakin == nullptr) {
return;
}
// We can generally assume it's not hooked at this point if the original bytes are empty.
if (!original_dbg_ui_remote_breakin_bytes || original_dbg_ui_remote_breakin_bytes->empty()) {
spdlog::info("[IntegrityCheckBypass]: Manually copying original bytes for DbgUiRemoteBreakin.");
if (!original_dbg_ui_remote_breakin_bytes) {
original_dbg_ui_remote_breakin_bytes = std::vector<uint8_t>{};
}
}
if (original_dbg_ui_remote_breakin_bytes->size() < 32) {
std::copy_n((uint8_t*)dbg_ui_remote_breakin + original_dbg_ui_remote_breakin_bytes->size(), 32 - original_dbg_ui_remote_breakin_bytes->size(), std::back_inserter(*original_dbg_ui_remote_breakin_bytes));
}
const uint64_t* first_8_bytes = (uint64_t*)dbg_ui_remote_breakin;
const uint8_t* first_8_bytes_ptr = (uint8_t*)dbg_ui_remote_breakin;
if (*(uint64_t*)original_dbg_ui_remote_breakin_bytes->data() != *first_8_bytes) {
spdlog::info("[IntegrityCheckBypass]: DbgUiRemoteBreakin was hooked, restoring original bytes.");
if (first_8_bytes_ptr[0] == 0xE9) {
spdlog::info("[IntegrityCheckBypass]: DbgUiRemoteBreakin was directly hooked, resolving...");
const auto resolved_jmp = utility::calculate_absolute((uintptr_t)dbg_ui_remote_breakin + 1);
const auto is_heap_allocated = utility::get_module_within(resolved_jmp).value_or(nullptr) == nullptr;
if (is_heap_allocated && !IsBadReadPtr((void*)resolved_jmp, 32)) {
spdlog::info("[IntegrityCheckBypass]: Nuking heap allocated code at 0x{:X}", resolved_jmp);
nuke_heap_allocated_code(resolved_jmp);
}
} else if (first_8_bytes_ptr[0] == 0xFF && first_8_bytes_ptr[1] == 0x25) {
spdlog::info("[IntegrityCheckBypass]: DbgUiRemoteBreakin was indirectly hooked, resolving...");
const auto resolved_ptr = utility::calculate_absolute((uintptr_t)dbg_ui_remote_breakin + 2);
const auto resolved_jmp = *(uintptr_t*)resolved_ptr;
const auto is_heap_allocated = utility::get_module_within(resolved_jmp).value_or(nullptr) == nullptr;
if (is_heap_allocated && !IsBadReadPtr((void*)resolved_jmp, 32)) {
spdlog::info("[IntegrityCheckBypass]: Nuking heap allocated code at 0x{:X}", resolved_jmp);
nuke_heap_allocated_code(resolved_jmp);
}
}
ProtectionOverride _{dbg_ui_remote_breakin, original_dbg_ui_remote_breakin_bytes->size(), PAGE_EXECUTE_READWRITE};
std::copy(original_dbg_ui_remote_breakin_bytes->begin(), original_dbg_ui_remote_breakin_bytes->end(), (uint8_t*)dbg_ui_remote_breakin);
spdlog::info("[IntegrityCheckBypass]: Restored DbgUiRemoteBreakin.");
}
} catch (const std::exception& e) {
spdlog::error("[IntegrityCheckBypass]: Exception in anti_debug_watcher: {}", e.what());
} catch (...) {
spdlog::error("[IntegrityCheckBypass]: Unknown exception in anti_debug_watcher!");
}
void IntegrityCheckBypass::init_anti_debug_watcher() {
if (s_anti_anti_debug_thread != nullptr) {
return;
}
// Run the original watcher once so we get it at least without creating a thread first.
anti_debug_watcher();
s_anti_anti_debug_thread = std::make_unique<std::jthread>([](std::stop_token stop_token) {
spdlog::info("[IntegrityCheckBypass]: Hello from anti_debug_watcher!");
spdlog::info("[IntegrityCheckBypass]: Waiting for REFramework startup to finish...");
while (g_framework == nullptr) {
std::this_thread::yield();
}
spdlog::info("[IntegrityCheckBypass]: REFramework startup finished!");
while (!stop_token.stop_requested()) {
anti_debug_watcher();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
});
}
void IntegrityCheckBypass::pak_load_check_function(safetyhook::Context& context) {
const auto return_address = *reinterpret_cast<uintptr_t*>(context.rsp);
auto pak_name_wstr = reinterpret_cast<const wchar_t*>(context.rdx);
spdlog::info("[IntegrityCheckBypass]: pak_load_check_function called from: 0x{:X}", return_address);
spdlog::info("[IntegrityCheckBypass]: Pak name: {}", utility::narrow(pak_name_wstr));
}
void IntegrityCheckBypass::patch_version_hook(safetyhook::Context& context) {
// THEY STORE PATCH VERSION INSIDE SOMEWHERE NOW! And only load until that patch version then dont load no more paks
spdlog::info("[IntegrityCheckBypass]: patch_version_hook called!");
uint64_t current_patch_version = 0;
switch (s_patch_version_reg_index) {
case NDR_RAX: current_patch_version = context.rax; break;
case NDR_RCX: current_patch_version = context.rcx; break;
case NDR_RDX: current_patch_version = context.rdx; break;
case NDR_RBX: current_patch_version = context.rbx; break;
case NDR_RSP: current_patch_version = context.rsp; break;
case NDR_RBP: current_patch_version = context.rbp; break;
case NDR_RSI: current_patch_version = context.rsi; break;
case NDR_RDI: current_patch_version = context.rdi; break;
case NDR_R8: current_patch_version = context.r8; break;
case NDR_R9: current_patch_version = context.r9; break;
case NDR_R10: current_patch_version = context.r10; break;
case NDR_R11: current_patch_version = context.r11; break;
case NDR_R12: current_patch_version = context.r12; break;
case NDR_R13: current_patch_version = context.r13; break;
case NDR_R14: current_patch_version = context.r14; break;
case NDR_R15: current_patch_version = context.r15; break;
default: current_patch_version = context.rax; break; // fallback
}
// Scan for amount of paks. Get exe directory. To be honest set this to 9999 is okay, but i feel like it might take a long time
int file_count_result = std::max<int>(scan_patch_files_count(), current_patch_version);
switch (s_patch_version_reg_index) {
case NDR_RAX:
spdlog::info("[IntegrityCheckBypass]: Patch version: {}. Game wont load past this patch version. Setting new patch version at RAX to {}", context.rax, file_count_result);
context.rax = file_count_result;
break;
case NDR_RCX:
spdlog::info("[IntegrityCheckBypass]: Patch version: {}. Game wont load past this patch version. Setting new patch version at RCX to {}", context.rcx, file_count_result);
context.rcx = file_count_result;
break;
case NDR_RDX:
spdlog::info("[IntegrityCheckBypass]: Patch version: {}. Game wont load past this patch version. Setting new patch version at RDX to {}", context.rdx, file_count_result);
context.rdx = file_count_result;
break;
case NDR_RBX:
spdlog::info("[IntegrityCheckBypass]: Patch version: {}. Game wont load past this patch version. Setting new patch version at RBX to {}", context.rbx, file_count_result);
context.rbx = file_count_result;
break;
case NDR_RSP:
spdlog::info("[IntegrityCheckBypass]: Patch version: {}. Game wont load past this patch version. Setting new patch version at RSP to {}", context.rsp, file_count_result);
context.rsp = file_count_result;
break;
case NDR_RBP:
spdlog::info("[IntegrityCheckBypass]: Patch version: {}. Game wont load past this patch version. Setting new patch version at RBP to {}", context.rbp, file_count_result);
context.rbp = file_count_result;
break;
case NDR_RSI:
spdlog::info("[IntegrityCheckBypass]: Patch version: {}. Game wont load past this patch version. Setting new patch version at RSI to {}", context.rsi, file_count_result);
context.rsi = file_count_result;
break;
case NDR_RDI:
spdlog::info("[IntegrityCheckBypass]: Patch version: {}. Game wont load past this patch version. Setting new patch version at RDI to {}", context.rdi, file_count_result);
context.rdi = file_count_result;
break;
case NDR_R8:
spdlog::info("[IntegrityCheckBypass]: Patch version: {}. Game wont load past this patch version. Setting new patch version at R8 to {}", context.r8, file_count_result);
context.r8 = file_count_result;
break;
case NDR_R9:
spdlog::info("[IntegrityCheckBypass]: Patch version: {}. Game wont load past this patch version. Setting new patch version at R9 to {}", context.r9, file_count_result);
context.r9 = file_count_result;
break;
case NDR_R10:
spdlog::info("[IntegrityCheckBypass]: Patch version: {}. Game wont load past this patch version. Setting new patch version at R10 to {}", context.r10, file_count_result);
context.r10 = file_count_result;
break;
case NDR_R11:
spdlog::info("[IntegrityCheckBypass]: Patch version: {}. Game wont load past this patch version. Setting new patch version at R11 to {}", context.r11, file_count_result);
context.r11 = file_count_result;
break;
case NDR_R12:
spdlog::info("[IntegrityCheckBypass]: Patch version: {}. Game wont load past this patch version. Setting new patch version at R12 to {}", context.r12, file_count_result);
context.r12 = file_count_result;
break;
case NDR_R13:
spdlog::info("[IntegrityCheckBypass]: Patch version: {}. Game wont load past this patch version. Setting new patch version at R13 to {}", context.r13, file_count_result);
context.r13 = file_count_result;
break;
case NDR_R14:
spdlog::info("[IntegrityCheckBypass]: Patch version: {}. Game wont load past this patch version. Setting new patch version at R14 to {}", context.r14, file_count_result);
context.r14 = file_count_result;
break;
case NDR_R15:
spdlog::info("[IntegrityCheckBypass]: Patch version: {}. Game wont load past this patch version. Setting new patch version at R15 to {}", context.r15, file_count_result);
context.r15 = file_count_result;
break;
default:
spdlog::info("[IntegrityCheckBypass]: Unknown register, falling back to RAX for patch version: {} (update it to {})", context.rax, file_count_result);
context.rax = file_count_result; // fallback to RAX
break;
}
}
// This allows unencrypted paks to load.
void IntegrityCheckBypass::sha3_rsa_code_midhook(safetyhook::Context& context) {
spdlog::info("[IntegrityCheckBypass]: sha3_code_midhook called!");
// Log registers
spdlog::info("[IntegrityCheckBypass]: RAX: 0x{:X}", context.rax);
spdlog::info("[IntegrityCheckBypass]: RCX: 0x{:X}", context.rcx);
spdlog::info("[IntegrityCheckBypass]: RDX: 0x{:X}", context.rdx);
spdlog::info("[IntegrityCheckBypass]: R8: 0x{:X}", context.r8);
spdlog::info("[IntegrityCheckBypass]: R9: 0x{:X}", context.r9);
spdlog::info("[IntegrityCheckBypass]: R10: 0x{:X}", context.r10);
spdlog::info("[IntegrityCheckBypass]: R11: 0x{:X}", context.r11);
spdlog::info("[IntegrityCheckBypass]: R12: 0x{:X}", context.r12);
spdlog::info("[IntegrityCheckBypass]: R13: 0x{:X}", context.r13);
spdlog::info("[IntegrityCheckBypass]: R14: 0x{:X}", context.r14);
spdlog::info("[IntegrityCheckBypass]: R15: 0x{:X}", context.r15);
spdlog::info("[IntegrityCheckBypass]: RSP: 0x{:X}", context.rsp);
spdlog::info("[IntegrityCheckBypass]: RIP: 0x{:X}", context.rip);
spdlog::info("[IntegrityCheckBypass]: RBP: 0x{:X}", context.rbp);
spdlog::info("[IntegrityCheckBypass]: RSI: 0x{:X}", context.rsi);
spdlog::info("[IntegrityCheckBypass]: RDI: 0x{:X}", context.rdi);
enum PakFlags : uint8_t {
ENCRYPTED = 0x8
};
//const auto pak_flags = (PakFlags)context.rax; // Might change, maybe add automated register detection later
PakFlags pak_flags{};
switch (s_sha3_reg_index) {
case NDR_RAX:
pak_flags = (PakFlags)context.rax;
SPDLOG_INFO("[IntegrityCheckBypass]: Using RAX for pak_flags");
break;
case NDR_RCX:
pak_flags = (PakFlags)context.rcx;
SPDLOG_INFO("[IntegrityCheckBypass]: Using RCX for pak_flags");
break;
case NDR_RDX:
pak_flags = (PakFlags)context.rdx;
SPDLOG_INFO("[IntegrityCheckBypass]: Using RDX for pak_flags");
break;
case NDR_RBX:
pak_flags = (PakFlags)context.rbx;
SPDLOG_INFO("[IntegrityCheckBypass]: Using RBX for pak_flags");
break;
case NDR_RSP:
pak_flags = (PakFlags)context.rsp;
SPDLOG_INFO("[IntegrityCheckBypass]: Using RSP for pak_flags");
break;
case NDR_RBP:
pak_flags = (PakFlags)context.rbp;
SPDLOG_INFO("[IntegrityCheckBypass]: Using RBP for pak_flags");
break;
case NDR_RSI:
pak_flags = (PakFlags)context.rsi;
SPDLOG_INFO("[IntegrityCheckBypass]: Using RSI for pak_flags");
break;
case NDR_RDI:
pak_flags = (PakFlags)context.rdi;
SPDLOG_INFO("[IntegrityCheckBypass]: Using RDI for pak_flags");
break;
case NDR_R8:
pak_flags = (PakFlags)context.r8;
SPDLOG_INFO("[IntegrityCheckBypass]: Using R8 for pak_flags");
break;
case NDR_R9:
pak_flags = (PakFlags)context.r9;
SPDLOG_INFO("[IntegrityCheckBypass]: Using R9 for pak_flags");
break;
case NDR_R10:
pak_flags = (PakFlags)context.r10;
SPDLOG_INFO("[IntegrityCheckBypass]: Using R10 for pak_flags");
break;
case NDR_R11:
pak_flags = (PakFlags)context.r11;
SPDLOG_INFO("[IntegrityCheckBypass]: Using R11 for pak_flags");
break;
case NDR_R12:
pak_flags = (PakFlags)context.r12;
SPDLOG_INFO("[IntegrityCheckBypass]: Using R12 for pak_flags");
break;
case NDR_R13:
pak_flags = (PakFlags)context.r13;
SPDLOG_INFO("[IntegrityCheckBypass]: Using R13 for pak_flags");
break;
case NDR_R14:
pak_flags = (PakFlags)context.r14;
SPDLOG_INFO("[IntegrityCheckBypass]: Using R14 for pak_flags");
break;
case NDR_R15:
pak_flags = (PakFlags)context.r15;
SPDLOG_INFO("[IntegrityCheckBypass]: Using R15 for pak_flags");
break;
default:
pak_flags = (PakFlags)context.r8; // fallback to R8
SPDLOG_INFO("[IntegrityCheckBypass]: Unknown register, falling back to R8 for pak_flags");
break;
}
if ((pak_flags & PakFlags::ENCRYPTED) != 0) {
spdlog::info("[IntegrityCheckBypass]: Pak is encrypted, allowing decryption code to run.");
return;
}
context.rip = *s_sha3_code_end;
spdlog::info("[IntegrityCheckBypass]: Unencrypted pak detected, skipping decryption code!");
}
void IntegrityCheckBypass::restore_unencrypted_paks() {
spdlog::info("[IntegrityCheckBypass]: Restoring unencrypted paks...");
// If this breaks... we'll fix it!
const auto game = utility::get_executable();
std::vector<std::string> possible_patterns = {
"C5 F8 57 C0 C5 FC 11 84 24 ? ? ? ? C5 FC 11 84 24 ? ? ? ? C5 FC 11 84 24 ? ? ? ? C5 FC 11 84 24 ? ? ? ? C5 FC 11 44 24 ? 48",
"C5 F8 57 C0 C5 FC 11 84 24 ? ? ? ? C5 FC 11 84 24 ? ? ? ? C5 FC 11 84 24 ? ? ? ? C5 FC 11 84 24 ? ? ? ? C5 FC 11 84 24 ? ? ? ? 48 C1 E9 10", // MHWILDS v1.041
};
std::optional<uintptr_t> sha3_code_start;
for (const auto& pattern : possible_patterns) {
sha3_code_start = utility::scan(game, pattern);
if (sha3_code_start) {
break;
}
}
if (!sha3_code_start) {
spdlog::error("[IntegrityCheckBypass]: Could not find sha3_rsa_code_start!");
return;
}
spdlog::info("[IntegrityCheckBypass]: Found sha3_rsa_code_start @ 0x{:X}", *sha3_code_start);
s_sha3_code_end = utility::scan(game, "48 8B 8E C0 00 00 00 48 C1 E9 ?");
if (!s_sha3_code_end) {
spdlog::error("[IntegrityCheckBypass]: Could not find sha3_rsa_code_end, cannot restore unencrypted paks!");
return;
}
spdlog::info("[IntegrityCheckBypass]: Found sha3_rsa_code_end @ 0x{:X}", *s_sha3_code_end);
s_sha3_rsa_code_midhook = safetyhook::create_mid((void*)*sha3_code_start, &sha3_rsa_code_midhook);
spdlog::info("[IntegrityCheckBypass]: Created sha3_rsa_code_midhook!");
#if defined(MHWILDS) || defined(MHSTORIES3)
const auto pak_load_check_start = utility::scan(game, "41 57 41 56 41 55 41 54 56 57 55 53 48 81 EC ? ? ? ? 48 89 CE 48 8B 05 ? ? ? ? 48 31 E0 48 89 84 24 ? ? ? ? 48 8B 81 ? ? ? ? 48 C1 E8 10");
if (pak_load_check_start) {
spdlog::info("[IntegrityCheckBypass]: Found pak_load_check_function @ 0x{:X}, hook!", (uintptr_t)*pak_load_check_start);
s_pak_load_check_function_hook = safetyhook::create_mid((void*)*pak_load_check_start, &IntegrityCheckBypass::pak_load_check_function);
find_try_hook_via_file_load_win32_create_file(*pak_load_check_start);
}
const auto patch_version_start = utility::scan(game, "48 89 ? 24 ? 48 85 FF 0F 84 ? ? ? ? 66 83 3F 72 0F 85 ? ? ? ? 66 BA 72 00");
if (patch_version_start) {
// Before patching, decode the instruction at patch_version_start to find the source register of the MOV instruction
auto move_instruction = utility::decode_one((std::uint8_t*)*patch_version_start);
spdlog::info("[IntegrityCheckBypass]: Created patch_version_hook to 0x{:X}, hook!", (uintptr_t)*patch_version_start);
s_patch_version_hook = safetyhook::create_mid((void*)*patch_version_start, &IntegrityCheckBypass::patch_version_hook);
// Get the source register of the MOV instruction
if (move_instruction && move_instruction->Instruction == ND_INS_MOV && move_instruction->Operands[1].Type == ND_OP_REG) {
s_patch_version_reg_index = move_instruction->Operands[1].Info.Register.Reg;
spdlog::info("[IntegrityCheckBypass]: patch_version_reg_index set to {}", s_patch_version_reg_index);
} else {
spdlog::error("[IntegrityCheckBypass]: Could not determine patch_version_reg_index! Default to RAX");
}
}
#endif
auto previous_instructions = utility::get_disassembly_behind(*s_sha3_code_end);
auto previous_instructions_start = utility::get_disassembly_behind(*sha3_code_start);
// This NOPs out the conditional jump that rejects the PAK file if the integrity check fails.
// Normally, the game computes a SHA3-256 hash of the TOC/resource headers before obfuscation.
// It then XORs the TOC/resource headers with this hash to make them unreadable.
// To verify integrity, the game decrypts a precomputed SHA3-256 hash from the PAK header using RSA.
// If the computed SHA3-256 hash of the TOC/resource headers doesn't match the decrypted one, the PAK is rejected.
// Without patching, bypassing this check would require obtaining Capcom's private RSA key to sign new hashes,
// which is infeasible. Instead, we NOP the conditional jump to force the game to accept modded PAKs.
// (Thanks to Rick Gibbed (@gibbed) for the explanation about how SHA3 and RSA fit together in this context)
if (!previous_instructions.empty()) {
const auto& previous_instruction = previous_instructions.back();
if (previous_instruction.instrux.BranchInfo.IsBranch && previous_instruction.instrux.BranchInfo.IsConditional) {
spdlog::info("[IntegrityCheckBypass]: Found conditional branch instruction @ 0x{:X}, NOPing it", previous_instruction.addr);
// NOP out the conditional jump
std::vector<int16_t> nops{};
nops.resize(previous_instruction.instrux.Length);
std::fill(nops.begin(), nops.end(), 0x90);
static auto patch = Patch::create(previous_instruction.addr, nops, true);
spdlog::info("[IntegrityCheckBypass]: NOP'd out conditional jump!");
} else {
spdlog::warn("[IntegrityCheckBypass]: Previous instruction is not a conditional branch, cannot NOP it!");
}
}
if (!previous_instructions_start.empty()) {
// reverse
std::reverse(previous_instructions_start.begin(), previous_instructions_start.end());
// go forward until we find a test insn
for (auto& insn : previous_instructions_start) {
if (std::string_view(insn.instrux.Mnemonic).contains("TEST")) {
spdlog::info("[IntegrityCheckBypass]: Found test instruction at 0x{:X}", insn.addr);
// Check if the first operand is a register
if (insn.instrux.Operands[0].Type == ND_OP_REG && insn.instrux.Operands[0].Info.Register.Type == ND_REG_GPR) {
s_sha3_reg_index = insn.instrux.Operands[0].Info.Register.Reg;
spdlog::info("[IntegrityCheckBypass]: sha3_reg_index set to {}", s_sha3_reg_index);
break;
} else {
spdlog::error("[IntegrityCheckBypass]: First operand of test instruction is not a register!");
}
}
}