-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdllmain.cpp
More file actions
191 lines (167 loc) · 5.28 KB
/
dllmain.cpp
File metadata and controls
191 lines (167 loc) · 5.28 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
#include <windows.h>
#include <cstdint>
#include <cstring>
#include "MinHook.h"
// MSVC x64 std::wstring layout
struct MsvcWstring {
union {
wchar_t* ptr; // heap pointer (size > 7)
wchar_t buf[8]; // inline SSO buffer (size <= 7)
};
size_t size;
size_t capacity;
};
static bool ZeroWstring(MsvcWstring* wstr)
{
DWORD old;
if (!VirtualProtect(wstr, sizeof(MsvcWstring), PAGE_EXECUTE_READWRITE, &old))
return false;
if (wstr->size > 7)
{
DWORD old2;
if (VirtualProtect(wstr->ptr, sizeof(wchar_t), PAGE_EXECUTE_READWRITE, &old2))
{
wstr->ptr[0] = L'\0';
VirtualProtect(wstr->ptr, sizeof(wchar_t), old2, &old2);
}
}
else
{
wstr->buf[0] = L'\0';
}
wstr->size = 0;
VirtualProtect(wstr, sizeof(MsvcWstring), old, &old);
return true;
}
static void GetSections(uint8_t* mod,
uint8_t** rdataBase, size_t* rdataSize,
uint8_t** dataBase, size_t* dataSize)
{
auto* dos = reinterpret_cast<IMAGE_DOS_HEADER*>(mod);
auto* nt = reinterpret_cast<IMAGE_NT_HEADERS*>(mod + dos->e_lfanew);
auto* sec = IMAGE_FIRST_SECTION(nt);
for (WORD i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++sec)
{
if (memcmp(sec->Name, ".rdata", 6) == 0)
{
*rdataBase = mod + sec->VirtualAddress;
*rdataSize = sec->Misc.VirtualSize;
}
else if (memcmp(sec->Name, ".data", 5) == 0)
{
*dataBase = mod + sec->VirtualAddress;
*dataSize = sec->Misc.VirtualSize;
}
}
}
// Scan for a wstring object whose content matches needle, also checks both heap-allocated and SSO cases.
static MsvcWstring* FindWstring(uint8_t* dataBase, size_t dataSize, const wchar_t* needle)
{
size_t needleLen = wcslen(needle);
size_t needleBytes = needleLen * sizeof(wchar_t);
for (size_t i = 0; i + sizeof(MsvcWstring) <= dataSize; i += sizeof(uintptr_t))
{
auto* wstr = reinterpret_cast<MsvcWstring*>(dataBase + i);
// Size must match
if (wstr->size != needleLen)
continue;
if (needleLen > 7)
{
// Heap allocated — validate pointer then compare
if (!wstr->ptr) continue;
__try
{
if (memcmp(wstr->ptr, needle, needleBytes) == 0)
return wstr;
}
__except (EXCEPTION_EXECUTE_HANDLER) {}
}
else
{
// SSO — compare inline buffer
if (memcmp(wstr->buf, needle, needleBytes) == 0)
return wstr;
}
}
return nullptr;
}
static MsvcWstring* FindWstringPrefix(uint8_t* dataBase, size_t dataSize, const wchar_t* prefix)
{
size_t prefixLen = wcslen(prefix);
size_t prefixBytes = prefixLen * sizeof(wchar_t);
for (size_t i = 0; i + sizeof(MsvcWstring) <= dataSize; i += sizeof(uintptr_t))
{
auto* wstr = reinterpret_cast<MsvcWstring*>(dataBase + i);
if (wstr->size < prefixLen)
continue;
if (wstr->size > 7)
{
if (!wstr->ptr) continue;
__try
{
if (memcmp(wstr->ptr, prefix, prefixBytes) == 0)
return wstr;
}
__except (EXCEPTION_EXECUTE_HANDLER) {}
}
else
{
if (memcmp(wstr->buf, prefix, prefixBytes) == 0)
return wstr;
}
}
return nullptr;
}
static bool PatchString(uint8_t* dataBase, size_t dataSize,
const wchar_t* needle, const char* name)
{
MsvcWstring* wstr = FindWstring(dataBase, dataSize, needle);
if (!wstr)
{
OutputDebugStringA("[LceAntiWatermark] wstring not found: ");
OutputDebugStringA(name);
OutputDebugStringA("\n");
return false;
}
return ZeroWstring(wstr);
}
static bool PatchVersionLines()
{
auto* mod = reinterpret_cast<uint8_t*>(GetModuleHandle(nullptr));
uint8_t* rdataBase = nullptr; size_t rdataSize = 0;
uint8_t* dataBase = nullptr; size_t dataSize = 0;
GetSections(mod, &rdataBase, &rdataSize, &dataBase, &dataSize);
if (!dataBase)
{
OutputDebugStringA("[LceAntiWatermark] Could not find .data section.\n");
return false;
}
bool ok = true;
MsvcWstring* versionStr = FindWstringPrefix(dataBase, dataSize, L"Minecraft LCE ");
if (!versionStr)
{
OutputDebugStringA("[LceAntiWatermark] wstring not found: VERSION_STRING\n");
ok = false;
}
else
ok &= ZeroWstring(versionStr);
ok &= PatchString(dataBase, dataSize, L"smartcmd/MinecraftConsoles/main", "BRANCH_STRING");
return ok;
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID)
{
if (reason == DLL_PROCESS_ATTACH)
{
DisableThreadLibraryCalls(hModule);
MH_Initialize();
if (!PatchVersionLines())
OutputDebugStringA("[LceAntiWatermark] FAILED.\n");
else
OutputDebugStringA("[LceAntiWatermark] Zeroed strings.\n");
}
else if (reason == DLL_PROCESS_DETACH)
{
MH_Uninitialize();
}
return TRUE;
}