-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindOffsets.cpp
104 lines (100 loc) · 3.62 KB
/
FindOffsets.cpp
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
//ïîèñê îôôñåòîâ
#include <vector>
#include <windows.h>
#include <filesystem>
class OffsetsFinder
{
private:
BYTE* fileBytes = nullptr;
size_t fileSize = 0;
uint32_t codeOffset = 0;
uint32_t world_offset = 0;
uint32_t objects_offset = 0;
uint32_t names_offset = 0;
uint32_t pool_offset = 0;
private:
bool CompareByteArray(BYTE* data, BYTE* sig, size_t size) {
for (size_t i = 0; i < size; i++)
{
if (data[i] != sig[i]) {
if (sig[i] == 0x00) continue;
return false;
}
}
return true;
}
size_t FindSignature(std::vector<BYTE> sig, size_t start_at = 0) {
for (size_t offset = start_at; offset < fileSize - sig.size(); offset++) {
bool equals = CompareByteArray(reinterpret_cast<BYTE*>(fileBytes + offset), sig.data(), sig.size());
if (equals) {
return offset;
}
}
return 0;
}
uint32_t FindOffset(std::vector<BYTE>* sigv, uint32_t size, size_t start_at = 0) {
for (auto i = 0; i < size; i++) {
auto sig = sigv[i];
auto offset = FindSignature(sig, start_at);
if (!offset) continue;
auto k = 0;
for (; sig[k]; k++);
uint32_t nextOffset = *reinterpret_cast<uint32_t*>(fileBytes + offset + k);
return offset + k + 4 + nextOffset + codeOffset;
}
return 0;
}
public:
~OffsetsFinder() {
if (fileBytes) {
delete[] fileBytes;
}
}
size_t LoadFile(const char* path) {
auto file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (file != INVALID_HANDLE_VALUE) {
LARGE_INTEGER size;
GetFileSizeEx(file, &size);
fileSize = size.QuadPart;
fileBytes = new BYTE[fileSize];
DWORD readed;
if (!ReadFile(file, fileBytes, fileSize, &readed, 0) || readed != fileSize) {
return 0;
};
auto new_header_ptr = reinterpret_cast<PIMAGE_DOS_HEADER>(fileBytes)->e_lfanew;
auto baseOfCode = reinterpret_cast<PIMAGE_NT_HEADERS>(fileBytes + new_header_ptr)->OptionalHeader.BaseOfCode;
auto rawDataPtr = reinterpret_cast<PIMAGE_SECTION_HEADER>(fileBytes + new_header_ptr + sizeof(IMAGE_NT_HEADERS))->PointerToRawData;
codeOffset = baseOfCode - rawDataPtr;
CloseHandle(file);
}
return fileSize;
};
uint32_t GetGWorld() {
if (!world_offset) {
static std::vector<BYTE> sigv[] = { { 0x48, 0x8B, 0x1D, 0x00, 0x00, 0x00, 0x00, 0x48, 0x85, 0xDB, 0x74, 0x00, 0x41, 0xB0, 0x01 }, { 0x48, 0x8b, 0x05, 0x00, 0x00, 0x00, 0x00, 0x75, 0x1b }, {0x48, 0x8B, 0x05, 0x00, 0x00, 0x00, 0x00, 0x48, 0x8B, 0x88, 0x00, 0x00, 0x00, 0x00, 0x48, 0x85, 0xC9, 0x74, 0x06, 0x48, 0x8B, 0x49, 0x70} };
world_offset = FindOffset(sigv, 3, 0x1000);
}
return world_offset;
}
uint32_t GetGObjects() {
if (!objects_offset) {
static std::vector<BYTE> sigv[] = { {0x48, 0x8B, 0x05, 0x00, 0x00, 0x00, 0x00, 0x48, 0x8B, 0x0C, 0xC8, 0x48, 0x8D, 0x04, 0xD1, 0xEB}, {0x48 , 0x8b , 0x0d , 0x00 , 0x00 , 0x00 , 0x00 , 0x81 , 0x4c , 0xd1 , 0x08 , 0x00 , 0x00 , 0x00 , 0x40} };
objects_offset = FindOffset(sigv, 2, 0x1000);
}
return objects_offset;
}
uint32_t GetGNames() {
if (!names_offset) {
static std::vector<BYTE> sigv[] = { { 0x48, 0x8B, 0x05, 0x00, 0x00, 0x00, 0x00, 0x48, 0x85, 0xC0, 0x75, 0x5F }, { 0x48, 0x8D, 0x15, 0x00, 0x00, 0x00, 0x00, 0xEB, 0x00, 0x48, 0x8D, 0x0D }, {0x48, 0x8b, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x48, 0x85, 0xff, 0x75, 0x3c} };
names_offset = FindOffset(sigv, 3, 0x1000);
}
return names_offset;
}
uint32_t GetNamePool() {
if (!pool_offset) {
static std::vector<BYTE> sigv[] = { { 0x48, 0x8d, 0x35, 0x00, 0x00, 0x00, 0x00, 0xeb, 0x16 } };
pool_offset = FindOffset(sigv, 1, 0x1000);
}
return pool_offset;
}
};