-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpatch.cpp
More file actions
1224 lines (1036 loc) · 39.6 KB
/
Copy pathpatch.cpp
File metadata and controls
1224 lines (1036 loc) · 39.6 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
/**
* EU5 Patcher - Enable Achievements Unconditionally
*
* Location strategy:
* CanGetAchievements string -> registration xref -> callback -> predicate
* Branch-2 -> checksum callee
* IsGameRuleEnabled string -> registration xref -> callback
*
* Patch #1/#2/#3/#4 all replace the target function entry with:
* mov eax, 1
* ret
*
* Compile (MSVC): cl /std:c++17 /O2 /EHsc patch.cpp
*/
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <optional>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <tuple>
#include <unordered_set>
#include <utility>
#include <vector>
#ifdef _WIN32
#define NOMINMAX
#include <windows.h>
#pragma comment(lib, "Advapi32.lib")
#endif
namespace fs = std::filesystem;
namespace
{
constexpr std::string_view CAN_GET_ACHIEVEMENTS_ANCHOR = "CanGetAchievements";
constexpr std::string_view IS_GAME_RULE_ENABLED_ANCHOR = "IsGameRuleEnabled";
constexpr std::array<uint8_t, 6> RETURN_TRUE = {
0xB8, 0x01, 0x00, 0x00, 0x00, 0xC3};
constexpr std::array<uint32_t, 6> CHECKSUM_STATE_OFFSETS = {
0x130, 0x131, 0x132, 0x133, 0x134, 0x139};
constexpr size_t CHECKSUM_MIN_STATE_FIELDS = 4;
constexpr size_t CHECKSUM_PATCHED_MIN_STATE_FIELDS = 3;
constexpr std::string_view EU5_PATH = "eu5.exe";
constexpr std::string_view GAME_FOLDER = "Europa Universalis V";
constexpr std::string_view APP_ID = "3450310";
constexpr std::string_view EU5_BACKUP_SUFFIX = ".backup";
bool debug_info = false;
struct PatchError : std::runtime_error
{
using std::runtime_error::runtime_error;
};
struct PESection
{
std::string name;
uint32_t rva{};
uint32_t virtual_size{};
uint32_t raw_offset{};
uint32_t raw_size{};
[[nodiscard]] uint64_t raw_rva_end() const
{
return static_cast<uint64_t>(rva) + raw_size;
}
[[nodiscard]] bool contains_rva(uint64_t value) const
{
const uint64_t size = std::max<uint64_t>(virtual_size, raw_size);
return value >= rva && value < static_cast<uint64_t>(rva) + size;
}
[[nodiscard]] bool contains_raw_offset(size_t value) const
{
return value >= raw_offset &&
value < static_cast<uint64_t>(raw_offset) + raw_size;
}
};
struct RuntimeFunctionRange
{
uint32_t begin{};
uint32_t end{};
};
struct RipTarget
{
uint32_t instruction_rva{};
uint32_t target_rva{};
};
struct Rel32Target
{
uint32_t instruction_rva{};
uint32_t target_rva{};
uint8_t opcode{};
};
struct ChecksumScore
{
size_t score{};
std::set<uint32_t> fields;
};
struct PatchJob
{
std::string label;
size_t offset{};
uint32_t rva{};
};
[[nodiscard]] uint16_t read_u16(const std::vector<uint8_t> &data, size_t offset)
{
if (offset + sizeof(uint16_t) > data.size())
throw PatchError("Unexpected end of file while reading PE data.");
uint16_t value{};
std::memcpy(&value, data.data() + offset, sizeof(value));
return value;
}
[[nodiscard]] uint32_t read_u32(const std::vector<uint8_t> &data, size_t offset)
{
if (offset + sizeof(uint32_t) > data.size())
throw PatchError("Unexpected end of file while reading PE data.");
uint32_t value{};
std::memcpy(&value, data.data() + offset, sizeof(value));
return value;
}
[[nodiscard]] int32_t read_i32(const std::vector<uint8_t> &data, size_t offset)
{
if (offset + sizeof(int32_t) > data.size())
throw PatchError("Unexpected end of file while reading x64 displacement.");
int32_t value{};
std::memcpy(&value, data.data() + offset, sizeof(value));
return value;
}
[[nodiscard]] bool is_checksum_state_offset(uint32_t value)
{
return std::find(CHECKSUM_STATE_OFFSETS.begin(),
CHECKSUM_STATE_OFFSETS.end(),
value) != CHECKSUM_STATE_OFFSETS.end();
}
[[nodiscard]] std::optional<std::vector<uint8_t>> read_file(const fs::path &filepath)
{
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
if (!file)
return std::nullopt;
const auto size = file.tellg();
if (size < 0)
return std::nullopt;
file.seekg(0, std::ios::beg);
std::vector<uint8_t> buffer(static_cast<size_t>(size));
if (!buffer.empty() &&
!file.read(reinterpret_cast<char *>(buffer.data()),
static_cast<std::streamsize>(buffer.size())))
{
return std::nullopt;
}
return buffer;
}
[[nodiscard]] bool write_file(const fs::path &filepath,
const std::vector<uint8_t> &data)
{
std::ofstream file(filepath, std::ios::binary | std::ios::trunc);
if (!file)
return false;
if (!data.empty())
{
file.write(reinterpret_cast<const char *>(data.data()),
static_cast<std::streamsize>(data.size()));
}
return file.good();
}
[[nodiscard]] bool create_backup(const fs::path &source, const fs::path &dest)
{
std::error_code ec;
fs::copy_file(source, dest, fs::copy_options::overwrite_existing, ec);
return !ec;
}
#ifdef _WIN32
[[nodiscard]] std::optional<std::wstring> read_registry_string(
HKEY root, const wchar_t *subkey, const wchar_t *value_name)
{
HKEY hKey = nullptr;
if (RegOpenKeyExW(root, subkey, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
return std::nullopt;
DWORD type = 0;
DWORD size = 0;
if (RegQueryValueExW(hKey, value_name, nullptr, &type, nullptr, &size) != ERROR_SUCCESS ||
type != REG_SZ)
{
RegCloseKey(hKey);
return std::nullopt;
}
std::wstring buffer(size / sizeof(wchar_t), L'\0');
if (RegQueryValueExW(hKey, value_name, nullptr, nullptr,
reinterpret_cast<LPBYTE>(buffer.data()), &size) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return std::nullopt;
}
RegCloseKey(hKey);
while (!buffer.empty() && buffer.back() == L'\0')
buffer.pop_back();
return buffer;
}
#endif
[[nodiscard]] std::optional<fs::path> get_steam_install_path()
{
#ifdef _WIN32
if (auto primary = read_registry_string(
HKEY_LOCAL_MACHINE,
L"SOFTWARE\\WOW6432Node\\Valve\\Steam",
L"InstallPath"))
{
return fs::path(*primary);
}
if (auto fallback = read_registry_string(
HKEY_CURRENT_USER,
L"Software\\Valve\\Steam",
L"SteamPath"))
{
return fs::path(*fallback);
}
#else
const char *home = std::getenv("HOME");
if (home)
{
const std::vector<fs::path> candidates = {
fs::path(home) / ".local/share/Steam",
fs::path(home) / ".steam/steam",
fs::path(home) / ".var/app/com.valvesoftware.Steam/.steam/steam",
};
for (const auto &p : candidates)
{
if (fs::is_directory(p))
return p;
}
}
#endif
return std::nullopt;
}
[[nodiscard]] std::string trim(const std::string &s)
{
const auto first = s.find_first_not_of(" \t\n\r");
if (first == std::string::npos)
return {};
const auto last = s.find_last_not_of(" \t\n\r");
return s.substr(first, last - first + 1);
}
[[nodiscard]] std::vector<std::string> extract_quoted_tokens(const std::string &line)
{
std::vector<std::string> tokens;
size_t pos = 0;
while (true)
{
const auto start = line.find('"', pos);
if (start == std::string::npos)
break;
const auto end = line.find('"', start + 1);
if (end == std::string::npos)
break;
tokens.push_back(line.substr(start + 1, end - start - 1));
pos = end + 1;
}
return tokens;
}
[[nodiscard]] std::vector<fs::path> find_all_steam_libraries_with_app(
const fs::path &vdf_path,
std::string_view target_appid)
{
std::vector<fs::path> results;
std::ifstream file(vdf_path);
if (!file)
return results;
std::string current_path;
bool in_apps_block = false;
std::string line;
while (std::getline(file, line))
{
const auto trimmed = trim(line);
const auto tokens = extract_quoted_tokens(trimmed);
if (tokens.empty())
{
if (in_apps_block && trimmed == "}")
in_apps_block = false;
continue;
}
if (!in_apps_block)
{
if (tokens[0] == "path" && tokens.size() >= 2)
{
current_path = tokens[1];
continue;
}
if (tokens[0] == "apps")
in_apps_block = true;
continue;
}
if (tokens[0] == target_appid && !current_path.empty())
results.emplace_back(current_path);
}
return results;
}
[[nodiscard]] std::optional<fs::path> get_game_folder(std::string_view name)
{
const auto steam_path = get_steam_install_path();
if (!steam_path)
return std::nullopt;
const auto library_db = *steam_path / "steamapps" / "libraryfolders.vdf";
const auto library_paths = find_all_steam_libraries_with_app(library_db, APP_ID);
for (const auto &library_path : library_paths)
{
const auto game_folder =
library_path / "steamapps" / "common" / std::string(name);
const auto binary = game_folder / "binaries" / EU5_PATH;
if (fs::is_regular_file(binary))
return game_folder;
}
return std::nullopt;
}
[[nodiscard]] std::optional<fs::path> locate_eu5()
{
const fs::path local_path{EU5_PATH};
if (fs::exists(local_path))
return local_path;
if (const auto game_folder = get_game_folder(GAME_FOLDER))
{
const auto steam_path = *game_folder / "binaries" / EU5_PATH;
if (fs::exists(steam_path))
return steam_path;
}
return std::nullopt;
}
[[nodiscard]] std::vector<PESection> parse_pe_sections(const std::vector<uint8_t> &data)
{
if (data.size() < 0x40 || data[0] != 'M' || data[1] != 'Z')
throw PatchError("Target is not a valid PE image (missing MZ header).");
const uint32_t pe_offset = read_u32(data, 0x3C);
if (static_cast<uint64_t>(pe_offset) + 24 > data.size() ||
data[pe_offset] != 'P' || data[pe_offset + 1] != 'E' ||
data[pe_offset + 2] != 0 || data[pe_offset + 3] != 0)
{
throw PatchError("Target is not a valid PE image (missing PE header).");
}
const uint16_t number_of_sections = read_u16(data, pe_offset + 6);
const uint16_t optional_header_size = read_u16(data, pe_offset + 20);
const uint64_t section_table =
static_cast<uint64_t>(pe_offset) + 24 + optional_header_size;
std::vector<PESection> sections;
sections.reserve(number_of_sections);
for (uint16_t i = 0; i < number_of_sections; ++i)
{
const uint64_t off64 = section_table + static_cast<uint64_t>(i) * 40;
if (off64 + 40 > data.size())
throw PatchError("PE section table is truncated.");
const size_t off = static_cast<size_t>(off64);
char name_buf[9]{};
std::memcpy(name_buf, data.data() + off, 8);
PESection section;
section.name = name_buf;
section.virtual_size = read_u32(data, off + 8);
section.rva = read_u32(data, off + 12);
section.raw_size = read_u32(data, off + 16);
section.raw_offset = read_u32(data, off + 20);
sections.push_back(std::move(section));
}
return sections;
}
[[nodiscard]] const PESection &get_section(const std::vector<PESection> §ions,
std::string_view name)
{
const auto it = std::find_if(
sections.begin(), sections.end(),
[name](const PESection §ion)
{ return section.name == name; });
if (it == sections.end())
throw PatchError("PE section '" + std::string(name) + "' was not found.");
return *it;
}
[[nodiscard]] std::optional<std::reference_wrapper<const PESection>>
try_get_section(const std::vector<PESection> §ions, std::string_view name)
{
const auto it = std::find_if(
sections.begin(), sections.end(),
[name](const PESection §ion)
{ return section.name == name; });
if (it == sections.end())
return std::nullopt;
return std::cref(*it);
}
[[nodiscard]] uint32_t raw_offset_to_rva(const std::vector<PESection> §ions,
size_t offset)
{
for (const auto §ion : sections)
{
if (section.contains_raw_offset(offset))
{
const uint64_t rva = static_cast<uint64_t>(section.rva) +
(offset - section.raw_offset);
if (rva > UINT32_MAX)
break;
return static_cast<uint32_t>(rva);
}
}
std::ostringstream oss;
oss << "File offset 0x" << std::hex << offset
<< " does not belong to a PE section.";
throw PatchError(oss.str());
}
[[nodiscard]] size_t rva_to_raw_offset(const std::vector<PESection> §ions,
uint32_t rva)
{
for (const auto §ion : sections)
{
if (rva >= section.rva)
{
const uint64_t delta = static_cast<uint64_t>(rva) - section.rva;
if (delta < section.raw_size)
return static_cast<size_t>(section.raw_offset + delta);
}
}
std::ostringstream oss;
oss << "RVA 0x" << std::hex << rva << " does not map to raw file data.";
throw PatchError(oss.str());
}
[[nodiscard]] std::vector<uint32_t> find_string_rvas(
const std::vector<uint8_t> &data,
const std::vector<PESection> §ions,
std::string_view anchor)
{
std::vector<uint8_t> needle(anchor.begin(), anchor.end());
needle.push_back(0);
std::vector<uint32_t> rvas;
auto begin = data.begin();
while (begin != data.end())
{
const auto it = std::search(begin, data.end(), needle.begin(), needle.end());
if (it == data.end())
break;
const size_t offset = static_cast<size_t>(std::distance(data.begin(), it));
try
{
rvas.push_back(raw_offset_to_rva(sections, offset));
}
catch (const PatchError &)
{
// Ignore matching bytes outside mapped PE sections.
}
begin = it + 1;
}
if (rvas.empty())
throw PatchError("String anchor '" + std::string(anchor) + "' was not found.");
return rvas;
}
[[nodiscard]] std::vector<RipTarget> iter_rip_lea_targets(
const std::vector<uint8_t> &data,
const PESection &text,
const std::array<uint8_t, 3> &opcode,
uint32_t start_rva,
uint32_t end_rva)
{
const uint64_t clipped_start = std::max<uint64_t>(start_rva, text.rva);
const uint64_t clipped_end = std::min<uint64_t>(end_rva, text.raw_rva_end());
std::vector<RipTarget> results;
if (clipped_start >= clipped_end)
return results;
size_t start_off = text.raw_offset +
static_cast<size_t>(clipped_start - text.rva);
size_t end_off = text.raw_offset +
static_cast<size_t>(clipped_end - text.rva);
end_off = std::min(end_off, data.size());
for (size_t pos = start_off; pos + 7 <= end_off; ++pos)
{
if (data[pos] != opcode[0] || data[pos + 1] != opcode[1] ||
data[pos + 2] != opcode[2])
{
continue;
}
const int32_t disp = read_i32(data, pos + 3);
const uint64_t insn_rva64 = static_cast<uint64_t>(text.rva) +
(pos - text.raw_offset);
const int64_t target = static_cast<int64_t>(insn_rva64) + 7 + disp;
if (target < 0 || target > UINT32_MAX)
continue;
results.push_back({static_cast<uint32_t>(insn_rva64),
static_cast<uint32_t>(target)});
}
return results;
}
[[nodiscard]] std::vector<RuntimeFunctionRange> parse_runtime_function_ranges(
const std::vector<uint8_t> &data,
const std::vector<PESection> §ions)
{
const auto pdata_opt = try_get_section(sections, ".pdata");
if (!pdata_opt)
return {};
const auto &pdata = pdata_opt->get();
const size_t section_end = std::min<size_t>(
data.size(), static_cast<uint64_t>(pdata.raw_offset) + pdata.raw_size);
std::vector<RuntimeFunctionRange> ranges;
for (size_t off = pdata.raw_offset; off + 12 <= section_end; off += 12)
{
const uint32_t begin = read_u32(data, off);
const uint32_t end = read_u32(data, off + 4);
if (begin != 0 && end > begin)
ranges.push_back({begin, end});
}
return ranges;
}
[[nodiscard]] RuntimeFunctionRange function_range_for_rva(
uint32_t rva,
const PESection &text,
const std::vector<RuntimeFunctionRange> &runtime_ranges,
uint32_t fallback_before = 0x20,
uint32_t fallback_after = 0x100)
{
std::optional<RuntimeFunctionRange> best;
for (const auto &range : runtime_ranges)
{
if (range.begin <= rva && rva < range.end)
{
if (!best || (range.end - range.begin) < (best->end - best->begin))
best = range;
}
}
if (best)
return *best;
const uint64_t begin =
rva > fallback_before ? static_cast<uint64_t>(rva) - fallback_before : text.rva;
const uint64_t end = static_cast<uint64_t>(rva) + fallback_after;
return {
static_cast<uint32_t>(std::max<uint64_t>(text.rva, begin)),
static_cast<uint32_t>(std::min<uint64_t>(text.raw_rva_end(), end))};
}
[[nodiscard]] std::vector<RipTarget> find_anchor_xrefs(
const std::vector<uint8_t> &data,
const PESection &text,
const std::vector<uint32_t> &anchor_rvas)
{
const std::unordered_set<uint32_t> anchors(anchor_rvas.begin(), anchor_rvas.end());
std::vector<RipTarget> results;
// lea rdx, [rip + disp32]
for (const auto &item : iter_rip_lea_targets(
data, text, {0x48, 0x8D, 0x15}, text.rva,
static_cast<uint32_t>(text.raw_rva_end())))
{
if (anchors.count(item.target_rva))
results.push_back(item);
}
return results;
}
[[nodiscard]] uint32_t find_registered_callback(
const std::vector<uint8_t> &data,
const PESection &text,
const std::vector<RuntimeFunctionRange> &runtime_ranges,
uint32_t string_xref_rva,
uint32_t string_rva)
{
const auto range = function_range_for_rva(
string_xref_rva, text, runtime_ranges, 0x20, 0x180);
std::vector<RipTarget> candidates;
for (const auto &item : iter_rip_lea_targets(
data, text, {0x48, 0x8D, 0x15}, string_xref_rva + 7, range.end))
{
if (item.target_rva != string_rva && text.contains_rva(item.target_rva))
candidates.push_back(item);
}
if (candidates.empty())
{
std::ostringstream oss;
oss << "Could not resolve registered callback after string xref at RVA 0x"
<< std::hex << string_xref_rva << '.';
throw PatchError(oss.str());
}
std::sort(candidates.begin(), candidates.end(),
[](const RipTarget &a, const RipTarget &b)
{
return a.instruction_rva < b.instruction_rva;
});
if (debug_info && candidates.size() > 1)
{
std::cout << "Warning: multiple callback candidates after RVA 0x"
<< std::hex << string_xref_rva << ": ";
for (size_t i = 0; i < candidates.size(); ++i)
{
if (i)
std::cout << ", ";
std::cout << "0x" << candidates[i].target_rva;
}
std::cout << std::dec << '\n';
}
return candidates.front().target_rva;
}
[[nodiscard]] uint32_t resolve_can_get_achievements_predicate(
const std::vector<uint8_t> &data,
const PESection &text,
const std::vector<RuntimeFunctionRange> &runtime_ranges,
uint32_t callback_rva)
{
const auto range = function_range_for_rva(
callback_rva, text, runtime_ranges, 0, 0x60);
std::vector<RipTarget> candidates;
// lea rcx, [rip + disp32]
for (const auto &item : iter_rip_lea_targets(
data, text, {0x48, 0x8D, 0x0D}, callback_rva, range.end))
{
if (text.contains_rva(item.target_rva))
candidates.push_back(item);
}
if (candidates.empty())
{
std::ostringstream oss;
oss << "Could not resolve inner CanGetAchievements predicate from callback RVA 0x"
<< std::hex << callback_rva << '.';
throw PatchError(oss.str());
}
std::sort(candidates.begin(), candidates.end(),
[](const RipTarget &a, const RipTarget &b)
{
return a.instruction_rva < b.instruction_rva;
});
if (debug_info && candidates.size() > 1)
{
std::cout << "Warning: multiple predicate candidates in callback RVA 0x"
<< std::hex << callback_rva << ": ";
for (size_t i = 0; i < candidates.size(); ++i)
{
if (i)
std::cout << ", ";
std::cout << "0x" << candidates[i].target_rva;
}
std::cout << std::dec << '\n';
}
return candidates.front().target_rva;
}
[[nodiscard]] std::vector<Rel32Target> iter_rel32_control_targets(
const std::vector<uint8_t> &data,
const PESection &text,
uint32_t start_rva,
uint32_t end_rva)
{
const uint64_t clipped_start = std::max<uint64_t>(start_rva, text.rva);
const uint64_t clipped_end = std::min<uint64_t>(end_rva, text.raw_rva_end());
std::vector<Rel32Target> results;
if (clipped_start >= clipped_end)
return results;
size_t start_off = text.raw_offset +
static_cast<size_t>(clipped_start - text.rva);
size_t end_off = text.raw_offset +
static_cast<size_t>(clipped_end - text.rva);
end_off = std::min(end_off, data.size());
for (size_t pos = start_off; pos + 5 <= end_off; ++pos)
{
const uint8_t opcode = data[pos];
if (opcode != 0xE8 && opcode != 0xE9)
continue;
const int32_t disp = read_i32(data, pos + 1);
const uint64_t insn_rva64 = static_cast<uint64_t>(text.rva) +
(pos - text.raw_offset);
const int64_t target = static_cast<int64_t>(insn_rva64) + 5 + disp;
if (target < 0 || target > UINT32_MAX)
continue;
const auto target_rva = static_cast<uint32_t>(target);
if (text.contains_rva(target_rva))
{
results.push_back({static_cast<uint32_t>(insn_rva64),
target_rva,
opcode});
}
}
return results;
}
[[nodiscard]] bool is_return_true_at_rva(
const std::vector<uint8_t> &data,
const std::vector<PESection> §ions,
uint32_t target_rva)
{
try
{
const size_t offset = rva_to_raw_offset(sections, target_rva);
if (offset + RETURN_TRUE.size() > data.size())
return false;
return std::equal(RETURN_TRUE.begin(), RETURN_TRUE.end(), data.begin() + offset);
}
catch (const PatchError &)
{
return false;
}
}
[[nodiscard]] ChecksumScore checksum_structure_score(
const std::vector<uint8_t> &data,
const std::vector<PESection> §ions,
const PESection &text,
const std::vector<RuntimeFunctionRange> &runtime_ranges,
uint32_t candidate_rva)
{
const auto range = function_range_for_rva(
candidate_rva, text, runtime_ranges, 0, 0x180);
size_t start_off{};
size_t end_off{};
try
{
start_off = rva_to_raw_offset(sections, range.begin);
if (range.end <= range.begin)
return {};
end_off = rva_to_raw_offset(sections, range.end - 1) + 1;
}
catch (const PatchError &)
{
return {};
}
end_off = std::min(end_off, data.size());
std::set<uint32_t> found;
// Match local instances of: 80 /7 [base+disp32], 00
// and score accesses to the checksum state neighborhood.
for (size_t pos = start_off; pos + 7 <= end_off; ++pos)
{
if (data[pos] != 0x80)
continue;
const uint8_t modrm = data[pos + 1];
const uint8_t mod = (modrm >> 6) & 0x3;
const uint8_t reg = (modrm >> 3) & 0x7;
if (mod != 0x2 || reg != 0x7 || data[pos + 6] != 0)
continue;
const uint32_t disp = read_u32(data, pos + 2);
if (is_checksum_state_offset(disp))
found.insert(disp);
}
return {found.size(), std::move(found)};
}
[[nodiscard]] std::pair<uint32_t, uint32_t> resolve_branch2_and_checksum(
const std::vector<uint8_t> &data,
const std::vector<PESection> §ions,
const PESection &text,
const std::vector<RuntimeFunctionRange> &runtime_ranges,
const std::vector<uint32_t> &predicates)
{
std::unordered_set<uint32_t> runtime_starts;
for (const auto &range : runtime_ranges)
runtime_starts.insert(range.begin);
struct Match
{
uint32_t branch_rva{};
uint32_t checksum_rva{};
size_t score{};
std::set<uint32_t> fields;
uint32_t instruction_rva{};
};
std::vector<Match> matches;
for (const uint32_t predicate_rva : predicates)
{
const auto range = function_range_for_rva(
predicate_rva, text, runtime_ranges, 0, 0x220);
std::unordered_set<uint32_t> seen_targets;
for (const auto &item : iter_rel32_control_targets(
data, text, range.begin, range.end))
{
if (item.target_rva == predicate_rva ||
!seen_targets.insert(item.target_rva).second)
{
continue;
}
if (!runtime_starts.empty() && !runtime_starts.count(item.target_rva))
continue;
const auto score = checksum_structure_score(
data, sections, text, runtime_ranges, item.target_rva);
const size_t min_score =
is_return_true_at_rva(data, sections, item.target_rva)
? CHECKSUM_PATCHED_MIN_STATE_FIELDS
: CHECKSUM_MIN_STATE_FIELDS;
if (score.score >= min_score)
{
matches.push_back({predicate_rva,
item.target_rva,
score.score,
score.fields,
item.instruction_rva});
}
}
}
if (debug_info)
{
for (const auto &match : matches)
{
std::cout << "Checksum candidate: branch=0x" << std::hex
<< match.branch_rva << ", call/jmp@0x"
<< match.instruction_rva << " -> 0x"
<< match.checksum_rva << ", score=" << std::dec
<< match.score << ", fields=";
bool first = true;
for (const auto field : match.fields)
{
if (!first)
std::cout << ',';
std::cout << "0x" << std::hex << field;
first = false;
}
std::cout << std::dec << '\n';
}
}
using Pair = std::pair<uint32_t, uint32_t>;
std::map<Pair, Match> unique_pairs;
for (const auto &match : matches)
{
const Pair key{match.branch_rva, match.checksum_rva};
const auto it = unique_pairs.find(key);
if (it == unique_pairs.end() || match.score > it->second.score)
unique_pairs[key] = match;
}
if (unique_pairs.size() != 1)
{
std::ostringstream oss;
oss << "Could not uniquely resolve Branch-2 -> checksum from local call structure; candidates: ";
if (unique_pairs.empty())
{
oss << "none";
}
else
{
bool first = true;
for (const auto &[key, value] : unique_pairs)
{
(void)value;
if (!first)
oss << ", ";
oss << "branch 0x" << std::hex << key.first
<< " -> checksum 0x" << key.second;
first = false;
}
}
oss << '.';
throw PatchError(oss.str());
}
return unique_pairs.begin()->first;
}
void add_return_true_job(std::vector<PatchJob> &jobs,
const std::vector<uint8_t> &data,
const std::vector<PESection> §ions,
std::string label,
uint32_t target_rva)
{
const size_t offset = rva_to_raw_offset(sections, target_rva);
if (offset + RETURN_TRUE.size() > data.size())
throw PatchError("Patch target extends past the end of the file.");
if (is_return_true_at_rva(data, sections, target_rva))
{
std::cout << label << ": already patched successfully at RVA 0x"
<< std::hex << target_rva << std::dec << ".\n";
return;
}
jobs.push_back({std::move(label), offset, target_rva});