-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstaller.c
More file actions
178 lines (153 loc) · 5.35 KB
/
Copy pathinstaller.c
File metadata and controls
178 lines (153 loc) · 5.35 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
/*
* installer.c
* Install / Uninstall logic for AppMain.exe.
*/
#include "common.h"
#include "installer.h"
typedef BOOL (WINAPI *KernelLibIoControl_t)(HANDLE hLib,
DWORD dwIoControlCode,
LPVOID lpInBuf,
DWORD nInBufSize,
LPVOID lpOutBuf,
DWORD nOutBufSize,
LPDWORD lpBytesReturned);
static KernelLibIoControl_t s_KernelLibIoControl = NULL;
static HMODULE s_hCoreDll = NULL;
static BOOL LoadKernelIoControl(void)
{
s_hCoreDll = LoadLibrary(L"coredll");
if (s_hCoreDll == NULL)
return FALSE;
s_KernelLibIoControl = (KernelLibIoControl_t)
GetProcAddress(s_hCoreDll, L"KernelLibIoControl");
return s_KernelLibIoControl != NULL;
}
static void UnloadKernelIoControl(void)
{
if (s_hCoreDll)
{
FreeLibrary(s_hCoreDll);
s_hCoreDll = NULL;
s_KernelLibIoControl = NULL;
}
}
/* Log an error and show a generic message box directing to the log file. */
static void LogError(LPCWSTR message)
{
AppendLog(message);
MessageBox(NULL,
L"An error occurred.\n"
L"Please check the log file for details:\n\n"
ERROR_LOG_PATH,
L"BER", MB_OK | MB_ICONERROR);
}
static BOOL SetJITDebuggerPath(LPCWSTR path)
{
return s_KernelLibIoControl((HANDLE)KMOD_CORE,
IOCTL_KLIB_SETJITDBGRPATH,
(LPVOID)path, 0, NULL, 0, NULL);
}
/* Return the directory of AppMain.exe (trailing backslash included). */
static void GetModuleDir(WCHAR *dir, DWORD maxChars)
{
WCHAR path[MAX_PATH];
WCHAR *p;
GetModuleFileName(NULL, path, MAX_PATH);
p = path + lstrlenW(path);
while (p > path && *p != L'\\')
p--;
if (*p == L'\\')
p++;
*p = L'\0';
lstrcpynW(dir, path, (int)maxChars);
}
/* ---------------------------------------------------------------
* Install
* ------------------------------------------------------------- */
int Install(void)
{
WCHAR srcDir[MAX_PATH];
WCHAR srcPath[MAX_PATH];
HKEY hKey;
DWORD installed = 1;
DWORD disp;
if (!LoadKernelIoControl())
{
LogError(L"Install: Failed to load KernelLibIoControl from coredll");
return EXIT_FAILURE;
}
GetModuleDir(srcDir, MAX_PATH);
/* 1. Copy BERDebugger.exe */
wsprintf(srcPath, L"%sBERDebugger.exe", srcDir);
if (CopyFile(srcPath, DEBUGGER_DST_PATH, FALSE) == FALSE)
{
LogError(L"Install: Failed to copy BERDebugger.exe to \\Windows\\");
UnloadKernelIoControl();
return EXIT_FAILURE;
}
/* 2. Copy BER.exe */
wsprintf(srcPath, L"%sBER.exe", srcDir);
if (CopyFile(srcPath, VIEWER_DST_PATH, FALSE) == FALSE)
{
LogError(L"Install: Failed to copy BER.exe to \\Windows\\");
DeleteFile(DEBUGGER_DST_PATH);
UnloadKernelIoControl();
return EXIT_FAILURE;
}
/* 3. Write registry defaults */
if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, REG_KEY_BER, 0, NULL, 0,
KEY_WRITE, NULL, &hKey, &disp) != ERROR_SUCCESS)
{
LogError(L"Install: Failed to create registry key");
DeleteFile(DEBUGGER_DST_PATH);
DeleteFile(VIEWER_DST_PATH);
UnloadKernelIoControl();
return EXIT_FAILURE;
}
RegSetValueEx(hKey, REG_VAL_INSTALLED, 0, REG_DWORD,
(LPBYTE)&installed, sizeof(DWORD));
RegSetValueEx(hKey, REG_VAL_LOGPATH, 0, REG_SZ,
(LPBYTE)DEFAULT_LOG_PATH,
(lstrlenW(DEFAULT_LOG_PATH) + 1) * sizeof(WCHAR));
RegCloseKey(hKey);
/* 4. Register BERDebugger.exe as the JIT debugger */
if (SetJITDebuggerPath(DEBUGGER_DST_PATH) == FALSE)
{
LogError(L"Install: Failed to set JIT debugger path");
DeleteFile(DEBUGGER_DST_PATH);
DeleteFile(VIEWER_DST_PATH);
RegDeleteKey(HKEY_LOCAL_MACHINE, REG_KEY_BER);
UnloadKernelIoControl();
return EXIT_FAILURE;
}
UnloadKernelIoControl();
MessageBox(NULL, L"BER installed successfully.", L"BER",
MB_OK | MB_ICONINFORMATION);
return EXIT_SUCCESS;
}
/* ---------------------------------------------------------------
* Uninstall
* ------------------------------------------------------------- */
int Uninstall(void)
{
if (!LoadKernelIoControl())
{
LogError(L"Uninstall: Failed to load KernelLibIoControl from coredll");
return EXIT_FAILURE;
}
/* 1. Restore original JIT debugger */
if (SetJITDebuggerPath(ORIGINAL_JIT_PATH) == FALSE)
LogError(L"Uninstall: Failed to restore original JIT debugger path");
/* 2. Delete BERDebugger.exe */
if (DeleteFile(DEBUGGER_DST_PATH) == FALSE)
LogError(L"Uninstall: Failed to delete \\Windows\\BERDebugger.exe");
/* 3. Delete BER.exe */
if (DeleteFile(VIEWER_DST_PATH) == FALSE)
LogError(L"Uninstall: Failed to delete \\Windows\\BER.exe");
/* 4. Delete registry key */
RegDeleteKey(HKEY_LOCAL_MACHINE, REG_KEY_BER);
UnloadKernelIoControl();
MessageBox(NULL, L"BER uninstalled successfully.", L"BER",
MB_OK | MB_ICONINFORMATION);
return EXIT_SUCCESS;
}