-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTabInstaller.cpp
More file actions
284 lines (243 loc) · 8.96 KB
/
Copy pathHashTabInstaller.cpp
File metadata and controls
284 lines (243 loc) · 8.96 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
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
#include <windows.h>
#include <iostream>
#include <string>
static constexpr wchar_t kClsid[] = L"{050DA657-893C-457F-A45E-1A989820E6AC}";
static constexpr wchar_t kAppId[] = L"{55CB3090-D635-4445-B7B3-402BA6DB0DC9}";
static constexpr wchar_t kTypeLib[] = L"{40711F20-20F5-4D19-A0C2-2FFEB51E63D0}";
static constexpr wchar_t kProgId[] = L"HashTabGen.HashPage.1";
static constexpr wchar_t kProgIdVersionless[] = L"HashTabGen.HashPage";
static constexpr wchar_t kClassName[] = L"HashTabGen Class";
static constexpr wchar_t kAppIdName[] = L"HashTabGen";
static constexpr wchar_t kApprovedName[] = L"HashTabGen Shell Extension";
static bool IsRunningAsAdmin()
{
BOOL isMember = FALSE;
PSID adminGroup = nullptr;
SID_IDENTIFIER_AUTHORITY ntAuthority = SECURITY_NT_AUTHORITY;
if (!AllocateAndInitializeSid(&ntAuthority, 2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&adminGroup))
{
return false;
}
CheckTokenMembership(nullptr, adminGroup, &isMember);
FreeSid(adminGroup);
return isMember == TRUE;
}
static bool RelaunchElevated()
{
wchar_t path[MAX_PATH] = {};
if (GetModuleFileNameW(nullptr, path, MAX_PATH) == 0)
{
return false;
}
SHELLEXECUTEINFOW sei = {};
sei.cbSize = sizeof(sei);
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.lpVerb = L"runas";
sei.lpFile = path;
sei.nShow = SW_SHOWNORMAL;
if (!ShellExecuteExW(&sei))
{
return false;
}
return true;
}
static std::wstring GetDefaultDllPath()
{
wchar_t path[MAX_PATH] = {};
if (GetModuleFileNameW(nullptr, path, MAX_PATH) == 0)
{
return L"HashTabGen.dll";
}
std::wstring exePath(path);
size_t pos = exePath.find_last_of(L"\\/");
if (pos != std::wstring::npos)
{
exePath = exePath.substr(0, pos + 1);
}
return exePath + L"HashTabGen.dll";
}
static bool FileExists(const std::wstring& path)
{
DWORD attrs = GetFileAttributesW(path.c_str());
return attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY) == 0;
}
static REGSAM GetRegistryView()
{
#if defined(_WIN64)
return KEY_WOW64_64KEY;
#else
return KEY_WOW64_32KEY;
#endif
}
static HRESULT WriteRegString(HKEY root, const std::wstring& subKey, const wchar_t* name, const std::wstring& value)
{
HKEY hKey = nullptr;
LSTATUS status = RegCreateKeyExW(root, subKey.c_str(), 0, nullptr, 0,
KEY_WRITE | GetRegistryView(), nullptr, &hKey, nullptr);
if (status != ERROR_SUCCESS)
{
return HRESULT_FROM_WIN32(status);
}
status = RegSetValueExW(hKey, name, 0, REG_SZ,
reinterpret_cast<const BYTE*>(value.c_str()),
static_cast<DWORD>((value.size() + 1) * sizeof(wchar_t)));
RegCloseKey(hKey);
return HRESULT_FROM_WIN32(status);
}
static HRESULT WriteRegDefaultString(HKEY root, const std::wstring& subKey, const std::wstring& value)
{
return WriteRegString(root, subKey, nullptr, value);
}
static HRESULT EnsureEmptyKey(HKEY root, const std::wstring& subKey)
{
HKEY hKey = nullptr;
LSTATUS status = RegCreateKeyExW(root, subKey.c_str(), 0, nullptr, 0,
KEY_WRITE | GetRegistryView(), nullptr, &hKey, nullptr);
if (status == ERROR_SUCCESS)
{
RegCloseKey(hKey);
return S_OK;
}
return HRESULT_FROM_WIN32(status);
}
static HRESULT RegisterHashTab(const std::wstring& dllPath)
{
const std::wstring clsidRoot = std::wstring(L"SOFTWARE\\Classes\\CLSID\\") + kClsid;
HRESULT hr = WriteRegDefaultString(HKEY_LOCAL_MACHINE, clsidRoot, kClassName);
if (FAILED(hr)) return hr;
hr = WriteRegString(HKEY_LOCAL_MACHINE, clsidRoot, L"AppID", kAppId);
if (FAILED(hr)) return hr;
hr = WriteRegDefaultString(HKEY_LOCAL_MACHINE, clsidRoot + L"\\InprocServer32", dllPath);
if (FAILED(hr)) return hr;
hr = WriteRegString(HKEY_LOCAL_MACHINE, clsidRoot + L"\\InprocServer32", L"ThreadingModel", L"Apartment");
if (FAILED(hr)) return hr;
hr = WriteRegDefaultString(HKEY_LOCAL_MACHINE, clsidRoot + L"\\ProgID", kProgId);
if (FAILED(hr)) return hr;
hr = EnsureEmptyKey(HKEY_LOCAL_MACHINE, clsidRoot + L"\\Programmable");
if (FAILED(hr)) return hr;
hr = WriteRegDefaultString(HKEY_LOCAL_MACHINE, clsidRoot + L"\\TypeLib", kTypeLib);
if (FAILED(hr)) return hr;
hr = WriteRegDefaultString(HKEY_LOCAL_MACHINE, clsidRoot + L"\\VersionIndependentProgID", kProgIdVersionless);
if (FAILED(hr)) return hr;
const std::wstring handlers[] = {
L"SOFTWARE\\Classes\\*\\shellex\\PropertySheetHandlers\\HashTabGen",
L"SOFTWARE\\Classes\\Directory\\shellex\\PropertySheetHandlers\\HashTabGen",
L"SOFTWARE\\Classes\\Drive\\shellex\\PropertySheetHandlers\\HashTabGen"
};
for (const auto& key : handlers)
{
hr = WriteRegDefaultString(HKEY_LOCAL_MACHINE, key, kClsid);
if (FAILED(hr)) return hr;
}
const std::wstring appIdKey = std::wstring(L"SOFTWARE\\Classes\\AppID\\") + kAppId;
hr = WriteRegDefaultString(HKEY_LOCAL_MACHINE, appIdKey, kAppIdName);
if (FAILED(hr)) return hr;
const std::wstring approvedKey = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved";
hr = WriteRegString(HKEY_LOCAL_MACHINE, approvedKey, kClsid, kApprovedName);
if (FAILED(hr)) return hr;
return S_OK;
}
static HRESULT UnregisterHashTab()
{
const std::wstring clsidRoot = std::wstring(L"SOFTWARE\\Classes\\CLSID\\") + kClsid;
LSTATUS status = RegDeleteTreeW(HKEY_LOCAL_MACHINE, clsidRoot.c_str());
if (status != ERROR_SUCCESS && status != ERROR_FILE_NOT_FOUND)
{
return HRESULT_FROM_WIN32(status);
}
const std::wstring handlers[] = {
L"SOFTWARE\\Classes\\*\\shellex\\PropertySheetHandlers\\HashTabGen",
L"SOFTWARE\\Classes\\Directory\\shellex\\PropertySheetHandlers\\HashTabGen",
L"SOFTWARE\\Classes\\Drive\\shellex\\PropertySheetHandlers\\HashTabGen"
};
for (const auto& key : handlers)
{
status = RegDeleteTreeW(HKEY_LOCAL_MACHINE, key.c_str());
if (status != ERROR_SUCCESS && status != ERROR_FILE_NOT_FOUND)
{
return HRESULT_FROM_WIN32(status);
}
}
const std::wstring appIdKey = std::wstring(L"SOFTWARE\\Classes\\AppID\\") + kAppId;
status = RegDeleteTreeW(HKEY_LOCAL_MACHINE, appIdKey.c_str());
if (status != ERROR_SUCCESS && status != ERROR_FILE_NOT_FOUND)
{
return HRESULT_FROM_WIN32(status);
}
const std::wstring approvedKey = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved";
HKEY hApproved = nullptr;
status = RegOpenKeyExW(HKEY_LOCAL_MACHINE, approvedKey.c_str(), 0,
KEY_SET_VALUE | GetRegistryView(), &hApproved);
if (status == ERROR_SUCCESS && hApproved)
{
RegDeleteValueW(hApproved, kClsid);
RegCloseKey(hApproved);
}
return S_OK;
}
int wmain()
{
auto pauseBeforeExit = []()
{
std::wcout << L"\nStisknete libovolnou klavesu pro ukonceni..." << std::endl;
std::wstring dummy;
std::getline(std::wcin, dummy);
};
if (!IsRunningAsAdmin())
{
std::wcout << L"Spoustim s opravnenim spravce..." << std::endl;
if (RelaunchElevated())
{
return 0;
}
std::wcout << L"Nepodarilo se spustit jako spravce." << std::endl;
pauseBeforeExit();
return 1;
}
std::wstring dllPath = GetDefaultDllPath();
if (!FileExists(dllPath))
{
std::wcout << L"DLL nenalezena: " << dllPath << std::endl;
std::wcout << L"Zadejte cestu k DLL: ";
std::wstring input;
std::getline(std::wcin, input);
if (!input.empty())
{
dllPath = input;
}
}
if (!FileExists(dllPath))
{
std::wcout << L"Soubor DLL neexistuje." << std::endl;
pauseBeforeExit();
return 1;
}
std::wcout << L"1) Instalovat (registrace v registru)" << std::endl;
std::wcout << L"2) Odinstalovat (odstraneni z registru)" << std::endl;
std::wcout << L"0) Konec" << std::endl;
std::wcout << L"Volba: ";
std::wstring choice;
std::getline(std::wcin, choice);
if (choice == L"1")
{
HRESULT hr = RegisterHashTab(dllPath);
std::wcout << L"Vysledek: 0x" << std::hex << hr << std::endl;
std::wcout << L"Pro zmenu v Exploreru muze byt potreba restartovat Explorer." << std::endl;
pauseBeforeExit();
return FAILED(hr) ? 1 : 0;
}
if (choice == L"2")
{
HRESULT hr = UnregisterHashTab();
std::wcout << L"Vysledek: 0x" << std::hex << hr << std::endl;
std::wcout << L"Pro zmenu v Exploreru muze byt potreba restartovat Explorer." << std::endl;
pauseBeforeExit();
return FAILED(hr) ? 1 : 0;
}
pauseBeforeExit();
return 0;
}