-
Notifications
You must be signed in to change notification settings - Fork 444
/
run.cpp
321 lines (274 loc) · 7.32 KB
/
run.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
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
// dll injection
#define _CRT_SECURE_NO_DEPRECATE 1
#define WINVER 0x500
#define _WIN32_WINNT 0x500
#define _WIN32_IE 0x601
#define WIN32_LEAN_AND_MEAN 1
#define UNICODE 1
#define _UNICODE 1
#include <Windows.h>
#include <ShellApi.h>
#include <ComDef.h>
#include <ShlObj.h>
#include <ShLwApi.h>
#include <tchar.h>
#include "array.h"
#include <strsafe.h>
// _vsnwprintf用
#include <wchar.h>
#include <stdarg.h>
#define _CRTDBG_MAP_ALLOC
#include <cstdlib>
#include <malloc.h>
#include <crtdbg.h>
#define for if(0);else for
#ifndef _countof
#define _countof(array) (sizeof(array) / sizeof((array)[0]))
#endif
#pragma comment(linker, "/subsystem:windows,5.0")
#pragma comment(lib, "Kernel32.lib")
#pragma comment(lib, "User32.lib")
#pragma comment(lib, "Shell32.lib")
#pragma comment(lib, "ShLwApi.lib")
#pragma comment(lib, "Ole32.lib")
#define IDS_USAGE 101
#define IDS_DLL 102
#define IDC_EXEC 103
static void showmsg(LPCSTR msg) {
MessageBoxA(NULL, msg, "MacType ERROR", MB_OK | MB_ICONSTOP);
}
static void errmsg(UINT id, DWORD code)
{
char buffer[512];
char format[128];
LoadStringA(GetModuleHandleA(NULL), id, format, 128);
wnsprintfA(buffer, 512, format, code);
showmsg(buffer);
}
inline HRESULT HresultFromLastError()
{
DWORD dwErr = GetLastError();
return HRESULT_FROM_WIN32(dwErr);
}
#include "detours.h"
#ifdef _M_IX86
#pragma comment (lib, "detours.lib")
const auto MacTypeDll = L"MacType.dll";
const auto MacTypeDllA = "MacType.dll";
#else
#pragma comment (lib, "detours64.lib")
const auto MacTypeDll = L"MacType64.dll";
const auto MacTypeDllA = "MacType64.dll";
#endif
HINSTANCE hinstDLL;
#include <stddef.h>
#define GetDLLInstance() (hinstDLL)
#define _GDIPP_EXE
#define _GDIPP_RUN_CPP
//#include "supinfo.h"
//#define OLD_PSDK
#ifdef OLD_PSDK
extern "C" {
HRESULT WINAPI _SHILCreateFromPath(LPCWSTR pszPath, LPITEMIDLIST* ppidl, DWORD* rgflnOut)
{
if (!pszPath || !ppidl) {
return E_INVALIDARG;
}
LPSHELLFOLDER psf;
HRESULT hr = ::SHGetDesktopFolder(&psf);
if (hr != NOERROR) {
return hr;
}
ULONG chEaten;
LPOLESTR lpszDisplayName = ::StrDupW(pszPath);
hr = psf->ParseDisplayName(NULL, NULL, lpszDisplayName, &chEaten, ppidl, rgflnOut);
::LocalFree(lpszDisplayName);
psf->Release();
return hr;
}
void WINAPI _SHFree(void* pv)
{
if (!pv) {
return;
}
LPMALLOC pMalloc = NULL;
if (::SHGetMalloc(&pMalloc) == NOERROR) {
pMalloc->Free(pv);
pMalloc->Release();
}
}
}
#else
#define _SHILCreateFromPath SHILCreateFromPath
#define _SHFree SHFree
#endif
bool isX64PE(const TCHAR* file_path) {
HANDLE hFile = CreateFile(file_path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
showmsg("Error opening file");
return false;
}
IMAGE_DOS_HEADER dosHeader;
DWORD bytesRead;
if (!ReadFile(hFile, &dosHeader, sizeof(IMAGE_DOS_HEADER), &bytesRead, NULL)) {
showmsg("Error reading file");
CloseHandle(hFile);
return false;
}
// Check if it's a PE file
if (dosHeader.e_magic != IMAGE_DOS_SIGNATURE) {
showmsg("Not a PE file");
CloseHandle(hFile);
return false;
}
IMAGE_NT_HEADERS ntHeaders;
// Seek to the PE header offset
SetFilePointer(hFile, dosHeader.e_lfanew, NULL, FILE_BEGIN);
if (!ReadFile(hFile, &ntHeaders, sizeof(IMAGE_NT_HEADERS), &bytesRead, NULL)) {
showmsg("Error reading PE header");
CloseHandle(hFile);
return false;
}
if (ntHeaders.FileHeader.Machine == IMAGE_FILE_MACHINE_I386) {
CloseHandle(hFile);
return false;
}
else if (ntHeaders.FileHeader.Machine == IMAGE_FILE_MACHINE_AMD64) {
CloseHandle(hFile);
return true;
}
else {
CloseHandle(hFile);
return false;
}
}
// 1つ目の引数だけファイルとして扱い、実行する。
//
// コマンドは こんな感じで連結されます。
// exe linkpath linkarg cmdarg2 cmdarg3 cmdarg4 ...
//
static HRESULT HookAndExecute(int show)
{
int argc = 0;
LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (!argv) {
return HresultFromLastError();
}
if (argc <= 1) {
char buffer[256];
LoadStringA(GetModuleHandleA(NULL), IDS_USAGE, buffer, 256);
MessageBoxA(NULL,
buffer
, "MacType", MB_OK | MB_ICONINFORMATION);
LocalFree(argv);
return S_OK;
}
int i;
size_t length = 1;
for (i = 1; i < argc; i++) {
length += wcslen(argv[i]) + 3;
}
LPWSTR cmdline = (WCHAR*)calloc(sizeof(WCHAR), length);
if (!cmdline) {
LocalFree(argv);
return E_OUTOFMEMORY;
}
LPWSTR p = cmdline;
*p = L'\0';
for (i = 1; i < argc; i++) {
const bool dq = !!wcschr(argv[i], L' ');
if (dq) {
*p++ = '"';
length--;
}
StringCchCopyExW(p, length, argv[i], &p, &length, STRSAFE_NO_TRUNCATION);
if (dq) {
*p++ = '"';
length--;
}
*p++ = L' ';
length--;
}
*CharPrevW(cmdline, p) = L'\0';
// now we got the full cmdline for external exetuble. let's check if we can hook into it
#ifdef _M_IX86
if (isX64PE(argv[1])) {
ShellExecute(NULL, NULL, L"macloader64.exe", cmdline, NULL, SW_SHOW);
return S_OK;
}
#else
if (!isX64PE(argv[1])) {
ShellExecute(NULL, NULL, L"macloader.exe", cmdline, NULL, SW_SHOW);
return S_OK;
}
#endif
WCHAR file[MAX_PATH], dir[MAX_PATH];
GetCurrentDirectoryW(_countof(dir), dir);
StringCchCopyW(file, _countof(file), argv[1]);
if (PathIsRelativeW(file)) {
PathCombineW(file, dir, file);
}
else {
WCHAR gdippDir[MAX_PATH];
GetModuleFileNameW(NULL, gdippDir, _countof(gdippDir));
PathRemoveFileSpec(gdippDir);
// カレントディレクトリがgdi++.exeの置かれているディレクトリと同じだったら、
// 起動しようとしているEXEのフルパスから抜き出したディレクトリ名をカレント
// ディレクトリとして起動する。(カレントディレクトリがEXEと同じ場所である
// 前提で作られているアプリ対策)
if (wcscmp(dir, gdippDir) == 0) {
StringCchCopyW(dir, _countof(dir), argv[1]);
PathRemoveFileSpec(dir);
}
}
#ifdef _DEBUG
if ((GetAsyncKeyState(VK_CONTROL) & 0x8000)
&& MessageBoxW(NULL, cmdline, NULL, MB_YESNO) != IDYES) {
free(cmdline);
return NOERROR;
}
#endif
PROCESS_INFORMATION processInfo;
STARTUPINFO startupInfo = { 0 };
startupInfo.cb = sizeof(startupInfo);
// get current directory and append mactype dll
char path[MAX_PATH] = { 0 };
if (GetModuleFileNameA(NULL, path, _countof(path))) {
PathRemoveFileSpecA(path);
strcat(path, "\\");
}
strcat(path, MacTypeDllA);
auto ret = DetourCreateProcessWithDllEx(NULL, cmdline, NULL, NULL, false, 0, NULL, dir, &startupInfo, &processInfo, path, NULL);
free(cmdline);
LocalFree(argv);
argv = NULL;
return ret ? S_OK : E_ACCESSDENIED;
}
int WINAPI wWinMain(HINSTANCE ins, HINSTANCE prev, LPWSTR cmd, int show)
{
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
OleInitialize(NULL);
WCHAR path[MAX_PATH];
if (GetModuleFileNameW(NULL, path, _countof(path))) {
PathRemoveFileSpec(path);
wcscat(path, L"\\");
wcscat(path, MacTypeDll);
//DONT_RESOLVE_DLL_REFERENCESを指定すると依存関係の解決や
//DllMainの呼び出しが行われない
hinstDLL = LoadLibraryExW(path, NULL, DONT_RESOLVE_DLL_REFERENCES);
}
if (!hinstDLL) {
errmsg(IDS_DLL, HresultFromLastError());
}
else {
PathRemoveFileSpecW(path);
SetCurrentDirectoryW(path);
HRESULT hr = HookAndExecute(show);
if (hr != S_OK) {
errmsg(IDC_EXEC, hr);
}
}
OleUninitialize();
return 0;
}
//EOF