-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
237 lines (197 loc) · 6.42 KB
/
Copy pathmain.cpp
File metadata and controls
237 lines (197 loc) · 6.42 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
#include <windows.h>
#include <string>
#include <vector>
#include <filesystem>
#include <cstdio>
static bool IsExe64(const std::wstring& path) {
HANDLE hFile = CreateFileW(path.c_str(), GENERIC_READ,
FILE_SHARE_READ, nullptr, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile == INVALID_HANDLE_VALUE) return false;
DWORD bytes;
IMAGE_DOS_HEADER dosh;
ReadFile(hFile, &dosh, sizeof(dosh), &bytes, nullptr);
if (dosh.e_magic != IMAGE_DOS_SIGNATURE) {
CloseHandle(hFile);
return false;
}
SetFilePointer(hFile, dosh.e_lfanew, NULL, FILE_BEGIN);
DWORD peSig;
ReadFile(hFile, &peSig, sizeof(peSig), &bytes, nullptr);
if (peSig != IMAGE_NT_SIGNATURE) {
CloseHandle(hFile);
return false;
}
IMAGE_FILE_HEADER fileHeader;
ReadFile(hFile, &fileHeader, sizeof(fileHeader), &bytes, nullptr);
CloseHandle(hFile);
return fileHeader.Machine == IMAGE_FILE_MACHINE_AMD64;
}
int wmain(int argc, wchar_t *argv[]) {
if (argc < 3) {
wprintf(L"Usage: WinPathShim.exe FakeRoot ExePath [args...]\n");
return 1;
}
std::wstring fakeRoot = argv[1];
std::wstring exePath = argv[2];
std::filesystem::path root(fakeRoot);
std::filesystem::path appdata = root / L"AppData";
std::filesystem::path roaming = appdata / L"Roaming";
std::filesystem::path local = appdata / L"Local";
std::filesystem::path localLow = appdata / L"LocalLow";
std::filesystem::path docs = root / L"Documents";
try {
std::filesystem::create_directories(roaming);
std::filesystem::create_directories(local);
std::filesystem::create_directories(localLow);
std::filesystem::create_directories(docs);
} catch (...) {
wprintf(L"Failed to create fake directories\n");
return 1;
}
bool debug = false;
std::wstring cmdLine = L"\"" + exePath + L"\"";
for (int i = 3; i < argc; ++i) {
if (wcscmp(argv[i], L"--debug") == 0) {
debug = true;
continue; // do not pass --debug to the game
}
cmdLine += L" \"";
cmdLine += argv[i];
cmdLine += L"\"";
}
if (debug) {
SetEnvironmentVariableW(L"FAKE_DEBUG", L"1");
wprintf(L"[DEBUG] logging enabled\n");
}
// variables for the DLL
SetEnvironmentVariableW(L"FAKE_ROAMING", roaming.c_str());
SetEnvironmentVariableW(L"FAKE_LOCAL", local.c_str());
SetEnvironmentVariableW(L"FAKE_LOCALLOW", localLow.c_str());
SetEnvironmentVariableW(L"FAKE_DOCS", docs.c_str());
// "normal" environment variables that many programs use
SetEnvironmentVariableW(L"APPDATA", roaming.c_str());
SetEnvironmentVariableW(L"LOCALAPPDATA", local.c_str());
SetEnvironmentVariableW(L"USERPROFILE", fakeRoot.c_str());
// home
std::filesystem::path fr(fakeRoot);
std::wstring drive = fr.root_name().wstring(); // e.g. "F:"
std::wstring home = fr.relative_path().wstring(); // e.g. "\FakeProfile"
if (home.empty()) home = L"\\";
SetEnvironmentVariableW(L"HOMEDRIVE", drive.c_str());
SetEnvironmentVariableW(L"HOMEPATH", home.c_str());
SetEnvironmentVariableW(L"HOME", fakeRoot.c_str());
// DOCUMENTS does not always exist as an env var, but some apps check it
SetEnvironmentVariableW(L"DOCUMENTS", docs.c_str());
SetEnvironmentVariableW(L"OneDrive", docs.c_str());
SetEnvironmentVariableW(L"OneDriveConsumer", docs.c_str());
SetEnvironmentVariableW(L"OneDriveCommercial", docs.c_str());
// public documents
std::wstring fakePublic = (root / L"Public").wstring();
std::wstring fakePublicDocs = (root / L"PublicDocuments").wstring();
SetEnvironmentVariableW(L"PUBLIC", fakePublic.c_str());
SetEnvironmentVariableW(L"PUBLICDOCUMENTS", fakePublicDocs.c_str());
STARTUPINFOW si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
si.cb = sizeof(si);
if (!CreateProcessW(
exePath.c_str(),
cmdLine.data(),
nullptr,
nullptr,
FALSE,
CREATE_SUSPENDED,
nullptr,
nullptr,
&si,
&pi)) {
wprintf(L"CreateProcessW failed with error %lu\n", GetLastError());
return 1;
}
// find the absolute path of the launcher
wchar_t selfPath[MAX_PATH];
DWORD r = GetModuleFileNameW(nullptr, selfPath, MAX_PATH);
if (r == 0 || r == MAX_PATH) {
wprintf(L"GetModuleFileNameW failed: %lu", GetLastError());
TerminateProcess(pi.hProcess, 1);
return 1;
}
std::filesystem::path exeDir = std::filesystem::path(selfPath).parent_path();
std::filesystem::path dllFull;
if (IsExe64(exePath)) {
dllFull = exeDir / L"WinPathShim64.dll";
} else {
dllFull = exeDir / L"WinPathShim32.dll";
}
std::wstring dllPath = dllFull.wstring();
wprintf(L"Loading DLL from: %ls", dllPath.c_str());
SIZE_T bytes = (dllPath.size() + 1) * sizeof(wchar_t);
LPVOID remoteMem = VirtualAllocEx(
pi.hProcess,
nullptr,
bytes,
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE
);
if (!remoteMem) {
wprintf(L"VirtualAllocEx failed\n");
TerminateProcess(pi.hProcess, 1);
return 1;
}
if (!WriteProcessMemory(
pi.hProcess,
remoteMem,
dllPath.c_str(),
bytes,
nullptr)) {
wprintf(L"WriteProcessMemory failed\n");
TerminateProcess(pi.hProcess, 1);
return 1;
}
HMODULE hKernel32 = GetModuleHandleW(L"kernel32.dll");
if (!hKernel32) {
wprintf(L"GetModuleHandleW(kernel32) failed\n");
TerminateProcess(pi.hProcess, 1);
return 1;
}
LPTHREAD_START_ROUTINE pLoadLibraryW =
(LPTHREAD_START_ROUTINE)GetProcAddress(hKernel32, "LoadLibraryW");
if (!pLoadLibraryW) {
wprintf(L"GetProcAddress(LoadLibraryW) failed\n");
TerminateProcess(pi.hProcess, 1);
return 1;
}
HANDLE hThread = CreateRemoteThread(
pi.hProcess,
nullptr,
0,
pLoadLibraryW,
remoteMem,
0,
nullptr
);
if (!hThread) {
wprintf(L"CreateRemoteThread failed\n");
TerminateProcess(pi.hProcess, 1);
return 1;
}
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
VirtualFreeEx(pi.hProcess, remoteMem, 0, MEM_RELEASE);
ResumeThread(pi.hThread);
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return 0;
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nShowCmd) {
int argc = 0;
LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc);
int ret = wmain(argc, argv);
if (argv) {
LocalFree(argv);
}
return ret;
}