forked from rileytestut/AltServer-Windows
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAnisetteDataManager.cpp
More file actions
executable file
·576 lines (439 loc) · 17.4 KB
/
Copy pathAnisetteDataManager.cpp
File metadata and controls
executable file
·576 lines (439 loc) · 17.4 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#include "AnisetteDataManager.h"
#include <WinSock2.h>
#include <sstream>
#include <Psapi.h>
#include <filesystem>
#include <ShlObj_core.h>
#include "Error.hpp"
#include "ServerError.hpp"
#include <set>
#include "AnisetteData.h"
#include "AltServerApp.h"
//#define SPOOF_MAC 1
#define id void*
#define SEL void*
typedef id(__cdecl* GETCLASSFUNC)(const char *name);
typedef id(__cdecl* REGISTERSELFUNC)(const char *name);
typedef id(__cdecl* SENDMSGFUNC)(id self, void *_cmd);
typedef id(__cdecl* SENDMSGFUNC_OBJ)(id self, void* _cmd, id parameter1);
typedef id(__cdecl* SENDMSGFUNC_INT)(id self, void* _cmd, int parameter1);
typedef id*(__cdecl* COPYMETHODLISTFUNC)(id cls, unsigned int* outCount);
typedef id(__cdecl* GETMETHODNAMEFUNC)(id method);
typedef const char*(__cdecl* GETSELNAMEFUNC)(SEL sel);
typedef id(__cdecl* GETOBJCCLASSFUNC)(id obj);
typedef id(__cdecl* GETOBJECTFUNC)();
typedef id(__cdecl* CLIENTINFOFUNC)(id obj);
typedef id(__cdecl* COPYANISETTEDATAFUNC)(void *, int, void *);
#define odslog(msg) { std::stringstream ss; ss << msg << std::endl; OutputDebugStringA(ss.str().c_str()); }
extern std::string StringFromWideString(std::wstring wideString);
namespace fs = std::filesystem;
GETCLASSFUNC objc_getClass;
REGISTERSELFUNC sel_registerName;
SENDMSGFUNC objc_msgSend;
COPYMETHODLISTFUNC class_copyMethodList;
GETMETHODNAMEFUNC method_getName;
GETSELNAMEFUNC sel_getName;
GETOBJCCLASSFUNC object_getClass;
GETOBJECTFUNC GetDeviceID;
GETOBJECTFUNC GetLocalUserID;
CLIENTINFOFUNC GetClientInfo;
COPYANISETTEDATAFUNC CopyAnisetteData;
class ObjcObject
{
public:
id isa;
std::string description() const
{
id descriptionSEL = sel_registerName("description");
id descriptionNSString = objc_msgSend((void*)this, descriptionSEL);
id cDescriptionSEL = sel_registerName("UTF8String");
const char* cDescription = ((const char* (*)(id, SEL))objc_msgSend)(descriptionNSString, cDescriptionSEL);
std::string description(cDescription);
return description;
}
};
id __cdecl ALTClientInfoReplacementFunction(void*)
{
ObjcObject* NSString = (ObjcObject*)objc_getClass("NSString");
id stringInit = sel_registerName("stringWithUTF8String:");
ObjcObject* clientInfo = (ObjcObject*)((id(*)(id, SEL, const char*))objc_msgSend)(NSString, stringInit, "<MacBookPro15,1> <macOS;13.2;22D49> <com.apple.AuthKit/1 (com.apple.dt.Xcode/3594.4.19)>");
odslog("Swizzled Client Info: " << clientInfo->description());
return clientInfo;
}
id __cdecl ALTDeviceIDReplacementFunction()
{
ObjcObject* NSString = (ObjcObject*)objc_getClass("NSString");
id stringInit = sel_registerName("stringWithUTF8String:");
auto deviceIDString = AltServerApp::instance()->serverID();
ObjcObject* deviceID = (ObjcObject*)((id(*)(id, SEL, const char*))objc_msgSend)(NSString, stringInit, deviceIDString.c_str());
odslog("Swizzled Device ID: " << deviceID->description());
return deviceID;
}
void convert_filetime(struct timeval* out_tv, const FILETIME* filetime)
{
// Microseconds between 1601-01-01 00:00:00 UTC and 1970-01-01 00:00:00 UTC
static const uint64_t EPOCH_DIFFERENCE_MICROS = 11644473600000000ull;
// First convert 100-ns intervals to microseconds, then adjust for the
// epoch difference
uint64_t total_us = (((uint64_t)filetime->dwHighDateTime << 32) | (uint64_t)filetime->dwLowDateTime) / 10;
total_us -= EPOCH_DIFFERENCE_MICROS;
// Convert to (seconds, microseconds)
out_tv->tv_sec = (time_t)(total_us / 1000000);
out_tv->tv_usec = (long)(total_us % 1000000);
}
AnisetteDataManager* AnisetteDataManager::_instance = nullptr;
AnisetteDataManager* AnisetteDataManager::instance()
{
if (_instance == 0)
{
_instance = new AnisetteDataManager();
}
return _instance;
}
AnisetteDataManager::AnisetteDataManager() : loadedDependencies(false)
{
}
AnisetteDataManager::~AnisetteDataManager()
{
}
#define JUMP_INSTRUCTION_SIZE 5 // 0x86 jump instruction is 5 bytes total (opcode + 4 byte address).
bool AnisetteDataManager::LoadiCloudDependencies()
{
wchar_t* programFilesCommonDirectory;
SHGetKnownFolderPath(FOLDERID_ProgramFilesCommon, 0, NULL, &programFilesCommonDirectory);
fs::path appleDirectoryPath(programFilesCommonDirectory);
appleDirectoryPath.append("Apple");
fs::path internetServicesDirectoryPath(appleDirectoryPath);
internetServicesDirectoryPath.append("Internet Services");
fs::path iCloudMainPath(internetServicesDirectoryPath);
iCloudMainPath.append("iCloud_main.dll");
HINSTANCE iCloudMain = LoadLibrary(iCloudMainPath.c_str());
if (iCloudMain == NULL)
{
return false;
}
// Retrieve known exported function address to provide reference point for accessing private functions.
uintptr_t exportedFunctionAddress = (uintptr_t)GetProcAddress(iCloudMain, "PL_FreeArenaPool");
size_t exportedFunctionDisassembledOffset = 0x1aa2a0;
/* Reprovision Anisette Function */
size_t anisetteFunctionDisassembledOffset = 0x241ee0;
size_t difference = anisetteFunctionDisassembledOffset - 0x1aa2a0;
CopyAnisetteData = (COPYANISETTEDATAFUNC)(exportedFunctionAddress + difference);
if (CopyAnisetteData == NULL)
{
return false;
}
/* Anisette Data Functions */
size_t clientInfoFunctionDisassembledOffset = 0x23e730;
size_t clientInfoFunctionRelativeOffset = clientInfoFunctionDisassembledOffset - exportedFunctionDisassembledOffset;
GetClientInfo = (CLIENTINFOFUNC)(exportedFunctionAddress + clientInfoFunctionRelativeOffset);
size_t deviceIDFunctionDisassembledOffset = 0x23d8b0;
size_t deviceIDFunctionRelativeOffset = deviceIDFunctionDisassembledOffset - exportedFunctionDisassembledOffset;
GetDeviceID = (GETOBJECTFUNC)(exportedFunctionAddress + deviceIDFunctionRelativeOffset);
size_t localUserIDFunctionDisassembledOffset = 0x23db30;
size_t localUserIDFunctionRelativeOffset = localUserIDFunctionDisassembledOffset - exportedFunctionDisassembledOffset;
GetLocalUserID = (GETOBJECTFUNC)(exportedFunctionAddress + localUserIDFunctionRelativeOffset);
if (GetClientInfo == NULL || GetDeviceID == NULL || GetLocalUserID == NULL)
{
return false;
}
{
/** Client Info Swizzling */
int64_t* targetFunction = (int64_t*)GetClientInfo;
int64_t* replacementFunction = (int64_t*)& ALTClientInfoReplacementFunction;
SYSTEM_INFO system;
GetSystemInfo(&system);
int pageSize = system.dwAllocationGranularity;
uintptr_t startAddress = (uintptr_t)targetFunction;
uintptr_t endAddress = startAddress + 1;
uintptr_t pageStart = startAddress & -pageSize;
// Mark page containing the target function implementation as writable so we can inject our own instruction.
DWORD permissions = 0;
BOOL value = VirtualProtect((LPVOID)pageStart, endAddress - pageStart, PAGE_EXECUTE_READWRITE, &permissions);
if (!value)
{
return false;
}
int32_t jumpOffset = (int64_t)replacementFunction - ((int64_t)targetFunction + JUMP_INSTRUCTION_SIZE); // Add jumpInstructionSize because offset is relative to _next_ instruction.
// Construct jump instruction.
// Jump doesn't return execution to target function afterwards, allowing us to completely replace the implementation.
char instruction[5];
instruction[0] = '\xE9'; // E9 = "Jump near (relative)" opcode
((int32_t*)(instruction + 1))[0] = jumpOffset; // Next 4 bytes = jump offset
// Replace first instruction in target target function with our unconditional jump to replacement function.
char* functionImplementation = (char*)targetFunction;
for (int i = 0; i < JUMP_INSTRUCTION_SIZE; i++)
{
functionImplementation[i] = instruction[i];
}
}
{
/** Device ID Swizzling */
int64_t* targetFunction = (int64_t*)GetDeviceID;
int64_t* replacementFunction = (int64_t*)& ALTDeviceIDReplacementFunction;
SYSTEM_INFO system;
GetSystemInfo(&system);
int pageSize = system.dwAllocationGranularity;
uintptr_t startAddress = (uintptr_t)targetFunction;
uintptr_t endAddress = startAddress + 1;
uintptr_t pageStart = startAddress & -pageSize;
// Mark page containing the target function implementation as writable so we can inject our own instruction.
DWORD permissions = 0;
BOOL value = VirtualProtect((LPVOID)pageStart, endAddress - pageStart, PAGE_EXECUTE_READWRITE, &permissions);
if (!value)
{
return false;
}
int32_t jumpOffset = (int64_t)replacementFunction - ((int64_t)targetFunction + JUMP_INSTRUCTION_SIZE); // Add jumpInstructionSize because offset is relative to _next_ instruction.
// Construct jump instruction.
// Jump doesn't return execution to target function afterwards, allowing us to completely replace the implementation.
char instruction[5];
instruction[0] = '\xE9'; // E9 = "Jump near (relative)" opcode
((int32_t*)(instruction + 1))[0] = jumpOffset; // Next 4 bytes = jump offset
// Replace first instruction in target target function with our unconditional jump to replacement function.
char* functionImplementation = (char*)targetFunction;
for (int i = 0; i < JUMP_INSTRUCTION_SIZE; i++)
{
functionImplementation[i] = instruction[i];
}
}
}
bool AnisetteDataManager::LoadDependencies()
{
fs::path appleFolderPath(AltServerApp::instance()->appleFolderPath());
if (!fs::exists(appleFolderPath))
{
throw AnisetteError(AnisetteErrorCode::iTunesNotInstalled);
}
fs::path internetServicesDirectoryPath(AltServerApp::instance()->internetServicesFolderPath());
if (!fs::exists(internetServicesDirectoryPath))
{
throw AnisetteError(AnisetteErrorCode::iCloudNotInstalled);
}
fs::path aosKitPath(internetServicesDirectoryPath);
aosKitPath.append("AOSKit.dll");
if (!fs::exists(aosKitPath))
{
throw AnisetteError(AnisetteErrorCode::MissingAOSKit);
}
fs::path applicationSupportDirectoryPath(AltServerApp::instance()->applicationSupportFolderPath());
if (!fs::exists(applicationSupportDirectoryPath))
{
throw AnisetteError(AnisetteErrorCode::MissingApplicationSupportFolder);
}
fs::path objcPath(applicationSupportDirectoryPath);
objcPath.append("objc.dll");
if (!fs::exists(objcPath))
{
throw AnisetteError(AnisetteErrorCode::MissingObjc);
}
fs::path foundationPath(applicationSupportDirectoryPath);
foundationPath.append("Foundation.dll");
if (!fs::exists(foundationPath))
{
throw AnisetteError(AnisetteErrorCode::MissingFoundation);
}
BOOL result = SetCurrentDirectory(applicationSupportDirectoryPath.c_str());
DWORD dwError = GetLastError();
HINSTANCE objcLibrary = LoadLibrary(objcPath.c_str());
HINSTANCE foundationLibrary = LoadLibrary(foundationPath.c_str());
HINSTANCE AOSKit = LoadLibrary(aosKitPath.c_str());
dwError = GetLastError();
if (objcLibrary == NULL || AOSKit == NULL || foundationLibrary == NULL)
{
char buffer[256];
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, 256, NULL);
throw AnisetteError(AnisetteErrorCode::InvalidiTunesInstallation, { {NSLocalizedDescriptionKey, buffer} });
}
/* Objective-C runtime functions */
objc_getClass = (GETCLASSFUNC)GetProcAddress(objcLibrary, "objc_getClass");
sel_registerName = (REGISTERSELFUNC)GetProcAddress(objcLibrary, "sel_registerName");
objc_msgSend = (SENDMSGFUNC)GetProcAddress(objcLibrary, "objc_msgSend");
class_copyMethodList = (COPYMETHODLISTFUNC)GetProcAddress(objcLibrary, "class_copyMethodList");
method_getName = (GETMETHODNAMEFUNC)GetProcAddress(objcLibrary, "method_getName");
sel_getName = (GETSELNAMEFUNC)GetProcAddress(objcLibrary, "sel_getName");
object_getClass = (GETOBJCCLASSFUNC)GetProcAddress(objcLibrary, "object_getClass");
if (objc_getClass == NULL)
{
throw AnisetteError(AnisetteErrorCode::InvalidiTunesInstallation);
}
#if SPOOF_MAC
if (!this->LoadiCloudDependencies())
{
return false;
}
#endif
this->loadedDependencies = true;
return true;
}
std::shared_ptr<AnisetteData> AnisetteDataManager::FetchAnisetteData()
{
if (!this->loadedDependencies)
{
this->LoadDependencies();
}
#if SPOOF_MAC
if (GetClientInfo == NULL || GetDeviceID == NULL || GetLocalUserID == NULL)
{
return NULL;
}
#endif
std::shared_ptr<AnisetteData> anisetteData = NULL;
this->ReprovisionDevice([&anisetteData]() {
// Device is temporarily provisioned as a Mac, so access anisette data now.
ObjcObject* NSString = (ObjcObject*)objc_getClass("NSString");
id stringInit = sel_registerName("stringWithUTF8String:");
/* One-Time Pasword */
ObjcObject* dsidString = (ObjcObject*)((id(*)(id, SEL, const char*))objc_msgSend)(NSString, stringInit, "-2");
ObjcObject* machineIDKey = (ObjcObject*)((id(*)(id, SEL, const char*))objc_msgSend)(NSString, stringInit, "X-Apple-MD-M");
ObjcObject* otpKey = (ObjcObject*)((id(*)(id, SEL, const char*))objc_msgSend)(NSString, stringInit, "X-Apple-MD");
ObjcObject* AOSUtilities = (ObjcObject*)objc_getClass("AOSUtilities");
ObjcObject* headers = (ObjcObject*)((id(*)(id, SEL, id))objc_msgSend)(AOSUtilities, sel_registerName("retrieveOTPHeadersForDSID:"), dsidString);
ObjcObject* machineID = (ObjcObject*)((id(*)(id, SEL, id))objc_msgSend)(headers, sel_registerName("objectForKey:"), machineIDKey);
ObjcObject* otp = (ObjcObject*)((id(*)(id, SEL, id))objc_msgSend)(headers, sel_registerName("objectForKey:"), otpKey);
if (otp == NULL || machineID == NULL)
{
return;
}
odslog("OTP: " << otp->description() << " MachineID: " << machineID->description());
/* Device Hardware */
ObjcObject* deviceDescription = (ObjcObject*)ALTClientInfoReplacementFunction(NULL);
ObjcObject* deviceID = (ObjcObject*)ALTDeviceIDReplacementFunction();
if (deviceDescription == NULL || deviceID == NULL)
{
return;
}
#if SPOOF_MAC
ObjcObject* localUserID = (ObjcObject*)GetLocalUserID();
#else
std::string description = deviceID->description();
std::vector<unsigned char> deviceIDData(description.begin(), description.end());
auto encodedDeviceID = StringFromWideString(utility::conversions::to_base64(deviceIDData));
ObjcObject* localUserID = (ObjcObject*)((id(*)(id, SEL, const char*))objc_msgSend)(NSString, stringInit, encodedDeviceID.c_str());
#endif
std::string deviceSerialNumber = "C02LKHBBFD57";
if (localUserID == NULL)
{
return;
}
FILETIME systemTime;
GetSystemTimeAsFileTime(&systemTime);
TIMEVAL date;
convert_filetime(&date, &systemTime);
anisetteData = std::make_shared<AnisetteData>(
machineID->description(),
otp->description(),
localUserID->description(),
17106176,
deviceID->description(),
deviceSerialNumber,
deviceDescription->description(),
date,
"en_US",
"PST");
odslog(*anisetteData);
});
return anisetteData;
}
bool AnisetteDataManager::ReprovisionDevice(std::function<void(void)> provisionCallback)
{
#if !SPOOF_MAC
provisionCallback();
return true;
#else
std::string adiDirectoryPath = "C:\\ProgramData\\Apple Computer\\iTunes\\adi";
/* Start Provisioning */
// Move iCloud's ADI files (so we don't mess with them).
for (const auto& entry : fs::directory_iterator(adiDirectoryPath))
{
if (entry.path().extension() == ".pb")
{
fs::path backupPath = entry.path();
backupPath += ".icloud";
fs::rename(entry.path(), backupPath);
}
}
// Copy existing AltServer .pb files into original location to reuse the MID.
for (const auto& entry : fs::directory_iterator(adiDirectoryPath))
{
if (entry.path().extension() == ".altserver")
{
fs::path path = entry.path();
path.replace_extension();
fs::rename(entry.path(), path);
}
}
auto cleanUp = [adiDirectoryPath]() {
/* Finish Provisioning */
// Backup AltServer ADI files.
for (const auto& entry : fs::directory_iterator(adiDirectoryPath))
{
// Backup AltStore file
if (entry.path().extension() == ".pb")
{
fs::path backupPath = entry.path();
backupPath += ".altserver";
fs::rename(entry.path(), backupPath);
}
}
// Copy iCloud ADI files back to original location.
for (const auto& entry : fs::directory_iterator(adiDirectoryPath))
{
if (entry.path().extension() == ".icloud")
{
// Move backup file to original location
fs::path path = entry.path();
path.replace_extension();
fs::rename(entry.path(), path);
odslog("Copying iCloud file from: " << entry.path().string() << " to: " << path.string());
}
}
};
// Calling CopyAnisetteData implicitly generates new anisette data,
// using the new client info string we injected.
ObjcObject* error = NULL;
ObjcObject* anisetteDictionary = (ObjcObject*)CopyAnisetteData(NULL, 0x1, &error);
try
{
if (anisetteDictionary == NULL)
{
odslog("Reprovision Error:" << ((ObjcObject*)error)->description());
ObjcObject* localizedDescription = (ObjcObject*)((id(*)(id, SEL))objc_msgSend)(error, sel_registerName("localizedDescription"));
if (localizedDescription)
{
int errorCode = ((int(*)(id, SEL))objc_msgSend)(error, sel_registerName("code"));
throw LocalizedError(errorCode, localizedDescription->description());
}
else
{
throw ServerError(ServerErrorCode::InvalidAnisetteData);
}
}
odslog("Reprovisioned Anisette:" << anisetteDictionary->description());
AltServerApp::instance()->setReprovisionedDevice(true);
// Call callback while machine is provisioned for AltServer.
provisionCallback();
}
catch (std::exception &exception)
{
cleanUp();
throw;
}
cleanUp();
return true;
#endif
}
bool AnisetteDataManager::ResetProvisioning()
{
std::string adiDirectoryPath = "C:\\ProgramData\\Apple Computer\\iTunes\\adi";
// Remove existing AltServer .pb files so we can create new ones next time we provision this device.
for (const auto& entry : fs::directory_iterator(adiDirectoryPath))
{
if (entry.path().extension() == ".altserver")
{
fs::remove(entry.path());
}
}
return true;
}