Repository navigation
Expand file tree
/
Copy pathkey_runner.cpp
More file actions
230 lines (200 loc) · 7.39 KB
/
Copy pathkey_runner.cpp
File metadata and controls
230 lines (200 loc) · 7.39 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
// CheckRegistryKeys.cpp
// Reads [key] section from components config file,
// checks existence (KEY_WOW64_64KEY) and writability of each registry key,
// then prints a summary of writable keys.
#include <windows.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <iomanip>
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
struct KeyResult {
std::wstring fullPath;
bool exists;
bool writable;
};
// Split "HKLM\SOFTWARE\NCS" into {HKEY_LOCAL_MACHINE, L"SOFTWARE\\NCS"}
static bool ParseRegistryPath(const std::wstring& fullPath,
HKEY& hiveOut,
std::wstring& subkeyOut)
{
static const std::map<std::wstring, HKEY> hiveMap = {
{ L"HKLM", HKEY_LOCAL_MACHINE },
{ L"HKEY_LOCAL_MACHINE", HKEY_LOCAL_MACHINE },
{ L"HKCU", HKEY_CURRENT_USER },
{ L"HKEY_CURRENT_USER", HKEY_CURRENT_USER },
{ L"HKCR", HKEY_CLASSES_ROOT },
{ L"HKEY_CLASSES_ROOT", HKEY_CLASSES_ROOT },
{ L"HKU", HKEY_USERS },
{ L"HKEY_USERS", HKEY_USERS },
{ L"HKLM", HKEY_LOCAL_MACHINE },
};
auto sep = fullPath.find(L'\\');
if (sep == std::wstring::npos) return false;
std::wstring hiveStr = fullPath.substr(0, sep);
subkeyOut = fullPath.substr(sep + 1);
auto it = hiveMap.find(hiveStr);
if (it == hiveMap.end()) return false;
hiveOut = it->second;
return true;
}
// Widen a narrow string (ASCII / UTF-8 file content)
static std::wstring ToWide(const std::string& s)
{
if (s.empty()) return {};
int len = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, nullptr, 0);
std::wstring w(len - 1, L'\0');
MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, &w[0], len);
return w;
}
// ---------------------------------------------------------------------------
// Parse config file, return only [key] entries
// ---------------------------------------------------------------------------
static std::vector<std::wstring> ReadKeySection(const std::string& filePath)
{
std::vector<std::wstring> keys;
std::ifstream file(filePath);
if (!file.is_open()) {
std::wcerr << L"[ERROR] Cannot open file: " << ToWide(filePath) << L"\n";
return keys;
}
bool inKeySection = false;
std::string line;
while (std::getline(file, line)) {
// Strip trailing CR
if (!line.empty() && line.back() == '\r') line.pop_back();
// Skip blank lines and comments
if (line.empty() || line[0] == '#') continue;
if (line[0] == '[') {
inKeySection = (line == "[key]");
continue;
}
if (inKeySection) {
keys.push_back(ToWide(line));
}
}
return keys;
}
// ---------------------------------------------------------------------------
// Check existence and writability
// ---------------------------------------------------------------------------
static KeyResult CheckKey(const std::wstring& fullPath)
{
KeyResult result{ fullPath, false, false };
HKEY hive = nullptr;
std::wstring sub;
if (!ParseRegistryPath(fullPath, hive, sub)) {
std::wcerr << L"[WARN] Cannot parse path: " << fullPath << L"\n";
return result;
}
// --- Existence check (read-only, 64-bit view) ---
HKEY hKey = nullptr;
LONG rc = RegOpenKeyExW(hive,
sub.c_str(),
0,
KEY_READ | KEY_WOW64_64KEY,
&hKey);
if (rc != ERROR_SUCCESS) {
// Key does not exist (or access denied at read level)
return result;
}
result.exists = true;
RegCloseKey(hKey);
hKey = nullptr;
// --- Writability check (KEY_SET_VALUE | KEY_CREATE_SUB_KEY, 64-bit view) ---
// Using KEY_SET_VALUE | KEY_CREATE_SUB_KEY is a meaningful proxy for
// "can this process modify the key" without actually changing anything.
rc = RegOpenKeyExW(hive,
sub.c_str(),
0,
KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_WOW64_64KEY,
&hKey);
if (rc == ERROR_SUCCESS) {
result.writable = true;
RegCloseKey(hKey);
}
return result;
}
// ---------------------------------------------------------------------------
// main
// ---------------------------------------------------------------------------
int wmain(int argc, wchar_t* argv[])
{
std::string configFile = "components.txt";
if (argc >= 2) {
// Allow passing config path as argument
int need = WideCharToMultiByte(CP_UTF8, 0, argv[1], -1,
nullptr, 0, nullptr, nullptr);
configFile.resize(need - 1);
WideCharToMultiByte(CP_UTF8, 0, argv[1], -1,
&configFile[0], need, nullptr, nullptr);
}
std::wcout << L"=== Registry Key Checker ===\n";
std::wcout << L"Config: " << ToWide(configFile) << L"\n\n";
auto keys = ReadKeySection(configFile);
if (keys.empty()) {
std::wcout << L"No keys found in [key] section.\n";
return 1;
}
std::vector<KeyResult> results;
results.reserve(keys.size());
// Column widths for table
const int COL_KEY = 70;
const int COL_EXIST = 8;
const int COL_WRITE = 10;
// Header
std::wcout << std::left
<< std::wstring(COL_KEY, L'-') << L" "
<< std::wstring(COL_EXIST, L'-') << L" "
<< std::wstring(COL_WRITE, L'-') << L"\n";
std::wcout << std::setw(COL_KEY) << L"Registry Key"
<< L" "
<< std::setw(COL_EXIST) << L"Exists"
<< L" "
<< std::setw(COL_WRITE) << L"Writable"
<< L"\n";
std::wcout << std::wstring(COL_KEY, L'-') << L" "
<< std::wstring(COL_EXIST, L'-') << L" "
<< std::wstring(COL_WRITE, L'-') << L"\n";
for (const auto& key : keys) {
auto r = CheckKey(key);
results.push_back(r);
std::wcout << std::setw(COL_KEY) << r.fullPath
<< L" "
<< std::setw(COL_EXIST) << (r.exists ? L"YES" : L"NO")
<< L" "
<< std::setw(COL_WRITE) << (r.writable ? L"YES" : L"NO")
<< L"\n";
}
// Summary: writable keys
std::wcout << L"\n=== Summary: Writable Keys ===\n";
bool anyWritable = false;
for (const auto& r : results) {
if (r.writable) {
std::wcout << L" [WRITABLE] " << r.fullPath << L"\n";
anyWritable = true;
}
}
if (!anyWritable) {
std::wcout << L" (none — all keys are read-only or do not exist)\n";
}
// Summary: missing keys
std::wcout << L"\n=== Summary: Missing Keys ===\n";
bool anyMissing = false;
for (const auto& r : results) {
if (!r.exists) {
std::wcout << L" [MISSING] " << r.fullPath << L"\n";
anyMissing = true;
}
}
if (!anyMissing) {
std::wcout << L" (all keys exist)\n";
}
std::wcout << L"\nDone.\n";
return 0;
}