• R/O
  • SSH
  • HTTPS

exewrap: Commit


Commit MetaInfo

Revision25 (tree)
Time2015-07-21 12:56:36
Authorhirukawa_ryo

Log Message

v1.1.0 *コミット漏れ。

Change Summary

Incremental Difference

--- exewrap/trunk/exewrap/src/trace.c (revision 0)
+++ exewrap/trunk/exewrap/src/trace.c (revision 25)
@@ -0,0 +1,423 @@
1+#define _CRT_SECURE_NO_WARNINGS
2+
3+#include <Windows.h>
4+#include <stdio.h>
5+#include <string.h>
6+#include "minhook\include\MinHook.h"
7+
8+#define BUFFER_SIZE 1024
9+
10+const char* StartTrace();
11+void StopTrace();
12+
13+static char* _w2a(LPCWSTR s);
14+
15+static char* filename1 = NULL;
16+static char* filename2 = NULL;
17+static HANDLE hStdOut = NULL;
18+static FILE* fpStdOut = NULL;
19+static FILE* fp = NULL;
20+
21+typedef HANDLE(WINAPI *CREATEFILEA)(LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE);
22+typedef HANDLE(WINAPI *CREATEFILEW)(LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE);
23+typedef HMODULE(WINAPI *LOADLIBRARYA)(LPCSTR);
24+typedef HMODULE(WINAPI *LOADLIBRARYW)(LPCWSTR);
25+typedef HMODULE(WINAPI *LOADLIBRARYEXA)(LPCSTR, HANDLE, DWORD);
26+typedef HMODULE(WINAPI *LOADLIBRARYEXW)(LPCWSTR, HANDLE, DWORD);
27+
28+CREATEFILEA fpCreateFileA = NULL;
29+CREATEFILEW fpCreateFileW = NULL;
30+LOADLIBRARYA fpLoadLibraryA = NULL;
31+LOADLIBRARYW fpLoadLibraryW = NULL;
32+LOADLIBRARYEXA fpLoadLibraryExA = NULL;
33+LOADLIBRARYEXW fpLoadLibraryExW = NULL;
34+
35+HANDLE WINAPI DetourCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);
36+HANDLE WINAPI DetourCreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);
37+HMODULE WINAPI DetourLoadLibraryA(LPCSTR lpLibFileName);
38+HMODULE WINAPI DetourLoadLibraryW(LPCWSTR lpLibFileName);
39+HMODULE WINAPI DetourLoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags);
40+HMODULE WINAPI DetourLoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags);
41+
42+static void print(LPCSTR type, LPCSTR full_path_name);
43+static LPSTR is_file_in_JRE(LPCSTR lpFileName);
44+
45+const char* StartTrace(BOOL replace_stdout)
46+{
47+ BOOL succeeded = FALSE;
48+
49+ filename1 = (char*)malloc(1024);
50+ GetModuleFileName(NULL, filename1, 1024);
51+ *strrchr(filename1, '.') = '\0';
52+ strcat(filename1, ".log");
53+
54+ if (replace_stdout)
55+ {
56+ hStdOut = CreateFile(filename1, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
57+ if (hStdOut == INVALID_HANDLE_VALUE)
58+ {
59+ goto EXIT;
60+ }
61+
62+ fpStdOut = freopen(filename1, "wb", stdout);
63+ if (fpStdOut == NULL)
64+ {
65+ goto EXIT;
66+ }
67+ }
68+ else
69+ {
70+ filename2 = (char*)malloc(1024);
71+ strcpy(filename2, filename1);
72+ *strrchr(filename2, '.') = '\0';
73+ strcat(filename2, "2.log");
74+ fp = fopen(filename2, "wb");
75+ }
76+
77+ if (MH_Initialize() != MH_OK)
78+ {
79+ goto EXIT;
80+ }
81+
82+ if (MH_CreateHook(&CreateFileA, &DetourCreateFileA, (LPVOID*)(&fpCreateFileA)) != MH_OK)
83+ {
84+ goto EXIT;
85+ }
86+ if (MH_EnableHook(&CreateFileA) != MH_OK)
87+ {
88+ goto EXIT;
89+ }
90+
91+ if (MH_CreateHook(&CreateFileW, &DetourCreateFileW, (LPVOID*)(&fpCreateFileW)) != MH_OK)
92+ {
93+ goto EXIT;
94+ }
95+ if (MH_EnableHook(&CreateFileW) != MH_OK)
96+ {
97+ goto EXIT;
98+ }
99+
100+ if (MH_CreateHook(&LoadLibraryA, &DetourLoadLibraryA, (LPVOID*)(&fpLoadLibraryA)) != MH_OK)
101+ {
102+ goto EXIT;
103+ }
104+ if (MH_EnableHook(&LoadLibraryA) != MH_OK)
105+ {
106+ goto EXIT;
107+ }
108+
109+ if (MH_CreateHook(&LoadLibraryW, &DetourLoadLibraryW, (LPVOID*)(&fpLoadLibraryW)) != MH_OK)
110+ {
111+ goto EXIT;
112+ }
113+ if (MH_EnableHook(&LoadLibraryW) != MH_OK)
114+ {
115+ goto EXIT;
116+ }
117+
118+ if (MH_CreateHook(&LoadLibraryExA, &DetourLoadLibraryExA, (LPVOID*)(&fpLoadLibraryExA)) != MH_OK)
119+ {
120+ goto EXIT;
121+ }
122+ if (MH_EnableHook(&LoadLibraryExA) != MH_OK)
123+ {
124+ goto EXIT;
125+ }
126+
127+ if (MH_CreateHook(&LoadLibraryExW, &DetourLoadLibraryExW, (LPVOID*)(&fpLoadLibraryExW)) != MH_OK)
128+ {
129+ goto EXIT;
130+ }
131+ if (MH_EnableHook(&LoadLibraryExW) != MH_OK)
132+ {
133+ goto EXIT;
134+ }
135+
136+ succeeded = TRUE;
137+
138+EXIT:
139+ if (!succeeded)
140+ {
141+ free(filename1);
142+ filename1 = NULL;
143+ }
144+ return filename1;
145+}
146+
147+
148+void StopTrace()
149+{
150+ if (MH_DisableHook(&LoadLibraryW) != MH_OK)
151+ {
152+ }
153+
154+ if (MH_DisableHook(&CreateFileW) != MH_OK)
155+ {
156+ }
157+
158+ if (MH_Uninitialize() != MH_OK)
159+ {
160+ }
161+
162+ if (hStdOut != NULL && hStdOut != INVALID_HANDLE_VALUE)
163+ {
164+ FlushFileBuffers(hStdOut);
165+ CloseHandle(hStdOut);
166+ }
167+ hStdOut = NULL;
168+
169+ if (fpStdOut != NULL)
170+ {
171+ fflush(fpStdOut);
172+ fclose(fpStdOut);
173+ }
174+ fpStdOut = NULL;
175+
176+ if (fp != NULL)
177+ {
178+ fclose(fp);
179+ }
180+ fp = NULL;
181+
182+ if (filename2 != NULL)
183+ {
184+ free(filename2);
185+ }
186+
187+ if (filename1 != NULL)
188+ {
189+ free(filename1);
190+ }
191+}
192+
193+
194+static void print(LPCSTR type, LPCSTR full_path_name)
195+{
196+ if (fpStdOut != NULL)
197+ {
198+ printf("[%s %s]\n", type, full_path_name);
199+ fflush(stdout);
200+ }
201+ else if (fp != NULL)
202+ {
203+ fprintf(fp, "[%s %s]\n", type, full_path_name);
204+ fflush(fp);
205+ }
206+}
207+
208+
209+static LPSTR is_file_in_JRE(LPCSTR lpFileName)
210+{
211+ BOOL b = FALSE;
212+ LPSTR buf = NULL;
213+ LPSTR java_home = NULL;
214+ LPSTR full_path_name = NULL;
215+ DWORD size1;
216+ DWORD size2;
217+ LPSTR ptr;
218+
219+ buf = (LPSTR)malloc(BUFFER_SIZE);
220+ size1 = GetEnvironmentVariableA("JAVA_HOME", buf, BUFFER_SIZE);
221+ if (size1 > 0)
222+ {
223+ java_home = (LPSTR)malloc(BUFFER_SIZE);
224+ size1 = GetFullPathNameA(buf, BUFFER_SIZE, java_home, &ptr);
225+ if (size1 > 0)
226+ {
227+ full_path_name = (LPSTR)malloc(BUFFER_SIZE);
228+ size2 = GetFullPathNameA(lpFileName, BUFFER_SIZE, full_path_name, &ptr);
229+ if (size2 > 0)
230+ {
231+ if (_memicmp(java_home, full_path_name, size1) == 0)
232+ {
233+ b = TRUE;
234+ }
235+ }
236+ }
237+ }
238+
239+ if (buf != NULL)
240+ {
241+ free(buf);
242+ }
243+ if (java_home != NULL)
244+ {
245+ free(java_home);
246+ }
247+ if (!b && full_path_name != NULL)
248+ {
249+ free(full_path_name);
250+ full_path_name = NULL;
251+ }
252+ return full_path_name;
253+}
254+
255+
256+HANDLE WINAPI DetourCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
257+{
258+ LPSTR full_path_name;
259+
260+ full_path_name = is_file_in_JRE(lpFileName);
261+ if (full_path_name != NULL)
262+ {
263+ print("CreateFile", full_path_name);
264+ free(full_path_name);
265+ }
266+
267+ return fpCreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
268+}
269+
270+
271+HANDLE WINAPI DetourCreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
272+{
273+ LPSTR lpFileNameA = _w2a(lpFileName);
274+ LPSTR full_path_name;
275+
276+ full_path_name = is_file_in_JRE(lpFileNameA);
277+ if (full_path_name != NULL)
278+ {
279+ print("CreateFile", full_path_name);
280+ free(full_path_name);
281+ }
282+ free(lpFileNameA);
283+
284+ return fpCreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
285+}
286+
287+
288+HMODULE WINAPI DetourLoadLibraryA(LPCSTR lpLibFileName)
289+{
290+ HMODULE hModule;
291+ LPSTR buf = (LPSTR)malloc(BUFFER_SIZE);
292+ LPSTR full_path_name = NULL;
293+
294+ hModule = fpLoadLibraryA(lpLibFileName);
295+ if (hModule != NULL)
296+ {
297+ if (GetModuleFileNameA(hModule, buf, BUFFER_SIZE) > 0)
298+ {
299+ full_path_name = is_file_in_JRE(buf);
300+ if (full_path_name != NULL)
301+ {
302+ print("LoadLibrary", full_path_name);
303+ }
304+ }
305+ }
306+
307+ if (full_path_name != NULL)
308+ {
309+ free(full_path_name);
310+ }
311+ free(buf);
312+
313+ return hModule;
314+}
315+
316+
317+HMODULE WINAPI DetourLoadLibraryW(LPCWSTR lpLibFileName)
318+{
319+ HMODULE hModule;
320+ LPSTR buf = (LPSTR)malloc(BUFFER_SIZE);
321+ LPSTR full_path_name = NULL;
322+
323+ hModule = fpLoadLibraryW(lpLibFileName);
324+ if (hModule != NULL)
325+ {
326+ if (GetModuleFileName(hModule, buf, BUFFER_SIZE) > 0)
327+ {
328+ full_path_name = is_file_in_JRE(buf);
329+ if (full_path_name != NULL)
330+ {
331+ print("LoadLibrary", full_path_name);
332+ }
333+ }
334+ }
335+
336+ if (full_path_name != NULL)
337+ {
338+ free(full_path_name);
339+ }
340+ free(buf);
341+
342+ return hModule;
343+}
344+
345+
346+HMODULE WINAPI DetourLoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
347+{
348+ HMODULE hModule;
349+ LPSTR buf = (LPSTR)malloc(BUFFER_SIZE);
350+ LPSTR full_path_name = NULL;
351+
352+ hModule = fpLoadLibraryExA(lpLibFileName, hFile, dwFlags);
353+ if (hModule != NULL)
354+ {
355+ if (GetModuleFileNameA(hModule, buf, BUFFER_SIZE) > 0)
356+ {
357+ full_path_name = is_file_in_JRE(buf);
358+ if (full_path_name != NULL)
359+ {
360+ print("LoadLibrary", full_path_name);
361+ }
362+ }
363+ }
364+
365+ if (full_path_name != NULL)
366+ {
367+ free(full_path_name);
368+ }
369+ free(buf);
370+
371+ return hModule;
372+}
373+
374+
375+HMODULE WINAPI DetourLoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
376+{
377+ HMODULE hModule;
378+ LPSTR buf = (LPSTR)malloc(BUFFER_SIZE);
379+ LPSTR full_path_name = NULL;
380+
381+ hModule = fpLoadLibraryExW(lpLibFileName, hFile, dwFlags);
382+ if (hModule != NULL)
383+ {
384+ if (GetModuleFileNameA(hModule, buf, BUFFER_SIZE) > 0)
385+ {
386+ full_path_name = is_file_in_JRE(buf);
387+ if (full_path_name != NULL)
388+ {
389+ print("LoadLibrary", full_path_name);
390+ }
391+ }
392+ }
393+
394+ if (full_path_name != NULL)
395+ {
396+ free(full_path_name);
397+ }
398+ free(buf);
399+
400+ return hModule;
401+}
402+
403+static char* _w2a(LPCWSTR s)
404+{
405+ char* buf;
406+ int ret;
407+
408+ ret = WideCharToMultiByte(CP_ACP, 0, s, -1, NULL, 0, NULL, NULL);
409+ if (ret <= 0)
410+ {
411+ return NULL;
412+ }
413+ buf = (LPSTR)malloc(ret + 1);
414+ ret = WideCharToMultiByte(CP_ACP, 0, s, -1, buf, (ret + 1), NULL, NULL);
415+ if (ret == 0)
416+ {
417+ free(buf);
418+ return NULL;
419+ }
420+ buf[ret] = '\0';
421+
422+ return buf;
423+}
Show on old repository browser