-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathxpmgr_lib.cpp
More file actions
389 lines (334 loc) · 13.7 KB
/
xpmgr_lib.cpp
File metadata and controls
389 lines (334 loc) · 13.7 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include "xpmgr.h"
#include <windows.h>
#include <objbase.h>
#include <oleauto.h>
#include <regex>
#include <string>
#include <algorithm>
#include <TlHelp32.h>
#include <iostream>
// CLSID and IID definitions
static CLSID XP_CLSID = { 0xACADF079, 0xCBCD, 0x4032, {0x83, 0xF2, 0xFA, 0x47, 0xC4, 0xDB, 0x09, 0x6F} };
static IID XP_IID = { 0xB8CBAD79, 0x3F1F, 0x481A, { 0xBB, 0x0C, 0xE7, 0xBB, 0xD7, 0x7B, 0xDD, 0xD1 } };
// Interface definition
DECLARE_INTERFACE_(ICOMLicenseAgent, IDispatch)
{
protected:
~ICOMLicenseAgent() = default;
public:
/*** IUnknown methods ***/
STDMETHOD(QueryInterface)(THIS_ REFIID riid, LPVOID* ppvObj) PURE;
STDMETHOD_(ULONG, AddRef)() PURE;
STDMETHOD_(ULONG, Release)() PURE;
/*** IDispatch methods ***/
STDMETHOD(GetTypeInfoCount)(THIS_ UINT FAR* pctinfo) PURE;
STDMETHOD(GetTypeInfo)(THIS_ UINT itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo) PURE;
STDMETHOD(GetIDsOfNames)(THIS_ REFIID riid, OLECHAR FAR* FAR* rgszNames, UINT cNames, LCID lcid, DISPID FAR* rgdispid) PURE;
STDMETHOD(Invoke)(THIS_ DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR* pdispparams, VARIANT FAR* pvarResult, EXCEPINFO FAR* pexcepinfo, UINT FAR* puArgErr) PURE;
/*** ICOMLicenseAgent methods ***/
STDMETHOD(Initialize)(THIS_ ULONG dwBPC, ULONG dwMode, BSTR bstrLicSource, ULONG* pdwRetCode) PURE;
STDMETHOD(GetFirstName)(THIS_ BSTR* pbstrVal) PURE;
STDMETHOD(SetFirstName)(THIS_ BSTR bstrNewVal) PURE;
STDMETHOD(GetLastName)(THIS_ BSTR* pbstrVal) PURE;
STDMETHOD(SetLastName)(THIS_ BSTR bstrNewVal) PURE;
STDMETHOD(GetOrgName)(THIS_ BSTR* pbstrVal) PURE;
STDMETHOD(SetOrgName)(THIS_ BSTR bstrNewVal) PURE;
STDMETHOD(GetEmail)(THIS_ BSTR* pbstrVal) PURE;
STDMETHOD(SetEmail)(THIS_ BSTR bstrNewVal) PURE;
STDMETHOD(GetPhone)(THIS_ BSTR* pbstrVal) PURE;
STDMETHOD(SetPhone)(THIS_ BSTR bstrNewVal) PURE;
STDMETHOD(GetAddress1)(THIS_ BSTR* pbstrVal) PURE;
STDMETHOD(SetAddress1)(THIS_ BSTR bstrNewVal) PURE;
STDMETHOD(GetCity)(THIS_ BSTR* pbstrVal) PURE;
STDMETHOD(SetCity)(THIS_ BSTR bstrNewVal) PURE;
STDMETHOD(GetState)(THIS_ BSTR* pbstrVal) PURE;
STDMETHOD(SetState)(THIS_ BSTR bstrNewVal) PURE;
STDMETHOD(GetCountryCode)(THIS_ BSTR* pbstrVal) PURE;
STDMETHOD(SetCountryCode)(THIS_ BSTR bstrNewVal) PURE;
STDMETHOD(GetCountryDesc)(THIS_ BSTR* pbstrVal) PURE;
STDMETHOD(SetCountryDesc)(THIS_ BSTR bstrNewVal) PURE;
STDMETHOD(GetZip)(THIS_ BSTR* pbstrVal) PURE;
STDMETHOD(SetZip)(THIS_ BSTR bstrNewVal) PURE;
STDMETHOD(GetIsoLanguage)(THIS_ ULONG* pdwVal) PURE;
STDMETHOD(SetIsoLanguage)(THIS_ ULONG dwNewVal) PURE;
STDMETHOD(GetMSUpdate)(THIS_ BSTR* pbstrVal) PURE;
STDMETHOD(SetMSUpdate)(THIS_ BSTR bstrNewVal) PURE;
STDMETHOD(GetMSOffer)(THIS_ BSTR* pbstrVal) PURE;
STDMETHOD(SetMSOffer)(THIS_ BSTR bstrNewVal) PURE;
STDMETHOD(GetOtherOffer)(THIS_ BSTR* pbstrVal) PURE;
STDMETHOD(SetOtherOffer)(THIS_ BSTR bstrNewVal) PURE;
STDMETHOD(GetAddress2)(THIS_ BSTR* pbstrVal) PURE;
STDMETHOD(SetAddress2)(THIS_ BSTR bstrNewVal) PURE;
STDMETHOD(AsyncProcessHandshakeRequest)(THIS_ LONG bReviseCustInfo) PURE;
STDMETHOD(AsyncProcessNewLicenseRequest)() PURE;
STDMETHOD(AsyncProcessReissueLicenseRequest)() PURE;
STDMETHOD(AsyncProcessReviseCustInfoRequest)() PURE;
STDMETHOD(GetAsyncProcessReturnCode)(THIS_ ULONG* pdwRetCode) PURE;
STDMETHOD(AsyncProcessDroppedLicenseRequest)() PURE;
STDMETHOD(GenerateInstallationId)(THIS_ BSTR* pbstrVal) PURE;
STDMETHOD(DepositConfirmationId)(THIS_ BSTR bstrVal, ULONG* pdwRetCode) PURE;
STDMETHOD(GetExpirationInfo)(THIS_ ULONG* pdwWPALeft, ULONG* pdwEvalLeft) PURE;
STDMETHOD(AsyncProcessRegistrationRequest)() PURE;
STDMETHOD(ProcessHandshakeRequest)(THIS_ LONG bReviseCustInfo) PURE;
STDMETHOD(ProcessNewLicenseRequest)() PURE;
STDMETHOD(ProcessDroppedLicenseRequest)() PURE;
STDMETHOD(ProcessReissueLicenseRequest)() PURE;
STDMETHOD(ProcessReviseCustInfoRequest)() PURE;
STDMETHOD(EnsureInternetConnection)() PURE;
STDMETHOD(SetProductKey)(THIS_ LPWSTR pszNewProductKey) PURE;
STDMETHOD(GetProductID)(THIS_ BSTR* pbstrVal) PURE;
STDMETHOD(VerifyCheckDigits)(THIS_ BSTR bstrCIDIID, LONG* pbValue) PURE;
};
// Static variables
static BOOL XP_ComInitialized = FALSE;
static ICOMLicenseAgent* XP_LicenseAgent = nullptr;
static wchar_t LastErrorMessage[1024] = L"";
// Helper functions
static void SetLastErrorMessage(const wchar_t* message) {
wcscpy(LastErrorMessage, message);
}
static BOOL XP_LoadLicenseManager() {
if (!XP_ComInitialized) {
const HRESULT status = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
if (FAILED(status)) {
SetLastErrorMessage(L"Failed to initialize COM");
return FALSE;
}
XP_ComInitialized = TRUE;
}
if (!XP_LicenseAgent) {
HRESULT status = CoCreateInstance(XP_CLSID, nullptr, CLSCTX_INPROC_SERVER, XP_IID, reinterpret_cast<void**>(&XP_LicenseAgent));
if (SUCCEEDED(status)) {
ULONG dwRetCode;
status = XP_LicenseAgent->Initialize(0xC475, 3, nullptr, &dwRetCode);
if (SUCCEEDED(status) && dwRetCode == 0) {
return TRUE;
}
XP_LicenseAgent->Release();
XP_LicenseAgent = nullptr;
}
SetLastErrorMessage(L"Failed to create license manager instance");
return FALSE;
}
return TRUE;
}
// Helper function to verify a confirmation ID chunk
static bool VerifyConfirmationIDChunk(ICOMLicenseAgent* agent, const wchar_t* chunk) {
LONG pbValue = 0;
BSTR bstrChunk = SysAllocString(chunk);
HRESULT status = agent->VerifyCheckDigits(bstrChunk, &pbValue);
SysFreeString(bstrChunk);
return SUCCEEDED(status) && pbValue != 0;
}
struct ProcessHandle {
HANDLE handle;
explicit ProcessHandle(HANDLE h) : handle(h) {}
~ProcessHandle() { if (handle != INVALID_HANDLE_VALUE) CloseHandle(handle); }
operator HANDLE() const { return handle; }
};
bool IsProcessRunning(const wchar_t* processName) {
ProcessHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
if (snapshot == INVALID_HANDLE_VALUE) {
std::cout << "Failed to create process snapshot: " << GetLastError() << std::endl;
return false;
}
PROCESSENTRY32W processEntry{};
processEntry.dwSize = sizeof(PROCESSENTRY32W);
if (!Process32FirstW(snapshot, &processEntry)) {
std::cout << "Failed to retrieve process information: " << GetLastError() << std::endl;
return false;
}
do {
if (wcscmp(processEntry.szExeFile, processName) == 0) {
return true;
}
} while (Process32NextW(snapshot, &processEntry));
return false;
}
bool TerminateProcessByName(const wchar_t* processName) {
ProcessHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
if (snapshot == INVALID_HANDLE_VALUE) {
std::cout << "Failed to create process snapshot: " << GetLastError() << std::endl;
return false;
}
PROCESSENTRY32W processEntry{};
processEntry.dwSize = sizeof(PROCESSENTRY32W);
if (!Process32FirstW(snapshot, &processEntry)) {
std::cout << "Failed to retrieve process information: " << GetLastError() << std::endl;
return false;
}
do {
if (wcscmp(processEntry.szExeFile, processName) == 0) {
ProcessHandle processHandle(OpenProcess(PROCESS_TERMINATE, FALSE, processEntry.th32ProcessID));
if (processHandle == nullptr) {
std::cout << "Failed to open process: " << GetLastError() << std::endl;
return false;
}
if (!TerminateProcess(processHandle, 0)) {
std::cout << "Failed to terminate process: " << GetLastError() << std::endl;
return false;
}
return true;
}
} while (Process32NextW(snapshot, &processEntry));
std::cout << "Process not found: " << processName << std::endl;
return false;
}
// Exported functions implementation
extern "C" {
void XPMGR_Initialize() {
XP_LoadLicenseManager();
}
void XPMGR_Cleanup() {
if (XP_LicenseAgent) {
XP_LicenseAgent->Release();
XP_LicenseAgent = nullptr;
}
if (XP_ComInitialized) {
CoUninitialize();
XP_ComInitialized = FALSE;
}
}
const wchar_t* XPMGR_GetLastError() {
return LastErrorMessage;
}
const wchar_t* XPMGR_GetInstallationID() {
if (!XP_LoadLicenseManager()) {
return XPMGR_GetLastError();
}
BSTR installationID = nullptr;
const HRESULT status = XP_LicenseAgent->GenerateInstallationId(&installationID);
if (FAILED(status) || !installationID) {
SetLastErrorMessage(L"Failed to generate installation ID");
return XPMGR_GetLastError();
}
static wchar_t result[256];
wcscpy(result, installationID);
SysFreeString(installationID);
return result;
}
const wchar_t* XPMGR_SetConfirmationID(const wchar_t* confirmationId) {
if (!XP_LoadLicenseManager()) {
return XPMGR_GetLastError();
}
// Create a working copy and remove dashes
wchar_t cleanCid[256] = {0};
const wchar_t* src = confirmationId;
wchar_t* dst = cleanCid;
while (*src) {
if (*src != L'-') {
*dst++ = *src;
}
src++;
}
*dst = 0;
// Verify each 6-character chunk
size_t len = wcslen(cleanCid);
for (size_t i = 0; i < len; i += 6) {
wchar_t chunk[7] = {0};
wcsncpy(chunk, cleanCid + i, 6);
if (!VerifyConfirmationIDChunk(XP_LicenseAgent, chunk)) {
SetLastErrorMessage(L"Invalid confirmation ID - verification failed");
return XPMGR_GetLastError();
}
}
// Pass the original confirmation ID (with dashes if present) to the API
BSTR bstrConfirmationId = SysAllocString(confirmationId);
ULONG dwRetCode;
const HRESULT status = XP_LicenseAgent->DepositConfirmationId(bstrConfirmationId, &dwRetCode);
SysFreeString(bstrConfirmationId);
if (FAILED(status) || dwRetCode) {
SetLastErrorMessage(L"Failed to deposit confirmation ID");
return XPMGR_GetLastError();
}
// Handle WPA notifier process
if (IsProcessRunning(L"wpabaln.exe")) {
if (!TerminateProcessByName(L"wpabaln.exe")) {
std::cout << "Warning: Failed to terminate WPA notifier process." << std::endl;
}
}
// Clean up activation files
HMODULE hMod = LoadLibraryW(L"setupapi.dll");
if (hMod) {
using InstallHinfSectionFunc = BOOL (WINAPI*)(HWND, HINSTANCE, PCWSTR, INT);
if (auto installHinfSection = reinterpret_cast<InstallHinfSectionFunc>(GetProcAddress(hMod, "InstallHinfSectionW"))) {
SetLastError(0);
const BOOL result = installHinfSection(nullptr, nullptr, L"DEL_OOBE_ACTIVATE 132 syssetup.inf", 132);
const DWORD error = GetLastError();
if (!result && error != 0 && error != 6) { // Ignore error 6 (ERROR_INVALID_HANDLE) as it indicates success
std::cout << "Warning: Failed to remove activation reminders. Error: " << error << std::endl;
std::cout << "You can try to run the following command yourself: " << std::endl;
std::cout << "rundll32 setupapi,InstallHinfSection DEL_OOBE_ACTIVATE 132 syssetup.inf" << std::endl;
}
}
FreeLibrary(hMod);
}
return L"Successfully set confirmation ID";
}
const wchar_t* XPMGR_GetWPALeft() {
if (!XP_LoadLicenseManager()) {
return XPMGR_GetLastError();
}
ULONG dwWPALeft = 0, dwEvalLeft = 0;
const HRESULT status = XP_LicenseAgent->GetExpirationInfo(&dwWPALeft, &dwEvalLeft);
if (FAILED(status)) {
SetLastErrorMessage(L"Failed to get expiration info");
return XPMGR_GetLastError();
}
if (dwWPALeft == 0x7FFFFFFF) {
return L"Windows is activated";
}
static wchar_t result[16];
_ultow(dwWPALeft, result, 10);
return result;
}
const wchar_t* XPMGR_GetEvalLeft() {
if (!XP_LoadLicenseManager()) {
return XPMGR_GetLastError();
}
ULONG dwWPALeft = 0, dwEvalLeft = 0;
const HRESULT status = XP_LicenseAgent->GetExpirationInfo(&dwWPALeft, &dwEvalLeft);
if (FAILED(status)) {
SetLastErrorMessage(L"Failed to get expiration info");
return XPMGR_GetLastError();
}
if (dwEvalLeft == 0x7FFFFFFF) {
return L"Not an evaluation copy";
}
static wchar_t result[16];
_ultow(dwEvalLeft, result, 10);
return result;
}
const wchar_t* XPMGR_SetProductKey(const wchar_t* productKey) {
if (!XP_LoadLicenseManager()) {
return XPMGR_GetLastError();
}
// Validate product key format
std::wregex pattern(L"[2346789BCDFGHJKMPQRTVWXY]{5}-[2346789BCDFGHJKMPQRTVWXY]{5}-[2346789BCDFGHJKMPQRTVWXY]{5}-[2346789BCDFGHJKMPQRTVWXY]{5}-[2346789BCDFGHJKMPQRTVWXY]{5}");
if (!std::regex_match(productKey, pattern)) {
SetLastErrorMessage(L"Invalid product key format");
return XPMGR_GetLastError();
}
const HRESULT status = XP_LicenseAgent->SetProductKey(const_cast<LPWSTR>(productKey));
if (FAILED(status)) {
SetLastErrorMessage(L"Failed to set product key");
return XPMGR_GetLastError();
}
return L"Successfully set product key";
}
const wchar_t* XPMGR_GetProductID() {
if (!XP_LoadLicenseManager()) {
return XPMGR_GetLastError();
}
BSTR productID = nullptr;
const HRESULT status = XP_LicenseAgent->GetProductID(&productID);
if (FAILED(status) || !productID) {
SetLastErrorMessage(L"Failed to get product ID");
return XPMGR_GetLastError();
}
static wchar_t result[256];
wcscpy(result, productID);
SysFreeString(productID);
return result;
}
}